-
Notifications
You must be signed in to change notification settings - Fork 0
/
uxndump.c
256 lines (203 loc) · 4.38 KB
/
uxndump.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// SPDX-License-Identifier: ISC
// copyright (C) 2022 SASANO Takayoshi <uaa@uaa.org.uk>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
static uint8_t *buf;
static size_t filesize;
static char *opecodeX[] = {
NULL, "INC", "POP", "NIP", "SWP", "ROT", "DUP", "OVR",
"EQU", "NEQ", "GTH", "LTH", "JMP", "JCN", "JSR", "STH",
"LDZ", "STZ", "LDR", "STR", "LDA", "STA", "DEI", "DEO",
"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT",
};
static char *opecode0[] = {
"BRK", "20 ", "40 ", "60 ", "LIT", "LIT", "LIT", "LIT",
};
#define HEXDUMP_WIDTH 3
#define INSTRUCTION_STRING_LEN 3
#define MODIFIER_STRING_LEN 3
#define OFFSET 0x100
static int put_space(char *out, int len)
{
int i;
/* simply put space (for padding) */
for (i = 0; i < len; i++)
*out++ = ' ';
*out = '\0';
return i;
}
/* hex values should use lower-case */
static int put_hex8(char *out, uint8_t value)
{
return sprintf(out, "%02x ", value);
}
static int put_hex16(char *out, uint16_t value)
{
return sprintf(out, "%04x ", value);
}
static int put_hexdump(char *out, uint8_t *bin, int len)
{
int i, n, l;
n = 0;
for (i = 0; i < len; i++) {
l = put_hex8(out, *bin++);
n += l;
out += l;
}
return n;
}
static int put_asciidump(char *out, uint8_t *bin, int len)
{
int i;
for (i = 0; i < len; i++) {
/* avoid comment identifier, '(' and ')' */
*out++ = (isascii(*bin) && !iscntrl(*bin) &&
(*bin != '(') && (*bin != ')')) ?
*bin : '.';
bin++;
}
*out = '\0';
return i;
}
static int get_immediate_size(uint8_t opecode)
{
if ((opecode & 0x9f) == 0x80) {
/* LIT */
return (opecode & 0x20) ? 2 : 1;
} else {
/* others */
return 0;
}
}
static int put_instruction(char *p, uint8_t opecode)
{
int op;
char *p0, *m;
p0 = p;
op = opecode & 0x1f;
if (op) {
m = opecodeX[op];
} else {
m = opecode0[opecode >> 5];
if (opecode & 0x80) {
/* LIT: k modifier is no need to display */
opecode &= ~0x80;
} else {
/* BRK/reserved instructions: no modifier */
opecode &= ~0xe0;
}
}
strcpy(p, m);
p += strlen(m);
if (opecode & 0x20) *p++ = '2';
if (opecode & 0x80) *p++ = 'k';
if (opecode & 0x40) *p++ = 'r';
*p = '\0';
return p - p0;
}
static void put_disassemble(char *line, uint8_t *code, int immediate_size)
{
char *p;
/* instruction */
p = line;
line += put_instruction(line, *code);
/* immediate (if needed) */
if (immediate_size) {
line += put_space(line,
INSTRUCTION_STRING_LEN +
MODIFIER_STRING_LEN + 1 - (line - p));
switch (immediate_size) {
case 1:
line += put_hex8(line, *(code + 1));
break;
case 2:
line += put_hex16(line,
(*(code + 1) << 8) | *(code + 2));
break;
default:
break;
}
}
}
static void put_dump(char *line, uint8_t *code, int len)
{
int i;
for (i = 0; i < len; i++)
line += put_hex8(line, code[i]);
}
static int build_line(char *line, uint16_t address, uint8_t *code, int remain)
{
int len, dump;
char *p;
len = get_immediate_size(*code) + 1;
dump = 0;
/* if remains is not enough, use hex dump instead of disassemble */
if (remain < len) {
len = remain;
dump = 1;
}
/* address, hex dump and ascii dump is commented-out */
line += sprintf(line, "( ");
line += put_hex16(line, address + OFFSET);
p = line;
line += put_hexdump(line, code, len);
line += put_space(line, (HEXDUMP_WIDTH * 3) - (line - p));
p = line;
line += put_asciidump(line, code, len);
line += put_space(line, HEXDUMP_WIDTH - (line - p));
line += sprintf(line, " )\t");
if (dump)
put_dump(line, code, len);
else
put_disassemble(line, code, len - 1);
/* the result of put_xxxx() terminates with '\0' */
return len;
}
static void disassemble_all(void)
{
int i;
char line[128];
printf("|%04x\n", OFFSET);
for (i = 0; i < filesize;) {
i += build_line(line, i, &buf[i], filesize - i);
printf("%s\n", line);
}
}
static int loadfile(char *filename)
{
int v = -1;
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL)
goto fin0;
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);
buf = malloc(filesize);
if (buf == NULL)
goto fin1;
fread(buf, filesize, 1, fp);
v = 0;
fin1:
fclose(fp);
fin0:
return v;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("%s [filename]\n", argv[0]);
goto fin0;
}
if (loadfile(argv[1])) {
printf("file open error\n");
goto fin0;
}
disassemble_all();
free(buf);
fin0:
return 0;
}