-
Notifications
You must be signed in to change notification settings - Fork 0
/
asmDefWriter.js
43 lines (38 loc) · 1.69 KB
/
asmDefWriter.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
const aluOperations = require('./alu.js')
const microcode = require('./microcode.js')
console.log(`declare var l: any;`)
console.log(`declare var machineCode: any;`)
console.log(`declare function ________________(any): void;`)
console.log(`declare function compile(): void;`)
for (let instructionName in microcode.Instructions) {
const instruction = microcode.Instructions[instructionName]
const argCount = instructionName === 'noop' ? 0 : determineArgumentCountFromInstructionSignals(instruction.signals)
if (!instruction.idMax) {
const functionName = instructionName
declareFunction(functionName, argCount, instruction.id)
}
else {
for (const aluOpName in aluOperations) {
const op = aluOperations[aluOpName]
const functionName = `${aluOpName}_${instructionName.replace(/aluTo/, 'into_')}`
declareFunction(functionName, argCount, instruction.id + op.opIndex)
}
}
}
function determineArgumentCountFromInstructionSignals(signals) {
let maxInstructionReads = 0
for (let flags = 0; flags <= microcode.FLAG_MASK; flags += 1) {
const controlSignalSequence = signals(flags)
let instructionReads = 0
controlSignalSequence.forEach(controlSignals => {
const memoryRead = controlSignals.split(' ').filter(controlSignal => controlSignal === 'MR').length
const memorySelect = controlSignals.split(' ').filter(controlSignal => controlSignal === 'MS').length
instructionReads += (memoryRead && !memorySelect) ? 1 : 0
})
maxInstructionReads = Math.max(maxInstructionReads, instructionReads)
}
return maxInstructionReads
}
function declareFunction(functionName, argCount, instructionCode) {
console.log(`declare function ${functionName}(${Array(argCount).fill('any').join(', ')}): void;`)
}