2
0

mac_via.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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 "migration/vmstate.h"
  19. #include "hw/sysbus.h"
  20. #include "hw/irq.h"
  21. #include "qemu/timer.h"
  22. #include "hw/misc/mac_via.h"
  23. #include "hw/misc/mos6522.h"
  24. #include "hw/input/adb.h"
  25. #include "sysemu/runstate.h"
  26. #include "qapi/error.h"
  27. #include "qemu/cutils.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/qdev-properties-system.h"
  30. #include "sysemu/block-backend.h"
  31. #include "sysemu/rtc.h"
  32. #include "trace.h"
  33. #include "qemu/log.h"
  34. /*
  35. * VIAs: There are two in every machine
  36. */
  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. * On Quadra 800 this bit toggles A/UX mode which
  128. * configures the glue logic to deliver some IRQs
  129. * at different levels compared to a classic
  130. * Mac.
  131. */
  132. #define VIA1B_vADBS2 0x20 /* ADB state input bit 1 (unused on IIfx) */
  133. #define VIA1B_vADBS1 0x10 /* ADB state input bit 0 (unused on IIfx) */
  134. #define VIA1B_vADBInt 0x08 /* ADB interrupt 0=interrupt (unused on IIfx)*/
  135. #define VIA1B_vRTCEnb 0x04 /* Enable Real time clock. 0=enabled. */
  136. #define VIA1B_vRTCClk 0x02 /* Real time clock serial-clock line. */
  137. #define VIA1B_vRTCData 0x01 /* Real time clock serial-data line. */
  138. /*
  139. * VIA2 A register is the interrupt lines raised off the nubus
  140. * slots.
  141. * The below info is from 'Macintosh Family Hardware.'
  142. * MkLinux calls the 'IIci internal video IRQ' below the 'RBV slot 0 irq.'
  143. * It also notes that the slot $9 IRQ is the 'Ethernet IRQ' and
  144. * defines the 'Video IRQ' as 0x40 for the 'EVR' VIA work-alike.
  145. * Perhaps OSS uses vRAM1 and vRAM2 for ADB.
  146. */
  147. #define VIA2A_vRAM1 0x80 /* RAM size bit 1 (IIci: reserved) */
  148. #define VIA2A_vRAM0 0x40 /* RAM size bit 0 (IIci: internal video IRQ) */
  149. #define VIA2A_vIRQE 0x20 /* IRQ from slot $E */
  150. #define VIA2A_vIRQD 0x10 /* IRQ from slot $D */
  151. #define VIA2A_vIRQC 0x08 /* IRQ from slot $C */
  152. #define VIA2A_vIRQB 0x04 /* IRQ from slot $B */
  153. #define VIA2A_vIRQA 0x02 /* IRQ from slot $A */
  154. #define VIA2A_vIRQ9 0x01 /* IRQ from slot $9 */
  155. /*
  156. * RAM size bits decoded as follows:
  157. * bit1 bit0 size of ICs in bank A
  158. * 0 0 256 kbit
  159. * 0 1 1 Mbit
  160. * 1 0 4 Mbit
  161. * 1 1 16 Mbit
  162. */
  163. /*
  164. * Register B has the fun stuff in it
  165. */
  166. #define VIA2B_vVBL 0x80 /*
  167. * VBL output to VIA1 (60.15Hz) driven by
  168. * timer T1.
  169. * on IIci, parity test: 0=test mode.
  170. * [MkLinux] RBV_PARODD: 1=odd,0=even.
  171. */
  172. #define VIA2B_vSndJck 0x40 /*
  173. * External sound jack status.
  174. * 0=plug is inserted. On SE/30, always 0
  175. */
  176. #define VIA2B_vTfr0 0x20 /* Transfer mode bit 0 ack from NuBus */
  177. #define VIA2B_vTfr1 0x10 /* Transfer mode bit 1 ack from NuBus */
  178. #define VIA2B_vMode32 0x08 /*
  179. * 24/32bit switch - doubles as cache flush
  180. * on II, AMU/PMMU control.
  181. * if AMU, 0=24bit to 32bit translation
  182. * if PMMU, 1=PMMU is accessing page table.
  183. * on SE/30 tied low.
  184. * on IIx,IIcx,IIfx, unused.
  185. * on IIci/RBV, cache control. 0=flush cache.
  186. */
  187. #define VIA2B_vPower 0x04 /*
  188. * Power off, 0=shut off power.
  189. * on SE/30 this signal sent to PDS card.
  190. */
  191. #define VIA2B_vBusLk 0x02 /*
  192. * Lock NuBus transactions, 0=locked.
  193. * on SE/30 sent to PDS card.
  194. */
  195. #define VIA2B_vCDis 0x01 /*
  196. * Cache control. On IIci, 1=disable cache card
  197. * on others, 0=disable processor's instruction
  198. * and data caches.
  199. */
  200. /* interrupt flags */
  201. #define IRQ_SET 0x80
  202. /* common */
  203. #define VIA_IRQ_TIMER1 0x40
  204. #define VIA_IRQ_TIMER2 0x20
  205. /*
  206. * Apple sez: http://developer.apple.com/technotes/ov/ov_04.html
  207. * Another example of a valid function that has no ROM support is the use
  208. * of the alternate video page for page-flipping animation. Since there
  209. * is no ROM call to flip pages, it is necessary to go play with the
  210. * right bit in the VIA chip (6522 Versatile Interface Adapter).
  211. * [CSA: don't know which one this is, but it's one of 'em!]
  212. */
  213. /*
  214. * 6522 registers - see databook.
  215. * CSA: Assignments for VIA1 confirmed from CHRP spec.
  216. */
  217. /* partial address decode. 0xYYXX : XX part for RBV, YY part for VIA */
  218. /* Note: 15 VIA regs, 8 RBV regs */
  219. #define vBufB 0x0000 /* [VIA/RBV] Register B */
  220. #define vBufAH 0x0200 /* [VIA only] Buffer A, with handshake. DON'T USE! */
  221. #define vDirB 0x0400 /* [VIA only] Data Direction Register B. */
  222. #define vDirA 0x0600 /* [VIA only] Data Direction Register A. */
  223. #define vT1CL 0x0800 /* [VIA only] Timer one counter low. */
  224. #define vT1CH 0x0a00 /* [VIA only] Timer one counter high. */
  225. #define vT1LL 0x0c00 /* [VIA only] Timer one latches low. */
  226. #define vT1LH 0x0e00 /* [VIA only] Timer one latches high. */
  227. #define vT2CL 0x1000 /* [VIA only] Timer two counter low. */
  228. #define vT2CH 0x1200 /* [VIA only] Timer two counter high. */
  229. #define vSR 0x1400 /* [VIA only] Shift register. */
  230. #define vACR 0x1600 /* [VIA only] Auxilary control register. */
  231. #define vPCR 0x1800 /* [VIA only] Peripheral control register. */
  232. /*
  233. * CHRP sez never ever to *write* this.
  234. * Mac family says never to *change* this.
  235. * In fact we need to initialize it once at start.
  236. */
  237. #define vIFR 0x1a00 /* [VIA/RBV] Interrupt flag register. */
  238. #define vIER 0x1c00 /* [VIA/RBV] Interrupt enable register. */
  239. #define vBufA 0x1e00 /* [VIA/RBV] register A (no handshake) */
  240. /* from linux 2.6 drivers/macintosh/via-macii.c */
  241. /* Bits in ACR */
  242. #define VIA1ACR_vShiftCtrl 0x1c /* Shift register control bits */
  243. #define VIA1ACR_vShiftExtClk 0x0c /* Shift on external clock */
  244. #define VIA1ACR_vShiftOut 0x10 /* Shift out if 1 */
  245. /*
  246. * Apple Macintosh Family Hardware Refenece
  247. * Table 19-10 ADB transaction states
  248. */
  249. #define ADB_STATE_NEW 0
  250. #define ADB_STATE_EVEN 1
  251. #define ADB_STATE_ODD 2
  252. #define ADB_STATE_IDLE 3
  253. #define VIA1B_vADB_StateMask (VIA1B_vADBS1 | VIA1B_vADBS2)
  254. #define VIA1B_vADB_StateShift 4
  255. #define VIA_TIMER_FREQ (783360)
  256. #define VIA_ADB_POLL_FREQ 50 /* XXX: not real */
  257. /*
  258. * Guide to the Macintosh Family Hardware ch. 12 "Displays" p. 401 gives the
  259. * precise 60Hz interrupt frequency as ~60.15Hz with a period of 16625.8 us
  260. */
  261. #define VIA_60HZ_TIMER_PERIOD_NS 16625800
  262. /* VIA returns time offset from Jan 1, 1904, not 1970 */
  263. #define RTC_OFFSET 2082844800
  264. enum {
  265. REG_0,
  266. REG_1,
  267. REG_2,
  268. REG_3,
  269. REG_TEST,
  270. REG_WPROTECT,
  271. REG_PRAM_ADDR,
  272. REG_PRAM_ADDR_LAST = REG_PRAM_ADDR + 19,
  273. REG_PRAM_SECT,
  274. REG_PRAM_SECT_LAST = REG_PRAM_SECT + 7,
  275. REG_INVALID,
  276. REG_EMPTY = 0xff,
  277. };
  278. static void via1_sixty_hz_update(MOS6522Q800VIA1State *v1s)
  279. {
  280. /* 60 Hz irq */
  281. v1s->next_sixty_hz = (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  282. VIA_60HZ_TIMER_PERIOD_NS) /
  283. VIA_60HZ_TIMER_PERIOD_NS * VIA_60HZ_TIMER_PERIOD_NS;
  284. timer_mod(v1s->sixty_hz_timer, v1s->next_sixty_hz);
  285. }
  286. static void via1_one_second_update(MOS6522Q800VIA1State *v1s)
  287. {
  288. v1s->next_second = (qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000) /
  289. 1000 * 1000;
  290. timer_mod(v1s->one_second_timer, v1s->next_second);
  291. }
  292. static void via1_sixty_hz(void *opaque)
  293. {
  294. MOS6522Q800VIA1State *v1s = opaque;
  295. MOS6522State *s = MOS6522(v1s);
  296. qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA1_IRQ_60HZ_BIT);
  297. /* Negative edge trigger */
  298. qemu_irq_lower(irq);
  299. qemu_irq_raise(irq);
  300. via1_sixty_hz_update(v1s);
  301. }
  302. static void via1_one_second(void *opaque)
  303. {
  304. MOS6522Q800VIA1State *v1s = opaque;
  305. MOS6522State *s = MOS6522(v1s);
  306. qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA1_IRQ_ONE_SECOND_BIT);
  307. /* Negative edge trigger */
  308. qemu_irq_lower(irq);
  309. qemu_irq_raise(irq);
  310. via1_one_second_update(v1s);
  311. }
  312. static void pram_update(MOS6522Q800VIA1State *v1s)
  313. {
  314. if (v1s->blk) {
  315. if (blk_pwrite(v1s->blk, 0, sizeof(v1s->PRAM), v1s->PRAM, 0) < 0) {
  316. qemu_log("pram_update: cannot write to file\n");
  317. }
  318. }
  319. }
  320. /*
  321. * RTC Commands
  322. *
  323. * Command byte Register addressed by the command
  324. *
  325. * z0000001 Seconds register 0 (lowest-order byte)
  326. * z0000101 Seconds register 1
  327. * z0001001 Seconds register 2
  328. * z0001101 Seconds register 3 (highest-order byte)
  329. * 00110001 Test register (write-only)
  330. * 00110101 Write-Protect Register (write-only)
  331. * z010aa01 RAM address 100aa ($10-$13) (first 20 bytes only)
  332. * z1aaaa01 RAM address 0aaaa ($00-$0F) (first 20 bytes only)
  333. * z0111aaa Extended memory designator and sector number
  334. *
  335. * For a read request, z=1, for a write z=0
  336. * The letter a indicates bits whose value depend on what parameter
  337. * RAM byte you want to address
  338. */
  339. static int via1_rtc_compact_cmd(uint8_t value)
  340. {
  341. uint8_t read = value & 0x80;
  342. value &= 0x7f;
  343. /* the last 2 bits of a command byte must always be 0b01 ... */
  344. if ((value & 0x78) == 0x38) {
  345. /* except for the extended memory designator */
  346. return read | (REG_PRAM_SECT + (value & 0x07));
  347. }
  348. if ((value & 0x03) == 0x01) {
  349. value >>= 2;
  350. if ((value & 0x1c) == 0) {
  351. /* seconds registers */
  352. return read | (REG_0 + (value & 0x03));
  353. } else if ((value == 0x0c) && !read) {
  354. return REG_TEST;
  355. } else if ((value == 0x0d) && !read) {
  356. return REG_WPROTECT;
  357. } else if ((value & 0x1c) == 0x08) {
  358. /* RAM address 0x10 to 0x13 */
  359. return read | (REG_PRAM_ADDR + 0x10 + (value & 0x03));
  360. } else if ((value & 0x43) == 0x41) {
  361. /* RAM address 0x00 to 0x0f */
  362. return read | (REG_PRAM_ADDR + (value & 0x0f));
  363. }
  364. }
  365. return REG_INVALID;
  366. }
  367. static void via1_rtc_update(MOS6522Q800VIA1State *v1s)
  368. {
  369. MOS6522State *s = MOS6522(v1s);
  370. int cmd, sector, addr;
  371. uint32_t time;
  372. if (s->b & VIA1B_vRTCEnb) {
  373. return;
  374. }
  375. if (s->dirb & VIA1B_vRTCData) {
  376. /* send bits to the RTC */
  377. if (!(v1s->last_b & VIA1B_vRTCClk) && (s->b & VIA1B_vRTCClk)) {
  378. v1s->data_out <<= 1;
  379. v1s->data_out |= s->b & VIA1B_vRTCData;
  380. v1s->data_out_cnt++;
  381. }
  382. trace_via1_rtc_update_data_out(v1s->data_out_cnt, v1s->data_out);
  383. } else {
  384. trace_via1_rtc_update_data_in(v1s->data_in_cnt, v1s->data_in);
  385. /* receive bits from the RTC */
  386. if ((v1s->last_b & VIA1B_vRTCClk) &&
  387. !(s->b & VIA1B_vRTCClk) &&
  388. v1s->data_in_cnt) {
  389. s->b = (s->b & ~VIA1B_vRTCData) |
  390. ((v1s->data_in >> 7) & VIA1B_vRTCData);
  391. v1s->data_in <<= 1;
  392. v1s->data_in_cnt--;
  393. }
  394. return;
  395. }
  396. if (v1s->data_out_cnt != 8) {
  397. return;
  398. }
  399. v1s->data_out_cnt = 0;
  400. trace_via1_rtc_internal_status(v1s->cmd, v1s->alt, v1s->data_out);
  401. /* first byte: it's a command */
  402. if (v1s->cmd == REG_EMPTY) {
  403. cmd = via1_rtc_compact_cmd(v1s->data_out);
  404. trace_via1_rtc_internal_cmd(cmd);
  405. if (cmd == REG_INVALID) {
  406. trace_via1_rtc_cmd_invalid(v1s->data_out);
  407. return;
  408. }
  409. if (cmd & 0x80) { /* this is a read command */
  410. switch (cmd & 0x7f) {
  411. case REG_0...REG_3: /* seconds registers */
  412. /*
  413. * register 0 is lowest-order byte
  414. * register 3 is highest-order byte
  415. */
  416. time = v1s->tick_offset + (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)
  417. / NANOSECONDS_PER_SECOND);
  418. trace_via1_rtc_internal_time(time);
  419. v1s->data_in = (time >> ((cmd & 0x03) << 3)) & 0xff;
  420. v1s->data_in_cnt = 8;
  421. trace_via1_rtc_cmd_seconds_read((cmd & 0x7f) - REG_0,
  422. v1s->data_in);
  423. break;
  424. case REG_PRAM_ADDR...REG_PRAM_ADDR_LAST:
  425. /* PRAM address 0x00 -> 0x13 */
  426. v1s->data_in = v1s->PRAM[(cmd & 0x7f) - REG_PRAM_ADDR];
  427. v1s->data_in_cnt = 8;
  428. trace_via1_rtc_cmd_pram_read((cmd & 0x7f) - REG_PRAM_ADDR,
  429. v1s->data_in);
  430. break;
  431. case REG_PRAM_SECT...REG_PRAM_SECT_LAST:
  432. /*
  433. * extended memory designator and sector number
  434. * the only two-byte read command
  435. */
  436. trace_via1_rtc_internal_set_cmd(cmd);
  437. v1s->cmd = cmd;
  438. break;
  439. default:
  440. g_assert_not_reached();
  441. break;
  442. }
  443. return;
  444. }
  445. /* this is a write command, needs a parameter */
  446. if (cmd == REG_WPROTECT || !v1s->wprotect) {
  447. trace_via1_rtc_internal_set_cmd(cmd);
  448. v1s->cmd = cmd;
  449. } else {
  450. trace_via1_rtc_internal_ignore_cmd(cmd);
  451. }
  452. return;
  453. }
  454. /* second byte: it's a parameter */
  455. if (v1s->alt == REG_EMPTY) {
  456. switch (v1s->cmd & 0x7f) {
  457. case REG_0...REG_3: /* seconds register */
  458. /* FIXME */
  459. trace_via1_rtc_cmd_seconds_write(v1s->cmd - REG_0, v1s->data_out);
  460. v1s->cmd = REG_EMPTY;
  461. break;
  462. case REG_TEST:
  463. /* device control: nothing to do */
  464. trace_via1_rtc_cmd_test_write(v1s->data_out);
  465. v1s->cmd = REG_EMPTY;
  466. break;
  467. case REG_WPROTECT:
  468. /* Write Protect register */
  469. trace_via1_rtc_cmd_wprotect_write(v1s->data_out);
  470. v1s->wprotect = !!(v1s->data_out & 0x80);
  471. v1s->cmd = REG_EMPTY;
  472. break;
  473. case REG_PRAM_ADDR...REG_PRAM_ADDR_LAST:
  474. /* PRAM address 0x00 -> 0x13 */
  475. trace_via1_rtc_cmd_pram_write(v1s->cmd - REG_PRAM_ADDR,
  476. v1s->data_out);
  477. v1s->PRAM[v1s->cmd - REG_PRAM_ADDR] = v1s->data_out;
  478. pram_update(v1s);
  479. v1s->cmd = REG_EMPTY;
  480. break;
  481. case REG_PRAM_SECT...REG_PRAM_SECT_LAST:
  482. addr = (v1s->data_out >> 2) & 0x1f;
  483. sector = (v1s->cmd & 0x7f) - REG_PRAM_SECT;
  484. if (v1s->cmd & 0x80) {
  485. /* it's a read */
  486. v1s->data_in = v1s->PRAM[sector * 32 + addr];
  487. v1s->data_in_cnt = 8;
  488. trace_via1_rtc_cmd_pram_sect_read(sector, addr,
  489. sector * 32 + addr,
  490. v1s->data_in);
  491. v1s->cmd = REG_EMPTY;
  492. } else {
  493. /* it's a write, we need one more parameter */
  494. trace_via1_rtc_internal_set_alt(addr, sector, addr);
  495. v1s->alt = addr;
  496. }
  497. break;
  498. default:
  499. g_assert_not_reached();
  500. break;
  501. }
  502. return;
  503. }
  504. /* third byte: it's the data of a REG_PRAM_SECT write */
  505. g_assert(REG_PRAM_SECT <= v1s->cmd && v1s->cmd <= REG_PRAM_SECT_LAST);
  506. sector = v1s->cmd - REG_PRAM_SECT;
  507. v1s->PRAM[sector * 32 + v1s->alt] = v1s->data_out;
  508. pram_update(v1s);
  509. trace_via1_rtc_cmd_pram_sect_write(sector, v1s->alt, sector * 32 + v1s->alt,
  510. v1s->data_out);
  511. v1s->alt = REG_EMPTY;
  512. v1s->cmd = REG_EMPTY;
  513. }
  514. static void adb_via_poll(void *opaque)
  515. {
  516. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  517. MOS6522State *s = MOS6522(v1s);
  518. ADBBusState *adb_bus = &v1s->adb_bus;
  519. uint8_t obuf[9];
  520. uint8_t *data = &s->sr;
  521. int olen;
  522. /*
  523. * Setting vADBInt below indicates that an autopoll reply has been
  524. * received, however we must block autopoll until the point where
  525. * the entire reply has been read back to the host
  526. */
  527. adb_autopoll_block(adb_bus);
  528. if (v1s->adb_data_in_size > 0 && v1s->adb_data_in_index == 0) {
  529. /*
  530. * For older Linux kernels that switch to IDLE mode after sending the
  531. * ADB command, detect if there is an existing response and return that
  532. * as a "fake" autopoll reply or bus timeout accordingly
  533. */
  534. *data = v1s->adb_data_out[0];
  535. olen = v1s->adb_data_in_size;
  536. s->b &= ~VIA1B_vADBInt;
  537. qemu_irq_raise(v1s->adb_data_ready);
  538. } else {
  539. /*
  540. * Otherwise poll as normal
  541. */
  542. v1s->adb_data_in_index = 0;
  543. v1s->adb_data_out_index = 0;
  544. olen = adb_poll(adb_bus, obuf, adb_bus->autopoll_mask);
  545. if (olen > 0) {
  546. /* Autopoll response */
  547. *data = obuf[0];
  548. olen--;
  549. memcpy(v1s->adb_data_in, &obuf[1], olen);
  550. v1s->adb_data_in_size = olen;
  551. s->b &= ~VIA1B_vADBInt;
  552. qemu_irq_raise(v1s->adb_data_ready);
  553. } else {
  554. *data = v1s->adb_autopoll_cmd;
  555. obuf[0] = 0xff;
  556. obuf[1] = 0xff;
  557. olen = 2;
  558. memcpy(v1s->adb_data_in, obuf, olen);
  559. v1s->adb_data_in_size = olen;
  560. s->b &= ~VIA1B_vADBInt;
  561. qemu_irq_raise(v1s->adb_data_ready);
  562. }
  563. }
  564. trace_via1_adb_poll(*data, (s->b & VIA1B_vADBInt) ? "+" : "-",
  565. adb_bus->status, v1s->adb_data_in_index, olen);
  566. }
  567. static int adb_via_send_len(uint8_t data)
  568. {
  569. /* Determine the send length from the given ADB command */
  570. uint8_t cmd = data & 0xc;
  571. uint8_t reg = data & 0x3;
  572. switch (cmd) {
  573. case 0x8:
  574. /* Listen command */
  575. switch (reg) {
  576. case 2:
  577. /* Register 2 is only used for the keyboard */
  578. return 3;
  579. case 3:
  580. /*
  581. * Fortunately our devices only implement writes
  582. * to register 3 which is fixed at 2 bytes
  583. */
  584. return 3;
  585. default:
  586. qemu_log_mask(LOG_UNIMP, "ADB unknown length for register %d\n",
  587. reg);
  588. return 1;
  589. }
  590. default:
  591. /* Talk, BusReset */
  592. return 1;
  593. }
  594. }
  595. static void adb_via_send(MOS6522Q800VIA1State *v1s, int state, uint8_t data)
  596. {
  597. MOS6522State *ms = MOS6522(v1s);
  598. ADBBusState *adb_bus = &v1s->adb_bus;
  599. uint16_t autopoll_mask;
  600. switch (state) {
  601. case ADB_STATE_NEW:
  602. /*
  603. * Command byte: vADBInt tells host autopoll data already present
  604. * in VIA shift register and ADB transceiver
  605. */
  606. adb_autopoll_block(adb_bus);
  607. if (adb_bus->status & ADB_STATUS_POLLREPLY) {
  608. /* Tell the host the existing data is from autopoll */
  609. ms->b &= ~VIA1B_vADBInt;
  610. } else {
  611. ms->b |= VIA1B_vADBInt;
  612. v1s->adb_data_out_index = 0;
  613. v1s->adb_data_out[v1s->adb_data_out_index++] = data;
  614. }
  615. trace_via1_adb_send(" NEW", data, (ms->b & VIA1B_vADBInt) ? "+" : "-");
  616. qemu_irq_raise(v1s->adb_data_ready);
  617. break;
  618. case ADB_STATE_EVEN:
  619. case ADB_STATE_ODD:
  620. ms->b |= VIA1B_vADBInt;
  621. v1s->adb_data_out[v1s->adb_data_out_index++] = data;
  622. trace_via1_adb_send(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  623. data, (ms->b & VIA1B_vADBInt) ? "+" : "-");
  624. qemu_irq_raise(v1s->adb_data_ready);
  625. break;
  626. case ADB_STATE_IDLE:
  627. return;
  628. }
  629. /* If the command is complete, execute it */
  630. if (v1s->adb_data_out_index == adb_via_send_len(v1s->adb_data_out[0])) {
  631. v1s->adb_data_in_size = adb_request(adb_bus, v1s->adb_data_in,
  632. v1s->adb_data_out,
  633. v1s->adb_data_out_index);
  634. v1s->adb_data_in_index = 0;
  635. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  636. /*
  637. * Bus timeout (but allow first EVEN and ODD byte to indicate
  638. * timeout via vADBInt and SRQ status)
  639. */
  640. v1s->adb_data_in[0] = 0xff;
  641. v1s->adb_data_in[1] = 0xff;
  642. v1s->adb_data_in_size = 2;
  643. }
  644. /*
  645. * If last command is TALK, store it for use by autopoll and adjust
  646. * the autopoll mask accordingly
  647. */
  648. if ((v1s->adb_data_out[0] & 0xc) == 0xc) {
  649. v1s->adb_autopoll_cmd = v1s->adb_data_out[0];
  650. autopoll_mask = 1 << (v1s->adb_autopoll_cmd >> 4);
  651. adb_set_autopoll_mask(adb_bus, autopoll_mask);
  652. }
  653. }
  654. }
  655. static void adb_via_receive(MOS6522Q800VIA1State *v1s, int state, uint8_t *data)
  656. {
  657. MOS6522State *ms = MOS6522(v1s);
  658. ADBBusState *adb_bus = &v1s->adb_bus;
  659. uint16_t pending;
  660. switch (state) {
  661. case ADB_STATE_NEW:
  662. ms->b |= VIA1B_vADBInt;
  663. return;
  664. case ADB_STATE_IDLE:
  665. ms->b |= VIA1B_vADBInt;
  666. adb_autopoll_unblock(adb_bus);
  667. trace_via1_adb_receive("IDLE", *data,
  668. (ms->b & VIA1B_vADBInt) ? "+" : "-", adb_bus->status,
  669. v1s->adb_data_in_index, v1s->adb_data_in_size);
  670. break;
  671. case ADB_STATE_EVEN:
  672. case ADB_STATE_ODD:
  673. switch (v1s->adb_data_in_index) {
  674. case 0:
  675. /* First EVEN byte: vADBInt indicates bus timeout */
  676. *data = v1s->adb_data_in[v1s->adb_data_in_index];
  677. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  678. ms->b &= ~VIA1B_vADBInt;
  679. } else {
  680. ms->b |= VIA1B_vADBInt;
  681. }
  682. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  683. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  684. adb_bus->status, v1s->adb_data_in_index,
  685. v1s->adb_data_in_size);
  686. v1s->adb_data_in_index++;
  687. break;
  688. case 1:
  689. /* First ODD byte: vADBInt indicates SRQ */
  690. *data = v1s->adb_data_in[v1s->adb_data_in_index];
  691. pending = adb_bus->pending & ~(1 << (v1s->adb_autopoll_cmd >> 4));
  692. if (pending) {
  693. ms->b &= ~VIA1B_vADBInt;
  694. } else {
  695. ms->b |= VIA1B_vADBInt;
  696. }
  697. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  698. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  699. adb_bus->status, v1s->adb_data_in_index,
  700. v1s->adb_data_in_size);
  701. v1s->adb_data_in_index++;
  702. break;
  703. default:
  704. /*
  705. * Otherwise vADBInt indicates end of data. Note that Linux
  706. * specifically checks for the sequence 0x0 0xff to confirm the
  707. * end of the poll reply, so provide these extra bytes below to
  708. * keep it happy
  709. */
  710. if (v1s->adb_data_in_index < v1s->adb_data_in_size) {
  711. /* Next data byte */
  712. *data = v1s->adb_data_in[v1s->adb_data_in_index];
  713. ms->b |= VIA1B_vADBInt;
  714. } else if (v1s->adb_data_in_index == v1s->adb_data_in_size) {
  715. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  716. /* Bus timeout (no more data) */
  717. *data = 0xff;
  718. } else {
  719. /* Return 0x0 after reply */
  720. *data = 0;
  721. }
  722. ms->b &= ~VIA1B_vADBInt;
  723. } else {
  724. /* Bus timeout (no more data) */
  725. *data = 0xff;
  726. ms->b &= ~VIA1B_vADBInt;
  727. adb_bus->status = 0;
  728. adb_autopoll_unblock(adb_bus);
  729. }
  730. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  731. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  732. adb_bus->status, v1s->adb_data_in_index,
  733. v1s->adb_data_in_size);
  734. if (v1s->adb_data_in_index <= v1s->adb_data_in_size) {
  735. v1s->adb_data_in_index++;
  736. }
  737. break;
  738. }
  739. qemu_irq_raise(v1s->adb_data_ready);
  740. break;
  741. }
  742. }
  743. static void via1_adb_update(MOS6522Q800VIA1State *v1s)
  744. {
  745. MOS6522State *s = MOS6522(v1s);
  746. int oldstate, state;
  747. oldstate = (v1s->last_b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
  748. state = (s->b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
  749. if (state != oldstate) {
  750. if (s->acr & VIA1ACR_vShiftOut) {
  751. /* output mode */
  752. adb_via_send(v1s, state, s->sr);
  753. } else {
  754. /* input mode */
  755. adb_via_receive(v1s, state, &s->sr);
  756. }
  757. }
  758. }
  759. static void via1_auxmode_update(MOS6522Q800VIA1State *v1s)
  760. {
  761. MOS6522State *s = MOS6522(v1s);
  762. int oldirq, irq;
  763. oldirq = (v1s->last_b & VIA1B_vMystery) ? 1 : 0;
  764. irq = (s->b & VIA1B_vMystery) ? 1 : 0;
  765. /* Check to see if the A/UX mode bit has changed */
  766. if (irq != oldirq) {
  767. trace_via1_auxmode(irq);
  768. qemu_set_irq(v1s->auxmode_irq, irq);
  769. }
  770. }
  771. static uint64_t mos6522_q800_via1_read(void *opaque, hwaddr addr, unsigned size)
  772. {
  773. MOS6522Q800VIA1State *s = MOS6522_Q800_VIA1(opaque);
  774. MOS6522State *ms = MOS6522(s);
  775. addr = (addr >> 9) & 0xf;
  776. return mos6522_read(ms, addr, size);
  777. }
  778. static void mos6522_q800_via1_write(void *opaque, hwaddr addr, uint64_t val,
  779. unsigned size)
  780. {
  781. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  782. MOS6522State *ms = MOS6522(v1s);
  783. addr = (addr >> 9) & 0xf;
  784. mos6522_write(ms, addr, val, size);
  785. switch (addr) {
  786. case VIA_REG_B:
  787. via1_rtc_update(v1s);
  788. via1_adb_update(v1s);
  789. via1_auxmode_update(v1s);
  790. v1s->last_b = ms->b;
  791. break;
  792. }
  793. }
  794. static const MemoryRegionOps mos6522_q800_via1_ops = {
  795. .read = mos6522_q800_via1_read,
  796. .write = mos6522_q800_via1_write,
  797. .endianness = DEVICE_BIG_ENDIAN,
  798. .valid = {
  799. .min_access_size = 1,
  800. .max_access_size = 4,
  801. },
  802. };
  803. static uint64_t mos6522_q800_via2_read(void *opaque, hwaddr addr, unsigned size)
  804. {
  805. MOS6522Q800VIA2State *s = MOS6522_Q800_VIA2(opaque);
  806. MOS6522State *ms = MOS6522(s);
  807. uint64_t val;
  808. addr = (addr >> 9) & 0xf;
  809. val = mos6522_read(ms, addr, size);
  810. switch (addr) {
  811. case VIA_REG_IFR:
  812. /*
  813. * On a Q800 an emulated VIA2 is integrated into the onboard logic. The
  814. * expectation of most OSs is that the DRQ bit is live, rather than
  815. * latched as it would be on a real VIA so do the same here.
  816. *
  817. * Note: DRQ is negative edge triggered
  818. */
  819. val &= ~VIA2_IRQ_SCSI_DATA;
  820. val |= (~ms->last_irq_levels & VIA2_IRQ_SCSI_DATA);
  821. break;
  822. }
  823. return val;
  824. }
  825. static void mos6522_q800_via2_write(void *opaque, hwaddr addr, uint64_t val,
  826. unsigned size)
  827. {
  828. MOS6522Q800VIA2State *s = MOS6522_Q800_VIA2(opaque);
  829. MOS6522State *ms = MOS6522(s);
  830. addr = (addr >> 9) & 0xf;
  831. mos6522_write(ms, addr, val, size);
  832. }
  833. static const MemoryRegionOps mos6522_q800_via2_ops = {
  834. .read = mos6522_q800_via2_read,
  835. .write = mos6522_q800_via2_write,
  836. .endianness = DEVICE_BIG_ENDIAN,
  837. .valid = {
  838. .min_access_size = 1,
  839. .max_access_size = 4,
  840. },
  841. };
  842. static void via1_postload_update_cb(void *opaque, bool running, RunState state)
  843. {
  844. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  845. qemu_del_vm_change_state_handler(v1s->vmstate);
  846. v1s->vmstate = NULL;
  847. pram_update(v1s);
  848. }
  849. static int via1_post_load(void *opaque, int version_id)
  850. {
  851. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  852. if (v1s->blk) {
  853. v1s->vmstate = qemu_add_vm_change_state_handler(
  854. via1_postload_update_cb, v1s);
  855. }
  856. return 0;
  857. }
  858. /* VIA 1 */
  859. static void mos6522_q800_via1_reset_hold(Object *obj)
  860. {
  861. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(obj);
  862. MOS6522State *ms = MOS6522(v1s);
  863. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(ms);
  864. ADBBusState *adb_bus = &v1s->adb_bus;
  865. if (mdc->parent_phases.hold) {
  866. mdc->parent_phases.hold(obj);
  867. }
  868. ms->timers[0].frequency = VIA_TIMER_FREQ;
  869. ms->timers[1].frequency = VIA_TIMER_FREQ;
  870. ms->b = VIA1B_vADB_StateMask | VIA1B_vADBInt | VIA1B_vRTCEnb;
  871. /* ADB/RTC */
  872. adb_set_autopoll_enabled(adb_bus, true);
  873. v1s->cmd = REG_EMPTY;
  874. v1s->alt = REG_EMPTY;
  875. }
  876. static void mos6522_q800_via1_realize(DeviceState *dev, Error **errp)
  877. {
  878. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(dev);
  879. ADBBusState *adb_bus = &v1s->adb_bus;
  880. struct tm tm;
  881. int ret;
  882. v1s->one_second_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, via1_one_second,
  883. v1s);
  884. via1_one_second_update(v1s);
  885. v1s->sixty_hz_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, via1_sixty_hz,
  886. v1s);
  887. via1_sixty_hz_update(v1s);
  888. qemu_get_timedate(&tm, 0);
  889. v1s->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
  890. adb_register_autopoll_callback(adb_bus, adb_via_poll, v1s);
  891. v1s->adb_data_ready = qdev_get_gpio_in(dev, VIA1_IRQ_ADB_READY_BIT);
  892. if (v1s->blk) {
  893. int64_t len = blk_getlength(v1s->blk);
  894. if (len < 0) {
  895. error_setg_errno(errp, -len,
  896. "could not get length of backing image");
  897. return;
  898. }
  899. ret = blk_set_perm(v1s->blk,
  900. BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
  901. BLK_PERM_ALL, errp);
  902. if (ret < 0) {
  903. return;
  904. }
  905. ret = blk_pread(v1s->blk, 0, sizeof(v1s->PRAM), v1s->PRAM, 0);
  906. if (ret < 0) {
  907. error_setg(errp, "can't read PRAM contents");
  908. return;
  909. }
  910. }
  911. }
  912. static void mos6522_q800_via1_init(Object *obj)
  913. {
  914. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(obj);
  915. SysBusDevice *sbd = SYS_BUS_DEVICE(v1s);
  916. memory_region_init_io(&v1s->via_mem, obj, &mos6522_q800_via1_ops, v1s,
  917. "via1", VIA_SIZE);
  918. sysbus_init_mmio(sbd, &v1s->via_mem);
  919. /* ADB */
  920. qbus_init((BusState *)&v1s->adb_bus, sizeof(v1s->adb_bus),
  921. TYPE_ADB_BUS, DEVICE(v1s), "adb.0");
  922. /* A/UX mode */
  923. qdev_init_gpio_out(DEVICE(obj), &v1s->auxmode_irq, 1);
  924. }
  925. static const VMStateDescription vmstate_q800_via1 = {
  926. .name = "q800-via1",
  927. .version_id = 0,
  928. .minimum_version_id = 0,
  929. .post_load = via1_post_load,
  930. .fields = (VMStateField[]) {
  931. VMSTATE_STRUCT(parent_obj, MOS6522Q800VIA1State, 0, vmstate_mos6522,
  932. MOS6522State),
  933. VMSTATE_UINT8(last_b, MOS6522Q800VIA1State),
  934. /* RTC */
  935. VMSTATE_BUFFER(PRAM, MOS6522Q800VIA1State),
  936. VMSTATE_UINT32(tick_offset, MOS6522Q800VIA1State),
  937. VMSTATE_UINT8(data_out, MOS6522Q800VIA1State),
  938. VMSTATE_INT32(data_out_cnt, MOS6522Q800VIA1State),
  939. VMSTATE_UINT8(data_in, MOS6522Q800VIA1State),
  940. VMSTATE_UINT8(data_in_cnt, MOS6522Q800VIA1State),
  941. VMSTATE_UINT8(cmd, MOS6522Q800VIA1State),
  942. VMSTATE_INT32(wprotect, MOS6522Q800VIA1State),
  943. VMSTATE_INT32(alt, MOS6522Q800VIA1State),
  944. /* ADB */
  945. VMSTATE_INT32(adb_data_in_size, MOS6522Q800VIA1State),
  946. VMSTATE_INT32(adb_data_in_index, MOS6522Q800VIA1State),
  947. VMSTATE_INT32(adb_data_out_index, MOS6522Q800VIA1State),
  948. VMSTATE_BUFFER(adb_data_in, MOS6522Q800VIA1State),
  949. VMSTATE_BUFFER(adb_data_out, MOS6522Q800VIA1State),
  950. VMSTATE_UINT8(adb_autopoll_cmd, MOS6522Q800VIA1State),
  951. /* Timers */
  952. VMSTATE_TIMER_PTR(one_second_timer, MOS6522Q800VIA1State),
  953. VMSTATE_INT64(next_second, MOS6522Q800VIA1State),
  954. VMSTATE_TIMER_PTR(sixty_hz_timer, MOS6522Q800VIA1State),
  955. VMSTATE_INT64(next_sixty_hz, MOS6522Q800VIA1State),
  956. VMSTATE_END_OF_LIST()
  957. }
  958. };
  959. static Property mos6522_q800_via1_properties[] = {
  960. DEFINE_PROP_DRIVE("drive", MOS6522Q800VIA1State, blk),
  961. DEFINE_PROP_END_OF_LIST(),
  962. };
  963. static void mos6522_q800_via1_class_init(ObjectClass *oc, void *data)
  964. {
  965. DeviceClass *dc = DEVICE_CLASS(oc);
  966. ResettableClass *rc = RESETTABLE_CLASS(oc);
  967. MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
  968. dc->realize = mos6522_q800_via1_realize;
  969. resettable_class_set_parent_phases(rc, NULL, mos6522_q800_via1_reset_hold,
  970. NULL, &mdc->parent_phases);
  971. dc->vmsd = &vmstate_q800_via1;
  972. device_class_set_props(dc, mos6522_q800_via1_properties);
  973. }
  974. static const TypeInfo mos6522_q800_via1_type_info = {
  975. .name = TYPE_MOS6522_Q800_VIA1,
  976. .parent = TYPE_MOS6522,
  977. .instance_size = sizeof(MOS6522Q800VIA1State),
  978. .instance_init = mos6522_q800_via1_init,
  979. .class_init = mos6522_q800_via1_class_init,
  980. };
  981. /* VIA 2 */
  982. static void mos6522_q800_via2_portB_write(MOS6522State *s)
  983. {
  984. if (s->dirb & VIA2B_vPower && (s->b & VIA2B_vPower) == 0) {
  985. /* shutdown */
  986. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  987. }
  988. }
  989. static void mos6522_q800_via2_reset_hold(Object *obj)
  990. {
  991. MOS6522State *ms = MOS6522(obj);
  992. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(ms);
  993. if (mdc->parent_phases.hold) {
  994. mdc->parent_phases.hold(obj);
  995. }
  996. ms->timers[0].frequency = VIA_TIMER_FREQ;
  997. ms->timers[1].frequency = VIA_TIMER_FREQ;
  998. ms->dirb = 0;
  999. ms->b = 0;
  1000. ms->dira = 0;
  1001. ms->a = 0x7f;
  1002. }
  1003. static void via2_nubus_irq_request(void *opaque, int n, int level)
  1004. {
  1005. MOS6522Q800VIA2State *v2s = opaque;
  1006. MOS6522State *s = MOS6522(v2s);
  1007. qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA2_IRQ_NUBUS_BIT);
  1008. if (level) {
  1009. /* Port A nubus IRQ inputs are active LOW */
  1010. s->a &= ~(1 << n);
  1011. } else {
  1012. s->a |= (1 << n);
  1013. }
  1014. /* Negative edge trigger */
  1015. qemu_set_irq(irq, !level);
  1016. }
  1017. static void mos6522_q800_via2_init(Object *obj)
  1018. {
  1019. MOS6522Q800VIA2State *v2s = MOS6522_Q800_VIA2(obj);
  1020. SysBusDevice *sbd = SYS_BUS_DEVICE(v2s);
  1021. memory_region_init_io(&v2s->via_mem, obj, &mos6522_q800_via2_ops, v2s,
  1022. "via2", VIA_SIZE);
  1023. sysbus_init_mmio(sbd, &v2s->via_mem);
  1024. qdev_init_gpio_in_named(DEVICE(obj), via2_nubus_irq_request, "nubus-irq",
  1025. VIA2_NUBUS_IRQ_NB);
  1026. }
  1027. static const VMStateDescription vmstate_q800_via2 = {
  1028. .name = "q800-via2",
  1029. .version_id = 0,
  1030. .minimum_version_id = 0,
  1031. .fields = (VMStateField[]) {
  1032. VMSTATE_STRUCT(parent_obj, MOS6522Q800VIA2State, 0, vmstate_mos6522,
  1033. MOS6522State),
  1034. VMSTATE_END_OF_LIST()
  1035. }
  1036. };
  1037. static void mos6522_q800_via2_class_init(ObjectClass *oc, void *data)
  1038. {
  1039. DeviceClass *dc = DEVICE_CLASS(oc);
  1040. ResettableClass *rc = RESETTABLE_CLASS(oc);
  1041. MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
  1042. resettable_class_set_parent_phases(rc, NULL, mos6522_q800_via2_reset_hold,
  1043. NULL, &mdc->parent_phases);
  1044. dc->vmsd = &vmstate_q800_via2;
  1045. mdc->portB_write = mos6522_q800_via2_portB_write;
  1046. }
  1047. static const TypeInfo mos6522_q800_via2_type_info = {
  1048. .name = TYPE_MOS6522_Q800_VIA2,
  1049. .parent = TYPE_MOS6522,
  1050. .instance_size = sizeof(MOS6522Q800VIA2State),
  1051. .instance_init = mos6522_q800_via2_init,
  1052. .class_init = mos6522_q800_via2_class_init,
  1053. };
  1054. static void mac_via_register_types(void)
  1055. {
  1056. type_register_static(&mos6522_q800_via1_type_info);
  1057. type_register_static(&mos6522_q800_via2_type_info);
  1058. }
  1059. type_init(mac_via_register_types);