Make all Instructions Copy

old-bit-manip
Nick Krichevsky 2023-05-07 16:38:23 -04:00
parent 4fb9144c6d
commit 85253e2576
10 changed files with 17 additions and 17 deletions

View File

@ -39,7 +39,7 @@ impl Processor {
/// # Panics /// # Panics
/// Panics if an internal error occurred within the CPU. These are always bugs. /// Panics if an internal error occurred within the CPU. These are always bugs.
pub fn run_instruction(&mut self, instruction: &RunnableInstruction) { pub fn run_instruction(&mut self, instruction: &RunnableInstruction) {
let run_res = run::run_instruction(self, &instruction.instruction); let run_res = run::run_instruction(self, instruction.instruction);
if let Err(err) = run_res { if let Err(err) = run_res {
panic!("Fatal CPU error occured: {err}") panic!("Fatal CPU error occured: {err}")
} }

View File

@ -10,7 +10,7 @@ pub mod load8;
// these are indexed with the instruction numbers in this manual // these are indexed with the instruction numbers in this manual
// http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf // http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub enum Instruction { pub enum Instruction {
EightBitLoad(load8::EightBitLoadInstruction), EightBitLoad(load8::EightBitLoadInstruction),
SixteenBitLoad(load16::SixteenBitLoadInstruction), SixteenBitLoad(load16::SixteenBitLoadInstruction),

View File

@ -3,7 +3,7 @@ use crate::register;
// `EightBitAddInstruction` represents one of the Gameboy's instructions for performing addition on // `EightBitAddInstruction` represents one of the Gameboy's instructions for performing addition on
// eight bit numbers // eight bit numbers
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub enum EightBitAddInstruction { pub enum EightBitAddInstruction {
AddSingleRegisterToA { src: register::SingleEightBit }, AddSingleRegisterToA { src: register::SingleEightBit },
AddImmediateToA { n: u8 }, AddImmediateToA { n: u8 },
@ -15,7 +15,7 @@ pub enum EightBitAddInstruction {
// `EightBitSubInstruction` represents one of the Gameboy's instructions for performing addition on // `EightBitSubInstruction` represents one of the Gameboy's instructions for performing addition on
// eight bit numbers // eight bit numbers
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub enum EightBitSubInstruction { pub enum EightBitSubInstruction {
SubSingleRegisterFromA { src: register::SingleEightBit }, SubSingleRegisterFromA { src: register::SingleEightBit },
SubHLAddressFromA, SubHLAddressFromA,

View File

@ -2,7 +2,7 @@ use crate::register;
/// `SixteenBitLoadInstruction` represents one of the Gameboy's instructions for loading /// `SixteenBitLoadInstruction` represents one of the Gameboy's instructions for loading
/// a sixteen bit value either into a register or into memory /// a sixteen bit value either into a register or into memory
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub enum SixteenBitLoadInstruction { pub enum SixteenBitLoadInstruction {
// 3.3.2.1 // 3.3.2.1
LoadImmediateToRegister { LoadImmediateToRegister {

View File

@ -2,7 +2,7 @@ use crate::register;
/// `EightBitLoadInstruction` represents one of the Gameboy's instructions for loading /// `EightBitLoadInstruction` represents one of the Gameboy's instructions for loading
/// an eight bit value either into a register or into memory /// an eight bit value either into a register or into memory
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub enum EightBitLoadInstruction { pub enum EightBitLoadInstruction {
// 3.3.1.1 // 3.3.1.1
LoadImmediateToRegister { LoadImmediateToRegister {

View File

@ -27,13 +27,13 @@ pub enum Error {
} }
/// `InstructionRunner` takes a single instruction and runs it on the given processor. /// `InstructionRunner` takes a single instruction and runs it on the given processor.
trait InstructionRunner<I> { trait InstructionRunner<I: Copy> {
fn run_instruction(processor: &mut Processor, instruction: &I) -> Result<(), Error>; fn run_instruction(processor: &mut Processor, instruction: I) -> Result<(), Error>;
} }
/// `run_instruction` will run the given instruction on the processor. /// `run_instruction` will run the given instruction on the processor.
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
pub fn run_instruction(processor: &mut Processor, instruction: &Instruction) -> Result<(), Error> { pub fn run_instruction(processor: &mut Processor, instruction: Instruction) -> Result<(), Error> {
match instruction { match instruction {
Instruction::EightBitLoad(load_instruction) => { Instruction::EightBitLoad(load_instruction) => {
load8::EightBitLoadRunner::run_instruction(processor, load_instruction) load8::EightBitLoadRunner::run_instruction(processor, load_instruction)

View File

@ -35,9 +35,9 @@ impl EightBitAddRunner {
impl InstructionRunner<EightBitAddInstruction> for EightBitAddRunner { impl InstructionRunner<EightBitAddInstruction> for EightBitAddRunner {
fn run_instruction( fn run_instruction(
processor: &mut Processor, processor: &mut Processor,
instruction: &EightBitAddInstruction, instruction: EightBitAddInstruction,
) -> Result<(), Error> { ) -> Result<(), Error> {
match *instruction { match instruction {
EightBitAddInstruction::AddSingleRegisterToA { src } => { EightBitAddInstruction::AddSingleRegisterToA { src } => {
let (lhs, rhs) = super::gather_operands( let (lhs, rhs) = super::gather_operands(
processor, processor,

View File

@ -35,9 +35,9 @@ impl EightBitSubRunner {
impl InstructionRunner<EightBitSubInstruction> for EightBitSubRunner { impl InstructionRunner<EightBitSubInstruction> for EightBitSubRunner {
fn run_instruction( fn run_instruction(
processor: &mut Processor, processor: &mut Processor,
instruction: &EightBitSubInstruction, instruction: EightBitSubInstruction,
) -> Result<(), Error> { ) -> Result<(), Error> {
match *instruction { match instruction {
EightBitSubInstruction::SubSingleRegisterFromA { src } => { EightBitSubInstruction::SubSingleRegisterFromA { src } => {
let (lhs, rhs) = super::gather_operands( let (lhs, rhs) = super::gather_operands(
processor, processor,

View File

@ -10,9 +10,9 @@ impl InstructionRunner<SixteenBitLoadInstruction> for SixteenBitLoadRunner {
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
fn run_instruction( fn run_instruction(
processor: &mut Processor, processor: &mut Processor,
instruction: &SixteenBitLoadInstruction, instruction: SixteenBitLoadInstruction,
) -> Result<(), Error> { ) -> Result<(), Error> {
match *instruction { match instruction {
SixteenBitLoadInstruction::LoadImmediateToRegister { dst, value } => { SixteenBitLoadInstruction::LoadImmediateToRegister { dst, value } => {
processor.registers.set_16bit_register(dst, value); processor.registers.set_16bit_register(dst, value);

View File

@ -11,9 +11,9 @@ impl InstructionRunner<EightBitLoadInstruction> for EightBitLoadRunner {
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
fn run_instruction( fn run_instruction(
processor: &mut Processor, processor: &mut Processor,
instruction: &EightBitLoadInstruction, instruction: EightBitLoadInstruction,
) -> Result<(), Error> { ) -> Result<(), Error> {
match *instruction { match instruction {
EightBitLoadInstruction::LoadImmediateToRegister { value, register } => { EightBitLoadInstruction::LoadImmediateToRegister { value, register } => {
processor processor
.registers .registers