-
Notifications
You must be signed in to change notification settings - Fork 0
/
bad_cpu.ucl
469 lines (446 loc) · 17.1 KB
/
bad_cpu.ucl
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// CPU spec that deliberately SHOULD FAIL most sanity tests
// when an `addi` instruction (needed to set up most tests) is executed, the sum is always computed
// to be 0
module cpu {
type * = common.*;
define * = common.*;
type * = instructions.*;
define * = instructions.*;
const * = encdec.*;
define * = encdec.*;
input imem : mem_t;
var dmem : mem_t;
var regfile : regfile_t;
var pc : pc_t;
var exception : exception_t; // placeholder for more complicated trap handling
const PC_START : pc_t = 0x80000000bv32;
const IMEM_PC_START : mem_word_addr_t = 0x20000000bv30;
define get_a0() : reg_data_t = regfile[registers.a0]; // a0 contains the exit code for the exit syscall
// ===== BEGIN STATE HELPER FUNCTIONS =====
procedure set_exception(cause : exception_cause_t)
modifies exception;
{
exception.cause = cause;
exception.epc = pc;
}
procedure do_reg_write(rd : reg_addr_t, value : reg_data_t)
modifies regfile;
{
if (rd != registers.x0) {
regfile[rd] = value;
}
}
procedure do_mem_read(addr : reg_data_t, width : mem_data_width_t, sext : boolean)
returns (new_rd_val : reg_data_t)
modifies exception;
{
var word_addr : mem_word_addr_t;
var word_ofs : bv2;
var shifted_data : mem_data_t;
word_addr = addr[31:2];
word_ofs = addr[1:0];
// Shift word by appropriate offset
// Assumes little endianness (LSB is at byte 0 of word, MSB at byte 3)
shifted_data = bv_a_right_shift(bv_left_shift(3bv32, bv_zero_extend(30, word_ofs)), dmem[word_addr]);
// Alignment check
if ((width == HALF && word_ofs[0:0] != 0bv1) || (width == WORD && word_ofs != 0bv2)) {
call set_exception(X_MISALIGNED_MEM);
} else {
case
(width == BYTE) : { new_rd_val = if (sext) then bv_sign_extend(24, shifted_data[7:0]) else bv_zero_extend(24, shifted_data[7:0]); }
(width == HALF) : { new_rd_val = if (sext) then bv_sign_extend(16, shifted_data[15:0]) else bv_zero_extend(16, shifted_data[15:0]); }
(width == WORD) : { new_rd_val = shifted_data; }
esac
}
}
procedure do_mem_write(addr : reg_data_t, width : mem_data_width_t, value : reg_data_t)
modifies dmem, exception;
{
// TODO handle alignment
var word_addr : mem_word_addr_t;
var bit_ofs: bv32;
var word_ofs : bv2;
var old_word : mem_data_t;
var new_mem_data : mem_data_t;
word_addr = addr[31:2];
word_ofs = addr[1:0];
bit_ofs = bv_left_shift(3bv32, bv_zero_extend(30, addr[1:0]));
old_word = dmem[word_addr];
if ((width == HALF && word_ofs[0:0] != 0bv1) || (width == WORD && word_ofs != 0bv2)) {
call set_exception(X_MISALIGNED_MEM);
} else {
case
(width == BYTE) : {
old_word = old_word & (~bv_left_shift(bit_ofs, 0xFFbv32));
new_mem_data = old_word | bv_left_shift(bit_ofs, value & 0xFFbv32);
}
(width == HALF) : {
old_word = old_word & (~bv_left_shift(bit_ofs, 0xFFFFbv32));
new_mem_data = old_word | bv_left_shift(bit_ofs, value & 0xFFFFbv32);
}
(width == WORD) : { new_mem_data = old_word | bv_left_shift(bit_ofs, value); }
esac
dmem[word_addr] = new_mem_data;
}
}
// ===== END STATE HELPER FUNCTIONS =====
// ===== BEGIN INSTRUCTION EXECUTE FUNCTIONS =====
procedure exec_i_arith(args : itype_arith_t)
modifies regfile, pc;
{
var imm_ext : reg_data_t;
var i : itype_arith_inst_t;
var rs1_val : reg_data_t;
var new_rd_val : reg_data_t;
imm_ext = bv_sign_extend(20, args.imm);
i = args.inst;
rs1_val = regfile[args.rs1];
case
(i == ADDI) : { new_rd_val = 0bv32; } // BROKE HERE
// FIX: bv_left_shift seems not to type check until it reaches the smt backend
(i == SLTI) : { new_rd_val = if (rs1_val < imm_ext) then 1bv32 else 0bv32; }
(i == SLTIU) : { new_rd_val = if (rs1_val <_u imm_ext) then 1bv32 else 0bv32; }
(i == XORI) : { new_rd_val = rs1_val ^ imm_ext; }
(i == ORI) : { new_rd_val = rs1_val | imm_ext; }
(i == ANDI) : { new_rd_val = rs1_val & imm_ext; }
esac
call do_reg_write(args.rd, new_rd_val);
pc = pc + 4bv32;
}
procedure exec_i_shift(args : itype_shift_t)
modifies regfile, pc;
{
var shamt_padded : reg_data_t;
var i : itype_shift_inst_t;
var rs1_val : reg_data_t;
var new_rd_val : reg_data_t;
shamt_padded = bv_zero_extend(27, args.shamt);
i = args.inst;
rs1_val = regfile[args.rs1];
case
(i == SLLI) : { new_rd_val = bv_left_shift(shamt_padded, rs1_val); }
(i == SRAI) : { new_rd_val = bv_a_right_shift(shamt_padded, rs1_val); }
(i == SRLI) : { new_rd_val = bv_l_right_shift(shamt_padded, rs1_val); }
esac
call do_reg_write(args.rd, new_rd_val);
pc = pc + 4bv32;
}
procedure exec_i_mem(args : itype_mem_t)
modifies regfile, pc, exception;
{
var addr : reg_data_t;
var i : itype_mem_inst_t;
var new_rd_val : reg_data_t;
addr = regfile[args.rs1] + bv_sign_extend(20, args.imm);
i = args.inst;
case
(i == LB) : { call (new_rd_val) = do_mem_read(addr, BYTE, true); }
(i == LBU) : { call (new_rd_val) = do_mem_read(addr, BYTE, false); }
(i == LH) : { call (new_rd_val) = do_mem_read(addr, HALF, true); }
(i == LHU) : { call (new_rd_val) = do_mem_read(addr, HALF, false); }
(i == LW) : { call (new_rd_val) = do_mem_read(addr, WORD, true); }
esac
call do_reg_write(args.rd, new_rd_val);
pc = pc + 4bv32;
}
procedure exec_s(args : stype_t)
modifies dmem, pc, exception;
{
// Returns memory value shifted/masked appropriately
var addr : reg_data_t;
var i : stype_inst_t;
addr = regfile[args.rs1] + bv_sign_extend(20, args.imm);
i = args.inst;
case
(i == SB) : { call do_mem_write(addr, BYTE, regfile[args.rs2]); }
(i == SH) : { call do_mem_write(addr, HALF, regfile[args.rs2]); }
(i == SW) : { call do_mem_write(addr, WORD, regfile[args.rs2]); }
esac
pc = pc + 4bv32;
}
procedure exec_r(args : rtype_t)
modifies regfile, pc;
{
var i : rtype_inst_t;
var new_rd_val : reg_data_t;
var rs1_val : reg_data_t;
var rs2_val : reg_data_t;
i = args.inst;
rs1_val = regfile[args.rs1];
rs2_val = regfile[args.rs2];
case
(i == ADD) : { new_rd_val = rs1_val + rs2_val; }
(i == SUB) : { new_rd_val = rs1_val - rs2_val; }
(i == SLL) : { new_rd_val = bv_left_shift(rs2_val, rs1_val); }
(i == SLT) : { new_rd_val = if (rs1_val < rs2_val) then 1bv32 else 0bv32; }
(i == SLTU) : { new_rd_val = if (rs1_val <_u rs2_val) then 1bv32 else 0bv32; }
(i == XOR) : { new_rd_val = rs1_val ^ rs2_val; }
(i == SRL) : { new_rd_val = bv_l_right_shift(rs2_val, rs1_val); }
(i == SRA) : { new_rd_val = bv_a_right_shift(rs2_val, rs1_val); }
(i == OR) : { new_rd_val = rs1_val | rs2_val; }
(i == AND) : { new_rd_val = rs1_val & rs2_val; }
esac
call do_reg_write(args.rd, new_rd_val);
pc = pc + 4bv32;
}
procedure exec_b(args : btype_t)
modifies pc, exception;
{
var i : btype_inst_t;
var b_tgt : pc_t;
var taken : boolean;
var rs1_val : reg_data_t;
var rs2_val : reg_data_t;
i = args.inst;
rs1_val = regfile[args.rs1];
rs2_val = regfile[args.rs2];
b_tgt = pc + bv_sign_extend(19, args.imm);
if (b_tgt[1:0] != 0bv2) {
call set_exception(X_MISALIGNED_INST);
pc = pc + 4bv32;
} else {
case
(i == BEQ) : { taken = rs1_val == rs2_val; }
(i == BNE) : { taken = rs1_val != rs2_val; }
(i == BLT) : { taken = rs1_val < rs2_val; }
(i == BGE) : { taken = rs1_val >= rs2_val; }
(i == BLTU) : { taken = rs1_val <_u rs2_val; }
(i == BGEU) : { taken = rs1_val >=_u rs2_val; }
esac
pc = if (taken) then b_tgt else pc + 4bv32;
}
}
procedure exec_u(args : utype_t)
modifies regfile, pc;
{
var i : utype_inst_t;
var new_rd_val : reg_data_t;
i = args.inst;
case
(i == AUIPC) : { new_rd_val = pc + args.imm; }
(i == LUI) : { new_rd_val = args.imm; }
esac
call do_reg_write(args.rd, new_rd_val);
pc = pc + 4bv32;
}
procedure exec_jal(args : jal_t)
modifies regfile, pc, exception;
{
var tgt_pc : pc_t;
tgt_pc = pc + bv_sign_extend(11, args.imm);
if (tgt_pc[1:0] != 0bv2) {
call set_exception(X_MISALIGNED_INST);
pc = pc + 4bv32;
} else {
call do_reg_write(args.rd, pc + 4bv32);
pc = tgt_pc;
}
}
procedure exec_jalr(args : jalr_t)
modifies regfile, pc, exception;
{
var tgt_pc : pc_t;
tgt_pc = regfile[args.rs1] + bv_sign_extend(20, args.imm);
if (tgt_pc[1:0] != 0bv2) {
call set_exception(X_MISALIGNED_INST);
pc = pc + 4bv32;
} else {
call do_reg_write(args.rd, pc + 4bv32);
pc = tgt_pc;
}
}
procedure exec_system(args : system_t)
modifies exception;
{
var i : system_inst_t;
i = args.inst;
case
(i == ECALL) : { call set_exception(X_ECALL); }
(i == EBREAK) : { call set_exception(X_EBREAK); }
esac
}
// ===== END INSTRUCTION EXECUTE FUNCTIONS =====
// Decodes and executes an instruction from IMEM
procedure exec_next_inst()
modifies pc, regfile, dmem, exception;
{
var inst : inst_t;
var oc : bv7;
var rs1 : bv5;
var rs2 : bv5;
var rd : bv5;
var f3 : bv3;
var f7 : bv7;
var i_imm : bv12;
var i_shift_inst : itype_shift_inst_t;
var i_mem_inst : itype_mem_inst_t;
var s_inst : stype_inst_t;
inst = imem[pc_to_mem_addr(pc)];
oc = get_opcode(inst);
rs1 = get_rs1(inst);
rs2 = get_rs2(inst);
rd = get_rd(inst);
f3 = get_f3(inst);
f7 = get_f7(inst);
i_imm = get_i_imm(inst);
case
(oc == OPCODE_I_ARITH) : {
if (f3 == F3_SLLI || f3 == F3_SRI) {
var args : itype_shift_t;
args.rs1 = rs1;
args.rd = rd;
args.shamt = i_imm[4:0];
case
(f3 == F3_SLLI) : { args.inst = SLLI; }
(f7[5:5] == 0bv1) : { args.inst = SRLI; }
(f7[5:5] == 1bv1) : { args.inst = SRAI; }
default: { call set_exception(X_INVALID_INST); }
esac
if (exception.cause != X_INVALID_INST) {
call exec_i_shift(args);
}
} else {
var args : itype_arith_t;
args.rs1 = rs1;
args.rd = rd;
args.imm = i_imm;
case
(f3 == F3_ADDI) : { args.inst = ADDI; }
(f3 == F3_SLTI) : { args.inst = SLTI; }
(f3 == F3_SLTIU) : { args.inst = SLTIU; }
(f3 == F3_XORI) : { args.inst = XORI; }
(f3 == F3_ORI) : { args.inst = ORI; }
(f3 == F3_ANDI) : { args.inst = ANDI; }
default: { call set_exception(X_INVALID_INST); }
esac
if (exception.cause != X_INVALID_INST) {
call exec_i_arith(args);
}
}
}
(oc == OPCODE_I_MEM) : {
var args : itype_mem_t;
args.rd = rd;
args.rs1 = rs1;
args.imm = i_imm;
case
(f3 == F3_LB) : { args.inst = LB; }
(f3 == F3_LBU) : { args.inst = LBU; }
(f3 == F3_LH) : { args.inst = LH; }
(f3 == F3_LHU) : { args.inst = LHU; }
(f3 == F3_LW) : { args.inst = LW; }
default: { call set_exception(X_INVALID_INST); }
esac
if (exception.cause != X_INVALID_INST) {
call exec_i_mem(args);
}
}
(oc == OPCODE_R) : {
var args : rtype_t;
args.rd = rd;
args.rs1 = rs1;
args.rs2 = rs2;
case
(f3 == F3_ADD) : { args.inst = if (f7[5:5] == 0bv1) then ADD else SUB; }
(f3 == F3_SLL) : { args.inst = SLL; }
(f3 == F3_SLT) : { args.inst = SLT; }
(f3 == F3_SLTU) : { args.inst = SLTU; }
(f3 == F3_XOR) : { args.inst = XOR; }
(f3 == F3_SR) : { args.inst = if (f7[5:5] == 0bv1) then SRL else SRA; }
(f3 == F3_OR) : { args.inst = OR; }
(f3 == F3_AND) : { args.inst = AND; }
default: { call set_exception(X_INVALID_INST); }
esac
if (exception.cause != X_INVALID_INST) {
call exec_r(args);
}
}
(oc == OPCODE_S) : {
var args : stype_t;
args.rs1 = rs1;
args.rs2 = rs2;
args.imm = get_s_imm(inst);
case
(f3 == F3_SB) : { args.inst = SB; }
(f3 == F3_SH) : { args.inst = SH; }
(f3 == F3_SW) : { args.inst = SW; }
default: { call set_exception(X_INVALID_INST); }
esac
if (exception.cause != X_INVALID_INST) {
call exec_s(args);
}
}
(oc == OPCODE_B) : {
var args : btype_t;
args.rs1 = rs1;
args.rs2 = rs2;
args.imm = get_b_imm(inst);
case
(f3 == F3_BEQ) : { args.inst = BEQ; }
(f3 == F3_BNE) : { args.inst = BNE; }
(f3 == F3_BLT) : { args.inst = BLT; }
(f3 == F3_BGE) : { args.inst = BGE; }
(f3 == F3_BLTU) : { args.inst = BLTU; }
(f3 == F3_BGEU) : { args.inst = BGEU; }
default: { call set_exception(X_INVALID_INST); }
esac
if (exception.cause != X_INVALID_INST) {
call exec_b(args);
}
}
(oc == OPCODE_AUIPC || oc == OPCODE_LUI) : {
var args : utype_t;
args.rd = rd;
args.imm = get_u_imm(inst);
case
(oc == OPCODE_AUIPC) : { args.inst = AUIPC; }
(oc == OPCODE_LUI) : { args.inst = LUI; }
default: { call set_exception(X_INVALID_INST); }
esac
if (exception.cause != X_INVALID_INST) {
call exec_u(args);
}
}
(oc == OPCODE_JAL) : {
var args : jal_t;
args.rd = rd;
args.imm = get_j_imm(inst);
call exec_jal(args);
}
(oc == OPCODE_JALR) : {
var args : jalr_t;
args.rd = rd;
args.rs1 = rs1;
args.imm = get_i_imm(inst);
call exec_jalr(args);
}
(oc == OPCODE_SYS) : {
var args : system_t;
case
(i_imm == 0bv12) : { args.inst = ECALL; }
(i_imm == 1bv12) : { args.inst = EBREAK; }
default: { call set_exception(X_INVALID_INST); }
esac
call exec_system(args);
}
(oc == OPCODE_FENCE) : {
var args : fence_t;
// nop
pc = pc + 4bv32;
}
default : { call set_exception(X_INVALID_INST); }
esac
}
init {
assume (forall (r : reg_addr_t) :: regfile[r] == 0bv32);
assume (forall (a : mem_word_addr_t) :: dmem[a] == 0bv32);
// assume (regfile[registers.x0] == 0bv32);
pc = PC_START;
exception.cause = X_NONE; // epc doesn't matter
}
next {
if (exception.cause == X_NONE) {
call exec_next_inst();
}
}
}