cr16_sim.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* Simulation code for the CR16 processor.
  2. Copyright (C) 2008-2015 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. #include "config.h"
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <limits.h>
  19. #include "ansidecl.h"
  20. #include "gdb/callback.h"
  21. #include "opcode/cr16.h"
  22. #include "bfd.h"
  23. #define DEBUG_TRACE 0x00000001
  24. #define DEBUG_VALUES 0x00000002
  25. #define DEBUG_LINE_NUMBER 0x00000004
  26. #define DEBUG_MEMSIZE 0x00000008
  27. #define DEBUG_INSTRUCTION 0x00000010
  28. #define DEBUG_TRAP 0x00000020
  29. #define DEBUG_MEMORY 0x00000040
  30. #ifndef DEBUG
  31. #define DEBUG (DEBUG_TRACE | DEBUG_VALUES | DEBUG_LINE_NUMBER)
  32. #endif
  33. extern int cr16_debug;
  34. #include "gdb/remote-sim.h"
  35. #include "sim-config.h"
  36. #include "sim-types.h"
  37. typedef unsigned8 uint8;
  38. typedef signed8 int8;
  39. typedef unsigned16 uint16;
  40. typedef signed16 int16;
  41. typedef unsigned32 uint32;
  42. typedef signed32 int32;
  43. typedef unsigned64 uint64;
  44. typedef signed64 int64;
  45. /* FIXME: CR16 defines */
  46. typedef uint16 reg_t;
  47. typedef uint32 creg_t;
  48. struct simops
  49. {
  50. char mnimonic[12];
  51. uint32 size;
  52. uint32 mask;
  53. uint32 opcode;
  54. int format;
  55. char fname[12];
  56. void (*func)();
  57. int numops;
  58. operand_desc operands[4];
  59. };
  60. enum _ins_type
  61. {
  62. INS_UNKNOWN, /* unknown instruction */
  63. INS_NO_TYPE_INS,
  64. INS_ARITH_INS,
  65. INS_LD_STOR_INS,
  66. INS_BRANCH_INS,
  67. INS_ARITH_BYTE_INS,
  68. INS_SHIFT_INS,
  69. INS_BRANCH_NEQ_INS,
  70. INS_STOR_IMM_INS,
  71. INS_CSTBIT_INS,
  72. INS_MAX
  73. };
  74. extern unsigned long ins_type_counters[ (int)INS_MAX ];
  75. enum {
  76. SP_IDX = 15,
  77. };
  78. /* Write-back slots */
  79. union slot_data {
  80. unsigned_1 _1;
  81. unsigned_2 _2;
  82. unsigned_4 _4;
  83. };
  84. struct slot {
  85. void *dest;
  86. int size;
  87. union slot_data data;
  88. union slot_data mask;
  89. };
  90. enum {
  91. NR_SLOTS = 16
  92. };
  93. #define SLOT (State.slot)
  94. #define SLOT_NR (State.slot_nr)
  95. #define SLOT_PEND_MASK(DEST, MSK, VAL) \
  96. do \
  97. { \
  98. SLOT[SLOT_NR].dest = &(DEST); \
  99. SLOT[SLOT_NR].size = sizeof (DEST); \
  100. switch (sizeof (DEST)) \
  101. { \
  102. case 1: \
  103. SLOT[SLOT_NR].data._1 = (unsigned_1) (VAL); \
  104. SLOT[SLOT_NR].mask._1 = (unsigned_1) (MSK); \
  105. break; \
  106. case 2: \
  107. SLOT[SLOT_NR].data._2 = (unsigned_2) (VAL); \
  108. SLOT[SLOT_NR].mask._2 = (unsigned_2) (MSK); \
  109. break; \
  110. case 4: \
  111. SLOT[SLOT_NR].data._4 = (unsigned_4) (VAL); \
  112. SLOT[SLOT_NR].mask._4 = (unsigned_4) (MSK); \
  113. break; \
  114. } \
  115. SLOT_NR = (SLOT_NR + 1); \
  116. } \
  117. while (0)
  118. #define SLOT_PEND(DEST, VAL) SLOT_PEND_MASK(DEST, 0, VAL)
  119. #define SLOT_DISCARD() (SLOT_NR = 0)
  120. #define SLOT_FLUSH() \
  121. do \
  122. { \
  123. int i; \
  124. for (i = 0; i < SLOT_NR; i++) \
  125. { \
  126. switch (SLOT[i].size) \
  127. { \
  128. case 1: \
  129. *(unsigned_1*) SLOT[i].dest &= SLOT[i].mask._1; \
  130. *(unsigned_1*) SLOT[i].dest |= SLOT[i].data._1; \
  131. break; \
  132. case 2: \
  133. *(unsigned_2*) SLOT[i].dest &= SLOT[i].mask._2; \
  134. *(unsigned_2*) SLOT[i].dest |= SLOT[i].data._2; \
  135. break; \
  136. case 4: \
  137. *(unsigned_4*) SLOT[i].dest &= SLOT[i].mask._4; \
  138. *(unsigned_4*) SLOT[i].dest |= SLOT[i].data._4; \
  139. break; \
  140. } \
  141. } \
  142. SLOT_NR = 0; \
  143. } \
  144. while (0)
  145. #define SLOT_DUMP() \
  146. do \
  147. { \
  148. int i; \
  149. for (i = 0; i < SLOT_NR; i++) \
  150. { \
  151. switch (SLOT[i].size) \
  152. { \
  153. case 1: \
  154. printf ("SLOT %d *0x%08lx & 0x%02x | 0x%02x\n", i, \
  155. (long) SLOT[i].dest, \
  156. (unsigned) SLOT[i].mask._1, \
  157. (unsigned) SLOT[i].data._1); \
  158. break; \
  159. case 2: \
  160. printf ("SLOT %d *0x%08lx & 0x%04x | 0x%04x\n", i, \
  161. (long) SLOT[i].dest, \
  162. (unsigned) SLOT[i].mask._2, \
  163. (unsigned) SLOT[i].data._2); \
  164. break; \
  165. case 4: \
  166. printf ("SLOT %d *0x%08lx & 0x%08x | 0x%08x\n", i, \
  167. (long) SLOT[i].dest, \
  168. (unsigned) SLOT[i].mask._4, \
  169. (unsigned) SLOT[i].data._4); \
  170. break; \
  171. case 8: \
  172. printf ("SLOT %d *0x%08lx & 0x%08x%08x | 0x%08x%08x\n", i, \
  173. (long) SLOT[i].dest, \
  174. (unsigned) (SLOT[i].mask._8 >> 32), \
  175. (unsigned) SLOT[i].mask._8, \
  176. (unsigned) (SLOT[i].data._8 >> 32), \
  177. (unsigned) SLOT[i].data._8); \
  178. break; \
  179. } \
  180. } \
  181. } \
  182. while (0)
  183. /* cr16 memory: There are three separate cr16 memory regions IMEM,
  184. UMEM and DMEM. The IMEM and DMEM are further broken down into
  185. blocks (very like VM pages). */
  186. enum
  187. {
  188. IMAP_BLOCK_SIZE = 0x2000000,
  189. DMAP_BLOCK_SIZE = 0x4000000
  190. };
  191. /* Implement the three memory regions using sparse arrays. Allocate
  192. memory using ``segments''. A segment must be at least as large as
  193. a BLOCK - ensures that an access that doesn't cross a block
  194. boundary can't cross a segment boundary */
  195. enum
  196. {
  197. SEGMENT_SIZE = 0x2000000, /* 128KB - MAX(IMAP_BLOCK_SIZE,DMAP_BLOCK_SIZE) */
  198. IMEM_SEGMENTS = 8, /* 1MB */
  199. DMEM_SEGMENTS = 8, /* 1MB */
  200. UMEM_SEGMENTS = 128 /* 16MB */
  201. };
  202. struct cr16_memory
  203. {
  204. uint8 *insn[IMEM_SEGMENTS];
  205. uint8 *data[DMEM_SEGMENTS];
  206. uint8 *unif[UMEM_SEGMENTS];
  207. uint8 fault[16];
  208. };
  209. struct _state
  210. {
  211. creg_t regs[16]; /* general-purpose registers */
  212. #define GPR(N) (State.regs[(N)] + 0)
  213. #define SET_GPR(N,VAL) (State.regs[(N)] = (VAL))
  214. #define GPR32(N) \
  215. (N < 12) ? \
  216. ((((uint16) State.regs[(N) + 1]) << 16) | (uint16) State.regs[(N)]) \
  217. : GPR (N)
  218. #define SET_GPR32(N,VAL) do { \
  219. if (N < 11) \
  220. { SET_GPR (N + 1, (VAL) >> 16); SET_GPR (N, ((VAL) & 0xffff));} \
  221. else { if ( N == 11) \
  222. { SET_GPR (N + 1, ((GPR32 (12)) & 0xffff0000)|((VAL) >> 16)); \
  223. SET_GPR (N, ((VAL) & 0xffff));} \
  224. else SET_GPR (N, (VAL));} \
  225. } while (0)
  226. creg_t cregs[16]; /* control registers */
  227. #define CREG(N) (State.cregs[(N)] + 0)
  228. #define SET_CREG(N,VAL) move_to_cr ((N), 0, (VAL), 0)
  229. #define SET_HW_CREG(N,VAL) move_to_cr ((N), 0, (VAL), 1)
  230. reg_t sp[2]; /* holding area for SPI(0)/SPU(1) */
  231. #define HELD_SP(N) (State.sp[(N)] + 0)
  232. #define SET_HELD_SP(N,VAL) SLOT_PEND (State.sp[(N)], (VAL))
  233. /* writeback info */
  234. struct slot slot[NR_SLOTS];
  235. int slot_nr;
  236. /* trace data */
  237. struct {
  238. uint16 psw;
  239. } trace;
  240. uint8 exe;
  241. int exception;
  242. int pc_changed;
  243. /* NOTE: everything below this line is not reset by
  244. sim_create_inferior() */
  245. struct cr16_memory mem;
  246. enum _ins_type ins_type;
  247. } State;
  248. extern host_callback *cr16_callback;
  249. extern uint32 OP[4];
  250. extern uint32 sign_flag;
  251. extern struct simops Simops[];
  252. enum
  253. {
  254. PC_CR = 0,
  255. BDS_CR = 1,
  256. BSR_CR = 2,
  257. DCR_CR = 3,
  258. CAR0_CR = 5,
  259. CAR1_CR = 7,
  260. CFG_CR = 9,
  261. PSR_CR = 10,
  262. INTBASE_CR = 11,
  263. ISP_CR = 13,
  264. USP_CR = 15
  265. };
  266. enum
  267. {
  268. PSR_I_BIT = 0x0800,
  269. PSR_P_BIT = 0x0400,
  270. PSR_E_BIT = 0x0200,
  271. PSR_N_BIT = 0x0080,
  272. PSR_Z_BIT = 0x0040,
  273. PSR_F_BIT = 0x0020,
  274. PSR_U_BIT = 0x0008,
  275. PSR_L_BIT = 0x0004,
  276. PSR_T_BIT = 0x0002,
  277. PSR_C_BIT = 0x0001
  278. };
  279. #define PSR CREG (PSR_CR)
  280. #define SET_PSR(VAL) SET_CREG (PSR_CR, (VAL))
  281. #define SET_HW_PSR(VAL) SET_HW_CREG (PSR_CR, (VAL))
  282. #define SET_PSR_BIT(MASK,VAL) move_to_cr (PSR_CR, ~((creg_t) MASK), (VAL) ? (MASK) : 0, 1)
  283. #define PSR_SM ((PSR & PSR_SM_BIT) != 0)
  284. #define SET_PSR_SM(VAL) SET_PSR_BIT (PSR_SM_BIT, (VAL))
  285. #define PSR_I ((PSR & PSR_I_BIT) != 0)
  286. #define SET_PSR_I(VAL) SET_PSR_BIT (PSR_I_BIT, (VAL))
  287. #define PSR_DB ((PSR & PSR_DB_BIT) != 0)
  288. #define SET_PSR_DB(VAL) SET_PSR_BIT (PSR_DB_BIT, (VAL))
  289. #define PSR_P ((PSR & PSR_P_BIT) != 0)
  290. #define SET_PSR_P(VAL) SET_PSR_BIT (PSR_P_BIT, (VAL))
  291. #define PSR_E ((PSR & PSR_E_BIT) != 0)
  292. #define SET_PSR_E(VAL) SET_PSR_BIT (PSR_E_BIT, (VAL))
  293. #define PSR_N ((PSR & PSR_N_BIT) != 0)
  294. #define SET_PSR_N(VAL) SET_PSR_BIT (PSR_N_BIT, (VAL))
  295. #define PSR_Z ((PSR & PSR_Z_BIT) != 0)
  296. #define SET_PSR_Z(VAL) SET_PSR_BIT (PSR_Z_BIT, (VAL))
  297. #define PSR_F ((PSR & PSR_F_BIT) != 0)
  298. #define SET_PSR_F(VAL) SET_PSR_BIT (PSR_F_BIT, (VAL))
  299. #define PSR_U ((PSR & PSR_U_BIT) != 0)
  300. #define SET_PSR_U(VAL) SET_PSR_BIT (PSR_U_BIT, (VAL))
  301. #define PSR_L ((PSR & PSR_L_BIT) != 0)
  302. #define SET_PSR_L(VAL) SET_PSR_BIT (PSR_L_BIT, (VAL))
  303. #define PSR_T ((PSR & PSR_T_BIT) != 0)
  304. #define SET_PSR_T(VAL) SET_PSR_BIT (PSR_T_BIT, (VAL))
  305. #define PSR_C ((PSR & PSR_C_BIT) != 0)
  306. #define SET_PSR_C(VAL) SET_PSR_BIT (PSR_C_BIT, (VAL))
  307. /* See simopsc.:move_to_cr() for registers that can not be read-from
  308. or assigned-to directly */
  309. #define PC CREG (PC_CR)
  310. #define SET_PC(VAL) SET_CREG (PC_CR, (VAL))
  311. //#define SET_PC(VAL) (State.cregs[PC_CR] = (VAL))
  312. #define BPSR CREG (BPSR_CR)
  313. #define SET_BPSR(VAL) SET_CREG (BPSR_CR, (VAL))
  314. #define BPC CREG (BPC_CR)
  315. #define SET_BPC(VAL) SET_CREG (BPC_CR, (VAL))
  316. #define DPSR CREG (DPSR_CR)
  317. #define SET_DPSR(VAL) SET_CREG (DPSR_CR, (VAL))
  318. #define DPC CREG (DPC_CR)
  319. #define SET_DPC(VAL) SET_CREG (DPC_CR, (VAL))
  320. #define RPT_C CREG (RPT_C_CR)
  321. #define SET_RPT_C(VAL) SET_CREG (RPT_C_CR, (VAL))
  322. #define RPT_S CREG (RPT_S_CR)
  323. #define SET_RPT_S(VAL) SET_CREG (RPT_S_CR, (VAL))
  324. #define RPT_E CREG (RPT_E_CR)
  325. #define SET_RPT_E(VAL) SET_CREG (RPT_E_CR, (VAL))
  326. #define MOD_S CREG (MOD_S_CR)
  327. #define SET_MOD_S(VAL) SET_CREG (MOD_S_CR, (VAL))
  328. #define MOD_E CREG (MOD_E_CR)
  329. #define SET_MOD_E(VAL) SET_CREG (MOD_E_CR, (VAL))
  330. #define IBA CREG (IBA_CR)
  331. #define SET_IBA(VAL) SET_CREG (IBA_CR, (VAL))
  332. #define SIG_CR16_STOP -1
  333. #define SIG_CR16_EXIT -2
  334. #define SIG_CR16_BUS -3
  335. #define SIG_CR16_IAD -4
  336. /* TODO: Resolve conflicts with common headers. */
  337. #undef SEXT8
  338. #undef SEXT16
  339. #undef SEXT32
  340. #define SEXT3(x) ((((x)&0x7)^(~3))+4)
  341. /* sign-extend a 4-bit number */
  342. #define SEXT4(x) ((((x)&0xf)^(~7))+8)
  343. /* sign-extend an 8-bit number */
  344. #define SEXT8(x) ((((x)&0xff)^(~0x7f))+0x80)
  345. /* sign-extend a 16-bit number */
  346. #define SEXT16(x) ((((x)&0xffff)^(~0x7fff))+0x8000)
  347. /* sign-extend a 24-bit number */
  348. #define SEXT24(x) ((((x)&0xffffff)^(~0x7fffff))+0x800000)
  349. /* sign-extend a 32-bit number */
  350. #define SEXT32(x) ((((x)&0xffffffff)^(~0x7fffffff))+0x80000000)
  351. extern uint8 *dmem_addr (uint32 offset);
  352. extern uint8 *imem_addr (uint32);
  353. extern bfd_vma decode_pc (void);
  354. #define RB(x) (*(dmem_addr(x)))
  355. #define SB(addr,data) ( RB(addr) = (data & 0xff))
  356. #if defined(__GNUC__) && defined(__OPTIMIZE__) && !defined(NO_ENDIAN_INLINE)
  357. #define ENDIAN_INLINE static __inline__
  358. #include "endian.c"
  359. #undef ENDIAN_INLINE
  360. #else
  361. extern uint32 get_longword (uint8 *);
  362. extern uint16 get_word (uint8 *);
  363. extern int64 get_longlong (uint8 *);
  364. extern void write_word (uint8 *addr, uint16 data);
  365. extern void write_longword (uint8 *addr, uint32 data);
  366. extern void write_longlong (uint8 *addr, int64 data);
  367. #endif
  368. #define SW(addr,data) write_word(dmem_addr(addr),data)
  369. #define RW(x) get_word(dmem_addr(x))
  370. #define SLW(addr,data) write_longword(dmem_addr(addr),data)
  371. #define RLW(x) get_longword(dmem_addr(x))
  372. #define READ_16(x) get_word(x)
  373. #define WRITE_16(addr,data) write_word(addr,data)
  374. #define READ_64(x) get_longlong(x)
  375. #define WRITE_64(addr,data) write_longlong(addr,data)
  376. #define JMP(x) do { SET_PC (x); State.pc_changed = 1; } while (0)
  377. #define RIE_VECTOR_START 0xffc2
  378. #define AE_VECTOR_START 0xffc3
  379. #define TRAP_VECTOR_START 0xffc4 /* vector for trap 0 */
  380. #define DBT_VECTOR_START 0xffd4
  381. #define SDBT_VECTOR_START 0xffd5
  382. #define INT_VECTOR_START 0xFFFE00 /*maskable interrupt - mapped to ICU */
  383. #define NMI_VECTOR_START 0xFFFF00 /*non-maskable interrupt;for observability*/
  384. #define ISE_VECTOR_START 0xFFFC00 /*in-system emulation trap */
  385. #define ADBG_VECTOR_START 0xFFFC02 /*alternate debug trap */
  386. #define ATRC_VECTOR_START 0xFFFC0C /*alternate trace trap */
  387. #define ABPT_VECTOR_START 0xFFFC0E /*alternate break point trap */
  388. /* Scedule a store of VAL into cr[CR]. MASK indicates the bits in
  389. cr[CR] that should not be modified (i.e. cr[CR] = (cr[CR] & MASK) |
  390. (VAL & ~MASK)). In addition, unless PSR_HW_P, a VAL intended for
  391. PSR is masked for zero bits. */
  392. extern creg_t move_to_cr (int cr, creg_t mask, creg_t val, int psw_hw_p);
  393. #ifndef SIGTRAP
  394. #define SIGTRAP 5
  395. #endif
  396. /* Special purpose trap */
  397. #define TRAP_BREAKPOINT 8