-
Notifications
You must be signed in to change notification settings - Fork 0
/
kasm.js
executable file
·87 lines (68 loc) · 1.82 KB
/
kasm.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
#!/usr/bin/env node
const assemble = require("./assembler.js");
const fs=require("fs");
var fileLoader = filename=>fs.readFileSync(filename,'utf8');
function byteAsHex(byte) {
let d1 = ((byte >> 4) &0x0f);
let d2 = byte &0x0f;
return d1.toString(16)+d2.toString(16);
}
var hexFile = function(){
let file = [];
function hexRecord(data,offset=0,type=0) {
let offsetLow = offset&0xff;
let offsetHigh = (offset >>8) & 0xff;
let bytes = [data.length,offsetHigh,offsetLow,type,...data];
let result=":";
let a = 0;
for (b of bytes) {
result+=byteAsHex(b);
a+=b;
}
result += byteAsHex(-a);
return result;
}
function addEof() {
file.push( hexRecord([],0,1) );
}
function addAddress(address) {
let data = [
(address >> 24) & 0xff,
(address >> 16) & 0xff,
(address >> 8) & 0xff,
(address >> 0) & 0xff,
];
file.push(hexRecord(data,0,5));
}
function addData(address,data) {
// addAddress(address);
let offset=0;
for (let offset=0; offset<data.length; offset+=16) {
let line = data.slice(offset,offset+16);
file.push(hexRecord(line,offset+address,0));
}
}
function clear() {
file=[];
}
function getText() {
return file.join('\n');
}
return {addEof, addAddress, addData, clear, getText, hexRecord};
}
var mainSourceName = process.argv[process.argv.length-1];
var outputFilename = "out.hex";
console.log ("assembling "+mainSourceName);
fileLoader(mainSourceName);
try {
var {output,debugInfo} = assemble(mainSourceName,fileLoader);
let hex=hexFile();
for (let chunk of output) {
hex.addData(chunk.address*2,new Uint8Array(chunk.data.buffer));
}
hex.addEof();
console.log("writing "+outputFilename );
fs.writeFileSync(outputFilename,hex.getText());
} catch (e) {
console.log("Fatal error:" + e.message);
}