summaryrefslogtreecommitdiff
path: root/disasm.c
blob: c055d07169938f348fb50667487c16fcb1b3e284 (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
#include "sux.h"
#include "disasm.h"

void disasm(struct sux *cpu, uint64_t *operands, uint8_t lines, uint8_t opcode, uint8_t prefix, uint8_t thread) {
	uint64_t value   = operands[0];
	uint64_t address = operands[1];
	uint64_t tmpaddr = operands[2];
	char postfix[3];
	char op[4];
	op[0] = opname[opcode][0];
	op[1] = opname[opcode][1];
	op[2] = opname[opcode][2];
	op[3] = '\0';
	switch(1 << (prefix >> 4)) {
		case 1: postfix[0] =   0; postfix[1] =   0; postfix[2] = 0; break;
		case 2: postfix[0] = '.'; postfix[1] = 'W'; postfix[2] = 0; break;
		case 4: postfix[0] = '.'; postfix[1] = 'D'; postfix[2] = 0; break;
		case 8: postfix[0] = '.'; postfix[1] = 'Q'; postfix[2] = 0; break;
	}
	switch (optype[opcode]) {
		case IMPL: wprintw(scr, "%s\r" , opname[opcode]); break;
		case IMM:
			switch(1 << (prefix >> 4)) {
				case 1: wprintw(scr, "%s #$%02X\r" , op, value); break;
				case 2: wprintw(scr, "%s%s #$%04X\r" , op, postfix, value); break;
				case 4: wprintw(scr, "%s%s #$%08X\r" , op, postfix, value); break;
				case 8: wprintw(scr, "%s%s #$%016"PRIX64"\r" , op, postfix, value); break;
			}
			break;
		case ZM:
		case ZMX:
		case ZMY:
			switch (optype[opcode]) {
				case ZMX: tmpaddr = address - cpu->x[thread]; break;
				case ZMY: tmpaddr = address - cpu->y[thread]; break;
			}
			switch ((prefix & 0x0C) >> 2) {
				case 3: wprintw(scr, "%s%s $%08X%s\r" , op, postfix, tmpaddr, (optype[opcode] == ZM) ? "" : ((optype[opcode] == ZMX) ? ", x" : ", y")); break;
				case 2: wprintw(scr, "%s%s $%014"PRIX64"%s\r" , op, postfix, tmpaddr, (optype[opcode] == ZM) ? "" : ((optype[opcode] == ZMX) ? ", x" : ", y")); break;
				case 1: wprintw(scr, "%s%s $%06X%s\r" , op, postfix, tmpaddr, (optype[opcode] == ZM) ? "" : ((optype[opcode] == ZMX) ? ", x" : ", y")); break;
				case 0: wprintw(scr, "%s%s $%02X%s\r" , op, postfix, tmpaddr, (optype[opcode] == ZM) ? "" : ((optype[opcode] == ZMX) ? ", x" : ", y")); break;
			}
			break;
		case IND:
		case INDX:
		case INDY:
			switch ((prefix & 0x0C) >> 2) {
				case 3: wprintw(scr, "%s%s ($%08X%s\r" , op, postfix, tmpaddr, (optype[opcode] == IND) ? ")" : ((optype[opcode] == INDX) ? ", x)" : "), y")); break;
				case 2: wprintw(scr, "%s%s ($%012"PRIX64"%s\r" , op, postfix, tmpaddr, (optype[opcode] == IND) ? ")" : ((optype[opcode] == INDX) ? ", x)" : "), y")); break;
				case 1: wprintw(scr, "%s%s ($%06X%s\r" , op, postfix, tmpaddr, (optype[opcode] == IND) ? ")" : ((optype[opcode] == INDX) ? ", x)" : "), y")); break;
				case 0: wprintw(scr, "%s%s ($%02X%s\r" , op, postfix, tmpaddr, (optype[opcode] == IND) ? ")" : ((optype[opcode] == INDX) ? ", x)" : "), y")); break;
			}
			break;
		case ABS:
			tmpaddr = address;
			switch ((prefix & 0x0C) >> 2) {
				case 3: wprintw(scr, "%s%s $%016"PRIX64"\r" , op, postfix, tmpaddr);	break;
				case 2: wprintw(scr, "%s%s $%014"PRIX64"\r" , op, postfix, tmpaddr);	break;
				case 1: wprintw(scr, "%s%s $%010"PRIX64"\r" , op, postfix, tmpaddr);	break;
				case 0: wprintw(scr, "%s%s $%04" PRIX64"\r" , op, postfix, tmpaddr);	break;
			}
			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]);
	}
	if (subdbg) {
		uint8_t ln = 33;
		uint16_t line_idx = 0;
		uint16_t tmpad = 0x2000;
		int row, col;
		uint8_t iscursor = 0;
		uint64_t ptr;
		uint8_t adr;
		wmove(scr, 30, 0);
		adr = 0x1F;
		ptr = (uint64_t)addr[adr+0] << 0x00 | (uint64_t)addr[adr+1] << 0x08 | (uint64_t)addr[adr+2] << 0x10 | (uint64_t)addr[adr+3] << 0x18 |
		      (uint64_t)addr[adr+4] << 0x20 | (uint64_t)addr[adr+5] << 0x28 | (uint64_t)addr[adr+6] << 0x30 | (uint64_t)addr[adr+7] << 0x38;
		wprintw(scr, "ptr1: $%04"PRIX64, ptr);
		adr = 0x27;
		ptr = (uint64_t)addr[adr+0] << 0x00 | (uint64_t)addr[adr+1] << 0x08 | (uint64_t)addr[adr+2] << 0x10 | (uint64_t)addr[adr+3] << 0x18 |
		      (uint64_t)addr[adr+4] << 0x20 | (uint64_t)addr[adr+5] << 0x28 | (uint64_t)addr[adr+6] << 0x30 | (uint64_t)addr[adr+7] << 0x38;
		wprintw(scr, ", ptr2: $%04"PRIX64, ptr);
		adr = 0x2F;
		ptr = (uint64_t)addr[adr+0] << 0x00 | (uint64_t)addr[adr+1] << 0x08 | (uint64_t)addr[adr+2] << 0x10 | (uint64_t)addr[adr+3] << 0x18 |
		      (uint64_t)addr[adr+4] << 0x20 | (uint64_t)addr[adr+5] << 0x28 | (uint64_t)addr[adr+6] << 0x30 | (uint64_t)addr[adr+7] << 0x38;
		wprintw(scr, ", ptr3: $%04"PRIX64, ptr);
		if (address == CTRL_ADDR || addr[STEP_ADDR]) {
			mvwprintw(scr, 29, 0, "address: $%04"PRIX64", scr_row: %02u, scr_col: %02u, scr_str: %02u, scr_end: %02u\r", address, addr[0], addr[1], addr[0x1C], addr[0x1D]);
			mvwprintw(scr, 32, 0, "bitabl: %02X%02X%02X%02X%02X%02X%02X%02X"
						"%02X%02X%02X%02X%02X%02X%02X%02X"
						, addr[0x1000], addr[0x1001], addr[0x1002], addr[0x1003], addr[0x1004], addr[0x1005], addr[0x1006], addr[0x1007]
						, addr[0x1008], addr[0x1009], addr[0x100A], addr[0x100B], addr[0x100C], addr[0x100D], addr[0x100E], addr[0x100F]);
			mvwprintw(scr, ln++, 0, "buffer:\r");
			wmove(scr, ln++, 0);
			for (uint8_t i = 0; i < 10; i++) {
				line_idx = (i << 6) + (i << 4);
				for (uint8_t j = 0; j < 0x50; j++) {
					wprintw(scr, "%02X", addr[tmpad+j+line_idx]);
						if ((addr[0]+addr[0x1C]) == i && addr[1] == j) {
							iscursor=1;
							getyx(scr,row, col);
							wmove(scr, ln++, 0);
							wclrtoeol(scr);
							wmove(scr, row+1, col-2);
							wprintw(scr, "/\\\r");
							wmove(scr, row, col);
						}
				}
				wprintw(scr, ", i: %02X", i);
				if (!iscursor) {
					wmove(scr, ln, 0);
					wclrtoeol(scr);
				}
				iscursor = 0;
				wmove(scr, ln++, 0);
			}
		}
		/*if (address == 0x4000 || tmpaddr == 0x4000 || addr[STEP_ADDR]) {
			ln = 46;
			tmpad = 0x4000;
			line_idx = 0;
			mvwprintw(scr, ln++, 0, "cmd_buf:");
			for (uint8_t i = 0; i < 5; i++) {
				wmove(scr, ln++, 0);
				line_idx = (i << 4)+(i << 6);
				for (uint8_t j = 0; j < 0x50; j++) {
					wprintw(scr, "%02X", addr[tmpad+j+line_idx]);
				}
				wprintw(scr, ", i: %02X", i);
			}
		}*/
	}
}