Newer
Older
}
fn call_nz_u16(cpu: &mut Cpu) {
let word = cpu.read_u16();
if cpu.get_zero() {
return;
}
cpu.push_word(cpu.pc);
cpu.pc = word;
cpu.ticks = cpu.ticks.wrapping_add(12);
}
fn push_bc(cpu: &mut Cpu) {
cpu.push_word(cpu.bc());
}
fn add_a_u8(cpu: &mut Cpu) {
let byte = cpu.read_u8();
cpu.a = add_set_flags(cpu, cpu.a, byte);
}
fn ret(cpu: &mut Cpu) {
cpu.pc = cpu.pop_word();
}
fn call_u16(cpu: &mut Cpu) {
let word = cpu.read_u16();
cpu.push_word(cpu.pc);
cpu.pc = word;
}
fn rst_08h(cpu: &mut Cpu) {
rst(cpu, 0x0008);
}
fn pop_de(cpu: &mut Cpu) {
let word = cpu.pop_word();
cpu.set_de(word);
}
fn push_de(cpu: &mut Cpu) {
cpu.push_word(cpu.de());
}
fn sub_a_u8(cpu: &mut Cpu) {
let byte = cpu.read_u8();
cpu.a = sub_set_flags(cpu, cpu.a, byte);
}
fn ld_mff00u8_a(cpu: &mut Cpu) {
let byte = cpu.read_u8();
cpu.mmu.write(0xff00 + byte as u16, cpu.a);
}
fn pop_hl(cpu: &mut Cpu) {
let word = cpu.pop_word();
cpu.set_hl(word);
}
fn ld_mff00c_a(cpu: &mut Cpu) {
cpu.mmu.write(0xff00 + cpu.c as u16, cpu.a);
}
fn push_hl(cpu: &mut Cpu) {
cpu.push_word(cpu.hl());
}
fn and_a_u8(cpu: &mut Cpu) {
let byte = cpu.read_u8();
cpu.a &= byte;
cpu.set_sub(false);
cpu.set_zero(cpu.a == 0);
cpu.set_half_carry(true);
cpu.set_carry(false);
}
fn ld_mu16_a(cpu: &mut Cpu) {
let word = cpu.read_u16();
cpu.mmu.write(word, cpu.a);
}
fn xor_a_u8(cpu: &mut Cpu) {
let byte = cpu.read_u8();
cpu.a ^= byte;
cpu.set_sub(false);
cpu.set_zero(cpu.a == 0);
cpu.set_half_carry(false);
cpu.set_carry(false);
}
fn rst_18h(cpu: &mut Cpu) {
rst(cpu, 0x0018);
}
fn ld_a_mff00u8(cpu: &mut Cpu) {
let byte = cpu.read_u8();
cpu.a = cpu.mmu.read(0xff00 + byte as u16);
}
fn pop_af(cpu: &mut Cpu) {
let word = cpu.pop_word();
cpu.set_af(word);
}
fn di(cpu: &mut Cpu) {
cpu.disable_int();
}
fn push_af(cpu: &mut Cpu) {
cpu.push_word(cpu.af());
}
fn ld_a_mu16(cpu: &mut Cpu) {
let word = cpu.read_u16();
let byte = cpu.mmu.read(word);
cpu.a = byte;
}
fn ei(cpu: &mut Cpu) {
cpu.enable_int();
}
fn cp_a_u8(cpu: &mut Cpu) {
let byte = cpu.read_u8();
sub_set_flags(cpu, cpu.a, byte);
}
fn rst_38h(cpu: &mut Cpu) {
rst(cpu, 0x0038);
}
fn rl_c(cpu: &mut Cpu) {
cpu.c = rl(cpu, cpu.c);
}
fn rr_c(cpu: &mut Cpu) {
cpu.c = rr(cpu, cpu.c);
}
fn rr_d(cpu: &mut Cpu) {
cpu.d = rr(cpu, cpu.d);
}
fn swap_a(cpu: &mut Cpu) {
cpu.a = swap(cpu, cpu.a)
}
fn srl_b(cpu: &mut Cpu) {
cpu.b = srl(cpu, cpu.b);
}
fn bit_7_h(cpu: &mut Cpu) {
bit_h(cpu, 7);
}
/// Helper function that rotates (shifts) left the given
/// byte (probably from a register) and updates the
/// proper flag registers.
fn rl(cpu: &mut Cpu, value: u8) -> u8 {
let result = (value << 1) | carry as u8;
cpu.set_sub(false);
cpu.set_zero(result == 0);
cpu.set_half_carry(false);
result
}
/// Helper function that rotates (shifts) right the given
/// byte (probably from a register) and updates the
/// proper flag registers.
fn rr(cpu: &mut Cpu, value: u8) -> u8 {
let carry = cpu.get_carry();
cpu.set_carry(value & 0x01 == 0x01);
let result = (value >> 1) | ((carry as u8) << 7);
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
cpu.set_sub(false);
cpu.set_zero(result == 0);
cpu.set_half_carry(false);
result
}
/// Helper function to test one bit in a u8.
/// Returns true if bit is 0.
fn bit_zero(val: u8, bit: u8) -> bool {
(val & (1u8 << (bit as usize))) == 0
}
fn bit_h(cpu: &mut Cpu, bit: u8) {
cpu.set_sub(false);
cpu.set_zero(bit_zero(cpu.h, bit));
cpu.set_half_carry(true);
}
fn add_set_flags(cpu: &mut Cpu, first: u8, second: u8) -> u8 {
let first = first as u32;
let second = second as u32;
let value = first.wrapping_add(second);
let value_b = value as u8;
cpu.set_sub(false);
cpu.set_zero(value_b == 0);
cpu.set_half_carry((first ^ second ^ value) & 0x10 == 0x10);
cpu.set_carry(value & 0x100 == 0x100);
value_b
}
fn sub_set_flags(cpu: &mut Cpu, first: u8, second: u8) -> u8 {
let first = first as u32;
let second = second as u32;
let value = first.wrapping_sub(second);
let value_b = value as u8;
cpu.set_sub(true);
cpu.set_zero(value_b == 0);
cpu.set_half_carry((first ^ second ^ value) & 0x10 == 0x10);
cpu.set_carry(value & 0x100 == 0x100);
value_b
}
fn add_u16_u16(cpu: &mut Cpu, first: u16, second: u16) -> u16 {
let first = first as u32;
let second = second as u32;
let value = first.wrapping_add(second);
cpu.set_sub(false);
cpu.set_half_carry((first ^ second ^ value) & 0x1000 == 0x1000);
cpu.set_carry(value & 0x10000 == 0x10000);
value as u16
}
fn swap(cpu: &mut Cpu, value: u8) -> u8 {
cpu.set_sub(false);
cpu.set_zero(value == 0);
cpu.set_half_carry(false);
cpu.set_carry(false);
(value << 4) | (value >> 4)
}
fn srl(cpu: &mut Cpu, value: u8) -> u8 {
let result = value >> 1;
cpu.set_sub(false);
cpu.set_zero(result == 0);
cpu.set_half_carry(false);
cpu.set_carry(value & 0x01 == 0x01);
result
}
/// Helper function for RST instructions, pushes the
/// current PC to the stack and jumps to the provided
/// address.
fn rst(cpu: &mut Cpu, addr: u16) {
cpu.push_word(cpu.pc);
cpu.pc = addr;
}