-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
60 lines (47 loc) · 1.26 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
#include "emulator.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
uint8_t *rom_loader(const char *rom_filename, size_t *rom_len) {
// Open the file in binary mode
FILE *fp = fopen(rom_filename, "rb");
if (fp == NULL) {
printf("File could not be opened.\n");
exit(1);
}
// Checks the size of the file
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
rewind(fp);
*rom_len = (size_t) file_size;
// Allocates the nessasary amount of space
uint8_t *rom = (uint8_t *) malloc(sizeof(uint8_t) * (size_t) file_size);
if (rom == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
// Read the entire file into the rom array
if (fread(rom, 1, (size_t) file_size, fp) != (size_t) file_size) {
printf("Error reading the file.\n");
fclose(fp);
free(rom);
exit(1);
}
fclose(fp);
return rom;
}
int main(void) {
srand((unsigned int) time(0));
size_t rom_len = 0;
const char *rom_file = "./assets/roms/PONG2";
uint8_t *rom_data = rom_loader(rom_file, &rom_len);
// Initialize an emulator instance
Emulator emu = {0};
emulator_init(&emu);
// Load the ROM into the emulator memory and free the temp buffer
emulator_load_rom(&emu, rom_data, rom_len);
free(rom_data);
// Work with the emulator here...
return 0;
}