18
#include "qemu/osdep.h"
19
#include "exec/address-spaces.h"
20
#include "migration/vmstate.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"
33
#include "sysemu/rtc.h"
54
#define VIA1A_vSccWrReq 0x80
62
#define VIA1A_vRev8 0x40
73
#define VIA1A_vHeadSel 0x20
79
#define VIA1A_vOverlay 0x10
94
#define VIA1A_vSync 0x08
113
#define VIA1A_vVolume 0x07
114
#define VIA1A_CPUID0 0x02
115
#define VIA1A_CPUID1 0x04
116
#define VIA1A_CPUID2 0x10
117
#define VIA1A_CPUID3 0x40
118
#define VIA1A_CPUID_MASK (VIA1A_CPUID0 | VIA1A_CPUID1 | \
119
VIA1A_CPUID2 | VIA1A_CPUID3)
120
#define VIA1A_CPUID_Q800 (VIA1A_CPUID0 | VIA1A_CPUID2)
126
#define VIA1B_vSound 0x80
132
#define VIA1B_vMystery 0x40
142
#define VIA1B_vADBS2 0x20
143
#define VIA1B_vADBS1 0x10
144
#define VIA1B_vADBInt 0x08
145
#define VIA1B_vRTCEnb 0x04
146
#define VIA1B_vRTCClk 0x02
147
#define VIA1B_vRTCData 0x01
159
#define VIA2A_vRAM1 0x80
160
#define VIA2A_vRAM0 0x40
161
#define VIA2A_vIRQE 0x20
162
#define VIA2A_vIRQD 0x10
163
#define VIA2A_vIRQC 0x08
164
#define VIA2A_vIRQB 0x04
165
#define VIA2A_vIRQA 0x02
166
#define VIA2A_vIRQ9 0x01
181
#define VIA2B_vVBL 0x80
187
#define VIA2B_vSndJck 0x40
191
#define VIA2B_vTfr0 0x20
192
#define VIA2B_vTfr1 0x10
193
#define VIA2B_vMode32 0x08
202
#define VIA2B_vPower 0x04
206
#define VIA2B_vBusLk 0x02
210
#define VIA2B_vCDis 0x01
222
#define VIA_IRQ_TIMER1 0x40
223
#define VIA_IRQ_TIMER2 0x20
268
#define VIA1ACR_vShiftCtrl 0x1c
269
#define VIA1ACR_vShiftExtClk 0x0c
270
#define VIA1ACR_vShiftOut 0x10
277
#define ADB_STATE_NEW 0
278
#define ADB_STATE_EVEN 1
279
#define ADB_STATE_ODD 2
280
#define ADB_STATE_IDLE 3
282
#define VIA1B_vADB_StateMask (VIA1B_vADBS1 | VIA1B_vADBS2)
283
#define VIA1B_vADB_StateShift 4
285
#define VIA_TIMER_FREQ (783360)
286
#define VIA_ADB_POLL_FREQ 50
292
#define VIA_60HZ_TIMER_PERIOD_NS 16625800
295
#define RTC_OFFSET 2082844800
305
REG_PRAM_ADDR_LAST = REG_PRAM_ADDR + 19,
307
REG_PRAM_SECT_LAST = REG_PRAM_SECT + 7,
312
static void via1_sixty_hz_update(MOS6522Q800VIA1State *v1s)
315
v1s->next_sixty_hz = (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
316
VIA_60HZ_TIMER_PERIOD_NS) /
317
VIA_60HZ_TIMER_PERIOD_NS * VIA_60HZ_TIMER_PERIOD_NS;
318
timer_mod(v1s->sixty_hz_timer, v1s->next_sixty_hz);
321
static void via1_one_second_update(MOS6522Q800VIA1State *v1s)
323
v1s->next_second = (qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000) /
325
timer_mod(v1s->one_second_timer, v1s->next_second);
328
static void via1_sixty_hz(void *opaque)
330
MOS6522Q800VIA1State *v1s = opaque;
331
MOS6522State *s = MOS6522(v1s);
332
qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA1_IRQ_60HZ_BIT);
338
via1_sixty_hz_update(v1s);
341
static void via1_one_second(void *opaque)
343
MOS6522Q800VIA1State *v1s = opaque;
344
MOS6522State *s = MOS6522(v1s);
345
qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA1_IRQ_ONE_SECOND_BIT);
351
via1_one_second_update(v1s);
355
static void pram_update(MOS6522Q800VIA1State *v1s)
358
if (blk_pwrite(v1s->blk, 0, sizeof(v1s->PRAM), v1s->PRAM, 0) < 0) {
359
qemu_log("pram_update: cannot write to file\n");
384
static int via1_rtc_compact_cmd(uint8_t value)
386
uint8_t read = value & 0x80;
391
if ((value & 0x78) == 0x38) {
393
return read | (REG_PRAM_SECT + (value & 0x07));
395
if ((value & 0x03) == 0x01) {
397
if ((value & 0x18) == 0) {
399
return read | (REG_0 + (value & 0x03));
400
} else if ((value == 0x0c) && !read) {
402
} else if ((value == 0x0d) && !read) {
404
} else if ((value & 0x1c) == 0x08) {
406
return read | (REG_PRAM_ADDR + 0x10 + (value & 0x03));
407
} else if ((value & 0x10) == 0x10) {
409
return read | (REG_PRAM_ADDR + (value & 0x0f));
415
static void via1_rtc_update(MOS6522Q800VIA1State *v1s)
417
MOS6522State *s = MOS6522(v1s);
418
int cmd, sector, addr;
421
if (s->b & VIA1B_vRTCEnb) {
425
if (s->dirb & VIA1B_vRTCData) {
427
if (!(v1s->last_b & VIA1B_vRTCClk) && (s->b & VIA1B_vRTCClk)) {
429
v1s->data_out |= s->b & VIA1B_vRTCData;
432
trace_via1_rtc_update_data_out(v1s->data_out_cnt, v1s->data_out);
434
trace_via1_rtc_update_data_in(v1s->data_in_cnt, v1s->data_in);
436
if ((v1s->last_b & VIA1B_vRTCClk) &&
437
!(s->b & VIA1B_vRTCClk) &&
439
s->b = (s->b & ~VIA1B_vRTCData) |
440
((v1s->data_in >> 7) & VIA1B_vRTCData);
447
if (v1s->data_out_cnt != 8) {
451
v1s->data_out_cnt = 0;
453
trace_via1_rtc_internal_status(v1s->cmd, v1s->alt, v1s->data_out);
455
if (v1s->cmd == REG_EMPTY) {
457
cmd = via1_rtc_compact_cmd(v1s->data_out);
458
trace_via1_rtc_internal_cmd(cmd);
460
if (cmd == REG_INVALID) {
461
trace_via1_rtc_cmd_invalid(v1s->data_out);
466
switch (cmd & 0x7f) {
473
time = v1s->tick_offset + (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)
474
/ NANOSECONDS_PER_SECOND);
475
trace_via1_rtc_internal_time(time);
476
v1s->data_in = (time >> ((cmd & 0x03) << 3)) & 0xff;
477
v1s->data_in_cnt = 8;
478
trace_via1_rtc_cmd_seconds_read((cmd & 0x7f) - REG_0,
481
case REG_PRAM_ADDR...REG_PRAM_ADDR_LAST:
483
v1s->data_in = v1s->PRAM[(cmd & 0x7f) - REG_PRAM_ADDR];
484
v1s->data_in_cnt = 8;
485
trace_via1_rtc_cmd_pram_read((cmd & 0x7f) - REG_PRAM_ADDR,
488
case REG_PRAM_SECT...REG_PRAM_SECT_LAST:
493
trace_via1_rtc_internal_set_cmd(cmd);
497
g_assert_not_reached();
504
if (cmd == REG_WPROTECT || !v1s->wprotect) {
505
trace_via1_rtc_internal_set_cmd(cmd);
508
trace_via1_rtc_internal_ignore_cmd(cmd);
514
if (v1s->alt == REG_EMPTY) {
515
switch (v1s->cmd & 0x7f) {
518
trace_via1_rtc_cmd_seconds_write(v1s->cmd - REG_0, v1s->data_out);
519
v1s->cmd = REG_EMPTY;
523
trace_via1_rtc_cmd_test_write(v1s->data_out);
524
v1s->cmd = REG_EMPTY;
528
trace_via1_rtc_cmd_wprotect_write(v1s->data_out);
529
v1s->wprotect = !!(v1s->data_out & 0x80);
530
v1s->cmd = REG_EMPTY;
532
case REG_PRAM_ADDR...REG_PRAM_ADDR_LAST:
534
trace_via1_rtc_cmd_pram_write(v1s->cmd - REG_PRAM_ADDR,
536
v1s->PRAM[v1s->cmd - REG_PRAM_ADDR] = v1s->data_out;
538
v1s->cmd = REG_EMPTY;
540
case REG_PRAM_SECT...REG_PRAM_SECT_LAST:
541
addr = (v1s->data_out >> 2) & 0x1f;
542
sector = (v1s->cmd & 0x7f) - REG_PRAM_SECT;
543
if (v1s->cmd & 0x80) {
545
v1s->data_in = v1s->PRAM[sector * 32 + addr];
546
v1s->data_in_cnt = 8;
547
trace_via1_rtc_cmd_pram_sect_read(sector, addr,
550
v1s->cmd = REG_EMPTY;
553
trace_via1_rtc_internal_set_alt(addr, sector, addr);
558
g_assert_not_reached();
565
g_assert(REG_PRAM_SECT <= v1s->cmd && v1s->cmd <= REG_PRAM_SECT_LAST);
566
sector = v1s->cmd - REG_PRAM_SECT;
567
v1s->PRAM[sector * 32 + v1s->alt] = v1s->data_out;
569
trace_via1_rtc_cmd_pram_sect_write(sector, v1s->alt, sector * 32 + v1s->alt,
571
v1s->alt = REG_EMPTY;
572
v1s->cmd = REG_EMPTY;
575
static void adb_via_poll(void *opaque)
577
MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
578
MOS6522State *s = MOS6522(v1s);
579
ADBBusState *adb_bus = &v1s->adb_bus;
581
uint8_t *data = &s->sr;
589
adb_autopoll_block(adb_bus);
591
if (v1s->adb_data_in_size > 0 && v1s->adb_data_in_index == 0) {
597
*data = v1s->adb_data_out[0];
598
olen = v1s->adb_data_in_size;
600
s->b &= ~VIA1B_vADBInt;
601
qemu_irq_raise(v1s->adb_data_ready);
606
v1s->adb_data_in_index = 0;
607
v1s->adb_data_out_index = 0;
608
olen = adb_poll(adb_bus, obuf, adb_bus->autopoll_mask);
614
memcpy(v1s->adb_data_in, &obuf[1], olen);
615
v1s->adb_data_in_size = olen;
617
s->b &= ~VIA1B_vADBInt;
618
qemu_irq_raise(v1s->adb_data_ready);
620
*data = v1s->adb_autopoll_cmd;
625
memcpy(v1s->adb_data_in, obuf, olen);
626
v1s->adb_data_in_size = olen;
628
s->b &= ~VIA1B_vADBInt;
629
qemu_irq_raise(v1s->adb_data_ready);
633
trace_via1_adb_poll(*data, (s->b & VIA1B_vADBInt) ? "+" : "-",
634
adb_bus->status, v1s->adb_data_in_index, olen);
637
static int adb_via_send_len(uint8_t data)
640
uint8_t cmd = data & 0xc;
641
uint8_t reg = data & 0x3;
657
qemu_log_mask(LOG_UNIMP, "ADB unknown length for register %d\n",
667
static void adb_via_send(MOS6522Q800VIA1State *v1s, int state, uint8_t data)
669
MOS6522State *ms = MOS6522(v1s);
670
ADBBusState *adb_bus = &v1s->adb_bus;
671
uint16_t autopoll_mask;
679
adb_autopoll_block(adb_bus);
681
if (adb_bus->status & ADB_STATUS_POLLREPLY) {
683
ms->b &= ~VIA1B_vADBInt;
685
ms->b |= VIA1B_vADBInt;
686
v1s->adb_data_out_index = 0;
687
v1s->adb_data_out[v1s->adb_data_out_index++] = data;
690
trace_via1_adb_send(" NEW", data, (ms->b & VIA1B_vADBInt) ? "+" : "-");
691
qemu_irq_raise(v1s->adb_data_ready);
696
ms->b |= VIA1B_vADBInt;
697
v1s->adb_data_out[v1s->adb_data_out_index++] = data;
699
trace_via1_adb_send(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
700
data, (ms->b & VIA1B_vADBInt) ? "+" : "-");
701
qemu_irq_raise(v1s->adb_data_ready);
705
ms->b |= VIA1B_vADBInt;
706
adb_autopoll_unblock(adb_bus);
708
trace_via1_adb_send("IDLE", data,
709
(ms->b & VIA1B_vADBInt) ? "+" : "-");
715
if (v1s->adb_data_out_index == adb_via_send_len(v1s->adb_data_out[0])) {
716
v1s->adb_data_in_size = adb_request(adb_bus, v1s->adb_data_in,
718
v1s->adb_data_out_index);
719
v1s->adb_data_in_index = 0;
721
if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
726
v1s->adb_data_in[0] = 0xff;
727
v1s->adb_data_in[1] = 0xff;
728
v1s->adb_data_in_size = 2;
735
if ((v1s->adb_data_out[0] & 0xc) == 0xc) {
736
v1s->adb_autopoll_cmd = v1s->adb_data_out[0];
738
autopoll_mask = 1 << (v1s->adb_autopoll_cmd >> 4);
739
adb_set_autopoll_mask(adb_bus, autopoll_mask);
744
static void adb_via_receive(MOS6522Q800VIA1State *v1s, int state, uint8_t *data)
746
MOS6522State *ms = MOS6522(v1s);
747
ADBBusState *adb_bus = &v1s->adb_bus;
752
ms->b |= VIA1B_vADBInt;
756
ms->b |= VIA1B_vADBInt;
757
adb_autopoll_unblock(adb_bus);
759
trace_via1_adb_receive("IDLE", *data,
760
(ms->b & VIA1B_vADBInt) ? "+" : "-", adb_bus->status,
761
v1s->adb_data_in_index, v1s->adb_data_in_size);
767
switch (v1s->adb_data_in_index) {
770
*data = v1s->adb_data_in[v1s->adb_data_in_index];
771
if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
772
ms->b &= ~VIA1B_vADBInt;
774
ms->b |= VIA1B_vADBInt;
777
trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
778
*data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
779
adb_bus->status, v1s->adb_data_in_index,
780
v1s->adb_data_in_size);
782
v1s->adb_data_in_index++;
787
*data = v1s->adb_data_in[v1s->adb_data_in_index];
788
pending = adb_bus->pending & ~(1 << (v1s->adb_autopoll_cmd >> 4));
790
ms->b &= ~VIA1B_vADBInt;
792
ms->b |= VIA1B_vADBInt;
795
trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
796
*data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
797
adb_bus->status, v1s->adb_data_in_index,
798
v1s->adb_data_in_size);
800
v1s->adb_data_in_index++;
810
if (v1s->adb_data_in_index < v1s->adb_data_in_size) {
812
*data = v1s->adb_data_in[v1s->adb_data_in_index];
813
ms->b |= VIA1B_vADBInt;
814
} else if (v1s->adb_data_in_index == v1s->adb_data_in_size) {
815
if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
822
ms->b &= ~VIA1B_vADBInt;
826
ms->b &= ~VIA1B_vADBInt;
828
adb_autopoll_unblock(adb_bus);
831
trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
832
*data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
833
adb_bus->status, v1s->adb_data_in_index,
834
v1s->adb_data_in_size);
836
if (v1s->adb_data_in_index <= v1s->adb_data_in_size) {
837
v1s->adb_data_in_index++;
842
qemu_irq_raise(v1s->adb_data_ready);
847
static void via1_adb_update(MOS6522Q800VIA1State *v1s)
849
MOS6522State *s = MOS6522(v1s);
852
oldstate = (v1s->last_b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
853
state = (s->b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
855
if (state != oldstate) {
856
if (s->acr & VIA1ACR_vShiftOut) {
858
adb_via_send(v1s, state, s->sr);
861
adb_via_receive(v1s, state, &s->sr);
866
static void via1_auxmode_update(MOS6522Q800VIA1State *v1s)
868
MOS6522State *s = MOS6522(v1s);
871
oldirq = (v1s->last_b & VIA1B_vMystery) ? 1 : 0;
872
irq = (s->b & VIA1B_vMystery) ? 1 : 0;
876
trace_via1_auxmode(irq);
877
qemu_set_irq(v1s->auxmode_irq, irq);
886
s->b |= VIA1B_vADBInt;
895
#define MACOS_TIMEDBRA 0xd00
896
#define MACOS_TIMESCCB 0xd02
898
#define MACOS_TIMEDBRA_VALUE (0x2a00 * 3)
899
#define MACOS_TIMESCCB_VALUE (0x079d * 3)
901
static bool via1_is_toolbox_timer_calibrated(void)
907
uint16_t timedbra = lduw_be_phys(&address_space_memory, MACOS_TIMEDBRA);
908
uint16_t timesccdb = lduw_be_phys(&address_space_memory, MACOS_TIMESCCB);
910
return (timedbra == MACOS_TIMEDBRA_VALUE &&
911
timesccdb == MACOS_TIMESCCB_VALUE);
914
static void via1_timer_calibration_hack(MOS6522Q800VIA1State *v1s, int addr,
915
uint64_t val, int size)
924
int old_timer_hack_state = v1s->timer_hack_state;
926
switch (v1s->timer_hack_state) {
928
if (addr == VIA_REG_PCR && val == 0x22) {
930
v1s->timer_hack_state = 1;
934
if (addr == VIA_REG_T2CL && val == 0xc) {
936
if (!via1_is_toolbox_timer_calibrated()) {
937
v1s->timer_hack_state = 2;
939
v1s->timer_hack_state = 0;
944
if (addr == VIA_REG_T2CH && val == 0x3) {
949
if (!via1_is_toolbox_timer_calibrated()) {
950
v1s->timer_hack_state = 3;
952
v1s->timer_hack_state = 0;
957
if (addr == VIA_REG_IER && val == 0x20) {
968
stw_be_phys(&address_space_memory, MACOS_TIMEDBRA,
969
MACOS_TIMEDBRA_VALUE);
970
stw_be_phys(&address_space_memory, MACOS_TIMESCCB,
971
MACOS_TIMESCCB_VALUE);
973
v1s->timer_hack_state = 4;
982
if (addr == VIA_REG_PCR && val == 0x22) {
984
v1s->timer_hack_state = 1;
987
if (addr == VIA_REG_T2CL && val == 0xf0) {
989
v1s->timer_hack_state = 5;
993
if (addr == VIA_REG_T2CH && val == 0x3c) {
999
v1s->timer_hack_state = 6;
1000
} else if ((addr == VIA_REG_IER && val == 0x20) ||
1001
addr == VIA_REG_T2CH) {
1003
v1s->timer_hack_state = 0;
1007
if ((addr == VIA_REG_IER && val == 0x20) || addr == VIA_REG_T2CH) {
1009
v1s->timer_hack_state = 7;
1011
v1s->timer_hack_state = 0;
1020
if (addr == VIA_REG_PCR && val == 0x22) {
1022
v1s->timer_hack_state = 1;
1026
g_assert_not_reached();
1029
if (old_timer_hack_state != v1s->timer_hack_state) {
1030
trace_via1_timer_hack_state(v1s->timer_hack_state);
1034
static uint64_t mos6522_q800_via1_read(void *opaque, hwaddr addr, unsigned size)
1036
MOS6522Q800VIA1State *s = MOS6522_Q800_VIA1(opaque);
1037
MOS6522State *ms = MOS6522(s);
1041
addr = (addr >> 9) & 0xf;
1042
ret = mos6522_read(ms, addr, size);
1047
ret = (ret & ~VIA1A_CPUID_MASK) | VIA1A_CPUID_Q800;
1050
if (s->timer_hack_state == 6) {
1060
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1070
static void mos6522_q800_via1_write(void *opaque, hwaddr addr, uint64_t val,
1073
MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
1074
MOS6522State *ms = MOS6522(v1s);
1075
int oldstate, state;
1078
addr = (addr >> 9) & 0xf;
1080
via1_timer_calibration_hack(v1s, addr, val, size);
1082
mos6522_write(ms, addr, val, size);
1086
via1_rtc_update(v1s);
1087
via1_adb_update(v1s);
1088
via1_auxmode_update(v1s);
1090
v1s->last_b = ms->b;
1112
oldstate = (v1s->last_b & VIA1B_vADB_StateMask) >>
1113
VIA1B_vADB_StateShift;
1114
state = (ms->b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
1116
if (oldstate == ADB_STATE_NEW && state == ADB_STATE_NEW &&
1117
(ms->acr & VIA1ACR_vShiftOut) &&
1119
trace_via1_adb_netbsd_enum_hack();
1120
adb_via_send(v1s, state, ms->sr);
1127
static const MemoryRegionOps mos6522_q800_via1_ops = {
1128
.read = mos6522_q800_via1_read,
1129
.write = mos6522_q800_via1_write,
1130
.endianness = DEVICE_BIG_ENDIAN,
1132
.min_access_size = 1,
1133
.max_access_size = 4,
1137
static uint64_t mos6522_q800_via2_read(void *opaque, hwaddr addr, unsigned size)
1139
MOS6522Q800VIA2State *s = MOS6522_Q800_VIA2(opaque);
1140
MOS6522State *ms = MOS6522(s);
1143
addr = (addr >> 9) & 0xf;
1144
val = mos6522_read(ms, addr, size);
1155
val &= ~VIA2_IRQ_SCSI_DATA;
1156
val |= (~ms->last_irq_levels & VIA2_IRQ_SCSI_DATA);
1163
static void mos6522_q800_via2_write(void *opaque, hwaddr addr, uint64_t val,
1166
MOS6522Q800VIA2State *s = MOS6522_Q800_VIA2(opaque);
1167
MOS6522State *ms = MOS6522(s);
1169
addr = (addr >> 9) & 0xf;
1170
mos6522_write(ms, addr, val, size);
1173
static const MemoryRegionOps mos6522_q800_via2_ops = {
1174
.read = mos6522_q800_via2_read,
1175
.write = mos6522_q800_via2_write,
1176
.endianness = DEVICE_BIG_ENDIAN,
1178
.min_access_size = 1,
1179
.max_access_size = 4,
1183
static void via1_postload_update_cb(void *opaque, bool running, RunState state)
1185
MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
1187
qemu_del_vm_change_state_handler(v1s->vmstate);
1188
v1s->vmstate = NULL;
1193
static int via1_post_load(void *opaque, int version_id)
1195
MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
1198
v1s->vmstate = qemu_add_vm_change_state_handler(
1199
via1_postload_update_cb, v1s);
1206
static void mos6522_q800_via1_reset_hold(Object *obj, ResetType type)
1208
MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(obj);
1209
MOS6522State *ms = MOS6522(v1s);
1210
MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(ms);
1211
ADBBusState *adb_bus = &v1s->adb_bus;
1213
if (mdc->parent_phases.hold) {
1214
mdc->parent_phases.hold(obj, type);
1217
ms->timers[0].frequency = VIA_TIMER_FREQ;
1218
ms->timers[1].frequency = VIA_TIMER_FREQ;
1220
ms->b = VIA1B_vADB_StateMask | VIA1B_vADBInt | VIA1B_vRTCEnb;
1223
adb_set_autopoll_enabled(adb_bus, true);
1224
v1s->cmd = REG_EMPTY;
1225
v1s->alt = REG_EMPTY;
1228
v1s->timer_hack_state = 0;
1231
static void mos6522_q800_via1_realize(DeviceState *dev, Error **errp)
1233
MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(dev);
1234
ADBBusState *adb_bus = &v1s->adb_bus;
1238
v1s->one_second_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, via1_one_second,
1240
via1_one_second_update(v1s);
1241
v1s->sixty_hz_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, via1_sixty_hz,
1243
via1_sixty_hz_update(v1s);
1245
qemu_get_timedate(&tm, 0);
1246
v1s->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
1248
adb_register_autopoll_callback(adb_bus, adb_via_poll, v1s);
1249
v1s->adb_data_ready = qdev_get_gpio_in(dev, VIA1_IRQ_ADB_READY_BIT);
1252
int64_t len = blk_getlength(v1s->blk);
1254
error_setg_errno(errp, -len,
1255
"could not get length of backing image");
1258
ret = blk_set_perm(v1s->blk,
1259
BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
1260
BLK_PERM_ALL, errp);
1265
ret = blk_pread(v1s->blk, 0, sizeof(v1s->PRAM), v1s->PRAM, 0);
1267
error_setg(errp, "can't read PRAM contents");
1273
static void mos6522_q800_via1_init(Object *obj)
1275
MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(obj);
1276
SysBusDevice *sbd = SYS_BUS_DEVICE(v1s);
1278
memory_region_init_io(&v1s->via_mem, obj, &mos6522_q800_via1_ops, v1s,
1280
sysbus_init_mmio(sbd, &v1s->via_mem);
1283
qbus_init((BusState *)&v1s->adb_bus, sizeof(v1s->adb_bus),
1284
TYPE_ADB_BUS, DEVICE(v1s), "adb.0");
1287
qdev_init_gpio_out(DEVICE(obj), &v1s->auxmode_irq, 1);
1290
static const VMStateDescription vmstate_q800_via1 = {
1291
.name = "q800-via1",
1293
.minimum_version_id = 0,
1294
.post_load = via1_post_load,
1295
.fields = (const VMStateField[]) {
1296
VMSTATE_STRUCT(parent_obj, MOS6522Q800VIA1State, 0, vmstate_mos6522,
1298
VMSTATE_UINT8(last_b, MOS6522Q800VIA1State),
1300
VMSTATE_BUFFER(PRAM, MOS6522Q800VIA1State),
1301
VMSTATE_UINT32(tick_offset, MOS6522Q800VIA1State),
1302
VMSTATE_UINT8(data_out, MOS6522Q800VIA1State),
1303
VMSTATE_INT32(data_out_cnt, MOS6522Q800VIA1State),
1304
VMSTATE_UINT8(data_in, MOS6522Q800VIA1State),
1305
VMSTATE_UINT8(data_in_cnt, MOS6522Q800VIA1State),
1306
VMSTATE_UINT8(cmd, MOS6522Q800VIA1State),
1307
VMSTATE_INT32(wprotect, MOS6522Q800VIA1State),
1308
VMSTATE_INT32(alt, MOS6522Q800VIA1State),
1310
VMSTATE_INT32(adb_data_in_size, MOS6522Q800VIA1State),
1311
VMSTATE_INT32(adb_data_in_index, MOS6522Q800VIA1State),
1312
VMSTATE_INT32(adb_data_out_index, MOS6522Q800VIA1State),
1313
VMSTATE_BUFFER(adb_data_in, MOS6522Q800VIA1State),
1314
VMSTATE_BUFFER(adb_data_out, MOS6522Q800VIA1State),
1315
VMSTATE_UINT8(adb_autopoll_cmd, MOS6522Q800VIA1State),
1317
VMSTATE_TIMER_PTR(one_second_timer, MOS6522Q800VIA1State),
1318
VMSTATE_INT64(next_second, MOS6522Q800VIA1State),
1319
VMSTATE_TIMER_PTR(sixty_hz_timer, MOS6522Q800VIA1State),
1320
VMSTATE_INT64(next_sixty_hz, MOS6522Q800VIA1State),
1322
VMSTATE_INT32(timer_hack_state, MOS6522Q800VIA1State),
1323
VMSTATE_END_OF_LIST()
1327
static Property mos6522_q800_via1_properties[] = {
1328
DEFINE_PROP_DRIVE("drive", MOS6522Q800VIA1State, blk),
1329
DEFINE_PROP_END_OF_LIST(),
1332
static void mos6522_q800_via1_class_init(ObjectClass *oc, void *data)
1334
DeviceClass *dc = DEVICE_CLASS(oc);
1335
ResettableClass *rc = RESETTABLE_CLASS(oc);
1336
MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
1338
dc->realize = mos6522_q800_via1_realize;
1339
resettable_class_set_parent_phases(rc, NULL, mos6522_q800_via1_reset_hold,
1340
NULL, &mdc->parent_phases);
1341
dc->vmsd = &vmstate_q800_via1;
1342
device_class_set_props(dc, mos6522_q800_via1_properties);
1345
static const TypeInfo mos6522_q800_via1_type_info = {
1346
.name = TYPE_MOS6522_Q800_VIA1,
1347
.parent = TYPE_MOS6522,
1348
.instance_size = sizeof(MOS6522Q800VIA1State),
1349
.instance_init = mos6522_q800_via1_init,
1350
.class_init = mos6522_q800_via1_class_init,
1354
static void mos6522_q800_via2_portB_write(MOS6522State *s)
1356
if (s->dirb & VIA2B_vPower && (s->b & VIA2B_vPower) == 0) {
1358
qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
1362
static void mos6522_q800_via2_reset_hold(Object *obj, ResetType type)
1364
MOS6522State *ms = MOS6522(obj);
1365
MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(ms);
1367
if (mdc->parent_phases.hold) {
1368
mdc->parent_phases.hold(obj, type);
1371
ms->timers[0].frequency = VIA_TIMER_FREQ;
1372
ms->timers[1].frequency = VIA_TIMER_FREQ;
1380
static void via2_nubus_irq_request(void *opaque, int n, int level)
1382
MOS6522Q800VIA2State *v2s = opaque;
1383
MOS6522State *s = MOS6522(v2s);
1384
qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA2_IRQ_NUBUS_BIT);
1394
qemu_set_irq(irq, !level);
1397
static void mos6522_q800_via2_init(Object *obj)
1399
MOS6522Q800VIA2State *v2s = MOS6522_Q800_VIA2(obj);
1400
SysBusDevice *sbd = SYS_BUS_DEVICE(v2s);
1402
memory_region_init_io(&v2s->via_mem, obj, &mos6522_q800_via2_ops, v2s,
1404
sysbus_init_mmio(sbd, &v2s->via_mem);
1406
qdev_init_gpio_in_named(DEVICE(obj), via2_nubus_irq_request, "nubus-irq",
1410
static const VMStateDescription vmstate_q800_via2 = {
1411
.name = "q800-via2",
1413
.minimum_version_id = 0,
1414
.fields = (const VMStateField[]) {
1415
VMSTATE_STRUCT(parent_obj, MOS6522Q800VIA2State, 0, vmstate_mos6522,
1417
VMSTATE_END_OF_LIST()
1421
static void mos6522_q800_via2_class_init(ObjectClass *oc, void *data)
1423
DeviceClass *dc = DEVICE_CLASS(oc);
1424
ResettableClass *rc = RESETTABLE_CLASS(oc);
1425
MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
1427
resettable_class_set_parent_phases(rc, NULL, mos6522_q800_via2_reset_hold,
1428
NULL, &mdc->parent_phases);
1429
dc->vmsd = &vmstate_q800_via2;
1430
mdc->portB_write = mos6522_q800_via2_portB_write;
1433
static const TypeInfo mos6522_q800_via2_type_info = {
1434
.name = TYPE_MOS6522_Q800_VIA2,
1435
.parent = TYPE_MOS6522,
1436
.instance_size = sizeof(MOS6522Q800VIA2State),
1437
.instance_init = mos6522_q800_via2_init,
1438
.class_init = mos6522_q800_via2_class_init,
1441
static void mac_via_register_types(void)
1443
type_register_static(&mos6522_q800_via1_type_info);
1444
type_register_static(&mos6522_q800_via2_type_info);
1447
type_init(mac_via_register_types);