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