-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
63 lines (53 loc) · 1.21 KB
/
utils.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
#include <string.h>
#include "utils.h"
void print_reg() {
printf("registers: ");
for (int i = 0; i < 16; i++) {
printf("%X ", reg[i]);
}
printf("\n");
}
void print_stack() {
printf("stack: ");
for (int i = 0; i < 16; i++) {
printf("%X ", stack[i]);
}
printf("\n");
}
void print_pointers() {
printf("pc: %X\n", pc);
printf("sp: %X\n", sp);
}
// load rom into memory at 0x200
void load_rom(char* const filename) {
FILE* fptr = fopen(filename, "rb");
if (fptr != NULL) {
fseek(fptr, 0L, SEEK_END);
uint16_t sz = ftell(fptr);
rewind(fptr);
// load into buffer and close the file
char* buf = (char*)malloc(sz + 1); // use a little bit extra memory
fread(buf, sizeof(char), sz, fptr);
fclose(fptr);
for (int i = 0; i < sz; i++) {
mem[START_ADDR + i] = buf[i];
}
free(buf);
}
}
void init_chip() {
// set the program to the start address of the rom
pc = START_ADDR;
init_gen();
// load fontset into memory
for (int i = 0; i < FONTSET_SZ; i++) {
mem[i] = fontset[i];
}
}
void init_gen() {
// seed the generator
srand(time(NULL));
}
uint8_t gen_rand() {
return (uint8_t) rand();
}