forked from Lea/invadeez
78 lines
2.7 KiB
Rust
78 lines
2.7 KiB
Rust
use crate::{get_register, structs::set_register, EmulatorState, Register};
|
|
|
|
/// Sets the condition code flags according to `result`. `flags` parameter
|
|
/// indicates which flags will be set, 0b1111 will set all (Z, S, C, P)
|
|
/// while 0b1000 will only set Z.
|
|
fn set_cc(state: &mut EmulatorState, result: u16, flags: u8) {
|
|
if flags & 0b1000 > 0 {
|
|
state.cc.z = (result & 0xff) == 0;
|
|
}
|
|
if flags & 0b0100 > 0 {
|
|
state.cc.s = (result & 0x80) > 0;
|
|
}
|
|
if flags & 0b0010 > 0 {
|
|
state.cc.c = result > 0xff;
|
|
}
|
|
if flags & 0b0001 > 0 {
|
|
state.cc.p = (result & 0xff).count_ones() % 2 == 0;
|
|
}
|
|
}
|
|
|
|
/// Add values of `register` and `A`
|
|
pub fn add(register: Register, state: &mut EmulatorState) {
|
|
let result = get_register(®ister, state) as u16 + state.a as u16;
|
|
set_cc(state, result, 0b1111);
|
|
state.a = (result & 0xff) as u8;
|
|
}
|
|
|
|
/// Add values of input byte and `A`
|
|
pub fn adi(byte: u8, state: &mut EmulatorState) {
|
|
let result = state.a as u16 + byte as u16;
|
|
set_cc(state, result, 0b1111);
|
|
state.a = result as u8;
|
|
}
|
|
|
|
/// Add values of `register` and `A` and add +1 if carry bit is set
|
|
pub fn adc(register: Register, state: &mut EmulatorState) {
|
|
let result = get_register(®ister, state) as u16 + state.a as u16 + u16::from(state.cc.c);
|
|
set_cc(state, result, 0b1111);
|
|
state.a = (result & 0xff) as u8;
|
|
}
|
|
|
|
/// Add values of input byte and `A` and add +1 if carry bit is set
|
|
pub fn aci(byte: u8, state: &mut EmulatorState) {
|
|
let result = state.a as u16 + byte as u16 + u16::from(state.cc.c);
|
|
set_cc(state, result, 0b1111);
|
|
state.a = result as u8;
|
|
}
|
|
|
|
/// Double precision add - Add B&C, D&E or H&L to H&L
|
|
pub fn dad(register: Register, state: &mut EmulatorState) {
|
|
let num = match register {
|
|
Register::B => u16::from_le_bytes([state.c, state.b]),
|
|
Register::D => u16::from_le_bytes([state.e, state.d]),
|
|
Register::H => u16::from_le_bytes([state.l, state.h]),
|
|
Register::SP => state.sp,
|
|
_ => panic!("Cannot perform DAD on register {:?}", register),
|
|
};
|
|
|
|
let (result, overflow) = num.overflowing_add(u16::from_le_bytes([state.l, state.h]));
|
|
state.cc.c = overflow;
|
|
state.h = (result >> 8) as u8;
|
|
state.l = result as u8;
|
|
}
|
|
|
|
/// Increase register
|
|
pub fn inr(register: Register, state: &mut EmulatorState) {
|
|
let (result, _) = get_register(®ister, state).overflowing_add(1);
|
|
set_cc(state, result as u16, 0b1101);
|
|
set_register(®ister, result, state);
|
|
}
|
|
|
|
/// Decrease register
|
|
pub fn dcr(register: Register, state: &mut EmulatorState) {
|
|
let (result, _) = get_register(®ister, state).overflowing_sub(1);
|
|
set_cc(state, result as u16, 0b1101);
|
|
set_register(®ister, result, state);
|
|
}
|