From 65598cf86eeb52bf47418fa45903e66f2e6082a6 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Aug 2025 10:50:05 +0200 Subject: [PATCH] cmov --- src/cpu.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 629af4b..518e560 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -33,6 +33,14 @@ impl Cpu { } } + fn gr(&self, r: u8) -> u32{ + self.reg[r as usize] + } + + fn sr(&mut self, r: u8, v:u32) { + self.reg[r as usize] = v + } + fn ins(&self) -> Result { if self.finger as usize >= self.arr[&0].len() { return Err(CpuError::ProgramEnd); @@ -75,14 +83,23 @@ impl Cpu { } fn handle_ortho(&mut self, i:SpecialInstruction) -> Result<(), CpuError>{ - self.reg[i.a as usize] = i.v as u32; + self.sr(i.a, i.v); + Ok(()) + } + + fn handle_cmov(&mut self, i:NormalInstruction) -> Result<(), CpuError>{ + if self.gr(i.c) > 0 { + let v = self.gr(i.b); + self.sr(i.a, v); + } Ok(()) } fn exec(&mut self, instruction: Instruction)-> Result<(), CpuError>{ - println!("{:?}", instruction); + //println!("{:?}", instruction); match instruction { Instruction::Ortho(i) => self.handle_ortho(i), + Instruction::ConditionalMove(i) => self.handle_cmov(i), _ => Err(CpuError::InvalidInstruction) } } @@ -121,4 +138,46 @@ mod tests{ Ok(()) } + #[test] + fn test_cmov() -> Result<(), String> { + let pgm = vec!( + ezop(13, 1, 65535, 0), + ezop(13, 2, 1337, 0), + ezop(13, 3, 1, 0), + ezop(0, 1, 2, 3), + ); + let mut c = Cpu::new(pgm); + _ = c.step(); + _ = c.step(); + _ = c.step(); + let result = c.step(); + assert!(matches!(result, Ok(_))); + assert_eq!(c.reg[0], 0); + assert_eq!(c.reg[3], 1); + assert_eq!(c.reg[2], 1337); + assert_eq!(c.reg[1], 1337); + Ok(()) + } + + #[test] + fn test_cmov_neg() -> Result<(), String> { + let pgm = vec!( + ezop(13, 1, 65535, 0), + ezop(13, 2, 1337, 0), + ezop(13, 3, 0, 0), + ezop(0, 1, 2, 3), + ); + let mut c = Cpu::new(pgm); + _ = c.step(); + _ = c.step(); + _ = c.step(); + let result = c.step(); + assert!(matches!(result, Ok(_))); + assert_eq!(c.reg[0], 0); + assert_eq!(c.reg[3], 0); + assert_eq!(c.reg[2], 1337); + assert_eq!(c.reg[1], 65535); + Ok(()) + } + } \ No newline at end of file