-
Notifications
You must be signed in to change notification settings - Fork 0
/
asm.js
162 lines (147 loc) · 5.21 KB
/
asm.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
const aluOperations = require('./alu.js')
const microcode = require('./microcode.js')
const util = require('./util.js')
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)
}
}
}
const machineCode = global['machineCode'] = []
const sourceMap = []
module.exports.reset = () => {
machineCode.length = 0
sourceMap.length = 0
Object.keys(labelDict).forEach((key) => { delete labelDict[key] })
}
function declareFunction(functionName, argCount, instructionCode) {
//console.log(functionName)
global[functionName] = (...args) => {
if (args.length !== argCount) {
throw new Error(`${functionName} accepts exactly ${argCount} argument(s)`)
}
sourceMap.push({ addr: machineCode.length, functionName, argCount })
machineCode.push(instructionCode, ...args)
}
}
// Labels
let programOffset
const labelDict = {}
global['Label'] = class Label {
constructor(name) { this.name = name }
setHere() { this.location = machineCode.length }
getLocation() {
if (this.location === undefined) { throw new Error(`Label ${this.name} cannot be resolved because it was never setHere() to location in machinecode`) }
return programOffset + this.location
}
resolveShort(jumpSourceAddr) {
if (jumpSourceAddr >> 8 !== this.getLocation() >> 8) { throw new Error(`Label.resolveShort attempting to jump from outside of page!`) }
return this.getLocation() & 0xff
}
resolveLong() {
return this.getLocation()
}
getLow() {
return this.getLocation() & 0xff
}
getHigh() {
return this.getLocation() >> 8
}
}
global['l'] = new Proxy(labelDict, {
get(target, key) {
if (key === '[object Object]') { throw new Error('wat') }
if (!target[key]) {
//console.log(`new Label ${key}`)
target[key] = new Label(key)
}
else {
//console.log(`old Label ${key}`)
}
return target[key]
}
})
global['________________'] = (label) => {
label.setHere()
}
// Compile
const functionArgumentResolvers = {}
'jump JZ JNZ JC JNC JNK JNKA'.split(' ').forEach(functionName => {
functionArgumentResolvers[functionName] = (labelArg, jumpSourceAddr) => {
return [ labelArg.resolveShort(jumpSourceAddr) ]
}
})
'jumpFar JZFar JNZFar JCFar JNCFar JNKFar JNKAFar'.split(' ').forEach(functionName => {
functionArgumentResolvers[functionName] = (labelArg, jumpSourceAddr) => {
const location = labelArg.resolveLong(jumpSourceAddr)
return [ location >> 8, location & 0xff ]
}
})
let alreadyCompiled = false
global['compile'] = (programOffset_) => {
if (alreadyCompiled) { throw new Error('already compiled!') }
alreadyCompiled = true
programOffset = programOffset_
if (programOffset === undefined) { programOffset = 0 }
for (let addr = 0; addr < machineCode.length; addr += 1) {
//if (machineCode[addr] instanceof Label) {
// machineCode[addr] = machineCode[addr].resolveShort(addr)
//}
if (typeof(machineCode[addr]) === 'string') {
machineCode[addr] = machineCode[addr].charCodeAt(0)
}
}
// resolve labels
sourceMap.forEach(({ addr, functionName, argCount }) => {
if (argCount >= 1) {
const firstArg = machineCode[addr + 1]
if (firstArg instanceof Label) {
const resolvedValues = functionArgumentResolvers[functionName](firstArg, addr + argCount)
machineCode.splice(addr + 1, argCount, ...resolvedValues)
}
}
for (let i = 1; i <= argCount; i += 1) {
if (typeof machineCode[addr + i] === 'function') {
const resolvedValue = machineCode[addr + i](addr)
machineCode.splice(addr + i, 1, resolvedValue)
}
}
})
// display source map
console.log(`SourceMap:`)
let sourceMapContent = ''
sourceMap.forEach(({ addr, functionName, argCount }) => {
const values = machineCode.slice(addr, addr + argCount + 1)
//console.log(JSON.stringify(values))
const addrDisplay = (addr).toString(16)
const valuesDisplay = values.map(n => util.leftPad(n.toString(16), 2)).join(' ')
sourceMapContent += `${addrDisplay} : ${util.rightPad(valuesDisplay, 10, ' ')} // ${functionName}\n`
})
console.log(sourceMapContent)
if (machineCode.length > 0x100) {
console.log(`*** WARNING *** machineCode extends beyond 0xFF - naive short jumps will likely fail`)
}
}
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
}