m68hc11_sim.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /* m6811_cpu.c -- 68HC11&68HC12 CPU Emulation
  2. Copyright 1999-2022 Free Software Foundation, Inc.
  3. Written by Stephane Carrez (stcarrez@nerim.fr)
  4. This file is part of GDB, GAS, and the GNU binutils.
  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. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "sim-assert.h"
  19. #include "sim-module.h"
  20. #include "sim-options.h"
  21. #include "sim-signal.h"
  22. #include <stdlib.h>
  23. enum {
  24. OPTION_CPU_RESET = OPTION_START,
  25. OPTION_EMUL_OS,
  26. OPTION_CPU_CONFIG,
  27. OPTION_CPU_BOOTSTRAP,
  28. OPTION_CPU_MODE
  29. };
  30. static DECLARE_OPTION_HANDLER (cpu_option_handler);
  31. static const OPTION cpu_options[] =
  32. {
  33. { {"cpu-reset", no_argument, NULL, OPTION_CPU_RESET },
  34. '\0', NULL, "Reset the CPU",
  35. cpu_option_handler },
  36. { {"emulos", no_argument, NULL, OPTION_EMUL_OS },
  37. '\0', NULL, "Emulate some OS system calls (read, write, ...)",
  38. cpu_option_handler },
  39. { {"cpu-config", required_argument, NULL, OPTION_CPU_CONFIG },
  40. '\0', NULL, "Specify the initial CPU configuration register",
  41. cpu_option_handler },
  42. { {"bootstrap", no_argument, NULL, OPTION_CPU_BOOTSTRAP },
  43. '\0', NULL, "Start the processing in bootstrap mode",
  44. cpu_option_handler },
  45. { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
  46. };
  47. static SIM_RC
  48. cpu_option_handler (SIM_DESC sd, sim_cpu *cpu,
  49. int opt, char *arg, int is_command)
  50. {
  51. int val;
  52. cpu = STATE_CPU (sd, 0);
  53. switch (opt)
  54. {
  55. case OPTION_CPU_RESET:
  56. sim_board_reset (sd);
  57. break;
  58. case OPTION_EMUL_OS:
  59. cpu->cpu_emul_syscall = 1;
  60. break;
  61. case OPTION_CPU_CONFIG:
  62. if (sscanf(arg, "0x%x", &val) == 1
  63. || sscanf(arg, "%d", &val) == 1)
  64. {
  65. cpu->cpu_config = val;
  66. cpu->cpu_use_local_config = 1;
  67. }
  68. else
  69. cpu->cpu_use_local_config = 0;
  70. break;
  71. case OPTION_CPU_BOOTSTRAP:
  72. cpu->cpu_start_mode = "bootstrap";
  73. break;
  74. case OPTION_CPU_MODE:
  75. break;
  76. }
  77. return SIM_RC_OK;
  78. }
  79. void
  80. cpu_call (sim_cpu *cpu, uint16_t addr)
  81. {
  82. cpu_set_pc (cpu, addr);
  83. }
  84. void
  85. cpu_return (sim_cpu *cpu)
  86. {
  87. }
  88. /* Set the stack pointer and re-compute the current frame. */
  89. void
  90. cpu_set_sp (sim_cpu *cpu, uint16_t val)
  91. {
  92. cpu->cpu_regs.sp = val;
  93. }
  94. static uint16_t
  95. cpu_get_reg (sim_cpu *cpu, uint8_t reg)
  96. {
  97. switch (reg)
  98. {
  99. case 0:
  100. return cpu_get_x (cpu);
  101. case 1:
  102. return cpu_get_y (cpu);
  103. case 2:
  104. return cpu_get_sp (cpu);
  105. case 3:
  106. return cpu_get_pc (cpu);
  107. default:
  108. return 0;
  109. }
  110. }
  111. static uint16_t
  112. cpu_get_src_reg (sim_cpu *cpu, uint8_t reg)
  113. {
  114. switch (reg)
  115. {
  116. case 0:
  117. return cpu_get_a (cpu);
  118. case 1:
  119. return cpu_get_b (cpu);
  120. case 2:
  121. return cpu_get_ccr (cpu);
  122. case 3:
  123. return cpu_get_tmp3 (cpu);
  124. case 4:
  125. return cpu_get_d (cpu);
  126. case 5:
  127. return cpu_get_x (cpu);
  128. case 6:
  129. return cpu_get_y (cpu);
  130. case 7:
  131. return cpu_get_sp (cpu);
  132. default:
  133. return 0;
  134. }
  135. }
  136. static void
  137. cpu_set_dst_reg (sim_cpu *cpu, uint8_t reg, uint16_t val)
  138. {
  139. switch (reg)
  140. {
  141. case 0:
  142. cpu_set_a (cpu, val);
  143. break;
  144. case 1:
  145. cpu_set_b (cpu, val);
  146. break;
  147. case 2:
  148. cpu_set_ccr (cpu, val);
  149. break;
  150. case 3:
  151. cpu_set_tmp2 (cpu, val);
  152. break;
  153. case 4:
  154. cpu_set_d (cpu, val);
  155. break;
  156. case 5:
  157. cpu_set_x (cpu, val);
  158. break;
  159. case 6:
  160. cpu_set_y (cpu, val);
  161. break;
  162. case 7:
  163. cpu_set_sp (cpu, val);
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. static void
  170. cpu_set_reg (sim_cpu *cpu, uint8_t reg, uint16_t val)
  171. {
  172. switch (reg)
  173. {
  174. case 0:
  175. cpu_set_x (cpu, val);
  176. break;
  177. case 1:
  178. cpu_set_y (cpu, val);
  179. break;
  180. case 2:
  181. cpu_set_sp (cpu, val);
  182. break;
  183. case 3:
  184. cpu_set_pc (cpu, val);
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. /* Returns the address of a 68HC12 indexed operand.
  191. Pre and post modifications are handled on the source register. */
  192. uint16_t
  193. cpu_get_indexed_operand_addr (sim_cpu *cpu, int restricted)
  194. {
  195. uint8_t reg;
  196. uint16_t sval;
  197. uint16_t addr;
  198. uint8_t code;
  199. code = cpu_fetch8 (cpu);
  200. /* n,r with 5-bit signed constant. */
  201. if ((code & 0x20) == 0)
  202. {
  203. reg = (code >> 6) & 3;
  204. sval = (code & 0x1f);
  205. if (code & 0x10)
  206. sval |= 0xfff0;
  207. addr = cpu_get_reg (cpu, reg);
  208. addr += sval;
  209. }
  210. /* Auto pre/post increment/decrement. */
  211. else if ((code & 0xc0) != 0xc0)
  212. {
  213. reg = (code >> 6) & 3;
  214. sval = (code & 0x0f);
  215. if (sval & 0x8)
  216. {
  217. sval |= 0xfff0;
  218. }
  219. else
  220. {
  221. sval = sval + 1;
  222. }
  223. addr = cpu_get_reg (cpu, reg);
  224. cpu_set_reg (cpu, reg, addr + sval);
  225. if ((code & 0x10) == 0)
  226. {
  227. addr += sval;
  228. }
  229. }
  230. /* [n,r] 16-bits offset indexed indirect. */
  231. else if ((code & 0x07) == 3)
  232. {
  233. if (restricted)
  234. {
  235. return 0;
  236. }
  237. reg = (code >> 3) & 0x03;
  238. addr = cpu_get_reg (cpu, reg);
  239. addr += cpu_fetch16 (cpu);
  240. addr = memory_read16 (cpu, addr);
  241. cpu_add_cycles (cpu, 1);
  242. }
  243. else if ((code & 0x4) == 0)
  244. {
  245. if (restricted)
  246. {
  247. return 0;
  248. }
  249. reg = (code >> 3) & 0x03;
  250. addr = cpu_get_reg (cpu, reg);
  251. if (code & 0x2)
  252. {
  253. sval = cpu_fetch16 (cpu);
  254. cpu_add_cycles (cpu, 1);
  255. }
  256. else
  257. {
  258. sval = cpu_fetch8 (cpu);
  259. if (code & 0x1)
  260. sval |= 0xff00;
  261. cpu_add_cycles (cpu, 1);
  262. }
  263. addr += sval;
  264. }
  265. else
  266. {
  267. reg = (code >> 3) & 0x03;
  268. addr = cpu_get_reg (cpu, reg);
  269. switch (code & 3)
  270. {
  271. case 0:
  272. addr += cpu_get_a (cpu);
  273. break;
  274. case 1:
  275. addr += cpu_get_b (cpu);
  276. break;
  277. case 2:
  278. addr += cpu_get_d (cpu);
  279. break;
  280. case 3:
  281. default:
  282. addr += cpu_get_d (cpu);
  283. addr = memory_read16 (cpu, addr);
  284. cpu_add_cycles (cpu, 1);
  285. break;
  286. }
  287. }
  288. return addr;
  289. }
  290. static uint8_t
  291. cpu_get_indexed_operand8 (sim_cpu *cpu, int restricted)
  292. {
  293. uint16_t addr;
  294. addr = cpu_get_indexed_operand_addr (cpu, restricted);
  295. return memory_read8 (cpu, addr);
  296. }
  297. static uint16_t
  298. cpu_get_indexed_operand16 (sim_cpu *cpu, int restricted)
  299. {
  300. uint16_t addr;
  301. addr = cpu_get_indexed_operand_addr (cpu, restricted);
  302. return memory_read16 (cpu, addr);
  303. }
  304. void
  305. cpu_move8 (sim_cpu *cpu, uint8_t code)
  306. {
  307. uint8_t src;
  308. uint16_t addr;
  309. switch (code)
  310. {
  311. case 0x0b:
  312. src = cpu_fetch8 (cpu);
  313. addr = cpu_fetch16 (cpu);
  314. break;
  315. case 0x08:
  316. addr = cpu_get_indexed_operand_addr (cpu, 1);
  317. src = cpu_fetch8 (cpu);
  318. break;
  319. case 0x0c:
  320. addr = cpu_fetch16 (cpu);
  321. src = memory_read8 (cpu, addr);
  322. addr = cpu_fetch16 (cpu);
  323. break;
  324. case 0x09:
  325. addr = cpu_get_indexed_operand_addr (cpu, 1);
  326. src = memory_read8 (cpu, cpu_fetch16 (cpu));
  327. break;
  328. case 0x0d:
  329. src = cpu_get_indexed_operand8 (cpu, 1);
  330. addr = cpu_fetch16 (cpu);
  331. break;
  332. case 0x0a:
  333. src = cpu_get_indexed_operand8 (cpu, 1);
  334. addr = cpu_get_indexed_operand_addr (cpu, 1);
  335. break;
  336. default:
  337. sim_engine_abort (CPU_STATE (cpu), cpu, 0,
  338. "Invalid code 0x%0x -- internal error?", code);
  339. return;
  340. }
  341. memory_write8 (cpu, addr, src);
  342. }
  343. void
  344. cpu_move16 (sim_cpu *cpu, uint8_t code)
  345. {
  346. uint16_t src;
  347. uint16_t addr;
  348. switch (code)
  349. {
  350. case 0x03:
  351. src = cpu_fetch16 (cpu);
  352. addr = cpu_fetch16 (cpu);
  353. break;
  354. case 0x00:
  355. addr = cpu_get_indexed_operand_addr (cpu, 1);
  356. src = cpu_fetch16 (cpu);
  357. break;
  358. case 0x04:
  359. addr = cpu_fetch16 (cpu);
  360. src = memory_read16 (cpu, addr);
  361. addr = cpu_fetch16 (cpu);
  362. break;
  363. case 0x01:
  364. addr = cpu_get_indexed_operand_addr (cpu, 1);
  365. src = memory_read16 (cpu, cpu_fetch16 (cpu));
  366. break;
  367. case 0x05:
  368. src = cpu_get_indexed_operand16 (cpu, 1);
  369. addr = cpu_fetch16 (cpu);
  370. break;
  371. case 0x02:
  372. src = cpu_get_indexed_operand16 (cpu, 1);
  373. addr = cpu_get_indexed_operand_addr (cpu, 1);
  374. break;
  375. default:
  376. sim_engine_abort (CPU_STATE (cpu), cpu, 0,
  377. "Invalid code 0x%0x -- internal error?", code);
  378. return;
  379. }
  380. memory_write16 (cpu, addr, src);
  381. }
  382. int
  383. cpu_initialize (SIM_DESC sd, sim_cpu *cpu)
  384. {
  385. sim_add_option_table (sd, 0, cpu_options);
  386. memset (&cpu->cpu_regs, 0, sizeof(cpu->cpu_regs));
  387. cpu->cpu_absolute_cycle = 0;
  388. cpu->cpu_current_cycle = 0;
  389. cpu->cpu_emul_syscall = 1;
  390. cpu->cpu_running = 1;
  391. cpu->cpu_stop_on_interrupt = 0;
  392. cpu->cpu_frequency = 8 * 1000 * 1000;
  393. cpu->cpu_use_elf_start = 0;
  394. cpu->cpu_elf_start = 0;
  395. cpu->cpu_use_local_config = 0;
  396. cpu->bank_start = 0;
  397. cpu->bank_end = 0;
  398. cpu->bank_shift = 0;
  399. cpu->cpu_config = M6811_NOSEC | M6811_NOCOP | M6811_ROMON |
  400. M6811_EEON;
  401. interrupts_initialize (sd, cpu);
  402. cpu->cpu_is_initialized = 1;
  403. return 0;
  404. }
  405. /* Reinitialize the processor after a reset. */
  406. int
  407. cpu_reset (sim_cpu *cpu)
  408. {
  409. /* Initialize the config register.
  410. It is only initialized at reset time. */
  411. memset (cpu->ios, 0, sizeof (cpu->ios));
  412. if (cpu->cpu_configured_arch->arch == bfd_arch_m68hc11)
  413. cpu->ios[M6811_INIT] = 0x1;
  414. else
  415. cpu->ios[M6811_INIT] = 0;
  416. /* Output compare registers set to 0xFFFF. */
  417. cpu->ios[M6811_TOC1_H] = 0xFF;
  418. cpu->ios[M6811_TOC1_L] = 0xFF;
  419. cpu->ios[M6811_TOC2_H] = 0xFF;
  420. cpu->ios[M6811_TOC2_L] = 0xFF;
  421. cpu->ios[M6811_TOC3_H] = 0xFF;
  422. cpu->ios[M6811_TOC4_L] = 0xFF;
  423. cpu->ios[M6811_TOC5_H] = 0xFF;
  424. cpu->ios[M6811_TOC5_L] = 0xFF;
  425. /* Setup the processor registers. */
  426. memset (&cpu->cpu_regs, 0, sizeof(cpu->cpu_regs));
  427. cpu->cpu_absolute_cycle = 0;
  428. cpu->cpu_current_cycle = 0;
  429. cpu->cpu_is_initialized = 0;
  430. /* Reset interrupts. */
  431. interrupts_reset (&cpu->cpu_interrupts);
  432. /* Reinitialize the CPU operating mode. */
  433. cpu->ios[M6811_HPRIO] = cpu->cpu_mode;
  434. return 0;
  435. }
  436. /* Reinitialize the processor after a reset. */
  437. int
  438. cpu_restart (sim_cpu *cpu)
  439. {
  440. uint16_t addr;
  441. /* Get CPU starting address depending on the CPU mode. */
  442. if (cpu->cpu_use_elf_start == 0)
  443. {
  444. switch ((cpu->ios[M6811_HPRIO]) & (M6811_SMOD | M6811_MDA))
  445. {
  446. /* Single Chip */
  447. default:
  448. case 0 :
  449. addr = memory_read16 (cpu, 0xFFFE);
  450. break;
  451. /* Expanded Multiplexed */
  452. case M6811_MDA:
  453. addr = memory_read16 (cpu, 0xFFFE);
  454. break;
  455. /* Special Bootstrap */
  456. case M6811_SMOD:
  457. addr = 0;
  458. break;
  459. /* Factory Test */
  460. case M6811_MDA | M6811_SMOD:
  461. addr = memory_read16 (cpu, 0xFFFE);
  462. break;
  463. }
  464. }
  465. else
  466. {
  467. addr = cpu->cpu_elf_start;
  468. }
  469. /* Setup the processor registers. */
  470. cpu->cpu_insn_pc = addr;
  471. cpu->cpu_regs.pc = addr;
  472. cpu->cpu_regs.ccr = M6811_X_BIT | M6811_I_BIT | M6811_S_BIT;
  473. cpu->cpu_absolute_cycle = 0;
  474. cpu->cpu_is_initialized = 1;
  475. cpu->cpu_current_cycle = 0;
  476. cpu_call (cpu, addr);
  477. return 0;
  478. }
  479. void
  480. print_io_reg_desc (SIM_DESC sd, io_reg_desc *desc, int val, int mode)
  481. {
  482. while (desc->mask)
  483. {
  484. if (val & desc->mask)
  485. sim_io_printf (sd, "%s",
  486. mode == 0 ? desc->short_name : desc->long_name);
  487. desc++;
  488. }
  489. }
  490. void
  491. print_io_byte (SIM_DESC sd, const char *name, io_reg_desc *desc,
  492. uint8_t val, uint16_t addr)
  493. {
  494. sim_io_printf (sd, " %-9.9s @ 0x%04x 0x%02x ", name, addr, val);
  495. if (desc)
  496. print_io_reg_desc (sd, desc, val, 0);
  497. }
  498. void
  499. print_io_word (SIM_DESC sd, const char *name, io_reg_desc *desc,
  500. uint16_t val, uint16_t addr)
  501. {
  502. sim_io_printf (sd, " %-9.9s @ 0x%04x 0x%04x ", name, addr, val);
  503. if (desc)
  504. print_io_reg_desc (sd, desc, val, 0);
  505. }
  506. void
  507. cpu_ccr_update_tst8 (sim_cpu *cpu, uint8_t val)
  508. {
  509. cpu_set_ccr_V (cpu, 0);
  510. cpu_set_ccr_N (cpu, val & 0x80 ? 1 : 0);
  511. cpu_set_ccr_Z (cpu, val == 0 ? 1 : 0);
  512. }
  513. uint16_t
  514. cpu_fetch_relbranch (sim_cpu *cpu)
  515. {
  516. uint16_t addr = (uint16_t) cpu_fetch8 (cpu);
  517. if (addr & 0x0080)
  518. {
  519. addr |= 0xFF00;
  520. }
  521. addr += cpu->cpu_regs.pc;
  522. return addr;
  523. }
  524. uint16_t
  525. cpu_fetch_relbranch16 (sim_cpu *cpu)
  526. {
  527. uint16_t addr = cpu_fetch16 (cpu);
  528. addr += cpu->cpu_regs.pc;
  529. return addr;
  530. }
  531. /* Push all the CPU registers (when an interruption occurs). */
  532. void
  533. cpu_push_all (sim_cpu *cpu)
  534. {
  535. if (cpu->cpu_configured_arch->arch == bfd_arch_m68hc11)
  536. {
  537. cpu_m68hc11_push_uint16 (cpu, cpu->cpu_regs.pc);
  538. cpu_m68hc11_push_uint16 (cpu, cpu->cpu_regs.iy);
  539. cpu_m68hc11_push_uint16 (cpu, cpu->cpu_regs.ix);
  540. cpu_m68hc11_push_uint16 (cpu, cpu->cpu_regs.d);
  541. cpu_m68hc11_push_uint8 (cpu, cpu->cpu_regs.ccr);
  542. }
  543. else
  544. {
  545. cpu_m68hc12_push_uint16 (cpu, cpu->cpu_regs.pc);
  546. cpu_m68hc12_push_uint16 (cpu, cpu->cpu_regs.iy);
  547. cpu_m68hc12_push_uint16 (cpu, cpu->cpu_regs.ix);
  548. cpu_m68hc12_push_uint16 (cpu, cpu->cpu_regs.d);
  549. cpu_m68hc12_push_uint8 (cpu, cpu->cpu_regs.ccr);
  550. }
  551. }
  552. /* Simulation of the dbcc/ibcc/tbcc 68HC12 conditional branch operations. */
  553. void
  554. cpu_dbcc (sim_cpu *cpu)
  555. {
  556. uint8_t code;
  557. uint16_t addr;
  558. uint16_t inc;
  559. uint16_t reg;
  560. code = cpu_fetch8 (cpu);
  561. switch (code & 0xc0)
  562. {
  563. case 0x80: /* ibcc */
  564. inc = 1;
  565. break;
  566. case 0x40: /* tbcc */
  567. inc = 0;
  568. break;
  569. case 0: /* dbcc */
  570. inc = -1;
  571. break;
  572. default:
  573. abort ();
  574. break;
  575. }
  576. addr = cpu_fetch8 (cpu);
  577. if (code & 0x10)
  578. addr |= 0xff00;
  579. addr += cpu_get_pc (cpu);
  580. reg = cpu_get_src_reg (cpu, code & 0x07);
  581. reg += inc;
  582. /* Branch according to register value. */
  583. if ((reg != 0 && (code & 0x20)) || (reg == 0 && !(code & 0x20)))
  584. {
  585. cpu_set_pc (cpu, addr);
  586. }
  587. cpu_set_dst_reg (cpu, code & 0x07, reg);
  588. }
  589. void
  590. cpu_exg (sim_cpu *cpu, uint8_t code)
  591. {
  592. uint8_t r1, r2;
  593. uint16_t src1;
  594. uint16_t src2;
  595. r1 = (code >> 4) & 0x07;
  596. r2 = code & 0x07;
  597. if (code & 0x80)
  598. {
  599. src1 = cpu_get_src_reg (cpu, r1);
  600. src2 = cpu_get_src_reg (cpu, r2);
  601. if (r2 == 1 || r2 == 2)
  602. src2 |= 0xff00;
  603. cpu_set_dst_reg (cpu, r2, src1);
  604. cpu_set_dst_reg (cpu, r1, src2);
  605. }
  606. else
  607. {
  608. src1 = cpu_get_src_reg (cpu, r1);
  609. /* Sign extend the 8-bit registers (A, B, CCR). */
  610. if ((r1 == 0 || r1 == 1 || r1 == 2) && (src1 & 0x80))
  611. src1 |= 0xff00;
  612. cpu_set_dst_reg (cpu, r2, src1);
  613. }
  614. }
  615. /* Handle special instructions. */
  616. void
  617. cpu_special (sim_cpu *cpu, enum M6811_Special special)
  618. {
  619. switch (special)
  620. {
  621. case M6811_RTI:
  622. {
  623. uint8_t ccr;
  624. ccr = cpu_m68hc11_pop_uint8 (cpu);
  625. cpu_set_ccr (cpu, ccr);
  626. cpu_set_d (cpu, cpu_m68hc11_pop_uint16 (cpu));
  627. cpu_set_x (cpu, cpu_m68hc11_pop_uint16 (cpu));
  628. cpu_set_y (cpu, cpu_m68hc11_pop_uint16 (cpu));
  629. cpu_set_pc (cpu, cpu_m68hc11_pop_uint16 (cpu));
  630. cpu_return (cpu);
  631. break;
  632. }
  633. case M6812_RTI:
  634. {
  635. uint8_t ccr;
  636. ccr = cpu_m68hc12_pop_uint8 (cpu);
  637. cpu_set_ccr (cpu, ccr);
  638. cpu_set_d (cpu, cpu_m68hc12_pop_uint16 (cpu));
  639. cpu_set_x (cpu, cpu_m68hc12_pop_uint16 (cpu));
  640. cpu_set_y (cpu, cpu_m68hc12_pop_uint16 (cpu));
  641. cpu_set_pc (cpu, cpu_m68hc12_pop_uint16 (cpu));
  642. cpu_return (cpu);
  643. break;
  644. }
  645. case M6811_WAI:
  646. /* In the ELF-start mode, we are in a special mode where
  647. the WAI corresponds to an exit. */
  648. if (cpu->cpu_use_elf_start)
  649. {
  650. cpu_set_pc (cpu, cpu->cpu_insn_pc);
  651. sim_engine_halt (CPU_STATE (cpu), cpu,
  652. NULL, NULL_CIA, sim_exited,
  653. cpu_get_d (cpu));
  654. return;
  655. }
  656. /* SCz: not correct... */
  657. cpu_push_all (cpu);
  658. break;
  659. case M6811_SWI:
  660. interrupts_raise (&cpu->cpu_interrupts, M6811_INT_SWI);
  661. interrupts_process (&cpu->cpu_interrupts);
  662. break;
  663. case M6811_EMUL_SYSCALL:
  664. case M6811_ILLEGAL:
  665. if (cpu->cpu_emul_syscall)
  666. {
  667. uint8_t op = memory_read8 (cpu,
  668. cpu_get_pc (cpu) - 1);
  669. if (op == 0x41)
  670. {
  671. cpu_set_pc (cpu, cpu->cpu_insn_pc);
  672. sim_engine_halt (CPU_STATE (cpu), cpu,
  673. NULL, NULL_CIA, sim_exited,
  674. cpu_get_d (cpu));
  675. return;
  676. }
  677. else
  678. {
  679. emul_os (op, cpu);
  680. }
  681. return;
  682. }
  683. interrupts_raise (&cpu->cpu_interrupts, M6811_INT_ILLEGAL);
  684. interrupts_process (&cpu->cpu_interrupts);
  685. break;
  686. case M6811_TEST:
  687. case M6812_BGND:
  688. {
  689. SIM_DESC sd;
  690. sd = CPU_STATE (cpu);
  691. /* Breakpoint instruction if we are under gdb. */
  692. if (STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG)
  693. {
  694. cpu->cpu_regs.pc --;
  695. sim_engine_halt (CPU_STATE (cpu), cpu,
  696. 0, cpu_get_pc (cpu), sim_stopped,
  697. SIM_SIGTRAP);
  698. }
  699. /* else this is a nop but not in test factory mode. */
  700. break;
  701. }
  702. case M6812_IDIVS:
  703. {
  704. int32_t src1 = (int16_t) cpu_get_d (cpu);
  705. int32_t src2 = (int16_t) cpu_get_x (cpu);
  706. if (src2 == 0)
  707. {
  708. cpu_set_ccr_C (cpu, 1);
  709. }
  710. else
  711. {
  712. cpu_set_d (cpu, src1 % src2);
  713. src1 = src1 / src2;
  714. cpu_set_x (cpu, src1);
  715. cpu_set_ccr_C (cpu, 0);
  716. cpu_set_ccr_Z (cpu, src1 == 0);
  717. cpu_set_ccr_N (cpu, src1 & 0x8000);
  718. cpu_set_ccr_V (cpu, src1 >= 32768 || src1 < -32768);
  719. }
  720. }
  721. break;
  722. case M6812_EDIV:
  723. {
  724. uint32_t src1 = (uint32_t) cpu_get_x (cpu);
  725. uint32_t src2 = (uint32_t) (cpu_get_y (cpu) << 16)
  726. | (uint32_t) (cpu_get_d (cpu));
  727. if (src1 == 0)
  728. {
  729. cpu_set_ccr_C (cpu, 1);
  730. }
  731. else
  732. {
  733. cpu_set_ccr_C (cpu, 0);
  734. cpu_set_d (cpu, src2 % src1);
  735. src2 = src2 / src1;
  736. cpu_set_y (cpu, src2);
  737. cpu_set_ccr_Z (cpu, src2 == 0);
  738. cpu_set_ccr_N (cpu, (src2 & 0x8000) != 0);
  739. cpu_set_ccr_V (cpu, (src2 & 0xffff0000) != 0);
  740. }
  741. }
  742. break;
  743. case M6812_EDIVS:
  744. {
  745. int32_t src1 = (int16_t) cpu_get_x (cpu);
  746. int32_t src2 = (uint32_t) (cpu_get_y (cpu) << 16)
  747. | (uint32_t) (cpu_get_d (cpu));
  748. if (src1 == 0)
  749. {
  750. cpu_set_ccr_C (cpu, 1);
  751. }
  752. else
  753. {
  754. cpu_set_ccr_C (cpu, 0);
  755. cpu_set_d (cpu, src2 % src1);
  756. src2 = src2 / src1;
  757. cpu_set_y (cpu, src2);
  758. cpu_set_ccr_Z (cpu, src2 == 0);
  759. cpu_set_ccr_N (cpu, (src2 & 0x8000) != 0);
  760. cpu_set_ccr_V (cpu, src2 > 32767 || src2 < -32768);
  761. }
  762. }
  763. break;
  764. case M6812_EMULS:
  765. {
  766. int32_t src1, src2;
  767. src1 = (int16_t) cpu_get_d (cpu);
  768. src2 = (int16_t) cpu_get_y (cpu);
  769. src1 = src1 * src2;
  770. cpu_set_d (cpu, src1 & 0x0ffff);
  771. cpu_set_y (cpu, src1 >> 16);
  772. cpu_set_ccr_Z (cpu, src1 == 0);
  773. cpu_set_ccr_N (cpu, (src1 & 0x80000000) != 0);
  774. cpu_set_ccr_C (cpu, (src1 & 0x00008000) != 0);
  775. }
  776. break;
  777. case M6812_EMACS:
  778. {
  779. int32_t src1, src2;
  780. uint16_t addr;
  781. addr = cpu_fetch16 (cpu);
  782. src1 = (int16_t) memory_read16 (cpu, cpu_get_x (cpu));
  783. src2 = (int16_t) memory_read16 (cpu, cpu_get_y (cpu));
  784. src1 = src1 * src2;
  785. src2 = (((uint32_t) memory_read16 (cpu, addr)) << 16)
  786. | (uint32_t) memory_read16 (cpu, addr + 2);
  787. memory_write16 (cpu, addr, (src1 + src2) >> 16);
  788. memory_write16 (cpu, addr + 2, (src1 + src2));
  789. }
  790. break;
  791. case M6812_CALL:
  792. {
  793. uint8_t page;
  794. uint16_t addr;
  795. addr = cpu_fetch16 (cpu);
  796. page = cpu_fetch8 (cpu);
  797. cpu_m68hc12_push_uint16 (cpu, cpu_get_pc (cpu));
  798. cpu_m68hc12_push_uint8 (cpu, cpu_get_page (cpu));
  799. cpu_set_page (cpu, page);
  800. cpu_set_pc (cpu, addr);
  801. }
  802. break;
  803. case M6812_CALL_INDIRECT:
  804. {
  805. uint8_t code;
  806. uint16_t addr;
  807. uint8_t page;
  808. code = memory_read8 (cpu, cpu_get_pc (cpu));
  809. /* Indirect addressing call has the page specified in the
  810. memory location pointed to by the address. */
  811. if ((code & 0xE3) == 0xE3)
  812. {
  813. addr = cpu_get_indexed_operand_addr (cpu, 0);
  814. page = memory_read8 (cpu, addr + 2);
  815. addr = memory_read16 (cpu, addr);
  816. }
  817. else
  818. {
  819. /* Otherwise, page is in the opcode. */
  820. addr = cpu_get_indexed_operand16 (cpu, 0);
  821. page = cpu_fetch8 (cpu);
  822. }
  823. cpu_m68hc12_push_uint16 (cpu, cpu_get_pc (cpu));
  824. cpu_m68hc12_push_uint8 (cpu, cpu_get_page (cpu));
  825. cpu_set_page (cpu, page);
  826. cpu_set_pc (cpu, addr);
  827. }
  828. break;
  829. case M6812_RTC:
  830. {
  831. uint8_t page = cpu_m68hc12_pop_uint8 (cpu);
  832. uint16_t addr = cpu_m68hc12_pop_uint16 (cpu);
  833. cpu_set_page (cpu, page);
  834. cpu_set_pc (cpu, addr);
  835. }
  836. break;
  837. case M6812_ETBL:
  838. default:
  839. sim_engine_halt (CPU_STATE (cpu), cpu, NULL,
  840. cpu_get_pc (cpu), sim_stopped,
  841. SIM_SIGILL);
  842. break;
  843. }
  844. }
  845. void
  846. cpu_single_step (sim_cpu *cpu)
  847. {
  848. cpu->cpu_current_cycle = 0;
  849. cpu->cpu_insn_pc = cpu_get_pc (cpu);
  850. /* Handle the pending interrupts. If an interrupt is handled,
  851. treat this as an single step. */
  852. if (interrupts_process (&cpu->cpu_interrupts))
  853. {
  854. cpu->cpu_absolute_cycle += cpu->cpu_current_cycle;
  855. return;
  856. }
  857. /* printf("PC = 0x%04x\n", cpu_get_pc (cpu));*/
  858. cpu->cpu_interpretor (cpu);
  859. cpu->cpu_absolute_cycle += cpu->cpu_current_cycle;
  860. }
  861. /* VARARGS */
  862. void
  863. sim_memory_error (sim_cpu *cpu, SIM_SIGNAL excep,
  864. uint16_t addr, const char *message, ...)
  865. {
  866. char buf[1024];
  867. va_list args;
  868. va_start (args, message);
  869. vsprintf (buf, message, args);
  870. va_end (args);
  871. sim_io_printf (CPU_STATE (cpu), "%s\n", buf);
  872. cpu_memory_exception (cpu, excep, addr, buf);
  873. }
  874. void
  875. cpu_memory_exception (sim_cpu *cpu, SIM_SIGNAL excep,
  876. uint16_t addr, const char *message)
  877. {
  878. if (cpu->cpu_running == 0)
  879. return;
  880. cpu_set_pc (cpu, cpu->cpu_insn_pc);
  881. sim_engine_halt (CPU_STATE (cpu), cpu, NULL,
  882. cpu_get_pc (cpu), sim_stopped, excep);
  883. #if 0
  884. cpu->mem_exception = excep;
  885. cpu->fault_addr = addr;
  886. cpu->fault_msg = strdup (message);
  887. if (cpu->cpu_use_handler)
  888. {
  889. longjmp (&cpu->cpu_exception_handler, 1);
  890. }
  891. (* cpu->callback->printf_filtered)
  892. (cpu->callback, "Fault at 0x%04x: %s\n", addr, message);
  893. #endif
  894. }
  895. void
  896. cpu_info (SIM_DESC sd, sim_cpu *cpu)
  897. {
  898. sim_io_printf (sd, "CPU info:\n");
  899. sim_io_printf (sd, " Absolute cycle: %s\n",
  900. cycle_to_string (cpu, cpu->cpu_absolute_cycle,
  901. PRINT_TIME | PRINT_CYCLE));
  902. sim_io_printf (sd, " Syscall emulation: %s\n",
  903. cpu->cpu_emul_syscall ? "yes, via 0xcd <n>" : "no");
  904. sim_io_printf (sd, " Memory errors detection: %s\n",
  905. cpu->cpu_check_memory ? "yes" : "no");
  906. sim_io_printf (sd, " Stop on interrupt: %s\n",
  907. cpu->cpu_stop_on_interrupt ? "yes" : "no");
  908. }