summaryrefslogtreecommitdiff
path: root/disasm.c
blob: da468dd7f473151a6de9300645d30c66dbbaa849 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#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 = "("; /* Falls through. */
		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);
			}
		}
		/*wclrtoeol(scr);
		wprintw(scr, "rega: $%02X, ", addr[0x0A]);
		wprintw(scr, "regb: $%02X, ", addr[0x0B]);
		wprintw(scr, "regc: $%02X, ", addr[0x0C]);
		wprintw(scr, "regd: $%02X, ", addr[0x0D]);
		wprintw(scr, "rege: $%02X, ", addr[0x0E]);
		wprintw(scr, "regf: $%02X, ", addr[0x0F]);
		wprintw(scr, "regg: $%02X", addr[0x10]);*/

		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);


		/*
		wmove(scr, 47, 0);
		wclrtoeol(scr);
		wprintw(scr, "a: $%02X,",  addr[0x0A]);
		wprintw(scr, " b: $%02X,", addr[0x0B]);
		wprintw(scr, " c: $%02X,", addr[0x0C]);
		wprintw(scr, " d: $%02X,", addr[0x0D]);
		wprintw(scr, " e: $%02X,", addr[0x0E]);
		wprintw(scr, " f: $%02X",  addr[0x0F]);
		tmpad += 8;
		adr = read_value(cpu, 0, tmpad, 7, 0, 0);
		line_idx = 0;
		wprintw(scr, "cmd_buf:");
		for (uint8_t i = 0; i < 1; i++) {
			wmove(scr, ln++, 0);
			wclrtoeol(scr);
			line_idx = (i << 4)+(i << 6);
			for (uint8_t j = 0; j < 0x50; j++) {
				wprintw(scr, "%02X", addr[adr+j+line_idx]);
			}
			wprintw(scr, ", i: %02X", i);
		}*/

	}
}