6 #include "gdb/callback.h"
7 #include "opcode/d10v.h"
10 #define DEBUG_TRACE 0x00000001
11 #define DEBUG_VALUES 0x00000002
12 #define DEBUG_LINE_NUMBER 0x00000004
13 #define DEBUG_MEMSIZE 0x00000008
14 #define DEBUG_INSTRUCTION 0x00000010
15 #define DEBUG_TRAP 0x00000020
16 #define DEBUG_MEMORY 0x00000040
19 #define DEBUG (DEBUG_TRACE | DEBUG_VALUES | DEBUG_LINE_NUMBER)
22 extern int d10v_debug;
24 #include "gdb/remote-sim.h"
25 #include "sim-config.h"
26 #include "sim-types.h"
28 typedef unsigned8 uint8;
29 typedef unsigned16 uint16;
30 typedef signed16 int16;
31 typedef unsigned32 uint32;
32 typedef signed32 int32;
33 typedef unsigned64 uint64;
34 typedef signed64 int64;
36 /* FIXME: D10V defines */
55 INS_UNKNOWN, /* unknown instruction */
56 INS_COND_TRUE, /* # times EXExxx executed other instruction */
57 INS_COND_FALSE, /* # times EXExxx did not execute other instruction */
58 INS_COND_JUMP, /* # times JUMP skipped other instruction */
59 INS_CYCLES, /* # cycles */
60 INS_LONG, /* long instruction (both containers, ie FM == 11) */
61 INS_LEFTRIGHT, /* # times instruction encoded as L -> R (ie, FM == 01) */
62 INS_RIGHTLEFT, /* # times instruction encoded as L <- R (ie, FM == 10) */
63 INS_PARALLEL, /* # times instruction encoded as L || R (ie, RM == 00) */
65 INS_LEFT, /* normal left instructions */
66 INS_LEFT_PARALLEL, /* left side of || */
67 INS_LEFT_COND_TEST, /* EXExx test on left side */
68 INS_LEFT_COND_EXE, /* execution after EXExxx test on right side succeeded */
69 INS_LEFT_NOPS, /* NOP on left side */
71 INS_RIGHT, /* normal right instructions */
72 INS_RIGHT_PARALLEL, /* right side of || */
73 INS_RIGHT_COND_TEST, /* EXExx test on right side */
74 INS_RIGHT_COND_EXE, /* execution after EXExxx test on left side succeeded */
75 INS_RIGHT_NOPS, /* NOP on right side */
80 extern unsigned long ins_type_counters[ (int)INS_MAX ];
86 /* Write-back slots */
102 #define SLOT (State.slot)
103 #define SLOT_NR (State.slot_nr)
104 #define SLOT_PEND_MASK(DEST, MSK, VAL) \
107 SLOT[SLOT_NR].dest = &(DEST); \
108 SLOT[SLOT_NR].size = sizeof (DEST); \
109 switch (sizeof (DEST)) \
112 SLOT[SLOT_NR].data._1 = (unsigned_1) (VAL); \
113 SLOT[SLOT_NR].mask._1 = (unsigned_1) (MSK); \
116 SLOT[SLOT_NR].data._2 = (unsigned_2) (VAL); \
117 SLOT[SLOT_NR].mask._2 = (unsigned_2) (MSK); \
120 SLOT[SLOT_NR].data._4 = (unsigned_4) (VAL); \
121 SLOT[SLOT_NR].mask._4 = (unsigned_4) (MSK); \
124 SLOT[SLOT_NR].data._8 = (unsigned_8) (VAL); \
125 SLOT[SLOT_NR].mask._8 = (unsigned_8) (MSK); \
128 SLOT_NR = (SLOT_NR + 1); \
131 #define SLOT_PEND(DEST, VAL) SLOT_PEND_MASK(DEST, 0, VAL)
132 #define SLOT_DISCARD() (SLOT_NR = 0)
133 #define SLOT_FLUSH() \
137 for (i = 0; i < SLOT_NR; i++) \
139 switch (SLOT[i].size) \
142 *(unsigned_1*) SLOT[i].dest &= SLOT[i].mask._1; \
143 *(unsigned_1*) SLOT[i].dest |= SLOT[i].data._1; \
146 *(unsigned_2*) SLOT[i].dest &= SLOT[i].mask._2; \
147 *(unsigned_2*) SLOT[i].dest |= SLOT[i].data._2; \
150 *(unsigned_4*) SLOT[i].dest &= SLOT[i].mask._4; \
151 *(unsigned_4*) SLOT[i].dest |= SLOT[i].data._4; \
154 *(unsigned_8*) SLOT[i].dest &= SLOT[i].mask._8; \
155 *(unsigned_8*) SLOT[i].dest |= SLOT[i].data._8; \
162 #define SLOT_DUMP() \
166 for (i = 0; i < SLOT_NR; i++) \
168 switch (SLOT[i].size) \
171 printf ("SLOT %d *0x%08lx & 0x%02x | 0x%02x\n", i, \
172 (long) SLOT[i].dest, \
173 (unsigned) SLOT[i].mask._1, \
174 (unsigned) SLOT[i].data._1); \
177 printf ("SLOT %d *0x%08lx & 0x%04x | 0x%04x\n", i, \
178 (long) SLOT[i].dest, \
179 (unsigned) SLOT[i].mask._2, \
180 (unsigned) SLOT[i].data._2); \
183 printf ("SLOT %d *0x%08lx & 0x%08x | 0x%08x\n", i, \
184 (long) SLOT[i].dest, \
185 (unsigned) SLOT[i].mask._4, \
186 (unsigned) SLOT[i].data._4); \
189 printf ("SLOT %d *0x%08lx & 0x%08x%08x | 0x%08x%08x\n", i, \
190 (long) SLOT[i].dest, \
191 (unsigned) (SLOT[i].mask._8 >> 32), \
192 (unsigned) SLOT[i].mask._8, \
193 (unsigned) (SLOT[i].data._8 >> 32), \
194 (unsigned) SLOT[i].data._8); \
201 /* d10v memory: There are three separate d10v memory regions IMEM,
202 UMEM and DMEM. The IMEM and DMEM are further broken down into
203 blocks (very like VM pages). */
207 IMAP_BLOCK_SIZE = 0x20000,
208 DMAP_BLOCK_SIZE = 0x4000,
211 /* Implement the three memory regions using sparse arrays. Allocate
212 memory using ``segments''. A segment must be at least as large as
213 a BLOCK - ensures that an access that doesn't cross a block
214 boundary can't cross a segment boundary */
218 SEGMENT_SIZE = 0x20000, /* 128KB - MAX(IMAP_BLOCK_SIZE,DMAP_BLOCK_SIZE) */
219 IMEM_SEGMENTS = 8, /* 1MB */
220 DMEM_SEGMENTS = 8, /* 1MB */
221 UMEM_SEGMENTS = 128 /* 16MB */
226 uint8 *insn[IMEM_SEGMENTS];
227 uint8 *data[DMEM_SEGMENTS];
228 uint8 *unif[UMEM_SEGMENTS];
234 reg_t regs[16]; /* general-purpose registers */
235 #define GPR(N) (State.regs[(N)] + 0)
236 #define SET_GPR(N,VAL) SLOT_PEND (State.regs[(N)], (VAL))
238 #define GPR32(N) ((((uint32) State.regs[(N) + 0]) << 16) \
239 | (uint16) State.regs[(N) + 1])
240 #define SET_GPR32(N,VAL) do { SET_GPR (OP[0] + 0, (VAL) >> 16); SET_GPR (OP[0] + 1, (VAL)); } while (0)
242 reg_t cregs[16]; /* control registers */
243 #define CREG(N) (State.cregs[(N)] + 0)
244 #define SET_CREG(N,VAL) move_to_cr ((N), 0, (VAL), 0)
245 #define SET_HW_CREG(N,VAL) move_to_cr ((N), 0, (VAL), 1)
247 reg_t sp[2]; /* holding area for SPI(0)/SPU(1) */
248 #define HELD_SP(N) (State.sp[(N)] + 0)
249 #define SET_HELD_SP(N,VAL) SLOT_PEND (State.sp[(N)], (VAL))
251 int64 a[2]; /* accumulators */
252 #define ACC(N) (State.a[(N)] + 0)
253 #define SET_ACC(N,VAL) SLOT_PEND (State.a[(N)], (VAL) & MASK40)
256 struct slot slot[NR_SLOTS];
268 /* NOTE: everything below this line is not reset by
269 sim_create_inferior() */
271 struct d10v_memory mem;
273 enum _ins_type ins_type;
278 extern host_callback *d10v_callback;
280 extern struct simops Simops[];
314 #define PSW CREG (PSW_CR)
315 #define SET_PSW(VAL) SET_CREG (PSW_CR, (VAL))
316 #define SET_HW_PSW(VAL) SET_HW_CREG (PSW_CR, (VAL))
317 #define SET_PSW_BIT(MASK,VAL) move_to_cr (PSW_CR, ~((reg_t) MASK), (VAL) ? (MASK) : 0, 1)
319 #define PSW_SM ((PSW & PSW_SM_BIT) != 0)
320 #define SET_PSW_SM(VAL) SET_PSW_BIT (PSW_SM_BIT, (VAL))
322 #define PSW_EA ((PSW & PSW_EA_BIT) != 0)
323 #define SET_PSW_EA(VAL) SET_PSW_BIT (PSW_EA_BIT, (VAL))
325 #define PSW_DB ((PSW & PSW_DB_BIT) != 0)
326 #define SET_PSW_DB(VAL) SET_PSW_BIT (PSW_DB_BIT, (VAL))
328 #define PSW_DM ((PSW & PSW_DM_BIT) != 0)
329 #define SET_PSW_DM(VAL) SET_PSW_BIT (PSW_DM_BIT, (VAL))
331 #define PSW_IE ((PSW & PSW_IE_BIT) != 0)
332 #define SET_PSW_IE(VAL) SET_PSW_BIT (PSW_IE_BIT, (VAL))
334 #define PSW_RP ((PSW & PSW_RP_BIT) != 0)
335 #define SET_PSW_RP(VAL) SET_PSW_BIT (PSW_RP_BIT, (VAL))
337 #define PSW_MD ((PSW & PSW_MD_BIT) != 0)
338 #define SET_PSW_MD(VAL) SET_PSW_BIT (PSW_MD_BIT, (VAL))
340 #define PSW_FX ((PSW & PSW_FX_BIT) != 0)
341 #define SET_PSW_FX(VAL) SET_PSW_BIT (PSW_FX_BIT, (VAL))
343 #define PSW_ST ((PSW & PSW_ST_BIT) != 0)
344 #define SET_PSW_ST(VAL) SET_PSW_BIT (PSW_ST_BIT, (VAL))
346 #define PSW_F0 ((PSW & PSW_F0_BIT) != 0)
347 #define SET_PSW_F0(VAL) SET_PSW_BIT (PSW_F0_BIT, (VAL))
349 #define PSW_F1 ((PSW & PSW_F1_BIT) != 0)
350 #define SET_PSW_F1(VAL) SET_PSW_BIT (PSW_F1_BIT, (VAL))
352 #define PSW_C ((PSW & PSW_C_BIT) != 0)
353 #define SET_PSW_C(VAL) SET_PSW_BIT (PSW_C_BIT, (VAL))
355 /* See simopsc.:move_to_cr() for registers that can not be read-from
356 or assigned-to directly */
358 #define PC CREG (PC_CR)
359 #define SET_PC(VAL) SET_CREG (PC_CR, (VAL))
361 #define BPSW CREG (BPSW_CR)
362 #define SET_BPSW(VAL) SET_CREG (BPSW_CR, (VAL))
364 #define BPC CREG (BPC_CR)
365 #define SET_BPC(VAL) SET_CREG (BPC_CR, (VAL))
367 #define DPSW CREG (DPSW_CR)
368 #define SET_DPSW(VAL) SET_CREG (DPSW_CR, (VAL))
370 #define DPC CREG (DPC_CR)
371 #define SET_DPC(VAL) SET_CREG (DPC_CR, (VAL))
373 #define RPT_C CREG (RPT_C_CR)
374 #define SET_RPT_C(VAL) SET_CREG (RPT_C_CR, (VAL))
376 #define RPT_S CREG (RPT_S_CR)
377 #define SET_RPT_S(VAL) SET_CREG (RPT_S_CR, (VAL))
379 #define RPT_E CREG (RPT_E_CR)
380 #define SET_RPT_E(VAL) SET_CREG (RPT_E_CR, (VAL))
382 #define MOD_S CREG (MOD_S_CR)
383 #define SET_MOD_S(VAL) SET_CREG (MOD_S_CR, (VAL))
385 #define MOD_E CREG (MOD_E_CR)
386 #define SET_MOD_E(VAL) SET_CREG (MOD_E_CR, (VAL))
388 #define IBA CREG (IBA_CR)
389 #define SET_IBA(VAL) SET_CREG (IBA_CR, (VAL))
392 #define SIG_D10V_STOP -1
393 #define SIG_D10V_EXIT -2
394 #define SIG_D10V_BUS -3
396 /* TODO: Resolve conflicts with common headers. */
402 #define SEXT3(x) ((((x)&0x7)^(~3))+4)
404 /* sign-extend a 4-bit number */
405 #define SEXT4(x) ((((x)&0xf)^(~7))+8)
407 /* sign-extend an 8-bit number */
408 #define SEXT8(x) ((((x)&0xff)^(~0x7f))+0x80)
410 /* sign-extend a 16-bit number */
411 #define SEXT16(x) ((((x)&0xffff)^(~0x7fff))+0x8000)
413 /* sign-extend a 32-bit number */
414 #define SEXT32(x) ((((x)&SIGNED64(0xffffffff))^(~SIGNED64(0x7fffffff)))+SIGNED64(0x80000000))
416 /* sign extend a 40 bit number */
417 #define SEXT40(x) ((((x)&SIGNED64(0xffffffffff))^(~SIGNED64(0x7fffffffff)))+SIGNED64(0x8000000000))
419 /* sign extend a 44 bit number */
420 #define SEXT44(x) ((((x)&SIGNED64(0xfffffffffff))^(~SIGNED64(0x7ffffffffff)))+SIGNED64(0x80000000000))
422 /* sign extend a 56 bit number */
423 #define SEXT56(x) ((((x)&SIGNED64(0xffffffffffffff))^(~SIGNED64(0x7fffffffffffff)))+SIGNED64(0x80000000000000))
425 /* sign extend a 60 bit number */
426 #define SEXT60(x) ((((x)&SIGNED64(0xfffffffffffffff))^(~SIGNED64(0x7ffffffffffffff)))+SIGNED64(0x800000000000000))
428 #define MAX32 SIGNED64(0x7fffffff)
429 #define MIN32 SIGNED64(0xff80000000)
430 #define MASK32 SIGNED64(0xffffffff)
431 #define MASK40 SIGNED64(0xffffffffff)
433 /* The alignment of MOD_E in the following macro depends upon "i"
434 always being a power of 2. */
435 #define INC_ADDR(x,i) \
438 int test_i = i < 0 ? i : ~((i) - 1); \
439 if (PSW_MD && GPR (x) == (MOD_E & test_i)) \
440 SET_GPR (x, MOD_S & test_i); \
442 SET_GPR (x, GPR (x) + (i)); \
446 extern uint8 *dmem_addr (uint16 offset);
447 extern uint8 *imem_addr (uint32);
448 extern bfd_vma decode_pc (void);
450 #define RB(x) (*(dmem_addr(x)))
451 #define SB(addr,data) ( RB(addr) = (data & 0xff))
453 #if defined(__GNUC__) && defined(__OPTIMIZE__) && !defined(NO_ENDIAN_INLINE)
454 #define ENDIAN_INLINE static __inline__
459 extern uint32 get_longword (uint8 *);
460 extern uint16 get_word (uint8 *);
461 extern int64 get_longlong (uint8 *);
462 extern void write_word (uint8 *addr, uint16 data);
463 extern void write_longword (uint8 *addr, uint32 data);
464 extern void write_longlong (uint8 *addr, int64 data);
467 #define SW(addr,data) write_word(dmem_addr(addr),data)
468 #define RW(x) get_word(dmem_addr(x))
469 #define SLW(addr,data) write_longword(dmem_addr(addr),data)
470 #define RLW(x) get_longword(dmem_addr(x))
471 #define READ_16(x) get_word(x)
472 #define WRITE_16(addr,data) write_word(addr,data)
473 #define READ_64(x) get_longlong(x)
474 #define WRITE_64(addr,data) write_longlong(addr,data)
476 #define JMP(x) do { SET_PC (x); State.pc_changed = 1; } while (0)
478 #define RIE_VECTOR_START 0xffc2
479 #define AE_VECTOR_START 0xffc3
480 #define TRAP_VECTOR_START 0xffc4 /* vector for trap 0 */
481 #define DBT_VECTOR_START 0xffd4
482 #define SDBT_VECTOR_START 0xffd5
484 /* Scedule a store of VAL into cr[CR]. MASK indicates the bits in
485 cr[CR] that should not be modified (i.e. cr[CR] = (cr[CR] & MASK) |
486 (VAL & ~MASK)). In addition, unless PSW_HW_P, a VAL intended for
487 PSW is masked for zero bits. */
489 extern reg_t move_to_cr (int cr, reg_t mask, reg_t val, int psw_hw_p);