Make a macro for the transfer module

jsmoo
Nick Krichevsky 2022-04-06 22:43:38 -04:00
parent e5609972c8
commit 8734a7fc8e
1 changed files with 72 additions and 250 deletions

View File

@ -1,265 +1,87 @@
use crate::register;
use crate::run::parse::Error;
use crate::run::{
instructions::RunnableInstruction,
parse::{self, OpcodeParser, ParseResult},
parse::{self, Error, OpcodeParser, ParseResult},
};
pub struct Between8BitRegisterParser;
macro_rules! match_opcode {
{$candidate_opcode: expr, $data: expr, $(($opcode: expr => dst=$dst: expr, src=$src: expr))+} => {
match $candidate_opcode {
$(
$opcode => Ok(RunnableInstruction::make_ldr1r2_data(
$dst,
$src,
$data
)),
)+
_ => Err(Error::UnknownOpcode($candidate_opcode))
}
};
}
impl OpcodeParser for Between8BitRegisterParser {
#[allow(clippy::too_many_lines)]
fn parse_opcode(data: &[u8]) -> ParseResult {
let opcode = parse::get_opcode_from_data(data)?;
match opcode {
0x7F => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::A,
register::Single::A,
data,
)),
0x78 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::A,
register::Single::B,
data,
)),
0x79 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::A,
register::Single::C,
data,
)),
0x7A => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::A,
register::Single::D,
data,
)),
0x7B => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::A,
register::Single::E,
data,
)),
0x7C => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::A,
register::Single::H,
data,
)),
0x7D => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::A,
register::Single::L,
data,
)),
match_opcode!(
opcode, data,
(0x7f => dst=register::Single::A, src=register::Single::A)
(0x78 => dst=register::Single::A, src=register::Single::B)
(0x79 => dst=register::Single::A, src=register::Single::C)
(0x7A => dst=register::Single::A, src=register::Single::D)
(0x7B => dst=register::Single::A, src=register::Single::E)
(0x7C => dst=register::Single::A, src=register::Single::H)
(0x7D => dst=register::Single::A, src=register::Single::L)
0x47 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::B,
register::Single::A,
data,
)),
0x40 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::B,
register::Single::B,
data,
)),
0x41 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::B,
register::Single::C,
data,
)),
0x42 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::B,
register::Single::D,
data,
)),
0x43 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::B,
register::Single::E,
data,
)),
0x44 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::B,
register::Single::H,
data,
)),
0x45 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::B,
register::Single::L,
data,
)),
0x4F => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::C,
register::Single::A,
data,
)),
0x48 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::C,
register::Single::B,
data,
)),
0x49 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::C,
register::Single::C,
data,
)),
0x4A => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::C,
register::Single::D,
data,
)),
0x4B => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::C,
register::Single::E,
data,
)),
0x4C => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::C,
register::Single::H,
data,
)),
0x4D => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::C,
register::Single::L,
data,
)),
0x57 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::D,
register::Single::A,
data,
)),
0x50 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::D,
register::Single::B,
data,
)),
0x51 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::D,
register::Single::C,
data,
)),
0x52 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::D,
register::Single::D,
data,
)),
0x53 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::D,
register::Single::E,
data,
)),
0x54 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::D,
register::Single::H,
data,
)),
0x55 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::D,
register::Single::L,
data,
)),
0x5F => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::E,
register::Single::A,
data,
)),
0x58 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::E,
register::Single::B,
data,
)),
0x59 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::E,
register::Single::C,
data,
)),
0x5A => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::E,
register::Single::D,
data,
)),
0x5B => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::E,
register::Single::E,
data,
)),
0x5C => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::E,
register::Single::H,
data,
)),
0x5D => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::E,
register::Single::L,
data,
)),
0x67 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::H,
register::Single::A,
data,
)),
0x60 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::H,
register::Single::B,
data,
)),
0x61 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::H,
register::Single::C,
data,
)),
0x62 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::H,
register::Single::D,
data,
)),
0x63 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::H,
register::Single::E,
data,
)),
0x64 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::H,
register::Single::H,
data,
)),
0x65 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::H,
register::Single::L,
data,
)),
0x6F => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::L,
register::Single::A,
data,
)),
0x68 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::L,
register::Single::B,
data,
)),
0x69 => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::L,
register::Single::C,
data,
)),
0x6A => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::L,
register::Single::D,
data,
)),
0x6B => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::L,
register::Single::E,
data,
)),
0x6C => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::L,
register::Single::H,
data,
)),
0x6D => Ok(RunnableInstruction::make_ldr1r2_data(
register::Single::L,
register::Single::L,
data,
)),
(0x47 => dst=register::Single::B, src=register::Single::A)
(0x40 => dst=register::Single::B, src=register::Single::B)
(0x41 => dst=register::Single::B, src=register::Single::C)
(0x42 => dst=register::Single::B, src=register::Single::D)
(0x43 => dst=register::Single::B, src=register::Single::E)
(0x44 => dst=register::Single::B, src=register::Single::H)
(0x45 => dst=register::Single::B, src=register::Single::L)
_ => Err(Error::UnknownOpcode(opcode)),
}
(0x4F => dst=register::Single::C, src=register::Single::A)
(0x48 => dst=register::Single::C, src=register::Single::B)
(0x49 => dst=register::Single::C, src=register::Single::C)
(0x4A => dst=register::Single::C, src=register::Single::D)
(0x4B => dst=register::Single::C, src=register::Single::E)
(0x4C => dst=register::Single::C, src=register::Single::H)
(0x4D => dst=register::Single::C, src=register::Single::L)
(0x57 => dst=register::Single::D, src=register::Single::A)
(0x50 => dst=register::Single::D, src=register::Single::B)
(0x51 => dst=register::Single::D, src=register::Single::C)
(0x52 => dst=register::Single::D, src=register::Single::D)
(0x53 => dst=register::Single::D, src=register::Single::E)
(0x54 => dst=register::Single::D, src=register::Single::H)
(0x55 => dst=register::Single::D, src=register::Single::L)
(0x5F => dst=register::Single::E, src=register::Single::A)
(0x58 => dst=register::Single::E, src=register::Single::B)
(0x59 => dst=register::Single::E, src=register::Single::C)
(0x5A => dst=register::Single::E, src=register::Single::D)
(0x5B => dst=register::Single::E, src=register::Single::E)
(0x5C => dst=register::Single::E, src=register::Single::H)
(0x5D => dst=register::Single::E, src=register::Single::L)
(0x67 => dst=register::Single::H, src=register::Single::A)
(0x60 => dst=register::Single::H, src=register::Single::B)
(0x61 => dst=register::Single::H, src=register::Single::C)
(0x62 => dst=register::Single::H, src=register::Single::D)
(0x63 => dst=register::Single::H, src=register::Single::E)
(0x64 => dst=register::Single::H, src=register::Single::H)
(0x65 => dst=register::Single::H, src=register::Single::L)
(0x6F => dst=register::Single::L, src=register::Single::A)
(0x68 => dst=register::Single::L, src=register::Single::B)
(0x69 => dst=register::Single::L, src=register::Single::C)
(0x6A => dst=register::Single::L, src=register::Single::D)
(0x6B => dst=register::Single::L, src=register::Single::E)
(0x6c => dst=register::Single::L, src=register::Single::H)
(0x6D => dst=register::Single::L, src=register::Single::L)
)
}
}