summaryrefslogtreecommitdiff
path: root/asmmon.c
blob: 75e331b349f343598238eca4904b7476513d97f0 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#include "asmmon.h"
#include "lexer.h"

uint16_t linenum = 10;
uint16_t lineidx = 0;
uint16_t stridx = 0;
uint16_t comidx = 0;
uint16_t inc_file = 0; /* Number of included files. */
uint16_t inc_count = 0;

uint8_t defined = 0;

uint8_t isfixup = 0;

char lexeme[MAX_TOK];
char *string[MAX_TOK];
char *comment[MAX_TOK];
uint16_t incl[MAX_TOK];
line *lines;
line *last_line;
symbol *symbols = 0;
symbol *last_sym = 0;
fixup *fixups = 0;
fixup *last_fix = 0;

strln *first_strln = 0;
strln *last_strln = 0;

static char tstr[2048];

void viewmem(uint64_t address) {
	putchar('\n');
	printf("\t\t\t");
	for (uint8_t ind = 0; ind < 0x10; ind++) {
		printf("%02X", ind);
		if (ind < 0x0F) {
			putchar(' ');
		}
	}
	puts("\n");
	for (uint8_t hi = 0; hi < 0x10; hi++) {
		printf("$%016"PRIX64":\t", (address)+(hi << 4));
		for (uint8_t lo = 0; lo < 0x10; lo++) {
			printf("%02X", addr[(address)+lo+(hi << 4)]);
			if (lo < 0x0F) {
				putchar(' ');
			}
		}
		putchar('\n');
	}
}

void usage() {
	puts("SuBAsm for CISC-0.2");
	puts("Commands:");
	puts("\tasm, a\t\t\tAssembles the currently written program.");
	puts("\tinst, i [inst]\t\tGet a descriptions of that instruction.");
	puts("\t\t\t\tIf no argument is specified, or the");
	puts("\t\t\t\targument specified is \"all\", list all");
	puts("\t\t\t\tinstructions, along with a description");
	puts("\t\t\t\tfor each of them.");
	puts("\tlist, l [$%][start][-][$%][end] [d[ebug], l[inenum], a[ddress]]]");
	puts("\t\t\t\tLists the currently written program.");
	puts("\tviewmem, v\t\tGet the contents of memory");
	puts("\t\t\t\t(Displays 256 bytes of memory");
	puts("\t\t\t\t starting from where the program counter");
	puts("\t\t\t\t currently is).");
	puts("\tquit, q\t\t\tQuit the emulator.");
	puts("\thelp, h\t\t\tDisplays this mesage.");
}

void instinfo(const char *instr) {
	for(int i = 0; i < OPNUM; i++) {
		if (instr[0] == mne[i][0]) {
			if (!strcasecmp(instr, mne[i])) {
				printf("%s\t%s\n", mne[i], instdesc[i]);
				break;
			}
		} else if (instr[0] == 'a') {
			if (!strcasecmp(instr, "all")) {
				printf("%s\t%s\n", mne[i], instdesc[i]);
			}
		}
	}
}

char *showbits(uint64_t value, uint8_t bitnum, uint8_t dbg) {
	if (dbg) {
		printf("showbits(): ");
	}
	char *bits = malloc((sizeof(uint64_t) << 3)+1);
	char bit = 0;
	uint8_t i;
	uint8_t j = 0;
	if (bitnum > 63) {
		bitnum = (sizeof(uint64_t) << 3) - 1;
	}
	if (!bitnum) {
		i = (sizeof(uint64_t) << 3) - 1;
	} else {
		i = bitnum;
	}
	for (; (value > 0 && !bitnum) || (j <= bitnum && bitnum); j++) {
		if (value > 0 && !bitnum) {
			bits[j] = (value & 1) ? '1' : '0';
			value>>=1;
		} else {
			bits[j] = (value & ((uint64_t)1 << i)) ? '1' : '0';
			i--;
		}
	}
	bits[j] = '\0';
	if (dbg) {
		printf("bits: %s\n", bits);
	}
	return bits;

}

static inline uint8_t isopdone(token *t) {
	switch (t->id) {
		case TOK_OF  :
		case TOK_HEX :
		case TOK_BIN :
		case TOK_DEC :
		case TOK_CHAR:
		case TOK_EXPR: return 0;
		default      : return 1;
	}
}

void list(uint16_t start, uint16_t end, uint8_t all, uint8_t ln, uint8_t addr, uint64_t address, uint8_t dbg) {
	line *s = (!all) ? find_line(start, dbg) : lines;
	line *e = (!all) ? find_line(  end, dbg) :  NULL;
	uint8_t j = 0;
	uint8_t flags = 0;
	uint8_t isstr;
	uint8_t iscom;
	uint8_t iscm = 0;
	uint8_t bitnum;
	uint8_t opsize = 0;
	uint8_t spaces;
	uint8_t tabs;

	char mne_lower[4];
	char ch[6];
	do {
		address = s->addr;
		token *t = s->tok;
		uint8_t am = 0xFF;
		uint8_t rs = 0xFF;
		uint8_t am_done = 1;
		uint8_t op_done = 1;
		uint16_t bline = s->bline;
		const char *inst_name;
		for (; bline; bline--) {
			putchar('\n');
		}
		if (dbg) {
			printf("list(): ");
		}
		if (ln) {
			printf("%5u\t\t", s->linenum);
		} else if (addr) {
			printf("$%"PRIX64":\t\t", s->addr);
		}
		while (t) {
			if (am != 0xFF && op_done && t->id != TOK_RS) {
				switch (am) {
					case IMM : putchar('#'); am_done = 1; break;
					case IND :
					case INDX:
					case INDY: putchar('('); am_done = 0; break;
					case ZMY :
					case ZMX : am_done = 0; break;
				}
				am = (am_done) ? 0xFF : am;
			}
			spaces = t->space;
			tabs = t->tab;
			while (spaces || tabs) {
				if (spaces) {
					putchar(' ');
					spaces--;
				}
				if (tabs) {
					putchar('\t');
					tabs--;
				}
			}
			switch (t->id) {
				case TOK_DIR   : printf(".%s", dir_t[t->type]); break;
				case TOK_RS    : printf("%s",   rs_t[t->type]); break;
				case TOK_EXTOP :
				case TOK_OPCODE:
					switch (t->id) {
						case TOK_EXTOP : inst_name = ext_mne[t->byte]; break;
						case TOK_OPCODE: inst_name = /**/mne[t->byte]; break;
					}
					for (; j < 3; j++) {
						mne_lower[j] = tolower(inst_name[j]);
					}
					mne_lower[j] = '\0';
					j = 0;
					printf("%s", mne_lower);
					am = t->type;
					break;
				case TOK_OF:
					switch (t->type) {
						case 1: printf("sp"); break;
						case 2: printf("pc"); break;
					}
					break;
				case TOK_SYM:
				case TOK_LABEL:
				case TOK_STRUCT:
				case TOK_UNION:
				case TOK_MEMBER:
					if (t->type == 1) {
						putchar('@');
					}
					printf("%s", (t->sym) ? t->sym->name : "unknown");
					if (t->sym && t->sym->isstruct && t->next && t->next->id == TOK_SYM) {
						putchar('.');
					}
					if (t->id == TOK_LABEL) {
						putchar(':');
					}
					break;
				case TOK_HEX:
					if (t->id == TOK_HEX) {
						printf("$%0*"PRIX64, t->digits, t->qword);
					} else if (t->id == TOK_DEC) {
				case TOK_DEC:	printf( "%0*"PRIu64, t->digits, t->qword);
					} else if (t->id == TOK_BIN) {
				case TOK_BIN:	if (t->digits) {
							bitnum = t->digits;
						} else {
							opsize = 1;
							opsize = (t->qword > 0x000000FF) ? 2 : opsize;
							opsize = (t->qword > 0x0000FFFF) ? 3 : opsize;
							opsize = (t->qword > 0xFFFFFFFF) ? 4 : opsize;
							if (opsize) {
								bitnum = bitsize[opsize-1];
							}
						}
						printf("%%%s", showbits(t->qword, bitnum, dbg));
						bitnum = 0;
						opsize = 0;
					} else if (t->id == TOK_STRING) {
			        case TOK_STRING:
						printf("\"%s\"", (t->str) ? t->str : "");
					} else if (t->id == TOK_CHAR) {
				case TOK_CHAR:	j = 0;
						switch (t->byte) {
							default  : ch[j++] = t->byte;              break;
							case '\n': ch[j++] = '\\'; ch[j++] = 'n' ; break;
							case '\r': ch[j++] = '\\'; ch[j++] = 'r' ; break;
							case '\t': ch[j++] = '\\'; ch[j++] = 't' ; break;
							case '\b': ch[j++] = '\\'; ch[j++] = 'b' ; break;
							case '\\': ch[j++] = '\\'; ch[j++] = '\\'; break;
							case '\0': ch[j++] = '\\'; ch[j++] = '0'; break;
							case '\'': ch[j++] = '\\'; ch[j++] = '\''; break;
							case '\"': ch[j++] = '\\'; ch[j++] = '\"'; break;
						}
						ch[j] = '\0';
						j = 0;
						printf("\'%s\'", ch);
					}
					if (t->next) {
						switch (t->next->id) {
							case TOK_STRING:
							case TOK_HEX   :
							case TOK_BIN   :
							case TOK_DEC   :
							case TOK_CHAR  : putchar(','); break;
						}
					}
					break;
				case TOK_COMMENT: printf(";%s", (t->str) ? t->str : ""); break;
				case TOK_EXPR:
					switch (t->type) {
						case EXPR_PLUS : putchar('+'); break;
						case EXPR_MINUS: putchar('-'); break;
						case EXPR_LSHFT: putchar('<'); /* Falls through. */
						case EXPR_HIGH : putchar('<'); break;
						case EXPR_RSHFT: putchar('>'); /* Falls through. */
						case EXPR_LOW  : putchar('>'); break;
						case EXPR_OR   : putchar('|'); break;
					}
					break;

			}
			if (t->subspace || t->subtab) {
				spaces = t->subspace;
				tabs = t->subtab;
				while (spaces || tabs) {
					if (spaces) {
						putchar(' ');
						spaces--;
					}
					if (tabs) {
						putchar('\t');
						tabs--;
					}
				}
			}
			if (t->next && !isopdone(t)) {
				op_done = isopdone(t->next);
			}
			if (am != 0xFF && op_done && t->id != TOK_RS) {
				switch (am) {
					case EIND: putchar('('); am_done = 0; break;
					case BREG: putchar('b'); am_done = 1; break;
				}
			}
			if (am != 0xFF && !am_done && op_done) {
				switch (am) {
					case INDX:
					case ZMX :
						printf(", x");
						if (am == ZMX) {
							break;
						}
						/* Falls Through. */
					case EIND:
						if (am == EIND) {
							putchar('e');
						}
						/* Falls Through. */
					case INDY:
					case IND :
						putchar(')');
						if (am == IND || am == EIND) {
							break;
						}
						/* Falls Through. */
					case ZMY : printf(", y"); break;
				}
				am = 0xFF;
				am_done = 1;
			}
			if (t == s->tok && t->id == TOK_SYM) {
				putchar('=');
			}
			t = t->next;
		}
		puts("");
		s = s->next;
	} while (s != e && s);
}

strln *make_strln(char *str, int blanks) {
	strln *new_strln = malloc(sizeof(strln));

	if (new_strln == NULL) {
		return NULL;
	}

	new_strln->next = NULL;
	new_strln->prev = NULL;
	new_strln->str = str;
	new_strln->blanks = blanks;

	if (last_strln == NULL) {
		first_strln = new_strln;
	} else {
		new_strln->prev = last_strln;
		last_strln->next = new_strln;
	}

	last_strln = new_strln;

	return new_strln;
}

int asmmon(const char *fn) {
	FILE *fp = NULL;
	FILE *fp2 = NULL;
	char *path = malloc(0x400);
	if (strcasecmp(fn, "stdin")) {
		uint16_t i = 0;
		uint8_t dir = 0;
		for (; fn[i] != '\0'; i++) dir = (fn[i] == '/') ? i : dir;
		if (dir) {
			memcpy(path, fn, dir);
			path[dir] = '\0';
		} else {
			path[0] = '.';
			path[1] = '\0';

		}
		fp = fopen(fn, "r");
		if (fp == NULL) {
			free(path);
			return 2;
		}
	}
	uint8_t done = 0;
	uint8_t use_lexer = 1;
	uint64_t address = 0;
	bytecount bc;
	uint64_t progsize = 0;
	uint64_t datasize = 0;
	uint8_t dbg = 0;
	uint8_t isinclude = 0;
	uint16_t tmp_lineidx = 0;
	uint16_t bline = 0;
	while (!done) {
		char *cmd;
		char *arg = malloc(sizeof(char *)*128);
		char *tmp = malloc(sizeof(char *)*128);
		char lex_line[0x1000];
		uint16_t size = 0;
		uint8_t cmds = 0;
		uint8_t is_valid = 0;
		/* Is single character command. */
		uint8_t isshcmd = 0;
		if (!isinclude) {
			fgets(lex_line, sizeof(lex_line), (!strcasecmp(fn, "stdin")) ? stdin : fp);
		} else {
			if (fp2 != NULL) {
				if (fgets(lex_line, sizeof(lex_line), fp2) == NULL && feof(fp2)) {
					lex_line[0] = 'a' ;
					lex_line[1] = '\n';
					lex_line[2] = '\0';
				}
			}
		}
		size = strlen(lex_line)+1;
		cmd = malloc(size);
		memcpy(cmd, lex_line, size);
		cmd = strtok_r(cmd, " \t\n", &tmp);
		if (cmd != NULL) {
			int cmd_len = strlen(cmd);
			isshcmd = (cmd_len > 0 && cmd_len <= 3 && (cmd[1] == '\0' || cmd[1] == ' '));
			if (tmp) {
				int stop = 0;
				for (int i = 0; !(isdelm(tmp[i], 0) & 1) && !stop; i++) {
					switch (get_ptok(tmp[i], 0)) {
						case PTOK_NUMBER :
						case PTOK_ALPHA  :
						case PTOK_DOLLAR :
						case PTOK_PERCENT:
						case PTOK_DQUOTE :
						case PTOK_SQUOTE :
						case PTOK_B      :
						case PTOK_E      :
						case PTOK_X      :
						case PTOK_Y      :
						case PTOK_S      :
						case PTOK_P      :
						case PTOK_A      :
						case PTOK_C      :
						case PTOK_D      :
						case PTOK_F      :
						case PTOK_R      :
						case PTOK_MINUS  :
						case PTOK_PLUS   : is_valid = 1;           break;
						default          : is_valid = 0; stop = 1; break;
					}
				}
				if (strlen(tmp) <= 1) {
					is_valid = 1;
				}
			}
			if (is_valid) {
				switch (cmd[0]) {
					case 'q': cmds = (isshcmd || !strcasecmp(cmd, "quit"   )) << 0; break;
					case 'v': cmds = (isshcmd || !strcasecmp(cmd, "viewmem")) << 1; break;
					case 'l': cmds = (isshcmd || !strcasecmp(cmd, "list"   )) << 2; break;
					case 'a': cmds = (isshcmd || !strcasecmp(cmd, "asm"    )) << 3; break;
					case 'h': cmds = (isshcmd || !strcasecmp(cmd, "help"   )) << 4; break;
					case 'i': cmds = (isshcmd || !strcasecmp(cmd, "inst"   )) << 5; break;
					case 'd': cmds = (isshcmd || !strcasecmp(cmd, "done"   )) << 6; break;
					case 's': cmds = (isshcmd || !strcasecmp(cmd, "sett"    )) << 7; break;
					case ' ':
					case '\t':
					default:
						is_valid = 0;
						cmds = 0xFF;
						break;
				}
				if (!cmds) {
					is_valid = 0;
				}
			}
			switch (cmds) {
				case 0x01:
					free(path);
					if (fp != NULL) {
						fclose(fp);
					}
					if (fp2 != NULL) {
						fclose(fp2);
					}
					cleanup();
					return 2;
				case 0x02:
					viewmem(address);
					break;
				case 0x04:
					if (tmp != NULL) {
						uint16_t i = 0;
						uint16_t j = 0;

						uint16_t start = 0;
						uint16_t end = 0;
						uint16_t value = 0;

						uint8_t base = 0xFF;

						uint8_t isflag = 0;
						uint8_t isstart = 1;

						uint8_t isdebug	  = 0;
						uint8_t islinenum = 0;
						uint8_t isaddr    = 0;

						while (tmp[i] != '\0') {
							if (isspace(tmp[i])) {
								for (; isspace(tmp[i]); i++);
							}
							switch (tmp[i]) {
								case '$': base = 16; i++; break;
								case '%': base =  2; i++; break;
								default:
									j = i;
									for (; isdigit(tmp[j]); j++, isflag++);
									base = (isflag) ? 10 : 0xFF;
									isflag = 0;
									j = 0;
									break;
							}
							for (; !isspace(tmp[i]) && tmp[i] != '-' && tmp[i] != '\0' && tmp[i] != '\n'; arg[j++] = tmp[i++]);
							arg[j] = '\0';
							j = 0;
							if (base != 0xFF) {
								value = strtol(arg, NULL, base);
								base = 0xFF;
								(isstart) ? (start = value) : (end = value);
								if (isstart) {
									isstart = (tmp[i] != '-');
								}
							} else {
								for (; isalpha(arg[j]); j++, isflag++);
								j = 0;
								if (isflag) {
									isdebug   = (arg[j] == 'd' || !strcasecmp(arg, "debug"  ));
									islinenum = (arg[j] == 'l' || !strcasecmp(arg, "linenum"));
									if (!islinenum) {
										isaddr = (arg[j] == 'a' || !strcasecmp(arg, "address"));
									}
								}
								isflag = 0;
							}
							i++;
						}
						list(start, end, isstart, islinenum, isaddr, 0, isdebug);
					} else {
						list(0, 0, 1, 0, 0, 0, 0);
					}
					break;
				case 0x08:
					if (!inc_count) {
						printf("Assembling %s\n", (strcasecmp(fn, "stdin")) ? fn : "typed in program.");
					}
					if (!inc_file) {
						puts("Now assembling.");
						address = 0;
						bc.progsize = 0;
						bc.datasize = 0;
						assemble(lines, &bc, dbg);
						progsize = bc.progsize;
						datasize = bc.datasize;
					}
					isinclude = (inc_file != 0);
					if (inc_file) {
						size = strlen(path)+strlen(string[incl[inc_count]])+1;
						char *fn2 = malloc(size+1);
						sprintf(fn2, "%s/%s", path, string[incl[inc_count]]);
						isinclude = (inc_file != 0);
						inc_file--;
						if (inc_file && fp2 != NULL) {
							fclose(fp2);
						}
						fp2 = fopen(fn2, "r");
						if (fp2 == NULL) {
							fclose(fp);
							cleanup();
							return 2;
						}
						printf("Including %s\n", string[incl[inc_count++]]);
					}
					if (!isinclude) {
						puts("Finished assembling.");
						printf("Total Assembled Program Size: %"PRIu64"/$%"PRIX64" bytes.\n", progsize, progsize);
						printf("Total Assembled Data Size: %"PRIu64"/$%"PRIX64" bytes.\n", datasize, datasize);
					}
					break;
				case 0x10:
					usage();
					break;
				case 0x20:
					if (tmp != NULL) {
						instinfo(tmp);
					} else {
						instinfo("all");
					}
					break;
				case 0x40:
					done = 1;
					break;
				case 0x80:
					if (tmp != NULL) {
						uint16_t i = 0;
						uint16_t j = 0;
						uint8_t isdebug = 0;
						is_valid = 0;
						while (tmp[i] != '\0') {
							if (isspace(tmp[i])) {
								for (; isspace(tmp[i]); i++);
							}
							for (; !(isdelm(tmp[i], 0) & 0x11); arg[j++] = tmp[i++]);
							arg[j] = '\0';
							j = 0;
							isdebug = (arg[j] == 'd' || !strcasecmp(arg, "debug"));
							if (isdebug) {
								is_valid = 1;
								dbg = !dbg;
								if (dbg) {
									puts("Debug mode has been enabled.");
								} else {
									puts("Debug mode has been disabled.");
								}
							}
							i++;
						}
					} else {
						is_valid = 0;
					}
				case 0xFF:
				default  : break;
			}
			if (!is_valid) {
				size_t len = strlen(lex_line);
				char *tmp = malloc(len+1);
				memcpy(tmp, lex_line, len);
				tmp[len] = '\0';
				strln *dummy = make_strln(tmp, bline);
				address = lex(lex_line, address, bline, dbg);
				bline = 0;
			}
		} else if (lex_line[0] == '\n') {
			bline++;
		}
	}
	free(path);

	if (fp != NULL) {
		fclose(fp);
	}

	if (fp2 != NULL) {
		fclose(fp2);
	}
	cleanup();
	return 0;
}