invadeez/src/emulator/structs.rs

114 lines
3 KiB
Rust
Raw Normal View History

2023-01-25 18:52:25 +00:00
use crate::MEMORY_SIZE;
pub struct EmulatorState {
pub a: u8,
pub b: u8,
pub c: u8,
pub d: u8,
pub e: u8,
pub h: u8,
pub l: u8,
pub cc: ConditionCodes,
/// Stack pointer
pub sp: u16,
/// Memory pointer
pub pc: u16,
/// Enable interrupts
pub ei: bool,
/// Memory map
pub memory: [u8; MEMORY_SIZE],
}
pub struct ConditionCodes {
/// Zero (Z), set if the result is zero.
pub z: bool,
/// Sign (S), set if the result is negative.
pub s: bool,
/// Parity (P), set if the number of 1 bits in the result is even.
pub p: bool,
/// Carry (C), set if the last addition operation resulted in a carry or if the last subtraction operation required a borrow.
pub c: bool,
// Auxiliary carry (AC or H), used for binary-coded decimal arithmetic (BCD).
// Can't test this so I won't implement it
// ac: bool,
}
2023-01-25 21:42:27 +00:00
#[derive(PartialEq, Debug)]
2023-01-25 18:52:25 +00:00
pub enum Register {
2023-01-25 21:42:27 +00:00
B,
C,
D,
E,
H,
L,
M,
A,
2023-01-25 18:52:25 +00:00
SP,
}
/// Returns a Register enum based on the input number 0..7 in the order B,C,D,E,H,L,M,A
pub fn register_from_num(b: u8) -> Register {
match b {
0 => Register::B,
1 => Register::C,
2 => Register::D,
3 => Register::E,
4 => Register::H,
5 => Register::L,
6 => Register::M,
7 => Register::A,
_ => panic!("'{b}' cannot be converted to register enum"),
}
}
2023-01-25 19:18:31 +00:00
pub fn get_register(register: &Register, state: &EmulatorState) -> u8 {
2023-01-25 18:52:25 +00:00
match register {
Register::B => state.b as u8,
Register::C => state.c as u8,
Register::D => state.d as u8,
Register::E => state.e as u8,
Register::H => state.h as u8,
Register::L => state.l as u8,
Register::A => state.a as u8,
Register::M => state.memory[u16::from_le_bytes([state.l, state.h]) as usize],
Register::SP => unreachable!(),
}
}
2023-01-25 19:18:31 +00:00
pub fn set_register(register: &Register, value: u8, state: &mut EmulatorState) {
2023-01-25 18:52:25 +00:00
match register {
Register::B => state.b = value,
Register::C => state.c = value,
Register::D => state.d = value,
Register::E => state.e = value,
Register::H => state.h = value,
Register::L => state.l = value,
Register::A => state.a = value,
Register::M => state.memory[u16::from_le_bytes([state.l, state.h]) as usize] = value,
Register::SP => panic!("Cannot set 'SP' through set_register()"),
};
}
/// Print values of registers and flags to stdout
pub fn print_state(state: &EmulatorState) {
// State
println!("\nsp\tpc\tei");
println!("{:#06x}\t{:#06x}\t{}", state.sp, state.pc, state.ei);
// Registers
println!("\nB\tC\tD\tE\tH\tL\tA");
2023-01-25 21:42:27 +00:00
println!(
"{:#04x}\t{:#04x}\t{:#04x}\t{:#04x}\t{:#04x}\t{:#04x}\t{:#04x}",
state.b, state.c, state.d, state.e, state.h, state.l, state.a
);
2023-01-25 18:52:25 +00:00
// Flags
println!("\nz\ts\tp\tc");
2023-01-25 21:42:27 +00:00
println!(
"{}\t{}\t{}\t{}",
state.cc.z, state.cc.s, state.cc.p, state.cc.c
);
2023-01-25 18:52:25 +00:00
}