Skip to content

Commit

Permalink
Merge pull request #47 from 0xPolygonHermez/edu/change_ziskjs_to_ziskemu
Browse files Browse the repository at this point in the history
Introduce ziskemu as a sim mode and minor fixes
  • Loading branch information
eduadiez authored Aug 5, 2024
2 parents 679a4ca + 5b31e47 commit 3d0acc6
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 47 additions & 15 deletions cli/src/bin/cargo-zisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use std::{

use std::{fs::File, io::Write, path::Path};

const DEFAULT_INPUT_VALUE: &str = "build/input.bin";
const ZISK_TARGET: &str = "riscv64ima-polygon-ziskos-elf";

// Main enum defining cargo subcommands.
#[derive(Parser)]
#[command(name = "cargo-zisk", bin_name = "cargo-zisk", version = ZISK_VERSION_MESSAGE)]
Expand Down Expand Up @@ -41,12 +44,16 @@ pub struct ZiskRun {
release: bool,
#[clap(long)]
no_default_features: bool,
#[clap(long)]
#[clap(long, short)]
sim: bool,
#[clap(long)]
stats: bool,
#[clap(long)]
gdb: bool,
#[clap(long, short, default_value = DEFAULT_INPUT_VALUE)]
input: Option<String>,
#[clap(long, short)]
metrics: bool,
#[clap(last = true)]
args: Vec<String>,
}
Expand Down Expand Up @@ -81,46 +88,71 @@ impl ZiskRun {
command.arg("--release");
}
if self.sim {
let mut stats_command = "";
let mut extra_command: String = "".to_string();
let mut input_command: String = "".to_string();
if self.stats {
stats_command = "-s"
extra_command += " -s ";
}
runner_command = format!(
"node /home/edu/ziskjs/src/sim/main.js -n 100000000000 -i output/input.bin {} -e",
stats_command
);
if self.metrics {
extra_command += " -m ";
}
if self.input.is_some() {
let path = Path::new(self.input.as_ref().unwrap());
if !path.exists() {
return Err(anyhow!("Input file does not exist at path: {}", path.display()));
}
input_command = format!("-i {}", self.input.as_ref().unwrap());
}
runner_command = format!("ziskemu {} {} -e", input_command, extra_command);
} else {
let mut gdb_command = "";
if self.gdb {
gdb_command = "-S";
}

let input_filename = "output/input.bin";
let output_filename = "output/input_size.bin";
let input_path: &Path = Path::new(self.input.as_ref().unwrap());

let input_path = Path::new(input_filename);
let metadata = std::fs::metadata(input_path)?;
if !input_path.exists() {
return Err(anyhow!("Input file does not exist at path: {}", input_path.display()));
}

let build_path = match input_path.parent() {
Some(parent) => parent.to_str().unwrap_or("./"),
None => "./",
};

let stem = input_path.file_stem().unwrap_or_default();
let extension = input_path.extension().unwrap_or_default();
let output_path = format!(
"{}{}_size.{}",
build_path,
stem.to_str().unwrap_or(""),
extension.to_str().unwrap_or("")
);

let metadata = std::fs::metadata(input_path)?;
let file_size = metadata.len();

let size_bytes = file_size.to_le_bytes();
let mut output_file = File::create(output_filename)?;
let mut output_file = File::create(output_path.clone())?;
output_file.write_all(&size_bytes)?;

runner_command = format!(
"
qemu-system-riscv64 \
-cpu rv64 \
-machine virt \
-device loader,file=./output/input_size.bin,addr=0x90000000 \
-device loader,file=./output/input.bin,addr=0x90000008 \
-device loader,file=./{},addr=0x90000000 \
-device loader,file=./{},addr=0x90000008 \
-m 1G \
-s \
{} \
-nographic \
-serial mon:stdio \
-bios none \
-kernel",
output_path,
input_path.display(),
gdb_command
);
}
Expand All @@ -132,7 +164,7 @@ impl ZiskRun {
env::var("CARGO_TARGET_RISCV64IMA_POLYGON_ZISKOS_ELF_RUNNER").unwrap()
);

command.args(["--target", "riscv64ima-polygon-ziskos-elf"]);
command.args(["--target", ZISK_TARGET]);

// Add any additional arguments passed to the run command
command.args(&self.args);
Expand Down
10 changes: 8 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ use std::{

pub const RUSTUP_TOOLCHAIN_NAME: &str = "zisk";

pub const ZISK_VERSION_MESSAGE: &str =
concat!("zisk", " (", env!("VERGEN_GIT_SHA"), " ", env!("VERGEN_BUILD_TIMESTAMP"), ")");
pub const ZISK_VERSION_MESSAGE: &str = concat!(
env!("CARGO_PKG_VERSION"),
" (",
env!("VERGEN_GIT_SHA"),
" ",
env!("VERGEN_BUILD_TIMESTAMP"),
")"
);

trait CommandExecutor {
fn run(&mut self) -> Result<()>;
Expand Down
18 changes: 13 additions & 5 deletions emulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ path = "src/lib.rs"
name = "ziskemu"
path = "src/bin/ziskemu.rs"

[dependencies]
clap = { version = "4.5.9", features = ["derive", "env"] }
zisk-core = {path="../core"}
riscv = {path="../riscv"}
[build-dependencies]
vergen = { version = "8", default-features = false, features = [
"build",
"git",
"git2",
] }

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
pprof = { version = "0.13.0", features = ["flamegraph", "criterion"] }


[dependencies]
clap = { version = "4.5.9", features = ["derive", "env"] }
zisk-core = { path = "../core" }
riscv = { path = "../riscv" }

[[bench]]
name = "benchmark"
harness = false
harness = false
3 changes: 3 additions & 0 deletions emulator/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
vergen::EmitBuilder::builder().build_timestamp().git_sha(true).emit().unwrap();
}
11 changes: 10 additions & 1 deletion emulator/src/emu_options.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
use clap::Parser;
use std::fmt;

pub const ZISK_VERSION_MESSAGE: &str = concat!(
env!("CARGO_PKG_VERSION"),
" (",
env!("VERGEN_GIT_SHA"),
" ",
env!("VERGEN_BUILD_TIMESTAMP"),
")"
);

/// ZisK emulator options structure
#[derive(Parser, Debug, Clone)]
#[command(version, about, long_about = None)]
#[command(version = ZISK_VERSION_MESSAGE, about, long_about = None)]
#[command(propagate_version = true)]
pub struct EmuOptions {
/// Sets the Zisk ROM data file path
Expand Down
8 changes: 5 additions & 3 deletions emulator/src/mem.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use zisk_core::UART_ADDR;

use crate::MemSection;

/// Memory structure, containing several read sections and one single write section
Expand Down Expand Up @@ -152,8 +154,8 @@ impl Mem {
}

// Log to console bytes written to UART address
// if (addr == UART_ADDR) && (width == 1) {
// print!("{}", String::from(val as u8 as char));
// }
if (addr == UART_ADDR) && (width == 1) {
print!("{}", String::from(val as u8 as char));
}
}
}
2 changes: 1 addition & 1 deletion ziskos/entrypoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn read_input() -> Vec<u8> {
pub fn read_input() -> Vec<u8> {
use std::{fs::File, io::Read};

let mut file = File::open("output/input.bin").unwrap();
let mut file = File::open("build/input.bin").unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
buffer
Expand Down

0 comments on commit 3d0acc6

Please sign in to comment.