/
main.c
182 lines (155 loc) · 3.74 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
#include <stdio.h>
#include <string.h>
#include <SDL2/SDL.h>
#include "cpu.h"
#include "screen.h"
CPU* c = NULL;
int stepping = 0;
int cpu_running = 1;
int cpu_thread_func(void* args)
{
FILE* f;
while(cpu_running) {
if(stepping) {
printf("IP: 0x%04X, ADDRESS: 0x%04X\n", c->ip, c->address);
printf("ACC: 0x%02X, X: 0x%02X, Y: 0x%02X\n", c->acc, c->x, c->y);
printf("HLT: %s\n", c->hlt ? "true" : "false");
get_input:
printf(": ");
char in = getchar();
switch (in) {
case 'x':
case 'X':
case 'q':
case 'Q':
cpu_running = 0;
return 0;
break;
case 'd':
case 'D':
f = fopen("memdump.bin", "wb");
fwrite(c->mem, sizeof(u8), 0x10000, f);
fclose(f);
goto get_input;
break;
}
}
if(c->hlt) {
cpu_running = 0;
return 0;
}
cpu_cycle(c);
}
return 0;
}
int main(int argc, char** argv)
{
int i;
int window = 1;
int showhelp = 0;
int fps = 0;
int width = 640;
int height = 360;
int fullscreen = 0;
if(argc > 2) {
for(i = 2; i < argc; i++) {
char* dup = strdup(argv[i]);
char* tok = strtok(dup, "=");
if(strcmp(argv[i], "-s") == 0) {
stepping = 1;
}else if(strcmp(argv[i], "-noscreen") == 0) {
window = 0;
}else if(strcmp(argv[i], "-h") == 0) {
showhelp = 1;
}else if(strcmp(argv[i], "-fps") == 0) {
fps = 1;
}else if(strcmp(tok, "-width") == 0) {
tok = strtok(NULL, "=");
width = strtol(tok, NULL, 0);
}else if(strcmp(tok, "-height") == 0) {
tok = strtok(NULL, "=");
height = strtol(tok, NULL, 0);
}else if(strcmp(argv[i], "-f") == 0) {
fullscreen = 1;
}
else {
printf("Ignoring unknown argument '%s'. Use '-h' for more information\n", argv[i]);
}
}
}
if(argc < 2 | showhelp) {
printf("Usage: ccpu <binary> [options]\n\nOptions:\n\t-s - Stepping mode\n\t-noscreen - No screen/window\n\t-fps - Output fps\n\t-width=w - Set window width\n\t-height=h - Set window height\n\t-f - Fullscreen mode\n");
return -1;
}
FILE* f = fopen(argv[1], "rb");
if(f == NULL) {
printf("Failed to open '%s'\n", argv[1]);
return -1;
}
fseek(f, 0, SEEK_END);
int length = ftell(f);
rewind(f);
u8* buffer = (u8*)malloc(sizeof(u8)*length);
int read = fread(buffer, sizeof(u8), length, f);
fclose(f);
if(read != length) {
printf("Failed to read file '%s'\n", argv[1]);
return -1;
}
f = fopen("rom.bin", "rb");
if(f == NULL) {
printf("Could not open 'rom.bin'\n");
return -1;
}
fseek(f, 0, SEEK_END);
int rom_length = ftell(f);
if(rom_length > 0x1000) {
printf("'rom.bin' is %04X bytes long, but can not be longer than 0x100 bytes\n", rom_length);
return -1;
}
rewind(f);
u8* rom_buffer = (u8*)malloc(sizeof(u8)*rom_length);
read = fread(rom_buffer, sizeof(u8), rom_length, f);
if(read != rom_length) {
printf("Failed to read 'rom.bin'\n");
return -1;
}
fclose(f);
c = cpu_init(0x1000);
for(i = 0; i < length; i++) {
c->mem[c->ip+i] = buffer[i];
}
for(i = 0; i < rom_length; i++) {
c->mem[0xF000+i] = rom_buffer[i];
}
c->ip = 0xF000;
SDL_Thread* ccpu_thread = SDL_CreateThread(cpu_thread_func, "ccpu_thread", NULL);
if(window) {
Screen* s = screen_init(width, height, 640, 360, 8, 12, 16, 16, fullscreen);
Uint32 now = SDL_GetTicks();
Uint32 lastTime = now;
Uint32 fpsCounterTime = now;
Uint32 interval = 16;
int frames = 0;
while(!screen_closerequested(s)) {
now = SDL_GetTicks();
if(now - lastTime > interval) {
screen_update(s, c);
frames++;
lastTime = now;
}
if(now - fpsCounterTime > 1000) {
if(fps) printf("fps %d\n", frames);
frames = 0;
fpsCounterTime = now;
}
screen_pollevents(s, c);
if(!cpu_running) break;
}
screen_cleanup(s);
SDL_DetachThread(ccpu_thread);
}else {
SDL_WaitThread(ccpu_thread, NULL);
}
return 0;
}