use std::fmt;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum AluOp {
Add,
AddCarry,
Sub,
SubCarry,
And,
Xor,
Or,
Compare,
}
impl AluOp {
pub(super) fn from_ycode(code: u8) -> Self {
match code {
0 => Self::Add,
1 => Self::AddCarry,
2 => Self::Sub,
3 => Self::SubCarry,
4 => Self::And,
5 => Self::Xor,
6 => Self::Or,
7 => Self::Compare,
_ => panic!("Unrecognized ALU operation type (y code) {}", code),
}
}
}
impl fmt::Display for AluOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::Add => f.write_str("ADD"),
Self::AddCarry => f.write_str("ADC"),
Self::Sub => f.write_str("SUB"),
Self::SubCarry => f.write_str("SBC"),
Self::And => f.write_str("AND"),
Self::Xor => f.write_str("XOR"),
Self::Or => f.write_str("OR"),
Self::Compare => f.write_str("CP"),
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum AluUnaryOp {
RotateLeft8,
RotateLeft9,
RotateRight8,
RotateRight9,
DecimalAdjust,
SetCarryFlag,
Compliment,
ComplimentCarryFlag,
}
impl AluUnaryOp {
pub(super) fn from_ycode(code: u8) -> Self {
match code {
0 => Self::RotateLeft8,
1 => Self::RotateRight8,
2 => Self::RotateLeft9,
3 => Self::RotateRight9,
4 => Self::DecimalAdjust,
5 => Self::Compliment,
6 => Self::SetCarryFlag,
7 => Self::ComplimentCarryFlag,
_ => panic!("Unrecognized ALU unary operation type (y code) {}", code),
}
}
}
impl fmt::Display for AluUnaryOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::RotateLeft8 => f.write_str("RLCA"),
Self::RotateLeft9 => f.write_str("RLA"),
Self::RotateRight8 => f.write_str("RRCA"),
Self::RotateRight9 => f.write_str("RRA"),
Self::DecimalAdjust => f.write_str("DAA"),
Self::SetCarryFlag => f.write_str("SCF"),
Self::Compliment => f.write_str("CPL"),
Self::ComplimentCarryFlag => f.write_str("CCF"),
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Operand8 {
B,
C,
D,
E,
H,
L,
A,
AddrHL,
AddrBC,
AddrDE,
AddrHLInc,
AddrHLDec,
Immediate,
AddrImmediate,
AddrRelC,
AddrRelImmediate,
}
impl Operand8 {
pub(super) fn from_regcode(code: u8) -> Self {
match code {
0 => Self::B,
1 => Self::C,
2 => Self::D,
3 => Self::E,
4 => Self::H,
5 => Self::L,
6 => Self::AddrHL,
7 => Self::A,
_ => panic!("Unrecognized Operand type {}", code),
}
}
pub(super) fn from_indirect(code: u8) -> Self {
match code {
0 => Self::AddrBC,
1 => Self::AddrDE,
2 => Self::AddrHLInc,
3 => Self::AddrHLDec,
_ => panic!("Unrecognized indirection code {}", code),
}
}
}
impl fmt::Display for Operand8 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::A => f.write_str("A"),
Self::B => f.write_str("B"),
Self::C => f.write_str("C"),
Self::D => f.write_str("D"),
Self::E => f.write_str("E"),
Self::H => f.write_str("H"),
Self::L => f.write_str("L"),
Self::AddrHL => f.write_str("(HL)"),
Self::AddrBC => f.write_str("(BC)"),
Self::AddrDE => f.write_str("(DE)"),
Self::AddrHLInc => f.write_str("(HL+)"),
Self::AddrHLDec => f.write_str("(HL-)"),
Self::Immediate => f.write_str("u8"),
Self::AddrImmediate => f.write_str("(u16)"),
Self::AddrRelC => f.write_str("(FF00+C)"),
Self::AddrRelImmediate => f.write_str("(FF00+u8)"),
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Operand16 {
BC,
DE,
HL,
AF,
Sp,
Immediate,
AddrImmediate,
}
impl Operand16 {
pub(super) fn from_pair_code_sp(code: u8) -> Operand16 {
match code {
0 => Operand16::BC,
1 => Operand16::DE,
2 => Operand16::HL,
3 => Operand16::Sp,
_ => panic!("Unrecognized register pair code {}", code),
}
}
pub(super) fn from_pair_code_af(code: u8) -> Operand16 {
match code {
0 => Operand16::BC,
1 => Operand16::DE,
2 => Operand16::HL,
3 => Operand16::AF,
_ => panic!("Unrecognized register pair code {}", code),
}
}
}
impl fmt::Display for Operand16 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Operand16::BC => f.write_str("BC"),
Operand16::DE => f.write_str("DE"),
Operand16::HL => f.write_str("HL"),
Operand16::AF => f.write_str("AF"),
Operand16::Sp => f.write_str("SP"),
Operand16::Immediate => f.write_str("u16"),
Operand16::AddrImmediate => f.write_str("(u16)"),
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum ConditionCode {
Unconditional,
NonZero,
Zero,
NoCarry,
Carry,
}
impl ConditionCode {
pub(super) fn from_relative_cond_code(code: u8) -> Self {
match code {
3 => Self::Unconditional,
4..=7 => Self::from_absolute_cond_code(code - 4),
_ => panic!("Unrecognized Relative-Jump condtion code {}", code),
}
}
pub(super) fn from_absolute_cond_code(code: u8) -> Self {
match code {
0 => Self::NonZero,
1 => Self::Zero,
2 => Self::NoCarry,
3 => Self::Carry,
_ => panic!("Unrecognized Absolute-Jump condtion code {}", code),
}
}
}
impl fmt::Display for ConditionCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::Unconditional => Ok(()),
Self::NonZero => f.write_str("NZ"),
Self::Zero => f.write_str("Z"),
Self::NoCarry => f.write_str("NC"),
Self::Carry => f.write_str("C"),
}
}
}