Skip to content

Commit

Permalink
Make Opcode part of Instruction; use get_byte/get_nybble.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbargatz committed Dec 30, 2023
1 parent 537201d commit 4c9f0cf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions kaiseki-chip8/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::instructions::{Chip8Instruction, Chip8Opcode};
use crate::instructions::Chip8Instruction;
use kaiseki_core::cpu::decoder::{DecodeError, DecodeOne, Result};
use kaiseki_core::cpu::Instruction;

pub struct Chip8Decoder {}

impl DecodeOne for Chip8Decoder {
type Instruction = Chip8Instruction;

fn decode_one(&self, bytes: &[u8]) -> Result<Self::Instruction> {
let opcode = Chip8Opcode::from_be_bytes(bytes);
let opcode = <Self::Instruction as Instruction>::Opcode::from_be_bytes(bytes);

let ins = match opcode.value() {
0x0000..=0x0FFF => match opcode.value() {
Expand All @@ -24,12 +25,12 @@ impl DecodeOne for Chip8Decoder {
addr: opcode.value() & 0x0FFF,
},
0x3000..=0x3FFF => Chip8Instruction::SkipIfEqual {
vx_idx: ((opcode.value() & 0x0F00) >> 2) as u8,
value: (opcode.value() & 0x00FF) as u8,
vx_idx: opcode.get_nybble(2),
value: opcode.get_byte(0),
},
0x4000..=0x4FFF => Chip8Instruction::SkipIfNotEqual {
vx_idx: ((opcode.value() & 0x0F00) >> 2) as u8,
value: (opcode.value() & 0x00FF) as u8,
vx_idx: opcode.get_nybble(2),
value: opcode.get_byte(0),
},
_ => Err(DecodeError::UnimplementedOpcode)?,
};
Expand Down
4 changes: 2 additions & 2 deletions kaiseki-chip8/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use kaiseki_core::cpu::{opcode::Opcode16, Instruction};

pub type Chip8Opcode = Opcode16;

#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Chip8Instruction {
Expand Down Expand Up @@ -127,6 +125,8 @@ pub enum Chip8Instruction {
}

impl Instruction for Chip8Instruction {
type Opcode = Opcode16;

fn len_bytes(&self) -> usize {
2
}
Expand Down
1 change: 1 addition & 0 deletions kaiseki-core/src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ pub mod decoder;
pub mod opcode;

pub trait Instruction {
type Opcode;
fn len_bytes(&self) -> usize;
}

0 comments on commit 4c9f0cf

Please sign in to comment.