rustzx_z80/opcode/
types.rs

1use crate::{
2    smallnum::{U1, U2, U3},
3    RegName8,
4};
5
6/// Instruction prefix type
7#[allow(clippy::upper_case_acronyms)]
8#[derive(Clone, Copy, PartialEq, Eq, Debug)]
9pub enum Prefix {
10    None,
11    CB,
12    DD,
13    ED,
14    FD,
15}
16
17impl Prefix {
18    /// Returns prefix type from byte value
19    pub fn from_byte(data: u8) -> Prefix {
20        match data {
21            0xCB => Prefix::CB,
22            0xDD => Prefix::DD,
23            0xED => Prefix::ED,
24            0xFD => Prefix::FD,
25            _ => Prefix::None,
26        }
27    }
28
29    /// Transforms prefix back to byte
30    pub fn to_byte(self) -> Option<u8> {
31        match self {
32            Prefix::DD => Some(0xDD),
33            Prefix::FD => Some(0xFD),
34            Prefix::ED => Some(0xED),
35            Prefix::CB => Some(0xCB),
36            Prefix::None => None,
37        }
38    }
39}
40
41/// Operand for 8-bit LD instructions
42pub enum LoadOperand8 {
43    Indirect(u16),
44    Reg(RegName8),
45}
46
47/// Operand for 8-bit Bit instructions
48pub enum BitOperand8 {
49    Indirect(u16),
50    Reg(RegName8),
51}
52
53/// Direction of address change in block functions
54pub enum BlockDir {
55    Inc,
56    Dec,
57}
58
59/// Opcode, divided in parts
60/// ```text
61/// xxyyyzzz
62/// xxppqzzz
63/// ```
64/// Used for splitting opcode byte into parts
65#[derive(Clone, Copy)]
66pub struct Opcode {
67    pub byte: u8,
68    pub x: U2,
69    pub y: U3,
70    pub z: U3,
71    pub p: U2,
72    pub q: U1,
73}
74impl Opcode {
75    /// splits opcode into parts
76    pub(crate) fn from_byte(data: u8) -> Opcode {
77        Opcode {
78            byte: data,
79            x: U2::from_byte(data, 6),
80            y: U3::from_byte(data, 3),
81            z: U3::from_byte(data, 0),
82            p: U2::from_byte(data, 4),
83            q: U1::from_byte(data, 3),
84        }
85    }
86}