declutter main.rs

This commit is contained in:
Lea 2023-01-25 19:52:25 +01:00
parent 22e0217890
commit d7871531ea
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
2 changed files with 107 additions and 103 deletions

View file

@ -1,111 +1,12 @@
use std::{fs, env};
use instructions::arithmetic;
use crate::structs::*;
mod instructions;
mod structs;
const MEMORY_SIZE: usize = 8192;
pub struct EmulatorState {
a: u8,
b: u8,
c: u8,
d: u8,
e: u8,
h: u8,
l: u8,
cc: ConditionCodes,
/// Stack pointer
sp: u16,
/// Memory pointer
pc: u16,
/// Enable interrupts
ei: bool,
/// Memory map
memory: [u8; MEMORY_SIZE],
}
pub struct ConditionCodes {
/// Zero (Z), set if the result is zero.
z: bool,
/// Sign (S), set if the result is negative.
s: bool,
/// Parity (P), set if the number of 1 bits in the result is even.
p: bool,
/// Carry (C), set if the last addition operation resulted in a carry or if the last subtraction operation required a borrow.
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,
}
#[derive(PartialEq)]
#[derive(Debug)]
pub enum Register {
B, C, D, E,
H, L, M, A,
SP,
}
/// Returns a Register enum based on the input number 0..7 in the order B,C,D,E,H,L,M,A
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"),
}
}
fn get_register(register: Register, state: &EmulatorState) -> u8 {
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!(),
}
}
fn set_register(register: Register, value: u8, state: &mut EmulatorState) {
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
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");
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);
// Flags
println!("\nz\ts\tp\tc");
println!("{}\t{}\t{}\t{}", state.cc.z, state.cc.s, state.cc.p, state.cc.c);
}
pub const MEMORY_SIZE: usize = 8192;
fn main() {
let mut state = EmulatorState {

103
src/emulator/structs.rs Normal file
View file

@ -0,0 +1,103 @@
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,
}
#[derive(PartialEq)]
#[derive(Debug)]
pub enum Register {
B, C, D, E,
H, L, M, A,
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"),
}
}
pub fn get_register(register: Register, state: &EmulatorState) -> u8 {
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!(),
}
}
pub fn set_register(register: Register, value: u8, state: &mut EmulatorState) {
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");
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);
// Flags
println!("\nz\ts\tp\tc");
println!("{}\t{}\t{}\t{}", state.cc.z, state.cc.s, state.cc.p, state.cc.c);
}