rvsim-core 2.0.0

A cycle-level RISC-V 64-bit system simulator.
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Compressed Instruction (RVC) Decompression Mapping Tests.
//!
//! Verifies that every compressed instruction expands to the correct
//! 32-bit equivalent. Tests cover all three quadrants (Q0, Q1, Q2)
//! and check register mappings, immediate extraction, and edge cases.

use crate::isa::instruction::decode;
use crate::isa::reg::RegIdx;
use crate::isa::rvc::expand;

use crate::isa::encoding::privileged as sys_op;
use crate::isa::encoding::rv64f::opcodes as f_op;
use crate::isa::encoding::rv64i::funct3 as i_f3;
use crate::isa::encoding::rv64i::funct7 as i_f7;
use crate::isa::encoding::rv64i::opcodes as i_op;

/// Expand a 16-bit compressed instruction and decode the resulting 32-bit instruction.
fn expand_and_decode(cinst: u16) -> crate::isa::instruction::Decoded {
    let expanded = expand(cinst);
    assert_ne!(expanded, 0, "Expansion must not produce illegal instruction 0 for {cinst:#06x}");
    decode(expanded)
}

#[test]
fn rvc_c_addi4spn() {
    // C.ADDI4SPN rd', nzuimm → ADDI rd'+8, x2, nzuimm
    let cinst: u16 = 0b0000_1000_0000_0000; // nzuimm=16, rd'=0(x8)
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM);
    assert_eq!(d.rs1, RegIdx::new(2), "C.ADDI4SPN base must be x2 (sp)");
    assert_eq!(d.rd, RegIdx::new(8), "rd' = 0 maps to x8");
    assert_eq!(d.imm, 16);
}

#[test]
fn rvc_c_addi4spn_zero_is_illegal() {
    // nzuimm=0 is reserved (illegal)
    let cinst: u16 = 0b0000_0000_0000_0000;
    let expanded = expand(cinst);
    assert_eq!(expanded, 0, "C.ADDI4SPN with nzuimm=0 must expand to illegal");
}

#[test]
fn rvc_c_lw() {
    // C.LW rd', offset(rs1') → LW rd'+8, offset(rs1'+8)
    let cinst: u16 = 0b0100_0000_0000_0100; // rs1'=0(x8), rd'=1(x9), offset=0
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_LOAD);
    assert_eq!(d.funct3, i_f3::LW);
    assert_eq!(d.rs1, RegIdx::new(8));
    assert_eq!(d.rd, RegIdx::new(9));
}

#[test]
fn rvc_c_ld() {
    // C.LD rd', offset(rs1') → LD rd'+8, offset(rs1'+8)
    let cinst: u16 = 0b0110_0000_0010_0000;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_LOAD);
    assert_eq!(d.funct3, i_f3::LD);
}

#[test]
fn rvc_c_fld() {
    // C.FLD rd', offset(rs1') → FLD rd'+8, offset(rs1'+8)
    let cinst: u16 = 0b0010_0000_0010_0000;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, f_op::OP_LOAD_FP);
    assert_eq!(d.funct3, i_f3::LD);
}

#[test]
fn rvc_c_sw() {
    // C.SW rs2', offset(rs1') → SW rs2'+8, offset(rs1'+8)
    let cinst: u16 = 0b1100_0000_0010_0000;
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, i_op::OP_STORE);
    assert_eq!(d.funct3, i_f3::SW);
}

#[test]
fn rvc_c_sd() {
    // C.SD rs2', offset(rs1') → SD rs2'+8, offset(rs1'+8)
    let cinst: u16 = 0b1110_0000_0010_0000;
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, i_op::OP_STORE);
    assert_eq!(d.funct3, i_f3::SD);
}

#[test]
fn rvc_c_fsd() {
    // C.FSD rs2', offset(rs1') → FSD rs2'+8, offset(rs1'+8)
    let cinst: u16 = 0b1010_0000_0010_0000;
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, f_op::OP_STORE_FP);
}

#[test]
fn rvc_c_addi() {
    // C.ADDI rd, nzimm → ADDI rd, rd, nzimm. Encoded: rd=1(x1), nzimm=1.
    let cinst: u16 = 0b0000_0000_1000_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM);
    assert_eq!(d.funct3, i_f3::ADD_SUB);
    assert_eq!(d.rd, RegIdx::new(1));
    assert_eq!(d.rs1, RegIdx::new(1));
    assert_eq!(d.imm, 1);
}

#[test]
fn rvc_c_addi_negative() {
    // C.ADDI x1, -1: [12]=1, rd=1, imm[4:0]=11111
    let cinst: u16 = 0b0001_0000_1111_1101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.imm, -1);
}

#[test]
fn rvc_c_addiw() {
    // C.ADDIW rd, imm → ADDIW rd, rd, imm
    let cinst: u16 = 0b0010_0010_1000_1101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM_32);
    assert_eq!(d.rd, RegIdx::new(5));
    assert_eq!(d.rs1, RegIdx::new(5));
}

#[test]
fn rvc_c_addiw_rd0_illegal() {
    // C.ADDIW with rd=0 is reserved
    let cinst: u16 = 0b0010_0000_0000_1101;
    let expanded = expand(cinst);
    assert_eq!(expanded, 0);
}

#[test]
fn rvc_c_li() {
    // C.LI rd, imm → ADDI rd, x0, imm
    let cinst: u16 = 0b0100_0001_1001_1101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM);
    assert_eq!(d.rd, RegIdx::new(3));
    assert_eq!(d.rs1, RegIdx::new(0), "C.LI uses x0 as source");
    assert_eq!(d.imm, 7);
}

#[test]
fn rvc_c_addi16sp() {
    // C.ADDI16SP nzimm → ADDI x2, x2, nzimm (rd=2). Encoded: nzimm=16.
    let cinst: u16 = 0b0110_0001_0100_0001; // bit6=1 → nzimm[4]=1 → nzimm=16
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM);
    assert_eq!(d.rd, RegIdx::new(2));
    assert_eq!(d.rs1, RegIdx::new(2));
    assert_eq!(d.imm, 16);
}

#[test]
fn rvc_c_lui() {
    // C.LUI rd, nzimm → LUI rd, nzimm (rd != 0, 2)
    let cinst: u16 = 0b0110_0001_1000_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_LUI);
    assert_eq!(d.rd, RegIdx::new(3));
}

#[test]
fn rvc_c_srli() {
    // C.SRLI rd', shamt → SRLI rd'+8, rd'+8, shamt
    let cinst: u16 = 0b1000_0000_0000_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM);
    assert_eq!(d.funct3, i_f3::SRL_SRA);
    assert_eq!(d.rd, RegIdx::new(8));
}

#[test]
fn rvc_c_srai() {
    // C.SRAI rd', shamt → SRAI rd'+8, rd'+8, shamt
    let cinst: u16 = 0b1000_0100_0000_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM);
    assert_eq!(d.funct3, i_f3::SRL_SRA);
    assert_eq!(d.funct7, i_f7::SRA);
}

#[test]
fn rvc_c_andi() {
    // C.ANDI rd', imm → ANDI rd'+8, rd'+8, imm
    let cinst: u16 = 0b1000_1000_0000_1101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM);
    assert_eq!(d.funct3, i_f3::AND);
    assert_eq!(d.rd, RegIdx::new(8));
}

#[test]
fn rvc_c_sub() {
    // C.SUB rd', rs2' → SUB rd'+8, rd'+8, rs2'+8
    let cinst: u16 = 0b1000_1100_0000_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_REG);
    assert_eq!(d.funct3, i_f3::ADD_SUB);
    assert_eq!(d.funct7, i_f7::SUB);
    assert_eq!(d.rd, RegIdx::new(8));
    assert_eq!(d.rs1, RegIdx::new(8));
    assert_eq!(d.rs2, RegIdx::new(9));
}

#[test]
fn rvc_c_xor() {
    // C.XOR: funct3=100, funct2=11, bit12=0, sub_op=01
    let cinst: u16 = 0b1000_1100_0010_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_REG);
    assert_eq!(d.funct3, i_f3::XOR);
}

#[test]
fn rvc_c_or() {
    // C.OR: funct3=100, funct2=11, bit12=0, sub_op=10
    let cinst: u16 = 0b1000_1100_0100_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_REG);
    assert_eq!(d.funct3, i_f3::OR);
}

#[test]
fn rvc_c_and() {
    // C.AND: funct3=100, funct2=11, bit12=0, sub_op=11
    let cinst: u16 = 0b1000_1100_0110_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_REG);
    assert_eq!(d.funct3, i_f3::AND);
}

#[test]
fn rvc_c_subw() {
    // C.SUBW: funct3=100, funct2=11, bit12=1, sub_op=00
    let cinst: u16 = 0b1001_1100_0000_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_REG_32);
    assert_eq!(d.funct3, i_f3::ADD_SUB);
    assert_eq!(d.funct7, i_f7::SUB);
}

#[test]
fn rvc_c_addw() {
    // C.ADDW: funct3=100, funct2=11, bit12=1, sub_op=01
    let cinst: u16 = 0b1001_1100_0010_0101;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_REG_32);
    assert_eq!(d.funct3, i_f3::ADD_SUB);
    assert_eq!(d.funct7, i_f7::DEFAULT, "ADDW uses funct7=0");
}

#[test]
fn rvc_c_j() {
    // C.J offset → JAL x0, offset.
    let cinst: u16 = 0xA009; // 0b1010_0000_0000_1001: funct3=101, bit3=1, op=01
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, i_op::OP_JAL);
    assert_eq!(d.rd, RegIdx::new(0), "C.J links to x0");
}

#[test]
fn rvc_c_beqz() {
    // C.BEQZ rs1', offset → BEQ rs1'+8, x0, offset
    let cinst: u16 = 0b1100_0000_0000_0101; // rs1'=0(x8), small offset
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, i_op::OP_BRANCH);
    assert_eq!(d.funct3, i_f3::BEQ);
    assert_eq!(d.rs1, RegIdx::new(8));
    assert_eq!(d.rs2, RegIdx::new(0));
}

#[test]
fn rvc_c_bnez() {
    // C.BNEZ rs1', offset → BNE rs1'+8, x0, offset
    let cinst: u16 = 0b1110_0000_0000_0101;
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, i_op::OP_BRANCH);
    assert_eq!(d.funct3, i_f3::BNE);
    assert_eq!(d.rs1, RegIdx::new(8));
}

#[test]
fn rvc_c_slli() {
    // C.SLLI rd, shamt → SLLI rd, rd, shamt
    let cinst: u16 = 0b0000_0000_1001_0010;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_IMM);
    assert_eq!(d.funct3, i_f3::SLL);
    assert_eq!(d.rd, RegIdx::new(1));
    assert_eq!(d.rs1, RegIdx::new(1));
}

#[test]
fn rvc_c_slli_rd0_illegal() {
    // C.SLLI with rd=0 is reserved
    let cinst: u16 = 0b0000_0000_0001_0010;
    let expanded = expand(cinst);
    assert_eq!(expanded, 0);
}

#[test]
fn rvc_c_lwsp() {
    // C.LWSP rd, offset(sp) → LW rd, offset(x2).
    let cinst: u16 = 0b0100_0000_1000_0010; // rd=1, offset=0
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_LOAD);
    assert_eq!(d.funct3, i_f3::LW);
    assert_eq!(d.rd, RegIdx::new(1));
    assert_eq!(d.rs1, RegIdx::new(2), "C.LWSP base is x2 (sp)");
}

#[test]
fn rvc_c_lwsp_rd0_illegal() {
    let cinst: u16 = 0b0100_0000_0000_0010;
    let expanded = expand(cinst);
    assert_eq!(expanded, 0, "C.LWSP with rd=0 is reserved");
}

#[test]
fn rvc_c_ldsp() {
    // C.LDSP rd, offset(sp) → LD rd, offset(x2)
    let cinst: u16 = 0b0110_0000_1000_0010;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_LOAD);
    assert_eq!(d.funct3, i_f3::LD);
    assert_eq!(d.rs1, RegIdx::new(2));
}

#[test]
fn rvc_c_ldsp_rd0_illegal() {
    let cinst: u16 = 0b0110_0000_0000_0010;
    let expanded = expand(cinst);
    assert_eq!(expanded, 0, "C.LDSP with rd=0 is reserved");
}

#[test]
fn rvc_c_fldsp() {
    // C.FLDSP rd, offset(sp) → FLD rd, offset(x2)
    let cinst: u16 = 0b0010_0000_1000_0010;
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, f_op::OP_LOAD_FP);
    assert_eq!(d.funct3, i_f3::LD);
    assert_eq!(d.rs1, RegIdx::new(2));
}

#[test]
fn rvc_c_jr() {
    // C.JR rs1 → JALR x0, rs1, 0 (bit12=0, rs2=0, rs1!=0)
    let cinst: u16 = 0b1000_0010_1000_0010; // rs1=5, rs2=0, bit12=0
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_JALR);
    assert_eq!(d.rd, RegIdx::new(0), "C.JR links to x0");
    assert_eq!(d.rs1, RegIdx::new(5));
}

#[test]
fn rvc_c_mv() {
    // C.MV rd, rs2 → ADD rd, x0, rs2 (bit12=0, rs2!=0)
    let cinst: u16 = 0b1000_0001_1001_0110; // rd=3, rs2=5, bit12=0
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_REG);
    assert_eq!(d.funct3, i_f3::ADD_SUB);
    assert_eq!(d.rd, RegIdx::new(3));
    assert_eq!(d.rs1, RegIdx::new(0), "C.MV uses x0 as rs1");
    assert_eq!(d.rs2, RegIdx::new(5));
}

#[test]
fn rvc_c_ebreak() {
    // C.EBREAK → EBREAK (bit12=1, rs1=0, rs2=0)
    let cinst: u16 = 0b1001_0000_0000_0010;
    let expanded = expand(cinst);
    assert_eq!(expanded, sys_op::EBREAK);
}

#[test]
fn rvc_c_jalr() {
    // C.JALR rs1 → JALR x1, rs1, 0 (bit12=1, rs2=0, rs1!=0)
    let cinst: u16 = 0b1001_0010_1000_0010; // rs1=5
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_JALR);
    assert_eq!(d.rd, RegIdx::new(1), "C.JALR links to x1 (ra)");
    assert_eq!(d.rs1, RegIdx::new(5));
}

#[test]
fn rvc_c_add() {
    // C.ADD rd, rs2 → ADD rd, rd, rs2 (bit12=1, rs2!=0)
    let cinst: u16 = 0b1001_0001_1001_0110; // rd=3, rs2=5
    let d = expand_and_decode(cinst);
    assert_eq!(d.opcode, i_op::OP_REG);
    assert_eq!(d.funct3, i_f3::ADD_SUB);
    assert_eq!(d.funct7, i_f7::DEFAULT);
    assert_eq!(d.rd, RegIdx::new(3));
    assert_eq!(d.rs1, RegIdx::new(3), "C.ADD uses rd as rs1");
    assert_eq!(d.rs2, RegIdx::new(5));
}

#[test]
fn rvc_c_swsp() {
    // C.SWSP rs2, offset(sp) → SW rs2, offset(x2)
    let cinst: u16 = 0b1100_0000_0000_1110; // rs2=3, offset=0
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, i_op::OP_STORE);
    assert_eq!(d.funct3, i_f3::SW);
    assert_eq!(d.rs1, RegIdx::new(2), "C.SWSP base is x2 (sp)");
    assert_eq!(d.rs2, RegIdx::new(3));
}

#[test]
fn rvc_c_sdsp() {
    // C.SDSP rs2, offset(sp) → SD rs2, offset(x2)
    let cinst: u16 = 0b1110_0000_0000_1110; // rs2=3, offset=0
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, i_op::OP_STORE);
    assert_eq!(d.funct3, i_f3::SD);
    assert_eq!(d.rs1, RegIdx::new(2));
}

#[test]
fn rvc_c_fsdsp() {
    // C.FSDSP rs2, offset(sp) → FSD rs2, offset(x2)
    let cinst: u16 = 0b1010_0000_0000_1110; // rs2=3, offset=0
    let expanded = expand(cinst);
    assert_ne!(expanded, 0);
    let d = decode(expanded);
    assert_eq!(d.opcode, f_op::OP_STORE_FP);
    assert_eq!(d.rs1, RegIdx::new(2));
}

#[test]
fn rvc_quadrant_3_is_not_compressed() {
    // bits[1:0] = 11 means 32-bit instruction, not compressed
    let cinst: u16 = 0x0003; // opcode = 0b11
    let expanded = expand(cinst);
    assert_eq!(expanded, 0, "Quadrant 3 (32-bit) should not be handled by RVC expander");
}

#[test]
fn rvc_all_register_mappings_q0() {
    // Verify compressed register rd'=0..7 maps to x8..x15
    for rd_prime in 0u16..8 {
        // C.LW rd', 0(x8) - funct3=010, rs1'=0(x8), offset=0
        let cinst: u16 = 0b0100_0000_0000_0000 | (rd_prime << 2);
        let d = expand_and_decode(cinst);
        assert_eq!(
            d.rd,
            RegIdx::new((8 + rd_prime) as u8),
            "rd'={rd_prime} should map to x{}",
            8 + rd_prime
        );
    }
}

#[test]
fn rvc_all_register_mappings_q0_rs1() {
    // Verify compressed register rs1'=0..7 maps to x8..x15
    for rs1_prime in 0u16..8 {
        // C.LW x8, 0(rs1') - rd'=0, varying rs1'
        let cinst: u16 = 0b0100_0000_0000_0000 | (rs1_prime << 7);
        let d = expand_and_decode(cinst);
        assert_eq!(
            d.rs1,
            RegIdx::new((8 + rs1_prime) as u8),
            "rs1'={rs1_prime} should map to x{}",
            8 + rs1_prime
        );
    }
}