-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions.js
138 lines (109 loc) · 2.86 KB
/
instructions.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
const fs = require('fs');
// const parse = require('../parsehex');
class Instructions {
constructor(opts) {
this.map = new Map();
this.buf = null;
this.offset = 0;
this.endian = 'little';
this.location = 0;
this.save = opts && !!opts.save;
this.lines = '';
}
init() {
const result = fs.readFileSync('opcodes.csv', 'utf-8');
if (result) {
const split = result.split('\n');
split.forEach(row => {
const vals = row.match(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g);
if (!vals) return;
if (vals[1] === '"-"') return;
this.map.set(vals[0], {
command: vals[1].replace(/"/gi, ''),
bytes: Number(vals[2]),
flags: vals[3] !== '' && vals[3].length ? vals[3].split(',') : [],
});
});
} else {
console.log('Error reading file');
}
}
get formattedLocation() {
return `${this.offset.toString(16).padStart(4, '0')},${this.offset
.toString(10)
.padStart(4, '0')}\t`;
}
parse() {
// this.buf = parse();
this.loadFile();
while (this.read());
if (this.write) {
this.saveFile();
}
}
loadFile() {
this.buf = fs.readFileSync('invaders');
}
iterateOffset() {
this.offset += 1;
}
read() {
let key = '';
try {
key = `0x${this.buf[this.offset].toString(16).padStart(2, '0')}`;
} catch (error) {
return false;
}
if (!this.map.has(key)) {
this.iterateOffset();
return true;
}
const res = this.map.get(key);
let { command } = res;
const instruction = command.split(' ')[0];
command = command.replace(instruction, instruction.padEnd(8));
const location = this.formattedLocation;
if (res.bytes === 1) {
this.write(`${location}\t${key}\t${''.padEnd(12)}${command}\n`);
this.iterateOffset();
} else if (res.bytes > 1) {
this.iterateOffset();
let bytes = this.take(res.bytes - 1);
bytes = bytes.map(val => val.padStart(2, '0'));
if (command.includes('adr')) {
command = command.replace('adr', `$${bytes.reverse().join('')}`);
}
const formatted = (bytes.length ? bytes.join(' ') : '').padEnd(12);
this.write(`${location}\t${key},\t${formatted}${command}\n`);
}
return this.offset <= this.buf.length;
}
take(count) {
const res = [];
for (let i = 0; i < count; i += 1) {
if (this.offset < this.buf.length) {
res.push(this.buf[this.offset].toString(16));
}
this.iterateOffset();
}
return res;
}
write(line) {
if (this.save) {
this.lines += line;
} else {
console.log(line);
}
}
saveFile() {
fs.writeFile('./invaders.out.txt', this.lines, err => {
console.log(err);
});
}
}
const instructions = new Instructions({
save: true,
write: true,
});
instructions.init();
instructions.parse();