[cpu] Add set reg

This commit is contained in:
2024-03-03 09:37:26 +05:30
parent 6aa8a24b75
commit 68e1866be9

View File

@@ -1,5 +1,5 @@
use byteorder::{BigEndian, ByteOrder}; use byteorder::{BigEndian, ByteOrder};
use crate::device::cpu::Instruction::{ClearScreen, JumpTo}; use crate::device::cpu::Instruction::{ClearScreen, JumpTo, SetRegister};
#[derive(Eq, PartialEq, Debug)] #[derive(Eq, PartialEq, Debug)]
enum Instruction { enum Instruction {
@@ -10,8 +10,9 @@ enum Instruction{
AddValueToRegister(usize, u8), AddValueToRegister(usize, u8),
SetIndex(u16), SetIndex(u16),
/// ///
DRAW(usize,usize,u8) DRAW(usize, usize, u8),
} }
impl Instruction { impl Instruction {
pub fn parse_fetched_instruction(location: &[u8]) -> Instruction { pub fn parse_fetched_instruction(location: &[u8]) -> Instruction {
assert_eq!(location.len(), 2); assert_eq!(location.len(), 2);
@@ -21,15 +22,18 @@ impl Instruction{
return ClearScreen; return ClearScreen;
} else if (instruction & 0x1000) == 0x1000 { } else if (instruction & 0x1000) == 0x1000 {
return JumpTo(instruction & 0xfff); return JumpTo(instruction & 0xfff);
} } else if (instruction & 0x6000) == 0x6000 {
return SetRegister(((instruction & 0x0f00)>>8) as usize, (instruction & 0xff) as u8);
} else {
todo!(); todo!();
} }
} }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::device::cpu::Instruction; use crate::device::cpu::Instruction;
use crate::device::cpu::Instruction::{ClearScreen, JumpTo}; use crate::device::cpu::Instruction::{ClearScreen, JumpTo, SetRegister};
#[test] #[test]
fn test_clear_screen() { fn test_clear_screen() {
@@ -37,16 +41,25 @@ mod tests{
let ins = Instruction::parse_fetched_instruction(&instruction_bytes); let ins = Instruction::parse_fetched_instruction(&instruction_bytes);
assert_eq!(ins, ClearScreen); assert_eq!(ins, ClearScreen);
} }
#[test] #[test]
fn test_jump_to_instruction_1() { fn test_jump_to_instruction_1() {
let instruction_bytes = 0x1123_u16.to_be_bytes(); let instruction_bytes = 0x1123_u16.to_be_bytes();
let ins = Instruction::parse_fetched_instruction(&instruction_bytes); let ins = Instruction::parse_fetched_instruction(&instruction_bytes);
assert_eq!(ins, JumpTo(0x123)); assert_eq!(ins, JumpTo(0x123));
} }
#[test] #[test]
fn test_jump_to_instruction_2() { fn test_jump_to_instruction_2() {
let instruction_bytes = 0x1faf_u16.to_be_bytes(); let instruction_bytes = 0x1faf_u16.to_be_bytes();
let ins = Instruction::parse_fetched_instruction(&instruction_bytes); let ins = Instruction::parse_fetched_instruction(&instruction_bytes);
assert_eq!(ins, JumpTo(0xfaf)); assert_eq!(ins, JumpTo(0xfaf));
} }
#[test]
fn test_set_instruction(){
let instruction_bytes = 0x6a00_u16.to_be_bytes();
let ins = Instruction::parse_fetched_instruction(&instruction_bytes);
assert_eq!(ins, SetRegister(10,0));
}
} }