feat: implement more registers

This commit is contained in:
2021-01-17 21:13:59 -06:00
parent 70de3b9142
commit c0b8b8bda2
6 changed files with 106 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
use super::bus::Bus;
use super::instruction::Instruction;
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Debug, Clone, Default)]
pub struct Cpu {
@@ -60,7 +61,28 @@ impl Cpu {
}
pub fn decode(&mut self, opcode: u8) -> Instruction {
Instruction::from_byte(self, opcode)
let tmp = Instruction::from_byte(self, opcode);
let a = self.register(Register::A);
let flag: Flags = self.register(Register::Flag).into();
let bc = self.register_pair(RegisterPair::BC);
let de = self.register_pair(RegisterPair::DE);
let hl = self.register_pair(RegisterPair::HL);
let sp = self.register_pair(RegisterPair::SP);
println!(
"A: {:#04X} | BC: {:#06X} | DE: {:#06X} | HL: {:#06X} | SP: {:#06X} | {}",
a, bc, de, hl, sp, flag
);
// Get info from serial port
// if self.bus.read_byte(0xFF02) == 0x81 {
// let c = self.bus.read_byte(0xFF01) as char;
// self.bus.write_byte(0xFF02, 0x00);
// print!("TEST RESULT!: {}", c);
// }
tmp
}
pub fn execute(&mut self, instruction: Instruction) {
@@ -68,6 +90,24 @@ impl Cpu {
}
}
impl Cpu {
pub fn dbg_read_byte(&self, addr: u16) -> u8 {
self.bus.read_byte(addr)
}
pub fn dbg_write_byte(&mut self, addr: u16, byte: u8) {
self.bus.write_byte(addr, byte);
}
pub fn dbg_read_word(&self, addr: u16) -> u16 {
self.bus.read_word(addr)
}
pub fn dbg_write_word(&mut self, addr: u16, word: u16) {
self.bus.write_word(addr, word)
}
}
impl Cpu {
pub fn read_byte(&mut self, addr: u16) -> u8 {
let byte = self.bus.read_byte(addr);
@@ -223,6 +263,34 @@ impl Flags {
}
}
impl Display for Flags {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
if self.z {
f.write_str("Z")?;
} else {
f.write_str("_")?;
}
if self.n {
f.write_str("N")?;
} else {
f.write_str("_")?;
}
if self.h {
f.write_str("H")?;
} else {
f.write_str("_")?;
}
if self.c {
f.write_str("C")
} else {
f.write_str("_")
}
}
}
impl From<u8> for Flags {
fn from(flag: u8) -> Self {
Self {