interp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "sim-main.h"
  2. #include "sim-options.h"
  3. #include "v850_sim.h"
  4. #include "sim-assert.h"
  5. #include "itable.h"
  6. #ifdef HAVE_STDLIB_H
  7. #include <stdlib.h>
  8. #endif
  9. #ifdef HAVE_STRING_H
  10. #include <string.h>
  11. #else
  12. #ifdef HAVE_STRINGS_H
  13. #include <strings.h>
  14. #endif
  15. #endif
  16. #include "bfd.h"
  17. static const char * get_insn_name (sim_cpu *, int);
  18. /* For compatibility. */
  19. SIM_DESC simulator;
  20. /* V850 interrupt model. */
  21. enum interrupt_type
  22. {
  23. int_reset,
  24. int_nmi,
  25. int_intov1,
  26. int_intp10,
  27. int_intp11,
  28. int_intp12,
  29. int_intp13,
  30. int_intcm4,
  31. num_int_types
  32. };
  33. const char *interrupt_names[] =
  34. {
  35. "reset",
  36. "nmi",
  37. "intov1",
  38. "intp10",
  39. "intp11",
  40. "intp12",
  41. "intp13",
  42. "intcm4",
  43. NULL
  44. };
  45. static void
  46. do_interrupt (SIM_DESC sd, void *data)
  47. {
  48. const char **interrupt_name = (const char**)data;
  49. enum interrupt_type inttype;
  50. inttype = (interrupt_name - STATE_WATCHPOINTS (sd)->interrupt_names);
  51. /* For a hardware reset, drop everything and jump to the start
  52. address */
  53. if (inttype == int_reset)
  54. {
  55. PC = 0;
  56. PSW = 0x20;
  57. ECR = 0;
  58. sim_engine_restart (sd, NULL, NULL, NULL_CIA);
  59. }
  60. /* Deliver an NMI when allowed */
  61. if (inttype == int_nmi)
  62. {
  63. if (PSW & PSW_NP)
  64. {
  65. /* We're already working on an NMI, so this one must wait
  66. around until the previous one is done. The processor
  67. ignores subsequent NMIs, so we don't need to count them.
  68. Just keep re-scheduling a single NMI until it manages to
  69. be delivered */
  70. if (STATE_CPU (sd, 0)->pending_nmi != NULL)
  71. sim_events_deschedule (sd, STATE_CPU (sd, 0)->pending_nmi);
  72. STATE_CPU (sd, 0)->pending_nmi =
  73. sim_events_schedule (sd, 1, do_interrupt, data);
  74. return;
  75. }
  76. else
  77. {
  78. /* NMI can be delivered. Do not deschedule pending_nmi as
  79. that, if still in the event queue, is a second NMI that
  80. needs to be delivered later. */
  81. FEPC = PC;
  82. FEPSW = PSW;
  83. /* Set the FECC part of the ECR. */
  84. ECR &= 0x0000ffff;
  85. ECR |= 0x10;
  86. PSW |= PSW_NP;
  87. PSW &= ~PSW_EP;
  88. PSW |= PSW_ID;
  89. PC = 0x10;
  90. sim_engine_restart (sd, NULL, NULL, NULL_CIA);
  91. }
  92. }
  93. /* deliver maskable interrupt when allowed */
  94. if (inttype > int_nmi && inttype < num_int_types)
  95. {
  96. if ((PSW & PSW_NP) || (PSW & PSW_ID))
  97. {
  98. /* Can't deliver this interrupt, reschedule it for later */
  99. sim_events_schedule (sd, 1, do_interrupt, data);
  100. return;
  101. }
  102. else
  103. {
  104. /* save context */
  105. EIPC = PC;
  106. EIPSW = PSW;
  107. /* Disable further interrupts. */
  108. PSW |= PSW_ID;
  109. /* Indicate that we're doing interrupt not exception processing. */
  110. PSW &= ~PSW_EP;
  111. /* Clear the EICC part of the ECR, will set below. */
  112. ECR &= 0xffff0000;
  113. switch (inttype)
  114. {
  115. case int_intov1:
  116. PC = 0x80;
  117. ECR |= 0x80;
  118. break;
  119. case int_intp10:
  120. PC = 0x90;
  121. ECR |= 0x90;
  122. break;
  123. case int_intp11:
  124. PC = 0xa0;
  125. ECR |= 0xa0;
  126. break;
  127. case int_intp12:
  128. PC = 0xb0;
  129. ECR |= 0xb0;
  130. break;
  131. case int_intp13:
  132. PC = 0xc0;
  133. ECR |= 0xc0;
  134. break;
  135. case int_intcm4:
  136. PC = 0xd0;
  137. ECR |= 0xd0;
  138. break;
  139. default:
  140. /* Should never be possible. */
  141. sim_engine_abort (sd, NULL, NULL_CIA,
  142. "do_interrupt - internal error - bad switch");
  143. break;
  144. }
  145. }
  146. sim_engine_restart (sd, NULL, NULL, NULL_CIA);
  147. }
  148. /* some other interrupt? */
  149. sim_engine_abort (sd, NULL, NULL_CIA,
  150. "do_interrupt - internal error - interrupt %d unknown",
  151. inttype);
  152. }
  153. /* Return name of an insn, used by insn profiling. */
  154. static const char *
  155. get_insn_name (sim_cpu *cpu, int i)
  156. {
  157. return itable[i].name;
  158. }
  159. /* These default values correspond to expected usage for the chip. */
  160. uint32 OP[4];
  161. static sim_cia
  162. v850_pc_get (sim_cpu *cpu)
  163. {
  164. return PC;
  165. }
  166. static void
  167. v850_pc_set (sim_cpu *cpu, sim_cia pc)
  168. {
  169. PC = pc;
  170. }
  171. SIM_DESC
  172. sim_open (SIM_OPEN_KIND kind,
  173. host_callback * cb,
  174. struct bfd * abfd,
  175. char ** argv)
  176. {
  177. int i;
  178. SIM_DESC sd = sim_state_alloc (kind, cb);
  179. int mach;
  180. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  181. /* The cpu data is kept in a separately allocated chunk of memory. */
  182. if (sim_cpu_alloc_all (sd, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK)
  183. return 0;
  184. /* for compatibility */
  185. simulator = sd;
  186. /* FIXME: should be better way of setting up interrupts */
  187. STATE_WATCHPOINTS (sd)->pc = &(PC);
  188. STATE_WATCHPOINTS (sd)->sizeof_pc = sizeof (PC);
  189. STATE_WATCHPOINTS (sd)->interrupt_handler = do_interrupt;
  190. STATE_WATCHPOINTS (sd)->interrupt_names = interrupt_names;
  191. /* Initialize the mechanism for doing insn profiling. */
  192. CPU_INSN_NAME (STATE_CPU (sd, 0)) = get_insn_name;
  193. CPU_MAX_INSNS (STATE_CPU (sd, 0)) = nr_itable_entries;
  194. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  195. return 0;
  196. /* Allocate core managed memory */
  197. /* "Mirror" the ROM addresses below 1MB. */
  198. sim_do_commandf (sd, "memory region 0,0x100000,0x%lx", V850_ROM_SIZE);
  199. /* Chunk of ram adjacent to rom */
  200. sim_do_commandf (sd, "memory region 0x100000,0x%lx", V850_LOW_END-0x100000);
  201. /* peripheral I/O region - mirror 1K across 4k (0x1000) */
  202. sim_do_command (sd, "memory region 0xfff000,0x1000,1024");
  203. /* similarly if in the internal RAM region */
  204. sim_do_command (sd, "memory region 0xffe000,0x1000,1024");
  205. /* getopt will print the error message so we just have to exit if this fails.
  206. FIXME: Hmmm... in the case of gdb we need getopt to call
  207. print_filtered. */
  208. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  209. {
  210. /* Uninstall the modules to avoid memory leaks,
  211. file descriptor leaks, etc. */
  212. sim_module_uninstall (sd);
  213. return 0;
  214. }
  215. /* check for/establish the a reference program image */
  216. if (sim_analyze_program (sd,
  217. (STATE_PROG_ARGV (sd) != NULL
  218. ? *STATE_PROG_ARGV (sd)
  219. : NULL),
  220. abfd) != SIM_RC_OK)
  221. {
  222. sim_module_uninstall (sd);
  223. return 0;
  224. }
  225. /* establish any remaining configuration options */
  226. if (sim_config (sd) != SIM_RC_OK)
  227. {
  228. sim_module_uninstall (sd);
  229. return 0;
  230. }
  231. if (sim_post_argv_init (sd) != SIM_RC_OK)
  232. {
  233. /* Uninstall the modules to avoid memory leaks,
  234. file descriptor leaks, etc. */
  235. sim_module_uninstall (sd);
  236. return 0;
  237. }
  238. /* determine the machine type */
  239. if (STATE_ARCHITECTURE (sd) != NULL
  240. && (STATE_ARCHITECTURE (sd)->arch == bfd_arch_v850
  241. || STATE_ARCHITECTURE (sd)->arch == bfd_arch_v850_rh850))
  242. mach = STATE_ARCHITECTURE (sd)->mach;
  243. else
  244. mach = bfd_mach_v850; /* default */
  245. /* set machine specific configuration */
  246. switch (mach)
  247. {
  248. case bfd_mach_v850:
  249. case bfd_mach_v850e:
  250. case bfd_mach_v850e1:
  251. case bfd_mach_v850e2:
  252. case bfd_mach_v850e2v3:
  253. case bfd_mach_v850e3v5:
  254. STATE_CPU (sd, 0)->psw_mask = (PSW_NP | PSW_EP | PSW_ID | PSW_SAT
  255. | PSW_CY | PSW_OV | PSW_S | PSW_Z);
  256. break;
  257. }
  258. /* CPU specific initialization. */
  259. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  260. {
  261. SIM_CPU *cpu = STATE_CPU (sd, i);
  262. CPU_PC_FETCH (cpu) = v850_pc_get;
  263. CPU_PC_STORE (cpu) = v850_pc_set;
  264. }
  265. return sd;
  266. }
  267. void
  268. sim_close (SIM_DESC sd, int quitting)
  269. {
  270. sim_module_uninstall (sd);
  271. }
  272. SIM_RC
  273. sim_create_inferior (SIM_DESC sd,
  274. struct bfd * prog_bfd,
  275. char ** argv,
  276. char ** env)
  277. {
  278. memset (&State, 0, sizeof (State));
  279. if (prog_bfd != NULL)
  280. PC = bfd_get_start_address (prog_bfd);
  281. return SIM_RC_OK;
  282. }
  283. int
  284. sim_fetch_register (SIM_DESC sd,
  285. int rn,
  286. unsigned char * memory,
  287. int length)
  288. {
  289. *(unsigned32*)memory = H2T_4 (State.regs[rn]);
  290. return -1;
  291. }
  292. int
  293. sim_store_register (SIM_DESC sd,
  294. int rn,
  295. unsigned char * memory,
  296. int length)
  297. {
  298. State.regs[rn] = T2H_4 (*(unsigned32 *) memory);
  299. return length;
  300. }