forked from vhelin/wla-dx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_gb.c
202 lines (183 loc) · 5.05 KB
/
decode_gb.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
case 0:
for ( ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == 0 && buffer[inz] == 0x0A) {
output_assembled_opcode(opt_tmp, "d%d ", opt_tmp->hex);
i = inz;
/* give a warning when assembling "JP (HL)"! */
/*
if (opt_tmp->hex == 0xE9 && strcmp(opt_tmp->op, "JP (HL)") == 0)
print_error("\"JP (HL)\" is semantically incorrect. Please use \"JP HL\" instead.\n", ERROR_WRN);
*/
return SUCCEEDED;
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
break;
case 4:
case 1:
for ( ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == 'x' || opt_tmp->op[x] == 's') {
y = i;
i = inz;
if (x > 0 && (opt_tmp->op[x-1] == '+' || opt_tmp->op[x-1] == '-'))
i--;
z = input_number();
inz = i;
i = y;
if (!(z == SUCCEEDED || z == INPUT_NUMBER_ADDRESS_LABEL || z == INPUT_NUMBER_STACK))
return FAILED;
if (z == SUCCEEDED) {
if ((opt_tmp->op[x] == 'x' && (d > 255 || d < -128)) ||
(opt_tmp->op[x] == 's' && (d > 127 || d < -128))) {
print_error("Out of 8-bit range.\n", ERROR_NUM);
return FAILED;
}
}
for (x++ ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == 0 && buffer[inz] == 0x0A) {
if (z == SUCCEEDED)
output_assembled_opcode(opt_tmp, "d%d d%d ", opt_tmp->hex, d);
else if (z == INPUT_NUMBER_ADDRESS_LABEL)
output_assembled_opcode(opt_tmp, "k%d d%d R%s ", active_file_info_last->line_current, opt_tmp->hex, label);
else {
output_assembled_opcode(opt_tmp, "d%d c%d ", opt_tmp->hex, latest_stack);
if (opt_tmp->type == 4) {
/* 4 -> let's configure the stack so that all label references inside are relative */
stacks_tmp->relative_references = 1;
}
}
i = inz;
return SUCCEEDED;
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
break;
case 2:
for ( ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == '?') {
y = i;
i = inz;
z = input_number();
inz = i;
i = y;
if (!(z == SUCCEEDED || z == INPUT_NUMBER_ADDRESS_LABEL || z == INPUT_NUMBER_STACK))
return FAILED;
if (z == SUCCEEDED && (d > 65535 || d < -32768)) {
print_error("Out of 16-bit range.\n", ERROR_NUM);
return FAILED;
}
for (x++ ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == 0 && buffer[inz] == 0x0A) {
if (z == SUCCEEDED)
output_assembled_opcode(opt_tmp, "d%d y%d ", opt_tmp->hex, d);
else if (z == INPUT_NUMBER_ADDRESS_LABEL)
output_assembled_opcode(opt_tmp, "k%d d%d r%s ", active_file_info_last->line_current, opt_tmp->hex, label);
else
output_assembled_opcode(opt_tmp, "d%d C%d ", opt_tmp->hex, latest_stack);
i = inz;
return SUCCEEDED;
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
break;
case 3:
for ( ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == 0 && buffer[inz] == 0x0A) {
output_assembled_opcode(opt_tmp, "y%d ", opt_tmp->hex);
i = inz;
return SUCCEEDED;
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
break;
case 8:
for ( ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == '*') {
y = i;
i = inz;
z = input_number();
inz = i;
i = y;
if (z != SUCCEEDED || d != opt_tmp->value)
break;
for (x++ ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == 0 && buffer[inz] == 0x0A) {
output_assembled_opcode(opt_tmp, "d%d ", opt_tmp->hex);
i = inz;
return SUCCEEDED;
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
break;
case 9:
for ( ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == '*') {
y = i;
i = inz;
z = input_number();
inz = i;
i = y;
if (z != SUCCEEDED || d != opt_tmp->value)
break;
for (x++ ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == 0 && buffer[inz] == 0x0A) {
output_assembled_opcode(opt_tmp, "y%d ", opt_tmp->hex);
i = inz;
return SUCCEEDED;
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
break;
case 100:
/* "RST *" that gets delayed to WLALINK */
for ( ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == '*') {
y = i;
i = inz;
z = input_number();
inz = i;
i = y;
if (!(z == INPUT_NUMBER_ADDRESS_LABEL || z == INPUT_NUMBER_STACK))
break;
for (x++ ; x < OP_SIZE_MAX; inz++, x++) {
if (opt_tmp->op[x] == 0 && buffer[inz] == 0x0A) {
output_assembled_opcode(opt_tmp, "k%d v1 ", active_file_info_last->line_current);
if (z == INPUT_NUMBER_ADDRESS_LABEL)
output_assembled_opcode(opt_tmp, "Q%s ", label);
else
output_assembled_opcode(opt_tmp, "c%d ", latest_stack);
/* reset to "no special case" */
output_assembled_opcode(opt_tmp, "v0 ");
i = inz;
return SUCCEEDED;
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
}
if (opt_tmp->op[x] != toupper((int)buffer[inz]))
break;
}
break;