a0e30f5555fea41dfeef723eb7d53f3f12aaa83a
1 /* Simulation code for the CR16 processor.
2 Copyright (C) 2008-2016 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/>. */
26 #include "gdb/callback.h"
27 #include "gdb/remote-sim.h"
30 #include "sim-options.h"
32 #include "gdb/sim-cr16.h"
33 #include "gdb/signals.h"
34 #include "opcode/cr16.h"
41 static struct hash_entry
*lookup_hash (SIM_DESC
, SIM_CPU
*, uint64 ins
, int size
);
42 static void get_operands (operand_desc
*s
, uint64 mcode
, int isize
, int nops
);
48 struct hash_entry
*next
;
56 struct hash_entry hash_table
[MAX_HASH
+1];
59 hash(unsigned long long insn
, int format
)
61 unsigned int i
= 4, tmp
;
64 while ((insn
>> i
) != 0) i
+=4;
66 return ((insn
>> (i
-4)) & 0xf); /* Use last 4 bits as hask key. */
68 return ((insn
& 0xF)); /* Use last 4 bits as hask key. */
72 INLINE
static struct hash_entry
*
73 lookup_hash (SIM_DESC sd
, SIM_CPU
*cpu
, uint64 ins
, int size
)
78 h
= &hash_table
[hash(ins
,1)];
81 mask
= (((1 << (32 - h
->mask
)) -1) << h
->mask
);
83 /* Adjuest mask for branch with 2 word instructions. */
84 if ((h
->ops
->mnimonic
!= NULL
) &&
85 ((streq(h
->ops
->mnimonic
,"b") && h
->size
== 2)))
89 while ((ins
& mask
) != (BIN(h
->opcode
, h
->mask
)))
92 sim_engine_halt (sd
, cpu
, NULL
, PC
, sim_stopped
, SIM_SIGILL
);
95 mask
= (((1 << (32 - h
->mask
)) -1) << h
->mask
);
96 /* Adjuest mask for branch with 2 word instructions. */
97 if ((streq(h
->ops
->mnimonic
,"b")) && h
->size
== 2)
105 get_operands (operand_desc
*s
, uint64 ins
, int isize
, int nops
)
107 uint32 i
, opn
= 0, start_bit
= 0, op_type
= 0;
108 int32 op_size
= 0, mask
= 0;
110 if (isize
== 1) /* Trunkcate the extra 16 bits of INS. */
113 for (i
=0; i
< 4; ++i
,++opn
)
115 if (s
[opn
].op_type
== dummy
) break;
117 op_type
= s
[opn
].op_type
;
118 start_bit
= s
[opn
].shift
;
119 op_size
= cr16_optab
[op_type
].bit_size
;
123 case imm3
: case imm4
: case imm5
: case imm6
:
126 OP
[i
] = ((ins
>> 4) & ((1 << op_size
) -1));
128 OP
[i
] = ((ins
>> (32 - start_bit
)) & ((1 << op_size
) -1));
130 if (OP
[i
] & ((long)1 << (op_size
-1)))
133 OP
[i
] = ~(OP
[i
]) + 1;
135 OP
[i
] = (unsigned long int)(OP
[i
] & (((long)1 << op_size
) -1));
139 case uimm3
: case uimm3_1
: case uimm4_1
:
143 OP
[i
] = ((ins
>> 4) & ((1 << op_size
) -1)); break;
145 OP
[i
] = ((ins
>> (32 - start_bit
)) & ((1 << op_size
) -1));break;
146 default: /* for case 3. */
147 OP
[i
] = ((ins
>> (16 + start_bit
)) & ((1 << op_size
) -1)); break;
157 OP
[i
] = ((ins
>> 4) & ((1 << op_size
) -1));
159 OP
[i
] = (ins
& ((1 << op_size
) -1));
162 OP
[i
] = ((ins
>> start_bit
) & ((1 << op_size
) -1));
165 OP
[i
] = ((ins
>> (start_bit
+ 16)) & ((1 << op_size
) -1));
168 OP
[i
] = ((ins
>> start_bit
) & ((1 << op_size
) -1));
173 case imm16
: case uimm16
:
174 OP
[i
] = ins
& 0xFFFF;
177 case uimm20
: case imm20
:
178 OP
[i
] = ins
& (((long)1 << op_size
) - 1);
181 case imm32
: case uimm32
:
182 OP
[i
] = ins
& 0xFFFFFFFF;
185 case uimm5
: break; /*NOT USED. */
186 OP
[i
] = ins
& ((1 << op_size
) - 1); break;
189 OP
[i
] = (ins
>> 4) & ((1 << 4) - 1);
190 OP
[i
] = (OP
[i
] * 2) + 2;
191 if (OP
[i
] & ((long)1 << 5))
194 OP
[i
] = ~(OP
[i
]) + 1;
195 OP
[i
] = (unsigned long int)(OP
[i
] & 0x1F);
200 OP
[i
] = ((((ins
>> 8) & 0xf) << 4) | (ins
& 0xf));
202 if (OP
[i
] & ((long)1 << 8))
205 OP
[i
] = ~(OP
[i
]) + 1;
206 OP
[i
] = (unsigned long int)(OP
[i
] & 0xFF);
211 OP
[i
] = (ins
& 0xFFFF);
214 OP
[i
] = (OP
[i
] & 0xFFFE);
216 OP
[i
] = ~(OP
[i
]) + 1;
217 OP
[i
] = (unsigned long int)(OP
[i
] & 0xFFFF);
223 OP
[i
] = (ins
& 0xFFFFFF);
225 OP
[i
] = (ins
& 0xFFFF) | (((ins
>> 24) & 0xf) << 16) |
226 (((ins
>> 16) & 0xf) << 20);
230 OP
[i
] = (OP
[i
] & 0xFFFFFE);
232 OP
[i
] = ~(OP
[i
]) + 1;
233 OP
[i
] = (unsigned long int)(OP
[i
] & 0xFFFFFF);
239 OP
[i
] = (ins
) & 0xFFFFF;
241 OP
[i
] = (ins
>> start_bit
) & 0xFFFFF;
245 OP
[i
] = ((ins
& 0xFFFF) | (((ins
>> 16) & 0xf) << 20)
246 | (((ins
>> 24) & 0xf) << 16));
248 OP
[i
] = (ins
>> 16) & 0xFFFFFF;
252 case rbase
: break; /* NOT USED. */
253 case rbase_disps20
: case rbase_dispe20
:
254 case rpbase_disps20
: case rpindex_disps20
:
255 OP
[i
] = ((((ins
>> 24)&0xf) << 16)|((ins
) & 0xFFFF));
256 OP
[++i
] = (ins
>> 16) & 0xF; /* get 4 bit for reg. */
259 OP
[i
] = 0; /* 4 bit disp const. */
260 OP
[++i
] = (ins
) & 0xF; /* get 4 bit for reg. */
263 OP
[i
] = ((ins
>> 8) & 0xF) * 2; /* 4 bit disp const. */
264 OP
[++i
] = (ins
) & 0xF; /* get 4 bit for reg. */
267 OP
[i
] = ((ins
>> 8) & 0xF); /* 4 bit disp const. */
268 OP
[++i
] = (ins
) & 0xF; /* get 4 bit for reg. */
271 OP
[i
] = (ins
) & 0xFFFF;
272 OP
[++i
] = (ins
>> 16) & 0xF; /* get 4 bit for reg. */
276 OP
[++i
] = (ins
>> 4) & 0xF; /* get 4 bit for reg. */
277 OP
[++i
] = (ins
>> 8) & 0x1; /* get 1 bit for index-reg. */
279 case rpindex_disps14
:
280 OP
[i
] = (ins
) & 0x3FFF;
281 OP
[++i
] = (ins
>> 14) & 0x1; /* get 1 bit for index-reg. */
282 OP
[++i
] = (ins
>> 16) & 0xF; /* get 4 bit for reg. */
285 OP
[i
] = (ins
) & 0xFFFFF;
286 OP
[++i
] = (ins
>> 24) & 0x1; /* get 1 bit for index-reg. */
287 OP
[++i
] = (ins
>> 20) & 0xF; /* get 4 bit for reg. */
289 case regr
: case regp
: case pregr
: case pregrp
:
293 if (start_bit
== 20) OP
[i
] = (ins
>> 4) & 0xF;
294 else if (start_bit
== 16) OP
[i
] = ins
& 0xF;
296 case 2: OP
[i
] = (ins
>> start_bit
) & 0xF; break;
297 case 3: OP
[i
] = (ins
>> (start_bit
+ 16)) & 0xF; break;
302 if (isize
== 1) OP
[i
] = (ins
>> 4) & 0xF;
303 else if (isize
== 2) OP
[i
] = (ins
>> start_bit
) & 0xF;
304 else OP
[i
] = (ins
>> (start_bit
+ 16)) & 0xF;
310 /* For ESC on uimm4_1 operand. */
311 if (op_type
== uimm4_1
)
315 /* For increment by 1. */
316 if ((op_type
== pregr
) || (op_type
== pregrp
))
319 /* FIXME: for tracing, update values that need to be updated each
320 instruction decode cycle */
321 State
.trace
.psw
= PSR
;
325 do_run (SIM_DESC sd
, SIM_CPU
*cpu
, uint64 mcode
)
327 struct hash_entry
*h
;
330 if ((cr16_debug
& DEBUG_INSTRUCTION
) != 0)
331 sim_io_printf (sd
, "do_long 0x%x\n", mcode
);
334 h
= lookup_hash (sd
, cpu
, mcode
, 1);
336 if ((h
== NULL
) || (h
->opcode
== 0))
340 mcode
= (mcode
<< 16) | RW (PC
+ 4);
342 /* Re-set OP list. */
343 OP
[0] = OP
[1] = OP
[2] = OP
[3] = sign_flag
= 0;
345 /* for push/pop/pushrtn with RA instructions. */
346 if ((h
->format
& REG_LIST
) && (mcode
& 0x800000))
347 OP
[2] = 1; /* Set 1 for RA operand. */
349 /* numops == 0 means, no operands. */
350 if (((h
->ops
) != NULL
) && (((h
->ops
)->numops
) != 0))
351 get_operands ((h
->ops
)->operands
, mcode
, h
->size
, (h
->ops
)->numops
);
353 //State.ins_type = h->flags;
355 (h
->ops
->func
) (sd
, cpu
);
361 cr16_pc_get (sim_cpu
*cpu
)
367 cr16_pc_set (sim_cpu
*cpu
, sim_cia pc
)
369 SIM_DESC sd
= CPU_STATE (cpu
);
374 free_state (SIM_DESC sd
)
376 if (STATE_MODULES (sd
) != NULL
)
377 sim_module_uninstall (sd
);
378 sim_cpu_free_all (sd
);
382 static int cr16_reg_fetch (SIM_CPU
*, int, unsigned char *, int);
383 static int cr16_reg_store (SIM_CPU
*, int, unsigned char *, int);
386 sim_open (SIM_OPEN_KIND kind
, struct host_callback_struct
*cb
, struct bfd
*abfd
, char **argv
)
389 struct hash_entry
*h
;
390 static int init_p
= 0;
393 SIM_DESC sd
= sim_state_alloc (kind
, cb
);
394 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
396 /* The cpu data is kept in a separately allocated chunk of memory. */
397 if (sim_cpu_alloc_all (sd
, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK
)
403 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
409 /* getopt will print the error message so we just have to exit if this fails.
410 FIXME: Hmmm... in the case of gdb we need getopt to call
412 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
418 /* Check for/establish the a reference program image. */
419 if (sim_analyze_program (sd
,
420 (STATE_PROG_ARGV (sd
) != NULL
421 ? *STATE_PROG_ARGV (sd
)
422 : NULL
), abfd
) != SIM_RC_OK
)
428 /* Configure/verify the target byte order and other runtime
429 configuration options. */
430 if (sim_config (sd
) != SIM_RC_OK
)
432 sim_module_uninstall (sd
);
436 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
438 /* Uninstall the modules to avoid memory leaks,
439 file descriptor leaks, etc. */
440 sim_module_uninstall (sd
);
444 /* CPU specific initialization. */
445 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
447 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
449 CPU_REG_FETCH (cpu
) = cr16_reg_fetch
;
450 CPU_REG_STORE (cpu
) = cr16_reg_store
;
451 CPU_PC_FETCH (cpu
) = cr16_pc_get
;
452 CPU_PC_STORE (cpu
) = cr16_pc_set
;
455 /* The CR16 has an interrupt controller at 0xFC00, but we don't currently
456 handle that. Revisit if anyone ever implements operating mode. */
457 /* cr16 memory: There are three separate cr16 memory regions IMEM,
458 UMEM and DMEM. The IMEM and DMEM are further broken down into
459 blocks (very like VM pages). This might not match the hardware,
460 but it matches what the toolchain currently expects. Ugh. */
461 sim_do_commandf (sd
, "memory-size %#x", 20 * 1024 * 1024);
463 /* put all the opcodes in the hash table. */
466 for (s
= Simops
; s
->func
; s
++)
471 h
= &hash_table
[hash(s
->opcode
, 0)];
475 if (((s
->opcode
<< 1) >> 4) != 0)
476 h
= &hash_table
[hash((s
->opcode
<< 1) >> 4, 0)];
478 h
= &hash_table
[hash((s
->opcode
<< 1), 0)];
482 if ((s
->opcode
>> 4) != 0)
483 h
= &hash_table
[hash(s
->opcode
>> 4, 0)];
485 h
= &hash_table
[hash(s
->opcode
, 0)];
489 if (((s
->opcode
>> 1) >> 4) != 0)
490 h
= &hash_table
[hash((s
->opcode
>>1) >> 4, 0)];
492 h
= &hash_table
[hash((s
->opcode
>> 1), 0)];
496 if ((s
->opcode
>> 8) != 0)
497 h
= &hash_table
[hash(s
->opcode
>> 8, 0)];
498 else if ((s
->opcode
>> 4) != 0)
499 h
= &hash_table
[hash(s
->opcode
>> 4, 0)];
501 h
= &hash_table
[hash(s
->opcode
, 0)];
505 if ((s
->opcode
>> 8) != 0)
506 h
= &hash_table
[hash(s
->opcode
>> 8, 0)];
507 else if ((s
->opcode
>> 4) != 0)
508 h
= &hash_table
[hash(s
->opcode
>> 4, 0)];
510 h
= &hash_table
[hash(s
->opcode
, 0)];
514 if (((s
->opcode
>> 1) >> 8) != 0)
515 h
= &hash_table
[hash((s
->opcode
>>1) >> 8, 0)];
516 else if (((s
->opcode
>> 1) >> 4) != 0)
517 h
= &hash_table
[hash((s
->opcode
>>1) >> 4, 0)];
519 h
= &hash_table
[hash((s
->opcode
>>1), 0)];
523 if ((s
->opcode
>> 0xc) != 0)
524 h
= &hash_table
[hash(s
->opcode
>> 12, 0)];
525 else if ((s
->opcode
>> 8) != 0)
526 h
= &hash_table
[hash(s
->opcode
>> 8, 0)];
527 else if ((s
->opcode
>> 4) != 0)
528 h
= &hash_table
[hash(s
->opcode
>> 4, 0)];
530 h
= &hash_table
[hash(s
->opcode
, 0)];
534 if ((s
->opcode
>> 16) != 0)
535 h
= &hash_table
[hash(s
->opcode
>> 16, 0)];
536 else if ((s
->opcode
>> 12) != 0)
537 h
= &hash_table
[hash(s
->opcode
>> 12, 0)];
538 else if ((s
->opcode
>> 8) != 0)
539 h
= &hash_table
[hash(s
->opcode
>> 8, 0)];
540 else if ((s
->opcode
>> 4) != 0)
541 h
= &hash_table
[hash(s
->opcode
>> 4, 0)];
543 h
= &hash_table
[hash(s
->opcode
, 0)];
549 /* go to the last entry in the chain. */
555 h
->next
= (struct hash_entry
*) calloc(1,sizeof(struct hash_entry
));
557 perror ("malloc failure");
563 h
->opcode
= s
->opcode
;
564 h
->format
= s
->format
;
573 step_once (SIM_DESC sd
, SIM_CPU
*cpu
)
575 uint32 curr_ins_size
= 0;
576 uint64 mcode
= RLW (PC
);
578 State
.pc_changed
= 0;
580 curr_ins_size
= do_run (sd
, cpu
, mcode
);
583 sim_io_printf (sd
, "INS: PC=0x%X, mcode=0x%X\n", PC
, mcode
);
586 if (curr_ins_size
== 0)
587 sim_engine_halt (sd
, cpu
, NULL
, PC
, sim_exited
, GPR (2));
588 else if (!State
.pc_changed
)
589 SET_PC (PC
+ (curr_ins_size
* 2)); /* For word instructions. */
592 /* Check for a breakpoint trap on this instruction. This
593 overrides any pending branches or loops */
594 if (PSR_DB
&& PC
== DBS
)
598 SET_PC (SDBT_VECTOR_START
);
602 /* Writeback all the DATA / PC changes */
607 sim_engine_run (SIM_DESC sd
,
608 int next_cpu_nr
, /* ignore */
609 int nr_cpus
, /* ignore */
614 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
616 cpu
= STATE_CPU (sd
, 0);
623 case GDB_SIGNAL_SEGV
:
626 JMP (AE_VECTOR_START
);
632 SET_HW_PSR ((PSR
& (PSR_C_BIT
)));
633 JMP (RIE_VECTOR_START
);
644 if (sim_events_tick (sd
))
645 sim_events_process (sd
);
650 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
, char **argv
, char **env
)
652 bfd_vma start_address
;
654 /* reset all state information */
655 memset (&State
, 0, sizeof (State
));
657 /* There was a hack here to copy the values of argc and argv into r0
658 and r1. The values were also saved into some high memory that
659 won't be overwritten by the stack (0x7C00). The reason for doing
660 this was to allow the 'run' program to accept arguments. Without
661 the hack, this is not possible anymore. If the simulator is run
662 from the debugger, arguments cannot be passed in, so this makes
667 start_address
= bfd_get_start_address (abfd
);
672 sim_io_printf (sd
, "sim_create_inferior: PC=0x%lx\n", (long) start_address
);
675 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
676 SET_CREG (PC_CR
, start_address
);
684 cr16_extract_unsigned_integer (unsigned char *addr
, int len
)
688 unsigned char * startaddr
= (unsigned char *)addr
;
689 unsigned char * endaddr
= startaddr
+ len
;
693 for (p
= endaddr
; p
> startaddr
;)
694 retval
= (retval
<< 8) | *--p
;
700 cr16_store_unsigned_integer (unsigned char *addr
, int len
, uint32 val
)
703 unsigned char *startaddr
= addr
;
704 unsigned char *endaddr
= startaddr
+ len
;
706 for (p
= startaddr
; p
< endaddr
;)
714 cr16_reg_fetch (SIM_CPU
*cpu
, int rn
, unsigned char *memory
, int length
)
717 switch ((enum sim_cr16_regs
) rn
)
719 case SIM_CR16_R0_REGNUM
:
720 case SIM_CR16_R1_REGNUM
:
721 case SIM_CR16_R2_REGNUM
:
722 case SIM_CR16_R3_REGNUM
:
723 case SIM_CR16_R4_REGNUM
:
724 case SIM_CR16_R5_REGNUM
:
725 case SIM_CR16_R6_REGNUM
:
726 case SIM_CR16_R7_REGNUM
:
727 case SIM_CR16_R8_REGNUM
:
728 case SIM_CR16_R9_REGNUM
:
729 case SIM_CR16_R10_REGNUM
:
730 case SIM_CR16_R11_REGNUM
:
731 cr16_store_unsigned_integer (memory
, 2, GPR (rn
- SIM_CR16_R0_REGNUM
));
734 case SIM_CR16_R12_REGNUM
:
735 case SIM_CR16_R13_REGNUM
:
736 case SIM_CR16_R14_REGNUM
:
737 case SIM_CR16_R15_REGNUM
:
738 cr16_store_unsigned_integer (memory
, 4, GPR (rn
- SIM_CR16_R0_REGNUM
));
741 case SIM_CR16_PC_REGNUM
:
742 case SIM_CR16_ISP_REGNUM
:
743 case SIM_CR16_USP_REGNUM
:
744 case SIM_CR16_INTBASE_REGNUM
:
745 case SIM_CR16_PSR_REGNUM
:
746 case SIM_CR16_CFG_REGNUM
:
747 case SIM_CR16_DBS_REGNUM
:
748 case SIM_CR16_DCR_REGNUM
:
749 case SIM_CR16_DSR_REGNUM
:
750 case SIM_CR16_CAR0_REGNUM
:
751 case SIM_CR16_CAR1_REGNUM
:
752 cr16_store_unsigned_integer (memory
, 4, CREG (rn
- SIM_CR16_PC_REGNUM
));
763 cr16_reg_store (SIM_CPU
*cpu
, int rn
, unsigned char *memory
, int length
)
765 SIM_DESC sd
= CPU_STATE (cpu
);
767 switch ((enum sim_cr16_regs
) rn
)
769 case SIM_CR16_R0_REGNUM
:
770 case SIM_CR16_R1_REGNUM
:
771 case SIM_CR16_R2_REGNUM
:
772 case SIM_CR16_R3_REGNUM
:
773 case SIM_CR16_R4_REGNUM
:
774 case SIM_CR16_R5_REGNUM
:
775 case SIM_CR16_R6_REGNUM
:
776 case SIM_CR16_R7_REGNUM
:
777 case SIM_CR16_R8_REGNUM
:
778 case SIM_CR16_R9_REGNUM
:
779 case SIM_CR16_R10_REGNUM
:
780 case SIM_CR16_R11_REGNUM
:
781 SET_GPR (rn
- SIM_CR16_R0_REGNUM
, cr16_extract_unsigned_integer (memory
, 2));
784 case SIM_CR16_R12_REGNUM
:
785 case SIM_CR16_R13_REGNUM
:
786 case SIM_CR16_R14_REGNUM
:
787 case SIM_CR16_R15_REGNUM
:
788 SET_GPR32 (rn
- SIM_CR16_R0_REGNUM
, cr16_extract_unsigned_integer (memory
, 2));
791 case SIM_CR16_PC_REGNUM
:
792 case SIM_CR16_ISP_REGNUM
:
793 case SIM_CR16_USP_REGNUM
:
794 case SIM_CR16_INTBASE_REGNUM
:
795 case SIM_CR16_PSR_REGNUM
:
796 case SIM_CR16_CFG_REGNUM
:
797 case SIM_CR16_DBS_REGNUM
:
798 case SIM_CR16_DCR_REGNUM
:
799 case SIM_CR16_DSR_REGNUM
:
800 case SIM_CR16_CAR0_REGNUM
:
801 case SIM_CR16_CAR1_REGNUM
:
802 SET_CREG (rn
- SIM_CR16_PC_REGNUM
, cr16_extract_unsigned_integer (memory
, 4));
This page took 0.048154 seconds and 4 git commands to generate.