W mode
[ScratchNES.git] / src / CPU / build-cpu.js
1 /* build-cpu.js
2 * this is the heart of the metaprogram
3 * at the entrypoint, the instruction set is complete and annotated,
4 * and the reference document is available.
5 * We can just sit back, relax, and put together parts :-)
6 */
7
8 var fs = require("fs");
9
10 // avoid excessive disk use
11 var instruction_cache = {};
12 var addressing_cache = {};
13
14 // read the table in
15 var table = JSON.parse(fs.readFileSync("bin/table.json").toString());
16
17 // jump table emission, etc.
18 var emission = [
19 "mapper read PC",
20 "set opcode to M"
21 ];
22
23 var sources = table.map(function(x, i) {
24 if(x) {
25 var instruction = [];
26
27 // add stub for addressing mode
28 // load if not already in-memory
29
30 if(!addressing_cache[x.addressing]) {
31 addressing_cache[x.addressing] =
32 fs.readFileSync("addressing/" + x.addressing)
33 .toString().trim().split("\n");
34
35 if(addressing_cache[x.addressing][0].trim().length == 0)
36 addressing_cache[x.addressing] = [];
37 }
38
39 instruction = instruction.concat(addressing_cache[x.addressing]);
40
41 // add stub for instruction
42 if(!instruction_cache[x.name]) {
43 instruction_cache[x.name] =
44 fs.readFileSync("instructions/" + x.name)
45 .toString().trim().split("\n");
46 }
47
48 // follow the flags
49 var flags = instruction_cache[x.name][0].replace(/ /g, '').split(',');
50 var negQ = false, zeroQ = false, mode = null, operand = null;
51
52 flags.forEach(function(flag) {
53 if(flag == "N") negQ = true;
54 else if(flag == "Z") zeroQ = true;
55 else if(["R", "RW", "IMPLIED", "RAW", "BRANCH"].indexOf(flag) > -1)
56 mode = flag;
57 else if (["A", "X", "Y", "tmp", "OP"].indexOf(flag) > -1)
58 operand = flag;
59 else
60 console.error("Unknown flag " + flag + " for instruction " + x.name);
61 });
62
63 var ins = instruction_cache[x.name].slice(1);
64
65 if(mode == "R") {
66 instruction.push("mapper read address");
67 instruction.push("set OP to M");
68 } else if(mode == "RW") {
69 if(x.addressing == "accumulator") {
70 operand = "A";
71 ins = ins.map(function(q) {
72 return q.replace(/OP/g, "A");
73 });
74 } else {
75 instruction.push("mapper read address");
76 instruction.push("set OP to M");
77 }
78
79 ins = ins.map(function(q) {
80 if(x.addressing != "accumulator") {
81 return q.replace(/set OP to/, "mapper write address");
82 } else {
83 return q;
84 }
85 });
86 } else if(mode == "W") {
87 ins = ins.map(function(q) {
88 return q.replace(/set OP to/, "mapper write address");
89 });
90 } else if(mode == "RAW") {
91 ins = ins.map(function(q) {
92 return q.replace(/OP/g, "address");
93 });
94 } else if(mode == "IMPLIED" || mode == "BRANCH") {
95
96 } else {
97 console.error("Unsupported mode " + mode);
98 }
99
100 // add the actual code of the instruction
101 instruction = instruction.concat(ins);
102
103 if(negQ) {
104 instruction.push("set flagN to <" + operand + " > 127>");
105 }
106
107 if(zeroQ) {
108 instruction.push("set flagZ to <" + operand + " = 0>");
109 }
110
111 instruction = instruction.concat([
112 'say "' + x.assembler + '" for 2 secs',
113 'change PC by ' + x.size
114 ]);
115
116 return instruction.join("\n");
117 } else {
118 return [
119 'say "Illegal Opcode ' + i + ' used, ignoring." for 2 secs',
120 'change PC by 1'
121 ].join("\n");
122 }
123 });
124
125 // dump out an 8 level deep BST
126 console.log(bst(sources, 0, 255).join('\n'));
127
128 function bst(sources, start, end) {
129 if(start == end)
130 return [sources[start]];
131
132 if(start + 1 == end)
133 return [
134 "if opcode = " + start + " then",
135 sources[start],
136 "else",
137 sources[end],
138 "end"
139 ];
140
141 var emission = ["if opcode < " + (start+end+1)/2 + " then"]
142 .concat(bst(sources, start, start + (end-start-1) / 2))
143 .concat(["else"])
144 .concat(bst(sources, start + (end-start+1) / 2, end))
145 .concat(["end"]);
146
147 return emission;
148 }
This page took 0.17379 seconds and 4 git commands to generate.