From cf1f3e15afcf69cacf1177ccc1e18183c33b67b5 Mon Sep 17 00:00:00 2001 From: Rachel Powers <508861+Ryex@users.noreply.github.com> Date: Sat, 20 Apr 2024 22:12:41 -0700 Subject: [PATCH] use full vm state to restore backwards compatable links --- ic10emu/src/device.rs | 41 +++++++- ic10emu_wasm/build.rs | 4 +- ic10emu_wasm/src/types.ts | 2 +- www/src/ts/presets/demo.ts | 137 +++++++++++++++++++++++++++ www/src/ts/presets/index.ts | 3 + www/src/ts/session.ts | 117 +++++++---------------- www/src/ts/virtual_machine/device.ts | 2 + 7 files changed, 217 insertions(+), 89 deletions(-) create mode 100644 www/src/ts/presets/demo.ts create mode 100644 www/src/ts/presets/index.ts diff --git a/ic10emu/src/device.rs b/ic10emu/src/device.rs index a2fa7a4..d5c3cf5 100644 --- a/ic10emu/src/device.rs +++ b/ic10emu/src/device.rs @@ -113,7 +113,43 @@ impl SlotOccupant { /// chainable constructor pub fn get_fields(&self) -> HashMap { - self.fields.clone() + let mut copy = self.fields.clone(); + copy.insert( + SlotLogicType::PrefabHash, + LogicField { + field_type: FieldType::Read, + value: self.prefab_hash as f64, + }, + ); + copy.insert( + SlotLogicType::SortingClass, + LogicField { + field_type: FieldType::Read, + value: self.sorting_class as u32 as f64, + }, + ); + copy.insert( + SlotLogicType::Quantity, + LogicField { + field_type: FieldType::Read, + value: self.quantity as f64, + }, + ); + copy.insert( + SlotLogicType::MaxQuantity, + LogicField { + field_type: FieldType::Read, + value: self.max_quantity as f64, + }, + ); + copy.insert( + SlotLogicType::Damage, + LogicField { + field_type: FieldType::Read, + value: self.damage, + }, + ); + copy } pub fn set_field( @@ -574,10 +610,11 @@ impl Device { }, ), ]); + let occupant = SlotOccupant::new(ic, -744098481); device.slots.push(Slot::with_occupant( SlotType::ProgrammableChip, // -744098481 = ItemIntegratedCircuit10 - SlotOccupant::new(ic, -744098481), + occupant, )); device diff --git a/ic10emu_wasm/build.rs b/ic10emu_wasm/build.rs index c452c1a..a2dda24 100644 --- a/ic10emu_wasm/build.rs +++ b/ic10emu_wasm/build.rs @@ -53,7 +53,7 @@ fn main() { "\n | ".to_owned(), ) .collect(); - let sc_tstype = format!("\nexport type SlotType = {};", sc_tsunion); + let sc_tstype = format!("\nexport type SortingClass = {};", sc_tsunion); ts_types.push_str(&sc_tstype); let st_tsunion: String = Itertools::intersperse( @@ -61,7 +61,7 @@ fn main() { "\n | ".to_owned(), ) .collect(); - let st_tstype = format!("\nexport type SortingClass = {};", st_tsunion); + let st_tstype = format!("\nexport type SlotType = {};", st_tsunion); ts_types.push_str(&st_tstype); let ct_tsunion: String = Itertools::intersperse( diff --git a/ic10emu_wasm/src/types.ts b/ic10emu_wasm/src/types.ts index 0b1ec95..db87feb 100644 --- a/ic10emu_wasm/src/types.ts +++ b/ic10emu_wasm/src/types.ts @@ -152,7 +152,7 @@ export interface FrozenIC { id: number; registers: number[]; ip: number; - ic: number[]; + ic: number; stack: number[]; aliases: Aliases; defines: Defines; diff --git a/www/src/ts/presets/demo.ts b/www/src/ts/presets/demo.ts new file mode 100644 index 0000000..00ffbc3 --- /dev/null +++ b/www/src/ts/presets/demo.ts @@ -0,0 +1,137 @@ +import { VMState } from "../session"; + +export const demoCode = `# Highlighting Demo + +# This is a comment + +# Hover a define id anywhere to see it's definition +define a_def 10 + +# Hover HASH("String")'s to see computed crc32 +# hover here vvvvvvvvvvvvvvvv +define a_hash HASH("This is a String") + +# hover over an alias anywhere in the code +# to see it's definition +alias a_var r0 +alias a_device d0 + +# instructions have Auto Completion, +# numeric logic types are identified on hover +s db 12 0 +# ^^ +# hover here + +# Enums and their values are Known, Hover them! +# vvvvvvvvvvvvvvvvvv +move r2 LogicType.Temperature +push r2 + +# same with constants +# vvvv +move r3 pinf + +# Labels are known +main: +l r1 dr15 RatioWater +move r2 100000.001 +push r2 + +# Hover Hash Strings of Known prefab names +# to get their documentation +# vvvvvvvvvvvvvvv +move r0 HASH("AccessCardBlack") +push r0 +beqzal r1 test + +# -2045627372 is the crc32 hash of a SolarPanel, +# hover it to see the documentation! +# vvvvvvvvvv +move r1 -2045627372 +jal test +move r1 $FF +push r1 +beqzal 0 test +move r1 %1000 +push r1 +yield +j main + +test: +add r15 r15 1 +j ra + +`; + +export const demoVMState: VMState = { + vm: { + ics: [ + { + device: 1, + id: 2, + registers: Array(18).fill(0), + ip: 0, + ic: 0, + stack: Array(512).fill(0), + aliases: new Map(), + defines: new Map(), + pins: Array(6).fill(undefined), + state: "Start", + code: demoCode, + }, + ], + devices: [ + { + id: 1, + prefab_name: "StructureCircuitHousing", + slots: [ + { + typ: "ProgrammableChip", + occupant: { + id: 2, + fields: { + "Quantity":{ + field_type: "Read", + value: 1 + }, + "MaxQuantity": { + field_type: "Read", + value: 1, + }, + "SortingClass": { + field_type: "Read", + value: 0, + } + }, + }, + }, + ], + connections: [ + { + CableNetwork: { + net: 1, + typ: "Data", + }, + }, + { + CableNetwork: { + net: undefined, + typ: "Power", + }, + }, + ], + fields: {}, + }, + ], + networks: [ + { + id: 1, + devices: [1], + power_only: [], + channels: Array(8).fill(NaN), + }, + ], + default_network: 1, + }, + activeIC: 1, +}; diff --git a/www/src/ts/presets/index.ts b/www/src/ts/presets/index.ts new file mode 100644 index 0000000..21054d2 --- /dev/null +++ b/www/src/ts/presets/index.ts @@ -0,0 +1,3 @@ +import { demoVMState } from "./demo"; + +export { demoVMState }; diff --git a/www/src/ts/session.ts b/www/src/ts/session.ts index 6125097..0523563 100644 --- a/www/src/ts/session.ts +++ b/www/src/ts/session.ts @@ -1,71 +1,13 @@ -const demoCode = `# Highlighting Demo -# This is a comment -# Hover a define id anywhere to see it's definition -define a_def 10 - -# Hover HASH("String")'s to see computed crc32 -# hover here vvvvvvvvvvvvvvvv -define a_hash HASH("This is a String") - -# hover over an alias anywhere in the code -# to see it's definition -alias a_var r0 -alias a_device d0 - -# instructions have Auto Completion, -# numeric logic types are identified on hover -s db 12 0 -# ^^ -# hover here - -# Enums and their values are Known, Hover them! -# vvvvvvvvvvvvvvvvvv -move r2 LogicType.Temperature -push r2 - -# same with constants -# vvvv -move r3 pinf - -# Labels are known -main: -l r1 dr15 RatioWater -move r2 100000.001 -push r2 - -# Hover Hash Strings of Known prefab names -# to get their documentation -# vvvvvvvvvvvvvvv -move r0 HASH("AccessCardBlack") -push r0 -beqzal r1 test - -# -2045627372 is the crc32 hash of a SolarPanel, -# hover it to see the documentation! -# vvvvvvvvvv -move r1 -2045627372 -jal test -move r1 $FF -push r1 -beqzal 0 test -move r1 %1000 -push r1 -yield -j main - -test: -add r15 r15 1 -j ra - -`; - -import type { ICError, FrozenVM } from "ic10emu_wasm"; +import type { ICError, FrozenVM, SlotType } from "ic10emu_wasm"; import { App } from "./app"; -import { openDB, DBSchema } from 'idb'; +import { openDB, DBSchema } from "idb"; import { fromJson, toJson } from "./utils"; +import * as presets from "./presets"; +const { demoVMState } = presets; + const LOCAL_DB_VERSION = 1; export class Session extends EventTarget { @@ -207,10 +149,14 @@ export class Session extends EventTarget { async load(data: VMState | OldPrograms | string) { if (typeof data === "string") { + this._activeIC = 1; + this.app.vm.restoreVMState(demoVMState.vm); this._programs = new Map([[1, data]]); - } else if ( "programs" in data) { + } else if ("programs" in data) { + this._activeIC = 1; + this.app.vm.restoreVMState(demoVMState.vm); this._programs = new Map(data.programs); - } else if ( "vm" in data ) { + } else if ("vm" in data) { this._programs = new Map(); const state = data.vm; // assign first so it's present when the @@ -227,7 +173,7 @@ export class Session extends EventTarget { async loadFromFragment() { const fragment = window.location.hash.slice(1); if (fragment === "demo") { - this.load(demoCode); + this.load(demoVMState); return; } if (fragment.length > 0) { @@ -257,9 +203,9 @@ export class Session extends EventTarget { upgrade(db, oldVersion, newVersion, transaction, event) { // only db verison currently known is v1 if (oldVersion < 1) { - const sessionStore = db.createObjectStore('sessions'); - sessionStore.createIndex('by-date', 'date'); - sessionStore.createIndex('by-name', 'name'); + const sessionStore = db.createObjectStore("sessions"); + sessionStore.createIndex("by-date", "date"); + sessionStore.createIndex("by-name", "name"); } }, }); @@ -271,14 +217,17 @@ export class Session extends EventTarget { activeIC: this.activeIC, }; const db = await this.openIndexDB(); - const transaction = db.transaction(['sessions'], "readwrite"); + const transaction = db.transaction(["sessions"], "readwrite"); const sessionStore = transaction.objectStore("sessions"); - await sessionStore.put({ + await sessionStore.put( + { + name, + date: new Date(), + session: state, + }, name, - date: new Date(), - session: state, - }, name); - this.dispatchEvent(new CustomEvent("sessions-local-update")) + ); + this.dispatchEvent(new CustomEvent("sessions-local-update")); } async loadFromLocal(name: string) { @@ -292,14 +241,14 @@ export class Session extends EventTarget { async deleteLocalSave(name: string) { const db = await this.openIndexDB(); - const transaction = db.transaction(['sessions'], "readwrite"); + const transaction = db.transaction(["sessions"], "readwrite"); const sessionStore = transaction.objectStore("sessions"); await sessionStore.delete(name); - this.dispatchEvent(new CustomEvent("sessions-local-update")) + this.dispatchEvent(new CustomEvent("sessions-local-update")); } async getLocalSaved() { const db = await this.openIndexDB(); - const sessions = await db.getAll('sessions'); + const sessions = await db.getAll("sessions"); return sessions; } } @@ -316,16 +265,16 @@ interface AppDBSchemaV1 extends DBSchema { name: string; date: Date; session: VMState; - } - indexes: { - 'by-date': Date; - 'by-name': string; }; - } + indexes: { + "by-date": Date; + "by-name": string; + }; + }; } export interface OldPrograms { - programs: [number, string][] + programs: [number, string][]; } const byteToHex: string[] = []; diff --git a/www/src/ts/virtual_machine/device.ts b/www/src/ts/virtual_machine/device.ts index 9ecff62..57b8415 100644 --- a/www/src/ts/virtual_machine/device.ts +++ b/www/src/ts/virtual_machine/device.ts @@ -165,6 +165,8 @@ export class VMDeviceCard extends VMDeviceMixin(BaseElement) { @state() set deviceDB(val: DeviceDB) { this._deviceDB = val; + this.updateDevice(); + this.requestUpdate(); } connectedCallback(): void {