1use std::fmt::Display;
2
3mod cycles;
4pub mod decoder;
5mod len;
6
7#[cfg(test)]
8mod testutils;
9
10pub use cycles::*;
11
12#[cfg(feature = "isa_display")]
13pub mod display;
14
15#[derive(Debug, Copy, Clone)]
16pub enum Reg8 {
17 A,
18 B,
19 C,
20 D,
21 E,
22 F,
23 H,
24 L,
25}
26
27#[derive(Debug, Copy, Clone)]
28pub enum Reg16 {
29 AF,
30 BC,
31 DE,
32 HL,
33 SP,
34}
35
36#[derive(Debug, Copy, Clone)]
37pub enum MemLoc {
38 HighMemReg(Reg8),
40 Reg(Reg16),
41 HighMemImm(u8),
43 Imm(u16),
44}
45
46#[derive(Debug, Copy, Clone)]
47pub enum ArithSrc {
48 Reg(Reg8),
49 Imm(u8),
50 Mem(MemLoc),
51}
52
53#[derive(Debug, Copy, Clone)]
54pub enum Ld8Src {
55 Reg(Reg8),
56 Mem(MemLoc),
57 Imm(u8),
58}
59
60#[derive(Debug, Copy, Clone)]
61pub enum Ld8Dst {
62 Mem(MemLoc),
63 Reg(Reg8),
64}
65
66#[derive(Debug, Copy, Clone)]
67pub enum Ld16Src {
68 Reg(Reg16),
69 Imm(u16),
70}
71
72impl Ld16Src {
73 const fn op_size(&self) -> u8 {
74 match self {
75 Ld16Src::Reg(_) => 0,
76 Ld16Src::Imm(_) => 2,
77 }
78 }
79}
80
81#[derive(Debug, Copy, Clone)]
82pub enum Ld16Dst {
83 Mem(MemLoc),
84 Reg(Reg16),
85}
86
87#[derive(Debug, Copy, Clone)]
88pub enum IncDecTarget {
89 Reg8(Reg8),
90 Reg16(Reg16),
91 MemHL,
92}
93
94#[derive(Debug, Copy, Clone)]
95pub enum PrefArithTarget {
96 Reg(Reg8),
97
98 MemHL,
100}
101
102#[derive(Debug, Copy, Clone)]
103pub enum Bit {
104 B0 = 0,
105 B1 = 1,
106 B2 = 2,
107 B3 = 3,
108 B4 = 4,
109 B5 = 5,
110 B6 = 6,
111 B7 = 7,
112}
113
114#[derive(Debug, Copy, Clone)]
115pub enum Condition {
116 Zero,
117 NotZero,
118 Carry,
119 NotCarry,
120}
121
122#[derive(Debug, Copy, Clone)]
123pub enum RsVec {
124 Rst0 = 0x00,
125 Rst1 = 0x08,
126 Rst2 = 0x10,
127 Rst3 = 0x18,
128 Rst4 = 0x20,
129 Rst5 = 0x28,
130 Rst6 = 0x30,
131 Rst7 = 0x38,
132}
133
134#[derive(Debug, Copy, Clone)]
135pub enum Instruction {
136 Nop,
138
139 Stop(u8),
141
142 Halt,
144
145 EI,
147
148 DI,
150
151 Add(ArithSrc),
153
154 AddCarry(ArithSrc),
156
157 AddHL(Reg16),
159
160 AddSP(i8),
162
163 Sub(ArithSrc),
165
166 SubCarry(ArithSrc),
168
169 And(ArithSrc),
171
172 Or(ArithSrc),
174
175 Xor(ArithSrc),
177
178 Cmp(ArithSrc),
180
181 Inc(IncDecTarget),
183
184 Dec(IncDecTarget),
186
187 RotLeftCircular(PrefArithTarget),
189
190 RotLeftCircularA,
192
193 RotRightCircular(PrefArithTarget),
195
196 RotRightCircularA,
198
199 RotLeft(PrefArithTarget),
201
202 RotLeftA,
204
205 RotRight(PrefArithTarget),
207
208 RotRightA,
210
211 ShiftLeftArith(PrefArithTarget),
213
214 ShiftRightArith(PrefArithTarget),
216
217 Swap(PrefArithTarget),
219
220 ShiftRightLogic(PrefArithTarget),
222
223 Bit(Bit, PrefArithTarget),
225
226 Res(Bit, PrefArithTarget),
228
229 Set(Bit, PrefArithTarget),
231
232 Load8(Ld8Dst, Ld8Src),
234
235 Load16(Ld16Dst, Ld16Src),
237
238 LoadAtoHLI,
240
241 LoadAtoHLD,
243
244 LoadHLItoA,
246
247 LoadHLDtoA,
249
250 LoadSPi8toHL(i8),
252
253 Jump(u16),
255
256 JumpRel(i8),
258
259 JumpHL,
261
262 JumpIf(u16, Condition),
264
265 JumpRelIf(i8, Condition),
267
268 Call(u16),
270
271 CallIf(u16, Condition),
273
274 Ret,
276
277 Reti,
279
280 RetIf(Condition),
282
283 Pop(Reg16),
285
286 Push(Reg16),
288
289 DecimalAdjust,
293
294 ComplementAccumulator,
296
297 SetCarryFlag,
299
300 ComplementCarry,
302
303 Rst(RsVec),
305
306 IllegalInstruction(u8),
308}
309
310impl Display for Instruction {
311 #[cfg(feature = "isa_display")]
312 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
313 let as_displayable = display::DisplayableInstruction::from(*self);
314
315 write!(
316 f,
317 "{}",
318 as_displayable.with_format(&display::FormatOpts::rgdbs())
319 )
320 }
321
322 #[cfg(not(feature = "isa_display"))]
323 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
324 write!(f, "{:?}", self)
325 }
326}