2
0

mac_via.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. * QEMU m68k Macintosh VIA device support
  3. *
  4. * Copyright (c) 2011-2018 Laurent Vivier
  5. * Copyright (c) 2018 Mark Cave-Ayland
  6. *
  7. * Some parts from hw/misc/macio/cuda.c
  8. *
  9. * Copyright (c) 2004-2007 Fabrice Bellard
  10. * Copyright (c) 2007 Jocelyn Mayer
  11. *
  12. * some parts from linux-2.6.29, arch/m68k/include/asm/mac_via.h
  13. *
  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.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "qemu-common.h"
  19. #include "migration/vmstate.h"
  20. #include "hw/sysbus.h"
  21. #include "hw/irq.h"
  22. #include "qemu/timer.h"
  23. #include "hw/misc/mac_via.h"
  24. #include "hw/misc/mos6522.h"
  25. #include "hw/input/adb.h"
  26. #include "sysemu/runstate.h"
  27. #include "qapi/error.h"
  28. #include "qemu/cutils.h"
  29. #include "hw/qdev-properties.h"
  30. #include "sysemu/block-backend.h"
  31. #include "trace.h"
  32. #include "qemu/log.h"
  33. /*
  34. * VIAs: There are two in every machine,
  35. */
  36. #define VIA_SIZE (0x2000)
  37. /*
  38. * Not all of these are true post MacII I think.
  39. * CSA: probably the ones CHRP marks as 'unused' change purposes
  40. * when the IWM becomes the SWIM.
  41. * http://www.rs6000.ibm.com/resource/technology/chrpio/via5.mak.html
  42. * ftp://ftp.austin.ibm.com/pub/technology/spec/chrp/inwork/CHRP_IORef_1.0.pdf
  43. *
  44. * also, http://developer.apple.com/technotes/hw/hw_09.html claims the
  45. * following changes for IIfx:
  46. * VIA1A_vSccWrReq not available and that VIA1A_vSync has moved to an IOP.
  47. * Also, "All of the functionality of VIA2 has been moved to other chips".
  48. */
  49. #define VIA1A_vSccWrReq 0x80 /*
  50. * SCC write. (input)
  51. * [CHRP] SCC WREQ: Reflects the state of the
  52. * Wait/Request pins from the SCC.
  53. * [Macintosh Family Hardware]
  54. * as CHRP on SE/30,II,IIx,IIcx,IIci.
  55. * on IIfx, "0 means an active request"
  56. */
  57. #define VIA1A_vRev8 0x40 /*
  58. * Revision 8 board ???
  59. * [CHRP] En WaitReqB: Lets the WaitReq_L
  60. * signal from port B of the SCC appear on
  61. * the PA7 input pin. Output.
  62. * [Macintosh Family] On the SE/30, this
  63. * is the bit to flip screen buffers.
  64. * 0=alternate, 1=main.
  65. * on II,IIx,IIcx,IIci,IIfx this is a bit
  66. * for Rev ID. 0=II,IIx, 1=IIcx,IIci,IIfx
  67. */
  68. #define VIA1A_vHeadSel 0x20 /*
  69. * Head select for IWM.
  70. * [CHRP] unused.
  71. * [Macintosh Family] "Floppy disk
  72. * state-control line SEL" on all but IIfx
  73. */
  74. #define VIA1A_vOverlay 0x10 /*
  75. * [Macintosh Family] On SE/30,II,IIx,IIcx
  76. * this bit enables the "Overlay" address
  77. * map in the address decoders as it is on
  78. * reset for mapping the ROM over the reset
  79. * vector. 1=use overlay map.
  80. * On the IIci,IIfx it is another bit of the
  81. * CPU ID: 0=normal IIci, 1=IIci with parity
  82. * feature or IIfx.
  83. * [CHRP] En WaitReqA: Lets the WaitReq_L
  84. * signal from port A of the SCC appear
  85. * on the PA7 input pin (CHRP). Output.
  86. * [MkLinux] "Drive Select"
  87. * (with 0x20 being 'disk head select')
  88. */
  89. #define VIA1A_vSync 0x08 /*
  90. * [CHRP] Sync Modem: modem clock select:
  91. * 1: select the external serial clock to
  92. * drive the SCC's /RTxCA pin.
  93. * 0: Select the 3.6864MHz clock to drive
  94. * the SCC cell.
  95. * [Macintosh Family] Correct on all but IIfx
  96. */
  97. /*
  98. * Macintosh Family Hardware sez: bits 0-2 of VIA1A are volume control
  99. * on Macs which had the PWM sound hardware. Reserved on newer models.
  100. * On IIci,IIfx, bits 1-2 are the rest of the CPU ID:
  101. * bit 2: 1=IIci, 0=IIfx
  102. * bit 1: 1 on both IIci and IIfx.
  103. * MkLinux sez bit 0 is 'burnin flag' in this case.
  104. * CHRP sez: VIA1A bits 0-2 and 5 are 'unused': if programmed as
  105. * inputs, these bits will read 0.
  106. */
  107. #define VIA1A_vVolume 0x07 /* Audio volume mask for PWM */
  108. #define VIA1A_CPUID0 0x02 /* CPU id bit 0 on RBV, others */
  109. #define VIA1A_CPUID1 0x04 /* CPU id bit 0 on RBV, others */
  110. #define VIA1A_CPUID2 0x10 /* CPU id bit 0 on RBV, others */
  111. #define VIA1A_CPUID3 0x40 /* CPU id bit 0 on RBV, others */
  112. /*
  113. * Info on VIA1B is from Macintosh Family Hardware & MkLinux.
  114. * CHRP offers no info.
  115. */
  116. #define VIA1B_vSound 0x80 /*
  117. * Sound enable (for compatibility with
  118. * PWM hardware) 0=enabled.
  119. * Also, on IIci w/parity, shows parity error
  120. * 0=error, 1=OK.
  121. */
  122. #define VIA1B_vMystery 0x40 /*
  123. * On IIci, parity enable. 0=enabled,1=disabled
  124. * On SE/30, vertical sync interrupt enable.
  125. * 0=enabled. This vSync interrupt shows up
  126. * as a slot $E interrupt.
  127. */
  128. #define VIA1B_vADBS2 0x20 /* ADB state input bit 1 (unused on IIfx) */
  129. #define VIA1B_vADBS1 0x10 /* ADB state input bit 0 (unused on IIfx) */
  130. #define VIA1B_vADBInt 0x08 /* ADB interrupt 0=interrupt (unused on IIfx)*/
  131. #define VIA1B_vRTCEnb 0x04 /* Enable Real time clock. 0=enabled. */
  132. #define VIA1B_vRTCClk 0x02 /* Real time clock serial-clock line. */
  133. #define VIA1B_vRTCData 0x01 /* Real time clock serial-data line. */
  134. /*
  135. * VIA2 A register is the interrupt lines raised off the nubus
  136. * slots.
  137. * The below info is from 'Macintosh Family Hardware.'
  138. * MkLinux calls the 'IIci internal video IRQ' below the 'RBV slot 0 irq.'
  139. * It also notes that the slot $9 IRQ is the 'Ethernet IRQ' and
  140. * defines the 'Video IRQ' as 0x40 for the 'EVR' VIA work-alike.
  141. * Perhaps OSS uses vRAM1 and vRAM2 for ADB.
  142. */
  143. #define VIA2A_vRAM1 0x80 /* RAM size bit 1 (IIci: reserved) */
  144. #define VIA2A_vRAM0 0x40 /* RAM size bit 0 (IIci: internal video IRQ) */
  145. #define VIA2A_vIRQE 0x20 /* IRQ from slot $E */
  146. #define VIA2A_vIRQD 0x10 /* IRQ from slot $D */
  147. #define VIA2A_vIRQC 0x08 /* IRQ from slot $C */
  148. #define VIA2A_vIRQB 0x04 /* IRQ from slot $B */
  149. #define VIA2A_vIRQA 0x02 /* IRQ from slot $A */
  150. #define VIA2A_vIRQ9 0x01 /* IRQ from slot $9 */
  151. /*
  152. * RAM size bits decoded as follows:
  153. * bit1 bit0 size of ICs in bank A
  154. * 0 0 256 kbit
  155. * 0 1 1 Mbit
  156. * 1 0 4 Mbit
  157. * 1 1 16 Mbit
  158. */
  159. /*
  160. * Register B has the fun stuff in it
  161. */
  162. #define VIA2B_vVBL 0x80 /*
  163. * VBL output to VIA1 (60.15Hz) driven by
  164. * timer T1.
  165. * on IIci, parity test: 0=test mode.
  166. * [MkLinux] RBV_PARODD: 1=odd,0=even.
  167. */
  168. #define VIA2B_vSndJck 0x40 /*
  169. * External sound jack status.
  170. * 0=plug is inserted. On SE/30, always 0
  171. */
  172. #define VIA2B_vTfr0 0x20 /* Transfer mode bit 0 ack from NuBus */
  173. #define VIA2B_vTfr1 0x10 /* Transfer mode bit 1 ack from NuBus */
  174. #define VIA2B_vMode32 0x08 /*
  175. * 24/32bit switch - doubles as cache flush
  176. * on II, AMU/PMMU control.
  177. * if AMU, 0=24bit to 32bit translation
  178. * if PMMU, 1=PMMU is accessing page table.
  179. * on SE/30 tied low.
  180. * on IIx,IIcx,IIfx, unused.
  181. * on IIci/RBV, cache control. 0=flush cache.
  182. */
  183. #define VIA2B_vPower 0x04 /*
  184. * Power off, 0=shut off power.
  185. * on SE/30 this signal sent to PDS card.
  186. */
  187. #define VIA2B_vBusLk 0x02 /*
  188. * Lock NuBus transactions, 0=locked.
  189. * on SE/30 sent to PDS card.
  190. */
  191. #define VIA2B_vCDis 0x01 /*
  192. * Cache control. On IIci, 1=disable cache card
  193. * on others, 0=disable processor's instruction
  194. * and data caches.
  195. */
  196. /* interrupt flags */
  197. #define IRQ_SET 0x80
  198. /* common */
  199. #define VIA_IRQ_TIMER1 0x40
  200. #define VIA_IRQ_TIMER2 0x20
  201. /*
  202. * Apple sez: http://developer.apple.com/technotes/ov/ov_04.html
  203. * Another example of a valid function that has no ROM support is the use
  204. * of the alternate video page for page-flipping animation. Since there
  205. * is no ROM call to flip pages, it is necessary to go play with the
  206. * right bit in the VIA chip (6522 Versatile Interface Adapter).
  207. * [CSA: don't know which one this is, but it's one of 'em!]
  208. */
  209. /*
  210. * 6522 registers - see databook.
  211. * CSA: Assignments for VIA1 confirmed from CHRP spec.
  212. */
  213. /* partial address decode. 0xYYXX : XX part for RBV, YY part for VIA */
  214. /* Note: 15 VIA regs, 8 RBV regs */
  215. #define vBufB 0x0000 /* [VIA/RBV] Register B */
  216. #define vBufAH 0x0200 /* [VIA only] Buffer A, with handshake. DON'T USE! */
  217. #define vDirB 0x0400 /* [VIA only] Data Direction Register B. */
  218. #define vDirA 0x0600 /* [VIA only] Data Direction Register A. */
  219. #define vT1CL 0x0800 /* [VIA only] Timer one counter low. */
  220. #define vT1CH 0x0a00 /* [VIA only] Timer one counter high. */
  221. #define vT1LL 0x0c00 /* [VIA only] Timer one latches low. */
  222. #define vT1LH 0x0e00 /* [VIA only] Timer one latches high. */
  223. #define vT2CL 0x1000 /* [VIA only] Timer two counter low. */
  224. #define vT2CH 0x1200 /* [VIA only] Timer two counter high. */
  225. #define vSR 0x1400 /* [VIA only] Shift register. */
  226. #define vACR 0x1600 /* [VIA only] Auxilary control register. */
  227. #define vPCR 0x1800 /* [VIA only] Peripheral control register. */
  228. /*
  229. * CHRP sez never ever to *write* this.
  230. * Mac family says never to *change* this.
  231. * In fact we need to initialize it once at start.
  232. */
  233. #define vIFR 0x1a00 /* [VIA/RBV] Interrupt flag register. */
  234. #define vIER 0x1c00 /* [VIA/RBV] Interrupt enable register. */
  235. #define vBufA 0x1e00 /* [VIA/RBV] register A (no handshake) */
  236. /* from linux 2.6 drivers/macintosh/via-macii.c */
  237. /* Bits in ACR */
  238. #define VIA1ACR_vShiftCtrl 0x1c /* Shift register control bits */
  239. #define VIA1ACR_vShiftExtClk 0x0c /* Shift on external clock */
  240. #define VIA1ACR_vShiftOut 0x10 /* Shift out if 1 */
  241. /*
  242. * Apple Macintosh Family Hardware Refenece
  243. * Table 19-10 ADB transaction states
  244. */
  245. #define ADB_STATE_NEW 0
  246. #define ADB_STATE_EVEN 1
  247. #define ADB_STATE_ODD 2
  248. #define ADB_STATE_IDLE 3
  249. #define VIA1B_vADB_StateMask (VIA1B_vADBS1 | VIA1B_vADBS2)
  250. #define VIA1B_vADB_StateShift 4
  251. #define VIA_TIMER_FREQ (783360)
  252. #define VIA_ADB_POLL_FREQ 50 /* XXX: not real */
  253. /* VIA returns time offset from Jan 1, 1904, not 1970 */
  254. #define RTC_OFFSET 2082844800
  255. enum {
  256. REG_0,
  257. REG_1,
  258. REG_2,
  259. REG_3,
  260. REG_TEST,
  261. REG_WPROTECT,
  262. REG_PRAM_ADDR,
  263. REG_PRAM_ADDR_LAST = REG_PRAM_ADDR + 19,
  264. REG_PRAM_SECT,
  265. REG_PRAM_SECT_LAST = REG_PRAM_SECT + 7,
  266. REG_INVALID,
  267. REG_EMPTY = 0xff,
  268. };
  269. static void via1_VBL_update(MOS6522Q800VIA1State *v1s)
  270. {
  271. MOS6522State *s = MOS6522(v1s);
  272. /* 60 Hz irq */
  273. v1s->next_VBL = (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 16630) /
  274. 16630 * 16630;
  275. if (s->ier & VIA1_IRQ_VBLANK) {
  276. timer_mod(v1s->VBL_timer, v1s->next_VBL);
  277. } else {
  278. timer_del(v1s->VBL_timer);
  279. }
  280. }
  281. static void via1_one_second_update(MOS6522Q800VIA1State *v1s)
  282. {
  283. MOS6522State *s = MOS6522(v1s);
  284. v1s->next_second = (qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000) /
  285. 1000 * 1000;
  286. if (s->ier & VIA1_IRQ_ONE_SECOND) {
  287. timer_mod(v1s->one_second_timer, v1s->next_second);
  288. } else {
  289. timer_del(v1s->one_second_timer);
  290. }
  291. }
  292. static void via1_VBL(void *opaque)
  293. {
  294. MOS6522Q800VIA1State *v1s = opaque;
  295. MOS6522State *s = MOS6522(v1s);
  296. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(s);
  297. s->ifr |= VIA1_IRQ_VBLANK;
  298. mdc->update_irq(s);
  299. via1_VBL_update(v1s);
  300. }
  301. static void via1_one_second(void *opaque)
  302. {
  303. MOS6522Q800VIA1State *v1s = opaque;
  304. MOS6522State *s = MOS6522(v1s);
  305. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(s);
  306. s->ifr |= VIA1_IRQ_ONE_SECOND;
  307. mdc->update_irq(s);
  308. via1_one_second_update(v1s);
  309. }
  310. static void via1_irq_request(void *opaque, int irq, int level)
  311. {
  312. MOS6522Q800VIA1State *v1s = opaque;
  313. MOS6522State *s = MOS6522(v1s);
  314. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(s);
  315. if (level) {
  316. s->ifr |= 1 << irq;
  317. } else {
  318. s->ifr &= ~(1 << irq);
  319. }
  320. mdc->update_irq(s);
  321. }
  322. static void via2_irq_request(void *opaque, int irq, int level)
  323. {
  324. MOS6522Q800VIA2State *v2s = opaque;
  325. MOS6522State *s = MOS6522(v2s);
  326. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(s);
  327. if (level) {
  328. s->ifr |= 1 << irq;
  329. } else {
  330. s->ifr &= ~(1 << irq);
  331. }
  332. mdc->update_irq(s);
  333. }
  334. static void pram_update(MacVIAState *m)
  335. {
  336. if (m->blk) {
  337. if (blk_pwrite(m->blk, 0, m->mos6522_via1.PRAM,
  338. sizeof(m->mos6522_via1.PRAM), 0) < 0) {
  339. qemu_log("pram_update: cannot write to file\n");
  340. }
  341. }
  342. }
  343. /*
  344. * RTC Commands
  345. *
  346. * Command byte Register addressed by the command
  347. *
  348. * z0000001 Seconds register 0 (lowest-order byte)
  349. * z0000101 Seconds register 1
  350. * z0001001 Seconds register 2
  351. * z0001101 Seconds register 3 (highest-order byte)
  352. * 00110001 Test register (write-only)
  353. * 00110101 Write-Protect Register (write-only)
  354. * z010aa01 RAM address 100aa ($10-$13) (first 20 bytes only)
  355. * z1aaaa01 RAM address 0aaaa ($00-$0F) (first 20 bytes only)
  356. * z0111aaa Extended memory designator and sector number
  357. *
  358. * For a read request, z=1, for a write z=0
  359. * The letter a indicates bits whose value depend on what parameter
  360. * RAM byte you want to address
  361. */
  362. static int via1_rtc_compact_cmd(uint8_t value)
  363. {
  364. uint8_t read = value & 0x80;
  365. value &= 0x7f;
  366. /* the last 2 bits of a command byte must always be 0b01 ... */
  367. if ((value & 0x78) == 0x38) {
  368. /* except for the extended memory designator */
  369. return read | (REG_PRAM_SECT + (value & 0x07));
  370. }
  371. if ((value & 0x03) == 0x01) {
  372. value >>= 2;
  373. if ((value & 0x1c) == 0) {
  374. /* seconds registers */
  375. return read | (REG_0 + (value & 0x03));
  376. } else if ((value == 0x0c) && !read) {
  377. return REG_TEST;
  378. } else if ((value == 0x0d) && !read) {
  379. return REG_WPROTECT;
  380. } else if ((value & 0x1c) == 0x08) {
  381. /* RAM address 0x10 to 0x13 */
  382. return read | (REG_PRAM_ADDR + 0x10 + (value & 0x03));
  383. } else if ((value & 0x43) == 0x41) {
  384. /* RAM address 0x00 to 0x0f */
  385. return read | (REG_PRAM_ADDR + (value & 0x0f));
  386. }
  387. }
  388. return REG_INVALID;
  389. }
  390. static void via1_rtc_update(MacVIAState *m)
  391. {
  392. MOS6522Q800VIA1State *v1s = &m->mos6522_via1;
  393. MOS6522State *s = MOS6522(v1s);
  394. int cmd, sector, addr;
  395. uint32_t time;
  396. if (s->b & VIA1B_vRTCEnb) {
  397. return;
  398. }
  399. if (s->dirb & VIA1B_vRTCData) {
  400. /* send bits to the RTC */
  401. if (!(v1s->last_b & VIA1B_vRTCClk) && (s->b & VIA1B_vRTCClk)) {
  402. m->data_out <<= 1;
  403. m->data_out |= s->b & VIA1B_vRTCData;
  404. m->data_out_cnt++;
  405. }
  406. trace_via1_rtc_update_data_out(m->data_out_cnt, m->data_out);
  407. } else {
  408. trace_via1_rtc_update_data_in(m->data_in_cnt, m->data_in);
  409. /* receive bits from the RTC */
  410. if ((v1s->last_b & VIA1B_vRTCClk) &&
  411. !(s->b & VIA1B_vRTCClk) &&
  412. m->data_in_cnt) {
  413. s->b = (s->b & ~VIA1B_vRTCData) |
  414. ((m->data_in >> 7) & VIA1B_vRTCData);
  415. m->data_in <<= 1;
  416. m->data_in_cnt--;
  417. }
  418. return;
  419. }
  420. if (m->data_out_cnt != 8) {
  421. return;
  422. }
  423. m->data_out_cnt = 0;
  424. trace_via1_rtc_internal_status(m->cmd, m->alt, m->data_out);
  425. /* first byte: it's a command */
  426. if (m->cmd == REG_EMPTY) {
  427. cmd = via1_rtc_compact_cmd(m->data_out);
  428. trace_via1_rtc_internal_cmd(cmd);
  429. if (cmd == REG_INVALID) {
  430. trace_via1_rtc_cmd_invalid(m->data_out);
  431. return;
  432. }
  433. if (cmd & 0x80) { /* this is a read command */
  434. switch (cmd & 0x7f) {
  435. case REG_0...REG_3: /* seconds registers */
  436. /*
  437. * register 0 is lowest-order byte
  438. * register 3 is highest-order byte
  439. */
  440. time = m->tick_offset + (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)
  441. / NANOSECONDS_PER_SECOND);
  442. trace_via1_rtc_internal_time(time);
  443. m->data_in = (time >> ((cmd & 0x03) << 3)) & 0xff;
  444. m->data_in_cnt = 8;
  445. trace_via1_rtc_cmd_seconds_read((cmd & 0x7f) - REG_0,
  446. m->data_in);
  447. break;
  448. case REG_PRAM_ADDR...REG_PRAM_ADDR_LAST:
  449. /* PRAM address 0x00 -> 0x13 */
  450. m->data_in = v1s->PRAM[(cmd & 0x7f) - REG_PRAM_ADDR];
  451. m->data_in_cnt = 8;
  452. trace_via1_rtc_cmd_pram_read((cmd & 0x7f) - REG_PRAM_ADDR,
  453. m->data_in);
  454. break;
  455. case REG_PRAM_SECT...REG_PRAM_SECT_LAST:
  456. /*
  457. * extended memory designator and sector number
  458. * the only two-byte read command
  459. */
  460. trace_via1_rtc_internal_set_cmd(cmd);
  461. m->cmd = cmd;
  462. break;
  463. default:
  464. g_assert_not_reached();
  465. break;
  466. }
  467. return;
  468. }
  469. /* this is a write command, needs a parameter */
  470. if (cmd == REG_WPROTECT || !m->wprotect) {
  471. trace_via1_rtc_internal_set_cmd(cmd);
  472. m->cmd = cmd;
  473. } else {
  474. trace_via1_rtc_internal_ignore_cmd(cmd);
  475. }
  476. return;
  477. }
  478. /* second byte: it's a parameter */
  479. if (m->alt == REG_EMPTY) {
  480. switch (m->cmd & 0x7f) {
  481. case REG_0...REG_3: /* seconds register */
  482. /* FIXME */
  483. trace_via1_rtc_cmd_seconds_write(m->cmd - REG_0, m->data_out);
  484. m->cmd = REG_EMPTY;
  485. break;
  486. case REG_TEST:
  487. /* device control: nothing to do */
  488. trace_via1_rtc_cmd_test_write(m->data_out);
  489. m->cmd = REG_EMPTY;
  490. break;
  491. case REG_WPROTECT:
  492. /* Write Protect register */
  493. trace_via1_rtc_cmd_wprotect_write(m->data_out);
  494. m->wprotect = !!(m->data_out & 0x80);
  495. m->cmd = REG_EMPTY;
  496. break;
  497. case REG_PRAM_ADDR...REG_PRAM_ADDR_LAST:
  498. /* PRAM address 0x00 -> 0x13 */
  499. trace_via1_rtc_cmd_pram_write(m->cmd - REG_PRAM_ADDR, m->data_out);
  500. v1s->PRAM[m->cmd - REG_PRAM_ADDR] = m->data_out;
  501. pram_update(m);
  502. m->cmd = REG_EMPTY;
  503. break;
  504. case REG_PRAM_SECT...REG_PRAM_SECT_LAST:
  505. addr = (m->data_out >> 2) & 0x1f;
  506. sector = (m->cmd & 0x7f) - REG_PRAM_SECT;
  507. if (m->cmd & 0x80) {
  508. /* it's a read */
  509. m->data_in = v1s->PRAM[sector * 32 + addr];
  510. m->data_in_cnt = 8;
  511. trace_via1_rtc_cmd_pram_sect_read(sector, addr,
  512. sector * 32 + addr,
  513. m->data_in);
  514. m->cmd = REG_EMPTY;
  515. } else {
  516. /* it's a write, we need one more parameter */
  517. trace_via1_rtc_internal_set_alt(addr, sector, addr);
  518. m->alt = addr;
  519. }
  520. break;
  521. default:
  522. g_assert_not_reached();
  523. break;
  524. }
  525. return;
  526. }
  527. /* third byte: it's the data of a REG_PRAM_SECT write */
  528. g_assert(REG_PRAM_SECT <= m->cmd && m->cmd <= REG_PRAM_SECT_LAST);
  529. sector = m->cmd - REG_PRAM_SECT;
  530. v1s->PRAM[sector * 32 + m->alt] = m->data_out;
  531. pram_update(m);
  532. trace_via1_rtc_cmd_pram_sect_write(sector, m->alt, sector * 32 + m->alt,
  533. m->data_out);
  534. m->alt = REG_EMPTY;
  535. m->cmd = REG_EMPTY;
  536. }
  537. static void adb_via_poll(void *opaque)
  538. {
  539. MacVIAState *m = opaque;
  540. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(&m->mos6522_via1);
  541. MOS6522State *s = MOS6522(v1s);
  542. ADBBusState *adb_bus = &m->adb_bus;
  543. uint8_t obuf[9];
  544. uint8_t *data = &s->sr;
  545. int olen;
  546. uint16_t pending;
  547. /*
  548. * Setting vADBInt below indicates that an autopoll reply has been
  549. * received, however we must block autopoll until the point where
  550. * the entire reply has been read back to the host
  551. */
  552. adb_autopoll_block(adb_bus);
  553. m->adb_data_in_index = 0;
  554. m->adb_data_out_index = 0;
  555. olen = adb_poll(adb_bus, obuf, adb_bus->autopoll_mask);
  556. if (olen > 0) {
  557. /* Autopoll response */
  558. *data = obuf[0];
  559. olen--;
  560. memcpy(m->adb_data_in, &obuf[1], olen);
  561. m->adb_data_in_size = olen;
  562. s->b &= ~VIA1B_vADBInt;
  563. qemu_irq_raise(m->adb_data_ready);
  564. } else if (olen < 0) {
  565. /* Bus timeout (device does not exist) */
  566. *data = 0xff;
  567. s->b |= VIA1B_vADBInt;
  568. adb_autopoll_unblock(adb_bus);
  569. } else {
  570. pending = adb_bus->pending & ~(1 << (m->adb_autopoll_cmd >> 4));
  571. if (pending) {
  572. /*
  573. * Bus timeout (device exists but another device has data). Block
  574. * autopoll so the OS can read out the first EVEN and first ODD
  575. * byte to determine bus timeout and SRQ status
  576. */
  577. *data = m->adb_autopoll_cmd;
  578. s->b &= ~VIA1B_vADBInt;
  579. obuf[0] = 0xff;
  580. obuf[1] = 0xff;
  581. olen = 2;
  582. memcpy(m->adb_data_in, obuf, olen);
  583. m->adb_data_in_size = olen;
  584. qemu_irq_raise(m->adb_data_ready);
  585. } else {
  586. /* Bus timeout (device exists but no other device has data) */
  587. *data = 0;
  588. s->b |= VIA1B_vADBInt;
  589. adb_autopoll_unblock(adb_bus);
  590. }
  591. }
  592. trace_via1_adb_poll(*data, (s->b & VIA1B_vADBInt) ? "+" : "-",
  593. adb_bus->status, m->adb_data_in_index, olen);
  594. }
  595. static int adb_via_send_len(uint8_t data)
  596. {
  597. /* Determine the send length from the given ADB command */
  598. uint8_t cmd = data & 0xc;
  599. uint8_t reg = data & 0x3;
  600. switch (cmd) {
  601. case 0x8:
  602. /* Listen command */
  603. switch (reg) {
  604. case 2:
  605. /* Register 2 is only used for the keyboard */
  606. return 3;
  607. case 3:
  608. /*
  609. * Fortunately our devices only implement writes
  610. * to register 3 which is fixed at 2 bytes
  611. */
  612. return 3;
  613. default:
  614. qemu_log_mask(LOG_UNIMP, "ADB unknown length for register %d\n",
  615. reg);
  616. return 1;
  617. }
  618. default:
  619. /* Talk, BusReset */
  620. return 1;
  621. }
  622. }
  623. static void adb_via_send(MacVIAState *s, int state, uint8_t data)
  624. {
  625. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(&s->mos6522_via1);
  626. MOS6522State *ms = MOS6522(v1s);
  627. ADBBusState *adb_bus = &s->adb_bus;
  628. uint16_t autopoll_mask;
  629. switch (state) {
  630. case ADB_STATE_NEW:
  631. /*
  632. * Command byte: vADBInt tells host autopoll data already present
  633. * in VIA shift register and ADB transceiver
  634. */
  635. adb_autopoll_block(adb_bus);
  636. if (adb_bus->status & ADB_STATUS_POLLREPLY) {
  637. /* Tell the host the existing data is from autopoll */
  638. ms->b &= ~VIA1B_vADBInt;
  639. } else {
  640. ms->b |= VIA1B_vADBInt;
  641. s->adb_data_out_index = 0;
  642. s->adb_data_out[s->adb_data_out_index++] = data;
  643. }
  644. trace_via1_adb_send(" NEW", data, (ms->b & VIA1B_vADBInt) ? "+" : "-");
  645. qemu_irq_raise(s->adb_data_ready);
  646. break;
  647. case ADB_STATE_EVEN:
  648. case ADB_STATE_ODD:
  649. ms->b |= VIA1B_vADBInt;
  650. s->adb_data_out[s->adb_data_out_index++] = data;
  651. trace_via1_adb_send(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  652. data, (ms->b & VIA1B_vADBInt) ? "+" : "-");
  653. qemu_irq_raise(s->adb_data_ready);
  654. break;
  655. case ADB_STATE_IDLE:
  656. return;
  657. }
  658. /* If the command is complete, execute it */
  659. if (s->adb_data_out_index == adb_via_send_len(s->adb_data_out[0])) {
  660. s->adb_data_in_size = adb_request(adb_bus, s->adb_data_in,
  661. s->adb_data_out,
  662. s->adb_data_out_index);
  663. s->adb_data_in_index = 0;
  664. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  665. /*
  666. * Bus timeout (but allow first EVEN and ODD byte to indicate
  667. * timeout via vADBInt and SRQ status)
  668. */
  669. s->adb_data_in[0] = 0xff;
  670. s->adb_data_in[1] = 0xff;
  671. s->adb_data_in_size = 2;
  672. }
  673. /*
  674. * If last command is TALK, store it for use by autopoll and adjust
  675. * the autopoll mask accordingly
  676. */
  677. if ((s->adb_data_out[0] & 0xc) == 0xc) {
  678. s->adb_autopoll_cmd = s->adb_data_out[0];
  679. autopoll_mask = 1 << (s->adb_autopoll_cmd >> 4);
  680. adb_set_autopoll_mask(adb_bus, autopoll_mask);
  681. }
  682. }
  683. }
  684. static void adb_via_receive(MacVIAState *s, int state, uint8_t *data)
  685. {
  686. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(&s->mos6522_via1);
  687. MOS6522State *ms = MOS6522(v1s);
  688. ADBBusState *adb_bus = &s->adb_bus;
  689. uint16_t pending;
  690. switch (state) {
  691. case ADB_STATE_NEW:
  692. ms->b |= VIA1B_vADBInt;
  693. return;
  694. case ADB_STATE_IDLE:
  695. /*
  696. * Since adb_request() will have already consumed the data from the
  697. * device, we must detect this extra state change and re-inject the
  698. * reponse as either a "fake" autopoll reply or bus timeout
  699. * accordingly
  700. */
  701. if (s->adb_data_in_index == 0) {
  702. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  703. *data = 0xff;
  704. ms->b |= VIA1B_vADBInt;
  705. qemu_irq_raise(s->adb_data_ready);
  706. } else if (s->adb_data_in_size > 0) {
  707. adb_bus->status = ADB_STATUS_POLLREPLY;
  708. *data = s->adb_autopoll_cmd;
  709. ms->b &= ~VIA1B_vADBInt;
  710. qemu_irq_raise(s->adb_data_ready);
  711. }
  712. } else {
  713. ms->b |= VIA1B_vADBInt;
  714. adb_autopoll_unblock(adb_bus);
  715. }
  716. trace_via1_adb_receive("IDLE", *data,
  717. (ms->b & VIA1B_vADBInt) ? "+" : "-", adb_bus->status,
  718. s->adb_data_in_index, s->adb_data_in_size);
  719. break;
  720. case ADB_STATE_EVEN:
  721. case ADB_STATE_ODD:
  722. switch (s->adb_data_in_index) {
  723. case 0:
  724. /* First EVEN byte: vADBInt indicates bus timeout */
  725. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  726. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  727. adb_bus->status, s->adb_data_in_index,
  728. s->adb_data_in_size);
  729. *data = s->adb_data_in[s->adb_data_in_index++];
  730. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  731. ms->b &= ~VIA1B_vADBInt;
  732. } else {
  733. ms->b |= VIA1B_vADBInt;
  734. }
  735. break;
  736. case 1:
  737. /* First ODD byte: vADBInt indicates SRQ */
  738. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  739. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  740. adb_bus->status, s->adb_data_in_index,
  741. s->adb_data_in_size);
  742. *data = s->adb_data_in[s->adb_data_in_index++];
  743. pending = adb_bus->pending & ~(1 << (s->adb_autopoll_cmd >> 4));
  744. if (pending) {
  745. ms->b &= ~VIA1B_vADBInt;
  746. } else {
  747. ms->b |= VIA1B_vADBInt;
  748. }
  749. break;
  750. default:
  751. /*
  752. * Otherwise vADBInt indicates end of data. Note that Linux
  753. * specifically checks for the sequence 0x0 0xff to confirm the
  754. * end of the poll reply, so provide these extra bytes below to
  755. * keep it happy
  756. */
  757. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  758. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  759. adb_bus->status, s->adb_data_in_index,
  760. s->adb_data_in_size);
  761. if (s->adb_data_in_index < s->adb_data_in_size) {
  762. /* Next data byte */
  763. *data = s->adb_data_in[s->adb_data_in_index++];
  764. ms->b |= VIA1B_vADBInt;
  765. } else if (s->adb_data_in_index == s->adb_data_in_size) {
  766. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  767. /* Bus timeout (no more data) */
  768. *data = 0xff;
  769. } else {
  770. /* Return 0x0 after reply */
  771. *data = 0;
  772. }
  773. s->adb_data_in_index++;
  774. ms->b &= ~VIA1B_vADBInt;
  775. } else {
  776. /* Bus timeout (no more data) */
  777. *data = 0xff;
  778. ms->b &= ~VIA1B_vADBInt;
  779. adb_bus->status = 0;
  780. adb_autopoll_unblock(adb_bus);
  781. }
  782. break;
  783. }
  784. qemu_irq_raise(s->adb_data_ready);
  785. break;
  786. }
  787. }
  788. static void via1_adb_update(MacVIAState *m)
  789. {
  790. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(&m->mos6522_via1);
  791. MOS6522State *s = MOS6522(v1s);
  792. int oldstate, state;
  793. oldstate = (v1s->last_b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
  794. state = (s->b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
  795. if (state != oldstate) {
  796. if (s->acr & VIA1ACR_vShiftOut) {
  797. /* output mode */
  798. adb_via_send(m, state, s->sr);
  799. } else {
  800. /* input mode */
  801. adb_via_receive(m, state, &s->sr);
  802. }
  803. }
  804. }
  805. static uint64_t mos6522_q800_via1_read(void *opaque, hwaddr addr, unsigned size)
  806. {
  807. MOS6522Q800VIA1State *s = MOS6522_Q800_VIA1(opaque);
  808. MOS6522State *ms = MOS6522(s);
  809. int64_t now = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
  810. /*
  811. * If IRQs are disabled, timers are disabled, but we need to update
  812. * VIA1_IRQ_VBLANK and VIA1_IRQ_ONE_SECOND bits in the IFR
  813. */
  814. if (now >= s->next_VBL) {
  815. ms->ifr |= VIA1_IRQ_VBLANK;
  816. via1_VBL_update(s);
  817. }
  818. if (now >= s->next_second) {
  819. ms->ifr |= VIA1_IRQ_ONE_SECOND;
  820. via1_one_second_update(s);
  821. }
  822. addr = (addr >> 9) & 0xf;
  823. return mos6522_read(ms, addr, size);
  824. }
  825. static void mos6522_q800_via1_write(void *opaque, hwaddr addr, uint64_t val,
  826. unsigned size)
  827. {
  828. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  829. MacVIAState *m = container_of(v1s, MacVIAState, mos6522_via1);
  830. MOS6522State *ms = MOS6522(v1s);
  831. addr = (addr >> 9) & 0xf;
  832. mos6522_write(ms, addr, val, size);
  833. switch (addr) {
  834. case VIA_REG_B:
  835. via1_rtc_update(m);
  836. via1_adb_update(m);
  837. v1s->last_b = ms->b;
  838. break;
  839. }
  840. via1_one_second_update(v1s);
  841. via1_VBL_update(v1s);
  842. }
  843. static const MemoryRegionOps mos6522_q800_via1_ops = {
  844. .read = mos6522_q800_via1_read,
  845. .write = mos6522_q800_via1_write,
  846. .endianness = DEVICE_BIG_ENDIAN,
  847. .valid = {
  848. .min_access_size = 1,
  849. .max_access_size = 1,
  850. },
  851. };
  852. static uint64_t mos6522_q800_via2_read(void *opaque, hwaddr addr, unsigned size)
  853. {
  854. MOS6522Q800VIA2State *s = MOS6522_Q800_VIA2(opaque);
  855. MOS6522State *ms = MOS6522(s);
  856. addr = (addr >> 9) & 0xf;
  857. return mos6522_read(ms, addr, size);
  858. }
  859. static void mos6522_q800_via2_write(void *opaque, hwaddr addr, uint64_t val,
  860. unsigned size)
  861. {
  862. MOS6522Q800VIA2State *s = MOS6522_Q800_VIA2(opaque);
  863. MOS6522State *ms = MOS6522(s);
  864. addr = (addr >> 9) & 0xf;
  865. mos6522_write(ms, addr, val, size);
  866. }
  867. static const MemoryRegionOps mos6522_q800_via2_ops = {
  868. .read = mos6522_q800_via2_read,
  869. .write = mos6522_q800_via2_write,
  870. .endianness = DEVICE_BIG_ENDIAN,
  871. .valid = {
  872. .min_access_size = 1,
  873. .max_access_size = 1,
  874. },
  875. };
  876. static void mac_via_reset(DeviceState *dev)
  877. {
  878. MacVIAState *m = MAC_VIA(dev);
  879. MOS6522Q800VIA1State *v1s = &m->mos6522_via1;
  880. ADBBusState *adb_bus = &m->adb_bus;
  881. adb_set_autopoll_enabled(adb_bus, true);
  882. timer_del(v1s->VBL_timer);
  883. v1s->next_VBL = 0;
  884. timer_del(v1s->one_second_timer);
  885. v1s->next_second = 0;
  886. m->cmd = REG_EMPTY;
  887. m->alt = REG_EMPTY;
  888. }
  889. static void mac_via_realize(DeviceState *dev, Error **errp)
  890. {
  891. MacVIAState *m = MAC_VIA(dev);
  892. MOS6522State *ms;
  893. ADBBusState *adb_bus = &m->adb_bus;
  894. struct tm tm;
  895. int ret;
  896. /* Init VIAs 1 and 2 */
  897. object_initialize_child(OBJECT(dev), "via1", &m->mos6522_via1,
  898. TYPE_MOS6522_Q800_VIA1);
  899. object_initialize_child(OBJECT(dev), "via2", &m->mos6522_via2,
  900. TYPE_MOS6522_Q800_VIA2);
  901. /* Pass through mos6522 output IRQs */
  902. ms = MOS6522(&m->mos6522_via1);
  903. object_property_add_alias(OBJECT(dev), "irq[0]", OBJECT(ms),
  904. SYSBUS_DEVICE_GPIO_IRQ "[0]");
  905. ms = MOS6522(&m->mos6522_via2);
  906. object_property_add_alias(OBJECT(dev), "irq[1]", OBJECT(ms),
  907. SYSBUS_DEVICE_GPIO_IRQ "[0]");
  908. sysbus_realize(SYS_BUS_DEVICE(&m->mos6522_via1), &error_abort);
  909. sysbus_realize(SYS_BUS_DEVICE(&m->mos6522_via2), &error_abort);
  910. /* Pass through mos6522 input IRQs */
  911. qdev_pass_gpios(DEVICE(&m->mos6522_via1), dev, "via1-irq");
  912. qdev_pass_gpios(DEVICE(&m->mos6522_via2), dev, "via2-irq");
  913. /* VIA 1 */
  914. m->mos6522_via1.one_second_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
  915. via1_one_second,
  916. &m->mos6522_via1);
  917. m->mos6522_via1.VBL_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, via1_VBL,
  918. &m->mos6522_via1);
  919. qemu_get_timedate(&tm, 0);
  920. m->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
  921. adb_register_autopoll_callback(adb_bus, adb_via_poll, m);
  922. m->adb_data_ready = qdev_get_gpio_in_named(dev, "via1-irq",
  923. VIA1_IRQ_ADB_READY_BIT);
  924. if (m->blk) {
  925. int64_t len = blk_getlength(m->blk);
  926. if (len < 0) {
  927. error_setg_errno(errp, -len,
  928. "could not get length of backing image");
  929. return;
  930. }
  931. ret = blk_set_perm(m->blk,
  932. BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
  933. BLK_PERM_ALL, errp);
  934. if (ret < 0) {
  935. return;
  936. }
  937. len = blk_pread(m->blk, 0, m->mos6522_via1.PRAM,
  938. sizeof(m->mos6522_via1.PRAM));
  939. if (len != sizeof(m->mos6522_via1.PRAM)) {
  940. error_setg(errp, "can't read PRAM contents");
  941. return;
  942. }
  943. }
  944. }
  945. static void mac_via_init(Object *obj)
  946. {
  947. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  948. MacVIAState *m = MAC_VIA(obj);
  949. /* MMIO */
  950. memory_region_init(&m->mmio, obj, "mac-via", 2 * VIA_SIZE);
  951. sysbus_init_mmio(sbd, &m->mmio);
  952. memory_region_init_io(&m->via1mem, obj, &mos6522_q800_via1_ops,
  953. &m->mos6522_via1, "via1", VIA_SIZE);
  954. memory_region_add_subregion(&m->mmio, 0x0, &m->via1mem);
  955. memory_region_init_io(&m->via2mem, obj, &mos6522_q800_via2_ops,
  956. &m->mos6522_via2, "via2", VIA_SIZE);
  957. memory_region_add_subregion(&m->mmio, VIA_SIZE, &m->via2mem);
  958. /* ADB */
  959. qbus_create_inplace((BusState *)&m->adb_bus, sizeof(m->adb_bus),
  960. TYPE_ADB_BUS, DEVICE(obj), "adb.0");
  961. }
  962. static void postload_update_cb(void *opaque, int running, RunState state)
  963. {
  964. MacVIAState *m = MAC_VIA(opaque);
  965. qemu_del_vm_change_state_handler(m->vmstate);
  966. m->vmstate = NULL;
  967. pram_update(m);
  968. }
  969. static int mac_via_post_load(void *opaque, int version_id)
  970. {
  971. MacVIAState *m = MAC_VIA(opaque);
  972. if (m->blk) {
  973. m->vmstate = qemu_add_vm_change_state_handler(postload_update_cb,
  974. m);
  975. }
  976. return 0;
  977. }
  978. static const VMStateDescription vmstate_mac_via = {
  979. .name = "mac-via",
  980. .version_id = 2,
  981. .minimum_version_id = 2,
  982. .post_load = mac_via_post_load,
  983. .fields = (VMStateField[]) {
  984. /* VIAs */
  985. VMSTATE_STRUCT(mos6522_via1.parent_obj, MacVIAState, 0, vmstate_mos6522,
  986. MOS6522State),
  987. VMSTATE_UINT8(mos6522_via1.last_b, MacVIAState),
  988. VMSTATE_BUFFER(mos6522_via1.PRAM, MacVIAState),
  989. VMSTATE_TIMER_PTR(mos6522_via1.one_second_timer, MacVIAState),
  990. VMSTATE_INT64(mos6522_via1.next_second, MacVIAState),
  991. VMSTATE_TIMER_PTR(mos6522_via1.VBL_timer, MacVIAState),
  992. VMSTATE_INT64(mos6522_via1.next_VBL, MacVIAState),
  993. VMSTATE_STRUCT(mos6522_via2.parent_obj, MacVIAState, 0, vmstate_mos6522,
  994. MOS6522State),
  995. /* RTC */
  996. VMSTATE_UINT32(tick_offset, MacVIAState),
  997. VMSTATE_UINT8(data_out, MacVIAState),
  998. VMSTATE_INT32(data_out_cnt, MacVIAState),
  999. VMSTATE_UINT8(data_in, MacVIAState),
  1000. VMSTATE_UINT8(data_in_cnt, MacVIAState),
  1001. VMSTATE_UINT8(cmd, MacVIAState),
  1002. VMSTATE_INT32(wprotect, MacVIAState),
  1003. VMSTATE_INT32(alt, MacVIAState),
  1004. /* ADB */
  1005. VMSTATE_INT32(adb_data_in_size, MacVIAState),
  1006. VMSTATE_INT32(adb_data_in_index, MacVIAState),
  1007. VMSTATE_INT32(adb_data_out_index, MacVIAState),
  1008. VMSTATE_BUFFER(adb_data_in, MacVIAState),
  1009. VMSTATE_BUFFER(adb_data_out, MacVIAState),
  1010. VMSTATE_UINT8(adb_autopoll_cmd, MacVIAState),
  1011. VMSTATE_END_OF_LIST()
  1012. }
  1013. };
  1014. static Property mac_via_properties[] = {
  1015. DEFINE_PROP_DRIVE("drive", MacVIAState, blk),
  1016. DEFINE_PROP_END_OF_LIST(),
  1017. };
  1018. static void mac_via_class_init(ObjectClass *oc, void *data)
  1019. {
  1020. DeviceClass *dc = DEVICE_CLASS(oc);
  1021. dc->realize = mac_via_realize;
  1022. dc->reset = mac_via_reset;
  1023. dc->vmsd = &vmstate_mac_via;
  1024. device_class_set_props(dc, mac_via_properties);
  1025. }
  1026. static TypeInfo mac_via_info = {
  1027. .name = TYPE_MAC_VIA,
  1028. .parent = TYPE_SYS_BUS_DEVICE,
  1029. .instance_size = sizeof(MacVIAState),
  1030. .instance_init = mac_via_init,
  1031. .class_init = mac_via_class_init,
  1032. };
  1033. /* VIA 1 */
  1034. static void mos6522_q800_via1_reset(DeviceState *dev)
  1035. {
  1036. MOS6522State *ms = MOS6522(dev);
  1037. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(ms);
  1038. mdc->parent_reset(dev);
  1039. ms->timers[0].frequency = VIA_TIMER_FREQ;
  1040. ms->timers[1].frequency = VIA_TIMER_FREQ;
  1041. ms->b = VIA1B_vADB_StateMask | VIA1B_vADBInt | VIA1B_vRTCEnb;
  1042. }
  1043. static void mos6522_q800_via1_init(Object *obj)
  1044. {
  1045. qdev_init_gpio_in_named(DEVICE(obj), via1_irq_request, "via1-irq",
  1046. VIA1_IRQ_NB);
  1047. }
  1048. static void mos6522_q800_via1_class_init(ObjectClass *oc, void *data)
  1049. {
  1050. DeviceClass *dc = DEVICE_CLASS(oc);
  1051. dc->reset = mos6522_q800_via1_reset;
  1052. }
  1053. static const TypeInfo mos6522_q800_via1_type_info = {
  1054. .name = TYPE_MOS6522_Q800_VIA1,
  1055. .parent = TYPE_MOS6522,
  1056. .instance_size = sizeof(MOS6522Q800VIA1State),
  1057. .instance_init = mos6522_q800_via1_init,
  1058. .class_init = mos6522_q800_via1_class_init,
  1059. };
  1060. /* VIA 2 */
  1061. static void mos6522_q800_via2_portB_write(MOS6522State *s)
  1062. {
  1063. if (s->dirb & VIA2B_vPower && (s->b & VIA2B_vPower) == 0) {
  1064. /* shutdown */
  1065. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  1066. }
  1067. }
  1068. static void mos6522_q800_via2_reset(DeviceState *dev)
  1069. {
  1070. MOS6522State *ms = MOS6522(dev);
  1071. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(ms);
  1072. mdc->parent_reset(dev);
  1073. ms->timers[0].frequency = VIA_TIMER_FREQ;
  1074. ms->timers[1].frequency = VIA_TIMER_FREQ;
  1075. ms->dirb = 0;
  1076. ms->b = 0;
  1077. }
  1078. static void mos6522_q800_via2_init(Object *obj)
  1079. {
  1080. qdev_init_gpio_in_named(DEVICE(obj), via2_irq_request, "via2-irq",
  1081. VIA2_IRQ_NB);
  1082. }
  1083. static void mos6522_q800_via2_class_init(ObjectClass *oc, void *data)
  1084. {
  1085. DeviceClass *dc = DEVICE_CLASS(oc);
  1086. MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
  1087. dc->reset = mos6522_q800_via2_reset;
  1088. mdc->portB_write = mos6522_q800_via2_portB_write;
  1089. }
  1090. static const TypeInfo mos6522_q800_via2_type_info = {
  1091. .name = TYPE_MOS6522_Q800_VIA2,
  1092. .parent = TYPE_MOS6522,
  1093. .instance_size = sizeof(MOS6522Q800VIA2State),
  1094. .instance_init = mos6522_q800_via2_init,
  1095. .class_init = mos6522_q800_via2_class_init,
  1096. };
  1097. static void mac_via_register_types(void)
  1098. {
  1099. type_register_static(&mos6522_q800_via1_type_info);
  1100. type_register_static(&mos6522_q800_via2_type_info);
  1101. type_register_static(&mac_via_info);
  1102. }
  1103. type_init(mac_via_register_types);