]> Witch of Git - jade-mouse/blob - toolchain/src/cpu/test.rs
Implement instruction execution
[jade-mouse] / toolchain / src / cpu / test.rs
1 use super::*;
2 use crate::inst::{Inst, Reg, Half};
3 use proptest::prelude::*;
4
5 fn cpu() -> impl Strategy<Value = Cpu> {
6 (
7 0..1024u16,
8 [any::<u16>(); 4],
9 prop::collection::vec(any::<u8>(), 0..1024),
10 )
11 .prop_map(|(pc, reg, memory)| Cpu {
12 init: true,
13 pc,
14 reg: RegFile { values: reg },
15 memory,
16 })
17 }
18
19 fn reg() -> impl Strategy<Value = Reg> {
20 (0..4).prop_map(|r| match r {
21 0 => Reg::R0,
22 1 => Reg::R1,
23 2 => Reg::R2,
24 3 => Reg::R3,
25 _ => unreachable!(),
26 })
27 }
28
29 proptest! {
30 #[test]
31 fn run_move(mut cpu in cpu(), dst in reg(), src in reg()) {
32 let inst = Inst::Move(dst, src);
33 let val = cpu.reg(src);
34 cpu.run_inst(&inst);
35 prop_assert_eq!(val, cpu.reg(src));
36 prop_assert_eq!(val, cpu.reg(dst));
37 }
38
39 #[test]
40 fn run_jalr(mut cpu in cpu(), r in reg()) {
41 let inst = Inst::Jalr(r);
42 let dest = cpu.reg(r);
43 let pc0 = cpu.pc;
44 cpu.run_inst(&inst);
45 prop_assert_eq!(pc0, cpu.reg(r));
46 prop_assert_eq!(dest, cpu.pc);
47 }
48
49 #[test]
50 fn run_imm_lohi(mut cpu in cpu(), r in reg(), val: u16) {
51 let [b0, b1] = val.to_le_bytes();
52 cpu.run_inst(&Inst::LdImm(Half::Low, r, b0));
53 cpu.run_inst(&Inst::LdImm(Half::High, r, b1));
54 prop_assert_eq!(val, cpu.reg(r));
55 }
56 }