-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
279 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::cpu::{instructions::BitInstruction, registers::Register, Cpu}; | ||
|
||
use super::Command; | ||
|
||
pub struct BitCommand<'a> { | ||
instruction: &'a BitInstruction, | ||
cpu: &'a mut Cpu, | ||
} | ||
|
||
impl<'a> BitCommand<'a> { | ||
pub fn new(instruction: &'a BitInstruction, cpu: &'a mut Cpu) -> BitCommand<'a> { | ||
BitCommand { instruction, cpu } | ||
} | ||
|
||
fn bit(&mut self, bit: &u8, from: &Register) -> u16 { | ||
let value = self.cpu.registers.get(from); | ||
let result = value & (1 << bit); | ||
|
||
self.cpu.registers.f.zero = result == 0; | ||
self.cpu.registers.f.subtract = false; | ||
self.cpu.registers.f.half_carry = true; | ||
|
||
self.cpu.pc.wrapping_add(2) | ||
} | ||
|
||
fn res(&mut self, bit: &u8, from: &Register) -> u16 { | ||
let value = self.cpu.registers.get(from); | ||
let result = value & !(1 << bit); | ||
|
||
self.cpu.registers.set(from, result); | ||
|
||
self.cpu.pc.wrapping_add(2) | ||
} | ||
|
||
fn set(&mut self, bit: &u8, from: &Register) -> u16 { | ||
let value = self.cpu.registers.get(from); | ||
let result = value | (1 << bit); | ||
|
||
self.cpu.registers.set(from, result); | ||
|
||
self.cpu.pc.wrapping_add(2) | ||
} | ||
} | ||
|
||
impl Command for BitCommand<'_> { | ||
fn execute(&mut self) -> u16 { | ||
match &self.instruction { | ||
BitInstruction::Bit(bit, from) => self.bit(bit, from), | ||
BitInstruction::Res(bit, from) => self.res(bit, from), | ||
BitInstruction::Set(bit, from) => self.set(bit, from), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::cpu::{instructions::{CallInstruction, FlagCondition}, Cpu}; | ||
|
||
use super::Command; | ||
|
||
|
||
pub struct CallCommand<'a> { | ||
instruction: &'a CallInstruction, | ||
cpu: &'a mut Cpu, | ||
} | ||
|
||
impl<'a> CallCommand<'a> { | ||
pub fn new(instruction: &'a CallInstruction, cpu: &'a mut Cpu) -> CallCommand<'a> { | ||
CallCommand { instruction, cpu } | ||
} | ||
|
||
fn call(&mut self) -> u16 { | ||
let address = self.cpu.memory.read_16(self.cpu.pc + 1); | ||
let next_pc = self.cpu.pc.wrapping_add(3); | ||
let next_sp = self.cpu.registers.sp.get().wrapping_sub(2); | ||
self.cpu.registers.sp.set(next_sp); | ||
self.cpu.memory.write_16(self.cpu.registers.sp.get(), next_pc); | ||
|
||
address | ||
} | ||
|
||
fn call_conditional(&mut self, condition: &FlagCondition) -> u16 { | ||
let address = self.cpu.memory.read_16(self.cpu.pc + 1); | ||
let next_pc = self.cpu.pc.wrapping_add(3); | ||
|
||
if self.cpu.resolve_flag_condition(&condition) { | ||
let next_sp = self.cpu.registers.sp.get().wrapping_sub(2); | ||
self.cpu.registers.sp.set(next_sp); | ||
self.cpu.memory.write_16(self.cpu.registers.sp.get(), next_pc); | ||
|
||
self.cpu.pc = address; | ||
} | ||
|
||
next_pc | ||
} | ||
} | ||
|
||
impl Command for CallCommand<'_> { | ||
fn execute(&mut self) -> u16 { | ||
match &self.instruction { | ||
CallInstruction::Call => self.call(), | ||
CallInstruction::CallCond(condition) => self.call_conditional(condition), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::cpu::{ | ||
instructions::{FlagCondition, JumpInstruction}, | ||
registers::Register, | ||
Cpu, | ||
}; | ||
|
||
use super::Command; | ||
|
||
pub struct JumpCommand<'a> { | ||
instruction: &'a JumpInstruction, | ||
cpu: &'a mut Cpu, | ||
} | ||
|
||
impl<'a> JumpCommand<'a> { | ||
pub fn new(instruction: &'a JumpInstruction, cpu: &'a mut Cpu) -> JumpCommand<'a> { | ||
JumpCommand { instruction, cpu } | ||
} | ||
|
||
fn jp(&mut self) -> u16 { | ||
self.cpu.memory.read_16(self.cpu.pc + 1) | ||
} | ||
|
||
fn jp_cc(&mut self, condition: &FlagCondition) -> u16 { | ||
let address = self.cpu.memory.read_16(self.cpu.pc + 1); | ||
if self.cpu.resolve_flag_condition(condition) { | ||
address | ||
} else { | ||
self.cpu.pc.wrapping_add(3) | ||
} | ||
} | ||
|
||
fn jp_hl(&mut self) -> u16 { | ||
self.cpu.registers.get_16(&Register::HL) | ||
} | ||
|
||
fn jr(&mut self) -> u16 { | ||
let offset = self.cpu.memory.read(self.cpu.pc + 1) as u16; | ||
self.cpu.pc.wrapping_add(offset) | ||
} | ||
|
||
fn jr_cc(&mut self, condition: &FlagCondition) -> u16 { | ||
let offset = self.cpu.memory.read(self.cpu.pc + 1) as u16; | ||
if self.cpu.resolve_flag_condition(condition) { | ||
self.cpu.pc.wrapping_add(offset) | ||
} else { | ||
self.cpu.pc.wrapping_add(2) | ||
} | ||
} | ||
} | ||
|
||
impl Command for JumpCommand<'_> { | ||
fn execute(&mut self) -> u16 { | ||
match &self.instruction { | ||
JumpInstruction::Jp => self.jp(), | ||
JumpInstruction::JpCond(condition) => self.jp_cc(condition), | ||
JumpInstruction::JpHL => self.jp_hl(), | ||
JumpInstruction::Jr => self.jr(), | ||
JumpInstruction::JrCond(condition) => self.jr_cc(condition), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::cpu::{ | ||
instructions::{FlagCondition, ReturnInstruction}, | ||
Cpu, | ||
}; | ||
|
||
use super::Command; | ||
|
||
pub struct ReturnCommand<'a> { | ||
instruction: &'a ReturnInstruction, | ||
cpu: &'a mut Cpu, | ||
} | ||
|
||
impl<'a> ReturnCommand<'a> { | ||
pub fn new(instruction: &'a ReturnInstruction, cpu: &'a mut Cpu) -> ReturnCommand<'a> { | ||
ReturnCommand { instruction, cpu } | ||
} | ||
|
||
fn rst(&mut self, address: &u8) -> u16 { | ||
let current_address = self.cpu.memory.read_16(self.cpu.pc); | ||
let next_sp = self.cpu.registers.sp.get().wrapping_sub(2); | ||
self.cpu.registers.sp.set(next_sp); | ||
self.cpu | ||
.memory | ||
.write_16(self.cpu.registers.sp.get(), current_address); | ||
|
||
0x0000 + (*address as u16) | ||
} | ||
|
||
fn ret(&mut self) -> u16 { | ||
let address = self.cpu.memory.read_16(self.cpu.registers.sp.get()); | ||
self.cpu | ||
.registers | ||
.sp | ||
.set(self.cpu.registers.sp.get().wrapping_add(2)); | ||
|
||
address | ||
} | ||
|
||
fn ret_conditional(&mut self, condition: &FlagCondition) -> u16 { | ||
if self.cpu.resolve_flag_condition(&condition) { | ||
let address = self.cpu.memory.read_16(self.cpu.registers.sp.get()); | ||
self.cpu | ||
.registers | ||
.sp | ||
.set(self.cpu.registers.sp.get().wrapping_add(2)); | ||
address | ||
} else { | ||
self.cpu.pc.wrapping_add(1) | ||
} | ||
} | ||
} | ||
|
||
impl Command for ReturnCommand<'_> { | ||
fn execute(&mut self) -> u16 { | ||
match &self.instruction { | ||
ReturnInstruction::Rst(address) => self.rst(&address), | ||
ReturnInstruction::Ret => self.ret(), | ||
ReturnInstruction::RetCond(condition) => self.ret_conditional(condition), | ||
_ => unimplemented!(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters