-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgencode.js
365 lines (284 loc) · 9.28 KB
/
gencode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
// Read and digest the microcode from the klx.mcr listing file,
// applying the conditions contained herein and the field names from
// define.mic. I know this is hacky as fuck. But it gets the job done
// and there will never be any new file format to handle.
const fs = require('fs');
const _ = require('lodash');
const util = require('util');
const StampIt = require('@stamp/it');
const {octal} = require('./util');
const EBOX = require('./ebox');
const {Named, EBOXUnit, CRAM, DRAM} = EBOX;
const cramDefs = {bpw: 84};
const dramDefs = {bpw: 12};
var cram, cramLines;
var dram;
function readMicroAssemblyListing() {
// Read our microcode assembly listing so we can suck out its brainz.
const ucodeListing = _.takeWhile(
fs.readFileSync('kl10-source/klx.mcr').toString()
.replace(/\f[^\n]*\n[^\n]*\n/g, '') // Elminate formfeeds and page headers
.split(/[\n\r\f]+/),
line => !line.match(/^\s+END\s*$/));
// The xram arrays are the BigInt representation of the microcode
// and the lines arrays are the strings of source code for each. The
// xramDefs objects are indexed by microcode field. Each property
// there defines an object whose properties are the field value
// names corresponding values for that field.
({bytes: cram, linesArray: cramLines} =
parse(ucodeListing,
/^U\s+(\d+), (\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+).*/,
/^.*?;NICOND \(NXT INSTR\)/));
({bytes: dram} = parse(ucodeListing, /^D\s+(\d+), (\d+),(\d+).*/));
}
// Extract from BigInt word `w` a PDP10 bit-numbering field starting
// at bit `s` and going through bit `e` (inclusive) in a word of
// `wordSize` bits.
function extract(w, s, e, wordSize) {
// E.g., extract s=1, e=3 with wordSize=10
// 0123456789
// xFFFxxxxxx
return (BigInt.asUintN(wordSize, w) >> BigInt(wordSize - e - 1)) &
((1n << BigInt(e - s + 1)) - 1n);
}
// Read DEFINE.MIC for all field definitions and the names of field values.
function readAndHandleDirectives() {
// These point to one or the other as we go through define.mic.
let fieldDefs = cramDefs;
let ramName = 'CRAM';
const ifStack = ['TRUE'];
let fn = null;
let widest = 0;
const conditionals = `\
.SET/TRUE=1 ; Define TRUE for ifStack
.SET/SNORM.OPT=1
.SET/XADDR=1
.SET/EPT540=1
.SET/LONG.PC=1
.SET/MODEL.B=1
.SET/KLPAGE=1
.SET/FPLONG=0
.SET/BLT.PXCT=1
.SET/SMP=0 ;No SMP (DOES RPW instead of RW FOR DPB, IDPB)
.SET/EXTEXP=1
.SET/MULTI=1 ;DOES NOT CACHE PAGE TABLE DATA
.SET/NOCST=1 ;DOES NOT DO AGE UPDATES, ETC. WITH CST = 0
.SET/OWGBP=1 ;ONE WORD GLOBAL BYTE POINTERS
.SET/IPA20=1 ;IPA20-L
.SET/GFTCNV=0 ;DO NOT DO GFLOAT CONVERSION INSTRUCTIONS [273]
;SAVES 75 WORDS. MONITOR WILL TAKE CARE OF THEM.
`.split(/\n/)
.reduce((cur, d) => {
const [, def, val] = d.match(/^\s*\.SET\/([^=]+)=(\d+)/) || [];
if (def) cur[def] = +val;
return cur;
}, {});
fs.readFileSync('kl10-source/define.mic')
.toString()
.split(/[\r\n\f]/)
.forEach(f => {
// Strip ^L
f = f.replace(/\f/, '');
// Strip comments
f = f.replace(/\s*;.*/, '');
// Handle directives
let m = f.match(/^\s*\.(dcode|ucode|endif|ifnot|if)(?:\/([^\s]+))?.*/im);
if (m) {
let dir = m[1].toUpperCase();
let name = m[2] ? m[2].toUpperCase() : '';
// console.log("directive=", dir, name);
switch (dir) {
case 'DCODE':
// console.log(".DCODE");
fieldDefs = dramDefs;
ramName = 'DRAM';
return;
case 'UCODE':
// console.log(".UCODE");
fieldDefs = cramDefs;
ramName = 'CRAM';
return;
case 'IF':
// Oddly, .IF and .IFNOT with the SAME CONDITIONAL don't nest,
// they act as ELSEIFs.
if ("!" + name === ifStack.slice(-1)[0]) {
// console.log("Pop-Push", name);
ifStack.pop();
}
ifStack.push(name);
// console.log("IF", ifStack.slice(-1)[0]);
return;
case 'IFNOT':
// Oddly, .IF and .IFNOT with the SAME CONDITIONAL don't nest,
// they act as ELSEIFs.
if (name === ifStack.slice(-1)[0]) {
// console.log("Pop-Push", "!" + name);
ifStack.pop();
}
ifStack.push('!' + name);
// console.log("IF !", ifStack.slice(-1)[0]);
return;
case 'ENDIF':
ifStack.pop();
// console.log("Back to", ifStack.slice(-1)[0]);
return;
}
}
// Stop if we're skipping based on .IF/.IFNOT
if (!conditionals[ifStack.slice(-1)[0]]) return;
// Ignore other directives
if (f.match(/^\s*\..*/)) return;
// console.log("Def line?", f);
m = f.match(/^([^.][^/=]*)\/=<(\d+)(?::(\d+))?>.*/);
if (m) {
// console.log(`${ramName}: ${m[1]}/=<${m[2]}${m[3]? ":" + m[3] : ''}>`);
fn = m[1];
fieldDefs[fn] = {s: +m[2]};
if (m[3])
fieldDefs[fn].e = +m[3];
else
fieldDefs[fn].e = +m[2];
let w = fieldDefs[fn].e - fieldDefs[fn].b + 1;
if (w > widest) widest = w;
} else if ((m = f.match(/^\s+(.*)=(\d+)/m))) {
fieldDefs[fn][m[1]] = +m[2];
}
});
}
// Parse a line of microassembly listing, extracting address and code.
// This works for CRAM or DRAM with the only different controlled by
// the `re` parameter. `linesRE` (only specified for CRAM) is the
// position to reference as the start of source code for the first
// CRAM word.
function parse(lines, re, linesRE) {
let lastPos = lines.findIndex(line => line.match(linesRE));
// This has to be a reduce() instead of a map because the
// microassembly listing builds microcode words in essentially
// random address order.
return lines.reduce(({bytes, linesArray}, line, lineX) => {
const m = line.match(re);
if (m) {
const a = parseInt(m[1], 8);
bytes[a] = `0o${m.slice(2).join('')}`;
if (lastPos) {
linesArray[a] = lines.slice(lastPos, lineX + 1).join('\n');
lastPos = lineX + 1;
}
}
return {bytes, linesArray};
}, {bytes: [], linesArray: []});
}
// Extract named `field` (as specified in `defs`) from microword `mw`
// and return it as a simple integer (not a BigInt).
function getField(mw, defs, field) {
const def = defs[field];
return Number(extract(mw, def.s, def.e, defs.bpw));
}
function handleSPEC(mw) {
const SPEC = getField(mw, cramDefs, 'SPEC');
const code = [
`// SPEC = ${octal(SPEC)}`,
];
switch (SPEC) {
case 0o15: // LOAD PC
code.push(`PC.input = AR_12_35;`);
break;
case 0o24: // FLAG CTL
const func = getField(mw, cramDefs, '#');
/*
if (func === 0o20) {
code.push(`SCD_FLAGS.input = AR_00_12;`);
}
*/
break;
}
return code;
}
function generateModel() {
const moduleHeader = `\
'use strict';
`;
const allCode = [
moduleHeader,
dumpDefs('cram', cramDefs),
dumpDefs('dram', dramDefs),
].join('\n\n');
fs.writeFileSync(`fields-model.js`, allCode, {mode: 0o664});
function dumpDefs(typeName, defs) {
return `module.exports.${typeName}Fields = ${util.inspect(defs)};`;
}
}
function generateFunctions() {
return _.range(CRAM.nWords).map(ma => {
const mw = cram[ma];
const headerCode = [
`// uW = ${octal(mw, cramDefs.bpw/3)}`,
`// J = ${octal(getField(mw, cramDefs, 'J'))}`,
`// # = ${octal(getField(mw, cramDefs, '#'))}`,
].join('\n ');
const specCode = handleSPEC(mw);
const latchCode = [];
latchCode.push(`EBOX.latch();`);
const clockCode = [];
clockCode.push(`EBOX.clockEdge();`);
return `\
function cram_${octal(ma)}(cpu) {
${[
headerCode,
latchCode,
specCode,
clockCode,
].flat().join('\n ')
}
},
`;
}).join('\n\n');
}
function generateXRAMArray(wordsArray, bitWidth) {
return wordsArray.map(mw => `${octal(mw, bitWidth/3, 0)}n`).join(',\n ');
}
function generateLinesArray(linesArray) {
return linesArray.map(line => '`' + line + '`').join(',\n ');
}
function generateFile({filename,
prefixCode = '',
code,
suffixCode = '',
arrayName = 'module.exports'}) {
const allCode = `\
'use strict';
${prefixCode}
${arrayName} = [
${code}
];
${suffixCode}
`;
fs.writeFileSync(filename, allCode, {mode: 0o664});
}
function generateMicrocode() {
generateFile({filename: 'cram.js', code: generateXRAMArray(cram, 84)});
generateFile({filename: 'cram-lines.js', code: generateLinesArray(cramLines)});
generateFile({filename: 'dram.js', code: generateXRAMArray(dram, 24)});
generateFile({filename: 'microcode.js', arrayName: 'const ops',
prefixCode: `\
var ${Object.keys(EBOX).join(',')};
module.exports.initialize = function initialize(e) {
EBOX = e;
${Object.keys(Named.units)
.map(n => `${n} = e.units.${n};`)
.join('\n ')}
};
`,
code: generateFunctions(),
suffixCode: `\
module.exports.ops = ops;
`});
}
function main() {
readMicroAssemblyListing();
readAndHandleDirectives();
generateModel();
generateMicrocode();
}
main();