Skip to content

Commit

Permalink
Add console and some midi syscall support
Browse files Browse the repository at this point in the history
  • Loading branch information
1whatleytay committed Sep 14, 2024
1 parent ca70a28 commit 103679f
Show file tree
Hide file tree
Showing 24 changed files with 387 additions and 112 deletions.
6 changes: 3 additions & 3 deletions src-backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions src-backend/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ pub struct BatchOptions {
// if false, mode (pausing) will not stop execution
// handy if we want to start in the breakpoint mode (and we only want to run 1 instruction anyway)
pub allow_interrupt: bool,
// If this variable is true, the mode will be forced to breakpoint at the end of the batch.
// The mode will only be changed if the mode is Running at the end of the batch.
// Useful for stepping over syscalls, since syscalls will set the mode to Running when finished.
pub break_at_end: bool,
}

pub struct ResumeOptions {
Expand Down Expand Up @@ -222,13 +226,25 @@ impl<Mem: Memory + Send, Track: Tracker<Mem> + Send> ExecutionDevice for Executi

let (frame, result) = {
if let Some(batch) = &options.batch {
delegate.run_batch(
let (frame, result) = delegate.run_batch(
&debugger,
batch.count,
is_breakpoint && batch.first_batch,
batch.allow_interrupt
).await
.unwrap_or((debugger.frame(), None))
.unwrap_or((debugger.frame(), None));

let frame = if frame.mode == ExecutorMode::Running && batch.break_at_end {
debugger.override_mode(ExecutorMode::Breakpoint);

// re-fetch the new frame, post override
// too tired to fetch it myself
debugger.frame()
} else {
frame
};

(frame, result)
} else {
delegate.run(&debugger, is_breakpoint).await
}
Expand Down
1 change: 1 addition & 0 deletions src-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub mod decode;
pub mod build;
pub mod hex_format;
pub mod regions;
pub mod midi;
File renamed without changes.
2 changes: 2 additions & 0 deletions src-backend/src/midi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod instruments;
pub mod note;
11 changes: 11 additions & 0 deletions src-backend/src/midi/note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Serialize, Deserialize)]
pub struct MidiNote {
pub sync: bool,
pub instrument: u64,
pub name: String,
pub note: u64,
pub duration: f64,
pub volume: u64,
}
22 changes: 11 additions & 11 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 2 additions & 12 deletions src-tauri/src/midi/handler.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
use crate::midi::install_instruments;
use crate::midi::instruments::to_instrument;
use saturn_backend::syscall::{MidiHandler, MidiRequest};
use serde::Serialize;
use std::collections::HashSet;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use tauri::api::path::app_local_data_dir;
use tauri::{AppHandle, Manager, Wry};

#[derive(Clone, Serialize)]
struct MidiNote {
sync: bool,
instrument: u64,
name: String,
note: u64,
duration: f64,
volume: u64,
}
use saturn_backend::midi::instruments::to_instrument;
use saturn_backend::midi::note::MidiNote;

#[derive(Clone)]
pub struct ForwardMidi {
Expand Down
1 change: 0 additions & 1 deletion src-tauri/src/midi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod fetch;
mod handler;
mod instruments;
mod protocol;

pub use fetch::{install_instruments, midi_install, MidiProviderContainer};
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub async fn resume(
batch: count.map(|count| BatchOptions {
count,
first_batch: true,
allow_interrupt: false
allow_interrupt: false,
break_at_end: true
}),
breakpoints,
display: Some(display),
Expand Down
13 changes: 10 additions & 3 deletions src-wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ wasm-bindgen = "0.2.91"
wasm-bindgen-futures = "0.4.41"
serde-wasm-bindgen = "0.6.4"
async-trait = "0.1.82"
send_wrapper = "0.6.0"
js-sys = "0.3.70"
gloo-timers = "0.3.0"

Expand Down
10 changes: 7 additions & 3 deletions src-wasm/src/console.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::sync::Arc;
use saturn_backend::syscall::ConsoleHandler;
use crate::EventHandler;

pub struct WasmConsole { }
pub struct WasmConsole {
pub events: Arc<EventHandler>
}

impl ConsoleHandler for WasmConsole {
fn print(&mut self, _text: &str, _error: bool) {
todo!()
fn print(&mut self, text: &str, error: bool) {
self.events.send_console_write(text, error)
}
}
42 changes: 40 additions & 2 deletions src-wasm/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
struct Listener {

use send_wrapper::SendWrapper;
use wasm_bindgen::JsValue;
use wasm_bindgen::prelude::wasm_bindgen;
use saturn_backend::midi::note::MidiNote;

#[wasm_bindgen]
pub struct EventHandler {
on_console_write: SendWrapper<js_sys::Function>,
on_midi_play: SendWrapper<js_sys::Function>,
}

#[wasm_bindgen]
impl EventHandler {
#[wasm_bindgen(constructor)]
pub fn new(
on_console_write: js_sys::Function,
on_midi_play: js_sys::Function,
) -> EventHandler {
EventHandler {
on_console_write: SendWrapper::new(on_console_write),
on_midi_play: SendWrapper::new(on_midi_play)
}
}
}

impl EventHandler {
pub fn send_console_write(&self, text: &str, error: bool) {
self.on_console_write.call2(
&JsValue::UNDEFINED,
&JsValue::from_str(text),
&JsValue::from_bool(error)
).ok();
}

pub fn send_midi_play(&self, note: MidiNote) {
self.on_midi_play.call1(
&JsValue::UNDEFINED,
&serde_wasm_bindgen::to_value(&note).unwrap()
).ok();
}
}
23 changes: 16 additions & 7 deletions src-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use crate::console::WasmConsole;
use crate::midi::WasmMidi;
use crate::time::WasmTime;

pub use events::EventHandler;

#[wasm_bindgen]
pub fn initialize() {
wasm_logger::init(wasm_logger::Config::default());
Expand Down Expand Up @@ -80,8 +82,8 @@ pub fn detailed_disassemble(bytes: Vec<u8>) -> Result<JsValue, String> {
}

#[wasm_bindgen]
#[derive(Default)]
pub struct Runner {
events: Arc<EventHandler>,
display: RefCell<FlushDisplayBody>,
device: RefCell<Option<Rc<dyn RewindableDevice>>>
}
Expand Down Expand Up @@ -143,8 +145,12 @@ impl Runner {
#[wasm_bindgen]
impl Runner {
#[wasm_bindgen(constructor)]
pub fn new() -> Runner {
Runner::default()
pub fn new(events: EventHandler) -> Runner {
Runner {
events: Arc::new(events),
display: RefCell::new(Arc::new(Mutex::new(Default::default()))),
device: RefCell::new(None),
}
}

pub fn set_breakpoints(&self, breakpoints: &[u32]) {
Expand Down Expand Up @@ -178,7 +184,7 @@ impl Runner {

let finished_pcs = get_elf_finished_pcs(&elf);

let console = Box::new(WasmConsole { });
let console = Box::new(WasmConsole { events: self.events.clone() });
let midi = Box::new(WasmMidi { });
let time = Arc::new(WasmTime { });
let history = HistoryTracker::new(TIME_TRAVEL_HISTORY_SIZE);
Expand Down Expand Up @@ -232,7 +238,7 @@ impl Runner {

let finished_pcs = get_binary_finished_pcs(&binary);

let console = Box::new(WasmConsole { });
let console = Box::new(WasmConsole { events: self.events.clone() });
let midi = Box::new(WasmMidi { });
let time = Arc::new(WasmTime { });
let history = HistoryTracker::new(TIME_TRAVEL_HISTORY_SIZE);
Expand Down Expand Up @@ -327,14 +333,17 @@ impl Runner {
return JsValue::NULL
};

let display = self.display.borrow().clone();

let result = device.resume(ResumeOptions {
batch: Some(BatchOptions {
count: batch_size,
first_batch,
allow_interrupt: !is_step
allow_interrupt: !is_step,
break_at_end: is_step
}),
breakpoints,
display: Some(self.display.borrow().clone()),
display: Some(display),
change_state: if !is_step && first_batch { Some(ExecutorMode::Running) } else { None }
}).await;

Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import App from './App.vue'
import { setupEvents } from './utils/events'
import { setupShortcuts } from './utils/platform-shortcuts'
import { setupWindow } from './utils/window'
import { setupBackend } from './state/backend'

createApp(App).mount('#app')

Expand All @@ -15,4 +16,6 @@ if (window.__TAURI__) {
// Needs backend tying.
setupShortcuts().then(() => {})
setupEvents().then(() => {})
}
}

setupBackend().then(() => {})
Loading

0 comments on commit 103679f

Please sign in to comment.