cuda.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * QEMU PowerMac CUDA device support
  3. *
  4. * Copyright (c) 2004-2007 Fabrice Bellard
  5. * Copyright (c) 2007 Jocelyn Mayer
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "hw.h"
  26. #include "ppc_mac.h"
  27. #include "qemu-timer.h"
  28. #include "sysemu.h"
  29. /* XXX: implement all timer modes */
  30. /* debug CUDA */
  31. //#define DEBUG_CUDA
  32. /* debug CUDA packets */
  33. //#define DEBUG_CUDA_PACKET
  34. #ifdef DEBUG_CUDA
  35. #define CUDA_DPRINTF(fmt, ...) \
  36. do { printf("CUDA: " fmt , ## __VA_ARGS__); } while (0)
  37. #else
  38. #define CUDA_DPRINTF(fmt, ...)
  39. #endif
  40. /* Bits in B data register: all active low */
  41. #define TREQ 0x08 /* Transfer request (input) */
  42. #define TACK 0x10 /* Transfer acknowledge (output) */
  43. #define TIP 0x20 /* Transfer in progress (output) */
  44. /* Bits in ACR */
  45. #define SR_CTRL 0x1c /* Shift register control bits */
  46. #define SR_EXT 0x0c /* Shift on external clock */
  47. #define SR_OUT 0x10 /* Shift out if 1 */
  48. /* Bits in IFR and IER */
  49. #define IER_SET 0x80 /* set bits in IER */
  50. #define IER_CLR 0 /* clear bits in IER */
  51. #define SR_INT 0x04 /* Shift register full/empty */
  52. #define T1_INT 0x40 /* Timer 1 interrupt */
  53. #define T2_INT 0x20 /* Timer 2 interrupt */
  54. /* Bits in ACR */
  55. #define T1MODE 0xc0 /* Timer 1 mode */
  56. #define T1MODE_CONT 0x40 /* continuous interrupts */
  57. /* commands (1st byte) */
  58. #define ADB_PACKET 0
  59. #define CUDA_PACKET 1
  60. #define ERROR_PACKET 2
  61. #define TIMER_PACKET 3
  62. #define POWER_PACKET 4
  63. #define MACIIC_PACKET 5
  64. #define PMU_PACKET 6
  65. /* CUDA commands (2nd byte) */
  66. #define CUDA_WARM_START 0x0
  67. #define CUDA_AUTOPOLL 0x1
  68. #define CUDA_GET_6805_ADDR 0x2
  69. #define CUDA_GET_TIME 0x3
  70. #define CUDA_GET_PRAM 0x7
  71. #define CUDA_SET_6805_ADDR 0x8
  72. #define CUDA_SET_TIME 0x9
  73. #define CUDA_POWERDOWN 0xa
  74. #define CUDA_POWERUP_TIME 0xb
  75. #define CUDA_SET_PRAM 0xc
  76. #define CUDA_MS_RESET 0xd
  77. #define CUDA_SEND_DFAC 0xe
  78. #define CUDA_BATTERY_SWAP_SENSE 0x10
  79. #define CUDA_RESET_SYSTEM 0x11
  80. #define CUDA_SET_IPL 0x12
  81. #define CUDA_FILE_SERVER_FLAG 0x13
  82. #define CUDA_SET_AUTO_RATE 0x14
  83. #define CUDA_GET_AUTO_RATE 0x16
  84. #define CUDA_SET_DEVICE_LIST 0x19
  85. #define CUDA_GET_DEVICE_LIST 0x1a
  86. #define CUDA_SET_ONE_SECOND_MODE 0x1b
  87. #define CUDA_SET_POWER_MESSAGES 0x21
  88. #define CUDA_GET_SET_IIC 0x22
  89. #define CUDA_WAKEUP 0x23
  90. #define CUDA_TIMER_TICKLE 0x24
  91. #define CUDA_COMBINED_FORMAT_IIC 0x25
  92. #define CUDA_TIMER_FREQ (4700000 / 6)
  93. #define CUDA_ADB_POLL_FREQ 50
  94. /* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */
  95. #define RTC_OFFSET 2082844800
  96. typedef struct CUDATimer {
  97. int index;
  98. uint16_t latch;
  99. uint16_t counter_value; /* counter value at load time */
  100. int64_t load_time;
  101. int64_t next_irq_time;
  102. QEMUTimer *timer;
  103. } CUDATimer;
  104. typedef struct CUDAState {
  105. /* cuda registers */
  106. uint8_t b; /* B-side data */
  107. uint8_t a; /* A-side data */
  108. uint8_t dirb; /* B-side direction (1=output) */
  109. uint8_t dira; /* A-side direction (1=output) */
  110. uint8_t sr; /* Shift register */
  111. uint8_t acr; /* Auxiliary control register */
  112. uint8_t pcr; /* Peripheral control register */
  113. uint8_t ifr; /* Interrupt flag register */
  114. uint8_t ier; /* Interrupt enable register */
  115. uint8_t anh; /* A-side data, no handshake */
  116. CUDATimer timers[2];
  117. uint32_t tick_offset;
  118. uint8_t last_b; /* last value of B register */
  119. uint8_t last_acr; /* last value of B register */
  120. int data_in_size;
  121. int data_in_index;
  122. int data_out_index;
  123. qemu_irq irq;
  124. uint8_t autopoll;
  125. uint8_t data_in[128];
  126. uint8_t data_out[16];
  127. QEMUTimer *adb_poll_timer;
  128. } CUDAState;
  129. static CUDAState cuda_state;
  130. ADBBusState adb_bus;
  131. static void cuda_update(CUDAState *s);
  132. static void cuda_receive_packet_from_host(CUDAState *s,
  133. const uint8_t *data, int len);
  134. static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
  135. int64_t current_time);
  136. static void cuda_update_irq(CUDAState *s)
  137. {
  138. if (s->ifr & s->ier & (SR_INT | T1_INT)) {
  139. qemu_irq_raise(s->irq);
  140. } else {
  141. qemu_irq_lower(s->irq);
  142. }
  143. }
  144. static unsigned int get_counter(CUDATimer *s)
  145. {
  146. int64_t d;
  147. unsigned int counter;
  148. d = muldiv64(qemu_get_clock_ns(vm_clock) - s->load_time,
  149. CUDA_TIMER_FREQ, get_ticks_per_sec());
  150. if (s->index == 0) {
  151. /* the timer goes down from latch to -1 (period of latch + 2) */
  152. if (d <= (s->counter_value + 1)) {
  153. counter = (s->counter_value - d) & 0xffff;
  154. } else {
  155. counter = (d - (s->counter_value + 1)) % (s->latch + 2);
  156. counter = (s->latch - counter) & 0xffff;
  157. }
  158. } else {
  159. counter = (s->counter_value - d) & 0xffff;
  160. }
  161. return counter;
  162. }
  163. static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val)
  164. {
  165. CUDA_DPRINTF("T%d.counter=%d\n", 1 + (ti->timer == NULL), val);
  166. ti->load_time = qemu_get_clock_ns(vm_clock);
  167. ti->counter_value = val;
  168. cuda_timer_update(s, ti, ti->load_time);
  169. }
  170. static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time)
  171. {
  172. int64_t d, next_time;
  173. unsigned int counter;
  174. /* current counter value */
  175. d = muldiv64(current_time - s->load_time,
  176. CUDA_TIMER_FREQ, get_ticks_per_sec());
  177. /* the timer goes down from latch to -1 (period of latch + 2) */
  178. if (d <= (s->counter_value + 1)) {
  179. counter = (s->counter_value - d) & 0xffff;
  180. } else {
  181. counter = (d - (s->counter_value + 1)) % (s->latch + 2);
  182. counter = (s->latch - counter) & 0xffff;
  183. }
  184. /* Note: we consider the irq is raised on 0 */
  185. if (counter == 0xffff) {
  186. next_time = d + s->latch + 1;
  187. } else if (counter == 0) {
  188. next_time = d + s->latch + 2;
  189. } else {
  190. next_time = d + counter;
  191. }
  192. CUDA_DPRINTF("latch=%d counter=%" PRId64 " delta_next=%" PRId64 "\n",
  193. s->latch, d, next_time - d);
  194. next_time = muldiv64(next_time, get_ticks_per_sec(), CUDA_TIMER_FREQ) +
  195. s->load_time;
  196. if (next_time <= current_time)
  197. next_time = current_time + 1;
  198. return next_time;
  199. }
  200. static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
  201. int64_t current_time)
  202. {
  203. if (!ti->timer)
  204. return;
  205. if ((s->acr & T1MODE) != T1MODE_CONT) {
  206. qemu_del_timer(ti->timer);
  207. } else {
  208. ti->next_irq_time = get_next_irq_time(ti, current_time);
  209. qemu_mod_timer(ti->timer, ti->next_irq_time);
  210. }
  211. }
  212. static void cuda_timer1(void *opaque)
  213. {
  214. CUDAState *s = opaque;
  215. CUDATimer *ti = &s->timers[0];
  216. cuda_timer_update(s, ti, ti->next_irq_time);
  217. s->ifr |= T1_INT;
  218. cuda_update_irq(s);
  219. }
  220. static uint32_t cuda_readb(void *opaque, target_phys_addr_t addr)
  221. {
  222. CUDAState *s = opaque;
  223. uint32_t val;
  224. addr = (addr >> 9) & 0xf;
  225. switch(addr) {
  226. case 0:
  227. val = s->b;
  228. break;
  229. case 1:
  230. val = s->a;
  231. break;
  232. case 2:
  233. val = s->dirb;
  234. break;
  235. case 3:
  236. val = s->dira;
  237. break;
  238. case 4:
  239. val = get_counter(&s->timers[0]) & 0xff;
  240. s->ifr &= ~T1_INT;
  241. cuda_update_irq(s);
  242. break;
  243. case 5:
  244. val = get_counter(&s->timers[0]) >> 8;
  245. cuda_update_irq(s);
  246. break;
  247. case 6:
  248. val = s->timers[0].latch & 0xff;
  249. break;
  250. case 7:
  251. /* XXX: check this */
  252. val = (s->timers[0].latch >> 8) & 0xff;
  253. break;
  254. case 8:
  255. val = get_counter(&s->timers[1]) & 0xff;
  256. s->ifr &= ~T2_INT;
  257. break;
  258. case 9:
  259. val = get_counter(&s->timers[1]) >> 8;
  260. break;
  261. case 10:
  262. val = s->sr;
  263. s->ifr &= ~SR_INT;
  264. cuda_update_irq(s);
  265. break;
  266. case 11:
  267. val = s->acr;
  268. break;
  269. case 12:
  270. val = s->pcr;
  271. break;
  272. case 13:
  273. val = s->ifr;
  274. if (s->ifr & s->ier)
  275. val |= 0x80;
  276. break;
  277. case 14:
  278. val = s->ier | 0x80;
  279. break;
  280. default:
  281. case 15:
  282. val = s->anh;
  283. break;
  284. }
  285. if (addr != 13 || val != 0) {
  286. CUDA_DPRINTF("read: reg=0x%x val=%02x\n", (int)addr, val);
  287. }
  288. return val;
  289. }
  290. static void cuda_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  291. {
  292. CUDAState *s = opaque;
  293. addr = (addr >> 9) & 0xf;
  294. CUDA_DPRINTF("write: reg=0x%x val=%02x\n", (int)addr, val);
  295. switch(addr) {
  296. case 0:
  297. s->b = val;
  298. cuda_update(s);
  299. break;
  300. case 1:
  301. s->a = val;
  302. break;
  303. case 2:
  304. s->dirb = val;
  305. break;
  306. case 3:
  307. s->dira = val;
  308. break;
  309. case 4:
  310. s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
  311. cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
  312. break;
  313. case 5:
  314. s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
  315. s->ifr &= ~T1_INT;
  316. set_counter(s, &s->timers[0], s->timers[0].latch);
  317. break;
  318. case 6:
  319. s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
  320. cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
  321. break;
  322. case 7:
  323. s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
  324. s->ifr &= ~T1_INT;
  325. cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
  326. break;
  327. case 8:
  328. s->timers[1].latch = val;
  329. set_counter(s, &s->timers[1], val);
  330. break;
  331. case 9:
  332. set_counter(s, &s->timers[1], (val << 8) | s->timers[1].latch);
  333. break;
  334. case 10:
  335. s->sr = val;
  336. break;
  337. case 11:
  338. s->acr = val;
  339. cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
  340. cuda_update(s);
  341. break;
  342. case 12:
  343. s->pcr = val;
  344. break;
  345. case 13:
  346. /* reset bits */
  347. s->ifr &= ~val;
  348. cuda_update_irq(s);
  349. break;
  350. case 14:
  351. if (val & IER_SET) {
  352. /* set bits */
  353. s->ier |= val & 0x7f;
  354. } else {
  355. /* reset bits */
  356. s->ier &= ~val;
  357. }
  358. cuda_update_irq(s);
  359. break;
  360. default:
  361. case 15:
  362. s->anh = val;
  363. break;
  364. }
  365. }
  366. /* NOTE: TIP and TREQ are negated */
  367. static void cuda_update(CUDAState *s)
  368. {
  369. int packet_received, len;
  370. packet_received = 0;
  371. if (!(s->b & TIP)) {
  372. /* transfer requested from host */
  373. if (s->acr & SR_OUT) {
  374. /* data output */
  375. if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
  376. if (s->data_out_index < sizeof(s->data_out)) {
  377. CUDA_DPRINTF("send: %02x\n", s->sr);
  378. s->data_out[s->data_out_index++] = s->sr;
  379. s->ifr |= SR_INT;
  380. cuda_update_irq(s);
  381. }
  382. }
  383. } else {
  384. if (s->data_in_index < s->data_in_size) {
  385. /* data input */
  386. if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
  387. s->sr = s->data_in[s->data_in_index++];
  388. CUDA_DPRINTF("recv: %02x\n", s->sr);
  389. /* indicate end of transfer */
  390. if (s->data_in_index >= s->data_in_size) {
  391. s->b = (s->b | TREQ);
  392. }
  393. s->ifr |= SR_INT;
  394. cuda_update_irq(s);
  395. }
  396. }
  397. }
  398. } else {
  399. /* no transfer requested: handle sync case */
  400. if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) {
  401. /* update TREQ state each time TACK change state */
  402. if (s->b & TACK)
  403. s->b = (s->b | TREQ);
  404. else
  405. s->b = (s->b & ~TREQ);
  406. s->ifr |= SR_INT;
  407. cuda_update_irq(s);
  408. } else {
  409. if (!(s->last_b & TIP)) {
  410. /* handle end of host to cuda transfer */
  411. packet_received = (s->data_out_index > 0);
  412. /* always an IRQ at the end of transfer */
  413. s->ifr |= SR_INT;
  414. cuda_update_irq(s);
  415. }
  416. /* signal if there is data to read */
  417. if (s->data_in_index < s->data_in_size) {
  418. s->b = (s->b & ~TREQ);
  419. }
  420. }
  421. }
  422. s->last_acr = s->acr;
  423. s->last_b = s->b;
  424. /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
  425. recursively */
  426. if (packet_received) {
  427. len = s->data_out_index;
  428. s->data_out_index = 0;
  429. cuda_receive_packet_from_host(s, s->data_out, len);
  430. }
  431. }
  432. static void cuda_send_packet_to_host(CUDAState *s,
  433. const uint8_t *data, int len)
  434. {
  435. #ifdef DEBUG_CUDA_PACKET
  436. {
  437. int i;
  438. printf("cuda_send_packet_to_host:\n");
  439. for(i = 0; i < len; i++)
  440. printf(" %02x", data[i]);
  441. printf("\n");
  442. }
  443. #endif
  444. memcpy(s->data_in, data, len);
  445. s->data_in_size = len;
  446. s->data_in_index = 0;
  447. cuda_update(s);
  448. s->ifr |= SR_INT;
  449. cuda_update_irq(s);
  450. }
  451. static void cuda_adb_poll(void *opaque)
  452. {
  453. CUDAState *s = opaque;
  454. uint8_t obuf[ADB_MAX_OUT_LEN + 2];
  455. int olen;
  456. olen = adb_poll(&adb_bus, obuf + 2);
  457. if (olen > 0) {
  458. obuf[0] = ADB_PACKET;
  459. obuf[1] = 0x40; /* polled data */
  460. cuda_send_packet_to_host(s, obuf, olen + 2);
  461. }
  462. qemu_mod_timer(s->adb_poll_timer,
  463. qemu_get_clock_ns(vm_clock) +
  464. (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ));
  465. }
  466. static void cuda_receive_packet(CUDAState *s,
  467. const uint8_t *data, int len)
  468. {
  469. uint8_t obuf[16];
  470. int autopoll;
  471. uint32_t ti;
  472. switch(data[0]) {
  473. case CUDA_AUTOPOLL:
  474. autopoll = (data[1] != 0);
  475. if (autopoll != s->autopoll) {
  476. s->autopoll = autopoll;
  477. if (autopoll) {
  478. qemu_mod_timer(s->adb_poll_timer,
  479. qemu_get_clock_ns(vm_clock) +
  480. (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ));
  481. } else {
  482. qemu_del_timer(s->adb_poll_timer);
  483. }
  484. }
  485. obuf[0] = CUDA_PACKET;
  486. obuf[1] = data[1];
  487. cuda_send_packet_to_host(s, obuf, 2);
  488. break;
  489. case CUDA_SET_TIME:
  490. ti = (((uint32_t)data[1]) << 24) + (((uint32_t)data[2]) << 16) + (((uint32_t)data[3]) << 8) + data[4];
  491. s->tick_offset = ti - (qemu_get_clock_ns(vm_clock) / get_ticks_per_sec());
  492. obuf[0] = CUDA_PACKET;
  493. obuf[1] = 0;
  494. obuf[2] = 0;
  495. cuda_send_packet_to_host(s, obuf, 3);
  496. break;
  497. case CUDA_GET_TIME:
  498. ti = s->tick_offset + (qemu_get_clock_ns(vm_clock) / get_ticks_per_sec());
  499. obuf[0] = CUDA_PACKET;
  500. obuf[1] = 0;
  501. obuf[2] = 0;
  502. obuf[3] = ti >> 24;
  503. obuf[4] = ti >> 16;
  504. obuf[5] = ti >> 8;
  505. obuf[6] = ti;
  506. cuda_send_packet_to_host(s, obuf, 7);
  507. break;
  508. case CUDA_FILE_SERVER_FLAG:
  509. case CUDA_SET_DEVICE_LIST:
  510. case CUDA_SET_AUTO_RATE:
  511. case CUDA_SET_POWER_MESSAGES:
  512. obuf[0] = CUDA_PACKET;
  513. obuf[1] = 0;
  514. cuda_send_packet_to_host(s, obuf, 2);
  515. break;
  516. case CUDA_POWERDOWN:
  517. obuf[0] = CUDA_PACKET;
  518. obuf[1] = 0;
  519. cuda_send_packet_to_host(s, obuf, 2);
  520. qemu_system_shutdown_request();
  521. break;
  522. case CUDA_RESET_SYSTEM:
  523. obuf[0] = CUDA_PACKET;
  524. obuf[1] = 0;
  525. cuda_send_packet_to_host(s, obuf, 2);
  526. qemu_system_reset_request();
  527. break;
  528. default:
  529. break;
  530. }
  531. }
  532. static void cuda_receive_packet_from_host(CUDAState *s,
  533. const uint8_t *data, int len)
  534. {
  535. #ifdef DEBUG_CUDA_PACKET
  536. {
  537. int i;
  538. printf("cuda_receive_packet_from_host:\n");
  539. for(i = 0; i < len; i++)
  540. printf(" %02x", data[i]);
  541. printf("\n");
  542. }
  543. #endif
  544. switch(data[0]) {
  545. case ADB_PACKET:
  546. {
  547. uint8_t obuf[ADB_MAX_OUT_LEN + 2];
  548. int olen;
  549. olen = adb_request(&adb_bus, obuf + 2, data + 1, len - 1);
  550. if (olen > 0) {
  551. obuf[0] = ADB_PACKET;
  552. obuf[1] = 0x00;
  553. } else {
  554. /* error */
  555. obuf[0] = ADB_PACKET;
  556. obuf[1] = -olen;
  557. olen = 0;
  558. }
  559. cuda_send_packet_to_host(s, obuf, olen + 2);
  560. }
  561. break;
  562. case CUDA_PACKET:
  563. cuda_receive_packet(s, data + 1, len - 1);
  564. break;
  565. }
  566. }
  567. static void cuda_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
  568. {
  569. }
  570. static void cuda_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  571. {
  572. }
  573. static uint32_t cuda_readw (void *opaque, target_phys_addr_t addr)
  574. {
  575. return 0;
  576. }
  577. static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr)
  578. {
  579. return 0;
  580. }
  581. static CPUWriteMemoryFunc * const cuda_write[] = {
  582. &cuda_writeb,
  583. &cuda_writew,
  584. &cuda_writel,
  585. };
  586. static CPUReadMemoryFunc * const cuda_read[] = {
  587. &cuda_readb,
  588. &cuda_readw,
  589. &cuda_readl,
  590. };
  591. static bool cuda_timer_exist(void *opaque, int version_id)
  592. {
  593. CUDATimer *s = opaque;
  594. return s->timer != NULL;
  595. }
  596. static const VMStateDescription vmstate_cuda_timer = {
  597. .name = "cuda_timer",
  598. .version_id = 0,
  599. .minimum_version_id = 0,
  600. .minimum_version_id_old = 0,
  601. .fields = (VMStateField[]) {
  602. VMSTATE_UINT16(latch, CUDATimer),
  603. VMSTATE_UINT16(counter_value, CUDATimer),
  604. VMSTATE_INT64(load_time, CUDATimer),
  605. VMSTATE_INT64(next_irq_time, CUDATimer),
  606. VMSTATE_TIMER_TEST(timer, CUDATimer, cuda_timer_exist),
  607. VMSTATE_END_OF_LIST()
  608. }
  609. };
  610. static const VMStateDescription vmstate_cuda = {
  611. .name = "cuda",
  612. .version_id = 1,
  613. .minimum_version_id = 1,
  614. .minimum_version_id_old = 1,
  615. .fields = (VMStateField[]) {
  616. VMSTATE_UINT8(a, CUDAState),
  617. VMSTATE_UINT8(b, CUDAState),
  618. VMSTATE_UINT8(dira, CUDAState),
  619. VMSTATE_UINT8(dirb, CUDAState),
  620. VMSTATE_UINT8(sr, CUDAState),
  621. VMSTATE_UINT8(acr, CUDAState),
  622. VMSTATE_UINT8(pcr, CUDAState),
  623. VMSTATE_UINT8(ifr, CUDAState),
  624. VMSTATE_UINT8(ier, CUDAState),
  625. VMSTATE_UINT8(anh, CUDAState),
  626. VMSTATE_INT32(data_in_size, CUDAState),
  627. VMSTATE_INT32(data_in_index, CUDAState),
  628. VMSTATE_INT32(data_out_index, CUDAState),
  629. VMSTATE_UINT8(autopoll, CUDAState),
  630. VMSTATE_BUFFER(data_in, CUDAState),
  631. VMSTATE_BUFFER(data_out, CUDAState),
  632. VMSTATE_UINT32(tick_offset, CUDAState),
  633. VMSTATE_STRUCT_ARRAY(timers, CUDAState, 2, 1,
  634. vmstate_cuda_timer, CUDATimer),
  635. VMSTATE_END_OF_LIST()
  636. }
  637. };
  638. static void cuda_reset(void *opaque)
  639. {
  640. CUDAState *s = opaque;
  641. s->b = 0;
  642. s->a = 0;
  643. s->dirb = 0;
  644. s->dira = 0;
  645. s->sr = 0;
  646. s->acr = 0;
  647. s->pcr = 0;
  648. s->ifr = 0;
  649. s->ier = 0;
  650. // s->ier = T1_INT | SR_INT;
  651. s->anh = 0;
  652. s->data_in_size = 0;
  653. s->data_in_index = 0;
  654. s->data_out_index = 0;
  655. s->autopoll = 0;
  656. s->timers[0].latch = 0xffff;
  657. set_counter(s, &s->timers[0], 0xffff);
  658. s->timers[1].latch = 0;
  659. set_counter(s, &s->timers[1], 0xffff);
  660. }
  661. void cuda_init (int *cuda_mem_index, qemu_irq irq)
  662. {
  663. struct tm tm;
  664. CUDAState *s = &cuda_state;
  665. s->irq = irq;
  666. s->timers[0].index = 0;
  667. s->timers[0].timer = qemu_new_timer_ns(vm_clock, cuda_timer1, s);
  668. s->timers[1].index = 1;
  669. qemu_get_timedate(&tm, 0);
  670. s->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
  671. s->adb_poll_timer = qemu_new_timer_ns(vm_clock, cuda_adb_poll, s);
  672. *cuda_mem_index = cpu_register_io_memory(cuda_read, cuda_write, s,
  673. DEVICE_NATIVE_ENDIAN);
  674. vmstate_register(NULL, -1, &vmstate_cuda, s);
  675. qemu_register_reset(cuda_reset, s);
  676. }