dv-m68hc11sio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /* dv-m68hc11sio.c -- Simulation of the 68HC11 serial device.
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. Written by Stephane Carrez (stcarrez@worldnet.fr)
  4. (From a driver model Contributed by Cygnus Solutions.)
  5. This file is part of the program GDB, the GNU debugger.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "sim-main.h"
  18. #include "hw-main.h"
  19. #include "dv-sockser.h"
  20. #include "sim-assert.h"
  21. /* DEVICE
  22. m68hc11sio - m68hc11 serial I/O
  23. DESCRIPTION
  24. Implements the m68hc11 serial I/O controller described in the m68hc11
  25. user guide. The serial I/O controller is directly connected to the CPU
  26. interrupt. The simulator implements:
  27. - baud rate emulation
  28. - 8-bits transfers
  29. PROPERTIES
  30. backend {tcp | stdio}
  31. Use dv-sockser TCP-port backend or stdio for backend. Default: stdio.
  32. PORTS
  33. reset (input)
  34. Reset port. This port is only used to simulate a reset of the serial
  35. I/O controller. It should be connected to the RESET output of the cpu.
  36. */
  37. /* port ID's */
  38. enum
  39. {
  40. RESET_PORT
  41. };
  42. static const struct hw_port_descriptor m68hc11sio_ports[] =
  43. {
  44. { "reset", RESET_PORT, 0, input_port, },
  45. { NULL, },
  46. };
  47. /* Serial Controller information. */
  48. struct m68hc11sio
  49. {
  50. enum {sio_tcp, sio_stdio} backend; /* backend */
  51. /* Number of cpu cycles to send a bit on the wire. */
  52. unsigned long baud_cycle;
  53. /* Length in bits of characters sent, this includes the
  54. start/stop and parity bits. Together with baud_cycle, this
  55. is used to find the number of cpu cycles to send/receive a data. */
  56. unsigned int data_length;
  57. /* Information about next character to be transmited. */
  58. unsigned char tx_has_char;
  59. unsigned char tx_char;
  60. unsigned char rx_char;
  61. unsigned char rx_clear_scsr;
  62. /* Periodic I/O polling. */
  63. struct hw_event* tx_poll_event;
  64. struct hw_event* rx_poll_event;
  65. };
  66. /* Finish off the partially created hw device. Attach our local
  67. callbacks. Wire up our port names etc. */
  68. static hw_io_read_buffer_method m68hc11sio_io_read_buffer;
  69. static hw_io_write_buffer_method m68hc11sio_io_write_buffer;
  70. static hw_port_event_method m68hc11sio_port_event;
  71. static hw_ioctl_method m68hc11sio_ioctl;
  72. #define M6811_SCI_FIRST_REG (M6811_BAUD)
  73. #define M6811_SCI_LAST_REG (M6811_SCDR)
  74. static void
  75. attach_m68hc11sio_regs (struct hw *me,
  76. struct m68hc11sio *controller)
  77. {
  78. hw_attach_address (hw_parent (me), M6811_IO_LEVEL, io_map,
  79. M6811_SCI_FIRST_REG,
  80. M6811_SCI_LAST_REG - M6811_SCI_FIRST_REG + 1,
  81. me);
  82. if (hw_find_property(me, "backend") != NULL)
  83. {
  84. const char *value = hw_find_string_property(me, "backend");
  85. if(! strcmp(value, "tcp"))
  86. controller->backend = sio_tcp;
  87. else if(! strcmp(value, "stdio"))
  88. controller->backend = sio_stdio;
  89. else
  90. hw_abort (me, "illegal value for backend parameter `%s':"
  91. "use tcp or stdio", value);
  92. }
  93. }
  94. static void
  95. m68hc11sio_finish (struct hw *me)
  96. {
  97. struct m68hc11sio *controller;
  98. controller = HW_ZALLOC (me, struct m68hc11sio);
  99. set_hw_data (me, controller);
  100. set_hw_io_read_buffer (me, m68hc11sio_io_read_buffer);
  101. set_hw_io_write_buffer (me, m68hc11sio_io_write_buffer);
  102. set_hw_ports (me, m68hc11sio_ports);
  103. set_hw_port_event (me, m68hc11sio_port_event);
  104. #ifdef set_hw_ioctl
  105. set_hw_ioctl (me, m68hc11sio_ioctl);
  106. #else
  107. me->to_ioctl = m68hc11sio_ioctl;
  108. #endif
  109. /* Preset defaults. */
  110. controller->backend = sio_stdio;
  111. /* Attach ourself to our parent bus. */
  112. attach_m68hc11sio_regs (me, controller);
  113. /* Initialize to reset state. */
  114. controller->tx_poll_event = NULL;
  115. controller->rx_poll_event = NULL;
  116. controller->tx_char = 0;
  117. controller->tx_has_char = 0;
  118. controller->rx_clear_scsr = 0;
  119. controller->rx_char = 0;
  120. }
  121. /* An event arrives on an interrupt port. */
  122. static void
  123. m68hc11sio_port_event (struct hw *me,
  124. int my_port,
  125. struct hw *source,
  126. int source_port,
  127. int level)
  128. {
  129. SIM_DESC sd;
  130. struct m68hc11sio *controller;
  131. sim_cpu *cpu;
  132. unsigned8 val;
  133. controller = hw_data (me);
  134. sd = hw_system (me);
  135. cpu = STATE_CPU (sd, 0);
  136. switch (my_port)
  137. {
  138. case RESET_PORT:
  139. {
  140. HW_TRACE ((me, "SCI reset"));
  141. /* Reset the state of SCI registers. */
  142. val = 0;
  143. m68hc11sio_io_write_buffer (me, &val, io_map,
  144. (unsigned_word) M6811_BAUD, 1);
  145. m68hc11sio_io_write_buffer (me, &val, io_map,
  146. (unsigned_word) M6811_SCCR1, 1);
  147. m68hc11sio_io_write_buffer (me, &val, io_map,
  148. (unsigned_word) M6811_SCCR2, 1);
  149. cpu->ios[M6811_SCSR] = M6811_TC | M6811_TDRE;
  150. controller->rx_char = 0;
  151. controller->tx_char = 0;
  152. controller->tx_has_char = 0;
  153. controller->rx_clear_scsr = 0;
  154. if (controller->rx_poll_event)
  155. {
  156. hw_event_queue_deschedule (me, controller->rx_poll_event);
  157. controller->rx_poll_event = 0;
  158. }
  159. if (controller->tx_poll_event)
  160. {
  161. hw_event_queue_deschedule (me, controller->tx_poll_event);
  162. controller->tx_poll_event = 0;
  163. }
  164. /* In bootstrap mode, initialize the SCI to 1200 bauds to
  165. simulate some initial setup by the internal rom. */
  166. if (((cpu->ios[M6811_HPRIO]) & (M6811_SMOD | M6811_MDA)) == M6811_SMOD)
  167. {
  168. unsigned char val = 0x33;
  169. m68hc11sio_io_write_buffer (me, &val, io_map,
  170. (unsigned_word) M6811_BAUD, 1);
  171. val = 0x12;
  172. m68hc11sio_io_write_buffer (me, &val, io_map,
  173. (unsigned_word) M6811_SCCR2, 1);
  174. }
  175. break;
  176. }
  177. default:
  178. hw_abort (me, "Event on unknown port %d", my_port);
  179. break;
  180. }
  181. }
  182. void
  183. m68hc11sio_rx_poll (struct hw *me, void *data)
  184. {
  185. SIM_DESC sd;
  186. struct m68hc11sio *controller;
  187. sim_cpu *cpu;
  188. char cc;
  189. int cnt;
  190. int check_interrupt = 0;
  191. controller = hw_data (me);
  192. sd = hw_system (me);
  193. cpu = STATE_CPU (sd, 0);
  194. switch (controller->backend)
  195. {
  196. case sio_tcp:
  197. cnt = dv_sockser_read (sd);
  198. if (cnt != -1)
  199. {
  200. cc = (char) cnt;
  201. cnt = 1;
  202. }
  203. break;
  204. case sio_stdio:
  205. cnt = sim_io_poll_read (sd, 0 /* stdin */, &cc, 1);
  206. break;
  207. default:
  208. cnt = 0;
  209. break;
  210. }
  211. if (cnt == 1)
  212. {
  213. /* Raise the overrun flag if the previous character was not read. */
  214. if (cpu->ios[M6811_SCSR] & M6811_RDRF)
  215. cpu->ios[M6811_SCSR] |= M6811_OR;
  216. cpu->ios[M6811_SCSR] |= M6811_RDRF;
  217. controller->rx_char = cc;
  218. controller->rx_clear_scsr = 0;
  219. check_interrupt = 1;
  220. }
  221. else
  222. {
  223. /* handle idle line detect here. */
  224. ;
  225. }
  226. if (controller->rx_poll_event)
  227. {
  228. hw_event_queue_deschedule (me, controller->rx_poll_event);
  229. controller->rx_poll_event = 0;
  230. }
  231. if (cpu->ios[M6811_SCCR2] & M6811_RE)
  232. {
  233. unsigned long clock_cycle;
  234. /* Compute CPU clock cycles to wait for the next character. */
  235. clock_cycle = controller->data_length * controller->baud_cycle;
  236. controller->rx_poll_event = hw_event_queue_schedule (me, clock_cycle,
  237. m68hc11sio_rx_poll,
  238. NULL);
  239. }
  240. if (check_interrupt)
  241. interrupts_update_pending (&cpu->cpu_interrupts);
  242. }
  243. void
  244. m68hc11sio_tx_poll (struct hw *me, void *data)
  245. {
  246. SIM_DESC sd;
  247. struct m68hc11sio *controller;
  248. sim_cpu *cpu;
  249. controller = hw_data (me);
  250. sd = hw_system (me);
  251. cpu = STATE_CPU (sd, 0);
  252. cpu->ios[M6811_SCSR] |= M6811_TDRE;
  253. cpu->ios[M6811_SCSR] |= M6811_TC;
  254. /* Transmitter is enabled and we have something to send. */
  255. if ((cpu->ios[M6811_SCCR2] & M6811_TE) && controller->tx_has_char)
  256. {
  257. cpu->ios[M6811_SCSR] &= ~M6811_TDRE;
  258. cpu->ios[M6811_SCSR] &= ~M6811_TC;
  259. controller->tx_has_char = 0;
  260. switch (controller->backend)
  261. {
  262. case sio_tcp:
  263. dv_sockser_write (sd, controller->tx_char);
  264. break;
  265. case sio_stdio:
  266. sim_io_write_stdout (sd, &controller->tx_char, 1);
  267. sim_io_flush_stdout (sd);
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. if (controller->tx_poll_event)
  274. {
  275. hw_event_queue_deschedule (me, controller->tx_poll_event);
  276. controller->tx_poll_event = 0;
  277. }
  278. if ((cpu->ios[M6811_SCCR2] & M6811_TE)
  279. && ((cpu->ios[M6811_SCSR] & M6811_TC) == 0))
  280. {
  281. unsigned long clock_cycle;
  282. /* Compute CPU clock cycles to wait for the next character. */
  283. clock_cycle = controller->data_length * controller->baud_cycle;
  284. controller->tx_poll_event = hw_event_queue_schedule (me, clock_cycle,
  285. m68hc11sio_tx_poll,
  286. NULL);
  287. }
  288. interrupts_update_pending (&cpu->cpu_interrupts);
  289. }
  290. /* Descriptions of the SIO I/O ports. These descriptions are only used to
  291. give information of the SIO device under GDB. */
  292. io_reg_desc sccr2_desc[] = {
  293. { M6811_TIE, "TIE ", "Transmit Interrupt Enable" },
  294. { M6811_TCIE, "TCIE ", "Transmit Complete Interrupt Enable" },
  295. { M6811_RIE, "RIE ", "Receive Interrupt Enable" },
  296. { M6811_ILIE, "ILIE ", "Idle Line Interrupt Enable" },
  297. { M6811_TE, "TE ", "Transmit Enable" },
  298. { M6811_RE, "RE ", "Receive Enable" },
  299. { M6811_RWU, "RWU ", "Receiver Wake Up" },
  300. { M6811_SBK, "SBRK ", "Send Break" },
  301. { 0, 0, 0 }
  302. };
  303. io_reg_desc sccr1_desc[] = {
  304. { M6811_R8, "R8 ", "Receive Data bit 8" },
  305. { M6811_T8, "T8 ", "Transmit Data bit 8" },
  306. { M6811_M, "M ", "SCI Character length (0=8-bits, 1=9-bits)" },
  307. { M6811_WAKE, "WAKE ", "Wake up method select (0=idle, 1=addr mark" },
  308. { 0, 0, 0 }
  309. };
  310. io_reg_desc scsr_desc[] = {
  311. { M6811_TDRE, "TDRE ", "Transmit Data Register Empty" },
  312. { M6811_TC, "TC ", "Transmit Complete" },
  313. { M6811_RDRF, "RDRF ", "Receive Data Register Full" },
  314. { M6811_IDLE, "IDLE ", "Idle Line Detect" },
  315. { M6811_OR, "OR ", "Overrun Error" },
  316. { M6811_NF, "NF ", "Noise Flag" },
  317. { M6811_FE, "FE ", "Framing Error" },
  318. { 0, 0, 0 }
  319. };
  320. io_reg_desc baud_desc[] = {
  321. { M6811_TCLR, "TCLR ", "Clear baud rate (test mode)" },
  322. { M6811_SCP1, "SCP1 ", "SCI baud rate prescaler select (SCP1)" },
  323. { M6811_SCP0, "SCP0 ", "SCI baud rate prescaler select (SCP0)" },
  324. { M6811_RCKB, "RCKB ", "Baur Rate Clock Check (test mode)" },
  325. { M6811_SCR2, "SCR2 ", "SCI Baud rate select (SCR2)" },
  326. { M6811_SCR1, "SCR1 ", "SCI Baud rate select (SCR1)" },
  327. { M6811_SCR0, "SCR0 ", "SCI Baud rate select (SCR0)" },
  328. { 0, 0, 0 }
  329. };
  330. static void
  331. m68hc11sio_info (struct hw *me)
  332. {
  333. SIM_DESC sd;
  334. uint16 base = 0;
  335. sim_cpu *cpu;
  336. struct m68hc11sio *controller;
  337. uint8 val;
  338. long clock_cycle;
  339. sd = hw_system (me);
  340. cpu = STATE_CPU (sd, 0);
  341. controller = hw_data (me);
  342. sim_io_printf (sd, "M68HC11 SIO:\n");
  343. base = cpu_get_io_base (cpu);
  344. val = cpu->ios[M6811_BAUD];
  345. print_io_byte (sd, "BAUD ", baud_desc, val, base + M6811_BAUD);
  346. sim_io_printf (sd, " (%ld baud)\n",
  347. (cpu->cpu_frequency / 4) / controller->baud_cycle);
  348. val = cpu->ios[M6811_SCCR1];
  349. print_io_byte (sd, "SCCR1", sccr1_desc, val, base + M6811_SCCR1);
  350. sim_io_printf (sd, " (%d bits) (%dN1)\n",
  351. controller->data_length, controller->data_length - 2);
  352. val = cpu->ios[M6811_SCCR2];
  353. print_io_byte (sd, "SCCR2", sccr2_desc, val, base + M6811_SCCR2);
  354. sim_io_printf (sd, "\n");
  355. val = cpu->ios[M6811_SCSR];
  356. print_io_byte (sd, "SCSR ", scsr_desc, val, base + M6811_SCSR);
  357. sim_io_printf (sd, "\n");
  358. clock_cycle = controller->data_length * controller->baud_cycle;
  359. if (controller->tx_poll_event)
  360. {
  361. signed64 t;
  362. int n;
  363. t = hw_event_remain_time (me, controller->tx_poll_event);
  364. n = (clock_cycle - t) / controller->baud_cycle;
  365. n = controller->data_length - n;
  366. sim_io_printf (sd, " Transmit finished in %s (%d bit%s)\n",
  367. cycle_to_string (cpu, t, PRINT_TIME | PRINT_CYCLE),
  368. n, (n > 1 ? "s" : ""));
  369. }
  370. if (controller->rx_poll_event)
  371. {
  372. signed64 t;
  373. t = hw_event_remain_time (me, controller->rx_poll_event);
  374. sim_io_printf (sd, " Receive finished in %s\n",
  375. cycle_to_string (cpu, t, PRINT_TIME | PRINT_CYCLE));
  376. }
  377. }
  378. static int
  379. m68hc11sio_ioctl (struct hw *me,
  380. hw_ioctl_request request,
  381. va_list ap)
  382. {
  383. m68hc11sio_info (me);
  384. return 0;
  385. }
  386. /* generic read/write */
  387. static unsigned
  388. m68hc11sio_io_read_buffer (struct hw *me,
  389. void *dest,
  390. int space,
  391. unsigned_word base,
  392. unsigned nr_bytes)
  393. {
  394. SIM_DESC sd;
  395. struct m68hc11sio *controller;
  396. sim_cpu *cpu;
  397. unsigned8 val;
  398. HW_TRACE ((me, "read 0x%08lx %d", (long) base, (int) nr_bytes));
  399. sd = hw_system (me);
  400. cpu = STATE_CPU (sd, 0);
  401. controller = hw_data (me);
  402. switch (base)
  403. {
  404. case M6811_SCSR:
  405. controller->rx_clear_scsr = cpu->ios[M6811_SCSR]
  406. & (M6811_RDRF | M6811_IDLE | M6811_OR | M6811_NF | M6811_FE);
  407. case M6811_BAUD:
  408. case M6811_SCCR1:
  409. case M6811_SCCR2:
  410. val = cpu->ios[base];
  411. break;
  412. case M6811_SCDR:
  413. if (controller->rx_clear_scsr)
  414. {
  415. cpu->ios[M6811_SCSR] &= ~controller->rx_clear_scsr;
  416. }
  417. val = controller->rx_char;
  418. break;
  419. default:
  420. return 0;
  421. }
  422. *((unsigned8*) dest) = val;
  423. return 1;
  424. }
  425. static unsigned
  426. m68hc11sio_io_write_buffer (struct hw *me,
  427. const void *source,
  428. int space,
  429. unsigned_word base,
  430. unsigned nr_bytes)
  431. {
  432. SIM_DESC sd;
  433. struct m68hc11sio *controller;
  434. sim_cpu *cpu;
  435. unsigned8 val;
  436. HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
  437. sd = hw_system (me);
  438. cpu = STATE_CPU (sd, 0);
  439. controller = hw_data (me);
  440. val = *((const unsigned8*) source);
  441. switch (base)
  442. {
  443. case M6811_BAUD:
  444. {
  445. long divisor;
  446. long baud;
  447. cpu->ios[M6811_BAUD] = val;
  448. switch (val & (M6811_SCP1|M6811_SCP0))
  449. {
  450. case M6811_BAUD_DIV_1:
  451. divisor = 1 * 16;
  452. break;
  453. case M6811_BAUD_DIV_3:
  454. divisor = 3 * 16;
  455. break;
  456. case M6811_BAUD_DIV_4:
  457. divisor = 4 * 16;
  458. break;
  459. default:
  460. case M6811_BAUD_DIV_13:
  461. divisor = 13 * 16;
  462. break;
  463. }
  464. val &= (M6811_SCR2|M6811_SCR1|M6811_SCR0);
  465. divisor *= (1 << val);
  466. baud = (cpu->cpu_frequency / 4) / divisor;
  467. HW_TRACE ((me, "divide rate %ld, baud rate %ld",
  468. divisor, baud));
  469. controller->baud_cycle = divisor;
  470. }
  471. break;
  472. case M6811_SCCR1:
  473. {
  474. if (val & M6811_M)
  475. controller->data_length = 11;
  476. else
  477. controller->data_length = 10;
  478. cpu->ios[M6811_SCCR1] = val;
  479. }
  480. break;
  481. case M6811_SCCR2:
  482. if ((val & M6811_RE) == 0)
  483. {
  484. val &= ~(M6811_RDRF|M6811_IDLE|M6811_OR|M6811_NF|M6811_NF);
  485. val |= (cpu->ios[M6811_SCCR2]
  486. & (M6811_RDRF|M6811_IDLE|M6811_OR|M6811_NF|M6811_NF));
  487. cpu->ios[M6811_SCCR2] = val;
  488. break;
  489. }
  490. /* Activate reception. */
  491. if (controller->rx_poll_event == 0)
  492. {
  493. long clock_cycle;
  494. /* Compute CPU clock cycles to wait for the next character. */
  495. clock_cycle = controller->data_length * controller->baud_cycle;
  496. controller->rx_poll_event = hw_event_queue_schedule (me, clock_cycle,
  497. m68hc11sio_rx_poll,
  498. NULL);
  499. }
  500. cpu->ios[M6811_SCCR2] = val;
  501. interrupts_update_pending (&cpu->cpu_interrupts);
  502. break;
  503. /* No effect. */
  504. case M6811_SCSR:
  505. return 1;
  506. case M6811_SCDR:
  507. if (!(cpu->ios[M6811_SCSR] & M6811_TDRE))
  508. {
  509. return 0;
  510. }
  511. controller->tx_char = val;
  512. controller->tx_has_char = 1;
  513. if ((cpu->ios[M6811_SCCR2] & M6811_TE)
  514. && controller->tx_poll_event == 0)
  515. {
  516. m68hc11sio_tx_poll (me, NULL);
  517. }
  518. return 1;
  519. default:
  520. return 0;
  521. }
  522. return nr_bytes;
  523. }
  524. const struct hw_descriptor dv_m68hc11sio_descriptor[] = {
  525. { "m68hc11sio", m68hc11sio_finish },
  526. { "m68hc12sio", m68hc11sio_finish },
  527. { NULL },
  528. };