mloop.in 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. # Simulator main loop for frv. -*- C -*-
  2. # Copyright (C) 1998-2015 Free Software Foundation, Inc.
  3. # Contributed by Red Hat.
  4. #
  5. # This file is part of the GNU Simulators.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. # Syntax:
  20. # /bin/sh mainloop.in command
  21. #
  22. # Command is one of:
  23. #
  24. # init
  25. # support
  26. # extract-{simple,scache,pbb}
  27. # {full,fast}-exec-{simple,scache,pbb}
  28. #
  29. # A target need only provide a "full" version of one of simple,scache,pbb.
  30. # If the target wants it can also provide a fast version of same.
  31. # It can't provide more than this.
  32. # ??? After a few more ports are done, revisit.
  33. # Will eventually need to machine generate a lot of this.
  34. case "x$1" in
  35. xsupport)
  36. cat <<EOF
  37. static INLINE const IDESC *
  38. extract (SIM_CPU *current_cpu, PCADDR pc, CGEN_INSN_INT insn, ARGBUF *abuf,
  39. int fast_p)
  40. {
  41. const IDESC *id = @cpu@_decode (current_cpu, pc, insn, insn, abuf);
  42. @cpu@_fill_argbuf (current_cpu, abuf, id, pc, fast_p);
  43. if (! fast_p)
  44. {
  45. int trace_p = PC_IN_TRACE_RANGE_P (current_cpu, pc);
  46. int profile_p = PC_IN_PROFILE_RANGE_P (current_cpu, pc);
  47. @cpu@_fill_argbuf_tp (current_cpu, abuf, trace_p, profile_p);
  48. }
  49. return id;
  50. }
  51. static INLINE SEM_PC
  52. execute (SIM_CPU *current_cpu, SCACHE *sc, int fast_p)
  53. {
  54. SEM_PC vpc;
  55. /* Force gr0 to zero before every insn. */
  56. @cpu@_h_gr_set (current_cpu, 0, 0);
  57. if (fast_p)
  58. {
  59. vpc = (*sc->argbuf.semantic.sem_fast) (current_cpu, sc);
  60. }
  61. else
  62. {
  63. ARGBUF *abuf = &sc->argbuf;
  64. const IDESC *idesc = abuf->idesc;
  65. #if WITH_SCACHE_PBB
  66. int virtual_p = CGEN_ATTR_VALUE (NULL, idesc->attrs, CGEN_INSN_VIRTUAL);
  67. #else
  68. int virtual_p = 0;
  69. #endif
  70. if (! virtual_p)
  71. {
  72. /* FIXME: call x-before */
  73. if (ARGBUF_PROFILE_P (abuf))
  74. PROFILE_COUNT_INSN (current_cpu, abuf->addr, idesc->num);
  75. /* FIXME: Later make cover macros: PROFILE_INSN_{INIT,FINI}. */
  76. if (FRV_COUNT_CYCLES (current_cpu, ARGBUF_PROFILE_P (abuf)))
  77. {
  78. @cpu@_model_insn_before (current_cpu, sc->first_insn_p);
  79. model_insn = FRV_INSN_MODEL_PASS_1;
  80. if (idesc->timing->model_fn != NULL)
  81. (*idesc->timing->model_fn) (current_cpu, sc);
  82. }
  83. else
  84. model_insn = FRV_INSN_NO_MODELING;
  85. CGEN_TRACE_INSN_INIT (current_cpu, abuf, 1);
  86. CGEN_TRACE_INSN (current_cpu, idesc->idata,
  87. (const struct argbuf *) abuf, abuf->addr);
  88. }
  89. #if WITH_SCACHE
  90. vpc = (*sc->argbuf.semantic.sem_full) (current_cpu, sc);
  91. #else
  92. vpc = (*sc->argbuf.semantic.sem_full) (current_cpu, abuf);
  93. #endif
  94. if (! virtual_p)
  95. {
  96. /* FIXME: call x-after */
  97. if (FRV_COUNT_CYCLES (current_cpu, ARGBUF_PROFILE_P (abuf)))
  98. {
  99. int cycles;
  100. if (idesc->timing->model_fn != NULL)
  101. {
  102. model_insn = FRV_INSN_MODEL_PASS_2;
  103. cycles = (*idesc->timing->model_fn) (current_cpu, sc);
  104. }
  105. else
  106. cycles = 1;
  107. @cpu@_model_insn_after (current_cpu, sc->last_insn_p, cycles);
  108. }
  109. CGEN_TRACE_INSN_FINI (current_cpu, abuf, 1);
  110. }
  111. }
  112. return vpc;
  113. }
  114. static void
  115. @cpu@_parallel_write_init (SIM_CPU *current_cpu)
  116. {
  117. CGEN_WRITE_QUEUE *q = CPU_WRITE_QUEUE (current_cpu);
  118. CGEN_WRITE_QUEUE_CLEAR (q);
  119. previous_vliw_pc = CPU_PC_GET(current_cpu);
  120. frv_interrupt_state.f_ne_flags[0] = 0;
  121. frv_interrupt_state.f_ne_flags[1] = 0;
  122. frv_interrupt_state.imprecise_interrupt = NULL;
  123. }
  124. static void
  125. @cpu@_parallel_write_queued (SIM_CPU *current_cpu)
  126. {
  127. int i;
  128. FRV_VLIW *vliw = CPU_VLIW (current_cpu);
  129. CGEN_WRITE_QUEUE *q = CPU_WRITE_QUEUE (current_cpu);
  130. /* Loop over the queued writes, executing them. Set the pc to the address
  131. of the insn which queued each write for the proper context in case an
  132. interrupt is caused. Restore the proper pc after the writes are
  133. completed. */
  134. IADDR save_pc = CPU_PC_GET (current_cpu);
  135. IADDR new_pc = save_pc;
  136. int branch_taken = 0;
  137. int limit = CGEN_WRITE_QUEUE_INDEX (q);
  138. frv_interrupt_state.data_written.length = 0;
  139. for (i = 0; i < limit; ++i)
  140. {
  141. CGEN_WRITE_QUEUE_ELEMENT *item = CGEN_WRITE_QUEUE_ELEMENT (q, i);
  142. /* If an imprecise interrupt was generated, then, check whether the
  143. result should still be written. */
  144. if (frv_interrupt_state.imprecise_interrupt != NULL)
  145. {
  146. /* Only check writes by the insn causing the exception. */
  147. if (CGEN_WRITE_QUEUE_ELEMENT_IADDR (item)
  148. == frv_interrupt_state.imprecise_interrupt->vpc)
  149. {
  150. /* Execute writes of floating point operations resulting in
  151. overflow, underflow or inexact. */
  152. if (frv_interrupt_state.imprecise_interrupt->kind
  153. == FRV_FP_EXCEPTION)
  154. {
  155. if ((frv_interrupt_state.imprecise_interrupt
  156. ->u.fp_info.fsr_mask
  157. & ~(FSR_INEXACT | FSR_OVERFLOW | FSR_UNDERFLOW)))
  158. continue; /* Don't execute */
  159. }
  160. /* Execute writes marked as 'forced'. */
  161. else if (! (CGEN_WRITE_QUEUE_ELEMENT_FLAGS (item)
  162. & FRV_WRITE_QUEUE_FORCE_WRITE))
  163. continue; /* Don't execute */
  164. }
  165. }
  166. /* Only execute the first branch on the queue. */
  167. if (CGEN_WRITE_QUEUE_ELEMENT_KIND (item) == CGEN_PC_WRITE
  168. || CGEN_WRITE_QUEUE_ELEMENT_KIND (item) == CGEN_FN_PC_WRITE)
  169. {
  170. if (branch_taken)
  171. continue;
  172. branch_taken = 1;
  173. if (CGEN_WRITE_QUEUE_ELEMENT_KIND (item) == CGEN_PC_WRITE)
  174. new_pc = item->kinds.pc_write.value;
  175. else
  176. new_pc = item->kinds.fn_pc_write.value;
  177. }
  178. CPU_PC_SET (current_cpu, CGEN_WRITE_QUEUE_ELEMENT_IADDR (item));
  179. frv_save_data_written_for_interrupts (current_cpu, item);
  180. cgen_write_queue_element_execute (current_cpu, item);
  181. }
  182. /* Update the LR with the address of the next insn if the flag is set.
  183. This flag gets set in frvbf_set_write_next_vliw_to_LR by the JMPL,
  184. JMPIL and CALL insns. */
  185. if (frvbf_write_next_vliw_addr_to_LR)
  186. {
  187. frvbf_h_spr_set_handler (current_cpu, H_SPR_LR, save_pc);
  188. frvbf_write_next_vliw_addr_to_LR = 0;
  189. }
  190. CPU_PC_SET (current_cpu, new_pc);
  191. CGEN_WRITE_QUEUE_CLEAR (q);
  192. }
  193. void
  194. @cpu@_perform_writeback (SIM_CPU *current_cpu)
  195. {
  196. @cpu@_parallel_write_queued (current_cpu);
  197. }
  198. static unsigned cache_reqno = 0x80000000; /* Start value is for debugging. */
  199. #if 0 /* experimental */
  200. /* FR400 has single prefetch. */
  201. static void
  202. fr400_simulate_insn_prefetch (SIM_CPU *current_cpu, IADDR vpc)
  203. {
  204. int cur_ix;
  205. FRV_CACHE *cache;
  206. /* The cpu receives 8 bytes worth of insn data for each fetch aligned
  207. on 8 byte boundary. */
  208. #define FR400_FETCH_SIZE 8
  209. cur_ix = LS;
  210. vpc &= ~(FR400_FETCH_SIZE - 1);
  211. cache = CPU_INSN_CACHE (current_cpu);
  212. /* Request a load of the current address buffer, if necessary. */
  213. if (frv_insn_fetch_buffer[cur_ix].address != vpc)
  214. {
  215. frv_insn_fetch_buffer[cur_ix].address = vpc;
  216. frv_insn_fetch_buffer[cur_ix].reqno = cache_reqno++;
  217. if (FRV_COUNT_CYCLES (current_cpu, 1))
  218. frv_cache_request_load (cache, frv_insn_fetch_buffer[cur_ix].reqno,
  219. frv_insn_fetch_buffer[cur_ix].address,
  220. UNIT_I0 + cur_ix);
  221. }
  222. /* Wait for the current address buffer to be loaded, if necessary. */
  223. if (FRV_COUNT_CYCLES (current_cpu, 1))
  224. {
  225. FRV_PROFILE_STATE *ps = CPU_PROFILE_STATE (current_cpu);
  226. int wait;
  227. /* Account for any branch penalty. */
  228. if (ps->branch_penalty > 0 && ! ps->past_first_p)
  229. {
  230. frv_model_advance_cycles (current_cpu, ps->branch_penalty);
  231. frv_model_trace_wait_cycles (current_cpu, ps->branch_penalty,
  232. "Branch penalty:");
  233. ps->branch_penalty = 0;
  234. }
  235. /* Account for insn fetch latency. */
  236. wait = 0;
  237. while (frv_insn_fetch_buffer[cur_ix].reqno != NO_REQNO)
  238. {
  239. frv_model_advance_cycles (current_cpu, 1);
  240. ++wait;
  241. }
  242. frv_model_trace_wait_cycles (current_cpu, wait, "Insn fetch:");
  243. return;
  244. }
  245. /* Otherwise just load the insns directly from the cache.
  246. */
  247. if (frv_insn_fetch_buffer[cur_ix].reqno != NO_REQNO)
  248. {
  249. frv_cache_read (cache, cur_ix, vpc);
  250. frv_insn_fetch_buffer[cur_ix].reqno = NO_REQNO;
  251. }
  252. }
  253. #endif /* experimental */
  254. /* FR500 has dual prefetch. */
  255. static void
  256. simulate_dual_insn_prefetch (SIM_CPU *current_cpu, IADDR vpc, int fetch_size)
  257. {
  258. int i;
  259. int cur_ix, pre_ix;
  260. SI pre_address;
  261. FRV_CACHE *cache;
  262. /* See if the pc is within the addresses specified by either of the
  263. fetch buffers. If so, that will be the current buffer. Otherwise,
  264. arbitrarily select the LD buffer as the current one since it gets
  265. priority in the case of interfering load requests. */
  266. cur_ix = LD;
  267. vpc &= ~(fetch_size - 1);
  268. for (i = LS; i < FRV_CACHE_PIPELINES; ++i)
  269. {
  270. if (frv_insn_fetch_buffer[i].address == vpc)
  271. {
  272. cur_ix = i;
  273. break;
  274. }
  275. }
  276. cache = CPU_INSN_CACHE (current_cpu);
  277. /* Request a load of the current address buffer, if necessary. */
  278. if (frv_insn_fetch_buffer[cur_ix].address != vpc)
  279. {
  280. frv_insn_fetch_buffer[cur_ix].address = vpc;
  281. frv_insn_fetch_buffer[cur_ix].reqno = cache_reqno++;
  282. if (FRV_COUNT_CYCLES (current_cpu, 1))
  283. frv_cache_request_load (cache, frv_insn_fetch_buffer[cur_ix].reqno,
  284. frv_insn_fetch_buffer[cur_ix].address,
  285. UNIT_I0 + cur_ix);
  286. }
  287. /* If the prefetch buffer does not represent the next sequential address, then
  288. request a load of the next sequential address. */
  289. pre_ix = (cur_ix + 1) % FRV_CACHE_PIPELINES;
  290. pre_address = vpc + fetch_size;
  291. if (frv_insn_fetch_buffer[pre_ix].address != pre_address)
  292. {
  293. frv_insn_fetch_buffer[pre_ix].address = pre_address;
  294. frv_insn_fetch_buffer[pre_ix].reqno = cache_reqno++;
  295. if (FRV_COUNT_CYCLES (current_cpu, 1))
  296. frv_cache_request_load (cache, frv_insn_fetch_buffer[pre_ix].reqno,
  297. frv_insn_fetch_buffer[pre_ix].address,
  298. UNIT_I0 + pre_ix);
  299. }
  300. /* If counting cycles, account for any branch penalty and/or insn fetch
  301. latency here. */
  302. if (FRV_COUNT_CYCLES (current_cpu, 1))
  303. {
  304. FRV_PROFILE_STATE *ps = CPU_PROFILE_STATE (current_cpu);
  305. int wait;
  306. /* Account for any branch penalty. */
  307. if (ps->branch_penalty > 0 && ! ps->past_first_p)
  308. {
  309. frv_model_advance_cycles (current_cpu, ps->branch_penalty);
  310. frv_model_trace_wait_cycles (current_cpu, ps->branch_penalty,
  311. "Branch penalty:");
  312. ps->branch_penalty = 0;
  313. }
  314. /* Account for insn fetch latency. */
  315. wait = 0;
  316. while (frv_insn_fetch_buffer[cur_ix].reqno != NO_REQNO)
  317. {
  318. frv_model_advance_cycles (current_cpu, 1);
  319. ++wait;
  320. }
  321. frv_model_trace_wait_cycles (current_cpu, wait, "Insn fetch:");
  322. return;
  323. }
  324. /* Otherwise just load the insns directly from the cache.
  325. */
  326. if (frv_insn_fetch_buffer[cur_ix].reqno != NO_REQNO)
  327. {
  328. frv_cache_read (cache, cur_ix, vpc);
  329. frv_insn_fetch_buffer[cur_ix].reqno = NO_REQNO;
  330. }
  331. if (frv_insn_fetch_buffer[pre_ix].reqno != NO_REQNO)
  332. {
  333. frv_cache_read (cache, pre_ix, pre_address);
  334. frv_insn_fetch_buffer[pre_ix].reqno = NO_REQNO;
  335. }
  336. }
  337. static void
  338. @cpu@_simulate_insn_prefetch (SIM_CPU *current_cpu, IADDR vpc)
  339. {
  340. SI hsr0;
  341. SIM_DESC sd;
  342. /* Nothing to do if not counting cycles and the cache is not enabled. */
  343. hsr0 = GET_HSR0 ();
  344. if (! GET_HSR0_ICE (hsr0) && ! FRV_COUNT_CYCLES (current_cpu, 1))
  345. return;
  346. /* Different machines handle prefetch defferently. */
  347. sd = CPU_STATE (current_cpu);
  348. switch (STATE_ARCHITECTURE (sd)->mach)
  349. {
  350. case bfd_mach_fr400:
  351. case bfd_mach_fr450:
  352. simulate_dual_insn_prefetch (current_cpu, vpc, 8);
  353. break;
  354. case bfd_mach_frvtomcat:
  355. case bfd_mach_fr500:
  356. case bfd_mach_fr550:
  357. case bfd_mach_frv:
  358. simulate_dual_insn_prefetch (current_cpu, vpc, 16);
  359. break;
  360. default:
  361. break;
  362. }
  363. }
  364. int frv_save_profile_model_p;
  365. EOF
  366. ;;
  367. xinit)
  368. cat <<EOF
  369. /*xxxinit*/
  370. /* If the timer is enabled, then we will enable model profiling during
  371. execution. This is because the timer needs accurate cycles counts to
  372. work properly. Save the original setting of model profiling. */
  373. if (frv_interrupt_state.timer.enabled)
  374. frv_save_profile_model_p = PROFILE_MODEL_P (current_cpu);
  375. EOF
  376. ;;
  377. xextract-simple | xextract-scache)
  378. # Inputs: current_cpu, vpc, sc, FAST_P
  379. # Outputs: sc filled in
  380. # SET_LAST_INSN_P(last_p) called to indicate whether insn is last one
  381. cat <<EOF
  382. {
  383. CGEN_INSN_INT insn = frvbf_read_imem_USI (current_cpu, vpc);
  384. extract (current_cpu, vpc, insn, SEM_ARGBUF (sc), FAST_P);
  385. SET_LAST_INSN_P ((insn & 0x80000000) != 0);
  386. }
  387. EOF
  388. ;;
  389. xfull-exec-* | xfast-exec-*)
  390. # Inputs: current_cpu, vpc, FAST_P
  391. # Outputs:
  392. # vpc contains the address of the next insn to execute
  393. # pc of current_cpu must be up to date (=vpc) upon exit
  394. # CPU_INSN_COUNT (current_cpu) must be updated by number of insns executed
  395. #
  396. # Unlike the non-parallel case, this version is responsible for doing the
  397. # scache lookup.
  398. cat <<EOF
  399. {
  400. FRV_VLIW *vliw;
  401. int first_insn_p = 1;
  402. int last_insn_p = 0;
  403. int ninsns;
  404. CGEN_ATTR_VALUE_ENUM_TYPE slot;
  405. /* If the timer is enabled, then enable model profiling. This is because
  406. the timer needs accurate cycles counts to work properly. */
  407. if (frv_interrupt_state.timer.enabled && ! frv_save_profile_model_p)
  408. sim_profile_set_option (current_state, "-model", PROFILE_MODEL_IDX, "1");
  409. /* Init parallel-write queue and vliw. */
  410. @cpu@_parallel_write_init (current_cpu);
  411. vliw = CPU_VLIW (current_cpu);
  412. frv_vliw_reset (vliw, STATE_ARCHITECTURE (CPU_STATE (current_cpu))->mach,
  413. CPU_ELF_FLAGS (current_cpu));
  414. frv_current_fm_slot = UNIT_NIL;
  415. for (ninsns = 0; ! last_insn_p && ninsns < FRV_VLIW_SIZE; ++ninsns)
  416. {
  417. SCACHE *sc;
  418. const CGEN_INSN *insn;
  419. int error;
  420. /* Go through the motions of finding the insns in the cache. */
  421. @cpu@_simulate_insn_prefetch (current_cpu, vpc);
  422. sc = @cpu@_scache_lookup (current_cpu, vpc, scache, hash_mask, FAST_P);
  423. sc->first_insn_p = first_insn_p;
  424. last_insn_p = sc->last_insn_p;
  425. /* Add the insn to the vliw and set up the interrupt state. */
  426. insn = sc->argbuf.idesc->idata;
  427. error = frv_vliw_add_insn (vliw, insn);
  428. if (! error)
  429. frv_vliw_setup_insn (current_cpu, insn);
  430. frv_detect_insn_access_interrupts (current_cpu, sc);
  431. slot = (*vliw->current_vliw)[vliw->next_slot - 1];
  432. if (slot >= UNIT_FM0 && slot <= UNIT_FM3)
  433. frv_current_fm_slot = slot;
  434. vpc = execute (current_cpu, sc, FAST_P);
  435. SET_H_PC (vpc); /* needed for interrupt handling */
  436. first_insn_p = 0;
  437. }
  438. /* If the timer is enabled, and model profiling was not originally enabled,
  439. then turn it off again. This is the only place we can currently gain
  440. control to do this. */
  441. if (frv_interrupt_state.timer.enabled && ! frv_save_profile_model_p)
  442. sim_profile_set_option (current_state, "-model", PROFILE_MODEL_IDX, "0");
  443. /* Check for interrupts. Also handles writeback if necessary. */
  444. frv_process_interrupts (current_cpu);
  445. CPU_INSN_COUNT (current_cpu) += ninsns;
  446. }
  447. EOF
  448. ;;
  449. *)
  450. echo "Invalid argument to mainloop.in: $1" >&2
  451. exit 1
  452. ;;
  453. esac