-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMBF.hx
181 lines (155 loc) · 4.33 KB
/
MBF.hx
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package;
import rom.ROM;
import sys.io.File;
import sys.io.FileInput;
class MBF extends ROM {
var env:Map<String,Int>;
public function new(raw:String,romName:String) {
super(romName);
var out = Sys.stdout();
out.writeString('Creating ${romName}.\n');
out.writeString("Initializing environment.\n");
env = new Map<String,Int>();
out.writeString("Stripping comments.\n");
var wComments = raw.toLowerCase().split('\n');
var woComments:Array<String> = [];
for (line in wComments) woComments.push(line.split(";")[0]);
out.writeString("Splitting columns.\n");
var asm:Array<Array<String>> = [];
var ws = ~/[ \t]+/g;
for (line in woComments) asm.push(ws.split(line));
out.writeString("Beginning first parsing pass.\n");
out.writeString("Processing line ");
var lineNo = 1;
for (line in asm) {
processLine(line);
out.writeString(' ${lineNo++} ');
out.flush();
}
out.writeString(".\n");
out.writeString("Resetting rom.\n");
rom.fill(0,4096,0);
out.writeString("Beginning second parsing pass.\n");
out.writeString("Processing line ");
lineNo = 1;
for (line in asm) {
processLine(line);
out.writeString(' ${lineNo++} ');
out.flush();
}
out.writeString(".\n");
out.writeString("Finalizing rom.\n");
end();
out.close();
}
function processLine(line:Array<String>):Void {
if (line.length == 1 && line[0] == "") return;
if (line.length > 1 && line[0] == "org") {
addr = num(line[1]);
return;
}
if (line.length > 2 && line[1] == "equ") {
env[line[0]] = num(line[2]);
return;
}
if (line.length > 2 && line[1] == ".word") {
rom.set(addr,num(line[2]));
incAddr();
return;
}
if (line[0] != "") {
env[line[0]] = addr;
if (line.length > 1 && line[1] == "") return;
}
if (line.length < 2) return;
var operand = 0;
if (line.length > 2) operand = num(line[2]);
var opcode = line[1];
switch opcode {
case "add": add();
case "add11": add11();
case "atbp": atbp();
case "atfc": atfc();
case "atl": atl();
case "atpl": atpl();
case "atr": atr();
case "bdc": bdc();
case "cend": cend();
case "coma": coma();
case "dc": dc();
case "decb": decb();
case "exbla": exbla();
case "idiv": idiv();
case "incb": incb();
case "kta": kta();
case "rc": rc();
case "rot": rot();
case "rtn0": rtn0();
case "rtn1": rtn1();
case "sbm": sbm();
case "sc": sc();
case "ta0": ta0();
case "tabl": tabl();
case "tal": tal();
case "tam": tam();
case "tb": tb();
case "tc": tc();
case "tf1": tf1();
case "tf4": tf4();
case "tis": tis();
case "wr": wr();
case "ws": ws();
case "adx": adx(operand);
case "exc": exc(operand);
case "excd": excd(operand);
case "exci": exci(operand);
case "lax": lax(operand);
case "lb": lb(operand);
case "lda": lda(operand);
case "rm": rm(operand);
case "sm": sm(operand);
case "t": t(operand);
case "tm": tm(operand);
case "tmi": tmi(operand);
case "lbl": lbl(operand);
case "tl": tl(operand);
case "tml": tml(operand);
default: skip();
}
}
function num(str:String):Int {
var val = 0;
if (str.charAt(0) != "$") {
val = env[str];
if (val != null) return val;
return 0;
}
str = str.split("$")[1];
var val = 0;
for (i in 0...str.length) val = val*16 + ((str.charCodeAt(i) >= 97) ? str.charCodeAt(i)-97+10 : str.charCodeAt(i)-48);
return val;
}
public static function main() {
if (Sys.args().length != 2) {
var out = Sys.stdout();
out.writeString("needed arguments:\n input.asm output.bin #assemble .asm into .bin\n");
out.close();
Sys.exit(1);
}
try {
var asm = File.read(Sys.args()[0]).readAll().toString();
var mbf = new MBF(asm,Sys.args()[1]);
} catch (e:Dynamic) {
var err = Sys.stderr();
try {
var msg = cast(e, String);
err.writeString('Error:\n ${msg}\n');
} catch (e:Dynamic) {
err.writeString('Unknown error.\n');
}
err.close();
Sys.exit(1);
}
Sys.exit(0);
}
}