#include "asmmon.h" void init_symbol() { uint16_t i = 0; for (; i < 0x1000; i++) { symbols[i] = 0; fixups[i] = 0; } } uint16_t sym_count = 0; uint16_t mksymbol(const char *name, uint64_t val, uint8_t def, uint8_t useid, uint16_t id, uint8_t dbg) { uint16_t i = 0; uint8_t flag = 0; for (; i < sym_count; i++) { if (useid) { flag = id == symbols[i]->id; } else { if (name[0] == symbols[i]->name[0]) { flag = !strcmp(name, symbols[i]->name); } } if (flag) { if (def) { if (symbols[i]->def) { if (dbg) { printf("mksymbol(): oof, you cannot redefine the symbol: %s\n", name); } defined = 1; } else { defined = 0; } symbols[i]->def = def; symbols[i]->val = val; symbols[i]->id = i; if (dbg) { printf("mksymbol(): def: %u, val: $%016"PRIX64", name: %s\n", def, val, name); printf("mksymbol(): i: $%X, id: $%04X\n", i, symbols[i]->id); } return symbols[i]->id; } else { return symbols[i]->id; } } } symbols[i] = malloc(sizeof(*symbols) + strlen(name)); symbols[i]->def = def; symbols[i]->val = val; strcpy(symbols[i]->name, name); symbols[i]->id = sym_count++; defined = 0; if (dbg) { printf("mksymbol(): def: %u, val: $%016"PRIX64", name: %s, id: $%04X\n", def, val, name, sym_count-1); } return sym_count-1; } uint64_t use_symbol(const char *name, uint16_t id, uint64_t val, uint8_t useid, uint8_t dbg) { uint16_t i; i = mksymbol(name, 0, 0, useid, id, dbg); uint8_t is_defined = (i != 0xFFFF); val++; if (dbg) { puts("use_symbol(): We also got here."); printf("use_symbol(): i: $%X\n", i); } if (symbols[i] != NULL) { if (symbols[i]->def) { return symbols[i]->val; } else { if (dbg) { printf("use_symbol(): "); printf("oof, symbol "); if (useid) { printf("id $%04X, ", id); } else { printf("%s, ", name); } puts("does not exist, yet."); } return val-1; } } return val-1; } uint8_t set_symval(const char *name, uint16_t id, uint64_t val, uint8_t useid, uint8_t dbg) { uint16_t i = mksymbol(name, 0, 0, useid, id, dbg); if (symbols[i] != NULL) { if (symbols[i]->def) { symbols[i]->val = val; return 1; } else { if (dbg) { printf("set_symval(): "); printf("oof, symbol "); if (useid) { printf("id $%04X, ", id); } else { printf("%s, ", name); } puts("does not exist, yet."); } return 0; } } } char *get_symname(uint16_t id, uint8_t dbg) { if (symbols[id]->def) { return symbols[id]->name; } else { if (dbg) { printf("get_symname(): oof, symbol id $%04X, has not been defined, yet.\n", id); } return NULL; } } uint16_t fixup_cnt = 0; uint16_t get_symid(const char *name, uint64_t val, uint16_t ln, uint8_t dbg) { uint16_t i = mksymbol(name, 0, 0, 0, 0, dbg); if (dbg) { printf("get_symid(): Symbol ID: $%X, i: $%X.\n", symbols[i]->id, i); } if (symbols[i]->def) { return symbols[i]->id; } else { if (dbg) { printf("get_symid(): oof, symbol %s, does not exist, yet.\n", name); } fixups[fixup_cnt] = malloc(sizeof(*fixups)); fixups[fixup_cnt]->adr = val; fixups[fixup_cnt]->ln = ln; fixups[fixup_cnt]->s = symbols[i]; fixup_cnt++; return 0xFFFF; } } uint16_t get_comment(const char *com, uint8_t dbg) { uint16_t i = 0; uint8_t iscom = 0; for (; i < comidx; i++) { if (comment[i] != NULL) { if (com[0] == comment[i][0]) { iscom = !strcmp(com, comment[i]); } } else { break; } if (iscom) { break; } } if (comment[i] == NULL) { if (dbg) { printf("get_comment(): oof, the index $%04X is NULL.\n", i); } return 0xFFFF; } if (i == comidx) { if (dbg) { printf("get_comment(): oof, the comment \"%s\", was not found in the comment table.\n", com); } return 0xFFFF; } if (dbg) { printf("get_comment(): Found comment \"%s\", in the table, at index $%04X.\n", com, i); } return i; } uint16_t get_string(const char *str, uint8_t dbg) { uint16_t i = 0; uint8_t isstr = 0; for (; i < stridx; i++) { if (string[i] != NULL) { if (str[0] == string[i][0]) { isstr = !strcmp(str, string[i]); } } else { break; } if (isstr) { break; } } if (string[i] == NULL) { if (dbg) { printf("get_string(): oof, the index $%04X is NULL.\n", i); } return 0xFFFF; } if (i == stridx) { if (dbg) { printf("get_string(): oof, the string \"%s\", was not found in the string table.\n", str); } return 0xFFFF; } if (dbg) { printf("get_string(): Found string \"%s\", in the table, at index $%04X.\n", str, i); } return i; } uint16_t reslv_fixups(uint8_t dbg) { uint16_t i = 0, j = 0; for (; fixups[j]; j++) { if (fixups[j]->s->def) { if (dbg) { printf("reslv_fixups(): Symbol ID: $%X, Symbol Name: %s.\n", fixups[j]->s->id, fixups[j]->s->name); } tokline[fixups[j]->ln].sym = fixups[j]->s->id; } else { if (dbg) { printf("reslv_fixups(): oof, undefined reference to '%s', at $%016"PRIX64".\n", fixups[j]->s->name, fixups[j]->adr); } i++; } } return i; } uint64_t update_addr(uint64_t address, uint8_t fixup, uint16_t l, uint8_t dbg) { uint64_t value = 0; uint16_t i = 0; uint16_t j = 0; uint16_t flags = 0; uint8_t opsize = 0; uint16_t symid = tokline[l].sym; uint16_t str = tokline[l].str; uint16_t com = tokline[l].com; uint8_t islabel = tokline[l].islabel; uint8_t issym = tokline[l].issym; uint8_t opbase = tokline[l].opbase; uint8_t aopbase = tokline[l].aopbase; uint8_t dir = tokline[l].dir; uint8_t am = tokline[l].am; uint8_t cm = tokline[l].cm; uint8_t rs = tokline[l].rs; uint8_t mne = tokline[l].mne; flags |= (dir != 0x00FF) << 0x00; flags |= (mne != 0x00FF) << 0x01; flags |= (rs != 0x00FF) << 0x02; flags |= (am != 0x00FF) << 0x03; flags |= (opbase != 0x00FF) << 0x04; flags |= (aopbase != 0x00FF) << 0x05; flags |= (symid != 0xFFFF) << 0x06; flags |= (fixup > 0x0000) << 0x06; flags |= (islabel ) << 0x07; flags |= (issym ) << 0x07; flags |= (am != 0x00FF) << 0x08; flags |= (cm != 0x00FF) << 0x09; flags |= (str != 0xFFFF) << 0x0A; if (dbg) { printf("update_addr(): "); printf("flags: $%04X\n", flags); } if (!flags || flags == 0x40) { if (dbg) { printf("update_addr(): "); puts("This line only contains a comment, so don't update the address."); } return address; } if (((flags & 0x53) == 0x42)) { if (fixup && symid == 0xFFFF && (opcodes[mne][IMPL] == 0xFF)) { value = address; } else if (opcodes[mne][IMPL] != 0xFF && symid == 0xFFFF) { value = 0; } else { value = use_symbol("", symid, address, 1, dbg); } } else { value = tokline[l].op; } if (flags & 0x220) { switch (cm) { case 0: value += tokline[l].aop; break; case 1: value -= tokline[l].aop; break; } } if (dbg) { printf("update_addr(): value: $%"PRIX64"\n", value); } switch (dir) { case DIR_ORG: address = value; if (dbg) { printf("update_addr(): "); printf("Set the Program Counter's Origin to $%"PRIX64".\n", address); } break; case DIR_BYTE: if (flags & 0x400) { for (; string[str][i] != '\0'; i++, j++, address++) { i += string[str][i] == '\\'; } j++; address++; if (dbg) { printf("update_addr(): "); printf("Increment Program Counter by $%04X", j); puts(", to make room for the string."); } } else { address += 1; } break; case DIR_WORD: address += 2; break; case DIR_DWORD: address += 4; break; case DIR_QWORD: address += 8; break; } if (flags & 0x01) { if (dbg) { printf("update_addr(): "); puts("This line contains a directive, so skip everything else."); } return address; } if ((flags & 0x15B) == 0x02 || (opcodes[mne][IMPL] != 0xFF && am == 0xFF && opbase == 0xFF && symid == 0xFFFF)) { tokline[l].am = IMPL; am = IMPL; if (dbg) { printf("update_addr(): "); puts("Addressing Mode has been set to Implied."); } } if (am == IMPL) { opsize = 0; } else if (am == IMM) { switch (rs) { case 3: address += 8; break; case 2: address += 4; break; case 1: address += 2; break; default: address += 1; break; } if (dbg) { if (!(flags & 0x04)) { rs = 0; } printf("update_addr(): "); printf("Increment Program Counter by $%02X", 1 << rs); puts(", to make room for the operand."); } } else if ((flags & 0x158) && (!(flags & 0x80))) { opsize = 0; opsize = (value <= 0x00000000000000FF) ? 1 : opsize; opsize = (value > 0x00000000000000FF) ? 2 : opsize; opsize = (value > 0x000000000000FFFF) ? 3 : opsize; opsize = (value > 0x0000000000FFFFFF) ? 4 : opsize; opsize = (value > 0x00000000FFFFFFFF) ? 5 : opsize; opsize = (value > 0x000000FFFFFFFFFF) ? 6 : opsize; opsize = (value > 0x0000FFFFFFFFFFFF) ? 7 : opsize; opsize = (value > 0x00FFFFFFFFFFFFFF) ? 8 : opsize; if (opsize) { switch (opsize-1) { case 0: case 2: case 5: case 3: if (!(flags & 0x100)) { am = ZM; tokline[l].am = am; if (dbg) { printf("update_addr(): "); puts("Addressing Mode has been set to Zero Matrix."); } } break; case 1: case 4: case 6: case 7: if (!(flags & 0x100)) { am = ABS; tokline[l].am = am; if (dbg) { printf("update_addr(): "); puts("Addressing Mode has been set to Absolute."); } } break; } address += opsize; if (dbg) { printf("update_addr(): "); printf("Increment Program Counter by $%02X", opsize); puts(", to make room for the address."); } } } if (dbg) { printf("update_addr(): "); printf("Address: $%"PRIX64"\n", address); } return address; } uint16_t find_line(uint16_t ln, uint8_t dbg) { uint16_t i = 0; for (; i < lineidx && tokline[i].linenum != ln; i++); if (tokline[i].linenum == ln) { if (dbg) { printf("find_line(): Found line number %u, at line index %X.\n", ln, i); } } if (dbg) { printf("find_line(): linenum: %u, i: %X\n", tokline[i].linenum, i); } return i; } uint64_t lex(char *str, uint64_t address, uint8_t dbg) { char sym[0x100]; uint16_t i = 0; uint16_t j = 0; uint16_t comid = 0; uint16_t strid = 0; uint16_t symid = 0; uint16_t line = 0; lex_type = 0xFF; uint8_t k = 0; uint8_t rs = 0; uint8_t isop = 0; int num = 0; int isch = 0; int16_t ln = -1; char lnum[6]; uint8_t islinenum; uint8_t space = 0; uint8_t tab = 0; uint8_t isstart = 1; uint8_t fall = 0; uint8_t done = 0; while (isdigit(str[i]) && !isspace(str[i])) { lnum[j++] = str[i++]; } islinenum = i; if (i) { lnum[j] = '\0'; ln = strtol(lnum, NULL, 10); j = 0; } else { ln = linenum; } for (; isspace(str[i]); i++); line = find_line(ln, dbg); if (line != lineidx) { address = tokline[line].addr; } tokline[line].dir = 0xFF; tokline[line].mne = 0xFF; tokline[line].rs = 0xFF; tokline[line].am = 0xFF; tokline[line].cm = 0xFF; tokline[line].opbase = 0xFF; tokline[line].aopbase = 0xFF; tokline[line].islabel = 0; tokline[line].issym = 0; tokline[line].str = 0xFFFF; tokline[line].com = 0xFFFF; tokline[line].sym = 0xFFFF; tokline[line].op = 0; tokline[line].aop = 0; tokline[line].addr = address; while (str[i] != '\0' && str[i] != '\n') { space = 0; tab = 0; while (isspace(str[i+j])) { tab += str[i+j] == '\t'; space += str[i+j] == ' '; j++; } j = 0; if (dbg) { printf("lex(): tab: %u, space: %u\n", tab, space); } if (isstart) { tokline[line].stab = tab; tokline[line].sspace = space; if (dbg) { printf("lex(): starting tabs: %u, starting spaces: %u\n", tokline[line].stab, tokline[line].sspace); } } if (isspace(str[i])) { while (isspace(str[i])) { i++; } } switch (str[i]) { case '.': i++; while (!isspace(str[i])) { lexeme[j++] = str[i++]; } lexeme[j] = '\0'; if (!isop) { for (k = 0; k < 5; k++) { if (tolower(lexeme[0]) == dir_t[k][0]) { if (!strcasecmp(lexeme, dir_t[k])) { lex_type = TOK_DIR; break; } } } tokline[line].dir = k; } else { lex_type = TOK_RS; switch (tolower(lexeme[j-1])) { case '2': case 'w': rs = 1; break; case '4': case 'd': rs = 2; break; case '8': case 'q': rs = 3; break; } address++; tokline[line].rs = rs; isop = 0; } break; case '\"': i++; while (str[i] != '\"') { lexeme[j++] = str[i++]; } strid = get_string(lexeme, dbg); if (strid == 0xFFFF) { if (line != lineidx && tokline[line].str != 0xFFFF) { strid = tokline[line].str; } else { strid = stridx; } string[strid] = malloc(j+1); memcpy(string[strid], lexeme, j+1); tokline[line].str = strid; if (dbg) { printf("lex(): str[0x%04X]: %s\n", strid, string[strid]); } stridx += (line == lineidx); } else { tokline[line].str = strid; if (dbg) { printf("lex(): str[0x%04X]: %s\n", strid, string[strid]); } } lex_type = TOK_STRING; break; case '#': lexeme[j] = '#'; lexeme[j+1] = '\0'; lexeme[j+2] = '\0'; tokline[line].am = IMM; lex_type = TOK_IMM; break; case '$': i++; while (isxdigit(str[i]) && (str[i] != '\0' && str[i] != '\n')) { lexeme[j++] = str[i++]; } lexeme[j] = '\0'; switch (lex_type) { case TOK_SYM: tokline[line].op = strtoull(lexeme, NULL, 16); mksymbol(sym, tokline[line].op, 1, 0, 0, dbg); tokline[line].sym = get_symid(sym, address, line, dbg); isfixup += (tokline[line].sym == 0xFFFF); if (dbg) { printf("lex(): isfixup: %u\n", isfixup); } tokline[line].opbase = BASE_HEX; break; case TOK_PLUS: case TOK_MINUS: tokline[line].aop = strtoull(lexeme, NULL, 16); tokline[line].aopbase = BASE_HEX; break; default: if (tokline[line].cm != 0xFF) { tokline[line].aop = strtoull(lexeme, NULL, 16); tokline[line].aopbase = BASE_HEX; } else { tokline[line].op = strtoull(lexeme, NULL, 16); tokline[line].opbase = BASE_HEX; } break; } lex_type = TOK_HEX; break; case '%': i++; while (isdigit(str[i]) && (str[i] != '\0' && str[i] != '\n')) { lexeme[j++] = str[i++]; } lexeme[j] = '\0'; switch (lex_type) { case TOK_SYM: tokline[line].op = strtoull(lexeme, NULL, 2); mksymbol(sym, tokline[line].op, 1, 0, 0, dbg); tokline[line].sym = get_symid(sym, address, line, dbg); isfixup += (tokline[line].sym == 0xFFFF); if (dbg) { printf("lex(): isfixup: %u\n", isfixup); } tokline[line].opbase = BASE_BIN; break; case TOK_PLUS: case TOK_MINUS: tokline[line].aop = strtoull(lexeme, NULL, 2); tokline[line].aopbase = BASE_BIN; break; default: if (tokline[lineidx].cm != 0xFF) { tokline[line].aop = strtoull(lexeme, NULL, 2); tokline[line].aopbase = BASE_BIN; } else { tokline[line].op = strtoull(lexeme, NULL, 2); tokline[line].opbase = BASE_BIN; } break; } lex_type = TOK_BIN; break; case '+': lexeme[j] = '+'; lexeme[j+1] = '\0'; tokline[line].cm = 0; lex_type = TOK_PLUS; break; case '-': lexeme[j] = '-'; lexeme[j+1] = '\0'; tokline[line].cm = 1; lex_type = TOK_MINUS; break; case '(': lexeme[j] = '('; lexeme[j+1] = '\0'; lexeme[j+2] = '\0'; tokline[line].am = IND; break; case ')': i++; if (str[i] == ',') { i++; while (isspace(str[i])) { lexeme[j++] = str[i++]; } if (tokline[line].am == IND && tolower(str[i]) == 'y') { lexeme[j++] = 'y'; tokline[line].am = INDY; } lexeme[j] = '\0'; } else { lexeme[j] = ')'; lexeme[j+1] = '\0'; lexeme[j+2] = '\0'; } break; case ',': i++; while (isspace(str[i])) { lexeme[j++] = str[i++]; } if (tokline[line].am == IND && tolower(str[i]) == 'x') { tokline[line].am = INDX; lexeme[j++] = 'x'; i++; } else { switch (tolower(str[i])) { case 'x': tokline[line].am = ZMX; lexeme[j++] = 'x'; break; case 'y': tokline[line].am = ZMY; lexeme[j++] = 'y'; break; } } lexeme[j] = '\0'; break; case ':': i++; lexeme[j] = ':'; lexeme[j+1] = '\0'; lex_type = TOK_LABEL; tokline[line].islabel = 1; mksymbol(sym, address, 1, 0, 0, dbg); if (isfixup) { isfixup = reslv_fixups(dbg); } tokline[line].sym = get_symid(sym, address, line, dbg); isfixup += (tokline[line].sym == 0xFFFF); if (dbg) { printf("lex(): isfixup: %u\n", isfixup); } break; case '=': i++; lexeme[j] = '='; lexeme[j+1] = 0; tokline[line].issym = 1; lex_type = TOK_SYM; break; case ';': i++; while (str[i] != '\0' && str[i] != '\n') { lexeme[j++] = str[i++]; } lexeme[j] = '\0'; comid = get_comment(lexeme, dbg); if (comid == 0xFFFF) { if (line != lineidx && tokline[line].com != 0xFFFF) { comid = tokline[line].com; } else { comid = comidx; } comment[comid] = malloc(j+1); memcpy(comment[comid], lexeme, j+1); tokline[line].com = comid; if (dbg) { printf("lex(): com[0x%04X]: %s\n", comid, comment[comid]); } comidx += (line == lineidx); } else { tokline[line].com = comid; if (dbg) { printf("lex(): com[0x%04X]: %s\n", comid, comment[comid]); } } lex_type = TOK_COMMENT; break; default: if (isalnum(str[i]) || str[i] == '_') { while (!isspace(str[i])) { switch (str[i]) { case ')': case ',': case '.': case '+': case '-': case ':': case '=': case ';': case '\0': case '\n': isch = 0; break; default: isch = 1; lexeme[j++] = str[i++]; break; } if (!isch) { break; } } lexeme[j] = '\0'; isch = 0; isop = 0; if (j == 3 && str[i] != ':') { for (k = 0; k < OPNUM; k++) { if (toupper(lexeme[0]) == mne[k][0]) { if (!strcasecmp(lexeme, mne[k])) { lex_type = TOK_OPCODE; isop = 1; tokline[line].mne = k; address++; break; } } } } if (!isop) { for (k = 0; lexeme[k] != '\0';) { switch (lexeme[k]) { case ')': case ',': case '.': case '+': case '-': case ':': case ';': case '=': case '\0': case '\n': fall = 1; break; default: fall = 0; break; } if (fall) { break; } if ((isalnum(lexeme[k]) || lexeme[k] == '_')) { if (!isch) { isch = isalpha(lexeme[k]); } num = isdigit(lexeme[k]) && !isch; k++; } else { break; } } if (lexeme[k] == '\0') { if (num) { switch (lex_type) { case TOK_SYM: tokline[line].op = strtoull(lexeme, NULL, 10); mksymbol(sym, tokline[line].op, 1, 0, 0, dbg); if (isfixup) { isfixup = reslv_fixups(dbg); } tokline[line].sym = get_symid(sym, address, line, dbg); isfixup += tokline[line].sym == 0xFFFF; if (dbg) { printf("lex(): isfixup: %u\n", isfixup); } tokline[line].opbase = BASE_DEC; break; case TOK_PLUS: case TOK_MINUS: tokline[line].aop = strtoull(lexeme, NULL, 10); tokline[line].aopbase = BASE_DEC; break; default: if (tokline[lineidx].cm != 0xFF) { tokline[line].aop = strtoull(lexeme, NULL, 10); tokline[line].aopbase = BASE_DEC; } else { tokline[line].op = strtoull(lexeme, NULL, 10); tokline[line].opbase = BASE_DEC; } break; } lex_type = TOK_DEC; } else if (isch && lex_type != TOK_HEX && lex_type != TOK_BIN) { lex_type = TOK_SYM; memcpy(sym, lexeme, j+1); uint8_t spaces = 0; for (; isspace(str[i+spaces]); spaces++); if (dbg) { printf("lex(): spaces: %u\n", spaces); } if (str[i+spaces] != ':' && str[i+spaces] != '=') { tokline[line].sym = get_symid(lexeme, address, line, dbg); isfixup += tokline[line].sym == 0xFFFF; if (dbg) { printf("lex(): isfixup: %u\n", isfixup); } } } } } } break; } if (dbg) { printf("lex(): lexeme: %s, lex_type: %s\n", lexeme, (lex_type != 0xFF) ? lex_tok[lex_type] : "TOK_NONE"); } isstart = 0; j = 0; if (lex_type == TOK_OPCODE && !isop) { j = 0; } else { if (lex_type == TOK_PLUS || lex_type == TOK_MINUS) { i++; } else { switch (str[i]) { case ')': case ',': case '.': case '+': case '-': case ':': case ';': case '=': case ' ': case '\t': case '\n': case '\0': break; default: i++; break; } } } if (lex_type == TOK_COMMENT) { if (!isstart) { tokline[line].etab = tab; tokline[line].espace = space; if (dbg) { printf("lex(): ending tabs: %u, ending spaces: %u\n", tokline[line].etab, tokline[line].espace); } } } if (lex_type != TOK_SYM) { memset(lexeme, 0, strlen(lexeme)+1); lex_type = 0xFF; } } if (i) { address = update_addr(address, isfixup, line, dbg); if (dbg) { printf("lex(): Next address: $%"PRIX64"\n", address); printf( "lex(): " "address: $%"PRIX64 ", dir: %u" ", mne: $%02X" ", rs: %u" ", am: %u" ", cm: %u" ", opbase: %u" ", com: $%04X" ", sym: $%04X" ", op: $%016"PRIX64 ", aop: $%016"PRIX64 ", ln: %u\n" , tokline[line].addr , tokline[line].dir , tokline[line].mne , tokline[line].rs , tokline[line].am , tokline[line].cm , tokline[line].opbase , tokline[line].com , tokline[line].sym , tokline[line].op , tokline[line].aop , line); } if (ln > linenum || islinenum) { tokline[line].linenum = ln; if (ln > linenum) { linenum+=(10+(ln & 10)); } } else if (!islinenum) { tokline[line].linenum = linenum; linenum += 10; } lineidx += (line == lineidx); } return address; }