Implement loading to the stack pointer

jsmoo
Nick Krichevsky 2022-04-09 14:00:33 -04:00
parent d85c7b8a98
commit 034811c54e
3 changed files with 30 additions and 13 deletions

View File

@ -117,8 +117,8 @@ impl Processor {
let load_res = self.load_from_address_to_register(dst, src_address); let load_res = self.load_from_address_to_register(dst, src_address);
assert_ok!(load_res); assert_ok!(load_res);
} }
Instruction::LD16bitImmediateToRegister { value, dst } => { Instruction::LD16bitImmediateToRegister { dst, value } => {
self.registers.set_combined_register(dst, value) self.registers.set_16bit_register(dst, value);
} }
} }
@ -517,12 +517,16 @@ mod tests {
assert_eq!(0xAF, processor.registers.a); assert_eq!(0xAF, processor.registers.a);
} }
#[test_case(0x01, register::Combined::BC)] #[test_case(0x01, register::SixteenBit::Combined(register::Combined::BC))]
#[test_case(0x11, register::Combined::DE)] #[test_case(0x11, register::SixteenBit::Combined(register::Combined::DE))]
#[test_case(0x21, register::Combined::HL)] #[test_case(0x21, register::SixteenBit::Combined(register::Combined::HL))]
fn test_load_16bit_immediate_to_regisetr( #[test_case(
0x31,
register::SixteenBit::Single(register::SingleSixteenBit::StackPointer)
)]
fn test_load_16bit_immediate_to_register(
opcode: u8, opcode: u8,
expected_dst_register: register::Combined, expected_dst_register: register::SixteenBit,
) { ) {
let mut processor = Processor::default(); let mut processor = Processor::default();
// The manual doesn't specify this is little endian, but from what I can gather // The manual doesn't specify this is little endian, but from what I can gather
@ -542,7 +546,7 @@ mod tests {
0x1234, 0x1234,
processor processor
.registers .registers
.get_combined_register(expected_dst_register) .get_16bit_register(expected_dst_register)
); );
} }
} }

View File

@ -82,7 +82,7 @@ pub enum Instruction {
// 3.3.2.1 // 3.3.2.1
LD16bitImmediateToRegister { LD16bitImmediateToRegister {
value: u16, value: u16,
dst: register::Combined, dst: register::SixteenBit,
}, },
} }

View File

@ -12,15 +12,28 @@ impl OpcodeParser for Immediate16BitLoadParser {
let opcode = parse::get_opcode_from_data(data)?; let opcode = parse::get_opcode_from_data(data)?;
match opcode { match opcode {
0x01 => make_load_immediate_data(register::Combined::BC, data), 0x01 => make_load_immediate_data(
0x11 => make_load_immediate_data(register::Combined::DE, data), register::SixteenBit::Combined(register::Combined::BC),
0x21 => make_load_immediate_data(register::Combined::HL, data), data,
),
0x11 => make_load_immediate_data(
register::SixteenBit::Combined(register::Combined::DE),
data,
),
0x21 => make_load_immediate_data(
register::SixteenBit::Combined(register::Combined::HL),
data,
),
0x31 => make_load_immediate_data(
register::SixteenBit::Single(register::SingleSixteenBit::StackPointer),
data,
),
_ => Err(Error::UnknownOpcode(opcode)), _ => Err(Error::UnknownOpcode(opcode)),
} }
} }
} }
fn make_load_immediate_data(dst: register::Combined, data: &[u8]) -> ParseResult { fn make_load_immediate_data(dst: register::SixteenBit, data: &[u8]) -> ParseResult {
let opcode = parse::get_opcode_from_data(data)?; let opcode = parse::get_opcode_from_data(data)?;
let args = data.get(1..=2).ok_or(Error::NotEnoughArgs(opcode))?; let args = data.get(1..=2).ok_or(Error::NotEnoughArgs(opcode))?;