[pk, sim] added interrupt support to sim; added timer interrupt
[riscv-isa-sim.git] / riscv / decode.h
1 #ifndef _RISCV_DECODE_H
2 #define _RISCV_DECODE_H
3
4 #define __STDC_LIMIT_MACROS
5 #include <stdint.h>
6 typedef int int128_t __attribute__((mode(TI)));
7 typedef unsigned int uint128_t __attribute__((mode(TI)));
8
9 #define support_64bit 1
10 typedef int64_t sreg_t;
11 typedef uint64_t reg_t;
12 typedef uint64_t freg_t;
13
14 const int OPCODE_BITS = 7;
15 const int JTYPE_OPCODE_BITS = 5;
16
17 const int GPR_BITS = 8*sizeof(reg_t);
18 const int GPRID_BITS = 5;
19 const int NGPR = 1 << GPRID_BITS;
20
21 const int FPR_BITS = 64;
22 const int FPRID_BITS = 5;
23 const int NFPR = 1 << FPRID_BITS;
24
25 const int IMM_BITS = 12;
26 const int TARGET_BITS = 27;
27 const int SHAMT_BITS = 6;
28 const int FUNCT_BITS = 3;
29 const int FFUNCT_BITS = 5;
30 const int BIGIMM_BITS = 20;
31 const int BRANCH_ALIGN_BITS = 1;
32 const int JUMP_ALIGN_BITS = 1;
33
34 #define SR_ET 0x0000000000000001ULL
35 #define SR_PS 0x0000000000000004ULL
36 #define SR_S 0x0000000000000008ULL
37 #define SR_EF 0x0000000000000010ULL
38 #define SR_UX 0x0000000000000020ULL
39 #define SR_SX 0x0000000000000040ULL
40 #define SR_IM 0x000000000000FF00ULL
41 #define SR_ZERO ~(SR_ET | SR_PS | SR_S | SR_EF | SR_UX | SR_SX | SR_IM)
42 #define SR_IM_SHIFT 8
43 #define TIMER_IRQ 7
44
45 #define FP_RD_NE 0
46 #define FP_RD_0 1
47 #define FP_RD_DN 2
48 #define FP_RD_UP 3
49 #define FP_RD_NMM 4
50 #define FSR_RD_SHIFT 10
51 #define FSR_RD (0x7 << FSR_RD_SHIFT)
52
53 #define FPEXC_NX 0x01
54 #define FPEXC_UF 0x02
55 #define FPEXC_OF 0x04
56 #define FPEXC_DZ 0x02
57 #define FPEXC_NV 0x10
58
59 #define FSR_AEXC_SHIFT 0
60 #define FSR_NVA (FPEXC_NV << FSR_AEXC_SHIFT)
61 #define FSR_OFA (FPEXC_OF << FSR_AEXC_SHIFT)
62 #define FSR_UFA (FPEXC_UF << FSR_AEXC_SHIFT)
63 #define FSR_DZA (FPEXC_DZ << FSR_AEXC_SHIFT)
64 #define FSR_NXA (FPEXC_NX << FSR_AEXC_SHIFT)
65 #define FSR_AEXC (FSR_NVA | FSR_OFA | FSR_UFA | FSR_DZA | FSR_NXA)
66
67 #define FSR_ZERO ~(FSR_RD | FSR_AEXC)
68
69 // note: bit fields are in little-endian order
70 struct itype_t
71 {
72 unsigned imm : IMM_BITS;
73 unsigned funct : FUNCT_BITS;
74 unsigned rb : GPRID_BITS;
75 unsigned ra : GPRID_BITS;
76 unsigned opcode : OPCODE_BITS;
77 };
78
79 struct jtype_t
80 {
81 unsigned target : TARGET_BITS;
82 unsigned jump_opcode : JTYPE_OPCODE_BITS;
83 };
84
85 struct rtype_t
86 {
87 unsigned rc : GPRID_BITS;
88 unsigned shamt : SHAMT_BITS;
89 unsigned unused : 1;
90 unsigned funct : FUNCT_BITS;
91 unsigned rb : GPRID_BITS;
92 unsigned ra : GPRID_BITS;
93 unsigned opcode : OPCODE_BITS;
94 };
95
96 struct btype_t
97 {
98 unsigned bigimm : BIGIMM_BITS;
99 unsigned rt : GPRID_BITS;
100 unsigned opcode : OPCODE_BITS;
101 };
102
103 struct ftype_t
104 {
105 unsigned rc : FPRID_BITS;
106 unsigned rd : FPRID_BITS;
107 unsigned ffunct : FFUNCT_BITS;
108 unsigned rb : FPRID_BITS;
109 unsigned ra : FPRID_BITS;
110 unsigned opcode : OPCODE_BITS;
111 };
112
113 union insn_t
114 {
115 itype_t itype;
116 jtype_t jtype;
117 rtype_t rtype;
118 btype_t btype;
119 ftype_t ftype;
120 uint32_t bits;
121 };
122
123 // helpful macros, etc
124 #define RA R[insn.rtype.ra]
125 #define RB R[insn.rtype.rb]
126 #define RC R[insn.rtype.rc]
127 #define FRA FR[insn.ftype.ra]
128 #define FRB FR[insn.ftype.rb]
129 #define FRC FR[insn.ftype.rc]
130 #define FRD FR[insn.ftype.rd]
131 #define BIGIMM insn.btype.bigimm
132 #define IMM insn.itype.imm
133 #define SIMM ((int32_t)((uint32_t)insn.itype.imm<<(32-IMM_BITS))>>(32-IMM_BITS))
134 #define SHAMT insn.rtype.shamt
135 #define TARGET insn.jtype.target
136 #define BRANCH_TARGET (pc + (SIMM << BRANCH_ALIGN_BITS))
137 #define JUMP_TARGET ((pc & ~((1<<(TARGET_BITS+JUMP_ALIGN_BITS))-1)) + (TARGET << JUMP_ALIGN_BITS))
138
139 #define require_supervisor if(!(sr & SR_S)) throw trap_privileged_instruction
140 #define require64 if(gprlen != 64) throw trap_illegal_instruction
141 #define require_fp if(!(sr & SR_EF)) throw trap_fp_disabled
142 #define cmp_trunc(reg) (reg_t(reg) << (64-gprlen))
143 #define set_fp_exceptions ({ set_fsr(fsr | \
144 (softfloat_exceptionFlags << FSR_AEXC_SHIFT)); \
145 softfloat_exceptionFlags = 0; })
146
147 static inline sreg_t sext32(int32_t arg)
148 {
149 return arg;
150 }
151
152 #endif