|
|
@ -4,3 +4,35 @@ |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|