or1k.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* OpenRISC simulator support code
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GDB, the GNU debugger.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* This must come before any other includes. */
  15. #include "defs.h"
  16. #define WANT_CPU_OR1K32BF
  17. #define WANT_CPU
  18. #include "sim-main.h"
  19. #include "symcat.h"
  20. #include "cgen-ops.h"
  21. #include "cgen-mem.h"
  22. #include "cpuall.h"
  23. #include <string.h>
  24. int
  25. or1k32bf_fetch_register (sim_cpu *current_cpu, int rn, unsigned char *buf,
  26. int len)
  27. {
  28. if (rn < 32)
  29. SETTWI (buf, GET_H_GPR (rn));
  30. else
  31. switch (rn)
  32. {
  33. case PPC_REGNUM:
  34. SETTWI (buf, GET_H_SYS_PPC ());
  35. break;
  36. case PC_REGNUM:
  37. SETTWI (buf, GET_H_PC ());
  38. break;
  39. case SR_REGNUM:
  40. SETTWI (buf, GET_H_SYS_SR ());
  41. break;
  42. default:
  43. return 0;
  44. }
  45. return sizeof (WI); /* WI from arch.h */
  46. }
  47. int
  48. or1k32bf_store_register (sim_cpu *current_cpu, int rn, unsigned char *buf,
  49. int len)
  50. {
  51. if (rn < 32)
  52. SET_H_GPR (rn, GETTWI (buf));
  53. else
  54. switch (rn)
  55. {
  56. case PPC_REGNUM:
  57. SET_H_SYS_PPC (GETTWI (buf));
  58. break;
  59. case PC_REGNUM:
  60. SET_H_PC (GETTWI (buf));
  61. break;
  62. case SR_REGNUM:
  63. SET_H_SYS_SR (GETTWI (buf));
  64. break;
  65. default:
  66. return 0;
  67. }
  68. return sizeof (WI); /* WI from arch.h */
  69. }
  70. int
  71. or1k32bf_model_or1200_u_exec (sim_cpu *current_cpu, const IDESC *idesc,
  72. int unit_num, int referenced)
  73. {
  74. return -1;
  75. }
  76. int
  77. or1k32bf_model_or1200nd_u_exec (sim_cpu *current_cpu, const IDESC *idesc,
  78. int unit_num, int referenced)
  79. {
  80. return -1;
  81. }
  82. void
  83. or1k32bf_model_insn_before (sim_cpu *current_cpu, int first_p)
  84. {
  85. }
  86. void
  87. or1k32bf_model_insn_after (sim_cpu *current_cpu, int last_p, int cycles)
  88. {
  89. }
  90. USI
  91. or1k32bf_h_spr_get_raw (sim_cpu *current_cpu, USI addr)
  92. {
  93. SIM_DESC sd = CPU_STATE (current_cpu);
  94. SIM_ASSERT (addr < NUM_SPR);
  95. return current_cpu->spr[addr];
  96. }
  97. void
  98. or1k32bf_h_spr_set_raw (sim_cpu *current_cpu, USI addr, USI val)
  99. {
  100. SIM_DESC sd = CPU_STATE (current_cpu);
  101. SIM_ASSERT (addr < NUM_SPR);
  102. current_cpu->spr[addr] = val;
  103. }
  104. USI
  105. or1k32bf_h_spr_field_get_raw (sim_cpu *current_cpu, USI addr, int msb, int lsb)
  106. {
  107. SIM_DESC sd = CPU_STATE (current_cpu);
  108. SIM_ASSERT (addr < NUM_SPR);
  109. return LSEXTRACTED (current_cpu->spr[addr], msb, lsb);
  110. }
  111. void
  112. or1k32bf_h_spr_field_set_raw (sim_cpu *current_cpu, USI addr, int msb, int lsb,
  113. USI val)
  114. {
  115. current_cpu->spr[addr] &= ~LSMASK32 (msb, lsb);
  116. current_cpu->spr[addr] |= LSINSERTED (val, msb, lsb);
  117. }
  118. /* Initialize a sim cpu object. */
  119. void
  120. or1k_cpu_init (SIM_DESC sd, sim_cpu *current_cpu, const USI or1k_vr,
  121. const USI or1k_upr, const USI or1k_cpucfgr)
  122. {
  123. /* Set the configuration registers passed from the user. */
  124. SET_H_SYS_VR (or1k_vr);
  125. SET_H_SYS_UPR (or1k_upr);
  126. SET_H_SYS_CPUCFGR (or1k_cpucfgr);
  127. #define CHECK_SPR_FIELD(GROUP, INDEX, FIELD, test) \
  128. do \
  129. { \
  130. USI field = GET_H_##SYS##_##INDEX##_##FIELD (); \
  131. if (!(test)) \
  132. sim_io_eprintf \
  133. (sd, "WARNING: unsupported %s field in %s register: 0x%x\n", \
  134. #FIELD, #INDEX, field); \
  135. } while (0)
  136. /* Set flags indicating if we are in a delay slot or not. */
  137. current_cpu->next_delay_slot = 0;
  138. current_cpu->delay_slot = 0;
  139. /* Verify any user passed fields and warn on configurations we don't
  140. support. */
  141. CHECK_SPR_FIELD (SYS, UPR, UP, field == 1);
  142. CHECK_SPR_FIELD (SYS, UPR, DCP, field == 0);
  143. CHECK_SPR_FIELD (SYS, UPR, ICP, field == 0);
  144. CHECK_SPR_FIELD (SYS, UPR, DMP, field == 0);
  145. CHECK_SPR_FIELD (SYS, UPR, MP, field == 0);
  146. CHECK_SPR_FIELD (SYS, UPR, IMP, field == 0);
  147. CHECK_SPR_FIELD (SYS, UPR, DUP, field == 0);
  148. CHECK_SPR_FIELD (SYS, UPR, PCUP, field == 0);
  149. CHECK_SPR_FIELD (SYS, UPR, PICP, field == 0);
  150. CHECK_SPR_FIELD (SYS, UPR, PMP, field == 0);
  151. CHECK_SPR_FIELD (SYS, UPR, TTP, field == 0);
  152. CHECK_SPR_FIELD (SYS, UPR, CUP, field == 0);
  153. CHECK_SPR_FIELD (SYS, CPUCFGR, NSGR, field == 0);
  154. CHECK_SPR_FIELD (SYS, CPUCFGR, CGF, field == 0);
  155. CHECK_SPR_FIELD (SYS, CPUCFGR, OB32S, field == 1);
  156. CHECK_SPR_FIELD (SYS, CPUCFGR, OF32S, field == 1);
  157. CHECK_SPR_FIELD (SYS, CPUCFGR, OB64S, field == 0);
  158. CHECK_SPR_FIELD (SYS, CPUCFGR, OF64S, field == 0);
  159. CHECK_SPR_FIELD (SYS, CPUCFGR, OV64S, field == 0);
  160. #undef CHECK_SPR_FIELD
  161. /* Configure the fpu operations and mark fpu available. */
  162. cgen_init_accurate_fpu (current_cpu, CGEN_CPU_FPU (current_cpu),
  163. or1k32bf_fpu_error);
  164. SET_H_SYS_CPUCFGR_OF32S (1);
  165. /* Set the UPR[UP] flag, even if the user tried to unset it, as we always
  166. support the Unit Present Register. */
  167. SET_H_SYS_UPR_UP (1);
  168. /* Set the supervisor register to indicate we are in supervisor mode and
  169. set the Fixed-One bit which must always be set. */
  170. SET_H_SYS_SR (SPR_FIELD_MASK_SYS_SR_SM | SPR_FIELD_MASK_SYS_SR_FO);
  171. /* Clear the floating point control status register. */
  172. SET_H_SYS_FPCSR (0);
  173. }
  174. void
  175. or1k32bf_insn_before (sim_cpu *current_cpu, SEM_PC vpc, const IDESC *idesc)
  176. {
  177. SIM_DESC sd = CPU_STATE (current_cpu);
  178. current_cpu->delay_slot = current_cpu->next_delay_slot;
  179. current_cpu->next_delay_slot = 0;
  180. if (current_cpu->delay_slot &&
  181. CGEN_ATTR_BOOLS (CGEN_INSN_ATTRS ((idesc)->idata)) &
  182. CGEN_ATTR_MASK (CGEN_INSN_NOT_IN_DELAY_SLOT))
  183. {
  184. USI pc;
  185. #ifdef WITH_SCACHE
  186. pc = vpc->argbuf.addr;
  187. #else
  188. pc = vpc;
  189. #endif
  190. sim_io_error (sd, "invalid instruction in a delay slot at PC 0x%08x",
  191. pc);
  192. }
  193. }
  194. void
  195. or1k32bf_insn_after (sim_cpu *current_cpu, SEM_PC vpc, const IDESC *idesc)
  196. {
  197. SIM_DESC sd = CPU_STATE (current_cpu);
  198. USI ppc;
  199. #ifdef WITH_SCACHE
  200. ppc = vpc->argbuf.addr;
  201. #else
  202. ppc = vpc;
  203. #endif
  204. SET_H_SYS_PPC (ppc);
  205. if (!GET_H_SYS_CPUCFGR_ND () &&
  206. CGEN_ATTR_BOOLS (CGEN_INSN_ATTRS ((idesc)->idata)) &
  207. CGEN_ATTR_MASK (CGEN_INSN_DELAYED_CTI))
  208. {
  209. SIM_ASSERT (!current_cpu->delay_slot);
  210. current_cpu->next_delay_slot = 1;
  211. }
  212. }
  213. void
  214. or1k32bf_nop (sim_cpu *current_cpu, USI uimm16)
  215. {
  216. SIM_DESC sd = CPU_STATE (current_cpu);
  217. switch (uimm16)
  218. {
  219. case NOP_NOP:
  220. break;
  221. case NOP_EXIT:
  222. sim_io_printf (CPU_STATE (current_cpu), "exit(%d)\n", GET_H_GPR (3));
  223. /* fall through */
  224. case NOP_EXIT_SILENT:
  225. sim_engine_halt (sd, current_cpu, NULL, CPU_PC_GET (current_cpu),
  226. sim_exited, GET_H_GPR (3));
  227. break;
  228. case NOP_REPORT:
  229. sim_io_printf (CPU_STATE (current_cpu), "report(0x%08x);\n",
  230. GET_H_GPR (3));
  231. break;
  232. case NOP_PUTC:
  233. sim_io_printf (CPU_STATE (current_cpu), "%c",
  234. (char) (GET_H_GPR (3) & 0xff));
  235. break;
  236. default:
  237. sim_io_eprintf (sd, "WARNING: l.nop with unsupported code 0x%08x\n",
  238. uimm16);
  239. break;
  240. }
  241. }
  242. /* Build an address value used for load and store instructions. For example,
  243. the instruction 'l.lws rD, I(rA)' will require to load data from the 4 byte
  244. address represented by rA + I. Here the argument base is rA, offset is I
  245. and the size is the read size in bytes. Note, OpenRISC requires that word
  246. and half-word access be word and half-word aligned respectively, the check
  247. for alignment is not needed here. */
  248. USI
  249. or1k32bf_make_load_store_addr (sim_cpu *current_cpu, USI base, SI offset,
  250. int size)
  251. {
  252. SIM_DESC sd = CPU_STATE (current_cpu);
  253. USI addr = base + offset;
  254. /* If little endian load/store is enabled we adjust the byte and half-word
  255. addresses to the little endian equivalent. */
  256. if (GET_H_SYS_SR_LEE ())
  257. {
  258. switch (size)
  259. {
  260. case 4: /* We are retrieving the entire word no adjustment. */
  261. break;
  262. case 2: /* Perform half-word adjustment 0 -> 2, 2 -> 0. */
  263. addr ^= 0x2;
  264. break;
  265. case 1: /* Perform byte adjustment, 0 -> 3, 2 -> 3, etc. */
  266. addr ^= 0x3;
  267. break;
  268. default:
  269. SIM_ASSERT (0);
  270. return 0;
  271. }
  272. }
  273. return addr;
  274. }
  275. /* The find first 1 instruction returns the location of the first set bit
  276. in the argument register. */
  277. USI
  278. or1k32bf_ff1 (sim_cpu *current_cpu, USI val)
  279. {
  280. USI bit;
  281. USI ret;
  282. for (bit = 1, ret = 1; bit; bit <<= 1, ret++)
  283. {
  284. if (val & bit)
  285. return ret;
  286. }
  287. return 0;
  288. }
  289. /* The find last 1 instruction returns the location of the last set bit in
  290. the argument register. */
  291. USI
  292. or1k32bf_fl1 (sim_cpu *current_cpu, USI val)
  293. {
  294. USI bit;
  295. USI ret;
  296. for (bit = 1 << 31, ret = 32; bit; bit >>= 1, ret--)
  297. {
  298. if (val & bit)
  299. return ret;
  300. }
  301. return 0;
  302. }