6809: eliminate usage of log0

Change-Id: Iddf14f72e9e848703aba07208708b55968d100af
This commit is contained in:
David Banks 2019-11-12 11:50:49 +00:00
parent ec53c9d54f
commit 15c212f4b6
4 changed files with 314 additions and 277 deletions

View File

@ -18,24 +18,6 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include "AtomBusMon.h" #include "AtomBusMon.h"
unsigned char get_memb(unsigned int addr) {
loadAddr(addr);
return readMemByteInc();
}
#include <stdio.h>
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 { enum opcodes {
OP_UU , OP_UU ,
OP_XX , OP_XX ,
@ -320,7 +302,7 @@ TSTB";
// 5 inherent // 5 inherent
// 6 relative // 6 relative
static const unsigned char map0[] PROGMEM = { static const uint8_t map0[] PROGMEM = {
OP_NEG , 0x22, OP_NEG , 0x22,
OP_XX , 0x22, OP_XX , 0x22,
OP_XX , 0x12, OP_XX , 0x12,
@ -579,7 +561,7 @@ static const unsigned char map0[] PROGMEM = {
OP_STU , 0x34, OP_STU , 0x34,
}; };
static const unsigned char map1[] PROGMEM = { static const uint8_t map1[] PROGMEM = {
33, OP_LBRN, 0x46, 33, OP_LBRN, 0x46,
34, OP_LBHI, 0x46, 34, OP_LBHI, 0x46,
35, OP_LBLS, 0x46, 35, OP_LBLS, 0x46,
@ -620,7 +602,7 @@ static const unsigned char map1[] PROGMEM = {
255, OP_STS , 0x44, 255, OP_STS , 0x44,
}; };
static const unsigned char map2[] PROGMEM = { static const uint8_t map2[] PROGMEM = {
63, OP_SWI3, 0x25, 63, OP_SWI3, 0x25,
131, OP_CMPU, 0x41, 131, OP_CMPU, 0x41,
140, OP_CMPS, 0x41, 140, OP_CMPS, 0x41,
@ -639,82 +621,50 @@ 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 *pshsregi[] = { "PC", "U", "Y", "X", "DP", "B", "A", "CC" };
static const char *pshuregi[] = { "PC", "S", "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) static uint8_t get_memb(addr_t addr) {
{ loadAddr(addr);
v &= 0xf; return readMemByteInc();
if (v <= 9)
return '0' + v;
else
return 'A' - 10 + v;
} }
char *hex8str(tt_u8 v) static uint16_t get_memw(addr_t addr) {
{ loadAddr(addr);
static char tmpbuf[3] = " "; return (readMemByteInc() << 8) + readMemByteInc();
tmpbuf[1] = hexdigit(v);
tmpbuf[0] = hexdigit(v >> 4);
return tmpbuf;
} }
char *hex16str(tt_u16 v) static char *strcc(char *ptr, uint8_t val) {
{ uint8_t i;
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;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
if (val & 0x80) *ptr++ = (val & 0x80) ? statusString[i] : '.';
tempbuf[i] = statusString[i];
else
tempbuf[i] = '.';
val <<= 1; val <<= 1;
} }
return ptr;
return tempbuf;
} }
unsigned int disassemble(unsigned int addr) /* disassemble one instruction at address addr and return the address of the next instruction */
{
int d = get_memb(addr); addr_t disassemble(addr_t addr) {
int s, i; uint8_t d = get_memb(addr);
tt_u8 pb; uint8_t s;
int8_t i;
uint8_t pb;
char reg; char reg;
const unsigned char *map = NULL; char *ptr;
static char buffer[64];
const uint8_t *map = NULL;
// Default for most undefined opcodes // Default for most undefined opcodes
unsigned char sm = 0x10; // size_mode byte unsigned char sm = 0x10; // size_mode byte
unsigned char oi = OP_XX; // opcode index unsigned char oi = OP_XX; // opcode index
FILE *stream = &ser0stream;
if (d == 0x10) { if (d == 0x10) {
d = get_memb(addr + 1); d = get_memb(addr + 1);
map = map1; map = map1;
} } else if (d == 0x11) {
if (d == 0x11) {
d = get_memb(addr + 1); d = get_memb(addr + 1);
map = map2; map = map2;
} }
@ -724,12 +674,13 @@ unsigned int disassemble(unsigned int addr)
map -= 3; map -= 3;
do { do {
map += 3; map += 3;
if (pgm_read_byte(map) == d) { s = pgm_read_byte(map);
if (s == d) {
oi = pgm_read_byte(++map); oi = pgm_read_byte(++map);
sm = pgm_read_byte(++map); sm = pgm_read_byte(++map);
break; break;
} }
} while (*map < 255); } while (s < 255);
} else { } else {
// Lookup directly in map0 // Lookup directly in map0
map = map0 + 2 * d; map = map0 + 2 * d;
@ -739,121 +690,180 @@ unsigned int disassemble(unsigned int addr)
s = sm >> 4; 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++) { for (i = 0; i < s; i++) {
fputs(hex8str(get_memb(addr + i)), stream); strhex2(ptr, get_memb(addr + i));
fputc(' ', stream); ptr += 3;
}
for (i = s; i < 4; i++) {
fputs(" ", stream);
} }
// Opcode
ptr = buffer + 21;
const char *ip = inst + oi * 4; const char *ip = inst + oi * 4;
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++) {
fputc(pgm_read_byte(ip++), stream); *ptr++ = pgm_read_byte(ip++);
}
fputs(" ", stream); ptr++;
switch(sm & 15) { switch(sm & 15) {
case 1: /* immediate */ case 1: /* immediate */
fputs("#$", stream); *ptr++ = '#';
if (s == 2) *ptr++ = '$';
fputs(hex8str(get_memb(addr + 1)), stream); if (s == 2) {
else ptr = strhex2(ptr, get_memb(addr + 1));
fputs(hex16str(get_memw(addr + s - 2)), stream); } else {
ptr = strhex4(ptr, get_memw(addr + s - 2));
}
break; break;
case 2: /* direct */ case 2: /* direct */
fputs("$", stream); *ptr++ = '$';
fputs(hex8str(get_memb(addr + s - 1)), stream); ptr = strhex2(ptr, get_memb(addr + s - 1));
break; break;
case 3: /* indexed */ case 3: /* indexed */
pb = get_memb(addr + s - 1); pb = get_memb(addr + s - 1);
reg = regi[(pb >> 5) & 0x03]; reg = regi[(pb >> 5) & 0x03];
if (!(pb & 0x80)) { /* n4,R */ if (!(pb & 0x80)) { /* n4,R */
if (pb & 0x10) if (pb & 0x10) {
fprintf(stream, "-$%s,%c", hex8str(((pb & 0x0f) ^ 0x0f) + 1), reg); *ptr++ = '-';
else *ptr++ = '$';
fprintf(stream, "$%s,%c", hex8str(pb & 0x0f), reg); ptr = strhex2(ptr, ((pb & 0x0f) ^ 0x0f) + 1);
} else {
*ptr++ = '$';
ptr = strhex2(ptr, pb & 0x0f);
}
*ptr++ = ',';
*ptr++ = reg;
} else {
if (pb & 0x10) {
*ptr++ = '[';
} }
else {
if (pb & 0x10)
fputc('[', stream);
switch (pb & 0x0f) { switch (pb & 0x0f) {
case 0: /* ,R+ */ case 0: /* ,R+ */
fprintf(stream, ",%c+", reg); *ptr++ = ',';
*ptr++ = reg;
*ptr++ = '+';
break; break;
case 1: /* ,R++ */ case 1: /* ,R++ */
fprintf(stream, ",%c++", reg); *ptr++ = ',';
*ptr++ = reg;
*ptr++ = '+';
*ptr++ = '+';
break; break;
case 2: /* ,-R */ case 2: /* ,-R */
fprintf(stream, ",-%c", reg); *ptr++ = ',';
*ptr++ = '-';
*ptr++ = reg;
break; break;
case 3: /* ,--R */ case 3: /* ,--R */
fprintf(stream, ",--%c", reg); *ptr++ = ',';
*ptr++ = '-';
*ptr++ = '-';
*ptr++ = reg;
break; break;
case 4: /* ,R */ case 4: /* ,R */
fprintf(stream, ",%c", reg); *ptr++ = ',';
*ptr++ = reg;
break; break;
case 5: /* B,R */ case 5: /* B,R */
fprintf(stream, "B,%c", reg); *ptr++ = 'B';
*ptr++ = ',';
*ptr++ = reg;
break; break;
case 6: /* A,R */ case 6: /* A,R */
fprintf(stream, "A,%c", reg); *ptr++ = 'A';
*ptr++ = ',';
*ptr++ = reg;
break; break;
case 8: /* n7,R */ case 8: /* n7,R */
s += 1; s += 1;
fprintf(stream, "$%s,%c", hex8str(get_memb(addr + s - 1)), reg); *ptr++ = '$';
ptr = strhex2(ptr, get_memb(addr + s - 1));
*ptr++ = ',';
*ptr++ = reg;
break; break;
case 9: /* n15,R */ case 9: /* n15,R */
s += 2; s += 2;
fprintf(stream, "$%s,%c", hex16str(get_memw(addr + s - 2)), reg); *ptr++ = '$';
ptr = strhex4(ptr, get_memw(addr + s - 2));
*ptr++ = ',';
*ptr++ = reg;
break; break;
case 11: /* D,R */ case 11: /* D,R */
fprintf(stream, "D,%c", reg); *ptr++ = 'D';
*ptr++ = ',';
*ptr++ = reg;
break; break;
case 12: /* n7,PCR */ case 12: /* n7,PCR */
s += 1; s += 1;
fprintf(stream, "$%s,PCR", hex8str(get_memb(addr + s - 1))); *ptr++ = '$';
ptr = strhex2(ptr, get_memb(addr + s - 1));
*ptr++ = ',';
*ptr++ = 'P';
*ptr++ = 'C';
*ptr++ = 'R';
break; break;
case 13: /* n15,PCR */ case 13: /* n15,PCR */
s += 2; s += 2;
fprintf(stream, "$%s,PCR", hex16str(get_memw(addr + s - 2))); *ptr++ = '$';
ptr = strhex4(ptr, get_memw(addr + s - 2));
*ptr++ = ',';
*ptr++ = 'P';
*ptr++ = 'C';
*ptr++ = 'R';
break; break;
case 15: /* [n] */ case 15: /* [n] */
s += 2; s += 2;
fprintf(stream, "$%s", hex16str(get_memw(addr + s - 2))); *ptr++ = '$';
ptr = strhex4(ptr, get_memw(addr + s - 2));
break; break;
default: default:
fputs("??", stream); *ptr++ = '?';
break; } *ptr++ = '?';
if (pb & 0x10) break;
fputc(']', stream); }
if (pb & 0x10) {
*ptr++ = ']';
}
} }
break; break;
case 4: /* extended */ case 4: /* extended */
fprintf(stream, "$%s", hex16str(get_memw(addr + s - 2))); *ptr++ = '$';
ptr = strhex4(ptr, get_memw(addr + s - 2));
break; break;
case 5: /* inherent */ case 5: /* inherent */
pb = get_memb(addr + 1); pb = get_memb(addr + 1);
switch (d) { switch (d) {
case 0x1e: case 0x1f: /* exg tfr */ case 0x1e: case 0x1f: /* exg tfr */
fprintf(stream, "%s,%s", exgi[(pb >> 4) & 0x0f], exgi[pb & 0x0f]); ptr = strinsert(ptr, exgi[(pb >> 4) & 0x0f]);
*ptr++ = ',';
ptr = strinsert(ptr, exgi[pb & 0x0f]);
break; break;
case 0x1a: case 0x1c: case 0x3c: /* orcc andcc cwai */ 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; break;
case 0x34: /* pshs */ case 0x34: /* pshs */
{ {
int p = 0; int p = 0;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
if (pb & 0x80) { if (pb & 0x80) {
if (p) if (p) {
fputc(',', stream); *ptr++ = ',';
fputs(pshsregi[i], stream); }
ptr = strinsert(ptr, pshsregi[i]);
p = 1; p = 1;
} }
pb <<= 1; pb <<= 1;
@ -863,12 +873,12 @@ unsigned int disassemble(unsigned int addr)
case 0x35: /* puls */ case 0x35: /* puls */
{ {
int p = 0; int p = 0;
for (i = 7; i >= 0; i--) { for (i = 7; i >= 0; i--) {
if (pb & 0x01) { if (pb & 0x01) {
if (p) if (p) {
fputc(',', stream); *ptr++ = ',';
fputs(pshsregi[i], stream); }
ptr = strinsert(ptr, pshsregi[i]);
p = 1; p = 1;
} }
pb >>= 1; pb >>= 1;
@ -878,12 +888,12 @@ unsigned int disassemble(unsigned int addr)
case 0x36: /* pshu */ case 0x36: /* pshu */
{ {
int p = 0; int p = 0;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
if (pb & 0x80) { if (pb & 0x80) {
if (p) if (p) {
fputc(',', stream); *ptr++ = ',';
fputs(pshuregi[i], stream); }
ptr = strinsert(ptr, pshuregi[i]);
p = 1; p = 1;
} }
pb <<= 1; pb <<= 1;
@ -893,12 +903,12 @@ unsigned int disassemble(unsigned int addr)
case 0x37: /* pulu */ case 0x37: /* pulu */
{ {
int p = 0; int p = 0;
for (i = 7; i >= 0; i--) { for (i = 7; i >= 0; i--) {
if (pb & 0x01) { if (pb & 0x01) {
if (p) if (p) {
fputc(',', stream); *ptr++ = ',';
fputs(pshuregi[i], stream); }
ptr = strinsert(ptr, pshuregi[i]);
p = 1; p = 1;
} }
pb >>= 1; pb >>= 1;
@ -909,17 +919,29 @@ unsigned int disassemble(unsigned int addr)
break; break;
case 6: /* relative */ case 6: /* relative */
{ {
tt_s16 v; int16_t v;
if (s == 2) {
if (s == 2) v = (int16_t)(int8_t)get_memb(addr + 1);
v = (tt_s16)(tt_s8)get_memb(addr + 1); } else {
else v = (int16_t)get_memw(addr + s - 2);
v = (tt_s16)get_memw(addr + s - 2); }
fprintf(stream, "$%s", hex16str(addr + (tt_u16)s + v)); *ptr++ = '$';
ptr = strhex4(ptr, addr + (uint16_t)s + v);
}
break; 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; return addr + s;
} }

View File

@ -10,28 +10,35 @@
#define OFFSET_REG_D 44 #define OFFSET_REG_D 44
#define OFFSET_REG_CC 45 #define OFFSET_REG_CC 45
char statusString[8] = "EFHINZVC"; const char statusString[8] = "EFHINZVC";
void doCmdRegs(char *params) { void doCmdRegs(char *params) {
int i; uint16_t i;
unsigned int p = hwRead8(OFFSET_REG_CC); uint8_t p = hwRead8(OFFSET_REG_CC);
log0("6809 Registers:\n A=%02X B=%02X X=%04X Y=%04X\n", const char *sp = statusString;
hwRead8(OFFSET_REG_A), logstr("6809 Registers:\n A=");
hwRead8(OFFSET_REG_B), loghex2(hwRead8(OFFSET_REG_A));
hwRead16(OFFSET_REG_X), logstr(" B=");
hwRead16(OFFSET_REG_Y)); loghex2(hwRead8(OFFSET_REG_B));
log0(" CC=%02X D=%02X U=%04X S=%04X PC=%04X\n", logstr(" X=");
p, loghex4(hwRead16(OFFSET_REG_X));
hwRead8(OFFSET_REG_D), logstr(" Y=");
hwRead16(OFFSET_REG_U), loghex4(hwRead16(OFFSET_REG_Y));
hwRead16(OFFSET_REG_S), logstr("\n CC=");
hwRead16(OFFSET_REG_PC)); loghex2(p);
char *sp = statusString; logstr(" D=");
log0(" Status: "); 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++) { for (i = 0; i <= 7; i++) {
log0("%c", ((p & 128) ? (*sp) : '-')); logc(((p & 128) ? (*sp) : '-'));
p <<= 1; p <<= 1;
sp++; sp++;
} }
log0("\n"); logc('\n');
} }

View File

@ -195,3 +195,10 @@ char *strint(char *buffer, int i) {
char *strlong(char *buffer, long i) { char *strlong(char *buffer, long i) {
return ltoa(i, buffer, 10); return ltoa(i, buffer, 10);
} }
char *strinsert(char *buffer, const char *s) {
while (*s) {
*buffer++ = *s++;
}
return buffer;
}

View File

@ -42,6 +42,7 @@ char *strhex2(char *buffer, uint8_t i);
char *strhex4(char *buffer, uint16_t i); char *strhex4(char *buffer, uint16_t i);
char *strint(char *buffer, int i); char *strint(char *buffer, int i);
char *strlong(char *buffer, long i); char *strlong(char *buffer, long i);
char *strinsert(char *buffer, const char *s);
#define log0(format,...) fprintf_P(&ser0stream,PSTR(format),##__VA_ARGS__) #define log0(format,...) fprintf_P(&ser0stream,PSTR(format),##__VA_ARGS__)