forked from ivanizag/iz80
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopcode_jumps.rs
290 lines (270 loc) · 8.6 KB
/
opcode_jumps.rs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use super::opcode::*;
use super::environment::*;
use super::registers::*;
use super::state::SizePrefix;
// Relative jumps
pub fn build_djnz() -> Opcode {
Opcode {
name: "DJNZ l".to_string(),
action: Box::new(move |env: &mut Environment| {
let offset = env.advance_pc();
let b = env.state.reg.get8(Reg8::B).wrapping_add(0xff /* -1 */);
env.state.reg.set8(Reg8::B, b);
if b != 0 {
// Condition not met
env.sys.use_cycles(1);
relative_jump(env, offset);
}
})
}
}
pub fn build_jr_unconditional() -> Opcode {
Opcode {
name: "JR l".to_string(),
action: Box::new(move |env: &mut Environment| {
let offset = env.advance_pc();
env.sys.use_cycles(1);
relative_jump(env, offset);
})
}
}
pub fn build_jr_eq((flag, value, name): (Flag, bool, &str)) -> Opcode {
Opcode {
name: format!("JR {}, l", name),
action: Box::new(move |env: &mut Environment| {
let offset = env.advance_pc();
if env.state.reg.get_flag(flag) == value {
env.sys.use_cycles(2);
relative_jump(env, offset);
}
})
}
}
fn relative_jump(env: &mut Environment, offset: u8) {
let mut pc = env.state.pc();
pc = env.wrap_address(pc, offset as i8 as i32);
env.state.set_pc(pc);
}
fn handle_jump_adl_state(env: &mut Environment) {
if env.state.reg.adl {
match env.state.sz_prefix {
SizePrefix::SIS => { env.state.reg.adl = false },
SizePrefix::LIS | SizePrefix::SIL => {
eprintln!("Invalid size prefix for ADL=1 with jump at PC=${:x}", env.state.pc());
}
SizePrefix::LIL |
SizePrefix::None => {}
}
} else {
match env.state.sz_prefix {
SizePrefix::LIL => { env.state.reg.adl = true },
SizePrefix::LIS | SizePrefix::SIL => {
eprintln!("Invalid size prefix for ADL=0 with jump at PC=${:x}", env.state.pc());
},
SizePrefix::SIS | SizePrefix::None => {}
}
}
}
// Absolute jumps
pub fn build_jp_unconditional() -> Opcode {
Opcode {
name: "JP nn".to_string(),
action: Box::new(move |env: &mut Environment| {
let address = env.advance_immediate_16mbase_or_24();
handle_jump_adl_state(env);
env.sys.use_cycles(1);
env.state.set_pc(address);
})
}
}
pub fn build_jp_eq((flag, value, name): (Flag, bool, &str)) -> Opcode {
Opcode {
name: format!("JP {}, nn", name),
action: Box::new(move |env: &mut Environment| {
let address = env.advance_immediate_16mbase_or_24();
if env.state.reg.get_flag(flag) == value {
env.sys.use_cycles(1);
env.state.set_pc(address);
}
})
}
}
pub fn build_jp_hl() -> Opcode {
Opcode {
name: "JP (HL)".to_string(),
action: Box::new(move |env: &mut Environment| {
// Note: no displacement added to the index
let address = env.index_value();
env.sys.use_cycles(1);
env.state.set_pc(address);
})
}
}
fn handle_call_size_prefix(env: &mut Environment) {
let pc = env.state.pc();
if env.state.reg.adl {
match env.state.sz_prefix {
SizePrefix::None => {
env.push(pc); // 3 bytes onto SPL
},
SizePrefix::SIS | // not valid according to docs, but works
SizePrefix::LIS => {
env.push_byte_sps((pc >> 8) as u8);
env.push_byte_sps(pc as u8);
env.push_byte_spl((pc >> 16) as u8);
env.push_byte_spl(3);
env.state.reg.adl = false;
}
SizePrefix::LIL => {
env.push(pc); // 3 bytes onto SPL
env.push_byte_spl(3);
}
prefix => {
env.push(pc); // 3 bytes onto SPL
eprintln!("invalid call size prefix for ADL=1: {}", prefix);
}
}
} else {
match env.state.sz_prefix {
SizePrefix::None => {
env.push_byte_sps((pc >> 8) as u8);
env.push_byte_sps(pc as u8);
},
SizePrefix::LIL | // not valid according to docs, but works
SizePrefix::SIL => {
env.push_byte_spl((pc >> 8) as u8);
env.push_byte_spl(pc as u8);
env.push_byte_spl(2);
env.state.reg.adl = true;
}
SizePrefix::SIS => {
env.push_byte_sps((pc >> 8) as u8);
env.push_byte_sps(pc as u8);
env.push_byte_spl(2);
}
SizePrefix::LIS => {
env.push_byte_spl((pc >> 8) as u8);
env.push_byte_spl(pc as u8);
eprintln!("invalid call size prefix for ADL=0: LIS");
}
}
}
}
// Calls to subroutine
pub fn build_call() -> Opcode {
Opcode {
name: "CALL nn".to_string(),
action: Box::new(move |env: &mut Environment| {
let address = env.advance_immediate16or24();
handle_call_size_prefix(env);
env.state.set_pc(address);
})
}
}
pub fn build_call_eq((flag, value, name): (Flag, bool, &str)) -> Opcode {
Opcode {
name: format!("CALL {}, nn", name),
action: Box::new(move |env: &mut Environment| {
let address = env.advance_immediate_16mbase_or_24();
if env.state.reg.get_flag(flag) == value {
handle_call_size_prefix(env);
env.state.set_pc(address);
}
})
}
}
fn handle_rst_size_prefix(env: &mut Environment, vec: u32) {
let pc = env.state.pc();
if env.state.reg.adl {
match env.state.sz_prefix {
SizePrefix::None => {
env.push(pc);
env.state.set_pc(vec);
},
SizePrefix::SIL => {
env.push_byte_sps((pc >> 8) as u8);
env.push_byte_sps(pc as u8);
env.push_byte_spl((pc >> 16) as u8);
env.push_byte_spl(3);
env.state.reg.pc = vec;
env.state.reg.adl = false;
}
SizePrefix::LIS | // not valid according to spec, but works on ez80
SizePrefix::LIL => {
env.push(pc);
env.push_byte_spl(3);
env.state.reg.pc = vec;
}
SizePrefix::SIS => {
eprintln!("invalid rst size prefix");
}
}
} else {
match env.state.sz_prefix {
SizePrefix::None => {
env.push(pc);
env.state.set_pc(vec);
},
SizePrefix::SIL | // <- SIL forbidden by spec, but works on ez80
SizePrefix::SIS => {
env.push(pc);
env.push_byte_spl(2);
env.state.reg.pc = vec;
}
SizePrefix::LIL | // <- LIL is forbidden by the spec, but work on ez80
SizePrefix::LIS => {
env.push_byte_spl((pc >> 8) as u8);
env.push_byte_spl(pc as u8);
env.push_byte_spl(2);
env.state.reg.adl = true;
env.state.reg.pc = vec;
}
}
}
}
pub fn build_rst(d: u8) -> Opcode {
Opcode {
name: format!("RST {:02x}h", d),
action: Box::new(move |env: &mut Environment| {
let address = d as u32;
handle_rst_size_prefix(env, address);
})
}
}
// Returns
pub fn build_ret() -> Opcode {
Opcode {
name: "RET".to_string(),
action: Box::new(move |env: &mut Environment| {
env.sys.use_cycles(2);
env.subroutine_return();
})
}
}
pub fn build_reti() -> Opcode {
Opcode {
name: "RETI".to_string(),
action: Box::new(move |env: &mut Environment| {
env.subroutine_return();
})
}
}
pub fn build_retn() -> Opcode {
Opcode {
name: "RETN".to_string(),
action: Box::new(move |env: &mut Environment| {
env.subroutine_return();
env.state.reg.end_nmi();
})
}
}
pub fn build_ret_eq((flag, value, name): (Flag, bool, &str)) -> Opcode {
Opcode {
name: format!("RET {}", name),
action: Box::new(move |env: &mut Environment| {
if env.state.reg.get_flag(flag) == value {
env.subroutine_return();
}
})
}
}