-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
238 lines (224 loc) · 6.2 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#define WSYNC 2
#define VBLANK 1
#define COLUPF 8
#define PF1 0x000E
#define UPDATE_ZERO_FLAG(x) cpu.flags.z = (x == 0) ? 1:0
#define INS_COMPLETE do{ip++; break;}while(0)
#define MEMORY_SIZE 8192
#define TIA_TICK(cycles) do{for (int i = 0; i < cycles*3; i++) {tv_tick(tv, &cpu.memory, &cpu);}}while(0)
static unsigned char memory[MEMORY_SIZE];
typedef struct {
uint8_t n, v, b, d, i, z, x;
} Flags;
typedef struct {
Flags flags;
unsigned char *memory;
uint8_t X, Y, A, SP;
} Cpu;
#include "tv.c"
int main(void) {
FILE *file = fopen("roms/kernel_15.bin", "rb");
Cpu cpu = {.flags={}, .memory=memory};
TV *tv = new_tv();
uint8_t *byte_stream = (uint8_t*)malloc(sizeof(uint8_t) * 4096);
for (int i = 0; i < 4096; i++) {
byte_stream[i] = fgetc(file);
}
fclose(file);
bool should_exit = false;
uint8_t byte;
int ip = 0;
int wsync_count = 0;
while (ip < 4096) {
byte = byte_stream[ip];
// printf("%x -> %d\n", byte, byte);
switch (byte) {
case 216:
TIA_TICK(2);
cpu.flags.d = 0;
ip++;
break;
case 120:
TIA_TICK(2);
cpu.flags.i = 1;
ip++;
break;
case 162:
//LDX
TIA_TICK(2);
ip++;
cpu.X = byte_stream[ip];
//TODO: sigN flag
UPDATE_ZERO_FLAG(byte_stream[ip]);
ip++;
break;
case 160:
//LDY
TIA_TICK(2);
ip++;
cpu.Y = byte_stream[ip];
//TODO: sigN flag
UPDATE_ZERO_FLAG(byte_stream[ip]);
ip++;
break;
case 154:
//TXS
TIA_TICK(2);
cpu.SP = cpu.X;
UPDATE_ZERO_FLAG(cpu.X);
ip++;
break;
case 232:
// INX
TIA_TICK(2);
cpu.X++;
UPDATE_ZERO_FLAG(cpu.X);
ip++;
break;
case 200:
// INY
TIA_TICK(2);
cpu.Y++;
UPDATE_ZERO_FLAG(cpu.Y);
ip++;
break;
case 136:
// DEY
TIA_TICK(2);
cpu.Y--;
UPDATE_ZERO_FLAG(cpu.Y);
ip++;
break;
case 138:
TIA_TICK(2);
cpu.A = cpu.X;
UPDATE_ZERO_FLAG(cpu.A);
ip++;
break;
case 149:
//STA
TIA_TICK(2);
ip++;
uint8_t address = byte_stream[ip];
cpu.memory[address + cpu.X] = cpu.A;
ip++;
break;
case 132:
//STY
TIA_TICK(2);
ip++;
cpu.memory[byte_stream[ip]] = cpu.Y;
ip++;
break;
case 208:
//BNE
TIA_TICK(2);
ip++;
int8_t jump = byte_stream[ip];
if (cpu.flags.z == 0) {
// ip = jump / 32;
ip += jump+1;
}
else {
ip++;
}
break;
case 169:
TIA_TICK(2);
ip++;
cpu.A = byte_stream[ip];
UPDATE_ZERO_FLAG(cpu.A);
ip++;
break;
case 133:
//STA
ip++;
TIA_TICK(2);
if (byte_stream[ip] == WSYNC) {
wsync_count++;
while (!tv->scanline_complete) {
if (!tv_tick(tv, &cpu.memory, &cpu)) {
should_exit = true;
break;
}
}
}
if (byte_stream[ip] == VBLANK) {
tv->scanline = 262;
}
cpu.memory[byte_stream[ip]] = cpu.A;
ip++;
break;
case 10:
cpu.A <<= 1;
UPDATE_ZERO_FLAG(cpu.A);
ip++;
break;
case 202:
cpu.X--;
TIA_TICK(2);
UPDATE_ZERO_FLAG(cpu.X);
ip++;
break;
case 134:
ip++;
TIA_TICK(2);
cpu.memory[byte_stream[ip]] = cpu.X;
ip++;
break;
case 76:
ip++;
TIA_TICK(2);
ip = byte_stream[ip];
break;
case 224:
ip++;
TIA_TICK(2);
int8_t t = cpu.X - byte_stream[ip];
UPDATE_ZERO_FLAG(t);
ip++;
break;
case 192:
//CPY
ip++;
TIA_TICK(2);
int8_t T = cpu.Y - byte_stream[ip];
UPDATE_ZERO_FLAG(T);
ip++;
break;
case 0xa5:
//LDA
TIA_TICK(3);
ip++;
cpu.A = cpu.memory[byte_stream[ip]];
ip++;
break;
case 234:
//NOP
TIA_TICK(2);
ip++;
break;
case 0xe6:
TIA_TICK(2);
ip++;
cpu.memory[byte_stream[ip]]++;
ip++;
break;
default:
printf("%d\n", byte);
assert(false);
}
if (!tv_tick(tv, &cpu.memory, &cpu) || should_exit) {
should_exit = true;
break;
}
if (should_exit) break;
}
free(byte_stream);
return 0;
}