Sitelet https://github.com/r55-eth/rvemu/commit/5e4d992976712ae0b04730d5d63fd5b23f085bcc
Skip to content

Commit 5e4d992

Browse files
committed
Clean up
1 parent 63ad4ff commit 5e4d992

3 files changed

Lines changed: 8 additions & 171 deletions

File tree

‎lib/rvemu-wasm/src/lib.rs‎

Lines changed: 3 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,18 @@ extern "C" {
1919
fn log(s: &str);
2020
}
2121

22+
/// Get a global window object.
2223
fn window() -> web_sys::Window {
2324
web_sys::window().expect("no global `window` exists")
2425
}
2526

27+
/// Sets a timer which executes a function or specified piece of code once the timer expires.
2628
fn set_timeout_with_callback(f: &Closure<dyn FnMut()>, timeout: i32) {
2729
window()
2830
.set_timeout_with_callback_and_timeout_and_arguments_0(f.as_ref().unchecked_ref(), timeout)
2931
.expect("should register `requestAnimationFrame` OK");
3032
}
3133

32-
/*
33-
/// Wrapper for rvemu::emulator::Emulator to connect to WebAssembly.
34-
#[wasm_bindgen]
35-
pub struct Emulator {
36-
emu: Rc<RefCell<emulator::Emulator>>,
37-
}
38-
*/
39-
40-
/// Output a message to the emulator console.
41-
pub fn stdout(message: &str) {
42-
let document = window()
43-
.document()
44-
.expect("should have a document on window");
45-
let buffer = document
46-
.get_element_by_id("buffer")
47-
.expect("should have a element with a `buffer` id");
48-
49-
let span = document
50-
.create_element("span")
51-
.expect("span element should be created successfully");
52-
span.set_inner_html(message);
53-
let result = buffer.append_child(&span);
54-
if result.is_err() {
55-
panic!("can't append a span node to a buffer node")
56-
}
57-
}
58-
59-
/// Output a message to both the browser console and the emulator console.
60-
pub fn stdout_log(message: &str) {
61-
log(message);
62-
//stdout(message);
63-
//for c in message.chars() {
64-
// write_to_buffer(c as u8);
65-
//}
66-
}
67-
6834
#[wasm_bindgen]
6935
pub fn emulator_start(kernel: Vec<u8>, fsimg: Vec<u8>) {
7036
utils::set_panic_hook();
@@ -102,7 +68,6 @@ pub fn emulator_start(kernel: Vec<u8>, fsimg: Vec<u8>) {
10268
}
10369

10470
if count > 500000 {
105-
log(&format!("count in generator: {}", count));
10671
count = 0;
10772
yield;
10873
}
@@ -112,127 +77,11 @@ pub fn emulator_start(kernel: Vec<u8>, fsimg: Vec<u8>) {
11277
let handler = Rc::new(RefCell::new(None));
11378
let cloned_handler = handler.clone();
11479

80+
// Set a timer to execute the emulator again.
11581
*cloned_handler.borrow_mut() = Some(Closure::wrap(Box::new(move || {
11682
Pin::new(&mut generator).resume(());
11783
set_timeout_with_callback(handler.borrow().as_ref().unwrap(), 0);
11884
}) as Box<dyn FnMut()>));
11985

12086
set_timeout_with_callback(cloned_handler.borrow().as_ref().unwrap(), 0);
12187
}
122-
123-
/*
124-
#[wasm_bindgen]
125-
impl Emulator {
126-
/// Constructor for the emulator.
127-
pub fn new() -> Emulator {
128-
// Initialize for putting error messages to a browser console.
129-
utils::set_panic_hook();
130-
131-
Self {
132-
emu: Rc::new(RefCell::new(emulator::Emulator::new())),
133-
}
134-
}
135-
136-
/// Reset the emulator.
137-
pub fn reset(&mut self) {
138-
(*self.emu.borrow_mut()).reset();
139-
}
140-
141-
/// Set binary data to the beginning of the DRAM from the emulator console of a browser.
142-
pub fn set_dram(&mut self, data: Vec<u8>) {
143-
(*self.emu.borrow_mut()).set_dram(data);
144-
}
145-
146-
/// Set binary data to the virtio disk from the emulator console.
147-
pub fn set_disk(&mut self, data: Vec<u8>) {
148-
(*self.emu.borrow_mut()).set_disk(data);
149-
}
150-
151-
/// Start executing.
152-
pub fn start(&mut self) {
153-
/*
154-
(*self.emu.borrow_mut()).set_pc(DRAM_BASE);
155-
156-
let mut count = 0;
157-
let key_event = Closure::wrap(Box::new(move || {
158-
log(&format!("count: {}", count));
159-
count += 1;
160-
}) as Box<dyn FnMut()>);
161-
window().set_onkeydown(Some(key_event.as_ref().unchecked_ref()));
162-
key_event.forget();
163-
164-
let mut cloned_emu = self.emu.clone();
165-
let mut generator = move || {
166-
let window = web_sys::window().expect("no global `window` exists");
167-
let document = window.document().expect("should have a document on window");
168-
//let input = document.get_element_by_id("input").unwrap();
169-
let mut count = 0;
170-
loop {
171-
let emu = *cloned_emu.borrow_mut();
172-
count += 1;
173-
// 1. Fetch.
174-
let data_or_error = emu.cpu.fetch();
175-
176-
// 2. Add 4 to the program counter.
177-
emu.cpu.pc += 4;
178-
emu.cpu.timer_increment();
179-
180-
// 3. Decode.
181-
// 4. Execution.
182-
let _result = match data_or_error {
183-
Ok(data) => match emu.cpu.execute(data) {
184-
Ok(_) => Ok(()),
185-
Err(exception) => exception.take_trap(&mut emu.cpu),
186-
},
187-
Err(exception) => exception.take_trap(&mut emu.cpu),
188-
};
189-
190-
// Take an interrupt.
191-
match emu.cpu.check_interrupt() {
192-
Some(interrupt) => interrupt.take_trap(&mut emu.cpu),
193-
None => {}
194-
}
195-
196-
/*
197-
if count > 10000 {
198-
yield;
199-
}
200-
*/
201-
log(&format!("count in generator: {}", count));
202-
//input.set_text_content(Some(&format!("{}", count).to_string()));
203-
}
204-
};
205-
*/
206-
207-
/*
208-
let animation_handler = Rc::new(RefCell::new(None));
209-
let cloned_animation_handler = animation_handler.clone();
210-
211-
*cloned_animation_handler.borrow_mut() = Some(Closure::wrap(Box::new(move || {
212-
Pin::new(&mut generator).resume();
213-
request_animation_frame(animation_handler.borrow().as_ref().unwrap());
214-
}) as Box<dyn FnMut()>));
215-
216-
request_animation_frame(cloned_animation_handler.borrow().as_ref().unwrap());
217-
*/
218-
}
219-
220-
/// Output current registers.
221-
pub fn dump_registers(&self) {
222-
let emu = self.emu.borrow();
223-
stdout_log(&format!("{}", emu.cpu.xregs));
224-
stdout_log(&format!(
225-
"-------------------------------------------------------------------------------------------"
226-
));
227-
stdout_log(&format!("{}", emu.cpu.fregs));
228-
stdout_log(&format!(
229-
"-------------------------------------------------------------------------------------------"
230-
));
231-
stdout_log(&format!("{}", emu.cpu.state));
232-
stdout_log(&format!(
233-
"-------------------------------------------------------------------------------------------"
234-
));
235-
stdout_log(&format!("pc: {}", emu.cpu.pc));
236-
}
237-
}
238-
*/

‎public/xv6.js‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,13 @@ async function initEmulator() {
4848
kernelReader.onloadend = e => {
4949
console.log("Starting to execute xv6 ...");
5050

51-
//emu = Emulator.new();
52-
5351
const kernelData = new Uint8Array(kernelReader.result);
54-
//emu.set_dram(kernelData);
55-
//emu.set_disk(fsImgData);
56-
emulator_start(kernelData, fsImgData);
5752

58-
/*
5953
try {
60-
emu.start();
54+
emulator_start(kernelData, fsImgData);
6155
} catch(err) {
6256
console.log(err);
63-
} finally {
64-
//emu.dump_registers();
65-
emu = null;
6657
}
67-
*/
6858
};
6959
}
7060

@@ -104,7 +94,6 @@ function initTerminal() {
10494
span.innerText = e.key;
10595
buffer.appendChild(span);
10696
}
107-
console.log("get key", e.key, inputBuffer);
10897
});
10998
}
11099

‎src/devices/uart_wasm.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! (UART) for WebAssembly. The device is 16550a UART, which is used in the QEMU virt machine. See more information
33
//! in http://byterunner.com/16550.html.
44
5-
use wasm_bindgen::prelude::*;
65
use wasm_bindgen::JsValue;
6+
use web_sys::Window;
77

88
use crate::bus::{UART_BASE, UART_SIZE};
99

@@ -35,8 +35,7 @@ pub const UART_LCR: u64 = UART_BASE + 3;
3535
/// 1 = transmit holding register is empty. In FIFO mode this bit is set to one whenever the the transmitter FIFO and transmit shift register are empty.
3636
pub const UART_LSR: u64 = UART_BASE + 5;
3737

38-
fn get_input() -> u8 {
39-
let window = web_sys::window().expect("failed to get a global window object");
38+
fn get_input(window: &Window) -> u8 {
4039
let document = window.document().expect("failed to get a document object");
4140
let buffer = document
4241
.get_element_by_id("buffer")
@@ -80,7 +79,7 @@ impl Uart {
8079
Self {
8180
uart,
8281
clock: 0,
83-
window: web_sys::window().expect("no global `window` exists"),
82+
window: web_sys::window().expect("failed to get a global window object"),
8483
}
8584
}
8685

@@ -90,7 +89,7 @@ impl Uart {
9089
// Avoid too many interrupting.
9190
if self.clock > 500000 {
9291
self.clock = 0;
93-
let b = get_input();
92+
let b = get_input(&self.window);
9493
if b == 0 {
9594
return false;
9695
}

0 commit comments

Comments
 (0)