Blame view

debug.c 7.45 KB
1
2
3
4
5
6
7
8
//
// Created by imanol on 12/26/16.
//

#include <malloc.h>
#include <string.h>
#include "debug.h"
9
//uint32_t cycle_count = 0;
10
11
12
13
14
uint8_t breakpoint = 1;
uint16_t breakpoints[0xFF];
uint16_t nbpoints = 0;
char lastchoice = 0;
15
void load_state(char *filename)
16
{
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    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);
38
39
}
40
void save_state(char *filename)
41
{
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    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);
57
58
59
60
}

void query()
{
61
    //TODO discard stdin
62
63
64
    uint8_t end = 0;
    uint16_t bpoint;
    uint16_t mempos;
65
66
67
68
    char input[100] = {0};
    char filename[32];
    uint8_t reg;
    uint8_t value;
69
70
71
    while(!end)
    {
        printf("\n> ");
72
73
74
        char command = 0;
        fgets(input,100,stdin);
        command = input[0];
75
76
77
78
79
80
81
82
83
84
85
        if (command != '\n')
        {
            lastchoice = command;
        }
        else
        {
            command = lastchoice;
        }
        switch (command)
        {
            case 'b':
86
                sscanf(input,"b %d\n",&bpoint);
87
88
89
90
91
                fprintf(stderr,"Set breakpoint %d to %d",nbpoints,bpoint);
                breakpoints[nbpoints] = bpoint;
                nbpoints = nbpoints + 1 % 0xFF;
                break;
            case 'd':
92
                sscanf(input,"d %d\n",&bpoint);
93
                fprintf(stderr,"Deleted breakpoint %d",bpoint);
94
                breakpoints[bpoint] = 0;
95
96
97
98
99
100
101
102
103
104
105
106
                break;
            case 'c':
                breakpoint = 0;
                end = 1;
                break;
            case 'n':
                end = 1;
                break;
            case 'r':
                print_regs();
                break;
            case 'm':
107
                sscanf(input,"m %d\n",&mempos);
108
109
                fprintf(stderr,"%d: %02x\n",mempos,mem[mempos]);
                break;
110
111
112
113
114
115
116
117
118
119
120
            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':
121
                //FIXME ALREADY
122
123
124
125
                sscanf(input,"w %d %d\n",&reg,&value);
                fprintf(stderr,"Setting register %d to %d",reg,value);
                regs[reg] = value;
                break;
126
127
128
129
130
131
            default:
                break;
        }
    }
}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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;
}
156
void translate_args(uint16_t a1, uint16_t a2, uint16_t a3, char *arg1, char *arg2, char *arg3)
157
{
158
159
160
161
162
163
    if(a1 > MAX_INT)
    {
        sprintf(arg1,"r%d",a1-(MAX_INT+1));
    }
    else
    {
164
        sprintf(arg1,"%02X",a1);
165
166
167
168
169
170
171
172
    }

    if(a2 > MAX_INT)
    {
        sprintf(arg2,"r%d",a2-(MAX_INT+1));
    }
    else
    {
173
        sprintf(arg2,"%02X",a2);
174
175
176
177
178
179
180
181
    }

    if(a3 > MAX_INT)
    {
        sprintf(arg3,"r%d",a3-(MAX_INT+1));
    }
    else
    {
182
        sprintf(arg3,"%02X",a3);
183
184
185
186
187
188
189
190
191
192
    }

}

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);
193
194
195
    switch(opcode)
    {
        case HALT:
196
            fprintf(stderr, "HALT\n");
197
198
            break;
        case MOV:
199
            fprintf(stderr, "MOV %s %s\n", arg1, arg2);
200
201
            break;
        case PUSH:
202
            fprintf(stderr, "PUSH %s\n", arg1);
203
204
            break;
        case POP:
205
            fprintf(stderr, "POP %s\n", arg1);
206
207
            break;
        case TEQ:
208
            fprintf(stderr, "TEQ %s %s %s\n", arg1, arg2, arg3);
209
210
            break;
        case TGT:
211
            fprintf(stderr, "TGT %s %s %s\n", arg1, arg2, arg3);
212
213
            break;
        case JMP:
214
            fprintf(stderr, "JMP %s\n", arg1);
215
216
            break;
        case JNZ:
217
            fprintf(stderr, "JNZ %s %s\n", arg1, arg2);
218
219
            break;
        case JZ:
220
            fprintf(stderr, "JZ %s %s\n", arg1, arg2);
221
222
            break;
        case ADD:
223
            fprintf(stderr, "ADD %s %s %s\n", arg1, arg2, arg3);
224
225
            break;
        case MUL:
226
            fprintf(stderr, "MUL %s %s %s\n", arg1, arg2, arg3);
227
228
            break;
        case MOD:
229
            fprintf(stderr, "MOD %s %s %s\n", arg1, arg2, arg3);
230
231
            break;
        case AND:
232
            fprintf(stderr, "AND %s %s %s\n", arg1, arg2, arg3);
233
234
            break;
        case OR:
235
            fprintf(stderr, "OR %s %s %s\n", arg1, arg2, arg3);
236
237
            break;
        case NOT:
238
            fprintf(stderr, "NOT %s %s\n", arg1, arg2);
239
240
            break;
        case LOAD:
241
            fprintf(stderr, "LOAD %s %s\n", arg1, arg2);
242
243
            break;
        case STOR:
244
            fprintf(stderr, "STOR %s %s\n", arg1, arg2);
245
246
            break;
        case CALL:
247
            fprintf(stderr, "CALL %s\n", arg1);
248
249
            break;
        case RET:
250
            fprintf(stderr, "RET\n");
251
252
            break;
        case OUT:
253
254
255
256
257
258
259
260
            if (a1 <= MAX_INT)
            {
                fprintf(stderr, "OUT '%c'\n",a1);
            }
            else
            {
                fprintf(stderr, "OUT %s\n", arg1);
            }
261
262
            break;
        case IN:
263
            fprintf(stderr,"IN %s\n",arg1);
264
265
            break;
        case NOP:
Imanol-Mikel Barba Sabariego authored
266
            fprintf(stderr,"NOP\n");
267
268
            break;
        default:
269
            fprintf(stderr,"UNK %02X\n", opcode);
270
271
272
273
274
            break;
    }
}
275
276
277
278
279
280
281
282
void debug_program()
{
    memset(breakpoints,0x00,0xFF);
    uint16_t arg1;
    uint16_t arg2;
    uint16_t arg3;
    for(;;)
    {
283
284
285
286
287
        if(in_breakpoint(pc))
        {
            breakpoint = 1;
            fprintf(stderr,"Breakpoint hit @ pc=%02X",pc);
        }
288
        if(in_breakpoint(pc) || breakpoint)
289
        {
290
            breakpoint = 1;
291
            fprintf(stderr, "%02X: ", pc);
292
293
            print_instruction(mem[pc], mem[pc+1], mem[pc+2], mem[pc+3]);
            query();
294
295
        }
        uint16_t opcode = fetch_debug();
296
297
        decode_instruction(opcode, &arg1, &arg2, &arg3);
        if (execute_instruction(opcode, arg1, arg2, arg3))
298
        {
299
            return;
300
301
302
        }
    }
}