mos6522.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * QEMU MOS6522 VIA emulation
  3. *
  4. * Copyright (c) 2004-2007 Fabrice Bellard
  5. * Copyright (c) 2007 Jocelyn Mayer
  6. * Copyright (c) 2018 Mark Cave-Ayland
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "hw/input/adb.h"
  28. #include "hw/irq.h"
  29. #include "hw/misc/mos6522.h"
  30. #include "hw/qdev-properties.h"
  31. #include "migration/vmstate.h"
  32. #include "qemu/timer.h"
  33. #include "qemu/cutils.h"
  34. #include "qemu/log.h"
  35. #include "qemu/module.h"
  36. #include "trace.h"
  37. /* XXX: implement all timer modes */
  38. static void mos6522_timer1_update(MOS6522State *s, MOS6522Timer *ti,
  39. int64_t current_time);
  40. static void mos6522_timer2_update(MOS6522State *s, MOS6522Timer *ti,
  41. int64_t current_time);
  42. static void mos6522_update_irq(MOS6522State *s)
  43. {
  44. if (s->ifr & s->ier) {
  45. qemu_irq_raise(s->irq);
  46. } else {
  47. qemu_irq_lower(s->irq);
  48. }
  49. }
  50. static uint64_t get_counter_value(MOS6522State *s, MOS6522Timer *ti)
  51. {
  52. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(s);
  53. if (ti->index == 0) {
  54. return mdc->get_timer1_counter_value(s, ti);
  55. } else {
  56. return mdc->get_timer2_counter_value(s, ti);
  57. }
  58. }
  59. static uint64_t get_load_time(MOS6522State *s, MOS6522Timer *ti)
  60. {
  61. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(s);
  62. if (ti->index == 0) {
  63. return mdc->get_timer1_load_time(s, ti);
  64. } else {
  65. return mdc->get_timer2_load_time(s, ti);
  66. }
  67. }
  68. static unsigned int get_counter(MOS6522State *s, MOS6522Timer *ti)
  69. {
  70. int64_t d;
  71. unsigned int counter;
  72. d = get_counter_value(s, ti);
  73. if (ti->index == 0) {
  74. /* the timer goes down from latch to -1 (period of latch + 2) */
  75. if (d <= (ti->counter_value + 1)) {
  76. counter = (ti->counter_value - d) & 0xffff;
  77. } else {
  78. counter = (d - (ti->counter_value + 1)) % (ti->latch + 2);
  79. counter = (ti->latch - counter) & 0xffff;
  80. }
  81. } else {
  82. counter = (ti->counter_value - d) & 0xffff;
  83. }
  84. return counter;
  85. }
  86. static void set_counter(MOS6522State *s, MOS6522Timer *ti, unsigned int val)
  87. {
  88. trace_mos6522_set_counter(1 + ti->index, val);
  89. ti->load_time = get_load_time(s, ti);
  90. ti->counter_value = val;
  91. if (ti->index == 0) {
  92. mos6522_timer1_update(s, ti, ti->load_time);
  93. } else {
  94. mos6522_timer2_update(s, ti, ti->load_time);
  95. }
  96. }
  97. static int64_t get_next_irq_time(MOS6522State *s, MOS6522Timer *ti,
  98. int64_t current_time)
  99. {
  100. int64_t d, next_time;
  101. unsigned int counter;
  102. if (ti->frequency == 0) {
  103. return INT64_MAX;
  104. }
  105. /* current counter value */
  106. d = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - ti->load_time,
  107. ti->frequency, NANOSECONDS_PER_SECOND);
  108. /* the timer goes down from latch to -1 (period of latch + 2) */
  109. if (d <= (ti->counter_value + 1)) {
  110. counter = (ti->counter_value - d) & 0xffff;
  111. } else {
  112. counter = (d - (ti->counter_value + 1)) % (ti->latch + 2);
  113. counter = (ti->latch - counter) & 0xffff;
  114. }
  115. /* Note: we consider the irq is raised on 0 */
  116. if (counter == 0xffff) {
  117. next_time = d + ti->latch + 1;
  118. } else if (counter == 0) {
  119. next_time = d + ti->latch + 2;
  120. } else {
  121. next_time = d + counter;
  122. }
  123. trace_mos6522_get_next_irq_time(ti->latch, d, next_time - d);
  124. next_time = muldiv64(next_time, NANOSECONDS_PER_SECOND, ti->frequency) +
  125. ti->load_time;
  126. if (next_time <= current_time) {
  127. next_time = current_time + 1;
  128. }
  129. return next_time;
  130. }
  131. static void mos6522_timer1_update(MOS6522State *s, MOS6522Timer *ti,
  132. int64_t current_time)
  133. {
  134. if (!ti->timer) {
  135. return;
  136. }
  137. ti->next_irq_time = get_next_irq_time(s, ti, current_time);
  138. if ((s->ier & T1_INT) == 0 || (s->acr & T1MODE) != T1MODE_CONT) {
  139. timer_del(ti->timer);
  140. } else {
  141. timer_mod(ti->timer, ti->next_irq_time);
  142. }
  143. }
  144. static void mos6522_timer2_update(MOS6522State *s, MOS6522Timer *ti,
  145. int64_t current_time)
  146. {
  147. if (!ti->timer) {
  148. return;
  149. }
  150. ti->next_irq_time = get_next_irq_time(s, ti, current_time);
  151. if ((s->ier & T2_INT) == 0) {
  152. timer_del(ti->timer);
  153. } else {
  154. timer_mod(ti->timer, ti->next_irq_time);
  155. }
  156. }
  157. static void mos6522_timer1(void *opaque)
  158. {
  159. MOS6522State *s = opaque;
  160. MOS6522Timer *ti = &s->timers[0];
  161. mos6522_timer1_update(s, ti, ti->next_irq_time);
  162. s->ifr |= T1_INT;
  163. mos6522_update_irq(s);
  164. }
  165. static void mos6522_timer2(void *opaque)
  166. {
  167. MOS6522State *s = opaque;
  168. MOS6522Timer *ti = &s->timers[1];
  169. mos6522_timer2_update(s, ti, ti->next_irq_time);
  170. s->ifr |= T2_INT;
  171. mos6522_update_irq(s);
  172. }
  173. static void mos6522_set_sr_int(MOS6522State *s)
  174. {
  175. trace_mos6522_set_sr_int();
  176. s->ifr |= SR_INT;
  177. mos6522_update_irq(s);
  178. }
  179. static uint64_t mos6522_get_counter_value(MOS6522State *s, MOS6522Timer *ti)
  180. {
  181. return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - ti->load_time,
  182. ti->frequency, NANOSECONDS_PER_SECOND);
  183. }
  184. static uint64_t mos6522_get_load_time(MOS6522State *s, MOS6522Timer *ti)
  185. {
  186. uint64_t load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  187. return load_time;
  188. }
  189. static void mos6522_portA_write(MOS6522State *s)
  190. {
  191. qemu_log_mask(LOG_UNIMP, "portA_write unimplemented\n");
  192. }
  193. static void mos6522_portB_write(MOS6522State *s)
  194. {
  195. qemu_log_mask(LOG_UNIMP, "portB_write unimplemented\n");
  196. }
  197. uint64_t mos6522_read(void *opaque, hwaddr addr, unsigned size)
  198. {
  199. MOS6522State *s = opaque;
  200. uint32_t val;
  201. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  202. if (now >= s->timers[0].next_irq_time) {
  203. mos6522_timer1_update(s, &s->timers[0], now);
  204. s->ifr |= T1_INT;
  205. }
  206. if (now >= s->timers[1].next_irq_time) {
  207. mos6522_timer2_update(s, &s->timers[1], now);
  208. s->ifr |= T2_INT;
  209. }
  210. switch (addr) {
  211. case VIA_REG_B:
  212. val = s->b;
  213. break;
  214. case VIA_REG_A:
  215. qemu_log_mask(LOG_UNIMP, "Read access to register A with handshake");
  216. /* fall through */
  217. case VIA_REG_ANH:
  218. val = s->a;
  219. break;
  220. case VIA_REG_DIRB:
  221. val = s->dirb;
  222. break;
  223. case VIA_REG_DIRA:
  224. val = s->dira;
  225. break;
  226. case VIA_REG_T1CL:
  227. val = get_counter(s, &s->timers[0]) & 0xff;
  228. s->ifr &= ~T1_INT;
  229. mos6522_update_irq(s);
  230. break;
  231. case VIA_REG_T1CH:
  232. val = get_counter(s, &s->timers[0]) >> 8;
  233. mos6522_update_irq(s);
  234. break;
  235. case VIA_REG_T1LL:
  236. val = s->timers[0].latch & 0xff;
  237. break;
  238. case VIA_REG_T1LH:
  239. /* XXX: check this */
  240. val = (s->timers[0].latch >> 8) & 0xff;
  241. break;
  242. case VIA_REG_T2CL:
  243. val = get_counter(s, &s->timers[1]) & 0xff;
  244. s->ifr &= ~T2_INT;
  245. mos6522_update_irq(s);
  246. break;
  247. case VIA_REG_T2CH:
  248. val = get_counter(s, &s->timers[1]) >> 8;
  249. break;
  250. case VIA_REG_SR:
  251. val = s->sr;
  252. s->ifr &= ~SR_INT;
  253. mos6522_update_irq(s);
  254. break;
  255. case VIA_REG_ACR:
  256. val = s->acr;
  257. break;
  258. case VIA_REG_PCR:
  259. val = s->pcr;
  260. break;
  261. case VIA_REG_IFR:
  262. val = s->ifr;
  263. if (s->ifr & s->ier) {
  264. val |= 0x80;
  265. }
  266. break;
  267. case VIA_REG_IER:
  268. val = s->ier | 0x80;
  269. break;
  270. default:
  271. g_assert_not_reached();
  272. }
  273. if (addr != VIA_REG_IFR || val != 0) {
  274. trace_mos6522_read(addr, val);
  275. }
  276. return val;
  277. }
  278. void mos6522_write(void *opaque, hwaddr addr, uint64_t val, unsigned size)
  279. {
  280. MOS6522State *s = opaque;
  281. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(s);
  282. trace_mos6522_write(addr, val);
  283. switch (addr) {
  284. case VIA_REG_B:
  285. s->b = (s->b & ~s->dirb) | (val & s->dirb);
  286. mdc->portB_write(s);
  287. break;
  288. case VIA_REG_A:
  289. qemu_log_mask(LOG_UNIMP, "Write access to register A with handshake");
  290. /* fall through */
  291. case VIA_REG_ANH:
  292. s->a = (s->a & ~s->dira) | (val & s->dira);
  293. mdc->portA_write(s);
  294. break;
  295. case VIA_REG_DIRB:
  296. s->dirb = val;
  297. break;
  298. case VIA_REG_DIRA:
  299. s->dira = val;
  300. break;
  301. case VIA_REG_T1CL:
  302. s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
  303. mos6522_timer1_update(s, &s->timers[0],
  304. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  305. break;
  306. case VIA_REG_T1CH:
  307. s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
  308. s->ifr &= ~T1_INT;
  309. set_counter(s, &s->timers[0], s->timers[0].latch);
  310. break;
  311. case VIA_REG_T1LL:
  312. s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
  313. mos6522_timer1_update(s, &s->timers[0],
  314. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  315. break;
  316. case VIA_REG_T1LH:
  317. s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
  318. s->ifr &= ~T1_INT;
  319. mos6522_timer1_update(s, &s->timers[0],
  320. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  321. break;
  322. case VIA_REG_T2CL:
  323. s->timers[1].latch = (s->timers[1].latch & 0xff00) | val;
  324. break;
  325. case VIA_REG_T2CH:
  326. /* To ensure T2 generates an interrupt on zero crossing with the
  327. common timer code, write the value directly from the latch to
  328. the counter */
  329. s->timers[1].latch = (s->timers[1].latch & 0xff) | (val << 8);
  330. s->ifr &= ~T2_INT;
  331. set_counter(s, &s->timers[1], s->timers[1].latch);
  332. break;
  333. case VIA_REG_SR:
  334. s->sr = val;
  335. break;
  336. case VIA_REG_ACR:
  337. s->acr = val;
  338. mos6522_timer1_update(s, &s->timers[0],
  339. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  340. break;
  341. case VIA_REG_PCR:
  342. s->pcr = val;
  343. break;
  344. case VIA_REG_IFR:
  345. /* reset bits */
  346. s->ifr &= ~val;
  347. mos6522_update_irq(s);
  348. break;
  349. case VIA_REG_IER:
  350. if (val & IER_SET) {
  351. /* set bits */
  352. s->ier |= val & 0x7f;
  353. } else {
  354. /* reset bits */
  355. s->ier &= ~val;
  356. }
  357. mos6522_update_irq(s);
  358. /* if IER is modified starts needed timers */
  359. mos6522_timer1_update(s, &s->timers[0],
  360. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  361. mos6522_timer2_update(s, &s->timers[1],
  362. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  363. break;
  364. default:
  365. g_assert_not_reached();
  366. }
  367. }
  368. static const MemoryRegionOps mos6522_ops = {
  369. .read = mos6522_read,
  370. .write = mos6522_write,
  371. .endianness = DEVICE_NATIVE_ENDIAN,
  372. .valid = {
  373. .min_access_size = 1,
  374. .max_access_size = 1,
  375. },
  376. };
  377. static const VMStateDescription vmstate_mos6522_timer = {
  378. .name = "mos6522_timer",
  379. .version_id = 0,
  380. .minimum_version_id = 0,
  381. .fields = (VMStateField[]) {
  382. VMSTATE_UINT16(latch, MOS6522Timer),
  383. VMSTATE_UINT16(counter_value, MOS6522Timer),
  384. VMSTATE_INT64(load_time, MOS6522Timer),
  385. VMSTATE_INT64(next_irq_time, MOS6522Timer),
  386. VMSTATE_TIMER_PTR(timer, MOS6522Timer),
  387. VMSTATE_END_OF_LIST()
  388. }
  389. };
  390. const VMStateDescription vmstate_mos6522 = {
  391. .name = "mos6522",
  392. .version_id = 0,
  393. .minimum_version_id = 0,
  394. .fields = (VMStateField[]) {
  395. VMSTATE_UINT8(a, MOS6522State),
  396. VMSTATE_UINT8(b, MOS6522State),
  397. VMSTATE_UINT8(dira, MOS6522State),
  398. VMSTATE_UINT8(dirb, MOS6522State),
  399. VMSTATE_UINT8(sr, MOS6522State),
  400. VMSTATE_UINT8(acr, MOS6522State),
  401. VMSTATE_UINT8(pcr, MOS6522State),
  402. VMSTATE_UINT8(ifr, MOS6522State),
  403. VMSTATE_UINT8(ier, MOS6522State),
  404. VMSTATE_STRUCT_ARRAY(timers, MOS6522State, 2, 0,
  405. vmstate_mos6522_timer, MOS6522Timer),
  406. VMSTATE_END_OF_LIST()
  407. }
  408. };
  409. static void mos6522_reset(DeviceState *dev)
  410. {
  411. MOS6522State *s = MOS6522(dev);
  412. s->b = 0;
  413. s->a = 0;
  414. s->dirb = 0xff;
  415. s->dira = 0;
  416. s->sr = 0;
  417. s->acr = 0;
  418. s->pcr = 0;
  419. s->ifr = 0;
  420. s->ier = 0;
  421. /* s->ier = T1_INT | SR_INT; */
  422. s->timers[0].frequency = s->frequency;
  423. s->timers[0].latch = 0xffff;
  424. set_counter(s, &s->timers[0], 0xffff);
  425. timer_del(s->timers[0].timer);
  426. s->timers[1].frequency = s->frequency;
  427. s->timers[1].latch = 0xffff;
  428. timer_del(s->timers[1].timer);
  429. }
  430. static void mos6522_init(Object *obj)
  431. {
  432. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  433. MOS6522State *s = MOS6522(obj);
  434. int i;
  435. memory_region_init_io(&s->mem, obj, &mos6522_ops, s, "mos6522", 0x10);
  436. sysbus_init_mmio(sbd, &s->mem);
  437. sysbus_init_irq(sbd, &s->irq);
  438. for (i = 0; i < ARRAY_SIZE(s->timers); i++) {
  439. s->timers[i].index = i;
  440. }
  441. s->timers[0].timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, mos6522_timer1, s);
  442. s->timers[1].timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, mos6522_timer2, s);
  443. }
  444. static Property mos6522_properties[] = {
  445. DEFINE_PROP_UINT64("frequency", MOS6522State, frequency, 0),
  446. DEFINE_PROP_END_OF_LIST()
  447. };
  448. static void mos6522_class_init(ObjectClass *oc, void *data)
  449. {
  450. DeviceClass *dc = DEVICE_CLASS(oc);
  451. MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
  452. dc->reset = mos6522_reset;
  453. dc->vmsd = &vmstate_mos6522;
  454. device_class_set_props(dc, mos6522_properties);
  455. mdc->parent_reset = dc->reset;
  456. mdc->set_sr_int = mos6522_set_sr_int;
  457. mdc->portB_write = mos6522_portB_write;
  458. mdc->portA_write = mos6522_portA_write;
  459. mdc->update_irq = mos6522_update_irq;
  460. mdc->get_timer1_counter_value = mos6522_get_counter_value;
  461. mdc->get_timer2_counter_value = mos6522_get_counter_value;
  462. mdc->get_timer1_load_time = mos6522_get_load_time;
  463. mdc->get_timer2_load_time = mos6522_get_load_time;
  464. }
  465. static const TypeInfo mos6522_type_info = {
  466. .name = TYPE_MOS6522,
  467. .parent = TYPE_SYS_BUS_DEVICE,
  468. .instance_size = sizeof(MOS6522State),
  469. .instance_init = mos6522_init,
  470. .abstract = true,
  471. .class_size = sizeof(MOS6522DeviceClass),
  472. .class_init = mos6522_class_init,
  473. };
  474. static void mos6522_register_types(void)
  475. {
  476. type_register_static(&mos6522_type_info);
  477. }
  478. type_init(mos6522_register_types)