Restructuring into multiple crates and projects
This commit is contained in:
11
hm-asm-cli/Cargo.toml
Normal file
11
hm-asm-cli/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "hm-asm-cli"
|
||||
version = "0.1.0"
|
||||
authors = ["Henrik Böving <hargonix@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
hm-asm-simulator = { path = "../hm-asm-simulator", version = "0.1.0" }
|
||||
pest = "2.0"
|
||||
28
hm-asm-cli/README.md
Normal file
28
hm-asm-cli/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# hm-asm-cli
|
||||
|
||||
A CLI frontend for `hm-asm-simulate`, it provides two commands:
|
||||
|
||||
## Generate
|
||||
You can generate the data and program memory for a program like this
|
||||
```
|
||||
$ cargo run -- generate examples/add_endless.asm
|
||||
Data Memory:
|
||||
0 1 1 0
|
||||
0 0 0 0
|
||||
0 0 0 0
|
||||
0 0 0 0
|
||||
Program Memory:
|
||||
1 4 8 0
|
||||
0 0 0 0
|
||||
0 0 0 0
|
||||
0 0 0 0
|
||||
And that's your program!
|
||||
```
|
||||
|
||||
## Simulate
|
||||
Alternatively you can simulate an asm program for n clock cycles like this:
|
||||
```
|
||||
$ cargo run -- simulate examples/add_endless.asm 4
|
||||
```
|
||||
It is going to proceed and print an HTML table of all states since the only purpose of this tool is to avoid using
|
||||
mahara as an in browser lab book -> we just autogenerate the tables.
|
||||
31
hm-asm-cli/src/html.rs
Normal file
31
hm-asm-cli/src/html.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use hm_asm_simulator::simulate::State;
|
||||
use std::fmt::Write;
|
||||
|
||||
static TABLE_HEADER: &str = "
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">Schritt</th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">clk</th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">PC<br></th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">Addressbus</th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">Datenbus</th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">IR</th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">DR</th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">A</th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">SR</th>
|
||||
<th style=\"border: 1px solid #000000; padding: 0mm 1.91mm;\">Bei Befehlen, die aus dem Speicher laden<br>bzw. in Speicher schreiben (LDA n, ADD n, STA n)</th>
|
||||
</tr>
|
||||
</thead>\n";
|
||||
|
||||
pub fn html_state_table(states: Vec<State>) -> String {
|
||||
let mut result = String::from(TABLE_HEADER);
|
||||
result.push_str("<tbody>\n");
|
||||
for state in states.iter() {
|
||||
write!(result, "{}", state).unwrap();
|
||||
}
|
||||
|
||||
result.push_str("</tbody>\n</table>");
|
||||
|
||||
result
|
||||
}
|
||||
50
hm-asm-cli/src/main.rs
Normal file
50
hm-asm-cli/src/main.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
||||
use hm_asm_simulator::{
|
||||
generate::generate_binary,
|
||||
parse::{parse_asm, AsmParser, Rule},
|
||||
simulate::simulate
|
||||
};
|
||||
|
||||
use pest::Parser;
|
||||
|
||||
mod html;
|
||||
use html::html_state_table;
|
||||
|
||||
|
||||
fn main() {
|
||||
let sub_cmd = env::args().nth(1);
|
||||
let file_name = env::args().nth(2);
|
||||
let steps = env::args().nth(3);
|
||||
|
||||
if let Some(sub_cmd) = sub_cmd {
|
||||
let file_content = match file_name {
|
||||
Some(file_name) => {
|
||||
fs::read_to_string(file_name).expect("Could not read the provided asm file")
|
||||
}
|
||||
None => {
|
||||
println!("No input file was provided");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let instructions = parse_asm(
|
||||
AsmParser::parse(Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e)),
|
||||
);
|
||||
|
||||
if sub_cmd == "generate" {
|
||||
let binary = generate_binary(instructions);
|
||||
println!("{}", binary);
|
||||
}
|
||||
else if sub_cmd == "simulate" {
|
||||
if let Some(steps) = steps {
|
||||
let states = simulate(instructions, steps.parse::<usize>().unwrap());
|
||||
//println!("{:#?}", states);
|
||||
println!("{}", html_state_table(states));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("No argument was passed, exiting");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user