2 * QEMU Parallel PORT emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 //#define DEBUG_PARALLEL
29 * These are the definitions for the Printer Status Register
31 #define PARA_STS_BUSY 0x80 /* Busy complement */
32 #define PARA_STS_ACK 0x40 /* Acknowledge */
33 #define PARA_STS_PAPER 0x20 /* Out of paper */
34 #define PARA_STS_ONLINE 0x10 /* Online */
35 #define PARA_STS_ERROR 0x08 /* Error complement */
38 * These are the definitions for the Printer Control Register
40 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
41 #define PARA_CTR_SELECT 0x08 /* Select In complement */
42 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
43 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
44 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
46 struct ParallelState {
48 uint8_t status; /* read only register */
55 static void parallel_update_irq(ParallelState *s)
58 pic_set_irq(s->irq, 1);
60 pic_set_irq(s->irq, 0);
63 static void parallel_ioport_write(void *opaque, uint32_t addr, uint32_t val)
65 ParallelState *s = opaque;
69 printf("parallel: write addr=0x%02x val=0x%02x\n", addr, val);
74 parallel_update_irq(s);
77 if ((val & PARA_CTR_INIT) == 0 ) {
78 s->status = PARA_STS_BUSY;
79 s->status |= PARA_STS_ACK;
80 s->status |= PARA_STS_ONLINE;
81 s->status |= PARA_STS_ERROR;
83 else if (val & PARA_CTR_SELECT) {
84 if (val & PARA_CTR_STROBE) {
85 s->status &= ~PARA_STS_BUSY;
86 if ((s->control & PARA_CTR_STROBE) == 0)
87 qemu_chr_write(s->chr, &s->data, 1);
89 if (s->control & PARA_CTR_INTEN) {
94 parallel_update_irq(s);
100 static uint32_t parallel_ioport_read(void *opaque, uint32_t addr)
102 ParallelState *s = opaque;
113 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
114 /* XXX Fixme: wait 5 microseconds */
115 if (s->status & PARA_STS_ACK)
116 s->status &= ~PARA_STS_ACK;
118 /* XXX Fixme: wait 5 microseconds */
119 s->status |= PARA_STS_ACK;
120 s->status |= PARA_STS_BUSY;
123 parallel_update_irq(s);
129 #ifdef DEBUG_PARALLEL
130 printf("parallel: read addr=0x%02x val=0x%02x\n", addr, ret);
135 static int parallel_can_receive(ParallelState *s)
140 static void parallel_receive_byte(ParallelState *s, int ch)
144 static int parallel_can_receive1(void *opaque)
146 ParallelState *s = opaque;
147 return parallel_can_receive(s);
150 static void parallel_receive1(void *opaque, const uint8_t *buf, int size)
152 ParallelState *s = opaque;
153 parallel_receive_byte(s, buf[0]);
156 static void parallel_event(void *opaque, int event)
160 /* If fd is zero, it means that the parallel device uses the console */
161 ParallelState *parallel_init(int base, int irq, CharDriverState *chr)
165 s = qemu_mallocz(sizeof(ParallelState));
170 s->status = PARA_STS_BUSY;
171 s->status |= PARA_STS_ACK;
172 s->status |= PARA_STS_ONLINE;
173 s->status |= PARA_STS_ERROR;
174 s->control = PARA_CTR_SELECT;
175 s->control |= PARA_CTR_INIT;
177 register_ioport_write(base, 8, 1, parallel_ioport_write, s);
178 register_ioport_read(base, 8, 1, parallel_ioport_read, s);
180 qemu_chr_add_read_handler(chr, parallel_can_receive1, parallel_receive1, s);
181 qemu_chr_add_event_handler(chr, parallel_event);