Files
sloop-fm1/web/fm1pkg.js
T
2026-10-04 11:08:49 -04:00

27 lines
1000 B
JavaScript

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
//
// FM-1 .fwsc packages in the browser: the package identity and the image the
// device reads during an update.
const BLOCKS = 20, BLK = 0x30, KEEP = 0x2F;
// the package identity ("FM-1_904"): one marker byte after each of the first 20 blocks
export function productOf(fwsc) {
if (fwsc.length < BLOCKS * BLK) throw new Error("not an FM-1 package (too short)");
let s = "";
for (let i = 0; i < BLOCKS; i++) {
const m = fwsc[i * BLK + KEEP];
if (m !== 0x7D) s += String.fromCharCode((m - i - 1) & 0xFF);
}
return s;
}
// the image the device addresses during an update: the .fwsc without the 20 marker bytes
export function logicalImage(fwsc) {
const out = new Uint8Array(fwsc.length - BLOCKS);
for (let i = 0; i < BLOCKS; i++) out.set(fwsc.subarray(i * BLK, i * BLK + KEEP), i * KEEP);
out.set(fwsc.subarray(BLOCKS * BLK), BLOCKS * KEEP);
return out;
}