1 /* Simulation code for the CR16 processor.
2 Copyright (C) 2008-2023 Free Software Foundation, Inc.
3 Contributed by M Ranga Swami Reddy <MR.Swami.Reddy@nsc.com>
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "sim/callback.h"
26 #include "opcode/cr16.h"
30 #define DEBUG_TRACE 0x00000001
31 #define DEBUG_VALUES 0x00000002
32 #define DEBUG_LINE_NUMBER 0x00000004
33 #define DEBUG_MEMSIZE 0x00000008
34 #define DEBUG_INSTRUCTION 0x00000010
35 #define DEBUG_TRAP 0x00000020
36 #define DEBUG_MEMORY 0x00000040
39 #define DEBUG (DEBUG_TRACE | DEBUG_VALUES | DEBUG_LINE_NUMBER)
42 extern int cr16_debug
;
45 #include "sim-config.h"
46 #include "sim-types.h"
48 /* FIXME: CR16 defines */
49 typedef uint16_t reg_t
;
50 typedef uint32_t creg_t
;
60 void (*func
)(SIM_DESC
, SIM_CPU
*);
62 operand_desc operands
[4];
67 INS_UNKNOWN
, /* unknown instruction */
80 extern unsigned long ins_type_counters
[ (int)INS_MAX
];
86 /* Write-back slots */
101 #define SLOT (State.slot)
102 #define SLOT_NR (State.slot_nr)
103 #define SLOT_PEND_MASK(DEST, MSK, VAL) \
106 SLOT[SLOT_NR].dest = &(DEST); \
107 SLOT[SLOT_NR].size = sizeof (DEST); \
108 switch (sizeof (DEST)) \
111 SLOT[SLOT_NR].data._1 = (unsigned_1) (VAL); \
112 SLOT[SLOT_NR].mask._1 = (unsigned_1) (MSK); \
115 SLOT[SLOT_NR].data._2 = (unsigned_2) (VAL); \
116 SLOT[SLOT_NR].mask._2 = (unsigned_2) (MSK); \
119 SLOT[SLOT_NR].data._4 = (unsigned_4) (VAL); \
120 SLOT[SLOT_NR].mask._4 = (unsigned_4) (MSK); \
123 SLOT_NR = (SLOT_NR + 1); \
126 #define SLOT_PEND(DEST, VAL) SLOT_PEND_MASK(DEST, 0, VAL)
127 #define SLOT_DISCARD() (SLOT_NR = 0)
128 #define SLOT_FLUSH() \
132 for (i = 0; i < SLOT_NR; i++) \
134 switch (SLOT[i].size) \
137 *(unsigned_1*) SLOT[i].dest &= SLOT[i].mask._1; \
138 *(unsigned_1*) SLOT[i].dest |= SLOT[i].data._1; \
141 *(unsigned_2*) SLOT[i].dest &= SLOT[i].mask._2; \
142 *(unsigned_2*) SLOT[i].dest |= SLOT[i].data._2; \
145 *(unsigned_4*) SLOT[i].dest &= SLOT[i].mask._4; \
146 *(unsigned_4*) SLOT[i].dest |= SLOT[i].data._4; \
153 #define SLOT_DUMP() \
157 for (i = 0; i < SLOT_NR; i++) \
159 switch (SLOT[i].size) \
162 printf ("SLOT %d *0x%08lx & 0x%02x | 0x%02x\n", i, \
163 (long) SLOT[i].dest, \
164 (unsigned) SLOT[i].mask._1, \
165 (unsigned) SLOT[i].data._1); \
168 printf ("SLOT %d *0x%08lx & 0x%04x | 0x%04x\n", i, \
169 (long) SLOT[i].dest, \
170 (unsigned) SLOT[i].mask._2, \
171 (unsigned) SLOT[i].data._2); \
174 printf ("SLOT %d *0x%08lx & 0x%08x | 0x%08x\n", i, \
175 (long) SLOT[i].dest, \
176 (unsigned) SLOT[i].mask._4, \
177 (unsigned) SLOT[i].data._4); \
180 printf ("SLOT %d *0x%08lx & 0x%08x%08x | 0x%08x%08x\n", i, \
181 (long) SLOT[i].dest, \
182 (unsigned) (SLOT[i].mask._8 >> 32), \
183 (unsigned) SLOT[i].mask._8, \
184 (unsigned) (SLOT[i].data._8 >> 32), \
185 (unsigned) SLOT[i].data._8); \
194 creg_t regs
[16]; /* general-purpose registers */
195 #define GPR(N) (State.regs[(N)] + 0)
196 #define SET_GPR(N,VAL) (State.regs[(N)] = (VAL))
200 ((((uint16_t) State.regs[(N) + 1]) << 16) | (uint16_t) State.regs[(N)]) \
203 #define SET_GPR32(N,VAL) do { \
205 { SET_GPR (N + 1, (VAL) >> 16); SET_GPR (N, ((VAL) & 0xffff));} \
206 else { if ( N == 11) \
207 { SET_GPR (N + 1, ((GPR32 (12)) & 0xffff0000)|((VAL) >> 16)); \
208 SET_GPR (N, ((VAL) & 0xffff));} \
209 else SET_GPR (N, (VAL));} \
212 creg_t cregs
[16]; /* control registers */
213 #define CREG(N) (State.cregs[(N)] + 0)
214 #define SET_CREG(N,VAL) move_to_cr (sd, cpu, (N), 0, (VAL), 0)
215 #define SET_HW_CREG(N,VAL) move_to_cr (sd, cpu, (N), 0, (VAL), 1)
217 reg_t sp
[2]; /* holding area for SPI(0)/SPU(1) */
218 #define HELD_SP(N) (State.sp[(N)] + 0)
219 #define SET_HELD_SP(N,VAL) SLOT_PEND (State.sp[(N)], (VAL))
222 struct slot slot
[NR_SLOTS
];
232 /* NOTE: everything below this line is not reset by
233 sim_create_inferior() */
235 enum _ins_type ins_type
;
239 extern struct _state State
;
242 extern uint32_t OP
[4];
243 extern uint32_t sign_flag
;
244 extern struct simops Simops
[];
275 #define PSR CREG (PSR_CR)
276 #define SET_PSR(VAL) SET_CREG (PSR_CR, (VAL))
277 #define SET_HW_PSR(VAL) SET_HW_CREG (PSR_CR, (VAL))
278 #define SET_PSR_BIT(MASK,VAL) move_to_cr (sd, cpu, PSR_CR, ~((creg_t) MASK), (VAL) ? (MASK) : 0, 1)
280 #define PSR_SM ((PSR & PSR_SM_BIT) != 0)
281 #define SET_PSR_SM(VAL) SET_PSR_BIT (PSR_SM_BIT, (VAL))
283 #define PSR_I ((PSR & PSR_I_BIT) != 0)
284 #define SET_PSR_I(VAL) SET_PSR_BIT (PSR_I_BIT, (VAL))
286 #define PSR_DB ((PSR & PSR_DB_BIT) != 0)
287 #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))
292 #define PSR_E ((PSR & PSR_E_BIT) != 0)
293 #define SET_PSR_E(VAL) SET_PSR_BIT (PSR_E_BIT, (VAL))
295 #define PSR_N ((PSR & PSR_N_BIT) != 0)
296 #define SET_PSR_N(VAL) SET_PSR_BIT (PSR_N_BIT, (VAL))
298 #define PSR_Z ((PSR & PSR_Z_BIT) != 0)
299 #define SET_PSR_Z(VAL) SET_PSR_BIT (PSR_Z_BIT, (VAL))
301 #define PSR_F ((PSR & PSR_F_BIT) != 0)
302 #define SET_PSR_F(VAL) SET_PSR_BIT (PSR_F_BIT, (VAL))
304 #define PSR_U ((PSR & PSR_U_BIT) != 0)
305 #define SET_PSR_U(VAL) SET_PSR_BIT (PSR_U_BIT, (VAL))
307 #define PSR_L ((PSR & PSR_L_BIT) != 0)
308 #define SET_PSR_L(VAL) SET_PSR_BIT (PSR_L_BIT, (VAL))
310 #define PSR_T ((PSR & PSR_T_BIT) != 0)
311 #define SET_PSR_T(VAL) SET_PSR_BIT (PSR_T_BIT, (VAL))
313 #define PSR_C ((PSR & PSR_C_BIT) != 0)
314 #define SET_PSR_C(VAL) SET_PSR_BIT (PSR_C_BIT, (VAL))
316 /* See simopsc.:move_to_cr() for registers that can not be read-from
317 or assigned-to directly */
319 #define PC CREG (PC_CR)
320 #define SET_PC(VAL) SET_CREG (PC_CR, (VAL))
321 //#define SET_PC(VAL) (State.cregs[PC_CR] = (VAL))
323 #define BPSR CREG (BPSR_CR)
324 #define SET_BPSR(VAL) SET_CREG (BPSR_CR, (VAL))
326 #define BPC CREG (BPC_CR)
327 #define SET_BPC(VAL) SET_CREG (BPC_CR, (VAL))
329 #define DPSR CREG (DPSR_CR)
330 #define SET_DPSR(VAL) SET_CREG (DPSR_CR, (VAL))
332 #define DPC CREG (DPC_CR)
333 #define SET_DPC(VAL) SET_CREG (DPC_CR, (VAL))
335 #define RPT_C CREG (RPT_C_CR)
336 #define SET_RPT_C(VAL) SET_CREG (RPT_C_CR, (VAL))
338 #define RPT_S CREG (RPT_S_CR)
339 #define SET_RPT_S(VAL) SET_CREG (RPT_S_CR, (VAL))
341 #define RPT_E CREG (RPT_E_CR)
342 #define SET_RPT_E(VAL) SET_CREG (RPT_E_CR, (VAL))
344 #define MOD_S CREG (MOD_S_CR)
345 #define SET_MOD_S(VAL) SET_CREG (MOD_S_CR, (VAL))
347 #define MOD_E CREG (MOD_E_CR)
348 #define SET_MOD_E(VAL) SET_CREG (MOD_E_CR, (VAL))
350 #define IBA CREG (IBA_CR)
351 #define SET_IBA(VAL) SET_CREG (IBA_CR, (VAL))
354 #define SIG_CR16_STOP -1
355 #define SIG_CR16_EXIT -2
356 #define SIG_CR16_BUS -3
357 #define SIG_CR16_IAD -4
359 /* TODO: Resolve conflicts with common headers. */
364 #define SEXT3(x) ((((x)&0x7)^(~3))+4)
366 /* sign-extend a 4-bit number */
367 #define SEXT4(x) ((((x)&0xf)^(~7))+8)
369 /* sign-extend an 8-bit number */
370 #define SEXT8(x) ((((x)&0xff)^(~0x7f))+0x80)
372 /* sign-extend a 16-bit number */
373 #define SEXT16(x) ((((x)&0xffff)^(~0x7fff))+0x8000)
375 /* sign-extend a 24-bit number */
376 #define SEXT24(x) ((((x)&0xffffff)^(~0x7fffff))+0x800000)
378 /* sign-extend a 32-bit number */
379 #define SEXT32(x) ((((x)&0xffffffff)^(~0x7fffffff))+0x80000000)
381 #define SB(addr, data) sim_core_write_1 (cpu, PC, read_map, addr, data)
382 #define RB(addr) sim_core_read_1 (cpu, PC, read_map, addr)
383 #define SW(addr, data) sim_core_write_unaligned_2 (cpu, PC, read_map, addr, data)
384 #define RW(addr) sim_core_read_unaligned_2 (cpu, PC, read_map, addr)
385 #define SLW(addr, data) sim_core_write_unaligned_4 (cpu, PC, read_map, addr, data)
387 /* Yes, this is as whacked as it looks. The sim currently reads little endian
388 for 16 bits, but then merge them like big endian to get 32 bits. */
389 static inline uint32_t get_longword (SIM_CPU
*cpu
, address_word addr
)
391 return (RW (addr
) << 16) | RW (addr
+ 2);
393 #define RLW(addr) get_longword (cpu, addr)
395 #define JMP(x) do { SET_PC (x); State.pc_changed = 1; } while (0)
397 #define RIE_VECTOR_START 0xffc2
398 #define AE_VECTOR_START 0xffc3
399 #define TRAP_VECTOR_START 0xffc4 /* vector for trap 0 */
400 #define DBT_VECTOR_START 0xffd4
401 #define SDBT_VECTOR_START 0xffd5
403 #define INT_VECTOR_START 0xFFFE00 /*maskable interrupt - mapped to ICU */
404 #define NMI_VECTOR_START 0xFFFF00 /*non-maskable interrupt;for observability*/
405 #define ISE_VECTOR_START 0xFFFC00 /*in-system emulation trap */
406 #define ADBG_VECTOR_START 0xFFFC02 /*alternate debug trap */
407 #define ATRC_VECTOR_START 0xFFFC0C /*alternate trace trap */
408 #define ABPT_VECTOR_START 0xFFFC0E /*alternate break point trap */
411 /* Scedule a store of VAL into cr[CR]. MASK indicates the bits in
412 cr[CR] that should not be modified (i.e. cr[CR] = (cr[CR] & MASK) |
413 (VAL & ~MASK)). In addition, unless PSR_HW_P, a VAL intended for
414 PSR is masked for zero bits. */
416 extern creg_t
move_to_cr (SIM_DESC
, SIM_CPU
*, int cr
, creg_t mask
, creg_t val
, int psw_hw_p
);
421 /* Special purpose trap */
422 #define TRAP_BREAKPOINT 8