use num_enum::TryFromPrimitive;
pub const OPCODE_LENGTH: u32 = 8;
pub const DATA_LENGTH: u32 = 8;
#[repr(u8)]
#[derive(
Debug, Eq, PartialEq, EnumIter, Copy, Clone, TryFromPrimitive, EnumCount, Ord, PartialOrd, Hash,
)]
pub enum InstructionType {
NOP = 0,
LDA,
LDB,
LIA,
LIB,
LDR,
STR,
STI,
SWP,
ADD,
SUB,
OUT,
IN,
JMP,
JMR,
JZ,
}
impl InstructionType {
pub fn with_data(&self, data: u8) -> Instruction {
Instruction { ty: *self, data }
}
pub fn with_0(&self) -> Instruction {
Instruction { ty: *self, data: 0 }
}
}
impl Into<u16> for InstructionType {
fn into(self) -> u16 {
self.with_0().into()
}
}
#[derive(Debug, Eq, PartialEq, Copy, Clone, Ord, PartialOrd, Hash)]
pub struct Instruction {
pub ty: InstructionType,
pub data: u8,
}
impl Into<u16> for Instruction {
fn into(self) -> u16 {
self.ty as u16 | ((self.data as u16) << OPCODE_LENGTH)
}
}