Krolyxon
Commit fa3bbf2

Branches

Tags

Refs →

bench51srcmain.rs

95 lines · 2.5 KB · Rust

1mod asm;
2mod isa;
3mod sfr;
4mod sim;
5mod ui;
6
7use std::path::PathBuf;
8use std::process::ExitCode;
9
10use clap::Parser;
11
12/// Intel 8051 assembler, simulator and TUI debugger.
13#[derive(Parser, Debug)]
14#[command(version, about)]
15struct Args {
16 /// Assembly source (.asm) or Intel HEX (.hex) file to open.
17 file: Option<PathBuf>,
18
19 /// Assemble FILE without starting the TUI, writing the Intel HEX file.
20 #[arg(short, long, requires = "file")]
21 assemble: bool,
22
23 /// Output HEX path (default: FILE with a .hex extension).
24 #[arg(short, long, requires = "assemble")]
25 output: Option<PathBuf>,
26
27 /// Also write a listing to this path (default: FILE with a .lst extension).
28 #[arg(long, requires = "assemble")]
29 lst: Option<PathBuf>,
30
31 /// Oscillator frequency in MHz, used to show elapsed time.
32 #[arg(long, default_value_t = 11.0592)]
33 clock: f64,
34}
35
36fn assemble_file(args: &Args) -> ExitCode {
37 let path = args.file.as_ref().unwrap();
38 let source = match std::fs::read_to_string(path) {
39 Ok(s) => s,
40 Err(e) => {
41 eprintln!("error: cannot read {}: {e}", path.display());
42 return ExitCode::FAILURE;
43 }
44 };
45 let out = asm::assemble(&source);
46 let lst = args
47 .lst
48 .clone()
49 .unwrap_or_else(|| path.with_extension("lst"));
50 if let Err(e) = std::fs::write(&lst, out.listing(&source)) {
51 eprintln!("error: cannot write {}: {e}", lst.display());
52 }
53 if !out.ok() {
54 for d in &out.diagnostics {
55 eprintln!(
56 "{}:{}:{}: error: {}",
57 path.display(),
58 d.line + 1,
59 d.col + 1,
60 d.msg
61 );
62 }
63 eprintln!("{} error(s); see {}", out.diagnostics.len(), lst.display());
64 return ExitCode::FAILURE;
65 }
66 let hex = args
67 .output
68 .clone()
69 .unwrap_or_else(|| path.with_extension("hex"));
70 if let Err(e) = std::fs::write(&hex, out.intel_hex()) {
71 eprintln!("error: cannot write {}: {e}", hex.display());
72 return ExitCode::FAILURE;
73 }
74 println!(
75 "{} bytes -> {}, listing -> {}",
76 out.code_size(),
77 hex.display(),
78 lst.display()
79 );
80 ExitCode::SUCCESS
81}
82
83fn main() -> ExitCode {
84 let args = Args::parse();
85 if args.assemble {
86 return assemble_file(&args);
87 }
88 match ui::run(args.file, args.clock) {
89 Ok(()) => ExitCode::SUCCESS,
90 Err(e) => {
91 eprintln!("error: {e}");
92 ExitCode::FAILURE
93 }
94 }
95}