Skip to content

Instantly share code, notes, and snippets.

@Noxime
Created May 13, 2018 13:10
Show Gist options
  • Save Noxime/a54709f1c077528a29629e3140a7a4ee to your computer and use it in GitHub Desktop.
Save Noxime/a54709f1c077528a29629e3140a7a4ee to your computer and use it in GitHub Desktop.
extern crate clap;
use clap::{App, Arg};
use std::time::Instant;
fn main() {
let matches = App::new("N-VM")
.version(env!("CARGO_PKG_VERSION"))
.author("Noxim")
.about("Emulator for EAGLE dialect of SUBLEQ")
.arg(Arg::with_name("ram")
.short("m")
.long("mem")
.value_name("KB")
.help("How much RAM allocated")
.takes_value(true))
.get_matches();
let mem = match matches.value_of("ram").unwrap_or("1024").parse::<usize>() {
Ok(v) => v,
Err(why) => {
eprintln!("RAM value failed to parse: {}", why);
return;
}
} * 1024;
let mut mem = {
let mut v = vec![];
for i in include_str!("../../DubuOS/target/kernel.sq").split_whitespace() {
v.push(i.parse::<i64>().unwrap());
}
println!("Loaded: {} bytes", v.len());
for _ in v.len() .. mem {
v.push(0);
}
println!("Memory total: {} bytes", v.len());
v
};
println!("Running VM");
let start = Instant::now();
let mut pc = 0;
let mut ins: usize = 0;
loop {
ins += 1;
if pc > 1024*1024*1024 * 16 {
break;
}
let a_a = mem[pc];
let a_b = mem[pc+1];
if a_b < 0 {
print!("{}", mem[a_a as usize] as u8 as char);
pc += 3;
} else {
let a_c = mem[pc+2];
let a = mem[a_a as usize];
let b = mem[a_b as usize];
let s = b - a;
mem[a_b as usize] = s;
if s <= 0 {
pc = a_c as usize;
} else {
pc += 3;
}
}
}
println!("Halted");
let time = {
let t = start.elapsed();
t.as_secs() as f64 + (t.subsec_nanos() as f64 / 1_000_000_000.0)
};
println!("Ran for {:.2}s", time);
println!("Instructions {}", ins);
println!("Mhz {}", ins as f64 / time / 1_000_000.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment