riscv: fix tests

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2023-12-29 23:17:10 +08:00
parent 3c85f46824
commit f296aaf6ee

View File

@ -3817,42 +3817,44 @@ mod test_cpu {
#[test] #[test]
fn tick() { fn tick() {
let mut cpu = create_cpu(); let mut cpu = create_cpu();
let memory_base = cpu.memory_base();
cpu.get_mut_mmu().init_memory(4); cpu.get_mut_mmu().init_memory(4);
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
// Write non-compressed "addi x1, x1, 1" instruction // Write non-compressed "addi x1, x1, 1" instruction
match cpu.get_mut_mmu().store_word(cpu.memory_base(), 0x00108093) { match cpu.get_mut_mmu().store_word(memory_base, 0x00108093) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
// Write compressed "addi x8, x0, 8" instruction // Write compressed "addi x8, x0, 8" instruction
match cpu.get_mut_mmu().store_word(cpu.memory_base() + 4, 0x20) { match cpu.get_mut_mmu().store_word(memory_base + 4, 0x20) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
cpu.tick(); cpu.tick();
assert_eq!(cpu.memory_base() + 4, cpu.read_pc()); assert_eq!(memory_base + 4, cpu.read_pc());
assert_eq!(1, cpu.read_register(1)); assert_eq!(1, cpu.read_register(1));
cpu.tick(); cpu.tick();
assert_eq!(cpu.memory_base() + 6, cpu.read_pc()); assert_eq!(memory_base + 6, cpu.read_pc());
assert_eq!(8, cpu.read_register(8)); assert_eq!(8, cpu.read_register(8));
} }
#[test] #[test]
fn tick_operate() { fn tick_operate() {
let mut cpu = create_cpu(); let mut cpu = create_cpu();
let memory_base = cpu.memory_base();
cpu.get_mut_mmu().init_memory(4); cpu.get_mut_mmu().init_memory(4);
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
// write non-compressed "addi a0, a0, 12" instruction // write non-compressed "addi a0, a0, 12" instruction
match cpu.get_mut_mmu().store_word(cpu.memory_base(), 0xc50513) { match cpu.get_mut_mmu().store_word(memory_base, 0xc50513) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
assert_eq!(cpu.memory_base(), cpu.read_pc()); assert_eq!(memory_base, cpu.read_pc());
assert_eq!(0, cpu.read_register(10)); assert_eq!(0, cpu.read_register(10));
match cpu.tick_operate() { match cpu.tick_operate() {
Ok(()) => {} Ok(()) => {}
@ -3860,7 +3862,7 @@ mod test_cpu {
}; };
// .tick_operate() increments the program counter by 4 for // .tick_operate() increments the program counter by 4 for
// non-compressed instruction. // non-compressed instruction.
assert_eq!(cpu.memory_base() + 4, cpu.read_pc()); assert_eq!(memory_base + 4, cpu.read_pc());
// "addi a0, a0, a12" instruction writes 12 to a0 register. // "addi a0, a0, a12" instruction writes 12 to a0 register.
assert_eq!(12, cpu.read_register(10)); assert_eq!(12, cpu.read_register(10));
// @TODO: Test compressed instruction operation // @TODO: Test compressed instruction operation
@ -3873,9 +3875,10 @@ mod test_cpu {
// .fetch() doesn't increment the program counter. // .fetch() doesn't increment the program counter.
// .tick_operate() does. // .tick_operate() does.
let mut cpu = create_cpu(); let mut cpu = create_cpu();
let memory_base = cpu.memory_base();
cpu.get_mut_mmu().init_memory(4); cpu.get_mut_mmu().init_memory(4);
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
match cpu.get_mut_mmu().store_word(cpu.memory_base(), 0xaaaaaaaa) { match cpu.get_mut_mmu().store_word(memory_base, 0xaaaaaaaa) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
@ -3883,7 +3886,7 @@ mod test_cpu {
Ok(data) => assert_eq!(0xaaaaaaaa, data), Ok(data) => assert_eq!(0xaaaaaaaa, data),
Err(_e) => panic!("Failed to fetch"), Err(_e) => panic!("Failed to fetch"),
}; };
match cpu.get_mut_mmu().store_word(cpu.memory_base(), 0x55555555) { match cpu.get_mut_mmu().store_word(memory_base, 0x55555555) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
@ -3926,28 +3929,29 @@ mod test_cpu {
fn wfi() { fn wfi() {
let wfi_instruction = 0x10500073; let wfi_instruction = 0x10500073;
let mut cpu = create_cpu(); let mut cpu = create_cpu();
let memory_base = cpu.memory_base();
// Just in case // Just in case
match cpu.decode(wfi_instruction) { match cpu.decode(wfi_instruction) {
Ok(inst) => assert_eq!(inst.name, "WFI"), Ok(inst) => assert_eq!(inst.name, "WFI"),
Err(_e) => panic!("Failed to decode"), Err(_e) => panic!("Failed to decode"),
}; };
cpu.get_mut_mmu().init_memory(4); cpu.get_mut_mmu().init_memory(4);
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
// write WFI instruction // write WFI instruction
match cpu match cpu
.get_mut_mmu() .get_mut_mmu()
.store_word(cpu.memory_base(), wfi_instruction) .store_word(memory_base, wfi_instruction)
{ {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
cpu.tick(); cpu.tick();
assert_eq!(cpu.memory_base() + 4, cpu.read_pc()); assert_eq!(memory_base + 4, cpu.read_pc());
for _i in 0..10 { for _i in 0..10 {
// Until interrupt happens, .tick() does nothing // Until interrupt happens, .tick() does nothing
// @TODO: Check accurately that the state is unchanged // @TODO: Check accurately that the state is unchanged
cpu.tick(); cpu.tick();
assert_eq!(cpu.memory_base() + 4, cpu.read_pc()); assert_eq!(memory_base + 4, cpu.read_pc());
} }
// Machine timer interrupt // Machine timer interrupt
cpu.write_csr_raw(CSR_MIE_ADDRESS, MIP_MTIP); cpu.write_csr_raw(CSR_MIE_ADDRESS, MIP_MTIP);
@ -3963,13 +3967,14 @@ mod test_cpu {
fn interrupt() { fn interrupt() {
let handler_vector = 0x10000000; let handler_vector = 0x10000000;
let mut cpu = create_cpu(); let mut cpu = create_cpu();
let memory_base = cpu.memory_base();
cpu.get_mut_mmu().init_memory(4); cpu.get_mut_mmu().init_memory(4);
// Write non-compressed "addi x0, x0, 1" instruction // Write non-compressed "addi x0, x0, 1" instruction
match cpu.get_mut_mmu().store_word(cpu.memory_base(), 0x00100013) { match cpu.get_mut_mmu().store_word(memory_base, 0x00100013) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
// Machine timer interrupt but mie in mstatus is not enabled yet // Machine timer interrupt but mie in mstatus is not enabled yet
cpu.write_csr_raw(CSR_MIE_ADDRESS, MIP_MTIP); cpu.write_csr_raw(CSR_MIE_ADDRESS, MIP_MTIP);
@ -3979,9 +3984,9 @@ mod test_cpu {
cpu.tick(); cpu.tick();
// Interrupt isn't caught because mie is disabled // Interrupt isn't caught because mie is disabled
assert_eq!(cpu.memory_base() + 4, cpu.read_pc()); assert_eq!(memory_base + 4, cpu.read_pc());
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
// Enable mie in mstatus // Enable mie in mstatus
cpu.write_csr_raw(CSR_MSTATUS_ADDRESS, 0x8); cpu.write_csr_raw(CSR_MSTATUS_ADDRESS, 0x8);
@ -4004,14 +4009,15 @@ mod test_cpu {
fn exception() { fn exception() {
let handler_vector = 0x10000000; let handler_vector = 0x10000000;
let mut cpu = create_cpu(); let mut cpu = create_cpu();
let memory_base = cpu.memory_base();
cpu.get_mut_mmu().init_memory(4); cpu.get_mut_mmu().init_memory(4);
// Write ECALL instruction // Write ECALL instruction
match cpu.get_mut_mmu().store_word(cpu.memory_base(), 0x00000073) { match cpu.get_mut_mmu().store_word(memory_base, 0x00000073) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
cpu.write_csr_raw(CSR_MTVEC_ADDRESS, handler_vector); cpu.write_csr_raw(CSR_MTVEC_ADDRESS, handler_vector);
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
cpu.tick(); cpu.tick();
@ -4030,18 +4036,19 @@ mod test_cpu {
#[test] #[test]
fn hardocded_zero() { fn hardocded_zero() {
let mut cpu = create_cpu(); let mut cpu = create_cpu();
let memory_base = cpu.memory_base();
cpu.get_mut_mmu().init_memory(8); cpu.get_mut_mmu().init_memory(8);
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
// Write non-compressed "addi x0, x0, 1" instruction // Write non-compressed "addi x0, x0, 1" instruction
match cpu.get_mut_mmu().store_word(cpu.memory_base(), 0x00100013) { match cpu.get_mut_mmu().store_word(memory_base, 0x00100013) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
// Write non-compressed "addi x1, x1, 1" instruction // Write non-compressed "addi x1, x1, 1" instruction
match cpu match cpu
.get_mut_mmu() .get_mut_mmu()
.store_word(cpu.memory_base() + 4, 0x00108093) .store_word(memory_base + 4, 0x00108093)
{ {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
@ -4063,11 +4070,12 @@ mod test_cpu {
#[test] #[test]
fn disassemble_next_instruction() { fn disassemble_next_instruction() {
let mut cpu = create_cpu(); let mut cpu = create_cpu();
let memory_base = cpu.memory_base();
cpu.get_mut_mmu().init_memory(4); cpu.get_mut_mmu().init_memory(4);
cpu.update_pc(cpu.memory_base()); cpu.update_pc(memory_base);
// Write non-compressed "addi x0, x0, 1" instruction // Write non-compressed "addi x0, x0, 1" instruction
match cpu.get_mut_mmu().store_word(cpu.memory_base(), 0x00100013) { match cpu.get_mut_mmu().store_word(memory_base, 0x00100013) {
Ok(()) => {} Ok(()) => {}
Err(_e) => panic!("Failed to store"), Err(_e) => panic!("Failed to store"),
}; };
@ -4078,7 +4086,7 @@ mod test_cpu {
); );
// No effect to PC // No effect to PC
assert_eq!(cpu.memory_base(), cpu.read_pc()); assert_eq!(memory_base, cpu.read_pc());
} }
} }