use core::ops::Deref;
use procem::{processor::Processor, register::Flag, word::Word};
use crate::instruction::Instruction;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum JumpCondition {
Unconditional,
Zero,
NotZero,
Carry,
NotCarry,
Signed,
NotSigned,
Greater,
Less,
GreaterOrEq,
LessOrEq,
}
impl JumpCondition {
#[inline]
pub(crate) const fn check<const STACK_SIZE: usize, W, P>(
self,
processor: &Processor<STACK_SIZE, Instruction<W>, P, W>,
) -> bool
where
W: Word,
P: Deref<Target = [Instruction<W>]>,
{
let flags = &processor.registers;
match self {
Self::Unconditional => true,
Self::Zero => flags.get_flag(Flag::Z),
Self::NotZero => !flags.get_flag(Flag::Z),
Self::Carry => flags.get_flag(Flag::C),
Self::NotCarry => !flags.get_flag(Flag::C),
Self::Signed => flags.get_flag(Flag::S),
Self::NotSigned => !flags.get_flag(Flag::S),
Self::Greater => !flags.get_flag(Flag::Z) && !flags.get_flag(Flag::S),
Self::Less => !flags.get_flag(Flag::Z) && flags.get_flag(Flag::S),
Self::GreaterOrEq => flags.get_flag(Flag::Z) || !flags.get_flag(Flag::S),
Self::LessOrEq => flags.get_flag(Flag::Z) || flags.get_flag(Flag::S),
}
}
}