-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot.c
40 lines (30 loc) · 913 Bytes
/
boot.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structs_and_consts.h"
cpu_state cpu_state_from_rom_file(const char* rom_path) {
cpu_state state;
if (!load_rom(&state, rom_path)) {
fprintf(stderr, "Error reading ROM - either file doesn't exist"
" or error reading memory\n");
exit(1);
}
state.pc = ROM_START;
state.sp = 0;
state.interrupt_enable = 0;
state.shift_left_byte = state.shift_right_byte = state.shift_offset = 0;
return state;
}
uint8_t load_rom(struct cpu_state *state, const char *filepath) {
FILE *f = fopen(filepath, "r");
if (!f) {
return 0;
}
fseek(f, 0, SEEK_END);
unsigned int size = (unsigned int)ftell(f);
fseek(f, 0, SEEK_SET);
fread(&state->memory[ROM_START], 1, size, f);
state->rom_size = size;
fclose(f);
return 1;
}