@@ -19,52 +19,18 @@ extern "C" {
1919 fn log ( s : & str ) ;
2020}
2121
22+ /// Get a global window object.
2223fn 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.
2628fn 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]
6935pub 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- */
0 commit comments