|
1
2
3
4
5
6
7
|
//
// Created by imanol on 12/26/16.
//
#include <malloc.h>
#include <string.h>
#include "debug.h"
|
|
8
|
#include <signal.h>
|
|
9
|
|
|
10
|
uint32_t cycle_count = 0;
|
|
11
12
13
14
|
uint8_t breakpoint = 1;
uint16_t breakpoints[0xFF];
uint16_t nbpoints = 0;
char lastchoice = 0;
|
|
15
16
17
18
19
20
|
uint8_t terminate = 0;
void to_debugger(int signal)
{
if(signal == SIGINT)
{
|
|
21
|
fprintf(stderr,"CTRL+C hit. Falling to debugger (Press enter if in the middle of input)\n");
|
|
22
23
24
25
26
|
breakpoint = 1;
return;
}
core_dump();
}
|
|
27
|
|
|
28
|
void load_state(char *filename)
|
|
29
|
{
|
|
30
31
32
33
|
FILE *state = fopen(filename,"r");
uint16_t regsdump[NUM_REGISTERS+1];
uint16_t memdump[MEMSIZE];
uint16_t *stackdump;
|
|
34
35
|
uint32_t stacksize;
long stackpos;
|
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
fread(regsdump,sizeof(uint16_t),NUM_REGISTERS+1,state);
fread(memdump,sizeof(uint16_t),MEMSIZE,state);
stackpos = ftell(state);
fseek(state,0,SEEK_END);
stacksize = (ftell(state) - stackpos)/sizeof(uint16_t);
stackdump = calloc(stacksize,sizeof(uint16_t));
fseek(state,stackpos,SEEK_SET);
fread(stackdump,sizeof(uint16_t),stacksize,state);
fclose(state);
load_regs(regsdump);
load_memory(memdump);
stack_load(stackdump,stacksize);
free(stackdump);
|
|
52
53
|
}
|
|
54
|
void save_state(char *filename)
|
|
55
|
{
|
|
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
FILE *state = fopen(filename,"w");
uint16_t regsdump[NUM_REGISTERS+1];
uint16_t memdump[MEMSIZE];
uint16_t *stackdump;
uint32_t stacksize;
dump_regs(regsdump);
dump_memory(memdump);
stacksize = stack_dump(&stackdump);
fwrite(regsdump,sizeof(uint16_t),NUM_REGISTERS+1,state);
fwrite(memdump,sizeof(uint16_t),MEMSIZE,state);
fwrite(stackdump,sizeof(uint16_t),stacksize,state);
fclose(state);
free(stackdump);
|
|
71
72
73
74
75
|
}
void query()
{
uint8_t end = 0;
|
|
76
77
|
uint16_t bpoint;
uint16_t mempos;
|
|
78
79
|
char input[100] = {0};
char filename[32];
|
|
80
81
|
uint8_t reg;
uint16_t value;
|
|
82
83
84
|
while(!end)
{
printf("\n> ");
|
|
85
86
87
|
char command = 0;
fgets(input,100,stdin);
command = input[0];
|
|
88
|
//FIXME: Input is not validated in any way. Crashes *will* occur on misuse. Have fun!
|
|
89
90
91
92
93
94
95
96
97
98
99
|
if (command != '\n')
{
lastchoice = command;
}
else
{
command = lastchoice;
}
switch (command)
{
case 'b':
|
|
100
101
|
sscanf(input,"b %hx\n",&bpoint);
fprintf(stderr,"Set breakpoint %hu to %hx",nbpoints,bpoint);
|
|
102
|
breakpoints[nbpoints] = bpoint;
|
|
103
|
nbpoints = (uint16_t)(nbpoints + 1 % 0xFF);
|
|
104
105
|
break;
case 'd':
|
|
106
107
|
sscanf(input,"d %hu\n",&bpoint);
fprintf(stderr,"Deleted breakpoint %hu",bpoint);
|
|
108
|
breakpoints[bpoint] = 0;
|
|
109
110
111
112
113
114
115
116
117
118
119
120
|
break;
case 'c':
breakpoint = 0;
end = 1;
break;
case 'n':
end = 1;
break;
case 'r':
print_regs();
break;
case 'm':
|
|
121
|
sscanf(input,"m %hX\n",&mempos);
|
|
122
|
fprintf(stderr,"%02X: %02x\n",mempos,mem[mempos]);
|
|
123
|
break;
|
|
124
125
126
127
128
129
130
131
132
133
134
|
case 's':
sscanf(input,"s %s\n",filename);
save_state(filename);
fprintf(stderr,"Saved state as %s",filename);
break;
case 'l':
sscanf(input,"l %s\n",filename);
load_state(filename);
fprintf(stderr,"Loaded state as %s",filename);
break;
case 'w':
|
|
135
136
|
sscanf(input,"w r%hhu %hu\n",®,&value);
fprintf(stderr,"Setting r%hhu = %02X",reg,value);
|
|
137
138
|
regs[reg] = value;
break;
|
|
139
140
141
142
|
case 'q':
terminate = 1;
end = 1;
break;
|
|
143
144
145
146
147
148
|
default:
break;
}
}
}
|
|
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
uint8_t in_breakpoint(uint16_t pc)
{
uint8_t i;
for(i = 0; i < nbpoints; i++)
{
if(breakpoints[i] == pc)
{
return 1;
}
}
return 0;
}
uint16_t fetch_debug()
{
uint16_t value = mem[pc++];
return value;
}
|
|
168
|
void translate_args(uint16_t a1, uint16_t a2, uint16_t a3, char *arg1, char *arg2, char *arg3)
|
|
169
|
{
|
|
170
171
172
173
174
175
|
if(a1 > MAX_INT)
{
sprintf(arg1,"r%d",a1-(MAX_INT+1));
}
else
{
|
|
176
|
sprintf(arg1,"%02X",a1);
|
|
177
178
179
180
181
182
183
184
|
}
if(a2 > MAX_INT)
{
sprintf(arg2,"r%d",a2-(MAX_INT+1));
}
else
{
|
|
185
|
sprintf(arg2,"%02X",a2);
|
|
186
187
188
189
190
191
192
193
|
}
if(a3 > MAX_INT)
{
sprintf(arg3,"r%d",a3-(MAX_INT+1));
}
else
{
|
|
194
|
sprintf(arg3,"%02X",a3);
|
|
195
196
197
198
199
200
201
202
203
204
|
}
}
void print_instruction(uint16_t opcode, uint16_t a1, uint16_t a2, uint16_t a3)
{
char arg1[10];
char arg2[10];
char arg3[10];
translate_args(a1,a2,a3,arg1,arg2,arg3);
|
|
205
206
207
|
switch(opcode)
{
case HALT:
|
|
208
|
fprintf(stderr, "HALT\n");
|
|
209
210
|
break;
case MOV:
|
|
211
|
fprintf(stderr, "MOV %s %s\n", arg1, arg2);
|
|
212
213
|
break;
case PUSH:
|
|
214
|
fprintf(stderr, "PUSH %s\n", arg1);
|
|
215
216
|
break;
case POP:
|
|
217
|
fprintf(stderr, "POP %s\n", arg1);
|
|
218
219
|
break;
case TEQ:
|
|
220
|
fprintf(stderr, "TEQ %s %s %s\n", arg1, arg2, arg3);
|
|
221
222
|
break;
case TGT:
|
|
223
|
fprintf(stderr, "TGT %s %s %s\n", arg1, arg2, arg3);
|
|
224
225
|
break;
case JMP:
|
|
226
|
fprintf(stderr, "JMP %s\n", arg1);
|
|
227
228
|
break;
case JNZ:
|
|
229
|
fprintf(stderr, "JNZ %s %s\n", arg1, arg2);
|
|
230
231
|
break;
case JZ:
|
|
232
|
fprintf(stderr, "JZ %s %s\n", arg1, arg2);
|
|
233
234
|
break;
case ADD:
|
|
235
|
fprintf(stderr, "ADD %s %s %s\n", arg1, arg2, arg3);
|
|
236
237
|
break;
case MUL:
|
|
238
|
fprintf(stderr, "MUL %s %s %s\n", arg1, arg2, arg3);
|
|
239
240
|
break;
case MOD:
|
|
241
|
fprintf(stderr, "MOD %s %s %s\n", arg1, arg2, arg3);
|
|
242
243
|
break;
case AND:
|
|
244
|
fprintf(stderr, "AND %s %s %s\n", arg1, arg2, arg3);
|
|
245
246
|
break;
case OR:
|
|
247
|
fprintf(stderr, "OR %s %s %s\n", arg1, arg2, arg3);
|
|
248
249
|
break;
case NOT:
|
|
250
|
fprintf(stderr, "NOT %s %s\n", arg1, arg2);
|
|
251
252
|
break;
case LOAD:
|
|
253
|
fprintf(stderr, "LOAD %s %s\n", arg1, arg2);
|
|
254
255
|
break;
case STOR:
|
|
256
|
fprintf(stderr, "STOR %s %s\n", arg1, arg2);
|
|
257
258
|
break;
case CALL:
|
|
259
|
fprintf(stderr, "CALL %s\n", arg1);
|
|
260
261
|
break;
case RET:
|
|
262
|
fprintf(stderr, "RET\n");
|
|
263
264
|
break;
case OUT:
|
|
265
266
267
268
269
270
271
272
|
if (a1 <= MAX_INT)
{
fprintf(stderr, "OUT '%c'\n",a1);
}
else
{
fprintf(stderr, "OUT %s\n", arg1);
}
|
|
273
274
|
break;
case IN:
|
|
275
|
fprintf(stderr,"IN %s\n",arg1);
|
|
276
277
|
break;
case NOP:
|
|
278
|
fprintf(stderr,"NOP\n");
|
|
279
280
|
break;
default:
|
|
281
|
fprintf(stderr,"UNK %02X\n", opcode);
|
|
282
283
284
285
286
|
break;
}
}
|
|
287
288
|
void debug_program()
{
|
|
289
|
signal(SIGINT, to_debugger);
|
|
290
291
292
293
294
295
|
memset(breakpoints,0x00,0xFF);
uint16_t arg1;
uint16_t arg2;
uint16_t arg3;
for(;;)
{
|
|
296
|
cycle_count++;
|
|
297
298
299
|
if(in_breakpoint(pc))
{
breakpoint = 1;
|
|
300
|
fprintf(stderr,"Breakpoint hit @ pc=%X\n",pc);
|
|
301
|
}
|
|
302
|
if(breakpoint)
|
|
303
|
{
|
|
304
|
fprintf(stderr, "cycle: %d\n", cycle_count);
|
|
305
|
fprintf(stderr, "%02X: ", pc);
|
|
306
|
print_instruction(mem[pc], mem[pc+1], mem[pc+2], mem[pc+3]);
|
|
307
308
309
310
|
if(stdin_length())
{
read_input();
}
|
|
311
|
query();
|
|
312
313
314
315
|
if(terminate)
{
return;
}
|
|
316
317
|
}
uint16_t opcode = fetch_debug();
|
|
318
319
|
decode_instruction(opcode, &arg1, &arg2, &arg3);
if (execute_instruction(opcode, arg1, arg2, arg3))
|
|
320
|
{
|
|
321
|
return;
|
|
322
323
324
|
}
}
}
|