diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..bbba86f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,2 @@ +[workspace] +members = ["base", "architectures/*"] diff --git a/base/Cargo.toml b/base/Cargo.toml index 2627023..78437b3 100644 --- a/base/Cargo.toml +++ b/base/Cargo.toml @@ -11,4 +11,6 @@ repository = "https://git.nerdcult.net/nerdcult/nerdemu" crate-type = ["cdylib"] [dependencies] -wasm-bindgen = "0.2" \ No newline at end of file +wasm-bindgen = "0.2" +console_error_panic_hook = "0.1" +chrono = "0.4" \ No newline at end of file diff --git a/base/src/clock/mod.rs b/base/src/clock/mod.rs new file mode 100644 index 0000000..4bb052c --- /dev/null +++ b/base/src/clock/mod.rs @@ -0,0 +1,57 @@ +#[path = "../utils/mod.rs"] +mod utils; + +use chrono::{Utc}; +use crate::utils::utils_log; + +pub struct NemuClock { + hertz: u32, + cycle_duration: u32, + command_queue: Vec +} + +impl NemuClock { + pub fn init(hertz: u32) -> NemuClock{ + let mut clock = NemuClock { + hertz, + cycle_duration: 0, + command_queue: Vec::new() + }; + clock.hertz_set(hertz); + + return clock; + } + + pub fn debug_info(&mut self) { + utils_log(&format!("Clock -> Hertz: {} Cycle Duration (nano): {}", self.hertz, self.cycle_duration)); + } + + pub fn hertz_set(&mut self, hertz:u32) { + self.hertz = hertz; + self.cycle_duration = 1000000000 / hertz; // this is the duration of on cycle in nanoseconds + } + + pub fn hertz_get(self) -> u32 { + return self.hertz; + } + + pub fn add_command(&mut self, command:fn()) { + self.command_queue.push(command); + } + + pub fn run(&mut self) { + + while true { + let cycle_start = Utc::now().timestamp_subsec_nanos(); + let cycle_end = cycle_start + self.cycle_duration; + + utils_log(&format!("Clock -> Tick start: {} end: {}", cycle_start, cycle_end)); + + for command in &self.command_queue { + command(); + } + + while Utc::now().timestamp_subsec_nanos() < cycle_end {}; + } + } +} \ No newline at end of file diff --git a/base/src/lib.rs b/base/src/lib.rs index 1dadb36..e827b67 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -1,11 +1,24 @@ +mod clock; +mod utils; +extern crate console_error_panic_hook; + +use std::ptr::null; use wasm_bindgen::prelude::*; +use crate::clock::NemuClock; +use crate::utils::utils_log; -#[wasm_bindgen] -extern { - pub fn alert(s: &str); +fn test() { + utils_log("test"); } #[wasm_bindgen] -pub fn greet(name: &str) { - alert(&format!("Hello, {}!", name)); +pub fn nemu_init() { + console_error_panic_hook::set_once(); + utils_log("starting nemu"); + let mut clock = NemuClock::init(8000); + + clock.add_command(test, ); + + clock.debug_info(); + clock.run(); } diff --git a/base/src/utils/mod.rs b/base/src/utils/mod.rs new file mode 100644 index 0000000..116c967 --- /dev/null +++ b/base/src/utils/mod.rs @@ -0,0 +1,18 @@ +use wasm_bindgen::prelude::*; + + +#[wasm_bindgen] +extern { + fn alert(s: &str); + + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +pub fn utils_log(s: &str) { + log(&format!("{}", s)); +} + +pub fn utils_panic(s: &str) { + alert(&format!("Panic: {}", s)); +} \ No newline at end of file diff --git a/index.html b/index.html index 8a7da43..9a94d88 100644 --- a/index.html +++ b/index.html @@ -38,10 +38,10 @@ diff --git a/systems/8086.json b/systems/8086.json index 20c3b50..5ae2e2e 100644 --- a/systems/8086.json +++ b/systems/8086.json @@ -1,6 +1,14 @@ { - "name": "i8086", - "version": "0.0", + "name": "i8086", + "version": "0.0", - "architecture": "x86" + "architecture": { + "id": "x86" + }, + + "devices": { + "monitor": { + "mode": "VGA" + } + } } \ No newline at end of file