Refactor arith8 parsers to avoid repetitive parses

old-bit-manip
Nick Krichevsky 2023-05-09 22:59:07 -04:00
parent 59bafee838
commit b4b72c73dd
5 changed files with 124 additions and 175 deletions

View File

@ -1,4 +1,50 @@
use crate::{
cpu::instructions::{Instruction, RunnableInstruction},
memory::{GetViewTuple, View},
register,
};
use super::ParseOutput;
pub mod add;
pub mod and;
pub mod sub;
pub mod xor;
fn build_operation_between_register_and_a_data<F: Fn(register::SingleEightBit) -> Instruction>(
src: register::SingleEightBit,
make_instruction: F,
) -> ParseOutput {
(
RunnableInstruction {
instruction: make_instruction(src),
cycles: 4,
},
1,
)
}
fn build_operation_between_hl_value_and_a_data(instruction: Instruction) -> ParseOutput {
(
RunnableInstruction {
instruction,
cycles: 8,
},
1,
)
}
fn build_operation_between_immediate_and_a_data<F: Fn(u8) -> Instruction>(
data: &View,
make_instruction: F,
) -> ParseOutput {
let (_opcode, n) = data.get_tuple();
(
RunnableInstruction {
instruction: make_instruction(n),
cycles: 8,
},
2,
)
}

View File

@ -1,9 +1,9 @@
use crate::{
cpu::{
instructions::{arith8::EightBitAddInstruction, Instruction, RunnableInstruction},
instructions::{arith8::EightBitAddInstruction, Instruction},
parse::{self, Error, OpcodeParser, ParseOutput, ParseResult},
},
memory::{GetViewTuple, View},
memory::View,
register,
};
@ -55,71 +55,39 @@ impl OpcodeParser for EightBitAddParser {
}
fn build_add_register_to_a_data(src: register::SingleEightBit) -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitAdd(EightBitAddInstruction::AddSingleRegisterToA {
src,
}),
cycles: 4,
},
1,
)
super::build_operation_between_register_and_a_data(src, |register| {
Instruction::EightBitAdd(EightBitAddInstruction::AddSingleRegisterToA { src: register })
})
}
fn build_add_register_to_a_with_carry_data(src: register::SingleEightBit) -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitAdd(
EightBitAddInstruction::AddSingleRegisterToAWithCarry { src },
),
cycles: 4,
},
1,
)
super::build_operation_between_register_and_a_data(src, |register| {
Instruction::EightBitAdd(EightBitAddInstruction::AddSingleRegisterToAWithCarry {
src: register,
})
})
}
fn build_add_hl_address_to_a_data() -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitAdd(EightBitAddInstruction::AddHLAddressToA),
cycles: 8,
},
1,
)
}
fn build_add_immediate_to_a_data(data: &View) -> ParseOutput {
let (_opcode, n) = data.get_tuple();
(
RunnableInstruction {
instruction: Instruction::EightBitAdd(EightBitAddInstruction::AddImmediateToA { n }),
cycles: 8,
},
2,
)
}
fn build_add_immediate_to_a_with_carry_data(data: &View) -> ParseOutput {
let (_opcode, n) = data.get_tuple();
(
RunnableInstruction {
instruction: Instruction::EightBitAdd(
EightBitAddInstruction::AddImmediateToAWithCarry { n },
),
cycles: 8,
},
2,
)
super::build_operation_between_hl_value_and_a_data(Instruction::EightBitAdd(
EightBitAddInstruction::AddHLAddressToA,
))
}
fn build_add_hl_address_to_a_with_carry_data() -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitAdd(EightBitAddInstruction::AddHLAddressToAWithCarry),
cycles: 8,
},
1,
)
super::build_operation_between_hl_value_and_a_data(Instruction::EightBitAdd(
EightBitAddInstruction::AddHLAddressToAWithCarry,
))
}
fn build_add_immediate_to_a_data(data: &View) -> ParseOutput {
super::build_operation_between_immediate_and_a_data(data, |n| {
Instruction::EightBitAdd(EightBitAddInstruction::AddImmediateToA { n })
})
}
fn build_add_immediate_to_a_with_carry_data(data: &View) -> ParseOutput {
super::build_operation_between_immediate_and_a_data(data, |n| {
Instruction::EightBitAdd(EightBitAddInstruction::AddImmediateToAWithCarry { n })
})
}

View File

@ -1,9 +1,9 @@
use crate::{
cpu::{
instructions::{arith8::EightBitAndInstruction, Instruction, RunnableInstruction},
instructions::{arith8::EightBitAndInstruction, Instruction},
parse::{self, Error, OpcodeParser, ParseOutput, ParseResult},
},
memory::{GetViewTuple, View},
memory::View,
register,
};
@ -28,35 +28,19 @@ impl OpcodeParser for EightBitAndParser {
}
fn build_and_register_with_a_data(src: register::SingleEightBit) -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitAnd(EightBitAndInstruction::AndSingleRegisterWithA {
src,
}),
cycles: 4,
},
1,
)
super::build_operation_between_register_and_a_data(src, |register| {
Instruction::EightBitAnd(EightBitAndInstruction::AndSingleRegisterWithA { src: register })
})
}
fn build_and_hl_value_with_a_data() -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitAnd(EightBitAndInstruction::AndHLAddressWithA),
cycles: 8,
},
1,
)
super::build_operation_between_hl_value_and_a_data(Instruction::EightBitAnd(
EightBitAndInstruction::AndHLAddressWithA,
))
}
fn build_and_immediate_with_a_data(data: &View) -> ParseOutput {
let (_opcode, n) = data.get_tuple();
(
RunnableInstruction {
instruction: Instruction::EightBitAnd(EightBitAndInstruction::AndImmediateWithA { n }),
cycles: 8,
},
2,
)
super::build_operation_between_immediate_and_a_data(data, |n| {
Instruction::EightBitAnd(EightBitAndInstruction::AndImmediateWithA { n })
})
}

View File

@ -1,9 +1,9 @@
use crate::{
cpu::{
instructions::{arith8::EightBitSubInstruction, Instruction, RunnableInstruction},
instructions::{arith8::EightBitSubInstruction, Instruction},
parse::{self, Error, OpcodeParser, ParseOutput, ParseResult},
},
memory::{GetViewTuple, View},
memory::View,
register,
};
@ -51,75 +51,41 @@ impl OpcodeParser for EightBitSubParser {
}
fn build_sub_register_from_a_data(src_register: register::SingleEightBit) -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitSub(EightBitSubInstruction::SubSingleRegisterFromA {
src: src_register,
}),
cycles: 4,
},
1,
)
}
fn build_sub_hl_value_from_a_data() -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitSub(EightBitSubInstruction::SubHLAddressFromA),
cycles: 8,
},
1,
)
}
fn build_sub_immediate_from_a_data(data: &View) -> ParseOutput {
let (_opcode, n) = data.get_tuple();
(
RunnableInstruction {
instruction: Instruction::EightBitSub(EightBitSubInstruction::SubImmediateFromA { n }),
cycles: 8,
},
2,
)
super::build_operation_between_register_and_a_data(src_register, |register| {
Instruction::EightBitSub(EightBitSubInstruction::SubSingleRegisterFromA { src: register })
})
}
fn build_sub_register_from_a_with_carry_data(
src_register: register::SingleEightBit,
) -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitSub(
EightBitSubInstruction::SubSingleRegisterFromAWithCarry { src: src_register },
),
cycles: 4,
},
1,
)
super::build_operation_between_register_and_a_data(src_register, |register| {
Instruction::EightBitSub(EightBitSubInstruction::SubSingleRegisterFromAWithCarry {
src: register,
})
})
}
fn build_sub_hl_value_from_a_data() -> ParseOutput {
super::build_operation_between_hl_value_and_a_data(Instruction::EightBitSub(
EightBitSubInstruction::SubHLAddressFromA,
))
}
fn build_sub_hl_value_from_a_with_carry_data() -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitSub(
EightBitSubInstruction::SubHLAddressFromAWithCarry,
),
cycles: 8,
},
1,
)
super::build_operation_between_hl_value_and_a_data(Instruction::EightBitSub(
EightBitSubInstruction::SubHLAddressFromAWithCarry,
))
}
fn build_sub_immediate_from_a_data(data: &View) -> ParseOutput {
super::build_operation_between_immediate_and_a_data(data, |n| {
Instruction::EightBitSub(EightBitSubInstruction::SubImmediateFromA { n })
})
}
fn build_sub_immediate_from_a_with_carry_data(data: &View) -> ParseOutput {
let (_opcode, n) = data.get_tuple();
(
RunnableInstruction {
instruction: Instruction::EightBitSub(
EightBitSubInstruction::SubImmediateFromAWithCarry { n },
),
cycles: 8,
},
2,
)
super::build_operation_between_immediate_and_a_data(data, |n| {
Instruction::EightBitSub(EightBitSubInstruction::SubImmediateFromAWithCarry { n })
})
}

View File

@ -1,9 +1,9 @@
use crate::{
cpu::{
instructions::{arith8::EightBitXorInstruction, Instruction, RunnableInstruction},
instructions::{arith8::EightBitXorInstruction, Instruction},
parse::{self, Error, OpcodeParser, ParseOutput, ParseResult},
},
memory::{GetViewTuple, View},
memory::View,
register,
};
@ -28,34 +28,19 @@ impl OpcodeParser for EightBitXorParser {
}
fn build_xor_register_with_a_data(src: register::SingleEightBit) -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitXor(EightBitXorInstruction::XorSingleRegisterWithA {
src,
}),
cycles: 4,
},
1,
)
super::build_operation_between_register_and_a_data(src, |register| {
Instruction::EightBitXor(EightBitXorInstruction::XorSingleRegisterWithA { src: register })
})
}
fn build_xor_hl_value_with_a_data() -> ParseOutput {
(
RunnableInstruction {
instruction: Instruction::EightBitXor(EightBitXorInstruction::XorHLAddressWithA),
cycles: 8,
},
1,
)
super::build_operation_between_hl_value_and_a_data(Instruction::EightBitXor(
EightBitXorInstruction::XorHLAddressWithA,
))
}
fn build_xor_immediate_with_a_data(data: &View) -> ParseOutput {
let (_opcode, n) = data.get_tuple();
(
RunnableInstruction {
instruction: Instruction::EightBitXor(EightBitXorInstruction::XorImmediateWithA { n }),
cycles: 9,
},
2,
)
super::build_operation_between_immediate_and_a_data(data, |n| {
Instruction::EightBitXor(EightBitXorInstruction::XorImmediateWithA { n })
})
}