2 * QEMU Parallel PORT emulation
4 * Copyright (c) 2003-2005 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 */
56 static void parallel_update_irq(ParallelState
*s
)
59 pic_set_irq(s
->irq
, 1);
61 pic_set_irq(s
->irq
, 0);
64 static void parallel_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
66 ParallelState
*s
= opaque
;
70 printf("parallel: write addr=0x%02x val=0x%02x\n", addr
, val
);
76 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &s
->data
);
79 parallel_update_irq(s
);
85 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &s
->control
);
87 if ((val
& PARA_CTR_INIT
) == 0 ) {
88 s
->status
= PARA_STS_BUSY
;
89 s
->status
|= PARA_STS_ACK
;
90 s
->status
|= PARA_STS_ONLINE
;
91 s
->status
|= PARA_STS_ERROR
;
93 else if (val
& PARA_CTR_SELECT
) {
94 if (val
& PARA_CTR_STROBE
) {
95 s
->status
&= ~PARA_STS_BUSY
;
96 if ((s
->control
& PARA_CTR_STROBE
) == 0)
97 qemu_chr_write(s
->chr
, &s
->data
, 1);
99 if (s
->control
& PARA_CTR_INTEN
) {
104 parallel_update_irq(s
);
111 static uint32_t parallel_ioport_read(void *opaque
, uint32_t addr
)
113 ParallelState
*s
= opaque
;
120 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_DATA
, &s
->data
);
126 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &s
->status
);
131 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
132 /* XXX Fixme: wait 5 microseconds */
133 if (s
->status
& PARA_STS_ACK
)
134 s
->status
&= ~PARA_STS_ACK
;
136 /* XXX Fixme: wait 5 microseconds */
137 s
->status
|= PARA_STS_ACK
;
138 s
->status
|= PARA_STS_BUSY
;
141 parallel_update_irq(s
);
146 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &s
->control
);
151 #ifdef DEBUG_PARALLEL
152 printf("parallel: read addr=0x%02x val=0x%02x\n", addr
, ret
);
157 /* If fd is zero, it means that the parallel device uses the console */
158 ParallelState
*parallel_init(int base
, int irq
, CharDriverState
*chr
)
163 s
= qemu_mallocz(sizeof(ParallelState
));
168 if (qemu_chr_ioctl(chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0)
173 s
->status
= PARA_STS_BUSY
;
174 s
->status
|= PARA_STS_ACK
;
175 s
->status
|= PARA_STS_ONLINE
;
176 s
->status
|= PARA_STS_ERROR
;
177 s
->control
= PARA_CTR_SELECT
;
178 s
->control
|= PARA_CTR_INIT
;
180 register_ioport_write(base
, 8, 1, parallel_ioport_write
, s
);
181 register_ioport_read(base
, 8, 1, parallel_ioport_read
, s
);