m32r2.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* m32r2 simulator support code
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support.
  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 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. #define WANT_CPU m32r2f
  18. #define WANT_CPU_M32R2F
  19. #include "sim-main.h"
  20. #include "cgen-mem.h"
  21. #include "cgen-ops.h"
  22. /* The contents of BUF are in target byte order. */
  23. int
  24. m32r2f_fetch_register (SIM_CPU *current_cpu, int rn, unsigned char *buf, int len)
  25. {
  26. return m32rbf_fetch_register (current_cpu, rn, buf, len);
  27. }
  28. /* The contents of BUF are in target byte order. */
  29. int
  30. m32r2f_store_register (SIM_CPU *current_cpu, int rn, unsigned char *buf, int len)
  31. {
  32. return m32rbf_store_register (current_cpu, rn, buf, len);
  33. }
  34. /* Cover fns to get/set the control registers.
  35. FIXME: Duplicated from m32r.c. The issue is structure offsets. */
  36. USI
  37. m32r2f_h_cr_get_handler (SIM_CPU *current_cpu, UINT cr)
  38. {
  39. switch (cr)
  40. {
  41. case H_CR_PSW : /* PSW. */
  42. return (((CPU (h_bpsw) & 0xc1) << 8)
  43. | ((CPU (h_psw) & 0xc0) << 0)
  44. | GET_H_COND ());
  45. case H_CR_BBPSW : /* Backup backup psw. */
  46. return CPU (h_bbpsw) & 0xc1;
  47. case H_CR_CBR : /* Condition bit. */
  48. return GET_H_COND ();
  49. case H_CR_SPI : /* Interrupt stack pointer. */
  50. if (! GET_H_SM ())
  51. return CPU (h_gr[H_GR_SP]);
  52. else
  53. return CPU (h_cr[H_CR_SPI]);
  54. case H_CR_SPU : /* User stack pointer. */
  55. if (GET_H_SM ())
  56. return CPU (h_gr[H_GR_SP]);
  57. else
  58. return CPU (h_cr[H_CR_SPU]);
  59. case H_CR_BPC : /* Backup pc. */
  60. return CPU (h_cr[H_CR_BPC]) & 0xfffffffe;
  61. case H_CR_BBPC : /* Backup backup pc. */
  62. return CPU (h_cr[H_CR_BBPC]) & 0xfffffffe;
  63. case 4 : /* ??? unspecified, but apparently available */
  64. case 5 : /* ??? unspecified, but apparently available */
  65. return CPU (h_cr[cr]);
  66. default :
  67. return 0;
  68. }
  69. }
  70. void
  71. m32r2f_h_cr_set_handler (SIM_CPU *current_cpu, UINT cr, USI newval)
  72. {
  73. switch (cr)
  74. {
  75. case H_CR_PSW : /* psw */
  76. {
  77. int old_sm = (CPU (h_psw) & 0x80) != 0;
  78. int new_sm = (newval & 0x80) != 0;
  79. CPU (h_bpsw) = (newval >> 8) & 0xff;
  80. CPU (h_psw) = newval & 0xff;
  81. SET_H_COND (newval & 1);
  82. /* When switching stack modes, update the registers. */
  83. if (old_sm != new_sm)
  84. {
  85. if (old_sm)
  86. {
  87. /* Switching user -> system. */
  88. CPU (h_cr[H_CR_SPU]) = CPU (h_gr[H_GR_SP]);
  89. CPU (h_gr[H_GR_SP]) = CPU (h_cr[H_CR_SPI]);
  90. }
  91. else
  92. {
  93. /* Switching system -> user. */
  94. CPU (h_cr[H_CR_SPI]) = CPU (h_gr[H_GR_SP]);
  95. CPU (h_gr[H_GR_SP]) = CPU (h_cr[H_CR_SPU]);
  96. }
  97. }
  98. break;
  99. }
  100. case H_CR_BBPSW : /* backup backup psw */
  101. CPU (h_bbpsw) = newval & 0xff;
  102. break;
  103. case H_CR_CBR : /* condition bit */
  104. SET_H_COND (newval & 1);
  105. break;
  106. case H_CR_SPI : /* interrupt stack pointer */
  107. if (! GET_H_SM ())
  108. CPU (h_gr[H_GR_SP]) = newval;
  109. else
  110. CPU (h_cr[H_CR_SPI]) = newval;
  111. break;
  112. case H_CR_SPU : /* user stack pointer */
  113. if (GET_H_SM ())
  114. CPU (h_gr[H_GR_SP]) = newval;
  115. else
  116. CPU (h_cr[H_CR_SPU]) = newval;
  117. break;
  118. case H_CR_BPC : /* backup pc */
  119. CPU (h_cr[H_CR_BPC]) = newval;
  120. break;
  121. case H_CR_BBPC : /* backup backup pc */
  122. CPU (h_cr[H_CR_BBPC]) = newval;
  123. break;
  124. case 4 : /* ??? unspecified, but apparently available */
  125. case 5 : /* ??? unspecified, but apparently available */
  126. CPU (h_cr[cr]) = newval;
  127. break;
  128. default :
  129. /* ignore */
  130. break;
  131. }
  132. }
  133. /* Cover fns to access h-psw. */
  134. UQI
  135. m32r2f_h_psw_get_handler (SIM_CPU *current_cpu)
  136. {
  137. return (CPU (h_psw) & 0xfe) | (CPU (h_cond) & 1);
  138. }
  139. void
  140. m32r2f_h_psw_set_handler (SIM_CPU *current_cpu, UQI newval)
  141. {
  142. CPU (h_psw) = newval;
  143. CPU (h_cond) = newval & 1;
  144. }
  145. /* Cover fns to access h-accum. */
  146. DI
  147. m32r2f_h_accum_get_handler (SIM_CPU *current_cpu)
  148. {
  149. /* Sign extend the top 8 bits. */
  150. DI r;
  151. r = ANDDI (CPU (h_accum), MAKEDI (0xffffff, 0xffffffff));
  152. r = XORDI (r, MAKEDI (0x800000, 0));
  153. r = SUBDI (r, MAKEDI (0x800000, 0));
  154. return r;
  155. }
  156. void
  157. m32r2f_h_accum_set_handler (SIM_CPU *current_cpu, DI newval)
  158. {
  159. CPU (h_accum) = newval;
  160. }
  161. /* Cover fns to access h-accums. */
  162. DI
  163. m32r2f_h_accums_get_handler (SIM_CPU *current_cpu, UINT regno)
  164. {
  165. /* FIXME: Yes, this is just a quick hack. */
  166. DI r;
  167. if (regno == 0)
  168. r = CPU (h_accum);
  169. else
  170. r = CPU (h_accums[1]);
  171. /* Sign extend the top 8 bits. */
  172. r = ANDDI (r, MAKEDI (0xffffff, 0xffffffff));
  173. r = XORDI (r, MAKEDI (0x800000, 0));
  174. r = SUBDI (r, MAKEDI (0x800000, 0));
  175. return r;
  176. }
  177. void
  178. m32r2f_h_accums_set_handler (SIM_CPU *current_cpu, UINT regno, DI newval)
  179. {
  180. /* FIXME: Yes, this is just a quick hack. */
  181. if (regno == 0)
  182. CPU (h_accum) = newval;
  183. else
  184. CPU (h_accums[1]) = newval;
  185. }
  186. #if WITH_PROFILE_MODEL_P
  187. /* Initialize cycle counting for an insn.
  188. FIRST_P is non-zero if this is the first insn in a set of parallel
  189. insns. */
  190. void
  191. m32r2f_model_insn_before (SIM_CPU *cpu, int first_p)
  192. {
  193. m32rbf_model_insn_before (cpu, first_p);
  194. }
  195. /* Record the cycles computed for an insn.
  196. LAST_P is non-zero if this is the last insn in a set of parallel insns,
  197. and we update the total cycle count.
  198. CYCLES is the cycle count of the insn. */
  199. void
  200. m32r2f_model_insn_after (SIM_CPU *cpu, int last_p, int cycles)
  201. {
  202. m32rbf_model_insn_after (cpu, last_p, cycles);
  203. }
  204. static INLINE void
  205. check_load_stall (SIM_CPU *cpu, int regno)
  206. {
  207. UINT h_gr = CPU_M32R_MISC_PROFILE (cpu)->load_regs;
  208. if (regno != -1
  209. && (h_gr & (1 << regno)) != 0)
  210. {
  211. CPU_M32R_MISC_PROFILE (cpu)->load_stall += 2;
  212. if (TRACE_INSN_P (cpu))
  213. cgen_trace_printf (cpu, " ; Load stall of 2 cycles.");
  214. }
  215. }
  216. int
  217. m32r2f_model_m32r2_u_exec (SIM_CPU *cpu, const IDESC *idesc,
  218. int unit_num, int referenced,
  219. INT sr, INT sr2, INT dr)
  220. {
  221. check_load_stall (cpu, sr);
  222. check_load_stall (cpu, sr2);
  223. return idesc->timing->units[unit_num].done;
  224. }
  225. int
  226. m32r2f_model_m32r2_u_cmp (SIM_CPU *cpu, const IDESC *idesc,
  227. int unit_num, int referenced,
  228. INT src1, INT src2)
  229. {
  230. check_load_stall (cpu, src1);
  231. check_load_stall (cpu, src2);
  232. return idesc->timing->units[unit_num].done;
  233. }
  234. int
  235. m32r2f_model_m32r2_u_mac (SIM_CPU *cpu, const IDESC *idesc,
  236. int unit_num, int referenced,
  237. INT src1, INT src2)
  238. {
  239. check_load_stall (cpu, src1);
  240. check_load_stall (cpu, src2);
  241. return idesc->timing->units[unit_num].done;
  242. }
  243. int
  244. m32r2f_model_m32r2_u_cti (SIM_CPU *cpu, const IDESC *idesc,
  245. int unit_num, int referenced,
  246. INT sr)
  247. {
  248. PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
  249. int taken_p = (referenced & (1 << 1)) != 0;
  250. check_load_stall (cpu, sr);
  251. if (taken_p)
  252. {
  253. CPU_M32R_MISC_PROFILE (cpu)->cti_stall += 2;
  254. PROFILE_MODEL_TAKEN_COUNT (profile) += 1;
  255. }
  256. else
  257. PROFILE_MODEL_UNTAKEN_COUNT (profile) += 1;
  258. return idesc->timing->units[unit_num].done;
  259. }
  260. int
  261. m32r2f_model_m32r2_u_load (SIM_CPU *cpu, const IDESC *idesc,
  262. int unit_num, int referenced,
  263. INT sr, INT dr)
  264. {
  265. CPU_M32R_MISC_PROFILE (cpu)->load_regs_pending |= (1 << dr);
  266. return idesc->timing->units[unit_num].done;
  267. }
  268. int
  269. m32r2f_model_m32r2_u_store (SIM_CPU *cpu, const IDESC *idesc,
  270. int unit_num, int referenced,
  271. INT src1, INT src2)
  272. {
  273. return idesc->timing->units[unit_num].done;
  274. }
  275. #endif /* WITH_PROFILE_MODEL_P */