From f0379ff1d3e522a60126d89009da40940057e1fd Mon Sep 17 00:00:00 2001 From: John Breaux Date: Fri, 26 Jan 2024 18:25:34 -0600 Subject: [PATCH] msp430-help: Refactor opcode tables --- examples/msp430-help/data.rs | 51 ++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/examples/msp430-help/data.rs b/examples/msp430-help/data.rs index 5b6c528..26e2a96 100644 --- a/examples/msp430-help/data.rs +++ b/examples/msp430-help/data.rs @@ -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() { - header!("Single-operand instructions:"); - println!("rrc\tswpb\trra\tsxt\tpush\tcall\nreti"); - header!("Relative Jump instructions:"); - println!("jnz\tjz\tjnc\tjc\tjn\tjge\njl\tjmp"); - header!("Double-operand instructions:"); - println!("mov\tadd\taddc\tsubc\tsub\tcmp\ndadd\tbit\tbic\tbis\txor\tand"); - header!("Simulated instructions:"); - println!("nop\tpop\tbr\tret\tclrc\tsetc\nclrz\tsetz\tclrn\tsetn\tdint\teint"); - println!("rla\trlc\tinv\tclr\ttst\tdec\ndecd\tinc\tincd\tadc\tdadc\tsbc"); + let mut stdout = std::io::stdout().lock(); + header!(stdout, "Single-operand instructions:"); + let _ = write_opcode_list(&mut stdout, &SINGLE); + header!(stdout, "Relative Jump instructions:"); + let _ = write_opcode_list(&mut stdout, &JUMP); + header!(stdout, "Double-operand instructions:"); + let _ = write_opcode_list(&mut stdout, &DOUBLE); + header!(stdout, "Simulated instructions:"); + let _ = write_opcode_list(&mut stdout, &SIMULATED); } -macro header ($($x: expr),+) { - {print!("{}",SetForegroundColor(Color::Cyan));print!($($x),+);println!("{}",ResetAttributes);} +fn write_opcode_list(mut f: impl std::io::Write, list: &[Opcode]) -> std::io::Result<()> { + 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),+) { {print!("{}",SetForegroundColor(Color::DarkGray));print!($($x),+);println!("{}",ResetAttributes);}