-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrvbase.c
221 lines (184 loc) · 6.32 KB
/
rvbase.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdint.h>
#include "riscv.h"
#include "rvexec.h"
#include "rvinsn.h"
#ifdef DEFINE_EXTENSION
DEFINE_EXTENSION(I)
#endif
#define SIGN (XWORD_C(1) << XWORD_BIT - 1)
static void illins(struct hart *t, uint_least32_t i) {
trap(t, ILLINS, i);
}
#define MBZ(X) do if (X) { trap(t, ILLINS, i); return; } while (0)
/* FIXME alu/alw duplication */
static void xalu(struct hart *t, uint_least32_t i) {
enum { MASK7 = XWORD_BIT / 32 - 1 }; /* shamt bleeds into funct7 */
unsigned reg = opcode(i) & 0x20;
xword_t in1 = t->ireg[rs1(i)],
in2 = reg ? t->ireg[rs2(i)] : iimm(i),
out;
switch (funct3(i)) {
case 0: out = reg && funct7(i) ? in1 - in2 : in1 + in2; break;
case 1: MBZ(funct7(i) & ~(unsigned)MASK7);
out = in1 << (in2 & XWORD_BIT - 1);
break;
case 2: MBZ(reg && funct7(i)); out = (in1 ^ SIGN) < (in2 ^ SIGN); break;
case 3: MBZ(reg && funct7(i)); out = in1 < in2; break;
case 4: MBZ(reg && funct7(i)); out = in1 ^ in2; break;
case 5: MBZ(funct7(i) & ~(unsigned)MASK7 & ~0x20u);
out = funct7(i) & ~(unsigned)MASK7 && in1 & SIGN
? ~((~in1 & XWORD_MAX) >> (in2 & XWORD_BIT - 1))
: in1 >> (in2 & XWORD_BIT - 1);
break;
case 6: MBZ(reg && funct7(i)); out = in1 | in2; break;
case 7: MBZ(reg && funct7(i)); out = in1 & in2; break;
}
if (rd(i)) t->ireg[rd(i)] = out & XWORD_MAX;
}
__attribute__((alias("xalu"))) execute_t exec04, xops00, xops20;
#if XWORD_BIT > 32
static void walu(struct hart *t, uint_least32_t i) {
unsigned reg = opcode(i) & 0x20;
xword_t in1 = t->ireg[rs1(i)],
in2 = reg ? t->ireg[rs2(i)] : iimm(i),
out;
switch (funct3(i)) {
case 0: out = reg && funct7(i) ? in1 - in2 : in1 + in2; break;
case 1: MBZ(funct7(i)); out = in1 << (in2 & 31); break;
case 5: MBZ(funct7(i) & ~0x20u);
out = funct7(i) && in1 & XWORD_C(1) << 31
? ~((~in1 & (XWORD_C(1) << 32) - 1) >> (in2 & 31))
: (in1 & (XWORD_C(1) << 32) - 1) >> (in2 & 31);
break;
default: trap(t, ILLINS, i); return;
}
out &= (XWORD_C(1) << 32) - 1;
out = (out ^ XWORD_C(1) << 31) - (XWORD_C(1) << 31);
if (rd(i)) t->ireg[rd(i)] = out & XWORD_MAX;
}
__attribute__((alias("walu"))) execute_t exec06, wops00, wops20;
#endif
#define xops00 xops00_ /* defined here */
#define xops20 xops20_ /* defined here */
__attribute__((alias("illins"), weak)) execute_t HEX7(xops);
#undef xops00
#undef xops20
static void xop(struct hart *t, uint_least32_t i) {
static execute_t *const xops[0x80] = { HEX7(&xops) };
(*xops[funct7(i)])(t, i);
}
__attribute__((alias("xop"))) execute_t exec0C;
#if XWORD_BIT > 32
#define wops00 wops00_ /* defined here */
#define wops20 wops20_ /* defined here */
__attribute__((alias("illins"), weak)) execute_t HEX7(wops);
#undef wops00
#undef wops20
static void wop(struct hart *t, uint_least32_t i) {
static execute_t *const wops[0x80] = { HEX7(&wops) };
(*wops[funct7(i)])(t, i);
}
__attribute__((alias("wop"))) execute_t exec0E;
#endif
static void lui(struct hart *t, uint_least32_t i) {
xword_t out = opcode(i) & 0x20 ? uimm(i) : t->pc + uimm(i) & XWORD_MAX;
if (rd(i)) t->ireg[rd(i)] = out;
}
__attribute__((alias("lui"))) execute_t exec05, exec0D;
static void bcc(struct hart *t, uint_least32_t i) {
xword_t in1 = t->ireg[rs1(i)],
in2 = t->ireg[rs2(i)];
int flg;
switch (funct3(i)) {
case 0: flg = in1 == in2; break;
case 1: flg = in1 != in2; break;
case 4: flg = (in1 ^ SIGN) < (in2 ^ SIGN); break;
case 5: flg = (in1 ^ SIGN) >= (in2 ^ SIGN); break;
case 6: flg = in1 < in2; break;
case 7: flg = in1 >= in2; break;
default: trap(t, ILLINS, i); return;
}
if (flg) t->nextpc = t->pc + bimm(i) & XWORD_MAX;
}
__attribute__((alias("bcc"))) execute_t exec18;
static void jal(struct hart *t, uint_least32_t i) {
t->lr = rd(i); t->nextpc = t->pc + jimm(i) & XWORD_MAX;
}
__attribute__((alias("jal"))) execute_t exec1B;
static void jalr(struct hart *t, uint_least32_t i) {
xword_t in = t->ireg[rs1(i)];
t->lr = rd(i); t->nextpc = in + iimm(i) & XWORD_MAX - 1;
}
__attribute__((alias("jalr"))) execute_t exec19;
static void ldr(struct hart *t, uint_least32_t i) {
const unsigned char *restrict m;
xword_t a = t->ireg[rs1(i)] + iimm(i) & XWORD_MAX,
x = 0,
s = 0;
switch (funct3(i)) {
case 0: s = XWORD_C(1) << 7;
case 4: if (!(m = map(t, a, 1, MAPR))) return; goto byte;
case 1: s = XWORD_C(1) << 15;
case 5: if (!(m = map(t, a, 2, MAPR))) return; goto half;
case 2:
#if XWORD_BIT > 32
s = XWORD_C(1) << 31;
case 6:
#endif
/*****/ if (!(m = map(t, a, 4, MAPR))) return; goto word;
#if XWORD_BIT >= 64
case 3: s = XWORD_C(1) << 63;
/*****/ if (!(m = map(t, a, 8, MAPR))) return; goto doub;
doub: x |= (xword_t)m[7] << 56;
x |= (xword_t)m[6] << 48;
x |= (xword_t)m[5] << 40;
x |= (xword_t)m[4] << 32;
#endif
word: x |= (xword_t)m[3] << 24;
x |= (xword_t)m[2] << 16;
half: x |= (xword_t)m[1] << 8;
byte: x |= (xword_t)m[0];
break;
default: trap(t, ILLINS, i); return;
}
if (rd(i)) t->ireg[rd(i)] = (x ^ s) - s & XWORD_MAX;
}
__attribute__((alias("ldr"))) execute_t exec00;
static void str(struct hart *t, uint_least32_t i) {
xword_t a = t->ireg[rs1(i)] + simm(i) & XWORD_MAX,
x = t->ireg[rs2(i)];
unsigned char *restrict m;
switch (funct3(i)) {
case 0: if (!(m = map(t, a, 1, MAPW))) return; goto byte;
case 1: if (!(m = map(t, a, 2, MAPW))) return; goto half;
case 2: if (!(m = map(t, a, 4, MAPW))) return; goto word;
#if XWORD_BIT >= 64
case 3: if (!(m = map(t, a, 8, MAPW))) return; goto doub;
doub: m[7] = x >> 56 & 0xFF;
m[6] = x >> 48 & 0xFF;
m[5] = x >> 40 & 0xFF;
m[4] = x >> 32 & 0xFF;
#endif
word: m[3] = x >> 24 & 0xFF;
m[2] = x >> 16 & 0xFF;
half: m[1] = x >> 8 & 0xFF;
byte: m[0] = x & 0xFF;
break;
default: trap(t, ILLINS, i); return;
}
}
__attribute__((alias("str"))) execute_t exec08;
static void fence(struct hart *t, uint_least32_t i) {
(void)t; (void)i; /* FIXME */
}
__attribute__((alias("fence"))) execute_t memo0, memo1;
#define memo0 memo0_ /* defined here */
#define memo1 memo1_ /* defined here */
__attribute__((alias("illins"), weak)) execute_t HEX3(memo);
#undef memo0
#undef memo1
static void mem(struct hart *t, uint_least32_t i) {
static execute_t *const memo[8] = { HEX3(memo) };
(*memo[funct3(i)])(t, i);
}
__attribute__((alias("mem"))) execute_t exec03;