43 const char *
regs =
"ABCXYZIJ";
51 while (
line[cn] != 0 &&
line[cn] !=
'\n' &&
line[cn] !=
';') {
52 if (
line[cn] >=
'!' &&
line[cn] <=
'~') {
53 char current_char =
line[cn];
56 if (current_char >=
'a' && current_char <=
'z')
57 current_char =
toupper((
unsigned char)current_char);
60 oline[
n] = current_char;
73 int square_brackets = 0;
74 int first_sqbracket = 0, last_sqbracket = 0;
75 if (param[0] ==
'[') {
79 if (param[strlen(param) - 1] ==
']') {
81 param[strlen(param) - 1] = 0;
85 if (first_sqbracket == 1) {
87 if (last_sqbracket != 1) {
88 fprintf(stderr,
"Missing last square bracket\n");
92 if (last_sqbracket == 1) {
93 fprintf(stderr,
"Missing first square bracket\n");
99 if (param[0] ==
'0' && param[1] ==
'X') {
103 int digit_count = strlen(param);
106 for (digit_num = 0; digit_num < digit_count; digit_num++) {
109 char current_digit = param[digit_num];
110 if (current_digit >=
'0' && current_digit <=
'9')
111 digit_val = current_digit -
'0';
112 if (current_digit >=
'A' && current_digit <=
'F')
113 digit_val = current_digit -
'A' + 10;
115 if (current_digit ==
'+' && square_brackets == 1) {
120 if (digit_val == -1) {
121 fprintf(stderr,
"invalid literal\n");
130 if (
value <= 0x1f && square_brackets == 0)
133 *extra_word_needed = 1;
134 *extra_word_value =
value;
136 if (square_brackets == 1) {
145 if (param[0] >=
'0' && param[0] <=
'9') {
148 int digit_count = strlen(param);
151 for (digit_num = 0; digit_num < digit_count; digit_num++) {
154 char current_digit = param[digit_num];
155 if (current_digit >=
'0' && current_digit <=
'9')
156 digit_val = current_digit -
'0';
158 if (current_digit ==
'+' && square_brackets == 1) {
163 if (digit_val == -1) {
164 fprintf(stderr,
"invalid literal\n");
172 if (
value <= 0x1f && square_brackets == 0)
175 *extra_word_needed = 1;
176 *extra_word_value =
value;
178 if (square_brackets == 1) {
189 if (square_brackets == 1)
195 if (!strncmp(
"POP", param, 3))
197 if (!strncmp(
"PEEK", param, 4))
199 if (!strncmp(
"PUSH", param, 4))
201 if (!strncmp(
"SP", param, 2))
203 if (!strncmp(
"PC", param, 2))
205 if (!strncmp(
"O", param, 1))
209 *extra_word_needed = 1;
212 *extra_word_value = 0;
217 ut16 wordA = 0, wordB = 0;
218 int basic_opcode = 0;
219 int non_basic_opcode = 0;
220 char line[256] = { 0 }, *param;
227 if (strlen(
line) < 4)
233 if (!strncmp(
"SET",
line, 3))
235 else if (!strncmp(
"ADD",
line, 3))
237 else if (!strncmp(
"SUB",
line, 3))
239 else if (!strncmp(
"MUL",
line, 3))
241 else if (!strncmp(
"DIV",
line, 3))
243 else if (!strncmp(
"MOD",
line, 3))
245 else if (!strncmp(
"SHL",
line, 3))
247 else if (!strncmp(
"SHR",
line, 3))
249 else if (!strncmp(
"AND",
line, 3))
251 else if (!strncmp(
"BOR",
line, 3))
253 else if (!strncmp(
"XOR",
line, 3))
255 else if (!strncmp(
"IFE",
line, 3))
257 else if (!strncmp(
"IFN",
line, 3))
259 else if (!strncmp(
"IFG",
line, 3))
261 else if (!strncmp(
"IFB",
line, 3))
265 if (basic_opcode == 0) {
266 if (!strncmp(
"JSR",
line, 3)) {
267 non_basic_opcode = 0x1;
269 fprintf(stderr,
"Unknown instruction\n");
275 if (basic_opcode != 0) {
276 ut8 paramA = 0, paramB = 0;
280 while (cn < 256 && param[cn] !=
',' && param[cn] !=
'\n' && param[cn] != 0)
283 if (param[cn] ==
',') {
305 first_word = ((paramB & 0x3F) << 10) | ((paramA & 0x3F) << 4) | (basic_opcode & 0xF);
320 out[0] = (first_word >> 8) & 0xff;
321 out[1] = first_word & 0xff;
323 out[2] = (wordA >> 8) & 0xff;
324 out[3] = wordA & 0xff;
329 out[
off] = (wordB >> 8) & 0xff;
330 out[
off + 1] = wordB & 0xff;
335 fprintf(stderr,
"Missing comma\n");
341 if (non_basic_opcode == 0x1) {
343 ut16 first_word, wordX = 0;
346 first_word = ((
p & 0x3F) << 10) | ((non_basic_opcode & 0x3F) << 4) | (basic_opcode & 0xF);
355 out[0] = (first_word >> 8) & 0xff;
356 out[1] = first_word & 0xff;
358 out[2] = (wordX >> 8) & 0xff;
359 out[3] = wordX & 0xff;
int dcpu16_assemble(ut8 *out, const char *unoline)
static void clean_line(char *oline, const char *line)
static ut8 decode_parameter(char *param, int *extra_word_needed, ut16 *extra_word_value)
static ut8 get_register_id(char reg)
const lzma_allocator const uint8_t size_t uint8_t * out
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))