machine_check_avr/
lib.rs

1#![doc = include_str!("../README.md")]
2
3mod system;
4mod util;
5
6use clap::Args;
7use machine_check::Bitvector;
8use machine_check::BitvectorArray;
9use machine_check::ExecArgs;
10use machine_check::ExecError;
11use machine_check::ExecResult;
12use machine_check::ExecStats;
13pub use system::machine_module::ATmega328P;
14pub use system::machine_module::Input;
15pub use system::machine_module::State;
16
17pub use util::read_hex_into_progmem;
18
19/// Execute machine-check-avr as if called from the command line.
20///
21/// The arguments are supplied as if they were entered from the command line.
22pub fn execute(args: impl Iterator<Item = String>) -> ExecResult {
23    let (exec_args, system_args) = machine_check::parse_args::<SystemArgs>(args);
24    execute_with_args(exec_args, system_args)
25}
26
27/// Execute machine-check-avr with given argument structures.
28pub fn execute_with_args(exec_args: ExecArgs, system_args: SystemArgs) -> ExecResult {
29    let hex = match std::fs::read_to_string(system_args.hex_file) {
30        Ok(ok) => ok,
31        Err(err) => {
32            eprintln!("Could not read hex file: {}", err);
33            return ExecResult {
34                result: Err(ExecError::OtherError(String::from(
35                    "Could not read hex file",
36                ))),
37                stats: ExecStats::default(),
38            };
39        }
40    };
41
42    // fill with ones which is a reserved instruction
43    // TODO: keep track of which progmem locations are filled instead
44    let all_ones = Bitvector::new(0xFFFF);
45    let mut progmem = BitvectorArray::new_filled(all_ones);
46
47    read_hex_into_progmem(&mut progmem, &hex);
48
49    let system = ATmega328P { PROGMEM: progmem };
50    machine_check::execute(system, exec_args)
51}
52
53#[derive(Args)]
54pub struct SystemArgs {
55    /// The machine-code program in an Intel Hex file.
56    #[arg(short = 'H', long = "system-hex-file")]
57    pub hex_file: String,
58}