-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
251 lines (205 loc) · 6.98 KB
/
Copy pathmain.cpp
File metadata and controls
251 lines (205 loc) · 6.98 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <stdio.h>
#include <SDL2/SDL.h>
#include <time.h>
#include <chrono>
#include "Display.h"
#include "MMU.h"
#include "CPU.h"
#include "SoundChip.h"
using namespace std::chrono;
void OpenFileError (const char* Filename) {
printf ("\n[ERR] There was an error opening the file: %s\n", Filename);
exit(1);
}
void LoadROMData (MMU* mmu, const char* Filename, uint16_t Address) {
FILE* ROMData;
ROMData = fopen (Filename, "rb");
if (ROMData == 0)
OpenFileError (Filename);
fseek (ROMData, 0, SEEK_END);
uint16_t ROMSize = ftell (ROMData);
fclose (ROMData);
ROMData = fopen (Filename, "rb");
uint8_t* Buffer = (uint8_t*) malloc(ROMSize);
if (fread(Buffer, 1, ROMSize, ROMData) != ROMSize)
OpenFileError (Filename);
mmu->LoadInMemory (Buffer, Address, ROMSize);
free (Buffer);
fclose (ROMData);
}
uint64_t GetCurrentTime (time_point <high_resolution_clock>* StartTime) {
auto TimeDifference = high_resolution_clock::now () - *StartTime;
return duration_cast <microseconds> (TimeDifference).count (); // Get Microseconds
}
int main (int argc, char** argv) {
if (argc < 3) {
printf ("Please specify -ROMType ROMFileName:\n");
printf ("\t- %s -g Demos/invaders\t\t// For Games, input the name behind the file extension.\n", argv[0]);
printf ("\t- %s -p Demos/CPUTEST.COM\t\t\t// For Programs\n", argv[0]);
return 1;
}
printf ("[INFO] Initializing SDL...");
if (SDL_Init (SDL_INIT_EVERYTHING) < 0) {
printf ("\n[ERR] SDL failed to initialize: %s", SDL_GetError());
return 1;
}
printf ("OK\n");
const uint8_t ConsoleMode = strcmp(argv[1], "-p") == 0;
MMU mmu (ConsoleMode);
CPU cpu (&mmu, ConsoleMode);
Display* Disp = NULL;
SoundChip* soundchip = NULL;
uint8_t SoundOn = 0;
printf ("[INFO] Reading ROM...");
if (ConsoleMode) {
LoadROMData (&mmu, argv[2], 0x0100);
} else {
LoadROMData (&mmu, argv[2], 0x0000);
Disp = new Display("Intel 8080", 224, 256, 2);
SoundOn = 1;
if (argc == 4) {
if (strcmp(argv[3], "--no-sound") == 0)
SoundOn = 0;
}
}
printf ("OK\n");
if (SoundOn)
soundchip = new SoundChip;
// Timers + Loop Variables
SDL_Event ev;
const uint8_t *Keyboard = SDL_GetKeyboardState(NULL);
uint64_t CurrentTime = 0;
uint64_t LastThrottle = 0;
uint64_t LastDraw = 0;
uint64_t LastDebugPrint = 0;
uint64_t LastInput = 0;
uint64_t LastSound = 0;
uint8_t DrawFull = 0;
uint8_t IsPlayingSound = 0;
uint32_t ClocksPerMS = 3000; // 3 MHz Sweet Spot
uint64_t LastClockCount = 0;
uint64_t ClockCompensation = 0;
auto StartTime = high_resolution_clock::now ();
printf ("\n");
while (!cpu.Halt) {
if (!ConsoleMode) {
CurrentTime = GetCurrentTime (&StartTime);
if (CurrentTime - LastThrottle <= 4000) { // Check every 4 ms
if (cpu.ClockCount - LastClockCount >= (ClocksPerMS << 2) + ClockCompensation) { // Throttle CPU
uint32_t usToSleep = 4000 - (CurrentTime - LastThrottle); // Sleep for the rest of the 4 ms
timespec req = {0, usToSleep * 1000};
nanosleep (&req, (timespec *) NULL);
LastThrottle = GetCurrentTime (&StartTime);
LastClockCount = cpu.ClockCount;
ClockCompensation = (ClocksPerMS * ((LastThrottle - CurrentTime) - usToSleep)) / 1000;
}
} else { // Host CPU is slower or equal to i8080
LastThrottle = CurrentTime;
LastClockCount = cpu.ClockCount;
}
if (CurrentTime - LastDraw > 1000000 / 120 || LastDraw > CurrentTime) { // 120 Hz - Manage Screen (Half screen in a cycle, then end screen in another)
LastDraw = CurrentTime;
if (DrawFull) {
Disp->Update (mmu.VRAM);
cpu.Interrupt (1);
} else
cpu.Interrupt (0);
DrawFull = 1 - DrawFull;
}
if (CurrentTime - LastInput > 1000000 / 30 || LastInput > CurrentTime) { // 30 Hz - Manage Events
LastInput = CurrentTime;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT) {
cpu.Halt = 1;
}
}
// Cleanup before input
cpu.InPort[0] &= 0b10001111;
cpu.InPort[1] &= 0b10001000;
cpu.InPort[2] &= 0b10001011;
if (Keyboard[SDL_SCANCODE_SPACE]) { // Fire
cpu.InPort[0] |= 1 << 4;
cpu.InPort[1] |= 1 << 4;
cpu.InPort[2] |= 1 << 4; // P2
}
if (Keyboard[SDL_SCANCODE_A] || Keyboard[SDL_SCANCODE_LEFT]) { // Left
cpu.InPort[0] |= 1 << 5;
cpu.InPort[1] |= 1 << 5;
cpu.InPort[2] |= 1 << 5; // P2
}
if (Keyboard[SDL_SCANCODE_D] || Keyboard[SDL_SCANCODE_RIGHT]) { // Right
cpu.InPort[0] |= 1 << 6;
cpu.InPort[1] |= 1 << 6;
cpu.InPort[2] |= 1 << 6; // P2
}
if (Keyboard[SDL_SCANCODE_RETURN]) // Credit
cpu.InPort[1] |= 1 << 0;
if (Keyboard[SDL_SCANCODE_1]) // 1P Start
cpu.InPort[1] |= 1 << 2;
if (Keyboard[SDL_SCANCODE_2]) // 2P Start
cpu.InPort[1] |= 1 << 1;
if (Keyboard[SDL_SCANCODE_DELETE]) // Tilt
cpu.InPort[2] |= 1 << 2;
}
if (SoundOn && (CurrentTime - LastSound > 1000000 / 60 || LastSound > CurrentTime)) { // 30 Hz Audio
LastSound = CurrentTime;
// Check Audio
if (cpu.OutPort[3] & (1 << 0)) { // UFO - Loop
soundchip->PlaySound (0);
}
if (cpu.OutPort[3] & (1 << 1)) { // Shoot
if ((IsPlayingSound & (1 << 1)) == 0)
soundchip->PlaySound (1);
IsPlayingSound |= 1 << 1;
} else
IsPlayingSound &= 0b11111101;
if (cpu.OutPort[3] & (1 << 2)) { // Player Death
if ((IsPlayingSound & (1 << 2)) == 0)
soundchip->PlaySound (2);
IsPlayingSound |= 1 << 2;
} else
IsPlayingSound &= 0b11111011;
if (cpu.OutPort[3] & (1 << 3)) { // Invader Death
if ((IsPlayingSound & (1 << 3)) == 0)
soundchip->PlaySound (3);
IsPlayingSound |= 1 << 3;
} else
IsPlayingSound &= 0b11110111;
if (cpu.OutPort[5] & (1 << 0)) { // Fleet 1
if ((IsPlayingSound & (1 << 4)) == 0)
soundchip->PlaySound (4);
IsPlayingSound |= 1 << 4;
} else
IsPlayingSound &= 0b11101111;
if (cpu.OutPort[5] & (1 << 1)) { // Fleet 2
if ((IsPlayingSound & (1 << 5)) == 0)
soundchip->PlaySound (5);
IsPlayingSound |= 1 << 5;
} else
IsPlayingSound &= 0b11011111;
if (cpu.OutPort[5] & (1 << 2)) { // Fleet 3
if ((IsPlayingSound & (1 << 6)) == 0)
soundchip->PlaySound (6);
IsPlayingSound |= 1 << 6;
} else
IsPlayingSound &= 0b10111111;
if (cpu.OutPort[5] & (1 << 3)) { // Fleet 4
if ((IsPlayingSound & (1 << 7)) == 0)
soundchip->PlaySound (7);
IsPlayingSound |= 1 << 7;
} else
IsPlayingSound &= 0b01111111;
}
if (CurrentTime - LastDebugPrint > 5000000 || LastDebugPrint > CurrentTime) { // 5 Seconds - Manage occasional prints
float Duration = (CurrentTime - LastDebugPrint) / 1000000;
uint64_t ClocksPerSec = cpu.ClockCount / Duration;
LastDebugPrint = CurrentTime;
printf ("[INFO] Running at @%f MHz (%lu Instructions Per Second)\n", (float) ClocksPerSec / 1000000, (uint64_t) (cpu.InstructionCount / Duration));
cpu.ClockCount = 0;
cpu.InstructionCount = 0;
}
}
cpu.Clock();
}
printf ("\n\n[INFO] CPU Stopped.\n");
}