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 */
48 void (*func
)(SIM_DESC
, SIM_CPU
*);
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
];
233 reg_t regs
[16]; /* general-purpose registers */
234 #define GPR(N) (State.regs[(N)] + 0)
235 #define SET_GPR(N,VAL) SLOT_PEND (State.regs[(N)], (VAL))
237 #define GPR32(N) ((((uint32) State.regs[(N) + 0]) << 16) \
238 | (uint16) State.regs[(N) + 1])
239 #define SET_GPR32(N,VAL) do { SET_GPR (OP[0] + 0, (VAL) >> 16); SET_GPR (OP[0] + 1, (VAL)); } while (0)
241 reg_t cregs
[16]; /* control registers */
242 #define CREG(N) (State.cregs[(N)] + 0)
243 #define SET_CREG(N,VAL) move_to_cr (sd, cpu, (N), 0, (VAL), 0)
244 #define SET_HW_CREG(N,VAL) move_to_cr (sd, cpu, (N), 0, (VAL), 1)
246 reg_t sp
[2]; /* holding area for SPI(0)/SPU(1) */
247 #define HELD_SP(N) (State.sp[(N)] + 0)
248 #define SET_HELD_SP(N,VAL) SLOT_PEND (State.sp[(N)], (VAL))
250 int64 a
[2]; /* accumulators */
251 #define ACC(N) (State.a[(N)] + 0)
252 #define SET_ACC(N,VAL) SLOT_PEND (State.a[(N)], (VAL) & MASK40)
255 struct slot slot
[NR_SLOTS
];
266 /* NOTE: everything below this line is not reset by
267 sim_create_inferior() */
269 struct d10v_memory mem
;
271 enum _ins_type ins_type
;
277 extern struct simops Simops
[];
311 #define PSW CREG (PSW_CR)
312 #define SET_PSW(VAL) SET_CREG (PSW_CR, (VAL))
313 #define SET_HW_PSW(VAL) SET_HW_CREG (PSW_CR, (VAL))
314 #define SET_PSW_BIT(MASK,VAL) move_to_cr (sd, cpu, PSW_CR, ~((reg_t) MASK), (VAL) ? (MASK) : 0, 1)
316 #define PSW_SM ((PSW & PSW_SM_BIT) != 0)
317 #define SET_PSW_SM(VAL) SET_PSW_BIT (PSW_SM_BIT, (VAL))
319 #define PSW_EA ((PSW & PSW_EA_BIT) != 0)
320 #define SET_PSW_EA(VAL) SET_PSW_BIT (PSW_EA_BIT, (VAL))
322 #define PSW_DB ((PSW & PSW_DB_BIT) != 0)
323 #define SET_PSW_DB(VAL) SET_PSW_BIT (PSW_DB_BIT, (VAL))
325 #define PSW_DM ((PSW & PSW_DM_BIT) != 0)
326 #define SET_PSW_DM(VAL) SET_PSW_BIT (PSW_DM_BIT, (VAL))
328 #define PSW_IE ((PSW & PSW_IE_BIT) != 0)
329 #define SET_PSW_IE(VAL) SET_PSW_BIT (PSW_IE_BIT, (VAL))
331 #define PSW_RP ((PSW & PSW_RP_BIT) != 0)
332 #define SET_PSW_RP(VAL) SET_PSW_BIT (PSW_RP_BIT, (VAL))
334 #define PSW_MD ((PSW & PSW_MD_BIT) != 0)
335 #define SET_PSW_MD(VAL) SET_PSW_BIT (PSW_MD_BIT, (VAL))
337 #define PSW_FX ((PSW & PSW_FX_BIT) != 0)
338 #define SET_PSW_FX(VAL) SET_PSW_BIT (PSW_FX_BIT, (VAL))
340 #define PSW_ST ((PSW & PSW_ST_BIT) != 0)
341 #define SET_PSW_ST(VAL) SET_PSW_BIT (PSW_ST_BIT, (VAL))
343 #define PSW_F0 ((PSW & PSW_F0_BIT) != 0)
344 #define SET_PSW_F0(VAL) SET_PSW_BIT (PSW_F0_BIT, (VAL))
346 #define PSW_F1 ((PSW & PSW_F1_BIT) != 0)
347 #define SET_PSW_F1(VAL) SET_PSW_BIT (PSW_F1_BIT, (VAL))
349 #define PSW_C ((PSW & PSW_C_BIT) != 0)
350 #define SET_PSW_C(VAL) SET_PSW_BIT (PSW_C_BIT, (VAL))
352 /* See simopsc.:move_to_cr() for registers that can not be read-from
353 or assigned-to directly */
355 #define PC CREG (PC_CR)
356 #define SET_PC(VAL) SET_CREG (PC_CR, (VAL))
358 #define BPSW CREG (BPSW_CR)
359 #define SET_BPSW(VAL) SET_CREG (BPSW_CR, (VAL))
361 #define BPC CREG (BPC_CR)
362 #define SET_BPC(VAL) SET_CREG (BPC_CR, (VAL))
364 #define DPSW CREG (DPSW_CR)
365 #define SET_DPSW(VAL) SET_CREG (DPSW_CR, (VAL))
367 #define DPC CREG (DPC_CR)
368 #define SET_DPC(VAL) SET_CREG (DPC_CR, (VAL))
370 #define RPT_C CREG (RPT_C_CR)
371 #define SET_RPT_C(VAL) SET_CREG (RPT_C_CR, (VAL))
373 #define RPT_S CREG (RPT_S_CR)
374 #define SET_RPT_S(VAL) SET_CREG (RPT_S_CR, (VAL))
376 #define RPT_E CREG (RPT_E_CR)
377 #define SET_RPT_E(VAL) SET_CREG (RPT_E_CR, (VAL))
379 #define MOD_S CREG (MOD_S_CR)
380 #define SET_MOD_S(VAL) SET_CREG (MOD_S_CR, (VAL))
382 #define MOD_E CREG (MOD_E_CR)
383 #define SET_MOD_E(VAL) SET_CREG (MOD_E_CR, (VAL))
385 #define IBA CREG (IBA_CR)
386 #define SET_IBA(VAL) SET_CREG (IBA_CR, (VAL))
389 #define SIG_D10V_STOP -1
390 #define SIG_D10V_EXIT -2
391 #define SIG_D10V_BUS -3
393 /* TODO: Resolve conflicts with common headers. */
399 #define SEXT3(x) ((((x)&0x7)^(~3))+4)
401 /* sign-extend a 4-bit number */
402 #define SEXT4(x) ((((x)&0xf)^(~7))+8)
404 /* sign-extend an 8-bit number */
405 #define SEXT8(x) ((((x)&0xff)^(~0x7f))+0x80)
407 /* sign-extend a 16-bit number */
408 #define SEXT16(x) ((((x)&0xffff)^(~0x7fff))+0x8000)
410 /* sign-extend a 32-bit number */
411 #define SEXT32(x) ((((x)&SIGNED64(0xffffffff))^(~SIGNED64(0x7fffffff)))+SIGNED64(0x80000000))
413 /* sign extend a 40 bit number */
414 #define SEXT40(x) ((((x)&SIGNED64(0xffffffffff))^(~SIGNED64(0x7fffffffff)))+SIGNED64(0x8000000000))
416 /* sign extend a 44 bit number */
417 #define SEXT44(x) ((((x)&SIGNED64(0xfffffffffff))^(~SIGNED64(0x7ffffffffff)))+SIGNED64(0x80000000000))
419 /* sign extend a 56 bit number */
420 #define SEXT56(x) ((((x)&SIGNED64(0xffffffffffffff))^(~SIGNED64(0x7fffffffffffff)))+SIGNED64(0x80000000000000))
422 /* sign extend a 60 bit number */
423 #define SEXT60(x) ((((x)&SIGNED64(0xfffffffffffffff))^(~SIGNED64(0x7ffffffffffffff)))+SIGNED64(0x800000000000000))
425 #define MAX32 SIGNED64(0x7fffffff)
426 #define MIN32 SIGNED64(0xff80000000)
427 #define MASK32 SIGNED64(0xffffffff)
428 #define MASK40 SIGNED64(0xffffffffff)
430 /* The alignment of MOD_E in the following macro depends upon "i"
431 always being a power of 2. */
432 #define INC_ADDR(x,i) \
435 int test_i = i < 0 ? i : ~((i) - 1); \
436 if (PSW_MD && GPR (x) == (MOD_E & test_i)) \
437 SET_GPR (x, MOD_S & test_i); \
439 SET_GPR (x, GPR (x) + (i)); \
443 extern uint8
*dmem_addr (SIM_DESC
, SIM_CPU
*, uint16 offset
);
444 extern uint8
*imem_addr (SIM_DESC
, SIM_CPU
*, uint32
);
445 extern bfd_vma
decode_pc (void);
447 #define RB(x) (*(dmem_addr (sd, cpu, x)))
448 #define SB(addr,data) ( RB(addr) = (data & 0xff))
450 #if defined(__GNUC__) && defined(__OPTIMIZE__) && !defined(NO_ENDIAN_INLINE)
451 #define ENDIAN_INLINE static __inline__
456 extern uint32
get_longword (uint8
*);
457 extern uint16
get_word (uint8
*);
458 extern int64
get_longlong (uint8
*);
459 extern void write_word (uint8
*addr
, uint16 data
);
460 extern void write_longword (uint8
*addr
, uint32 data
);
461 extern void write_longlong (uint8
*addr
, int64 data
);
464 #define SW(addr,data) write_word (dmem_addr (sd, cpu, addr), data)
465 #define RW(x) get_word (dmem_addr (sd, cpu, x))
466 #define SLW(addr,data) write_longword (dmem_addr (sd, cpu, addr), data)
467 #define RLW(x) get_longword (dmem_addr (sd, cpu, x))
468 #define READ_16(x) get_word(x)
469 #define WRITE_16(addr,data) write_word(addr,data)
470 #define READ_64(x) get_longlong(x)
471 #define WRITE_64(addr,data) write_longlong(addr,data)
473 #define JMP(x) do { SET_PC (x); State.pc_changed = 1; } while (0)
475 #define RIE_VECTOR_START 0xffc2
476 #define AE_VECTOR_START 0xffc3
477 #define TRAP_VECTOR_START 0xffc4 /* vector for trap 0 */
478 #define DBT_VECTOR_START 0xffd4
479 #define SDBT_VECTOR_START 0xffd5
481 /* Scedule a store of VAL into cr[CR]. MASK indicates the bits in
482 cr[CR] that should not be modified (i.e. cr[CR] = (cr[CR] & MASK) |
483 (VAL & ~MASK)). In addition, unless PSW_HW_P, a VAL intended for
484 PSW is masked for zero bits. */
486 extern reg_t
move_to_cr (SIM_DESC
, SIM_CPU
*, int cr
, reg_t mask
, reg_t val
, int psw_hw_p
);