Blame view

cpu.c 6.85 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
//
// Created by imanol on 12/25/16.
//
5
#include <stdlib.h>
Imanol-Mikel Barba Sabariego authored
6
7
#include "cpu.h"
8
void mov(uint16_t dst, uint16_t src)
9
10
11
{
    if(src > MAX_INT)
    {
12
        regs[dst % (MAX_INT+1)] = regs[src % (MAX_INT+1)];
13
14
15
    }
    else
    {
16
        regs[dst % (MAX_INT+1)] = src;
17
18
19
    }
}
20
void push(uint16_t src)
21
22
23
24
25
26
27
28
29
30
31
{
    if(src > MAX_INT)
    {
        stack_push(regs[src % (MAX_INT+1)]);
    }
    else
    {
        stack_push(src);
    }
}
32
void pop(uint16_t dst)
33
{
34
    uint16_t value = stack_pop();
35
36
37
38
39
    if(STACK_FAULT)
    {
        fprintf(stderr,"CRITICAL: STACK VIOLATION\n");
        core_dump();
    }
Imanol-Mikel Barba Sabariego authored
40
    regs[dst % (MAX_INT+1)] = value;
41
42
}
43
void teq(uint16_t dst, uint16_t a, uint16_t b)
44
45
46
47
48
49
50
51
52
{
    if(a > MAX_INT)
    {
        a = regs[a % (MAX_INT+1)];
    }
    if(b > MAX_INT)
    {
        b = regs[b % (MAX_INT+1)];
    }
53
    regs[dst % (MAX_INT+1)] = (uint16_t)(a == b);
54
55
}
56
void tgt(uint16_t dst, uint16_t a, uint16_t b)
57
58
59
60
61
62
63
64
65
{
    if(a > MAX_INT)
    {
        a = regs[a % (MAX_INT+1)];
    }
    if(b > MAX_INT)
    {
        b = regs[b % (MAX_INT+1)];
    }
66
    regs[dst % (MAX_INT+1)] = (uint16_t)(a > b);
67
68
}
69
void jmp(uint16_t dst)
70
71
72
73
74
75
76
77
78
79
80
{
    if(dst > MAX_INT)
    {
        pc = regs[dst % (MAX_INT+1)];
    }
    else
    {
        pc = dst;
    }
}
81
void jnz(uint16_t cond, uint16_t dst)
82
{
Imanol-Mikel Barba Sabariego authored
83
84
    if(cond > MAX_INT)
    {
85
        cond = regs[cond % (MAX_INT+1)];
Imanol-Mikel Barba Sabariego authored
86
    }
87
88
89
90
91
92
    if(cond)
    {
        jmp(dst);
    }
}
93
void jz(uint16_t cond, uint16_t dst)
94
{
Imanol-Mikel Barba Sabariego authored
95
96
    if(cond > MAX_INT)
    {
97
        cond = regs[cond % (MAX_INT+1)];
Imanol-Mikel Barba Sabariego authored
98
    }
99
100
101
102
103
104
    if(!cond)
    {
        jmp(dst);
    }
}
105
void add(uint16_t dst, uint16_t a, uint16_t b)
106
107
108
109
110
111
112
113
114
{
    if(a > MAX_INT)
    {
        a = regs[a % (MAX_INT+1)];
    }
    if(b > MAX_INT)
    {
        b = regs[b % (MAX_INT+1)];
    }
115
    regs[dst % (MAX_INT+1)] = (uint16_t)((a + b) % (MAX_INT+1));
116
117
}
118
void mul(uint16_t dst, uint16_t a, uint16_t b)
119
120
121
122
123
124
125
126
127
{
    if(a > MAX_INT)
    {
        a = regs[a % (MAX_INT+1)];
    }
    if(b > MAX_INT)
    {
        b = regs[b % (MAX_INT+1)];
    }
128
    regs[dst % (MAX_INT+1)] = (uint16_t)((a * b) % (MAX_INT+1));
129
130
}
131
void mod(uint16_t dst, uint16_t a, uint16_t b)
132
133
134
135
136
137
138
139
140
141
142
143
{
    if(a > MAX_INT)
    {
        a = regs[a % (MAX_INT+1)];
    }
    if(b > MAX_INT)
    {
        b = regs[b % (MAX_INT+1)];
    }
    regs[dst % (MAX_INT+1)] = (a % b);
}
144
void and(uint16_t dst, uint16_t a, uint16_t b)
145
146
147
148
149
150
151
152
153
154
155
156
{
    if(a > MAX_INT)
    {
        a = regs[a % (MAX_INT+1)];
    }
    if(b > MAX_INT)
    {
        b = regs[b % (MAX_INT+1)];
    }
    regs[dst % (MAX_INT+1)] = (a & b);
}
157
void or(uint16_t dst, uint16_t a, uint16_t b)
158
159
160
161
162
163
164
165
166
167
168
169
{
    if(a > MAX_INT)
    {
        a = regs[a % (MAX_INT+1)];
    }
    if(b > MAX_INT)
    {
        b = regs[b % (MAX_INT+1)];
    }
    regs[dst % (MAX_INT+1)] = (a | b);
}
170
void not(uint16_t dst, uint16_t src)
171
172
173
174
175
{
    if(src > MAX_INT)
    {
        src = regs[src % (MAX_INT+1)];
    }
176
    regs[dst % (MAX_INT+1)] = (uint16_t)(~src & 0x7FFF);
177
178
}
179
void load(uint16_t dst, uint16_t src)
180
181
182
183
184
{
    if(src > MAX_INT)
    {
        src = regs[src % (MAX_INT+1)];
    }
185
    regs[dst % (MAX_INT+1)] = mem[src];
186
187
}
188
void stor(uint16_t dst, uint16_t src)
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{
    if(src > MAX_INT)
    {
        src = regs[src % (MAX_INT+1)];
    }
    if(dst > MAX_INT)
    {
        mem[regs[dst % (MAX_INT+1)]] = src;
    }
    else
    {
        mem[dst] = src;
    }
}
204
void call(uint16_t dst)
205
206
207
208
209
210
211
212
213
214
215
{
    stack_push(pc);
    jmp(dst);
}

uint8_t ret()
{
    pc = stack_pop();
    return STACK_FAULT;
}
216
void out(uint16_t src)
217
{
218
219
220
221
222
    if(src > MAX_INT)
    {
        src = regs[src % (MAX_INT+1)];
    }
    putchar(src);
223
224
}
225
void in(uint16_t dst)
226
{
227
228
229
230
231
    if(!input_buffer_size())
    {
        read_input();
    }
    char c = get_input();
232
    regs[dst % (MAX_INT + 1)] = (uint16_t)c;
233
234
}
Imanol-Mikel Barba Sabariego authored
235
236
237
238
239
void nop()
{
    return;
}
240
uint16_t fetch()
Imanol-Mikel Barba Sabariego authored
241
{
242
    uint16_t value = mem[pc++];
Imanol-Mikel Barba Sabariego authored
243
244
245
    return value;
}
246
247
248
249
250
251

void decode_instruction(uint16_t opcode, uint16_t *arg1, uint16_t *arg2, uint16_t *arg3)
{
    switch(opcode)
    {
        case HALT:
Imanol-Mikel Barba Sabariego authored
252
253
        case RET:
        case NOP:
254
255
256
257
            break;
        case PUSH:
        case POP:
        case JMP:
Imanol-Mikel Barba Sabariego authored
258
259
260
        case CALL:
        case OUT:
        case IN:
261
262
            *arg1 = fetch();
            break;
Imanol-Mikel Barba Sabariego authored
263
        case MOV:
264
265
        case JNZ:
        case JZ:
Imanol-Mikel Barba Sabariego authored
266
267
268
        case NOT:
        case LOAD:
        case STOR:
269
270
271
            *arg1 = fetch();
            *arg2 = fetch();
            break;
Imanol-Mikel Barba Sabariego authored
272
273
        case TEQ:
        case TGT:
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
        case ADD:
        case MUL:
        case MOD:
        case AND:
        case OR:
            *arg1 = fetch();
            *arg2 = fetch();
            *arg3 = fetch();
            break;
        default:
            break;
    }
}

uint8_t execute_instruction(uint16_t opcode, uint16_t arg1, uint16_t arg2, uint16_t arg3)
{
    switch(opcode)
    {
        case HALT:
            return 1;
        case MOV:
            mov(arg1,arg2);
            break;
        case PUSH:
            push(arg1);
            break;
        case POP:
            pop(arg1);
            break;
        case TEQ:
            teq(arg1,arg2,arg3);
            break;
        case TGT:
            tgt(arg1,arg2,arg3);
            break;
        case JMP:
            jmp(arg1);
            break;
        case JNZ:
            jnz(arg1,arg2);
            break;
        case JZ:
            jz(arg1,arg2);
            break;
        case ADD:
            add(arg1,arg2,arg3);
            break;
        case MUL:
            mul(arg1,arg2,arg3);
            break;
        case MOD:
            mod(arg1,arg2,arg3);
            break;
        case AND:
            and(arg1,arg2,arg3);
            break;
        case OR:
            or(arg1,arg2,arg3);
            break;
        case NOT:
            not(arg1,arg2);
            break;
        case LOAD:
            load(arg1,arg2);
            break;
        case STOR:
            stor(arg1,arg2);
            break;
        case CALL:
            call(arg1);
            break;
        case RET:
346
            return ret();
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
        case OUT:
            out(arg1);
            break;
        case IN:
            in(arg1);
            break;
        case NOP:
            nop();
            break;
        default:
            fprintf(stderr,"CRITICAL: UNKNOWN OPCODE %2x FOUND IN %2x\n",opcode,pc);
            core_dump();
            break;
    }
    return 0;
}
364
void start_execution()
Imanol-Mikel Barba Sabariego authored
365
{
366
367
368
    uint16_t arg1;
    uint16_t arg2;
    uint16_t arg3;
369
370
371
    for(;;)
    {
372
        uint16_t opcode = fetch();
373
374
        decode_instruction(opcode, &arg1, &arg2, &arg3);
        if(execute_instruction(opcode, arg1, arg2, arg3))
375
        {
376
            return;
377
378
        }
    }
Imanol-Mikel Barba Sabariego authored
379
380
}
381
382
383
void core_dump()
{
    print_regs();
384
385
386
387
388
389
390
391
392
393
394
395
    uint16_t memdump[MEMSIZE];
    uint16_t *stackdump;
    dump_memory(memdump);
    uint32_t stacksize = stack_dump(&stackdump);

    FILE *fpm = fopen("synacor.mem","w");
    FILE *fps = fopen("synacor.stack","w");
    fwrite(memdump,MEMSIZE,sizeof(uint16_t),fpm);
    fwrite(stackdump,stacksize,sizeof(uint16_t),fps);
    fclose(fpm);
    fclose(fps);
396
    exit(1);
397
}
398
399