aboutsummaryrefslogtreecommitdiff
path: root/src/js/extract_genome.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/extract_genome.js')
-rw-r--r--src/js/extract_genome.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/js/extract_genome.js b/src/js/extract_genome.js
new file mode 100644
index 0000000..5cb2b38
--- /dev/null
+++ b/src/js/extract_genome.js
@@ -0,0 +1,45 @@
+const fs = require('fs');
+const path = require('path');
+
+const srcPath = path.join(__dirname, '..', 'core', 'genome.h');
+const outPath = path.join(__dirname, 'genome.json');
+
+const text = fs.readFileSync(srcPath, 'utf8');
+const gorgMatch = text.match(/#define\s+GORG\s+(\d+)/);
+const glenMatch = text.match(/#define\s+GLEN\s+(\d+)/);
+
+const gorg = parseInt(gorgMatch[1], 10);
+const glen = parseInt(glenMatch[1], 10);
+
+const arrMatch = text.match(/genome\[\]\s*=\s*\{([\s\S]*?)\};/);
+if (!arrMatch) {
+ console.error('Could not find genome array in genome.h');
+ process.exit(1);
+}
+
+const words = arrMatch[1]
+ .split(',')
+ .map(s => s.trim())
+ .filter(s => s.length > 0)
+ .map(s => s); // keep as string numbers
+
+console.log(`Parsed genome: GORG=${gorg}, GLEN=${words.length}`);
+
+if (words.length !== glen) throw new Error('genome length does not match GLEN');
+
+// the compiler's map, from src/core/memmap.h, as typescript constants: the js vm reads
+// them instead of its own copies. the values are computed here, from the map.
+const { mapValues } = require('./mapvalues.js');
+const names = ['MW', 'PW', 'ZFP', 'GORG', 'DS0', 'DSLIM', 'RS0', 'RSLIM', 'SYSGATE', 'HEAP_LO', 'HEAP_HI'];
+const map = mapValues(path.join(__dirname, '..', 'core', 'memmap.h'), names);
+if (map.GORG !== gorg) throw new Error('genome GORG ' + gorg + ' is not the map GORG ' + map.GORG);
+fs.writeFileSync(outPath, JSON.stringify({ gorg, glen, words }));
+console.log(`saved genome to ${outPath}`);
+const lines = names.map(k => ` ${k}: ${map[k]},`).join('\n');
+fs.writeFileSync(path.join(__dirname, 'layout.ts'),
+ '// generated by src/js/extract_genome.js from src/core/memmap.h. do not edit.\n' +
+ 'export const MAP = {\n' + lines + '\n} as const;\n');
+console.log(`Saved the map to ${path.join(__dirname, 'layout.ts')}`);
+fs.writeFileSync(path.join(__dirname, 'layout.js'),
+ '// generated by src/js/extract_genome.js from src/core/memmap.h. do not edit.\n' +
+ 'exports.MAP = ' + JSON.stringify(map, null, 4) + ';\n');