base (clock): implemented a basic clocking structure
This commit is contained in:
parent
d583605e06
commit
6ae32ac2f2
|
@ -0,0 +1,2 @@
|
|||
[workspace]
|
||||
members = ["base", "architectures/*"]
|
|
@ -12,3 +12,5 @@ crate-type = ["cdylib"]
|
|||
|
||||
[dependencies]
|
||||
wasm-bindgen = "0.2"
|
||||
console_error_panic_hook = "0.1"
|
||||
chrono = "0.4"
|
|
@ -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 {};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
|
@ -38,10 +38,10 @@
|
|||
|
||||
<script src="test.js"></script>
|
||||
<script type="module">
|
||||
import init, { greet } from "./base/pkg/nemu_base.js";
|
||||
import init, { nemu_init } from "./base/pkg/nemu_base.js";
|
||||
|
||||
init().then(() => {
|
||||
greet("NerdEMU");
|
||||
nemu_init();
|
||||
});
|
||||
</script>
|
||||
</html>
|
||||
|
|
|
@ -2,5 +2,13 @@
|
|||
"name": "i8086",
|
||||
"version": "0.0",
|
||||
|
||||
"architecture": "x86"
|
||||
"architecture": {
|
||||
"id": "x86"
|
||||
},
|
||||
|
||||
"devices": {
|
||||
"monitor": {
|
||||
"mode": "VGA"
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue