dv-m68hc11eepr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* dv-m68hc11eepr.c -- Simulation of the 68HC11 Internal EEPROM.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Stephane Carrez (stcarrez@nerim.fr)
  4. (From a driver model Contributed by Cygnus Solutions.)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /* This must come before any other includes. */
  17. #include "defs.h"
  18. #include "sim-main.h"
  19. #include "hw-main.h"
  20. #include "sim-assert.h"
  21. #include "sim-events.h"
  22. #include "sim-signal.h"
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. /* DEVICE
  27. m68hc11eepr - m68hc11 EEPROM
  28. DESCRIPTION
  29. Implements the 68HC11 eeprom device described in the m68hc11
  30. user guide (Chapter 4 in the pink book).
  31. PROPERTIES
  32. reg <base> <length>
  33. Base of eeprom and its length.
  34. file <path>
  35. Path of the EEPROM file. The default is 'm6811.eeprom'.
  36. PORTS
  37. None
  38. */
  39. /* static functions */
  40. /* port ID's */
  41. enum
  42. {
  43. RESET_PORT
  44. };
  45. static const struct hw_port_descriptor m68hc11eepr_ports[] =
  46. {
  47. { "reset", RESET_PORT, 0, input_port, },
  48. { NULL, },
  49. };
  50. /* The timer/counter register internal state. Note that we store
  51. state using the control register images, in host endian order. */
  52. struct m68hc11eepr
  53. {
  54. address_word base_address; /* control register base */
  55. int attach_space;
  56. unsigned size;
  57. int mapped;
  58. /* Current state of the eeprom programing:
  59. - eeprom_wmode indicates whether the EEPROM address and byte have
  60. been latched.
  61. - eeprom_waddr indicates the EEPROM address that was latched
  62. and eeprom_wbyte is the byte that was latched.
  63. - eeprom_wcycle indicates the CPU absolute cycle type when
  64. the high voltage was applied (successfully) on the EEPROM.
  65. These data members are setup only when we detect good EEPROM programing
  66. conditions (see Motorola EEPROM Programming and PPROG register usage).
  67. When the high voltage is switched off, we look at the CPU absolute
  68. cycle time to see if the EEPROM command must succeeds or not.
  69. The EEPROM content is updated and saved only at that time.
  70. (EEPROM command is: byte zero bits program, byte erase, row erase
  71. and bulk erase).
  72. The CONFIG register is programmed in the same way. It is physically
  73. located at the end of the EEPROM (eeprom size + 1). It is not mapped
  74. in memory but it's saved in the EEPROM file. */
  75. unsigned long eeprom_wcycle;
  76. uint16_t eeprom_waddr;
  77. uint8_t eeprom_wbyte;
  78. uint8_t eeprom_wmode;
  79. uint8_t* eeprom;
  80. /* Minimum time in CPU cycles for programming the EEPROM. */
  81. unsigned long eeprom_min_cycles;
  82. const char* file_name;
  83. };
  84. /* Finish off the partially created hw device. Attach our local
  85. callbacks. Wire up our port names etc. */
  86. static hw_io_read_buffer_method m68hc11eepr_io_read_buffer;
  87. static hw_io_write_buffer_method m68hc11eepr_io_write_buffer;
  88. static hw_ioctl_method m68hc11eepr_ioctl;
  89. /* Read or write the memory bank content from/to a file.
  90. Returns 0 if the operation succeeded and -1 if it failed. */
  91. static int
  92. m6811eepr_memory_rw (struct m68hc11eepr *controller, int mode)
  93. {
  94. const char *name = controller->file_name;
  95. int fd;
  96. size_t size;
  97. size = controller->size;
  98. fd = open (name, mode, 0644);
  99. if (fd < 0)
  100. {
  101. if (mode == O_RDONLY)
  102. {
  103. memset (controller->eeprom, 0xFF, size);
  104. /* Default value for CONFIG register (0xFF should be ok):
  105. controller->eeprom[size - 1] = M6811_NOSEC | M6811_NOCOP
  106. | M6811_ROMON | M6811_EEON; */
  107. return 0;
  108. }
  109. return -1;
  110. }
  111. if (mode == O_RDONLY)
  112. {
  113. if (read (fd, controller->eeprom, size) != size)
  114. {
  115. close (fd);
  116. return -1;
  117. }
  118. }
  119. else
  120. {
  121. if (write (fd, controller->eeprom, size) != size)
  122. {
  123. close (fd);
  124. return -1;
  125. }
  126. }
  127. close (fd);
  128. return 0;
  129. }
  130. static void
  131. attach_m68hc11eepr_regs (struct hw *me,
  132. struct m68hc11eepr *controller)
  133. {
  134. unsigned_word attach_address;
  135. int attach_space;
  136. unsigned attach_size;
  137. reg_property_spec reg;
  138. if (hw_find_property (me, "reg") == NULL)
  139. hw_abort (me, "Missing \"reg\" property");
  140. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  141. hw_abort (me, "\"reg\" property must contain one addr/size entry");
  142. hw_unit_address_to_attach_address (hw_parent (me),
  143. &reg.address,
  144. &attach_space,
  145. &attach_address,
  146. me);
  147. hw_unit_size_to_attach_size (hw_parent (me),
  148. &reg.size,
  149. &attach_size, me);
  150. /* Attach the two IO registers that control the EEPROM.
  151. The EEPROM is only attached at reset time because it may
  152. be enabled/disabled by the EEON bit in the CONFIG register. */
  153. hw_attach_address (hw_parent (me), M6811_IO_LEVEL,
  154. io_map, M6811_PPROG, 1, me);
  155. hw_attach_address (hw_parent (me), M6811_IO_LEVEL,
  156. io_map, M6811_CONFIG, 1, me);
  157. if (hw_find_property (me, "file") == NULL)
  158. controller->file_name = "m6811.eeprom";
  159. else
  160. controller->file_name = hw_find_string_property (me, "file");
  161. controller->attach_space = attach_space;
  162. controller->base_address = attach_address;
  163. controller->eeprom = hw_malloc (me, attach_size + 1);
  164. controller->eeprom_min_cycles = 10000;
  165. controller->size = attach_size + 1;
  166. controller->mapped = 0;
  167. m6811eepr_memory_rw (controller, O_RDONLY);
  168. }
  169. /* An event arrives on an interrupt port. */
  170. static void
  171. m68hc11eepr_port_event (struct hw *me,
  172. int my_port,
  173. struct hw *source,
  174. int source_port,
  175. int level)
  176. {
  177. SIM_DESC sd;
  178. struct m68hc11eepr *controller;
  179. sim_cpu *cpu;
  180. controller = hw_data (me);
  181. sd = hw_system (me);
  182. cpu = STATE_CPU (sd, 0);
  183. switch (my_port)
  184. {
  185. case RESET_PORT:
  186. {
  187. HW_TRACE ((me, "EEPROM reset"));
  188. /* Re-read the EEPROM from the file. This gives the chance
  189. to users to erase this file before doing a reset and have
  190. a fresh EEPROM taken into account. */
  191. m6811eepr_memory_rw (controller, O_RDONLY);
  192. /* Reset the state of EEPROM programmer. The CONFIG register
  193. is also initialized from the EEPROM/file content. */
  194. cpu->ios[M6811_PPROG] = 0;
  195. if (cpu->cpu_use_local_config)
  196. cpu->ios[M6811_CONFIG] = cpu->cpu_config;
  197. else
  198. cpu->ios[M6811_CONFIG] = controller->eeprom[controller->size-1];
  199. controller->eeprom_wmode = 0;
  200. controller->eeprom_waddr = 0;
  201. controller->eeprom_wbyte = 0;
  202. /* Attach or detach to the bus depending on the EEPROM enable bit.
  203. The EEPROM CONFIG register is still enabled and can be programmed
  204. for a next configuration (taken into account only after a reset,
  205. see Motorola spec). */
  206. if (!(cpu->ios[M6811_CONFIG] & M6811_EEON))
  207. {
  208. if (controller->mapped)
  209. hw_detach_address (hw_parent (me), M6811_EEPROM_LEVEL,
  210. controller->attach_space,
  211. controller->base_address,
  212. controller->size - 1,
  213. me);
  214. controller->mapped = 0;
  215. }
  216. else
  217. {
  218. if (!controller->mapped)
  219. hw_attach_address (hw_parent (me), M6811_EEPROM_LEVEL,
  220. controller->attach_space,
  221. controller->base_address,
  222. controller->size - 1,
  223. me);
  224. controller->mapped = 1;
  225. }
  226. break;
  227. }
  228. default:
  229. hw_abort (me, "Event on unknown port %d", my_port);
  230. break;
  231. }
  232. }
  233. static void
  234. m68hc11eepr_finish (struct hw *me)
  235. {
  236. struct m68hc11eepr *controller;
  237. controller = HW_ZALLOC (me, struct m68hc11eepr);
  238. set_hw_data (me, controller);
  239. set_hw_io_read_buffer (me, m68hc11eepr_io_read_buffer);
  240. set_hw_io_write_buffer (me, m68hc11eepr_io_write_buffer);
  241. set_hw_ports (me, m68hc11eepr_ports);
  242. set_hw_port_event (me, m68hc11eepr_port_event);
  243. #ifdef set_hw_ioctl
  244. set_hw_ioctl (me, m68hc11eepr_ioctl);
  245. #else
  246. me->to_ioctl = m68hc11eepr_ioctl;
  247. #endif
  248. attach_m68hc11eepr_regs (me, controller);
  249. }
  250. static io_reg_desc pprog_desc[] = {
  251. { M6811_BYTE, "BYTE ", "Byte Program Mode" },
  252. { M6811_ROW, "ROW ", "Row Program Mode" },
  253. { M6811_ERASE, "ERASE ", "Erase Mode" },
  254. { M6811_EELAT, "EELAT ", "EEProm Latch Control" },
  255. { M6811_EEPGM, "EEPGM ", "EEProm Programming Voltable Enable" },
  256. { 0, 0, 0 }
  257. };
  258. extern io_reg_desc config_desc[];
  259. /* Describe the state of the EEPROM device. */
  260. static void
  261. m68hc11eepr_info (struct hw *me)
  262. {
  263. SIM_DESC sd;
  264. uint16_t base = 0;
  265. sim_cpu *cpu;
  266. struct m68hc11eepr *controller;
  267. uint8_t val;
  268. sd = hw_system (me);
  269. cpu = STATE_CPU (sd, 0);
  270. controller = hw_data (me);
  271. base = cpu_get_io_base (cpu);
  272. sim_io_printf (sd, "M68HC11 EEprom:\n");
  273. val = cpu->ios[M6811_PPROG];
  274. print_io_byte (sd, "PPROG ", pprog_desc, val, base + M6811_PPROG);
  275. sim_io_printf (sd, "\n");
  276. val = cpu->ios[M6811_CONFIG];
  277. print_io_byte (sd, "CONFIG ", config_desc, val, base + M6811_CONFIG);
  278. sim_io_printf (sd, "\n");
  279. val = controller->eeprom[controller->size - 1];
  280. print_io_byte (sd, "(*NEXT*) ", config_desc, val, base + M6811_CONFIG);
  281. sim_io_printf (sd, "\n");
  282. /* Describe internal state of EEPROM. */
  283. if (controller->eeprom_wmode)
  284. {
  285. if (controller->eeprom_waddr == controller->size - 1)
  286. sim_io_printf (sd, " Programming CONFIG register ");
  287. else
  288. sim_io_printf (sd, " Programming: 0x%04x ",
  289. controller->eeprom_waddr + controller->base_address);
  290. sim_io_printf (sd, "with 0x%02x\n",
  291. controller->eeprom_wbyte);
  292. }
  293. sim_io_printf (sd, " EEProm file: %s\n",
  294. controller->file_name);
  295. }
  296. static int
  297. m68hc11eepr_ioctl (struct hw *me,
  298. hw_ioctl_request request,
  299. va_list ap)
  300. {
  301. m68hc11eepr_info (me);
  302. return 0;
  303. }
  304. /* generic read/write */
  305. static unsigned
  306. m68hc11eepr_io_read_buffer (struct hw *me,
  307. void *dest,
  308. int space,
  309. unsigned_word base,
  310. unsigned nr_bytes)
  311. {
  312. SIM_DESC sd;
  313. struct m68hc11eepr *controller;
  314. sim_cpu *cpu;
  315. HW_TRACE ((me, "read 0x%08lx %d", (long) base, (int) nr_bytes));
  316. sd = hw_system (me);
  317. controller = hw_data (me);
  318. cpu = STATE_CPU (sd, 0);
  319. if (space == io_map)
  320. {
  321. unsigned cnt = 0;
  322. while (nr_bytes != 0)
  323. {
  324. switch (base)
  325. {
  326. case M6811_PPROG:
  327. case M6811_CONFIG:
  328. *((uint8_t*) dest) = cpu->ios[base];
  329. break;
  330. default:
  331. hw_abort (me, "reading wrong register 0x%04x", base);
  332. }
  333. dest = (uint8_t*) (dest) + 1;
  334. base++;
  335. nr_bytes--;
  336. cnt++;
  337. }
  338. return cnt;
  339. }
  340. /* In theory, we can't read the EEPROM when it's being programmed. */
  341. if ((cpu->ios[M6811_PPROG] & M6811_EELAT) != 0
  342. && cpu_is_running (cpu))
  343. {
  344. sim_memory_error (cpu, SIM_SIGBUS, base,
  345. "EEprom not configured for reading");
  346. }
  347. base = base - controller->base_address;
  348. memcpy (dest, &controller->eeprom[base], nr_bytes);
  349. return nr_bytes;
  350. }
  351. static unsigned
  352. m68hc11eepr_io_write_buffer (struct hw *me,
  353. const void *source,
  354. int space,
  355. unsigned_word base,
  356. unsigned nr_bytes)
  357. {
  358. SIM_DESC sd;
  359. struct m68hc11eepr *controller;
  360. sim_cpu *cpu;
  361. uint8_t val;
  362. HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
  363. sd = hw_system (me);
  364. controller = hw_data (me);
  365. cpu = STATE_CPU (sd, 0);
  366. /* Programming several bytes at a time is not possible. */
  367. if (space != io_map && nr_bytes != 1)
  368. {
  369. sim_memory_error (cpu, SIM_SIGBUS, base,
  370. "EEprom write error (only 1 byte can be programmed)");
  371. return 0;
  372. }
  373. if (nr_bytes != 1)
  374. hw_abort (me, "Cannot write more than 1 byte to EEPROM device at a time");
  375. val = *((const uint8_t*) source);
  376. /* Write to the EEPROM control register. */
  377. if (space == io_map && base == M6811_PPROG)
  378. {
  379. uint8_t wrong_bits;
  380. uint16_t addr;
  381. addr = base + cpu_get_io_base (cpu);
  382. /* Setting EELAT and EEPGM at the same time is an error.
  383. Clearing them both is ok. */
  384. wrong_bits = (cpu->ios[M6811_PPROG] ^ val) & val;
  385. wrong_bits &= (M6811_EELAT | M6811_EEPGM);
  386. if (wrong_bits == (M6811_EEPGM|M6811_EELAT))
  387. {
  388. sim_memory_error (cpu, SIM_SIGBUS, addr,
  389. "Wrong eeprom programing value");
  390. return 0;
  391. }
  392. if ((val & M6811_EELAT) == 0)
  393. {
  394. val = 0;
  395. }
  396. if ((val & M6811_EEPGM) && !(cpu->ios[M6811_PPROG] & M6811_EELAT))
  397. {
  398. sim_memory_error (cpu, SIM_SIGBUS, addr,
  399. "EEProm high voltage applied after EELAT");
  400. }
  401. if ((val & M6811_EEPGM) && controller->eeprom_wmode == 0)
  402. {
  403. sim_memory_error (cpu, SIM_SIGSEGV, addr,
  404. "EEProm high voltage applied without address");
  405. }
  406. if (val & M6811_EEPGM)
  407. {
  408. controller->eeprom_wcycle = cpu_current_cycle (cpu);
  409. }
  410. else if (cpu->ios[M6811_PPROG] & M6811_PPROG)
  411. {
  412. int i;
  413. unsigned long t = cpu_current_cycle (cpu);
  414. t -= controller->eeprom_wcycle;
  415. if (t < controller->eeprom_min_cycles)
  416. {
  417. sim_memory_error (cpu, SIM_SIGILL, addr,
  418. "EEprom programmed only for %lu cycles",
  419. t);
  420. }
  421. /* Program the byte by clearing some bits. */
  422. if (!(cpu->ios[M6811_PPROG] & M6811_ERASE))
  423. {
  424. controller->eeprom[controller->eeprom_waddr]
  425. &= controller->eeprom_wbyte;
  426. }
  427. /* Erase a byte, row or the complete eeprom. Erased value is 0xFF.
  428. Ignore row or complete eeprom erase when we are programming the
  429. CONFIG register (last EEPROM byte). */
  430. else if ((cpu->ios[M6811_PPROG] & M6811_BYTE)
  431. || controller->eeprom_waddr == controller->size - 1)
  432. {
  433. controller->eeprom[controller->eeprom_waddr] = 0xff;
  434. }
  435. else if (cpu->ios[M6811_BYTE] & M6811_ROW)
  436. {
  437. size_t max_size;
  438. /* Size of EEPROM (-1 because the last byte is the
  439. CONFIG register. */
  440. max_size = controller->size;
  441. controller->eeprom_waddr &= 0xFFF0;
  442. for (i = 0; i < 16
  443. && controller->eeprom_waddr < max_size; i++)
  444. {
  445. controller->eeprom[controller->eeprom_waddr] = 0xff;
  446. controller->eeprom_waddr ++;
  447. }
  448. }
  449. else
  450. {
  451. size_t max_size;
  452. max_size = controller->size;
  453. for (i = 0; i < max_size; i++)
  454. {
  455. controller->eeprom[i] = 0xff;
  456. }
  457. }
  458. /* Save the eeprom in a file. We have to save after each
  459. change because the simulator can be stopped or crash... */
  460. if (m6811eepr_memory_rw (controller, O_WRONLY | O_CREAT) != 0)
  461. {
  462. sim_memory_error (cpu, SIM_SIGABRT, addr,
  463. "EEPROM programing failed: errno=%d", errno);
  464. }
  465. controller->eeprom_wmode = 0;
  466. }
  467. cpu->ios[M6811_PPROG] = val;
  468. return 1;
  469. }
  470. /* The CONFIG IO register is mapped at end of EEPROM.
  471. It's not visible. */
  472. if (space == io_map && base == M6811_CONFIG)
  473. {
  474. base = controller->size - 1;
  475. }
  476. else
  477. {
  478. base = base - controller->base_address;
  479. }
  480. /* Writing the memory is allowed for the Debugger or simulator
  481. (cpu not running). */
  482. if (cpu_is_running (cpu))
  483. {
  484. if ((cpu->ios[M6811_PPROG] & M6811_EELAT) == 0)
  485. {
  486. sim_memory_error (cpu, SIM_SIGSEGV, base,
  487. "EEprom not configured for writing");
  488. return 0;
  489. }
  490. if (controller->eeprom_wmode != 0)
  491. {
  492. sim_memory_error (cpu, SIM_SIGSEGV, base,
  493. "EEprom write error");
  494. return 0;
  495. }
  496. controller->eeprom_wmode = 1;
  497. controller->eeprom_waddr = base;
  498. controller->eeprom_wbyte = val;
  499. }
  500. else
  501. {
  502. controller->eeprom[base] = val;
  503. m6811eepr_memory_rw (controller, O_WRONLY);
  504. }
  505. return 1;
  506. }
  507. const struct hw_descriptor dv_m68hc11eepr_descriptor[] = {
  508. { "m68hc11eepr", m68hc11eepr_finish },
  509. { "m68hc12eepr", m68hc11eepr_finish },
  510. { NULL },
  511. };