rk86 2.0.7 → 2.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/rk86.js +48 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rk86",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
4
4
  "description": "Эмулятор Радио-86РК (Intel 8080) для терминала",
5
5
  "bin": {
6
6
  "rk86": "rk86.js"
package/rk86.js CHANGED
@@ -2468,6 +2468,18 @@ var extract_rk86_word = function(v, i) {
2468
2468
  };
2469
2469
  var to_text = (binary) => binary.reduce((a, x) => a + String.fromCharCode(x), "");
2470
2470
  var is_hex_file = (image) => to_text(image.slice(0, 6)) === "#!rk86";
2471
+ var parse = (binary) => {
2472
+ try {
2473
+ if (!binary)
2474
+ return { ok: false };
2475
+ if (binary instanceof Uint8Array)
2476
+ binary = Array.from(binary);
2477
+ const text = to_text(binary);
2478
+ return { ok: true, json: JSON.parse(text) };
2479
+ } catch {
2480
+ return { ok: false };
2481
+ }
2482
+ };
2471
2483
  var convert_hex_to_binary = function(text) {
2472
2484
  const lines = text.split(`
2473
2485
  `).filter((line) => line.trim().length).filter((line) => !line.startsWith(";") && !line.startsWith("#"));
@@ -2659,7 +2671,7 @@ function create(array, width = 16) {
2659
2671
  }
2660
2672
  return v;
2661
2673
  }
2662
- function parse(hex2) {
2674
+ function parse2(hex2) {
2663
2675
  const array = [];
2664
2676
  for (let [label, line] of Object.entries(hex2)) {
2665
2677
  const address = parseInt(label.slice(1), 16);
@@ -2773,7 +2785,7 @@ class Memory {
2773
2785
  this.video_screen_cursor_y = snapshot.video_screen_cursor_y;
2774
2786
  this.last_access_address = h(snapshot.last_access_address);
2775
2787
  this.last_access_operation = snapshot.last_access_operation;
2776
- this.buf = parse(snapshot.memory);
2788
+ this.buf = parse2(snapshot.memory);
2777
2789
  };
2778
2790
  invalidate_access_variables() {
2779
2791
  this.last_access_address = 0;
@@ -3208,6 +3220,29 @@ class Screen {
3208
3220
  }
3209
3221
  }
3210
3222
 
3223
+ // src/lib/rk86_snapshot.ts
3224
+ function rk86_snapshot_restore(snapshot, machine, keys_injector) {
3225
+ try {
3226
+ const json = typeof snapshot === "string" ? JSON.parse(snapshot) : snapshot;
3227
+ if (json.id != "rk86")
3228
+ return false;
3229
+ if (!machine)
3230
+ return false;
3231
+ const { screen, cpu, memory, keyboard } = machine;
3232
+ cpu.import(json.cpu);
3233
+ keyboard.import(json.keyboard);
3234
+ screen.import(json.screen);
3235
+ memory.import(json.memory);
3236
+ screen.apply_import();
3237
+ if (keys_injector && json.boot?.keyboard)
3238
+ keys_injector(json.boot?.keyboard);
3239
+ return true;
3240
+ } catch (e) {
3241
+ console.error("failed restoring snapshot", e);
3242
+ return false;
3243
+ }
3244
+ }
3245
+
3211
3246
  // src/lib/rk86_tape.ts
3212
3247
  class Tape {
3213
3248
  machine;
@@ -3684,10 +3719,17 @@ async function main() {
3684
3719
  let entryPoint;
3685
3720
  if (programFile) {
3686
3721
  const content = await fetchFile(programFile);
3687
- const file = parse_rk86_binary(programFile, content);
3688
- machine.memory.load_file(file);
3689
- entryPoint = file.entry;
3690
- console.error(`\u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043D: ${programFile} (${file.start.toString(16)}-${file.end.toString(16)}, G${file.entry.toString(16)})`);
3722
+ const { ok, json } = parse(content);
3723
+ if (ok) {
3724
+ rk86_snapshot_restore(json, machine);
3725
+ entryPoint = parseInt(json.cpu.pc);
3726
+ console.error(`\u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043D \u043E\u0431\u0440\u0430\u0437: ${programFile} (PC=${entryPoint.toString(16)})`);
3727
+ } else {
3728
+ const file = parse_rk86_binary(programFile, content);
3729
+ machine.memory.load_file(file);
3730
+ entryPoint = file.entry;
3731
+ console.error(`\u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043D: ${programFile} (${file.start.toString(16)}-${file.end.toString(16)}, G${file.entry.toString(16)})`);
3732
+ }
3691
3733
  }
3692
3734
  process.stdout.write("\x1B[?25l");
3693
3735
  process.stdout.write("\x1B[2J");