Skip to content

Commit

Permalink
Fix deafult session Id, edit register values
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryex committed Mar 28, 2024
1 parent baf67b2 commit 2b9f9ba
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ic10emu/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ fn write_instructions_enum() {

write!(
&mut writer,
"#[derive(PartialEq, Debug)]\n\
"#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]\n\
pub enum InstructionOp {{\n\
"
)
Expand Down
2 changes: 1 addition & 1 deletion ic10emu/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl FromStr for Comment {
}
}

#[derive(PartialEq, Debug)]
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Instruction {
pub instruction: InstructionOp,
pub operands: Vec<Operand>,
Expand Down
16 changes: 10 additions & 6 deletions ic10emu/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub struct IC {
pub state: ICState,
}

#[derive(Debug)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Program {
pub instructions: Vec<grammar::Instruction>,
pub labels: HashMap<String, u32>,
Expand All @@ -169,12 +169,14 @@ impl Default for Program {
}

impl Program {

pub fn new() -> Self {
Program {
instructions: Vec::new(),
labels: HashMap::new(),
}
}

pub fn try_from_code(code: &str) -> Result<Self, ICError> {
let parse_tree = grammar::parse(&code)?;
let mut labels_set = HashSet::new();
Expand Down Expand Up @@ -209,6 +211,7 @@ impl Program {
labels,
})
}

pub fn get_line(&self, line: u32) -> Result<&grammar::Instruction, ICError> {
self.instructions
.get(line as usize)
Expand All @@ -217,6 +220,7 @@ impl Program {
}

impl IC {

pub fn new(id: u16, device: u16) -> Self {
IC {
device,
Expand Down Expand Up @@ -319,7 +323,7 @@ impl IC {
self.registers[17] = self.ip as f64 + 1.0;
}

fn push(&mut self, val: f64) -> Result<f64, ICError> {
pub fn push(&mut self, val: f64) -> Result<f64, ICError> {
let sp = (self.registers[16]) as i32;
if sp < 0 {
Err(ICError::StackUnderflow)
Expand All @@ -333,7 +337,7 @@ impl IC {
}
}

fn pop(&mut self) -> Result<f64, ICError> {
pub fn pop(&mut self) -> Result<f64, ICError> {
let sp = (self.registers[16]) as i32;
if sp < 0 {
Err(ICError::StackUnderflow)
Expand All @@ -346,7 +350,7 @@ impl IC {
}
}

fn poke(&mut self, address: f64, val: f64) -> Result<f64, ICError> {
pub fn poke(&mut self, address: f64, val: f64) -> Result<f64, ICError> {
let sp = address as i32;
if sp < 0 || sp >= 512 {
Err(ICError::StackIndexOutOfRange(address))
Expand All @@ -357,7 +361,7 @@ impl IC {
}
}

fn peek(&self) -> Result<f64, ICError> {
pub fn peek(&self) -> Result<f64, ICError> {
let sp = (self.registers[16]) as i32;
if sp < 0 {
Err(ICError::StackUnderflow)
Expand All @@ -369,7 +373,7 @@ impl IC {
}
}

fn peek_addr(&self, addr: f64) -> Result<f64, ICError> {
pub fn peek_addr(&self, addr: f64) -> Result<f64, ICError> {
let sp = (addr) as i32;
if sp < 0 {
Err(ICError::StackUnderflow)
Expand Down
57 changes: 55 additions & 2 deletions ic10emu_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ impl DeviceRef {

#[wasm_bindgen(getter, js_name = "state")]
pub fn ic_state(&self) -> Option<String> {
self
.device
self.device
.borrow()
.ic
.as_ref()
Expand All @@ -221,6 +220,26 @@ impl DeviceRef {
.map(|state| state.to_string())
}

#[wasm_bindgen(getter, js_name = "program")]
pub fn ic_program(&self) -> JsValue {
serde_wasm_bindgen::to_value(
&self
.device
.borrow()
.ic
.as_ref()
.map(|ic| {
self.vm
.borrow()
.ics
.get(ic)
.map(|ic| ic.borrow().program.clone())
})
.flatten(),
)
.unwrap()
}

#[wasm_bindgen(js_name = "step")]
pub fn step_ic(&self) -> Result<bool, JsError> {
let id = self.device.borrow().id;
Expand All @@ -244,6 +263,40 @@ impl DeviceRef {
let id = self.device.borrow().id;
Ok(self.vm.borrow().set_code(id, code)?)
}

#[wasm_bindgen(js_name = "setRegister")]
pub fn ic_set_register(&self, index: u32, val: f64) -> Result<f64, JsError> {
let ic_id = *self
.device
.borrow()
.ic
.as_ref()
.ok_or(ic10emu::VMError::NoIC(self.device.borrow().id))?;
let vm_borrow = self.vm.borrow();
let ic = vm_borrow
.ics
.get(&ic_id)
.ok_or(ic10emu::VMError::NoIC(self.device.borrow().id))?;
let result = ic.borrow_mut().set_register(0, index, val)?;
Ok(result)
}

#[wasm_bindgen(js_name = "setStack")]
pub fn ic_set_stack(&mut self, address: f64, val: f64) -> Result<f64, JsError> {
let ic_id = *self
.device
.borrow()
.ic
.as_ref()
.ok_or(ic10emu::VMError::NoIC(self.device.borrow().id))?;
let vm_borrow = self.vm.borrow();
let ic = vm_borrow
.ics
.get(&ic_id)
.ok_or(ic10emu::VMError::NoIC(self.device.borrow().id))?;
let result = ic.borrow_mut().poke(address, val)?;
Ok(result)
}
}

#[wasm_bindgen]
Expand Down
30 changes: 22 additions & 8 deletions www/src/js/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class IC10Editor {
customScrollbar: false,
firstLineNumber: 0,
printMarginColumn: 52,
placeholder: "Your code goes here ...",
});

this.sessions = {};
Expand Down Expand Up @@ -80,16 +81,17 @@ class IC10Editor {
if (marker) {
self.sessions[id].removeMarker(marker);
self.active_line_markers[id] = null;

}
self.active_line_markers[id] = self.sessions[id].addMarker(new Range(active_line, 0, active_line, 1), "vm_ic_active_line", "fullLine", true);
if (self.active_session == id) {
// editor.resize(true);
self.aceEditor.scrollToLine(active_line, true, true)
const session = self.sessions[id];
if (session) {
self.active_line_markers[id] = session.addMarker(new Range(active_line, 0, active_line, 1), "vm_ic_active_line", "fullLine", true);
if (self.active_session == id) {
// editor.resize(true);
self.aceEditor.scrollToLine(active_line, true, true)
}
}
}
}

})

}
Expand All @@ -105,8 +107,20 @@ class IC10Editor {
if (this.sessions.hasOwnProperty(session_id)) {
return false;
}
this.sessions[session_id] = ace.createEditSession("", this.mode);
this.bindSession(session_id, this.sessions[session_id]);
const session = ace.createEditSession("", this.mode);
session.setOptions({
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
enableSnippets: true,
theme: "ace/theme/one_dark",
fontSize: "16px",
customScrollbar: false,
firstLineNumber: 0,
printMarginColumn: 52,
placeholder: "Your code goes here ...",
})
this.sessions[session_id] = session;
this.bindSession(session_id, session);
}

setupLsp(lsp_worker) {
Expand Down
4 changes: 3 additions & 1 deletion www/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ docReady(() => {

App.vm = new VirtualMachine();

App.editor = new IC10Editor();
const init_session_id = App.vm.devices[0];

App.editor = new IC10Editor(init_session_id);

setupLspWorker().then((worker) => {
App.editor.setupLsp(worker);
Expand Down
Loading

0 comments on commit 2b9f9ba

Please sign in to comment.