base (clock): implemented a basic clocking structure

This commit is contained in:
antifallobst 2023-03-19 10:45:25 +01:00
parent d583605e06
commit 6ae32ac2f2
7 changed files with 111 additions and 11 deletions

2
Cargo.toml Normal file
View File

@ -0,0 +1,2 @@
[workspace]
members = ["base", "architectures/*"]

View File

@ -12,3 +12,5 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
console_error_panic_hook = "0.1"
chrono = "0.4"

57
base/src/clock/mod.rs Normal file
View File

@ -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<fn()>
}
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 {};
}
}
}

View File

@ -1,11 +1,24 @@
mod clock;
mod utils;
extern crate console_error_panic_hook;
use std::ptr::null;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use crate::clock::NemuClock;
use crate::utils::utils_log;
#[wasm_bindgen] fn test() {
extern { utils_log("test");
pub fn alert(s: &str);
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn greet(name: &str) { pub fn nemu_init() {
alert(&format!("Hello, {}!", name)); 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();
} }

18
base/src/utils/mod.rs Normal file
View File

@ -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));
}

View File

@ -38,10 +38,10 @@
<script src="test.js"></script> <script src="test.js"></script>
<script type="module"> <script type="module">
import init, { greet } from "./base/pkg/nemu_base.js"; import init, { nemu_init } from "./base/pkg/nemu_base.js";
init().then(() => { init().then(() => {
greet("NerdEMU"); nemu_init();
}); });
</script> </script>
</html> </html>

View File

@ -2,5 +2,13 @@
"name": "i8086", "name": "i8086",
"version": "0.0", "version": "0.0",
"architecture": "x86" "architecture": {
"id": "x86"
},
"devices": {
"monitor": {
"mode": "VGA"
}
}
} }