interp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /* Simulation code for the CR16 processor.
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. Contributed by M Ranga Swami Reddy <MR.Swami.Reddy@nsc.com>
  4. This file is part of GDB, the GNU debugger.
  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, or (at your option)
  8. 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 <inttypes.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "bfd.h"
  22. #include "sim/callback.h"
  23. #include "sim/sim.h"
  24. #include "sim-main.h"
  25. #include "sim-options.h"
  26. #include "sim-signal.h"
  27. #include "gdb/sim-cr16.h"
  28. #include "gdb/signals.h"
  29. #include "opcode/cr16.h"
  30. #include "target-newlib-syscall.h"
  31. struct _state State;
  32. int cr16_debug;
  33. uint32_t OP[4];
  34. uint32_t sign_flag;
  35. static struct hash_entry *lookup_hash (SIM_DESC, SIM_CPU *, uint64_t ins, int size);
  36. static void get_operands (operand_desc *s, uint64_t mcode, int isize, int nops);
  37. #define MAX_HASH 16
  38. struct hash_entry
  39. {
  40. struct hash_entry *next;
  41. uint32_t opcode;
  42. uint32_t mask;
  43. int format;
  44. int size;
  45. struct simops *ops;
  46. };
  47. struct hash_entry hash_table[MAX_HASH+1];
  48. INLINE static long
  49. hash(unsigned long long insn, int format)
  50. {
  51. unsigned int i = 4, tmp;
  52. if (format)
  53. {
  54. while ((insn >> i) != 0) i +=4;
  55. return ((insn >> (i-4)) & 0xf); /* Use last 4 bits as hask key. */
  56. }
  57. return ((insn & 0xF)); /* Use last 4 bits as hask key. */
  58. }
  59. INLINE static struct hash_entry *
  60. lookup_hash (SIM_DESC sd, SIM_CPU *cpu, uint64_t ins, int size)
  61. {
  62. uint32_t mask;
  63. struct hash_entry *h;
  64. h = &hash_table[hash(ins,1)];
  65. mask = (((1 << (32 - h->mask)) -1) << h->mask);
  66. /* Adjuest mask for branch with 2 word instructions. */
  67. if (streq(h->ops->mnemonic,"b") && h->size == 2)
  68. mask = 0xff0f0000;
  69. while ((ins & mask) != (BIN(h->opcode, h->mask)))
  70. {
  71. if (h->next == NULL)
  72. sim_engine_halt (sd, cpu, NULL, PC, sim_stopped, SIM_SIGILL);
  73. h = h->next;
  74. mask = (((1 << (32 - h->mask)) -1) << h->mask);
  75. /* Adjuest mask for branch with 2 word instructions. */
  76. if ((streq(h->ops->mnemonic,"b")) && h->size == 2)
  77. mask = 0xff0f0000;
  78. }
  79. return (h);
  80. }
  81. INLINE static void
  82. get_operands (operand_desc *s, uint64_t ins, int isize, int nops)
  83. {
  84. uint32_t i, opn = 0, start_bit = 0, op_type = 0;
  85. int32_t op_size = 0, mask = 0;
  86. if (isize == 1) /* Trunkcate the extra 16 bits of INS. */
  87. ins = ins >> 16;
  88. for (i=0; i < 4; ++i,++opn)
  89. {
  90. if (s[opn].op_type == dummy) break;
  91. op_type = s[opn].op_type;
  92. start_bit = s[opn].shift;
  93. op_size = cr16_optab[op_type].bit_size;
  94. switch (op_type)
  95. {
  96. case imm3: case imm4: case imm5: case imm6:
  97. {
  98. if (isize == 1)
  99. OP[i] = ((ins >> 4) & ((1 << op_size) -1));
  100. else
  101. OP[i] = ((ins >> (32 - start_bit)) & ((1 << op_size) -1));
  102. if (OP[i] & ((long)1 << (op_size -1)))
  103. {
  104. sign_flag = 1;
  105. OP[i] = ~(OP[i]) + 1;
  106. }
  107. OP[i] = (unsigned long int)(OP[i] & (((long)1 << op_size) -1));
  108. }
  109. break;
  110. case uimm3: case uimm3_1: case uimm4_1:
  111. switch (isize)
  112. {
  113. case 1:
  114. OP[i] = ((ins >> 4) & ((1 << op_size) -1)); break;
  115. case 2:
  116. OP[i] = ((ins >> (32 - start_bit)) & ((1 << op_size) -1));break;
  117. default: /* for case 3. */
  118. OP[i] = ((ins >> (16 + start_bit)) & ((1 << op_size) -1)); break;
  119. break;
  120. }
  121. break;
  122. case uimm4:
  123. switch (isize)
  124. {
  125. case 1:
  126. if (start_bit == 20)
  127. OP[i] = ((ins >> 4) & ((1 << op_size) -1));
  128. else
  129. OP[i] = (ins & ((1 << op_size) -1));
  130. break;
  131. case 2:
  132. OP[i] = ((ins >> start_bit) & ((1 << op_size) -1));
  133. break;
  134. case 3:
  135. OP[i] = ((ins >> (start_bit + 16)) & ((1 << op_size) -1));
  136. break;
  137. default:
  138. OP[i] = ((ins >> start_bit) & ((1 << op_size) -1));
  139. break;
  140. }
  141. break;
  142. case imm16: case uimm16:
  143. OP[i] = ins & 0xFFFF;
  144. break;
  145. case uimm20: case imm20:
  146. OP[i] = ins & (((long)1 << op_size) - 1);
  147. break;
  148. case imm32: case uimm32:
  149. OP[i] = ins & 0xFFFFFFFF;
  150. break;
  151. case uimm5: break; /*NOT USED. */
  152. OP[i] = ins & ((1 << op_size) - 1); break;
  153. case disps5:
  154. OP[i] = (ins >> 4) & ((1 << 4) - 1);
  155. OP[i] = (OP[i] * 2) + 2;
  156. if (OP[i] & ((long)1 << 5))
  157. {
  158. sign_flag = 1;
  159. OP[i] = ~(OP[i]) + 1;
  160. OP[i] = (unsigned long int)(OP[i] & 0x1F);
  161. }
  162. break;
  163. case dispe9:
  164. OP[i] = ((((ins >> 8) & 0xf) << 4) | (ins & 0xf));
  165. OP[i] <<= 1;
  166. if (OP[i] & ((long)1 << 8))
  167. {
  168. sign_flag = 1;
  169. OP[i] = ~(OP[i]) + 1;
  170. OP[i] = (unsigned long int)(OP[i] & 0xFF);
  171. }
  172. break;
  173. case disps17:
  174. OP[i] = (ins & 0xFFFF);
  175. if (OP[i] & 1)
  176. {
  177. OP[i] = (OP[i] & 0xFFFE);
  178. sign_flag = 1;
  179. OP[i] = ~(OP[i]) + 1;
  180. OP[i] = (unsigned long int)(OP[i] & 0xFFFF);
  181. }
  182. break;
  183. case disps25:
  184. if (isize == 2)
  185. OP[i] = (ins & 0xFFFFFF);
  186. else
  187. OP[i] = (ins & 0xFFFF) | (((ins >> 24) & 0xf) << 16) |
  188. (((ins >> 16) & 0xf) << 20);
  189. if (OP[i] & 1)
  190. {
  191. OP[i] = (OP[i] & 0xFFFFFE);
  192. sign_flag = 1;
  193. OP[i] = ~(OP[i]) + 1;
  194. OP[i] = (unsigned long int)(OP[i] & 0xFFFFFF);
  195. }
  196. break;
  197. case abs20:
  198. if (isize == 3)
  199. OP[i] = (ins) & 0xFFFFF;
  200. else
  201. OP[i] = (ins >> start_bit) & 0xFFFFF;
  202. break;
  203. case abs24:
  204. if (isize == 3)
  205. OP[i] = ((ins & 0xFFFF) | (((ins >> 16) & 0xf) << 20)
  206. | (((ins >> 24) & 0xf) << 16));
  207. else
  208. OP[i] = (ins >> 16) & 0xFFFFFF;
  209. break;
  210. case rra:
  211. case rbase: break; /* NOT USED. */
  212. case rbase_disps20: case rbase_dispe20:
  213. case rpbase_disps20: case rpindex_disps20:
  214. OP[i] = ((((ins >> 24)&0xf) << 16)|((ins) & 0xFFFF));
  215. OP[++i] = (ins >> 16) & 0xF; /* get 4 bit for reg. */
  216. break;
  217. case rpbase_disps0:
  218. OP[i] = 0; /* 4 bit disp const. */
  219. OP[++i] = (ins) & 0xF; /* get 4 bit for reg. */
  220. break;
  221. case rpbase_dispe4:
  222. OP[i] = ((ins >> 8) & 0xF) * 2; /* 4 bit disp const. */
  223. OP[++i] = (ins) & 0xF; /* get 4 bit for reg. */
  224. break;
  225. case rpbase_disps4:
  226. OP[i] = ((ins >> 8) & 0xF); /* 4 bit disp const. */
  227. OP[++i] = (ins) & 0xF; /* get 4 bit for reg. */
  228. break;
  229. case rpbase_disps16:
  230. OP[i] = (ins) & 0xFFFF;
  231. OP[++i] = (ins >> 16) & 0xF; /* get 4 bit for reg. */
  232. break;
  233. case rpindex_disps0:
  234. OP[i] = 0;
  235. OP[++i] = (ins >> 4) & 0xF; /* get 4 bit for reg. */
  236. OP[++i] = (ins >> 8) & 0x1; /* get 1 bit for index-reg. */
  237. break;
  238. case rpindex_disps14:
  239. OP[i] = (ins) & 0x3FFF;
  240. OP[++i] = (ins >> 14) & 0x1; /* get 1 bit for index-reg. */
  241. OP[++i] = (ins >> 16) & 0xF; /* get 4 bit for reg. */
  242. case rindex7_abs20:
  243. case rindex8_abs20:
  244. OP[i] = (ins) & 0xFFFFF;
  245. OP[++i] = (ins >> 24) & 0x1; /* get 1 bit for index-reg. */
  246. OP[++i] = (ins >> 20) & 0xF; /* get 4 bit for reg. */
  247. break;
  248. case regr: case regp: case pregr: case pregrp:
  249. switch(isize)
  250. {
  251. case 1:
  252. if (start_bit == 20) OP[i] = (ins >> 4) & 0xF;
  253. else if (start_bit == 16) OP[i] = ins & 0xF;
  254. break;
  255. case 2: OP[i] = (ins >> start_bit) & 0xF; break;
  256. case 3: OP[i] = (ins >> (start_bit + 16)) & 0xF; break;
  257. }
  258. break;
  259. case cc:
  260. {
  261. if (isize == 1) OP[i] = (ins >> 4) & 0xF;
  262. else if (isize == 2) OP[i] = (ins >> start_bit) & 0xF;
  263. else OP[i] = (ins >> (start_bit + 16)) & 0xF;
  264. break;
  265. }
  266. default: break;
  267. }
  268. /* For ESC on uimm4_1 operand. */
  269. if (op_type == uimm4_1)
  270. if (OP[i] == 9)
  271. OP[i] = -1;
  272. /* For increment by 1. */
  273. if ((op_type == pregr) || (op_type == pregrp))
  274. OP[i] += 1;
  275. }
  276. /* FIXME: for tracing, update values that need to be updated each
  277. instruction decode cycle */
  278. State.trace.psw = PSR;
  279. }
  280. static int
  281. do_run (SIM_DESC sd, SIM_CPU *cpu, uint64_t mcode)
  282. {
  283. struct hash_entry *h;
  284. #ifdef DEBUG
  285. if ((cr16_debug & DEBUG_INSTRUCTION) != 0)
  286. sim_io_printf (sd, "do_long 0x%" PRIx64 "\n", mcode);
  287. #endif
  288. h = lookup_hash (sd, cpu, mcode, 1);
  289. if ((h == NULL) || (h->opcode == 0))
  290. return 0;
  291. if (h->size == 3)
  292. mcode = (mcode << 16) | RW (PC + 4);
  293. /* Re-set OP list. */
  294. OP[0] = OP[1] = OP[2] = OP[3] = sign_flag = 0;
  295. /* for push/pop/pushrtn with RA instructions. */
  296. if ((h->format & REG_LIST) && (mcode & 0x800000))
  297. OP[2] = 1; /* Set 1 for RA operand. */
  298. /* numops == 0 means, no operands. */
  299. if (((h->ops) != NULL) && (((h->ops)->numops) != 0))
  300. get_operands ((h->ops)->operands, mcode, h->size, (h->ops)->numops);
  301. //State.ins_type = h->flags;
  302. (h->ops->func) (sd, cpu);
  303. return h->size;
  304. }
  305. static sim_cia
  306. cr16_pc_get (sim_cpu *cpu)
  307. {
  308. return PC;
  309. }
  310. static void
  311. cr16_pc_set (sim_cpu *cpu, sim_cia pc)
  312. {
  313. SIM_DESC sd = CPU_STATE (cpu);
  314. SET_PC (pc);
  315. }
  316. static void
  317. free_state (SIM_DESC sd)
  318. {
  319. if (STATE_MODULES (sd) != NULL)
  320. sim_module_uninstall (sd);
  321. sim_cpu_free_all (sd);
  322. sim_state_free (sd);
  323. }
  324. static int cr16_reg_fetch (SIM_CPU *, int, unsigned char *, int);
  325. static int cr16_reg_store (SIM_CPU *, int, unsigned char *, int);
  326. SIM_DESC
  327. sim_open (SIM_OPEN_KIND kind, struct host_callback_struct *cb,
  328. struct bfd *abfd, char * const *argv)
  329. {
  330. struct simops *s;
  331. struct hash_entry *h;
  332. static int init_p = 0;
  333. char **p;
  334. int i;
  335. SIM_DESC sd = sim_state_alloc (kind, cb);
  336. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  337. /* Set default options before parsing user options. */
  338. current_target_byte_order = BFD_ENDIAN_LITTLE;
  339. cb->syscall_map = cb_cr16_syscall_map;
  340. /* The cpu data is kept in a separately allocated chunk of memory. */
  341. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
  342. {
  343. free_state (sd);
  344. return 0;
  345. }
  346. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  347. {
  348. free_state (sd);
  349. return 0;
  350. }
  351. /* The parser will print an error message for us, so we silently return. */
  352. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  353. {
  354. free_state (sd);
  355. return 0;
  356. }
  357. /* Check for/establish the a reference program image. */
  358. if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
  359. {
  360. free_state (sd);
  361. return 0;
  362. }
  363. /* Configure/verify the target byte order and other runtime
  364. configuration options. */
  365. if (sim_config (sd) != SIM_RC_OK)
  366. {
  367. sim_module_uninstall (sd);
  368. return 0;
  369. }
  370. if (sim_post_argv_init (sd) != SIM_RC_OK)
  371. {
  372. /* Uninstall the modules to avoid memory leaks,
  373. file descriptor leaks, etc. */
  374. sim_module_uninstall (sd);
  375. return 0;
  376. }
  377. /* CPU specific initialization. */
  378. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  379. {
  380. SIM_CPU *cpu = STATE_CPU (sd, i);
  381. CPU_REG_FETCH (cpu) = cr16_reg_fetch;
  382. CPU_REG_STORE (cpu) = cr16_reg_store;
  383. CPU_PC_FETCH (cpu) = cr16_pc_get;
  384. CPU_PC_STORE (cpu) = cr16_pc_set;
  385. }
  386. /* The CR16 has an interrupt controller at 0xFC00, but we don't currently
  387. handle that. Revisit if anyone ever implements operating mode. */
  388. /* cr16 memory: There are three separate cr16 memory regions IMEM,
  389. UMEM and DMEM. The IMEM and DMEM are further broken down into
  390. blocks (very like VM pages). This might not match the hardware,
  391. but it matches what the toolchain currently expects. Ugh. */
  392. sim_do_commandf (sd, "memory-size %#x", 20 * 1024 * 1024);
  393. /* put all the opcodes in the hash table. */
  394. if (!init_p++)
  395. {
  396. for (s = Simops; s->func; s++)
  397. {
  398. switch(32 - s->mask)
  399. {
  400. case 0x4:
  401. h = &hash_table[hash(s->opcode, 0)];
  402. break;
  403. case 0x7:
  404. if (((s->opcode << 1) >> 4) != 0)
  405. h = &hash_table[hash((s->opcode << 1) >> 4, 0)];
  406. else
  407. h = &hash_table[hash((s->opcode << 1), 0)];
  408. break;
  409. case 0x8:
  410. if ((s->opcode >> 4) != 0)
  411. h = &hash_table[hash(s->opcode >> 4, 0)];
  412. else
  413. h = &hash_table[hash(s->opcode, 0)];
  414. break;
  415. case 0x9:
  416. if (((s->opcode >> 1) >> 4) != 0)
  417. h = &hash_table[hash((s->opcode >>1) >> 4, 0)];
  418. else
  419. h = &hash_table[hash((s->opcode >> 1), 0)];
  420. break;
  421. case 0xa:
  422. if ((s->opcode >> 8) != 0)
  423. h = &hash_table[hash(s->opcode >> 8, 0)];
  424. else if ((s->opcode >> 4) != 0)
  425. h = &hash_table[hash(s->opcode >> 4, 0)];
  426. else
  427. h = &hash_table[hash(s->opcode, 0)];
  428. break;
  429. case 0xc:
  430. if ((s->opcode >> 8) != 0)
  431. h = &hash_table[hash(s->opcode >> 8, 0)];
  432. else if ((s->opcode >> 4) != 0)
  433. h = &hash_table[hash(s->opcode >> 4, 0)];
  434. else
  435. h = &hash_table[hash(s->opcode, 0)];
  436. break;
  437. case 0xd:
  438. if (((s->opcode >> 1) >> 8) != 0)
  439. h = &hash_table[hash((s->opcode >>1) >> 8, 0)];
  440. else if (((s->opcode >> 1) >> 4) != 0)
  441. h = &hash_table[hash((s->opcode >>1) >> 4, 0)];
  442. else
  443. h = &hash_table[hash((s->opcode >>1), 0)];
  444. break;
  445. case 0x10:
  446. if ((s->opcode >> 0xc) != 0)
  447. h = &hash_table[hash(s->opcode >> 12, 0)];
  448. else if ((s->opcode >> 8) != 0)
  449. h = &hash_table[hash(s->opcode >> 8, 0)];
  450. else if ((s->opcode >> 4) != 0)
  451. h = &hash_table[hash(s->opcode >> 4, 0)];
  452. else
  453. h = &hash_table[hash(s->opcode, 0)];
  454. break;
  455. case 0x14:
  456. if ((s->opcode >> 16) != 0)
  457. h = &hash_table[hash(s->opcode >> 16, 0)];
  458. else if ((s->opcode >> 12) != 0)
  459. h = &hash_table[hash(s->opcode >> 12, 0)];
  460. else if ((s->opcode >> 8) != 0)
  461. h = &hash_table[hash(s->opcode >> 8, 0)];
  462. else if ((s->opcode >> 4) != 0)
  463. h = &hash_table[hash(s->opcode >> 4, 0)];
  464. else
  465. h = &hash_table[hash(s->opcode, 0)];
  466. break;
  467. default:
  468. continue;
  469. }
  470. /* go to the last entry in the chain. */
  471. while (h->next)
  472. h = h->next;
  473. if (h->ops)
  474. {
  475. h->next = (struct hash_entry *) calloc(1,sizeof(struct hash_entry));
  476. if (!h->next)
  477. perror ("malloc failure");
  478. h = h->next;
  479. }
  480. h->ops = s;
  481. h->mask = s->mask;
  482. h->opcode = s->opcode;
  483. h->format = s->format;
  484. h->size = s->size;
  485. }
  486. }
  487. return sd;
  488. }
  489. static void
  490. step_once (SIM_DESC sd, SIM_CPU *cpu)
  491. {
  492. uint32_t curr_ins_size = 0;
  493. uint64_t mcode = RLW (PC);
  494. State.pc_changed = 0;
  495. curr_ins_size = do_run (sd, cpu, mcode);
  496. #if CR16_DEBUG
  497. sim_io_printf (sd, "INS: PC=0x%X, mcode=0x%X\n", PC, mcode);
  498. #endif
  499. if (curr_ins_size == 0)
  500. sim_engine_halt (sd, cpu, NULL, PC, sim_exited, GPR (2));
  501. else if (!State.pc_changed)
  502. SET_PC (PC + (curr_ins_size * 2)); /* For word instructions. */
  503. #if 0
  504. /* Check for a breakpoint trap on this instruction. This
  505. overrides any pending branches or loops */
  506. if (PSR_DB && PC == DBS)
  507. {
  508. SET_BPC (PC);
  509. SET_BPSR (PSR);
  510. SET_PC (SDBT_VECTOR_START);
  511. }
  512. #endif
  513. /* Writeback all the DATA / PC changes */
  514. SLOT_FLUSH ();
  515. }
  516. void
  517. sim_engine_run (SIM_DESC sd,
  518. int next_cpu_nr, /* ignore */
  519. int nr_cpus, /* ignore */
  520. int siggnal)
  521. {
  522. sim_cpu *cpu;
  523. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  524. cpu = STATE_CPU (sd, 0);
  525. switch (siggnal)
  526. {
  527. case 0:
  528. break;
  529. case GDB_SIGNAL_BUS:
  530. case GDB_SIGNAL_SEGV:
  531. SET_PC (PC);
  532. SET_PSR (PSR);
  533. JMP (AE_VECTOR_START);
  534. SLOT_FLUSH ();
  535. break;
  536. case GDB_SIGNAL_ILL:
  537. SET_PC (PC);
  538. SET_PSR (PSR);
  539. SET_HW_PSR ((PSR & (PSR_C_BIT)));
  540. JMP (RIE_VECTOR_START);
  541. SLOT_FLUSH ();
  542. break;
  543. default:
  544. /* just ignore it */
  545. break;
  546. }
  547. while (1)
  548. {
  549. step_once (sd, cpu);
  550. if (sim_events_tick (sd))
  551. sim_events_process (sd);
  552. }
  553. }
  554. SIM_RC
  555. sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
  556. char * const *argv, char * const *env)
  557. {
  558. bfd_vma start_address;
  559. /* reset all state information */
  560. memset (&State, 0, sizeof (State));
  561. /* There was a hack here to copy the values of argc and argv into r0
  562. and r1. The values were also saved into some high memory that
  563. won't be overwritten by the stack (0x7C00). The reason for doing
  564. this was to allow the 'run' program to accept arguments. Without
  565. the hack, this is not possible anymore. If the simulator is run
  566. from the debugger, arguments cannot be passed in, so this makes
  567. no difference. */
  568. /* set PC */
  569. if (abfd != NULL)
  570. start_address = bfd_get_start_address (abfd);
  571. else
  572. start_address = 0x0;
  573. #ifdef DEBUG
  574. if (cr16_debug)
  575. sim_io_printf (sd, "sim_create_inferior: PC=0x%" BFD_VMA_FMT "x\n",
  576. start_address);
  577. #endif
  578. {
  579. SIM_CPU *cpu = STATE_CPU (sd, 0);
  580. SET_CREG (PC_CR, start_address);
  581. }
  582. SLOT_FLUSH ();
  583. return SIM_RC_OK;
  584. }
  585. static uint32_t
  586. cr16_extract_unsigned_integer (unsigned char *addr, int len)
  587. {
  588. uint32_t retval;
  589. unsigned char * p;
  590. unsigned char * startaddr = (unsigned char *)addr;
  591. unsigned char * endaddr = startaddr + len;
  592. retval = 0;
  593. for (p = endaddr; p > startaddr;)
  594. retval = (retval << 8) | *--p;
  595. return retval;
  596. }
  597. static void
  598. cr16_store_unsigned_integer (unsigned char *addr, int len, uint32_t val)
  599. {
  600. unsigned char *p;
  601. unsigned char *startaddr = addr;
  602. unsigned char *endaddr = startaddr + len;
  603. for (p = startaddr; p < endaddr;)
  604. {
  605. *p++ = val & 0xff;
  606. val >>= 8;
  607. }
  608. }
  609. static int
  610. cr16_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
  611. {
  612. int size;
  613. switch ((enum sim_cr16_regs) rn)
  614. {
  615. case SIM_CR16_R0_REGNUM:
  616. case SIM_CR16_R1_REGNUM:
  617. case SIM_CR16_R2_REGNUM:
  618. case SIM_CR16_R3_REGNUM:
  619. case SIM_CR16_R4_REGNUM:
  620. case SIM_CR16_R5_REGNUM:
  621. case SIM_CR16_R6_REGNUM:
  622. case SIM_CR16_R7_REGNUM:
  623. case SIM_CR16_R8_REGNUM:
  624. case SIM_CR16_R9_REGNUM:
  625. case SIM_CR16_R10_REGNUM:
  626. case SIM_CR16_R11_REGNUM:
  627. cr16_store_unsigned_integer (memory, 2, GPR (rn - SIM_CR16_R0_REGNUM));
  628. size = 2;
  629. break;
  630. case SIM_CR16_R12_REGNUM:
  631. case SIM_CR16_R13_REGNUM:
  632. case SIM_CR16_R14_REGNUM:
  633. case SIM_CR16_R15_REGNUM:
  634. cr16_store_unsigned_integer (memory, 4, GPR (rn - SIM_CR16_R0_REGNUM));
  635. size = 4;
  636. break;
  637. case SIM_CR16_PC_REGNUM:
  638. case SIM_CR16_ISP_REGNUM:
  639. case SIM_CR16_USP_REGNUM:
  640. case SIM_CR16_INTBASE_REGNUM:
  641. case SIM_CR16_PSR_REGNUM:
  642. case SIM_CR16_CFG_REGNUM:
  643. case SIM_CR16_DBS_REGNUM:
  644. case SIM_CR16_DCR_REGNUM:
  645. case SIM_CR16_DSR_REGNUM:
  646. case SIM_CR16_CAR0_REGNUM:
  647. case SIM_CR16_CAR1_REGNUM:
  648. cr16_store_unsigned_integer (memory, 4, CREG (rn - SIM_CR16_PC_REGNUM));
  649. size = 4;
  650. break;
  651. default:
  652. size = 0;
  653. break;
  654. }
  655. return size;
  656. }
  657. static int
  658. cr16_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
  659. {
  660. SIM_DESC sd = CPU_STATE (cpu);
  661. int size;
  662. switch ((enum sim_cr16_regs) rn)
  663. {
  664. case SIM_CR16_R0_REGNUM:
  665. case SIM_CR16_R1_REGNUM:
  666. case SIM_CR16_R2_REGNUM:
  667. case SIM_CR16_R3_REGNUM:
  668. case SIM_CR16_R4_REGNUM:
  669. case SIM_CR16_R5_REGNUM:
  670. case SIM_CR16_R6_REGNUM:
  671. case SIM_CR16_R7_REGNUM:
  672. case SIM_CR16_R8_REGNUM:
  673. case SIM_CR16_R9_REGNUM:
  674. case SIM_CR16_R10_REGNUM:
  675. case SIM_CR16_R11_REGNUM:
  676. SET_GPR (rn - SIM_CR16_R0_REGNUM, cr16_extract_unsigned_integer (memory, 2));
  677. size = 2;
  678. break;
  679. case SIM_CR16_R12_REGNUM:
  680. case SIM_CR16_R13_REGNUM:
  681. case SIM_CR16_R14_REGNUM:
  682. case SIM_CR16_R15_REGNUM:
  683. SET_GPR32 (rn - SIM_CR16_R0_REGNUM, cr16_extract_unsigned_integer (memory, 2));
  684. size = 4;
  685. break;
  686. case SIM_CR16_PC_REGNUM:
  687. case SIM_CR16_ISP_REGNUM:
  688. case SIM_CR16_USP_REGNUM:
  689. case SIM_CR16_INTBASE_REGNUM:
  690. case SIM_CR16_PSR_REGNUM:
  691. case SIM_CR16_CFG_REGNUM:
  692. case SIM_CR16_DBS_REGNUM:
  693. case SIM_CR16_DCR_REGNUM:
  694. case SIM_CR16_DSR_REGNUM:
  695. case SIM_CR16_CAR0_REGNUM:
  696. case SIM_CR16_CAR1_REGNUM:
  697. SET_CREG (rn - SIM_CR16_PC_REGNUM, cr16_extract_unsigned_integer (memory, 4));
  698. size = 4;
  699. break;
  700. default:
  701. size = 0;
  702. break;
  703. }
  704. SLOT_FLUSH ();
  705. return size;
  706. }