-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcpu.c
177 lines (130 loc) · 3.5 KB
/
cpu.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
#include <conio.h> /* kbhit, getch */
#include "binary.h"
#include "screen.h"
#include "cpu.h"
#include "opcodes.h"
int Accumulator;
int
next_instruction(int counter)
{
/* the next instruction to execute */
if (counter < 255) {
return counter+1;
}
/* else, loop back around to zero (keep in 0 to 255: 255+1=0) */
draw_status(STATUS_ERR | STATUS_RUN); /* overflow */
return 0;
}
int
run_instruction(int *mem, int counter)
{
int next, addr;
/* execute a single instruction from memory. implements a single
fetch/decode/execute model. fetch is the reference from mem,
decode is via the switch block, execute is each case in the
switch block. */
draw_count(counter);
draw_instr(mem[counter]);
tick();
next = next_instruction(counter);
/* some operations require an address from the next program
instruction. these instructions have a "fetch" bit set. */
if (mem[counter] & FETCH_BIT) {
draw_count(next);
draw_instr(mem[next]);
tick();
addr = mem[next]; /* set the address value */
next = next_instruction(next); /* set the next instruction */
}
/* the switch block recognizes each CPU instruction as a CPU
opcode, written in machine language (binary).
technically, there is a NOP instruction, but it's not
listed here because any unrecognized opcode is considered
a NOP instruction. BE CAREFUL OF USING UNRECOGNIZED
OPCODES WITH THE FETCH BIT SET, these will also advance
the next counter, and will result in a programming bug. */
switch(mem[counter]) {
case CPU_STOP:
return -1;
case CPU_RIGHT:
Accumulator = Accumulator>>1;
break;
case CPU_LEFT:
Accumulator = Accumulator<<1;
break;
case CPU_NOT:
Accumulator = ~Accumulator;
break;
case CPU_AND:
Accumulator = Accumulator & mem[addr];
break;
case CPU_OR:
Accumulator = Accumulator | mem[addr];
break;
case CPU_XOR:
Accumulator = Accumulator ^ mem[addr];
break;
case CPU_LOAD:
Accumulator = mem[addr];
break;
case CPU_STORE:
mem[addr] = Accumulator;
break;
case CPU_ADD:
Accumulator += mem[addr];
if (Accumulator > 255) {
Accumulator -= 256; /* keep accum in bounds 0 to 255: 255+1=0 */
draw_status(STATUS_ERR | STATUS_RUN); /* overflow */
}
break;
case CPU_SUB:
Accumulator -= mem[addr];
if (Accumulator < 0) {
Accumulator += 256; /* keep accum in bounds 0 to 255: 0-1=255 */
draw_status(STATUS_ERR | STATUS_RUN); /* underflow */
}
break;
case CPU_GOTO:
next = addr;
break;
case CPU_IFZERO:
if (Accumulator == 0) {
next = addr;
}
}
Accumulator &= 255; /* keep accum in bounds 0 to 255: 11111111 */
/* update the display */
draw_accum(Accumulator);
tick();
return next;
}
int
run_program(int *mem, int start)
{
int key;
int counter;
draw_status(STATUS_RUN); /* run mode */
/* run the program */
Accumulator = 0;
draw_accum(Accumulator);
counter = start;
do {
counter = run_instruction(mem, counter);
/* do we need to abort? */
if (kbhit()) {
key = getch();
if (key==0) {
/* getch returns zero if extended key .. call getch
again to clear it */
getch();
}
else if (key == 27) { /* ESC key */
draw_status(STATUS_ABT | STATUS_RUN);
tick();
return Accumulator;
}
}
} while (counter >= 0);
tick();
return Accumulator;
}