Flatten; simplify opcodes
[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 var opcodes = [];
32 for(var i = 0; i < instructions.length; ++i) {
33 opcodes = opcodes.concat(parseInstruction(instructions[i]));
34 }
35
36 console.log(opcodes);
37
38 function parseInstruction(instruction) {
39 // get instruction name
40 var name = instruction[0].split(" ")[0];
41
42 // find the opcode list
43 var nums = instruction.findIndex(function(el) {
44 return el.indexOf("---------") > -1;
45 });
46
47 var forms = instruction.slice(nums + 1, -2).map(function(form) {
48 var addressing = form.slice(6-1, 20-1).trim();
49 var assembler = form.slice(20-1, 34-1).trim();
50 var opcode = ("0x" + form.slice(34-1, 40-1).trim()) * 1;
51 var size = form.slice(40-1, 46-1) * 1;
52 var cycles = form.slice(46-1).trim();
53
54 return {
55 "name": name,
56 "addressing": addressing,
57 "size": size,
58 "opcode": opcode,
59 "cycles": cycles
60 };
61 });
62
63 return forms;
64 }
65
66 function PolyFill() {
67 if (!Array.prototype.findIndex) {
68 Array.prototype.findIndex = function(predicate) {
69 if (this === null) {
70 throw new TypeError('Array.prototype.findIndex called on null or undefined');
71 }
72 if (typeof predicate !== 'function') {
73 throw new TypeError('predicate must be a function');
74 }
75 var list = Object(this);
76 var length = list.length >>> 0;
77 var thisArg = arguments[1];
78 var value;
79
80 for (var i = 0; i < length; i++) {
81 value = list[i];
82 if (predicate.call(thisArg, value, i, list)) {
83 return i;
84 }
85 }
86 return -1;
87 };
88 }
89 }
This page took 0.072437 seconds and 4 git commands to generate.