From 15c212f4b606b4dce27eb00324d9559b1db31dc8 Mon Sep 17 00:00:00 2001 From: David Banks Date: Tue, 12 Nov 2019 11:50:49 +0000 Subject: [PATCH] 6809: eliminate usage of log0 Change-Id: Iddf14f72e9e848703aba07208708b55968d100af --- firmware/dis6809.c | 540 +++++++++++++++++++++++--------------------- firmware/regs6809.c | 43 ++-- firmware/status.c | 7 + firmware/status.h | 1 + 4 files changed, 314 insertions(+), 277 deletions(-) diff --git a/firmware/dis6809.c b/firmware/dis6809.c index d9bc2ae..31725ce 100644 --- a/firmware/dis6809.c +++ b/firmware/dis6809.c @@ -18,24 +18,6 @@ #include #include "AtomBusMon.h" -unsigned char get_memb(unsigned int addr) { - loadAddr(addr); - return readMemByteInc(); -} - -#include - -typedef unsigned char tt_u8; -typedef signed char tt_s8; -typedef unsigned short tt_u16; -typedef signed short tt_s16; - - -unsigned int get_memw(unsigned int addr) { - loadAddr(addr); - return (readMemByteInc() << 8) + readMemByteInc(); -} - enum opcodes { OP_UU , OP_XX , @@ -312,15 +294,15 @@ TSTB"; // The first byte is the opcode index // The second byte is -// modes: -// 1 immediate -// 2 direct -// 3 indexed -// 4 extended -// 5 inherent -// 6 relative +// modes: +// 1 immediate +// 2 direct +// 3 indexed +// 4 extended +// 5 inherent +// 6 relative -static const unsigned char map0[] PROGMEM = { +static const uint8_t map0[] PROGMEM = { OP_NEG , 0x22, OP_XX , 0x22, OP_XX , 0x12, @@ -579,142 +561,110 @@ static const unsigned char map0[] PROGMEM = { OP_STU , 0x34, }; -static const unsigned char map1[] PROGMEM = { - 33, OP_LBRN, 0x46, - 34, OP_LBHI, 0x46, - 35, OP_LBLS, 0x46, - 36, OP_LBCC, 0x46, - 37, OP_LBLO, 0x46, - 38, OP_LBNE, 0x46, - 39, OP_LBEQ, 0x46, - 40, OP_LBVC, 0x46, - 41, OP_LBVS, 0x46, - 42, OP_LBPL, 0x46, - 43, OP_LBMI, 0x46, - 44, OP_LBGE, 0x46, - 45, OP_LBLT, 0x46, - 46, OP_LBGT, 0x46, - 47, OP_LBLE, 0x46, - 63, OP_SWI2, 0x25, - 131, OP_CMPD, 0x41, - 140, OP_CMPY, 0x41, - 142, OP_LDY , 0x41, - 147, OP_CMPD, 0x32, - 156, OP_CMPY, 0x32, - 158, OP_LDY , 0x32, - 159, OP_STY , 0x32, - 163, OP_CMPD, 0x33, - 172, OP_CMPY, 0x33, - 174, OP_LDY , 0x33, - 175, OP_STY , 0x33, - 179, OP_CMPD, 0x44, - 188, OP_CMPY, 0x44, - 190, OP_LDY , 0x44, - 191, OP_STY , 0x44, - 206, OP_LDS , 0x41, - 222, OP_LDS , 0x32, - 223, OP_STS , 0x32, - 238, OP_LDS , 0x33, - 239, OP_STS , 0x33, - 254, OP_LDS , 0x44, - 255, OP_STS , 0x44, +static const uint8_t map1[] PROGMEM = { + 33, OP_LBRN, 0x46, + 34, OP_LBHI, 0x46, + 35, OP_LBLS, 0x46, + 36, OP_LBCC, 0x46, + 37, OP_LBLO, 0x46, + 38, OP_LBNE, 0x46, + 39, OP_LBEQ, 0x46, + 40, OP_LBVC, 0x46, + 41, OP_LBVS, 0x46, + 42, OP_LBPL, 0x46, + 43, OP_LBMI, 0x46, + 44, OP_LBGE, 0x46, + 45, OP_LBLT, 0x46, + 46, OP_LBGT, 0x46, + 47, OP_LBLE, 0x46, + 63, OP_SWI2, 0x25, + 131, OP_CMPD, 0x41, + 140, OP_CMPY, 0x41, + 142, OP_LDY , 0x41, + 147, OP_CMPD, 0x32, + 156, OP_CMPY, 0x32, + 158, OP_LDY , 0x32, + 159, OP_STY , 0x32, + 163, OP_CMPD, 0x33, + 172, OP_CMPY, 0x33, + 174, OP_LDY , 0x33, + 175, OP_STY , 0x33, + 179, OP_CMPD, 0x44, + 188, OP_CMPY, 0x44, + 190, OP_LDY , 0x44, + 191, OP_STY , 0x44, + 206, OP_LDS , 0x41, + 222, OP_LDS , 0x32, + 223, OP_STS , 0x32, + 238, OP_LDS , 0x33, + 239, OP_STS , 0x33, + 254, OP_LDS , 0x44, + 255, OP_STS , 0x44, }; -static const unsigned char map2[] PROGMEM = { - 63, OP_SWI3, 0x25, - 131, OP_CMPU, 0x41, - 140, OP_CMPS, 0x41, - 147, OP_CMPU, 0x32, - 156, OP_CMPS, 0x32, - 163, OP_CMPU, 0x33, - 172, OP_CMPS, 0x33, - 179, OP_CMPU, 0x44, - 188, OP_CMPS, 0x44, +static const uint8_t map2[] PROGMEM = { + 63, OP_SWI3, 0x25, + 131, OP_CMPU, 0x41, + 140, OP_CMPS, 0x41, + 147, OP_CMPU, 0x32, + 156, OP_CMPS, 0x32, + 163, OP_CMPU, 0x33, + 172, OP_CMPS, 0x33, + 179, OP_CMPU, 0x44, + 188, OP_CMPS, 0x44, 255, OP_XX , 0x10 }; static const char regi[] = { 'X', 'Y', 'U', 'S' }; static const char *exgi[] = { "D", "X", "Y", "U", "S", "PC", "??", "??", "A", - "B", "CC", "DP", "??", "??", "??", "??" }; + "B", "CC", "DP", "??", "??", "??", "??" }; static const char *pshsregi[] = { "PC", "U", "Y", "X", "DP", "B", "A", "CC" }; + static const char *pshuregi[] = { "PC", "S", "Y", "X", "DP", "B", "A", "CC" }; -/* disassemble one instruction at adress adr and return its size */ +extern const char statusString[]; -char hexdigit(tt_u16 v) -{ - v &= 0xf; - if (v <= 9) - return '0' + v; - else - return 'A' - 10 + v; +static uint8_t get_memb(addr_t addr) { + loadAddr(addr); + return readMemByteInc(); } -char *hex8str(tt_u8 v) -{ - static char tmpbuf[3] = " "; - - tmpbuf[1] = hexdigit(v); - tmpbuf[0] = hexdigit(v >> 4); - - return tmpbuf; +static uint16_t get_memw(addr_t addr) { + loadAddr(addr); + return (readMemByteInc() << 8) + readMemByteInc(); } -char *hex16str(tt_u16 v) -{ - static char tmpbuf[5] = " "; - - tmpbuf[3] = hexdigit(v); - v >>= 4; - tmpbuf[2] = hexdigit(v); - v >>= 4; - tmpbuf[1] = hexdigit(v); - v >>= 4; - tmpbuf[0] = hexdigit(v); - - return tmpbuf; -} - -extern char *statusString; - -char *ccstr(tt_u8 val) -{ - static char tempbuf[9] = " "; - int i; - +static char *strcc(char *ptr, uint8_t val) { + uint8_t i; for (i = 0; i < 8; i++) { - if (val & 0x80) - tempbuf[i] = statusString[i]; - else - tempbuf[i] = '.'; + *ptr++ = (val & 0x80) ? statusString[i] : '.'; val <<= 1; } - - return tempbuf; + return ptr; } -unsigned int disassemble(unsigned int addr) -{ - int d = get_memb(addr); - int s, i; - tt_u8 pb; +/* disassemble one instruction at address addr and return the address of the next instruction */ + +addr_t disassemble(addr_t addr) { + uint8_t d = get_memb(addr); + uint8_t s; + int8_t i; + uint8_t pb; char reg; - const unsigned char *map = NULL; + char *ptr; + static char buffer[64]; + const uint8_t *map = NULL; // Default for most undefined opcodes unsigned char sm = 0x10; // size_mode byte unsigned char oi = OP_XX; // opcode index - FILE *stream = &ser0stream; - if (d == 0x10) { - d = get_memb(addr + 1); + d = get_memb(addr + 1); map = map1; - } - - if (d == 0x11) { + } else if (d == 0x11) { d = get_memb(addr + 1); map = map2; } @@ -724,12 +674,13 @@ unsigned int disassemble(unsigned int addr) map -= 3; do { map += 3; - if (pgm_read_byte(map) == d) { - oi = pgm_read_byte(++map); - sm = pgm_read_byte(++map); - break; + s = pgm_read_byte(map); + if (s == d) { + oi = pgm_read_byte(++map); + sm = pgm_read_byte(++map); + break; } - } while (*map < 255); + } while (s < 255); } else { // Lookup directly in map0 map = map0 + 2 * d; @@ -739,187 +690,258 @@ unsigned int disassemble(unsigned int addr) s = sm >> 4; - fprintf(stream, "%04X ", addr); + // 0123456789012345678901234567890123456789 + // AAAA : HH HH HH HH : OOOO AAAAAAAAA + strfill(buffer, ' ', sizeof(buffer)); + buffer[5] = ':'; + buffer[19] = ':'; + + // Address + strhex4(buffer, addr); + + // Hex + ptr = buffer + 7; for (i = 0; i < s; i++) { - fputs(hex8str(get_memb(addr + i)), stream); - fputc(' ', stream); - } - for (i = s; i < 4; i++) { - fputs(" ", stream); + strhex2(ptr, get_memb(addr + i)); + ptr += 3; } - const char *ip = inst + oi * 4; - for (i = 0; i < 4; i++) - fputc(pgm_read_byte(ip++), stream); + // Opcode + ptr = buffer + 21; + const char *ip = inst + oi * 4; + for (i = 0; i < 4; i++) { + *ptr++ = pgm_read_byte(ip++); + } + ptr++; - fputs(" ", stream); - - switch(sm & 15) { case 1: /* immediate */ - fputs("#$", stream); - if (s == 2) - fputs(hex8str(get_memb(addr + 1)), stream); - else - fputs(hex16str(get_memw(addr + s - 2)), stream); + *ptr++ = '#'; + *ptr++ = '$'; + if (s == 2) { + ptr = strhex2(ptr, get_memb(addr + 1)); + } else { + ptr = strhex4(ptr, get_memw(addr + s - 2)); + } break; case 2: /* direct */ - fputs("$", stream); - fputs(hex8str(get_memb(addr + s - 1)), stream); + *ptr++ = '$'; + ptr = strhex2(ptr, get_memb(addr + s - 1)); break; case 3: /* indexed */ pb = get_memb(addr + s - 1); reg = regi[(pb >> 5) & 0x03]; - if (!(pb & 0x80)) { /* n4,R */ - if (pb & 0x10) - fprintf(stream, "-$%s,%c", hex8str(((pb & 0x0f) ^ 0x0f) + 1), reg); - else - fprintf(stream, "$%s,%c", hex8str(pb & 0x0f), reg); - } - else { - if (pb & 0x10) - fputc('[', stream); + if (pb & 0x10) { + *ptr++ = '-'; + *ptr++ = '$'; + ptr = strhex2(ptr, ((pb & 0x0f) ^ 0x0f) + 1); + } else { + *ptr++ = '$'; + ptr = strhex2(ptr, pb & 0x0f); + } + *ptr++ = ','; + *ptr++ = reg; + } else { + if (pb & 0x10) { + *ptr++ = '['; + } switch (pb & 0x0f) { case 0: /* ,R+ */ - fprintf(stream, ",%c+", reg); - break; + *ptr++ = ','; + *ptr++ = reg; + *ptr++ = '+'; + break; case 1: /* ,R++ */ - fprintf(stream, ",%c++", reg); - break; + *ptr++ = ','; + *ptr++ = reg; + *ptr++ = '+'; + *ptr++ = '+'; + break; case 2: /* ,-R */ - fprintf(stream, ",-%c", reg); - break; + *ptr++ = ','; + *ptr++ = '-'; + *ptr++ = reg; + break; case 3: /* ,--R */ - fprintf(stream, ",--%c", reg); - break; + *ptr++ = ','; + *ptr++ = '-'; + *ptr++ = '-'; + *ptr++ = reg; + break; case 4: /* ,R */ - fprintf(stream, ",%c", reg); - break; + *ptr++ = ','; + *ptr++ = reg; + break; case 5: /* B,R */ - fprintf(stream, "B,%c", reg); - break; + *ptr++ = 'B'; + *ptr++ = ','; + *ptr++ = reg; + break; case 6: /* A,R */ - fprintf(stream, "A,%c", reg); - break; + *ptr++ = 'A'; + *ptr++ = ','; + *ptr++ = reg; + break; case 8: /* n7,R */ - s += 1; - fprintf(stream, "$%s,%c", hex8str(get_memb(addr + s - 1)), reg); - break; + s += 1; + *ptr++ = '$'; + ptr = strhex2(ptr, get_memb(addr + s - 1)); + *ptr++ = ','; + *ptr++ = reg; + break; case 9: /* n15,R */ - s += 2; - fprintf(stream, "$%s,%c", hex16str(get_memw(addr + s - 2)), reg); - break; + s += 2; + *ptr++ = '$'; + ptr = strhex4(ptr, get_memw(addr + s - 2)); + *ptr++ = ','; + *ptr++ = reg; + break; case 11: /* D,R */ - fprintf(stream, "D,%c", reg); - break; + *ptr++ = 'D'; + *ptr++ = ','; + *ptr++ = reg; + break; case 12: /* n7,PCR */ - s += 1; - fprintf(stream, "$%s,PCR", hex8str(get_memb(addr + s - 1))); - break; + s += 1; + *ptr++ = '$'; + ptr = strhex2(ptr, get_memb(addr + s - 1)); + *ptr++ = ','; + *ptr++ = 'P'; + *ptr++ = 'C'; + *ptr++ = 'R'; + break; case 13: /* n15,PCR */ - s += 2; - fprintf(stream, "$%s,PCR", hex16str(get_memw(addr + s - 2))); - break; + s += 2; + *ptr++ = '$'; + ptr = strhex4(ptr, get_memw(addr + s - 2)); + *ptr++ = ','; + *ptr++ = 'P'; + *ptr++ = 'C'; + *ptr++ = 'R'; + break; case 15: /* [n] */ - s += 2; - fprintf(stream, "$%s", hex16str(get_memw(addr + s - 2))); - break; + s += 2; + *ptr++ = '$'; + ptr = strhex4(ptr, get_memw(addr + s - 2)); + break; default: - fputs("??", stream); - break; } - if (pb & 0x10) - fputc(']', stream); + *ptr++ = '?'; + *ptr++ = '?'; + break; + } + if (pb & 0x10) { + *ptr++ = ']'; + } } break; case 4: /* extended */ - fprintf(stream, "$%s", hex16str(get_memw(addr + s - 2))); + *ptr++ = '$'; + ptr = strhex4(ptr, get_memw(addr + s - 2)); break; case 5: /* inherent */ pb = get_memb(addr + 1); switch (d) { case 0x1e: case 0x1f: /* exg tfr */ - fprintf(stream, "%s,%s", exgi[(pb >> 4) & 0x0f], exgi[pb & 0x0f]); - break; + ptr = strinsert(ptr, exgi[(pb >> 4) & 0x0f]); + *ptr++ = ','; + ptr = strinsert(ptr, exgi[pb & 0x0f]); + break; case 0x1a: case 0x1c: case 0x3c: /* orcc andcc cwai */ - fprintf(stream, "#$%s=%s", hex8str(pb), ccstr(pb)); + *ptr++ = '#'; + *ptr++ = '$'; + ptr = strhex2(ptr, pb); + *ptr++ = '='; + ptr = strcc(ptr, pb); break; case 0x34: /* pshs */ { - int p = 0; - - for (i = 0; i < 8; i++) { - if (pb & 0x80) { - if (p) - fputc(',', stream); - fputs(pshsregi[i], stream); - p = 1; - } - pb <<= 1; - } + int p = 0; + for (i = 0; i < 8; i++) { + if (pb & 0x80) { + if (p) { + *ptr++ = ','; + } + ptr = strinsert(ptr, pshsregi[i]); + p = 1; + } + pb <<= 1; + } } break; case 0x35: /* puls */ { - int p = 0; - - for (i = 7; i >= 0; i--) { - if (pb & 0x01) { - if (p) - fputc(',', stream); - fputs(pshsregi[i], stream); - p = 1; - } - pb >>= 1; - } + int p = 0; + for (i = 7; i >= 0; i--) { + if (pb & 0x01) { + if (p) { + *ptr++ = ','; + } + ptr = strinsert(ptr, pshsregi[i]); + p = 1; + } + pb >>= 1; + } } break; case 0x36: /* pshu */ { - int p = 0; - - for (i = 0; i < 8; i++) { - if (pb & 0x80) { - if (p) - fputc(',', stream); - fputs(pshuregi[i], stream); - p = 1; - } - pb <<= 1; - } + int p = 0; + for (i = 0; i < 8; i++) { + if (pb & 0x80) { + if (p) { + *ptr++ = ','; + } + ptr = strinsert(ptr, pshuregi[i]); + p = 1; + } + pb <<= 1; + } } break; case 0x37: /* pulu */ { - int p = 0; - - for (i = 7; i >= 0; i--) { - if (pb & 0x01) { - if (p) - fputc(',', stream); - fputs(pshuregi[i], stream); - p = 1; - } - pb >>= 1; - } + int p = 0; + for (i = 7; i >= 0; i--) { + if (pb & 0x01) { + if (p) { + *ptr++ = ','; + } + ptr = strinsert(ptr, pshuregi[i]); + p = 1; + } + pb >>= 1; + } } break; } - break; + break; case 6: /* relative */ { - tt_s16 v; - - if (s == 2) - v = (tt_s16)(tt_s8)get_memb(addr + 1); - else - v = (tt_s16)get_memw(addr + s - 2); - fprintf(stream, "$%s", hex16str(addr + (tt_u16)s + v)); - break; - } + int16_t v; + if (s == 2) { + v = (int16_t)(int8_t)get_memb(addr + 1); + } else { + v = (int16_t)get_memw(addr + s - 2); + } + *ptr++ = '$'; + ptr = strhex4(ptr, addr + (uint16_t)s + v); + } + break; } - fputc('\n', stream); + // Get rid of trailing white space + while (*(--ptr) == ' '); + ptr++; + + // Add a newline and terminate the string + *ptr++ = '\n'; + *ptr++ = '\0'; + + // Log using the normal (data memory) string logger + logs(buffer); + + // Return the address of the next instruction return addr + s; } diff --git a/firmware/regs6809.c b/firmware/regs6809.c index ea16b89..d5609c9 100644 --- a/firmware/regs6809.c +++ b/firmware/regs6809.c @@ -10,28 +10,35 @@ #define OFFSET_REG_D 44 #define OFFSET_REG_CC 45 -char statusString[8] = "EFHINZVC"; +const char statusString[8] = "EFHINZVC"; void doCmdRegs(char *params) { - int i; - unsigned int p = hwRead8(OFFSET_REG_CC); - log0("6809 Registers:\n A=%02X B=%02X X=%04X Y=%04X\n", - hwRead8(OFFSET_REG_A), - hwRead8(OFFSET_REG_B), - hwRead16(OFFSET_REG_X), - hwRead16(OFFSET_REG_Y)); - log0(" CC=%02X D=%02X U=%04X S=%04X PC=%04X\n", - p, - hwRead8(OFFSET_REG_D), - hwRead16(OFFSET_REG_U), - hwRead16(OFFSET_REG_S), - hwRead16(OFFSET_REG_PC)); - char *sp = statusString; - log0(" Status: "); + uint16_t i; + uint8_t p = hwRead8(OFFSET_REG_CC); + const char *sp = statusString; + logstr("6809 Registers:\n A="); + loghex2(hwRead8(OFFSET_REG_A)); + logstr(" B="); + loghex2(hwRead8(OFFSET_REG_B)); + logstr(" X="); + loghex4(hwRead16(OFFSET_REG_X)); + logstr(" Y="); + loghex4(hwRead16(OFFSET_REG_Y)); + logstr("\n CC="); + loghex2(p); + logstr(" D="); + loghex2(hwRead8(OFFSET_REG_D)); + logstr(" U="); + loghex4(hwRead16(OFFSET_REG_U)); + logstr(" S="); + loghex4(hwRead16(OFFSET_REG_S)); + logstr(" PC="); + loghex4(hwRead16(OFFSET_REG_PC)); + logstr("\n Status: "); for (i = 0; i <= 7; i++) { - log0("%c", ((p & 128) ? (*sp) : '-')); + logc(((p & 128) ? (*sp) : '-')); p <<= 1; sp++; } - log0("\n"); + logc('\n'); } diff --git a/firmware/status.c b/firmware/status.c index e0cafc1..07d6a14 100644 --- a/firmware/status.c +++ b/firmware/status.c @@ -195,3 +195,10 @@ char *strint(char *buffer, int i) { char *strlong(char *buffer, long i) { return ltoa(i, buffer, 10); } + +char *strinsert(char *buffer, const char *s) { + while (*s) { + *buffer++ = *s++; + } + return buffer; +} diff --git a/firmware/status.h b/firmware/status.h index f59e7e2..811a6aa 100644 --- a/firmware/status.h +++ b/firmware/status.h @@ -42,6 +42,7 @@ char *strhex2(char *buffer, uint8_t i); char *strhex4(char *buffer, uint16_t i); char *strint(char *buffer, int i); char *strlong(char *buffer, long i); +char *strinsert(char *buffer, const char *s); #define log0(format,...) fprintf_P(&ser0stream,PSTR(format),##__VA_ARGS__)