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