1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
use super::super::error; #[derive(Debug, PartialEq)] pub enum Reg16 { AF, BC, DE, HL, SP, PC, } impl std::fmt::Display for Reg16 { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{:?}", self) } } #[derive(Debug, PartialEq)] pub enum Operand { Imm8(u8), Imm16(u16), Reg16(Reg16), } impl Operand { pub fn imm8_from_bytes(bytes: &[u8]) -> Result<Operand, error::ParserError> { if bytes.len() < 1 { return Err(error::ParserError::NotEnoughBytes(bytes.len())); } Ok(Operand::Imm8(u8::from_le_bytes([bytes[0]]))) } pub fn imm16_from_bytes(bytes: &[u8]) -> Result<Operand, error::ParserError> { if bytes.len() < 2 { return Err(error::ParserError::NotEnoughBytes(bytes.len())); } Ok(Operand::Imm16(u16::from_le_bytes([bytes[0], bytes[1]]))) } pub fn is_immediate(&self) -> bool { match self { Self::Imm8(_) | Self::Imm16(_) => true, _ => false, } } pub fn is_register(&self) -> bool { match self { Self::Reg16(_) => true, _ => false, } } } impl Operand { pub fn encoding_size(&self) -> usize { match self { Self::Imm8(_) => 1, _ => 0, } } } impl std::fmt::Display for Operand { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Self::Imm8(byte) => write!(f, "{:#X}", byte), Self::Imm16(word) => write!(f, "{:#X}", word), Self::Reg16(reg16) => write!(f, "{}", reg16), } } }