-
Notifications
You must be signed in to change notification settings - Fork 0
/
Directive.java
111 lines (71 loc) · 2.52 KB
/
Directive.java
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
abstract class Directive {
abstract boolean execute(String operands, Line line) throws Exception;
}
class DB extends Directive {
String hexify(String s) throws Exception {
int temp;
try {
if(s.charAt(0) == '"')
return s.substring(1, s.length() - 1);
else if(s.charAt(s.length() - 1) == 'H')
temp = Integer.parseInt(s.substring(0, s.length() - 1), 16);
else if(s.charAt(s.length() - 1) == 'B')
temp = Integer.parseInt(s.substring(0, s.length() - 1), 2);
else
temp = Integer.parseInt(s.replace("D", ""));
//If number is negative, add shift value to obtain two's complement.
if(temp < 0)
temp += temp > -255 ? 256 : 65536;
s = Integer.toHexString(temp);
if(s.length() > 2)
throw new Exception("Directive DB expects 8-bit data or String.");
return String.format("%2S", s).replace(' ', '0');
}
catch(NumberFormatException e) {
throw new Exception("Invalid number for given base.");
}
}
boolean execute(String operands, Line line) throws Exception {
int size;
String temp = "";
for(String data : operands.replace("#", "").split(","))
temp += this.hexify(data);
size = temp.length() / 2;
if(size > 16)
throw new Exception(String.format("DB can handle only 16 bytes of data. Given %d bytes.", size));
line.m = new Mnemonics("\"RESERVED\"");
line.m.size = size;
line.m.opcode = temp;
return true;
}
}
class ORG extends Directive {
boolean execute(String operands, Line line) throws Exception {
line.address = operands.replace("H", "");
return true;
}
}
class BIT extends Directive {
boolean execute(String operands, Line line) throws Exception {
String[] tokens = operands.split(" ");
if(Rift.opcodes.containsKey(tokens[0]) || Rift.symbols.containsKey(tokens[0]) || Rift.directives.containsKey(tokens[0]))
throw new Exception("Symbol already defined or is a Mnemonic/Directive");
Rift.symbols.put(tokens[0], tokens[1]);
return true;
}
}
class EQU extends Directive {
boolean execute(String operands, Line line) throws Exception {
String[] tokens = operands.split(" ");
if(Rift.opcodes.containsKey(tokens[0]) || Rift.symbols.containsKey(tokens[0]) || Rift.directives.containsKey(tokens[0]))
throw new Exception("Symbol already defined or is a Mnemonic/Directive");
Rift.symbols.put(tokens[0], tokens[1]);
return true;
}
}
class END extends Directive {
boolean execute(String operands, Line line) throws Exception {
line.parsedLine = "";
return false;
}
}