4-bit virtual CPU
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

38 lines
1.1 KiB

// SPDX-License-Identifier: MIT
// Copyright Murad Karammaev, Nikita Kuzmin
pub mod assembler;
pub mod cpu;
pub mod instruction;
#[cfg(test)]
mod tests {
use crate::{assembler::Assembler, cpu::Cpu};
use std::num::Wrapping;
#[test]
fn integration_assemble_execute() {
let code = r#"
MOV R0.l, 0xF # R0 = 15
MOV R1.l, 0x3 # R1 = 3
ADD # R0 += R1 (18)
MOV [0], R0 # MEM[0] = R0 (18)
ZERO R0 # R0 = 0
HALT # EXIT
"#;
let code = Assembler::new().assemble(code).unwrap();
let mut cpu = Cpu::new(&code);
for _ in 0..5 {
assert!(!cpu.step()); // CPU does not halt…
}
assert!(cpu.step()); // …until it reaches halt instruction
assert_eq!(cpu.IP, Wrapping(5)); // IP points at halt instruction
assert_eq!(cpu.R0, Wrapping(0));
assert_eq!(cpu.R1, Wrapping(3));
assert!(!cpu.C);
assert_eq!(cpu.data[0], 18);
for i in 1..=15 {
assert_eq!(cpu.data[i], 0);
}
}
}