-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
118 lines (104 loc) · 3.44 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
/* Copyright (C) 2021 Alice Shelton <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <SDL.h>
#include <time.h>
#include "invaders.h"
#include "sdlFunctions.h"
int main(int argc, char *argv[]) {
if(argc == 1) {
printf("Invalid number of command line arguments!\nUsage: invaders [Path to ROM]\n");
return 1;
}
//Opens a file pointer to the location specified and reads it as binary
FILE *rom = fopen(argv[1], "rb");
if(rom == NULL) {
printf("Error: File specified does not exist!\nUsage: invaders [Path to ROM]\n");
return 2;
}
fseek(rom, 0, SEEK_END);
if(ftell(rom) > 0xFFFF) {
printf("Error: File too large to fit in memory of 8080\n");
fclose(rom);
return 3;
}
fseek(rom, 0, SEEK_SET);
invaders *machine = initializeMachine(rom);
fclose(rom);
SDL_Window *window;
SDL_Texture *texture;
SDL_Renderer *renderer;
graphicsInit(&window, &renderer, &texture, 4);
clock_t freq;
while(1) {
freq = clock();
machine->cpu->pendingInterrupt = 0xD7;
while(machine->cpu->cycles < 16667) {
executeInstruction(machine->cpu);
if(machine->cpu->halt) {
printf("CPU has halted!\n");
graphicsEnd(&window, &renderer, &texture);
freeMachine(machine);
return 1;
}
}
machine->cpu->cycles -= 16667;
machine->cpu->pendingInterrupt = 0xCF;
while(machine->cpu->cycles < 16667) {
executeInstruction(machine->cpu);
if(machine->cpu->halt) {
printf("CPU has halted!\n");
graphicsEnd(&window, &renderer, &texture);
freeMachine(machine);
return 1;
}
}
machine->cpu->cycles -= 16667;
updateFrame(machine, renderer, texture);
if(queryKey(0)) {
machine->inputTwo |= 32;
} else {
machine->inputTwo &= 0b11011111;
}
if(queryKey(1)) {
machine->inputTwo |= 64;
} else {
machine->inputTwo &= 0b10111111;
}
if(queryKey(2)) {
machine->inputTwo |= 1;
} else {
machine->inputTwo &= 0xFE;
}
if(queryKey(3)) {
machine->inputTwo |= 16;
} else {
machine->inputTwo &= 0b11101111;
}
if(queryKey(4)) {
machine->inputTwo |= 4;
} else {
machine->inputTwo &= 0b11111011;
}
if(queryKey(5)) {
machine->cpu->halt = 1;
}
freq = clock() - freq;
if((double) freq / CLOCKS_PER_SEC < 1 / 60.0) {
SDL_Delay(((1.0 / 60.0) - ((double) freq / CLOCKS_PER_SEC)) * 1000);
}
}
}