7586531cf677ca7a6f895c70a6641ed199b06289
[ScratchNES.git] / src / CPU / parse-reference.js
1 /**
2 * parse-reference.js
3 * argv[2] is a text-only 6502 reference sheet
4 */
5
6 var fs = require("fs");
7 PolyFill();
8
9 var reference = fs.readFileSync(process.argv[2])
10 .toString()
11 .split("\n");
12
13 // an instruction is delimited by a character on column 0
14 // split into instructions
15
16 var instructions = [[]];
17 var temp = 0;
18
19 for(var i = 0; i < reference.length; ++i) {
20 if(reference[i].length == 0 || reference[i][0] == " ") {
21 instructions[temp].push(reference[i]);
22 } else {
23 instructions.push([reference[i]]);
24 ++temp;
25 }
26 }
27
28 // remove car
29 instructions = instructions.slice(1);
30
31 // parse instructions individually
32 var opcodes = [];
33 for(var i = 0; i < instructions.length; ++i) {
34 opcodes = opcodes.concat(parseInstruction(instructions[i]));
35 }
36
37 // sort by opcode
38 var table = [];
39 for(var j = 0; j < 256; ++j) { table.push(null); }
40
41 opcodes.forEach(function(opcode) {
42 table[opcode.opcode] = opcode;
43 });
44
45 fs.writeFileSync("bin/table.json", JSON.stringify(table));
46
47 function parseInstruction(instruction) {
48 // get instruction name
49 var name = instruction[0].split(" ")[0];
50
51 // find the opcode list
52 var nums = instruction.findIndex(function(el) {
53 return el.indexOf("---------") > -1;
54 });
55
56 var forms = instruction.slice(nums + 1, -2).map(function(form) {
57 var addressing = form.slice(6-1, 20-1).trim();
58 var assembler = form.slice(20-1, 34-1).trim();
59 var opcode = ("0x" + form.slice(34-1, 40-1).trim()) * 1;
60 var size = form.slice(40-1, 46-1) * 1;
61 var cycles = form.slice(46-1).trim();
62
63 return {
64 "name": name,
65 "addressing": addressing,
66 "size": size,
67 "opcode": opcode,
68 "cycles": cycles,
69 "assembler": assembler
70 };
71 });
72
73 return forms;
74 }
75
76 function PolyFill() {
77 if (!Array.prototype.findIndex) {
78 Array.prototype.findIndex = function(predicate) {
79 if (this === null) {
80 throw new TypeError('Array.prototype.findIndex called on null or undefined');
81 }
82 if (typeof predicate !== 'function') {
83 throw new TypeError('predicate must be a function');
84 }
85 var list = Object(this);
86 var length = list.length >>> 0;
87 var thisArg = arguments[1];
88 var value;
89
90 for (var i = 0; i < length; i++) {
91 value = list[i];
92 if (predicate.call(thisArg, value, i, list)) {
93 return i;
94 }
95 }
96 return -1;
97 };
98 }
99 }
This page took 0.091784 seconds and 5 git commands to generate.