add EI, DI, HLT

This commit is contained in:
Lea 2023-01-25 19:30:02 +01:00
parent 1398de8181
commit 1f98c57eb0
Signed by: Lea
GPG key ID: 1BAFFE8347019C42

View file

@ -18,6 +18,9 @@ pub struct EmulatorState {
sp: u16,
/// Memory pointer
pc: u16,
/// Enable interrupts
ei: bool,
/// Memory map
memory: [u8; 8000],
}
@ -86,6 +89,21 @@ fn set_register(register: Register, value: u8, state: &mut EmulatorState) {
};
}
/// Print values of registers and flags to stdout
fn print_state(state: &EmulatorState) {
// State
println!("\nsp\tpc\tei");
println!("{}\t{}\t{}", state.sp, state.pc, state.ei);
// Registers
println!("\nB\tC\tD\tE\tH\tL\tA");
println!("{}\t{}\t{}\t{}\t{}\t{}\t{}", state.b, state.c, state.d, state.e, state.h, state.l, state.a);
// Flags
println!("\nz\ts\tp\tc");
println!("{}\t{}\t{}\t{}", state.cc.z, state.cc.s, state.cc.p, state.cc.c);
}
fn main() {
let mut state = EmulatorState {
a: 0,
@ -98,6 +116,7 @@ fn main() {
sp: 0,
cc: ConditionCodes { z: true, s: true, p: true, c: true },
pc: 0,
ei: true,
memory: [0; 8000],
};
@ -140,7 +159,18 @@ fn tick(state: &mut EmulatorState) {
0x88..=0x8f => arithmetic::adc(register_from_num(instruction & 0xf), state), // ADC
0xc6 => arithmetic::adi(next_byte(), state), // ADI
0xce => arithmetic::aci(next_byte(), state), // ACI
_ => not_implemented(state),
/* Special */
0xfb => state.ei = true, // EI
0xf3 => state.ei = false, // DI
0x76 => if state.ei { todo!() } else { // HLT
println!("HLT called after DI; exiting.");
print_state(state);
std::process::exit(0);
},
_ => not_implemented(state),
}
state.pc += 1;