Skip to content

Commit

Permalink
Add wasm worker base
Browse files Browse the repository at this point in the history
  • Loading branch information
1whatleytay committed Feb 28, 2024
1 parent f18b16c commit 643f794
Show file tree
Hide file tree
Showing 28 changed files with 888 additions and 70 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"tailwindcss": "^3.2.4",
"typescript": "^4.6.4",
"vite": "^5.1.1",
"vite-plugin-top-level-await": "^1.4.1",
"vite-plugin-wasm": "^3.3.0",
"vue-tsc": "^1.0.0"
}
}
21 changes: 20 additions & 1 deletion src-backend/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use std::collections::HashMap;
use std::io::Cursor;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use titan::assembler::binary::Binary;
use titan::assembler::binary::{Binary, RegionFlags};
use titan::assembler::line_details::LineDetails;
use titan::assembler::string::{assemble_from, assemble_from_path, SourceError};
use titan::cpu::memory::{Mountable, Region};
use titan::cpu::memory::section::SectionMemory;
use titan::cpu::{Memory, State};
use titan::execution::elf::inspection::Inspection;
use titan::elf::Elf;
use titan::elf::program::ProgramHeaderFlags;
use crate::keyboard::{KeyboardHandler, KeyboardState, KEYBOARD_SELECTOR};

pub const TIME_TRAVEL_HISTORY_SIZE: usize = 1000;
Expand Down Expand Up @@ -40,6 +41,24 @@ pub enum AssemblerResult {
},
}

pub fn get_elf_finished_pcs(elf: &Elf) -> Vec<u32> {
elf
.program_headers
.iter()
.filter(|header| header.flags.contains(ProgramHeaderFlags::EXECUTABLE))
.map(|header| header.virtual_address + header.data.len() as u32)
.collect()
}

pub fn get_binary_finished_pcs(binary: &Binary) -> Vec<u32> {
binary
.regions
.iter()
.filter(|region| region.flags.contains(RegionFlags::EXECUTABLE))
.map(|region| region.address + region.data.len() as u32)
.collect()
}

impl AssemblerResult {
pub fn from_result_with_binary(
result: Result<Binary, SourceError>,
Expand Down
5 changes: 3 additions & 2 deletions src-backend/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub trait RewindableDevice: ExecutionDevice + ExecutionRewindable { }
pub trait ExecutionDevice: Send + Sync {
async fn resume(
&self,
count: Option<u32>,
count: Option<usize>,
breakpoints: Option<Vec<u32>>,
display: Option<FlushDisplayBody>
) -> Result<ResumeResult, ()>;
Expand All @@ -177,7 +177,7 @@ pub trait ExecutionDevice: Send + Sync {
impl<Mem: Memory + Send, Track: Tracker<Mem> + Send> ExecutionDevice for ExecutionState<Mem, Track> {
async fn resume(
&self,
count: Option<u32>,
count: Option<usize>,
breakpoints: Option<Vec<u32>>,
display: Option<FlushDisplayBody>
) -> Result<ResumeResult, ()> {
Expand All @@ -200,6 +200,7 @@ impl<Mem: Memory + Send, Track: Tracker<Mem> + Send> ExecutionDevice for Executi

let (frame, result) = {
if let Some(count) = count {
// Taylor: Why did I split the last iteration out?
for _ in 0..count - 1 {
delegate.cycle(&debugger).await;
}
Expand Down
4 changes: 2 additions & 2 deletions src-backend/src/hex_format.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::cmp::min;
use byteorder::ByteOrder;
use serde::Deserialize;
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Deserialize)]
#[derive(Copy, Clone, Serialize, Deserialize)]
#[serde(rename_all="snake_case")]
pub enum HexEncoding {
Byte,
Expand Down
4 changes: 2 additions & 2 deletions src-backend/src/regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ pub struct HexRegion {
pub data: String // base64 encoded
}

#[derive(Copy, Clone, Deserialize)]
#[derive(Copy, Clone, Serialize, Deserialize)]
#[serde(rename_all="snake_case")]
pub enum AssembleRegionsKind {
Plain,
HexV3
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct AssembleRegionsOptions {
pub kind: AssembleRegionsKind,
pub continuous: bool,
Expand Down
18 changes: 4 additions & 14 deletions src-tauri/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use titan::execution::Executor;
use titan::execution::trackers::empty::EmptyTracker;
use titan::execution::trackers::history::HistoryTracker;
use titan::execution::trackers::Tracker;
use saturn_backend::build::{assemble_text, AssemblerResult, configure_keyboard, create_elf_state, DisassembleResult, PrintPayload, TIME_TRAVEL_HISTORY_SIZE};
use saturn_backend::build::{assemble_text, AssemblerResult, configure_keyboard, create_elf_state, DisassembleResult, get_binary_finished_pcs, get_elf_finished_pcs, PrintPayload, TIME_TRAVEL_HISTORY_SIZE};
use saturn_backend::device::{ExecutionState, setup_state, state_from_binary};
use saturn_backend::execution::RewindableDevice;
use saturn_backend::keyboard::KeyboardState;
Expand Down Expand Up @@ -92,13 +92,8 @@ pub fn configure_elf(
) -> bool {
let Ok(elf) = Elf::read(&mut Cursor::new(bytes)) else { return false };

let finished_pcs = elf
.program_headers
.iter()
.filter(|header| header.flags.contains(ProgramHeaderFlags::EXECUTABLE))
.map(|header| header.virtual_address + header.data.len() as u32)
.collect();

let finished_pcs = get_elf_finished_pcs(&elf);

let console = forward_print(app_handle.clone());
let midi = Box::new(ForwardMidi::new(app_handle));
let history = HistoryTracker::new(TIME_TRAVEL_HISTORY_SIZE);
Expand Down Expand Up @@ -151,12 +146,7 @@ pub fn configure_asm(

let Some(binary) = binary else { return result };

// No EXECUTABLE marked regions from assembler yet.
let finished_pcs = binary
.regions
.iter()
.map(|region| region.address + region.data.len() as u32)
.collect();
let finished_pcs = get_binary_finished_pcs(&binary);

let console = forward_print(app_handle.clone());
let midi = Box::new(ForwardMidi::new(app_handle));
Expand Down
8 changes: 4 additions & 4 deletions src-tauri/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use saturn_backend::execution::{ResumeResult, RewindableDevice};
pub type DebuggerBody = Mutex<Option<Arc<dyn RewindableDevice>>>;

#[tauri::command]
pub async fn last_pc(state: tauri::State<'_, DebuggerBody>) -> Result<Option<u32>, ()> {
Ok(state.lock().unwrap()
pub fn last_pc(state: tauri::State<'_, DebuggerBody>) -> Option<u32> {
state.lock().unwrap()
.as_ref()
.map(|debugger| debugger.last_pc())
.and_then(|x| x))
.and_then(|x| x)
}

#[tauri::command]
pub async fn resume(
count: Option<u32>,
count: Option<usize>,
breakpoints: Option<Vec<u32>>,
state: tauri::State<'_, DebuggerBody>,
display: tauri::State<'_, FlushDisplayBody>,
Expand Down
60 changes: 60 additions & 0 deletions src-wasm/Cargo.lock

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

4 changes: 4 additions & 0 deletions src-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ crate-type = ["cdylib", "rlib"]
default = ["console_error_panic_hook"]

[dependencies]
serde = "1.0.197"
serde_json = "1.0.114"
wasm-bindgen = "0.2.91"
wasm-bindgen-futures = "0.4.41"
serde-wasm-bindgen = "0.6.4"

console_error_panic_hook = { version = "0.1.7", optional = true }

Expand Down
1 change: 1 addition & 0 deletions src-wasm/pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wasm-pack build --out-dir ../src/utils/mips/wasm/
9 changes: 9 additions & 0 deletions src-wasm/src/console.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use saturn_backend::syscall::ConsoleHandler;

pub struct WasmConsole { }

impl ConsoleHandler for WasmConsole {
fn print(&mut self, _text: &str, _error: bool) {
todo!()
}
}
Loading

0 comments on commit 643f794

Please sign in to comment.