-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoder.js
More file actions
33 lines (27 loc) · 789 Bytes
/
coder.js
File metadata and controls
33 lines (27 loc) · 789 Bytes
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
var Instructions = require('./instructions')
var Tables = require('./tables')
function Coder() {}
module.exports = Coder
Coder.prototype.encode = function(instruction) {
if (instruction instanceof Instructions.AInstruction) {
return padZero(dec2bin(parseInt(instruction.label)), 16)
} else if (instruction instanceof Instructions.CInstruction) {
var dest = Tables.DEST[instruction.dest]
var comp = Tables.COMP[instruction.comp]
var jump = Tables.JUMP[instruction.jump]
return '111' + comp + dest + jump
} else {
return null
}
}
function dec2bin(dec) {
return (dec >>> 0).toString(2)
}
function padZero(arg, len) {
var zeroes = len - arg.length
var padding = ''
while (zeroes--) {
padding = padding.concat('0')
}
return padding + arg
}