#include "sux.h"
#include "disasm.h"
#include <string.h>
void print_regs(struct sux *cpu, uint8_t lines, uint8_t thread) {
for (uint8_t i = (24*thread)+2; i <= 24*(thread+1); i++) {
wmove(scr, i, 0);
waddch(scr, (i == lines) ? '>' : ' ');
}
wmove(scr, lines, 1);
wclrtoeol(scr);
wprintw(scr, "pc: $%04"PRIX64 , cpu->pc);
wprintw(scr, ", a: $%016"PRIX64, cpu->a);
wprintw(scr, ", b: $%016"PRIX64, cpu->b);
wprintw(scr, ", x: $%016"PRIX64, cpu->x);
wprintw(scr, ", y: $%016"PRIX64, cpu->y);
wprintw(scr, ", sp: $%04X", cpu->sp);
wprintw(scr, ", ps: $%02X", cpu->ps.u8[thread]);
wprintw(scr, ", inst: ");
}
void disasm(struct sux *cpu, uint8_t lines, uint8_t opcode, uint8_t prefix, uint8_t thread) {
uint64_t value;
uint64_t address = get_addr(cpu, opcode, prefix, 0, 0, thread);
uint8_t rs = (prefix >> 4) & 3;
char *postfix;
char *of;
char op[4];
char *sign = "";
char *ind = "";
uint8_t tmp = 0;
union reg mask;
mask.u64 = 0;
memcpy(op, opname[opcode], 3);
op[3] = 0;
switch (rs) {
case 0: postfix = ""; break;
case 1: postfix = ".W"; break;
case 2: postfix = ".D"; break;
case 3: postfix = ".Q"; break;
}
if (prefix >> 6) {
tmp = 0;
}
switch (prefix >> 6) {
default: of = ""; break;
case 1 : of = "SP"; break;
case 2 : of = "PC"; break;
}
uint8_t addrsize = 0;
char *idx;
switch (optype[opcode]) {
case IND : idx = ")"; break;
case INDX: idx = ", x)"; break;
case INDY: idx = "), y"; break;
case ZMX : idx = ", x"; break;
case ZMY : idx = ", y"; break;
default : idx = ""; break;
}
switch (optype[opcode]) {
case ZM :
case ZMX :
case ZMY :
case IND :
case INDX:
case INDY: addrsize = get_addrsize(prefix, ZM); break;
case ABS : addrsize = get_addrsize(prefix, ABS); break;
case IMM :
case REL : addrsize = (1 << rs)-1; break;
}
mask.u64 = (-(uint64_t)1 >> ((7 - addrsize) * 8));
value = read_value(cpu, 0, cpu->pc, addrsize, 0, 0);
if ((prefix >> 6) == 1 || (prefix >> 6) == 2 || optype[opcode] == REL) {
switch (addrsize) {
case 0 : sign = ((int8_t )value < 0) ? "-" : "+"; break;
case 1 : sign = ((int16_t)value < 0) ? "-" : "+"; break;
case 2 :
case 3 : sign = ((int32_t)value < 0) ? "-" : "+"; break;
case 4 :
case 5 :
case 6 :
case 7 : sign = ((int64_t)value < 0) ? "-" : "+"; break;
}
value = (sign[0] == '-') ? (~value + 1) & mask.u64 : value;
}
switch (optype[opcode]) {
case BREG:
case IMPL: wprintw(scr, "%s%s" , opname[opcode], postfix); break;
case IMM : wprintw(scr, "%s%s #$%0*"PRIX64 , op, postfix, (addrsize+1) << 1, value); break;
case IND :
case INDX:
case INDY: ind = "(";
case ZMX :
case ZMY :
case ZM :
case ABS : wprintw(scr, "%s%s %s%s%s$%0*" PRIX64"%s" , op, postfix, ind, of, sign, (addrsize+1) << 1, value, idx); break;
case REL : wprintw(scr, "%s%s %s$%0*"PRIX64 , op, postfix, sign, (addrsize+1) << 1, value); break;
}
if (address == TX_ADDR || address == RX_ADDR) {
wmove(scr, 27, 0);
wprintw(scr, "TX_ADDR: $%02X, RX_ADDR: $%02X", addr[TX_ADDR], addr[RX_ADDR]);
}
wmove(scr, 29, 0);
wclrtoeol(scr);
wprintw(scr, "address: $%04"PRIX64, address);
if (subdbg) {
uint8_t ln = 33;
uint16_t line_idx = 0;
uint32_t tmpad = 0x20237;
int row, col;
uint8_t iscursor = 0;
union reg ptr;
ptr.u64 = 0;
uint32_t adr;
if (address == CTRL_ADDR || addr[STEP_ADDR]) {
adr = read_value(cpu, 0, tmpad, 7, 0, 0);
tmpad += 8;
wprintw(scr, ", scr_row: %02u, scr_col: %02u, scr_str: %02u, scr_end: %02u", addr[0], addr[1], addr[2], addr[3]);
wmove(scr, 32, 0);
wprintw(scr, "bitabl: ");
for (uint8_t i = 0; i < 16; i++) {
wprintw(scr, "%s", showbits(addr[adr+i], 7, 0));
}
mvwprintw(scr, ln++, 0, "buffer: ");
wmove(scr, ln++, 0);
uint8_t maxrow = 10;
int line_offset = (addr[0]-(maxrow-1) >= 0) ? addr[0]-(maxrow-1) : 0;
adr = read_value(cpu, 0, tmpad, 7, 0, 0);
for (uint8_t i = 0; i < maxrow; i++) {
line_idx = (i+addr[2]+line_offset << 6) + (i+addr[2]+line_offset << 4);
for (uint8_t j = 0; j < 0x50; j++) {
wprintw(scr, "%02X", addr[adr+j+line_idx]);
if ((addr[0] == i+line_offset) && addr[1] == j) {
iscursor=1;
getyx(scr,row, col);
wmove(scr, ln++, 0);
wclrtoeol(scr);
wmove(scr, row+1, col-2);
wprintw(scr, "/\\");
wmove(scr, row, col);
}
}
wprintw(scr, ", i: %02X", i);
if (!iscursor) {
wmove(scr, ln, 0);
wclrtoeol(scr);
}
iscursor = 0;
wmove(scr, ln++, 0);
}
}
wmove(scr, 30, 0);
wclrtoeol(scr);
adr = 0x25;
ptr.u64 = read_value(cpu, 0, adr, 7, 0, 0);
wprintw(scr, "ptr1: $%04"PRIX64, ptr.u64);
adr = 0x2D;
ptr.u64 = read_value(cpu, 0, adr, 7, 0, 0);
wprintw(scr, ", ptr2: $%04"PRIX64, ptr.u64);
adr = 0x35;
ptr.u64 = read_value(cpu, 0, adr, 7, 0, 0);
wprintw(scr, ", ptr3: $%04"PRIX64, ptr.u64);
adr = 0x02049A;
ptr.u64 = read_value(cpu, 0, adr, 7, 0, 0);
wprintw(scr, ", idx0: $%04"PRIX64, ptr.u64);
adr = 0x334BA;
ptr.u64 = read_value(cpu, 0, adr, 7, 0, 0);
wprintw(scr, ", valbuf: $%016"PRIX64, ptr.u64);
wmove(scr, 31, 0);
wclrtoeol(scr);
adr = 0x200CA;
ptr.u64 = read_value(cpu, 0, adr, 3, 0, 0);
wprintw(scr, "t_id: $%02X", ptr.u8[0]);
wprintw(scr, ", t_type: $%02X", ptr.u8[1]);
wprintw(scr, ", t_space: $%02X", ptr.u8[2]);
wprintw(scr, ", t_tab: $%02X", ptr.u8[3]);
adr += 4;
ptr.u64 = read_value(cpu, 0, adr, 7, 0, 0);
wprintw(scr, ", t_val: $%"PRIX64, ptr.u64);
adr += 8;
ptr.u64 = read_value(cpu, 0, adr, 7, 0, 0);
wprintw(scr, ", t_str: $%"PRIX64, ptr.u64);
adr += 8;
ptr.u64 = read_value(cpu, 0, adr, 7, 0, 0);
wprintw(scr, ", t_sym: $%"PRIX64, ptr.u64);
}
}