25 Companion zmachine.js (4/28)

    this.alphabets = [A0, A1, A2];
    const custom = v >= 5 ? this.rw(0x34) : 0;
    if (custom) {
      const read = (base) => Array.from({ length: 26 }, (_, i) => this.zsciiToChar(this.mem[custom + base + i])).join('');
      this.alphabets = [read(0), read(26), ' \n' + read(52).slice(2)];
    }

    this.extra = DEFAULT_EXTRA.split('');
    const ext = v >= 5 ? this.rw(0x36) : 0;
    if (ext && this.rw(ext) >= 3) {
      const table = this.rw(ext + 6);
      if (table) {
        const n = this.mem[table];
        this.extra = Array.from({ length: n }, (_, i) => String.fromCodePoint(this.rw(table + 1 + 2 * i)));
      }
    }
    this.unicodeToZscii = new Map(this.extra.map((ch, i) => [ch, 155 + i]));
  }

  seed(n) {
    // xorshift32; the state lives in the snapshot so restores replay the same way
    this.rng = (Number(n) >>> 0) || 0x2545f491;
  }

  nextRandom() {
    let x = this.rng;
    x ^= x << 13; x >>>= 0;
    x ^= x >>> 17;
    x ^= x << 5; x >>>= 0;
    this.rng = x;
    return x;
  }

  // ---------------------------------------------------------------- memory
‹ PreviousReader file (.txt)Next ›