summaryrefslogtreecommitdiff
path: root/website-emu/src
diff options
context:
space:
mode:
authorNathan Lee <me@nwlee.tech>2026-09-26 16:22:04 -0500
committerNathan Lee <me@nwlee.tech>2026-09-26 16:22:04 -0500
commit9660366219c28fc827c24ce7b428ad7e2534ead0 (patch)
treeb4ca2c6d46bc4f0ba46961bc903c130b09b03934 /website-emu/src
parent548905a86473101325540446f35ffda8acd0c3bb (diff)
scaffold rest of website
Diffstat (limited to 'website-emu/src')
-rw-r--r--website-emu/src/dev.js6
-rw-r--r--website-emu/src/lib.rs27
-rw-r--r--website-emu/src/state.rs4
3 files changed, 27 insertions, 10 deletions
diff --git a/website-emu/src/dev.js b/website-emu/src/dev.js
new file mode 100644
index 0000000..4a22523
--- /dev/null
+++ b/website-emu/src/dev.js
@@ -0,0 +1,6 @@
+export function uart_push_line(line) {
+ const elem = document.createElement("span");
+ elem.classList.add("uart_line");
+ elem.textContent = line;
+ document.body.appendChild(elem);
+} \ No newline at end of file
diff --git a/website-emu/src/lib.rs b/website-emu/src/lib.rs
index ffaa677..b29c901 100644
--- a/website-emu/src/lib.rs
+++ b/website-emu/src/lib.rs
@@ -16,20 +16,20 @@ const CONSOLE_LOAD: u32 = 0x70000000;
const INITRD_LOAD: u32 = 0;
-#[wasm_bindgen]
+#[wasm_bindgen(module = "/src/dev.js")]
extern "C" {
- #[wasm_bindgen(js_namespace = console)]
- fn log(s: &str);
+ fn uart_push_line(line: &str);
}
pub struct Uart {
pub addr: u32,
- pub value: u8
+ pub value: u8,
+ acc: String
}
impl MemoryDevice for Uart {
fn read(&self, address: u32) -> Result<u8, ()> {
- if (address == self.addr) {
+ if address == self.addr {
Ok(self.value)
} else {
Err(())
@@ -37,8 +37,15 @@ impl MemoryDevice for Uart {
}
fn write(&mut self, address: u32, value: u8) -> Result<(), ()> {
- if (address == self.addr) {
+ if address == self.addr {
self.value = value;
+
+ if self.value as char == '\n' {
+ uart_push_line(self.acc.as_str());
+ self.acc = String::new();
+ } else {
+ self.acc.push(self.value as char);
+ }
Ok(())
} else {
Err(())
@@ -76,7 +83,8 @@ pub fn boot(image: &[u8], initrd: &[u8], memory: &[u8]) {
let uart = Uart {
addr: CONSOLE_LOAD,
- value: 0
+ value: 0,
+ acc: "".to_string(),
};
cpu_memory.register_device(
@@ -102,7 +110,7 @@ pub fn boot(image: &[u8], initrd: &[u8], memory: &[u8]) {
state.registers[10] = 0;
state.registers[2] = DRAM_LOAD + memory_len as u32;
- loop {
+ while state.on {
{
handle_instruction(&mut state);
state.registers[0] = 0;
@@ -278,7 +286,8 @@ fn handle_instruction(state: &mut CpuState) {
0 => trap_to_kernel(state), // ecall
258 => return_to_userspace(state), // sret
261 => {
- println!("wfi");
+ state.on = false;
+ return;
}
_ => panic!("Illegal instruction: {:x}", word),
}
diff --git a/website-emu/src/state.rs b/website-emu/src/state.rs
index f26167f..4b32713 100644
--- a/website-emu/src/state.rs
+++ b/website-emu/src/state.rs
@@ -5,6 +5,7 @@ pub struct CpuState {
pub registers: [u32; 32],
pub csr: [u32; 4096],
pub pc: u32,
+ pub on: bool
}
impl CpuState {
@@ -13,7 +14,8 @@ impl CpuState {
memory,
registers: [0u32; 32],
csr: [0u32; 4096],
- pc: 0
+ pc: 0,
+ on: true
}
}
}