msp430-help: Refactor opcode tables

This commit is contained in:
John 2024-01-26 18:25:34 -06:00
parent 04c5e2ddf3
commit f0379ff1d3

View File

@ -173,20 +173,49 @@ pub fn usage(opcode: &Opcode) -> (&'static str, &'static str) {
} }
} }
const SINGLE: [Opcode; 7] =
[Opcode::Rrc, Opcode::Swpb, Opcode::Rra, Opcode::Sxt, Opcode::Push, Opcode::Call, Opcode::Reti];
const JUMP: [Opcode; 8] =
[Opcode::Jnz, Opcode::Jz, Opcode::Jnc, Opcode::Jc, Opcode::Jn, Opcode::Jge, Opcode::Jl, Opcode::Jmp];
#[rustfmt::skip]
const DOUBLE: [Opcode; 12] = [
Opcode::Mov, Opcode::Add, Opcode::Addc, Opcode::Subc, Opcode::Sub, Opcode::Cmp,
Opcode::Dadd, Opcode::Bit, Opcode::Bic, Opcode::Bis, Opcode::Xor, Opcode::And,
];
#[rustfmt::skip]
const SIMULATED: [Opcode; 24] = [
Opcode::Nop, Opcode::Pop, Opcode::Br, Opcode::Ret, Opcode::Clrc, Opcode::Setc,
Opcode::Clrz, Opcode::Setz, Opcode::Clrn, Opcode::Setn, Opcode::Dint, Opcode::Eint,
Opcode::Rla, Opcode::Rlc, Opcode::Inv, Opcode::Clr, Opcode::Tst, Opcode::Dec,
Opcode::Decd, Opcode::Inc, Opcode::Incd, Opcode::Adc, Opcode::Dadc, Opcode::Sbc,
];
pub fn list_opcodes() { pub fn list_opcodes() {
header!("Single-operand instructions:"); let mut stdout = std::io::stdout().lock();
println!("rrc\tswpb\trra\tsxt\tpush\tcall\nreti"); header!(stdout, "Single-operand instructions:");
header!("Relative Jump instructions:"); let _ = write_opcode_list(&mut stdout, &SINGLE);
println!("jnz\tjz\tjnc\tjc\tjn\tjge\njl\tjmp"); header!(stdout, "Relative Jump instructions:");
header!("Double-operand instructions:"); let _ = write_opcode_list(&mut stdout, &JUMP);
println!("mov\tadd\taddc\tsubc\tsub\tcmp\ndadd\tbit\tbic\tbis\txor\tand"); header!(stdout, "Double-operand instructions:");
header!("Simulated instructions:"); let _ = write_opcode_list(&mut stdout, &DOUBLE);
println!("nop\tpop\tbr\tret\tclrc\tsetc\nclrz\tsetz\tclrn\tsetn\tdint\teint"); header!(stdout, "Simulated instructions:");
println!("rla\trlc\tinv\tclr\ttst\tdec\ndecd\tinc\tincd\tadc\tdadc\tsbc"); let _ = write_opcode_list(&mut stdout, &SIMULATED);
} }
macro header ($($x: expr),+) { fn write_opcode_list(mut f: impl std::io::Write, list: &[Opcode]) -> std::io::Result<()> {
{print!("{}",SetForegroundColor(Color::Cyan));print!($($x),+);println!("{}",ResetAttributes);} for (idx, opcode) in list.iter().enumerate() {
write!(f, "{opcode}{}", if idx % 6 == 5 { "\n" } else { "\t" })?;
}
if list.len() % 6 != 0 {
writeln!(f)?;
}
Ok(())
}
macro header ($f:ident, $($x: expr),+) {
{write!($f, "{}",SetForegroundColor(Color::Cyan)).ok();write!($f, $($x),+).ok();writeln!($f, "{}",ResetAttributes).ok();}
} }
macro footer ($($x: expr),+) { macro footer ($($x: expr),+) {
{print!("{}",SetForegroundColor(Color::DarkGray));print!($($x),+);println!("{}",ResetAttributes);} {print!("{}",SetForegroundColor(Color::DarkGray));print!($($x),+);println!("{}",ResetAttributes);}