2 * QEMU m68k Macintosh VIA device support
4 * Copyright (c) 2011-2018 Laurent Vivier
5 * Copyright (c) 2018 Mark Cave-Ayland
7 * Some parts from hw/misc/macio/cuda.c
9 * Copyright (c) 2004-2007 Fabrice Bellard
10 * Copyright (c) 2007 Jocelyn Mayer
12 * some parts from linux-2.6.29, arch/m68k/include/asm/mac_via.h
14 * This work is licensed under the terms of the GNU GPL, version 2 or later.
15 * See the COPYING file in the top-level directory.
18 #include "qemu/osdep.h"
19 #include "qemu-common.h"
20 #include "migration/vmstate.h"
21 #include "hw/sysbus.h"
23 #include "qemu/timer.h"
24 #include "hw/misc/mac_via.h"
25 #include "hw/misc/mos6522.h"
26 #include "hw/input/adb.h"
27 #include "sysemu/runstate.h"
28 #include "qapi/error.h"
29 #include "qemu/cutils.h"
30 #include "hw/qdev-properties.h"
31 #include "hw/qdev-properties-system.h"
32 #include "sysemu/block-backend.h"
37 * VIAs: There are two in every machine
41 * Not all of these are true post MacII I think.
42 * CSA: probably the ones CHRP marks as 'unused' change purposes
43 * when the IWM becomes the SWIM.
44 * http://www.rs6000.ibm.com/resource/technology/chrpio/via5.mak.html
45 * ftp://ftp.austin.ibm.com/pub/technology/spec/chrp/inwork/CHRP_IORef_1.0.pdf
47 * also, http://developer.apple.com/technotes/hw/hw_09.html claims the
48 * following changes for IIfx:
49 * VIA1A_vSccWrReq not available and that VIA1A_vSync has moved to an IOP.
50 * Also, "All of the functionality of VIA2 has been moved to other chips".
53 #define VIA1A_vSccWrReq 0x80 /*
55 * [CHRP] SCC WREQ: Reflects the state of the
56 * Wait/Request pins from the SCC.
57 * [Macintosh Family Hardware]
58 * as CHRP on SE/30,II,IIx,IIcx,IIci.
59 * on IIfx, "0 means an active request"
61 #define VIA1A_vRev8 0x40 /*
62 * Revision 8 board ???
63 * [CHRP] En WaitReqB: Lets the WaitReq_L
64 * signal from port B of the SCC appear on
65 * the PA7 input pin. Output.
66 * [Macintosh Family] On the SE/30, this
67 * is the bit to flip screen buffers.
68 * 0=alternate, 1=main.
69 * on II,IIx,IIcx,IIci,IIfx this is a bit
70 * for Rev ID. 0=II,IIx, 1=IIcx,IIci,IIfx
72 #define VIA1A_vHeadSel 0x20 /*
73 * Head select for IWM.
75 * [Macintosh Family] "Floppy disk
76 * state-control line SEL" on all but IIfx
78 #define VIA1A_vOverlay 0x10 /*
79 * [Macintosh Family] On SE/30,II,IIx,IIcx
80 * this bit enables the "Overlay" address
81 * map in the address decoders as it is on
82 * reset for mapping the ROM over the reset
83 * vector. 1=use overlay map.
84 * On the IIci,IIfx it is another bit of the
85 * CPU ID: 0=normal IIci, 1=IIci with parity
87 * [CHRP] En WaitReqA: Lets the WaitReq_L
88 * signal from port A of the SCC appear
89 * on the PA7 input pin (CHRP). Output.
90 * [MkLinux] "Drive Select"
91 * (with 0x20 being 'disk head select')
93 #define VIA1A_vSync 0x08 /*
94 * [CHRP] Sync Modem: modem clock select:
95 * 1: select the external serial clock to
96 * drive the SCC's /RTxCA pin.
97 * 0: Select the 3.6864MHz clock to drive
99 * [Macintosh Family] Correct on all but IIfx
103 * Macintosh Family Hardware sez: bits 0-2 of VIA1A are volume control
104 * on Macs which had the PWM sound hardware. Reserved on newer models.
105 * On IIci,IIfx, bits 1-2 are the rest of the CPU ID:
106 * bit 2: 1=IIci, 0=IIfx
107 * bit 1: 1 on both IIci and IIfx.
108 * MkLinux sez bit 0 is 'burnin flag' in this case.
109 * CHRP sez: VIA1A bits 0-2 and 5 are 'unused': if programmed as
110 * inputs, these bits will read 0.
112 #define VIA1A_vVolume 0x07 /* Audio volume mask for PWM */
113 #define VIA1A_CPUID0 0x02 /* CPU id bit 0 on RBV, others */
114 #define VIA1A_CPUID1 0x04 /* CPU id bit 0 on RBV, others */
115 #define VIA1A_CPUID2 0x10 /* CPU id bit 0 on RBV, others */
116 #define VIA1A_CPUID3 0x40 /* CPU id bit 0 on RBV, others */
119 * Info on VIA1B is from Macintosh Family Hardware & MkLinux.
120 * CHRP offers no info.
122 #define VIA1B_vSound 0x80 /*
123 * Sound enable (for compatibility with
124 * PWM hardware) 0=enabled.
125 * Also, on IIci w/parity, shows parity error
128 #define VIA1B_vMystery 0x40 /*
129 * On IIci, parity enable. 0=enabled,1=disabled
130 * On SE/30, vertical sync interrupt enable.
131 * 0=enabled. This vSync interrupt shows up
132 * as a slot $E interrupt.
134 #define VIA1B_vADBS2 0x20 /* ADB state input bit 1 (unused on IIfx) */
135 #define VIA1B_vADBS1 0x10 /* ADB state input bit 0 (unused on IIfx) */
136 #define VIA1B_vADBInt 0x08 /* ADB interrupt 0=interrupt (unused on IIfx)*/
137 #define VIA1B_vRTCEnb 0x04 /* Enable Real time clock. 0=enabled. */
138 #define VIA1B_vRTCClk 0x02 /* Real time clock serial-clock line. */
139 #define VIA1B_vRTCData 0x01 /* Real time clock serial-data line. */
142 * VIA2 A register is the interrupt lines raised off the nubus
144 * The below info is from 'Macintosh Family Hardware.'
145 * MkLinux calls the 'IIci internal video IRQ' below the 'RBV slot 0 irq.'
146 * It also notes that the slot $9 IRQ is the 'Ethernet IRQ' and
147 * defines the 'Video IRQ' as 0x40 for the 'EVR' VIA work-alike.
148 * Perhaps OSS uses vRAM1 and vRAM2 for ADB.
151 #define VIA2A_vRAM1 0x80 /* RAM size bit 1 (IIci: reserved) */
152 #define VIA2A_vRAM0 0x40 /* RAM size bit 0 (IIci: internal video IRQ) */
153 #define VIA2A_vIRQE 0x20 /* IRQ from slot $E */
154 #define VIA2A_vIRQD 0x10 /* IRQ from slot $D */
155 #define VIA2A_vIRQC 0x08 /* IRQ from slot $C */
156 #define VIA2A_vIRQB 0x04 /* IRQ from slot $B */
157 #define VIA2A_vIRQA 0x02 /* IRQ from slot $A */
158 #define VIA2A_vIRQ9 0x01 /* IRQ from slot $9 */
161 * RAM size bits decoded as follows:
162 * bit1 bit0 size of ICs in bank A
170 * Register B has the fun stuff in it
173 #define VIA2B_vVBL 0x80 /*
174 * VBL output to VIA1 (60.15Hz) driven by
176 * on IIci, parity test: 0=test mode.
177 * [MkLinux] RBV_PARODD: 1=odd,0=even.
179 #define VIA2B_vSndJck 0x40 /*
180 * External sound jack status.
181 * 0=plug is inserted. On SE/30, always 0
183 #define VIA2B_vTfr0 0x20 /* Transfer mode bit 0 ack from NuBus */
184 #define VIA2B_vTfr1 0x10 /* Transfer mode bit 1 ack from NuBus */
185 #define VIA2B_vMode32 0x08 /*
186 * 24/32bit switch - doubles as cache flush
187 * on II, AMU/PMMU control.
188 * if AMU, 0=24bit to 32bit translation
189 * if PMMU, 1=PMMU is accessing page table.
191 * on IIx,IIcx,IIfx, unused.
192 * on IIci/RBV, cache control. 0=flush cache.
194 #define VIA2B_vPower 0x04 /*
195 * Power off, 0=shut off power.
196 * on SE/30 this signal sent to PDS card.
198 #define VIA2B_vBusLk 0x02 /*
199 * Lock NuBus transactions, 0=locked.
200 * on SE/30 sent to PDS card.
202 #define VIA2B_vCDis 0x01 /*
203 * Cache control. On IIci, 1=disable cache card
204 * on others, 0=disable processor's instruction
208 /* interrupt flags */
214 #define VIA_IRQ_TIMER1 0x40
215 #define VIA_IRQ_TIMER2 0x20
218 * Apple sez: http://developer.apple.com/technotes/ov/ov_04.html
219 * Another example of a valid function that has no ROM support is the use
220 * of the alternate video page for page-flipping animation. Since there
221 * is no ROM call to flip pages, it is necessary to go play with the
222 * right bit in the VIA chip (6522 Versatile Interface Adapter).
223 * [CSA: don't know which one this is, but it's one of 'em!]
227 * 6522 registers - see databook.
228 * CSA: Assignments for VIA1 confirmed from CHRP spec.
231 /* partial address decode. 0xYYXX : XX part for RBV, YY part for VIA */
232 /* Note: 15 VIA regs, 8 RBV regs */
234 #define vBufB 0x0000 /* [VIA/RBV] Register B */
235 #define vBufAH 0x0200 /* [VIA only] Buffer A, with handshake. DON'T USE! */
236 #define vDirB 0x0400 /* [VIA only] Data Direction Register B. */
237 #define vDirA 0x0600 /* [VIA only] Data Direction Register A. */
238 #define vT1CL 0x0800 /* [VIA only] Timer one counter low. */
239 #define vT1CH 0x0a00 /* [VIA only] Timer one counter high. */
240 #define vT1LL 0x0c00 /* [VIA only] Timer one latches low. */
241 #define vT1LH 0x0e00 /* [VIA only] Timer one latches high. */
242 #define vT2CL 0x1000 /* [VIA only] Timer two counter low. */
243 #define vT2CH 0x1200 /* [VIA only] Timer two counter high. */
244 #define vSR 0x1400 /* [VIA only] Shift register. */
245 #define vACR 0x1600 /* [VIA only] Auxilary control register. */
246 #define vPCR 0x1800 /* [VIA only] Peripheral control register. */
248 * CHRP sez never ever to *write* this.
249 * Mac family says never to *change* this.
250 * In fact we need to initialize it once at start.
252 #define vIFR 0x1a00 /* [VIA/RBV] Interrupt flag register. */
253 #define vIER 0x1c00 /* [VIA/RBV] Interrupt enable register. */
254 #define vBufA 0x1e00 /* [VIA/RBV] register A (no handshake) */
256 /* from linux 2.6 drivers/macintosh/via-macii.c */
260 #define VIA1ACR_vShiftCtrl 0x1c /* Shift register control bits */
261 #define VIA1ACR_vShiftExtClk 0x0c /* Shift on external clock */
262 #define VIA1ACR_vShiftOut 0x10 /* Shift out if 1 */
265 * Apple Macintosh Family Hardware Refenece
266 * Table 19-10 ADB transaction states
269 #define ADB_STATE_NEW 0
270 #define ADB_STATE_EVEN 1
271 #define ADB_STATE_ODD 2
272 #define ADB_STATE_IDLE 3
274 #define VIA1B_vADB_StateMask (VIA1B_vADBS1 | VIA1B_vADBS2)
275 #define VIA1B_vADB_StateShift 4
277 #define VIA_TIMER_FREQ (783360)
278 #define VIA_ADB_POLL_FREQ 50 /* XXX: not real */
281 * Guide to the Macintosh Family Hardware ch. 12 "Displays" p. 401 gives the
282 * precise 60Hz interrupt frequency as ~60.15Hz with a period of 16625.8 us
284 #define VIA_60HZ_TIMER_PERIOD_NS 16625800
286 /* VIA returns time offset from Jan 1, 1904, not 1970 */
287 #define RTC_OFFSET 2082844800
297 REG_PRAM_ADDR_LAST
= REG_PRAM_ADDR
+ 19,
299 REG_PRAM_SECT_LAST
= REG_PRAM_SECT
+ 7,
304 static void via1_sixty_hz_update(MOS6522Q800VIA1State
*v1s
)
307 v1s
->next_sixty_hz
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
308 VIA_60HZ_TIMER_PERIOD_NS
) /
309 VIA_60HZ_TIMER_PERIOD_NS
* VIA_60HZ_TIMER_PERIOD_NS
;
310 timer_mod(v1s
->sixty_hz_timer
, v1s
->next_sixty_hz
);
313 static void via1_one_second_update(MOS6522Q800VIA1State
*v1s
)
315 v1s
->next_second
= (qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 1000) /
317 timer_mod(v1s
->one_second_timer
, v1s
->next_second
);
320 static void via1_sixty_hz(void *opaque
)
322 MOS6522Q800VIA1State
*v1s
= opaque
;
323 MOS6522State
*s
= MOS6522(v1s
);
324 MOS6522DeviceClass
*mdc
= MOS6522_GET_CLASS(s
);
326 s
->ifr
|= VIA1_IRQ_60HZ
;
329 via1_sixty_hz_update(v1s
);
332 static void via1_one_second(void *opaque
)
334 MOS6522Q800VIA1State
*v1s
= opaque
;
335 MOS6522State
*s
= MOS6522(v1s
);
336 MOS6522DeviceClass
*mdc
= MOS6522_GET_CLASS(s
);
338 s
->ifr
|= VIA1_IRQ_ONE_SECOND
;
341 via1_one_second_update(v1s
);
344 static void via1_irq_request(void *opaque
, int irq
, int level
)
346 MOS6522Q800VIA1State
*v1s
= opaque
;
347 MOS6522State
*s
= MOS6522(v1s
);
348 MOS6522DeviceClass
*mdc
= MOS6522_GET_CLASS(s
);
353 s
->ifr
&= ~(1 << irq
);
359 static void via2_irq_request(void *opaque
, int irq
, int level
)
361 MOS6522Q800VIA2State
*v2s
= opaque
;
362 MOS6522State
*s
= MOS6522(v2s
);
363 MOS6522DeviceClass
*mdc
= MOS6522_GET_CLASS(s
);
368 s
->ifr
&= ~(1 << irq
);
375 static void pram_update(MOS6522Q800VIA1State
*v1s
)
378 if (blk_pwrite(v1s
->blk
, 0, v1s
->PRAM
, sizeof(v1s
->PRAM
), 0) < 0) {
379 qemu_log("pram_update: cannot write to file\n");
387 * Command byte Register addressed by the command
389 * z0000001 Seconds register 0 (lowest-order byte)
390 * z0000101 Seconds register 1
391 * z0001001 Seconds register 2
392 * z0001101 Seconds register 3 (highest-order byte)
393 * 00110001 Test register (write-only)
394 * 00110101 Write-Protect Register (write-only)
395 * z010aa01 RAM address 100aa ($10-$13) (first 20 bytes only)
396 * z1aaaa01 RAM address 0aaaa ($00-$0F) (first 20 bytes only)
397 * z0111aaa Extended memory designator and sector number
399 * For a read request, z=1, for a write z=0
400 * The letter a indicates bits whose value depend on what parameter
401 * RAM byte you want to address
403 static int via1_rtc_compact_cmd(uint8_t value
)
405 uint8_t read
= value
& 0x80;
409 /* the last 2 bits of a command byte must always be 0b01 ... */
410 if ((value
& 0x78) == 0x38) {
411 /* except for the extended memory designator */
412 return read
| (REG_PRAM_SECT
+ (value
& 0x07));
414 if ((value
& 0x03) == 0x01) {
416 if ((value
& 0x1c) == 0) {
417 /* seconds registers */
418 return read
| (REG_0
+ (value
& 0x03));
419 } else if ((value
== 0x0c) && !read
) {
421 } else if ((value
== 0x0d) && !read
) {
423 } else if ((value
& 0x1c) == 0x08) {
424 /* RAM address 0x10 to 0x13 */
425 return read
| (REG_PRAM_ADDR
+ 0x10 + (value
& 0x03));
426 } else if ((value
& 0x43) == 0x41) {
427 /* RAM address 0x00 to 0x0f */
428 return read
| (REG_PRAM_ADDR
+ (value
& 0x0f));
434 static void via1_rtc_update(MOS6522Q800VIA1State
*v1s
)
436 MOS6522State
*s
= MOS6522(v1s
);
437 int cmd
, sector
, addr
;
440 if (s
->b
& VIA1B_vRTCEnb
) {
444 if (s
->dirb
& VIA1B_vRTCData
) {
445 /* send bits to the RTC */
446 if (!(v1s
->last_b
& VIA1B_vRTCClk
) && (s
->b
& VIA1B_vRTCClk
)) {
448 v1s
->data_out
|= s
->b
& VIA1B_vRTCData
;
451 trace_via1_rtc_update_data_out(v1s
->data_out_cnt
, v1s
->data_out
);
453 trace_via1_rtc_update_data_in(v1s
->data_in_cnt
, v1s
->data_in
);
454 /* receive bits from the RTC */
455 if ((v1s
->last_b
& VIA1B_vRTCClk
) &&
456 !(s
->b
& VIA1B_vRTCClk
) &&
458 s
->b
= (s
->b
& ~VIA1B_vRTCData
) |
459 ((v1s
->data_in
>> 7) & VIA1B_vRTCData
);
466 if (v1s
->data_out_cnt
!= 8) {
470 v1s
->data_out_cnt
= 0;
472 trace_via1_rtc_internal_status(v1s
->cmd
, v1s
->alt
, v1s
->data_out
);
473 /* first byte: it's a command */
474 if (v1s
->cmd
== REG_EMPTY
) {
476 cmd
= via1_rtc_compact_cmd(v1s
->data_out
);
477 trace_via1_rtc_internal_cmd(cmd
);
479 if (cmd
== REG_INVALID
) {
480 trace_via1_rtc_cmd_invalid(v1s
->data_out
);
484 if (cmd
& 0x80) { /* this is a read command */
485 switch (cmd
& 0x7f) {
486 case REG_0
...REG_3
: /* seconds registers */
488 * register 0 is lowest-order byte
489 * register 3 is highest-order byte
492 time
= v1s
->tick_offset
+ (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)
493 / NANOSECONDS_PER_SECOND
);
494 trace_via1_rtc_internal_time(time
);
495 v1s
->data_in
= (time
>> ((cmd
& 0x03) << 3)) & 0xff;
496 v1s
->data_in_cnt
= 8;
497 trace_via1_rtc_cmd_seconds_read((cmd
& 0x7f) - REG_0
,
500 case REG_PRAM_ADDR
...REG_PRAM_ADDR_LAST
:
501 /* PRAM address 0x00 -> 0x13 */
502 v1s
->data_in
= v1s
->PRAM
[(cmd
& 0x7f) - REG_PRAM_ADDR
];
503 v1s
->data_in_cnt
= 8;
504 trace_via1_rtc_cmd_pram_read((cmd
& 0x7f) - REG_PRAM_ADDR
,
507 case REG_PRAM_SECT
...REG_PRAM_SECT_LAST
:
509 * extended memory designator and sector number
510 * the only two-byte read command
512 trace_via1_rtc_internal_set_cmd(cmd
);
516 g_assert_not_reached();
522 /* this is a write command, needs a parameter */
523 if (cmd
== REG_WPROTECT
|| !v1s
->wprotect
) {
524 trace_via1_rtc_internal_set_cmd(cmd
);
527 trace_via1_rtc_internal_ignore_cmd(cmd
);
532 /* second byte: it's a parameter */
533 if (v1s
->alt
== REG_EMPTY
) {
534 switch (v1s
->cmd
& 0x7f) {
535 case REG_0
...REG_3
: /* seconds register */
537 trace_via1_rtc_cmd_seconds_write(v1s
->cmd
- REG_0
, v1s
->data_out
);
538 v1s
->cmd
= REG_EMPTY
;
541 /* device control: nothing to do */
542 trace_via1_rtc_cmd_test_write(v1s
->data_out
);
543 v1s
->cmd
= REG_EMPTY
;
546 /* Write Protect register */
547 trace_via1_rtc_cmd_wprotect_write(v1s
->data_out
);
548 v1s
->wprotect
= !!(v1s
->data_out
& 0x80);
549 v1s
->cmd
= REG_EMPTY
;
551 case REG_PRAM_ADDR
...REG_PRAM_ADDR_LAST
:
552 /* PRAM address 0x00 -> 0x13 */
553 trace_via1_rtc_cmd_pram_write(v1s
->cmd
- REG_PRAM_ADDR
,
555 v1s
->PRAM
[v1s
->cmd
- REG_PRAM_ADDR
] = v1s
->data_out
;
557 v1s
->cmd
= REG_EMPTY
;
559 case REG_PRAM_SECT
...REG_PRAM_SECT_LAST
:
560 addr
= (v1s
->data_out
>> 2) & 0x1f;
561 sector
= (v1s
->cmd
& 0x7f) - REG_PRAM_SECT
;
562 if (v1s
->cmd
& 0x80) {
564 v1s
->data_in
= v1s
->PRAM
[sector
* 32 + addr
];
565 v1s
->data_in_cnt
= 8;
566 trace_via1_rtc_cmd_pram_sect_read(sector
, addr
,
569 v1s
->cmd
= REG_EMPTY
;
571 /* it's a write, we need one more parameter */
572 trace_via1_rtc_internal_set_alt(addr
, sector
, addr
);
577 g_assert_not_reached();
583 /* third byte: it's the data of a REG_PRAM_SECT write */
584 g_assert(REG_PRAM_SECT
<= v1s
->cmd
&& v1s
->cmd
<= REG_PRAM_SECT_LAST
);
585 sector
= v1s
->cmd
- REG_PRAM_SECT
;
586 v1s
->PRAM
[sector
* 32 + v1s
->alt
] = v1s
->data_out
;
588 trace_via1_rtc_cmd_pram_sect_write(sector
, v1s
->alt
, sector
* 32 + v1s
->alt
,
590 v1s
->alt
= REG_EMPTY
;
591 v1s
->cmd
= REG_EMPTY
;
594 static void adb_via_poll(void *opaque
)
596 MOS6522Q800VIA1State
*v1s
= MOS6522_Q800_VIA1(opaque
);
597 MOS6522State
*s
= MOS6522(v1s
);
598 ADBBusState
*adb_bus
= &v1s
->adb_bus
;
600 uint8_t *data
= &s
->sr
;
604 * Setting vADBInt below indicates that an autopoll reply has been
605 * received, however we must block autopoll until the point where
606 * the entire reply has been read back to the host
608 adb_autopoll_block(adb_bus
);
610 if (v1s
->adb_data_in_size
> 0 && v1s
->adb_data_in_index
== 0) {
612 * For older Linux kernels that switch to IDLE mode after sending the
613 * ADB command, detect if there is an existing response and return that
614 * as a a "fake" autopoll reply or bus timeout accordingly
616 *data
= v1s
->adb_data_out
[0];
617 olen
= v1s
->adb_data_in_size
;
619 s
->b
&= ~VIA1B_vADBInt
;
620 qemu_irq_raise(v1s
->adb_data_ready
);
623 * Otherwise poll as normal
625 v1s
->adb_data_in_index
= 0;
626 v1s
->adb_data_out_index
= 0;
627 olen
= adb_poll(adb_bus
, obuf
, adb_bus
->autopoll_mask
);
630 /* Autopoll response */
633 memcpy(v1s
->adb_data_in
, &obuf
[1], olen
);
634 v1s
->adb_data_in_size
= olen
;
636 s
->b
&= ~VIA1B_vADBInt
;
637 qemu_irq_raise(v1s
->adb_data_ready
);
639 *data
= v1s
->adb_autopoll_cmd
;
644 memcpy(v1s
->adb_data_in
, obuf
, olen
);
645 v1s
->adb_data_in_size
= olen
;
647 s
->b
&= ~VIA1B_vADBInt
;
648 qemu_irq_raise(v1s
->adb_data_ready
);
652 trace_via1_adb_poll(*data
, (s
->b
& VIA1B_vADBInt
) ? "+" : "-",
653 adb_bus
->status
, v1s
->adb_data_in_index
, olen
);
656 static int adb_via_send_len(uint8_t data
)
658 /* Determine the send length from the given ADB command */
659 uint8_t cmd
= data
& 0xc;
660 uint8_t reg
= data
& 0x3;
667 /* Register 2 is only used for the keyboard */
671 * Fortunately our devices only implement writes
672 * to register 3 which is fixed at 2 bytes
676 qemu_log_mask(LOG_UNIMP
, "ADB unknown length for register %d\n",
686 static void adb_via_send(MOS6522Q800VIA1State
*v1s
, int state
, uint8_t data
)
688 MOS6522State
*ms
= MOS6522(v1s
);
689 ADBBusState
*adb_bus
= &v1s
->adb_bus
;
690 uint16_t autopoll_mask
;
695 * Command byte: vADBInt tells host autopoll data already present
696 * in VIA shift register and ADB transceiver
698 adb_autopoll_block(adb_bus
);
700 if (adb_bus
->status
& ADB_STATUS_POLLREPLY
) {
701 /* Tell the host the existing data is from autopoll */
702 ms
->b
&= ~VIA1B_vADBInt
;
704 ms
->b
|= VIA1B_vADBInt
;
705 v1s
->adb_data_out_index
= 0;
706 v1s
->adb_data_out
[v1s
->adb_data_out_index
++] = data
;
709 trace_via1_adb_send(" NEW", data
, (ms
->b
& VIA1B_vADBInt
) ? "+" : "-");
710 qemu_irq_raise(v1s
->adb_data_ready
);
715 ms
->b
|= VIA1B_vADBInt
;
716 v1s
->adb_data_out
[v1s
->adb_data_out_index
++] = data
;
718 trace_via1_adb_send(state
== ADB_STATE_EVEN
? "EVEN" : " ODD",
719 data
, (ms
->b
& VIA1B_vADBInt
) ? "+" : "-");
720 qemu_irq_raise(v1s
->adb_data_ready
);
727 /* If the command is complete, execute it */
728 if (v1s
->adb_data_out_index
== adb_via_send_len(v1s
->adb_data_out
[0])) {
729 v1s
->adb_data_in_size
= adb_request(adb_bus
, v1s
->adb_data_in
,
731 v1s
->adb_data_out_index
);
732 v1s
->adb_data_in_index
= 0;
734 if (adb_bus
->status
& ADB_STATUS_BUSTIMEOUT
) {
736 * Bus timeout (but allow first EVEN and ODD byte to indicate
737 * timeout via vADBInt and SRQ status)
739 v1s
->adb_data_in
[0] = 0xff;
740 v1s
->adb_data_in
[1] = 0xff;
741 v1s
->adb_data_in_size
= 2;
745 * If last command is TALK, store it for use by autopoll and adjust
746 * the autopoll mask accordingly
748 if ((v1s
->adb_data_out
[0] & 0xc) == 0xc) {
749 v1s
->adb_autopoll_cmd
= v1s
->adb_data_out
[0];
751 autopoll_mask
= 1 << (v1s
->adb_autopoll_cmd
>> 4);
752 adb_set_autopoll_mask(adb_bus
, autopoll_mask
);
757 static void adb_via_receive(MOS6522Q800VIA1State
*v1s
, int state
, uint8_t *data
)
759 MOS6522State
*ms
= MOS6522(v1s
);
760 ADBBusState
*adb_bus
= &v1s
->adb_bus
;
765 ms
->b
|= VIA1B_vADBInt
;
769 ms
->b
|= VIA1B_vADBInt
;
770 adb_autopoll_unblock(adb_bus
);
772 trace_via1_adb_receive("IDLE", *data
,
773 (ms
->b
& VIA1B_vADBInt
) ? "+" : "-", adb_bus
->status
,
774 v1s
->adb_data_in_index
, v1s
->adb_data_in_size
);
780 switch (v1s
->adb_data_in_index
) {
782 /* First EVEN byte: vADBInt indicates bus timeout */
783 *data
= v1s
->adb_data_in
[v1s
->adb_data_in_index
];
784 if (adb_bus
->status
& ADB_STATUS_BUSTIMEOUT
) {
785 ms
->b
&= ~VIA1B_vADBInt
;
787 ms
->b
|= VIA1B_vADBInt
;
790 trace_via1_adb_receive(state
== ADB_STATE_EVEN
? "EVEN" : " ODD",
791 *data
, (ms
->b
& VIA1B_vADBInt
) ? "+" : "-",
792 adb_bus
->status
, v1s
->adb_data_in_index
,
793 v1s
->adb_data_in_size
);
795 v1s
->adb_data_in_index
++;
799 /* First ODD byte: vADBInt indicates SRQ */
800 *data
= v1s
->adb_data_in
[v1s
->adb_data_in_index
];
801 pending
= adb_bus
->pending
& ~(1 << (v1s
->adb_autopoll_cmd
>> 4));
803 ms
->b
&= ~VIA1B_vADBInt
;
805 ms
->b
|= VIA1B_vADBInt
;
808 trace_via1_adb_receive(state
== ADB_STATE_EVEN
? "EVEN" : " ODD",
809 *data
, (ms
->b
& VIA1B_vADBInt
) ? "+" : "-",
810 adb_bus
->status
, v1s
->adb_data_in_index
,
811 v1s
->adb_data_in_size
);
813 v1s
->adb_data_in_index
++;
818 * Otherwise vADBInt indicates end of data. Note that Linux
819 * specifically checks for the sequence 0x0 0xff to confirm the
820 * end of the poll reply, so provide these extra bytes below to
823 if (v1s
->adb_data_in_index
< v1s
->adb_data_in_size
) {
825 *data
= v1s
->adb_data_in
[v1s
->adb_data_in_index
];
826 ms
->b
|= VIA1B_vADBInt
;
827 } else if (v1s
->adb_data_in_index
== v1s
->adb_data_in_size
) {
828 if (adb_bus
->status
& ADB_STATUS_BUSTIMEOUT
) {
829 /* Bus timeout (no more data) */
832 /* Return 0x0 after reply */
835 ms
->b
&= ~VIA1B_vADBInt
;
837 /* Bus timeout (no more data) */
839 ms
->b
&= ~VIA1B_vADBInt
;
841 adb_autopoll_unblock(adb_bus
);
844 trace_via1_adb_receive(state
== ADB_STATE_EVEN
? "EVEN" : " ODD",
845 *data
, (ms
->b
& VIA1B_vADBInt
) ? "+" : "-",
846 adb_bus
->status
, v1s
->adb_data_in_index
,
847 v1s
->adb_data_in_size
);
849 if (v1s
->adb_data_in_index
<= v1s
->adb_data_in_size
) {
850 v1s
->adb_data_in_index
++;
855 qemu_irq_raise(v1s
->adb_data_ready
);
860 static void via1_adb_update(MOS6522Q800VIA1State
*v1s
)
862 MOS6522State
*s
= MOS6522(v1s
);
865 oldstate
= (v1s
->last_b
& VIA1B_vADB_StateMask
) >> VIA1B_vADB_StateShift
;
866 state
= (s
->b
& VIA1B_vADB_StateMask
) >> VIA1B_vADB_StateShift
;
868 if (state
!= oldstate
) {
869 if (s
->acr
& VIA1ACR_vShiftOut
) {
871 adb_via_send(v1s
, state
, s
->sr
);
874 adb_via_receive(v1s
, state
, &s
->sr
);
879 static uint64_t mos6522_q800_via1_read(void *opaque
, hwaddr addr
, unsigned size
)
881 MOS6522Q800VIA1State
*s
= MOS6522_Q800_VIA1(opaque
);
882 MOS6522State
*ms
= MOS6522(s
);
884 addr
= (addr
>> 9) & 0xf;
885 return mos6522_read(ms
, addr
, size
);
888 static void mos6522_q800_via1_write(void *opaque
, hwaddr addr
, uint64_t val
,
891 MOS6522Q800VIA1State
*v1s
= MOS6522_Q800_VIA1(opaque
);
892 MOS6522State
*ms
= MOS6522(v1s
);
894 addr
= (addr
>> 9) & 0xf;
895 mos6522_write(ms
, addr
, val
, size
);
899 via1_rtc_update(v1s
);
900 via1_adb_update(v1s
);
907 static const MemoryRegionOps mos6522_q800_via1_ops
= {
908 .read
= mos6522_q800_via1_read
,
909 .write
= mos6522_q800_via1_write
,
910 .endianness
= DEVICE_BIG_ENDIAN
,
912 .min_access_size
= 1,
913 .max_access_size
= 4,
917 static uint64_t mos6522_q800_via2_read(void *opaque
, hwaddr addr
, unsigned size
)
919 MOS6522Q800VIA2State
*s
= MOS6522_Q800_VIA2(opaque
);
920 MOS6522State
*ms
= MOS6522(s
);
922 addr
= (addr
>> 9) & 0xf;
923 return mos6522_read(ms
, addr
, size
);
926 static void mos6522_q800_via2_write(void *opaque
, hwaddr addr
, uint64_t val
,
929 MOS6522Q800VIA2State
*s
= MOS6522_Q800_VIA2(opaque
);
930 MOS6522State
*ms
= MOS6522(s
);
932 addr
= (addr
>> 9) & 0xf;
933 mos6522_write(ms
, addr
, val
, size
);
936 static const MemoryRegionOps mos6522_q800_via2_ops
= {
937 .read
= mos6522_q800_via2_read
,
938 .write
= mos6522_q800_via2_write
,
939 .endianness
= DEVICE_BIG_ENDIAN
,
941 .min_access_size
= 1,
942 .max_access_size
= 4,
946 static void via1_postload_update_cb(void *opaque
, bool running
, RunState state
)
948 MOS6522Q800VIA1State
*v1s
= MOS6522_Q800_VIA1(opaque
);
950 qemu_del_vm_change_state_handler(v1s
->vmstate
);
956 static int via1_post_load(void *opaque
, int version_id
)
958 MOS6522Q800VIA1State
*v1s
= MOS6522_Q800_VIA1(opaque
);
961 v1s
->vmstate
= qemu_add_vm_change_state_handler(
962 via1_postload_update_cb
, v1s
);
969 static void mos6522_q800_via1_reset(DeviceState
*dev
)
971 MOS6522Q800VIA1State
*v1s
= MOS6522_Q800_VIA1(dev
);
972 MOS6522State
*ms
= MOS6522(v1s
);
973 MOS6522DeviceClass
*mdc
= MOS6522_GET_CLASS(ms
);
974 ADBBusState
*adb_bus
= &v1s
->adb_bus
;
976 mdc
->parent_reset(dev
);
978 ms
->timers
[0].frequency
= VIA_TIMER_FREQ
;
979 ms
->timers
[1].frequency
= VIA_TIMER_FREQ
;
981 ms
->b
= VIA1B_vADB_StateMask
| VIA1B_vADBInt
| VIA1B_vRTCEnb
;
984 adb_set_autopoll_enabled(adb_bus
, true);
985 v1s
->cmd
= REG_EMPTY
;
986 v1s
->alt
= REG_EMPTY
;
989 static void mos6522_q800_via1_realize(DeviceState
*dev
, Error
**errp
)
991 MOS6522Q800VIA1State
*v1s
= MOS6522_Q800_VIA1(dev
);
992 ADBBusState
*adb_bus
= &v1s
->adb_bus
;
996 v1s
->one_second_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, via1_one_second
,
998 via1_one_second_update(v1s
);
999 v1s
->sixty_hz_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, via1_sixty_hz
,
1001 via1_sixty_hz_update(v1s
);
1003 qemu_get_timedate(&tm
, 0);
1004 v1s
->tick_offset
= (uint32_t)mktimegm(&tm
) + RTC_OFFSET
;
1006 adb_register_autopoll_callback(adb_bus
, adb_via_poll
, v1s
);
1007 v1s
->adb_data_ready
= qdev_get_gpio_in(dev
, VIA1_IRQ_ADB_READY_BIT
);
1010 int64_t len
= blk_getlength(v1s
->blk
);
1012 error_setg_errno(errp
, -len
,
1013 "could not get length of backing image");
1016 ret
= blk_set_perm(v1s
->blk
,
1017 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
,
1018 BLK_PERM_ALL
, errp
);
1023 len
= blk_pread(v1s
->blk
, 0, v1s
->PRAM
, sizeof(v1s
->PRAM
));
1024 if (len
!= sizeof(v1s
->PRAM
)) {
1025 error_setg(errp
, "can't read PRAM contents");
1031 static void mos6522_q800_via1_init(Object
*obj
)
1033 MOS6522Q800VIA1State
*v1s
= MOS6522_Q800_VIA1(obj
);
1034 SysBusDevice
*sbd
= SYS_BUS_DEVICE(v1s
);
1036 memory_region_init_io(&v1s
->via_mem
, obj
, &mos6522_q800_via1_ops
, v1s
,
1038 sysbus_init_mmio(sbd
, &v1s
->via_mem
);
1041 qbus_init((BusState
*)&v1s
->adb_bus
, sizeof(v1s
->adb_bus
),
1042 TYPE_ADB_BUS
, DEVICE(v1s
), "adb.0");
1044 qdev_init_gpio_in(DEVICE(obj
), via1_irq_request
, VIA1_IRQ_NB
);
1047 static const VMStateDescription vmstate_q800_via1
= {
1048 .name
= "q800-via1",
1050 .minimum_version_id
= 0,
1051 .post_load
= via1_post_load
,
1052 .fields
= (VMStateField
[]) {
1053 VMSTATE_STRUCT(parent_obj
, MOS6522Q800VIA1State
, 0, vmstate_mos6522
,
1055 VMSTATE_UINT8(last_b
, MOS6522Q800VIA1State
),
1057 VMSTATE_BUFFER(PRAM
, MOS6522Q800VIA1State
),
1058 VMSTATE_UINT32(tick_offset
, MOS6522Q800VIA1State
),
1059 VMSTATE_UINT8(data_out
, MOS6522Q800VIA1State
),
1060 VMSTATE_INT32(data_out_cnt
, MOS6522Q800VIA1State
),
1061 VMSTATE_UINT8(data_in
, MOS6522Q800VIA1State
),
1062 VMSTATE_UINT8(data_in_cnt
, MOS6522Q800VIA1State
),
1063 VMSTATE_UINT8(cmd
, MOS6522Q800VIA1State
),
1064 VMSTATE_INT32(wprotect
, MOS6522Q800VIA1State
),
1065 VMSTATE_INT32(alt
, MOS6522Q800VIA1State
),
1067 VMSTATE_INT32(adb_data_in_size
, MOS6522Q800VIA1State
),
1068 VMSTATE_INT32(adb_data_in_index
, MOS6522Q800VIA1State
),
1069 VMSTATE_INT32(adb_data_out_index
, MOS6522Q800VIA1State
),
1070 VMSTATE_BUFFER(adb_data_in
, MOS6522Q800VIA1State
),
1071 VMSTATE_BUFFER(adb_data_out
, MOS6522Q800VIA1State
),
1072 VMSTATE_UINT8(adb_autopoll_cmd
, MOS6522Q800VIA1State
),
1074 VMSTATE_TIMER_PTR(one_second_timer
, MOS6522Q800VIA1State
),
1075 VMSTATE_INT64(next_second
, MOS6522Q800VIA1State
),
1076 VMSTATE_TIMER_PTR(sixty_hz_timer
, MOS6522Q800VIA1State
),
1077 VMSTATE_INT64(next_sixty_hz
, MOS6522Q800VIA1State
),
1078 VMSTATE_END_OF_LIST()
1082 static Property mos6522_q800_via1_properties
[] = {
1083 DEFINE_PROP_DRIVE("drive", MOS6522Q800VIA1State
, blk
),
1084 DEFINE_PROP_END_OF_LIST(),
1087 static void mos6522_q800_via1_class_init(ObjectClass
*oc
, void *data
)
1089 DeviceClass
*dc
= DEVICE_CLASS(oc
);
1091 dc
->realize
= mos6522_q800_via1_realize
;
1092 dc
->reset
= mos6522_q800_via1_reset
;
1093 dc
->vmsd
= &vmstate_q800_via1
;
1094 device_class_set_props(dc
, mos6522_q800_via1_properties
);
1097 static const TypeInfo mos6522_q800_via1_type_info
= {
1098 .name
= TYPE_MOS6522_Q800_VIA1
,
1099 .parent
= TYPE_MOS6522
,
1100 .instance_size
= sizeof(MOS6522Q800VIA1State
),
1101 .instance_init
= mos6522_q800_via1_init
,
1102 .class_init
= mos6522_q800_via1_class_init
,
1106 static void mos6522_q800_via2_portB_write(MOS6522State
*s
)
1108 if (s
->dirb
& VIA2B_vPower
&& (s
->b
& VIA2B_vPower
) == 0) {
1110 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
1114 static void mos6522_q800_via2_reset(DeviceState
*dev
)
1116 MOS6522State
*ms
= MOS6522(dev
);
1117 MOS6522DeviceClass
*mdc
= MOS6522_GET_CLASS(ms
);
1119 mdc
->parent_reset(dev
);
1121 ms
->timers
[0].frequency
= VIA_TIMER_FREQ
;
1122 ms
->timers
[1].frequency
= VIA_TIMER_FREQ
;
1130 static void via2_nubus_irq_request(void *opaque
, int irq
, int level
)
1132 MOS6522Q800VIA2State
*v2s
= opaque
;
1133 MOS6522State
*s
= MOS6522(v2s
);
1134 MOS6522DeviceClass
*mdc
= MOS6522_GET_CLASS(s
);
1137 /* Port A nubus IRQ inputs are active LOW */
1138 s
->a
&= ~(1 << irq
);
1139 s
->ifr
|= 1 << VIA2_IRQ_NUBUS_BIT
;
1142 s
->ifr
&= ~(1 << VIA2_IRQ_NUBUS_BIT
);
1148 static void mos6522_q800_via2_init(Object
*obj
)
1150 MOS6522Q800VIA2State
*v2s
= MOS6522_Q800_VIA2(obj
);
1151 SysBusDevice
*sbd
= SYS_BUS_DEVICE(v2s
);
1153 memory_region_init_io(&v2s
->via_mem
, obj
, &mos6522_q800_via2_ops
, v2s
,
1155 sysbus_init_mmio(sbd
, &v2s
->via_mem
);
1157 qdev_init_gpio_in(DEVICE(obj
), via2_irq_request
, VIA2_IRQ_NB
);
1159 qdev_init_gpio_in_named(DEVICE(obj
), via2_nubus_irq_request
, "nubus-irq",
1163 static const VMStateDescription vmstate_q800_via2
= {
1164 .name
= "q800-via2",
1166 .minimum_version_id
= 0,
1167 .fields
= (VMStateField
[]) {
1168 VMSTATE_STRUCT(parent_obj
, MOS6522Q800VIA2State
, 0, vmstate_mos6522
,
1170 VMSTATE_END_OF_LIST()
1174 static void mos6522_q800_via2_class_init(ObjectClass
*oc
, void *data
)
1176 DeviceClass
*dc
= DEVICE_CLASS(oc
);
1177 MOS6522DeviceClass
*mdc
= MOS6522_CLASS(oc
);
1179 dc
->reset
= mos6522_q800_via2_reset
;
1180 dc
->vmsd
= &vmstate_q800_via2
;
1181 mdc
->portB_write
= mos6522_q800_via2_portB_write
;
1184 static const TypeInfo mos6522_q800_via2_type_info
= {
1185 .name
= TYPE_MOS6522_Q800_VIA2
,
1186 .parent
= TYPE_MOS6522
,
1187 .instance_size
= sizeof(MOS6522Q800VIA2State
),
1188 .instance_init
= mos6522_q800_via2_init
,
1189 .class_init
= mos6522_q800_via2_class_init
,
1192 static void mac_via_register_types(void)
1194 type_register_static(&mos6522_q800_via1_type_info
);
1195 type_register_static(&mos6522_q800_via2_type_info
);
1198 type_init(mac_via_register_types
);