PancakeNESEmu
A homebrewed NES Emulator written in C
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1#include "UI/tui.h"
2#include "core/cpu.h"
3#include "core/mem.h"
4#include "macros.h"
5#include "rom_reader/reader.h"
6
7#include "UI/ui.h"
8#include <getopt.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12
13extern int opterr;
14
15int main(int argc, char** argv) {
16
17 int opt;
18 rom* my_rom = NULL;
19
20 enum ui_mode mode = OTHER;
21 bool debug = FALSE;
22 opterr = 0;
23 const struct option options[] = {{"opt-arg", optional_argument, 0, 't'},
24 {"req-arg", required_argument, 0, 'i'},
25 {"no-arg", no_argument, 0, 'd'},
26 {NULL, 0, 0, '\0'}};
27
28 while ((opt = getopt_long(argc, argv, "di:t::", options, NULL)) != -1) {
29
30 switch (opt) {
31 case 'i':
32 if (optarg == NULL) {
33 goto err;
34 }
35 printf("Filepath: %s\n", optarg);
36 my_rom = open_rom(optarg);
37 break;
38 case 't':
39 // en_US.UTF-8
40 if (optarg == NULL) {
41 init_nscreen("en_US.UTF-8");
42 } else {
43 init_nscreen(optarg);
44 }
45 mode = TUI;
46 break;
47 case 'd':
48 debug = TRUE;
49 break;
50 default:
51 err:
52 fprintf(stderr, "Usage: %s [-t[locale]] [-i path/to/rom]\n", argv[0]);
53 break;
54 }
55 }
56
57 unsigned char ch = 0;
58
59 CPU* cpu = (CPU*)malloc(sizeof(CPU));
60 memory mem = init_memory();
61
62 init_cpu(cpu, mem);
63 reset_memory(cpu->mem);
64
65 // cpu->flags = 0xff;
66
67 if (debug) {
68 display_rom(my_rom);
69 }
70
71 if (mode == TUI) {
72 while (ch != (char)KEY_F(1)) {
73 printw("\nPress 's' to go to the next step.\n");
75
76 ch = getch();
77 erase();
78
79 if (ch == (char)KEY_F(1)) {
80 printw("Press any key to leave");
81 } else if (ch == 's') {
82 printw("New step !\n");
83 if (my_rom == NULL) {
84 INC_X(cpu);
85 } else {
86 step_cpu(cpu);
87 }
88 }
89 }
91 }
92
93 free_cpu(cpu);
94
95 if (my_rom != NULL) {
96 free_rom(my_rom);
97 }
98
99 return EXIT_SUCCESS;
100}
errcode_t step_cpu(CPU *cpu)
Function that step a clock cycle.
Definition cpu.c:1190
void init_cpu(CPU *cpu, memory mem)
Procedure that initialize the CPU to its boot/reset state.
Definition cpu.c:9
#define INC_X(processor)
Procedure that increments the X register.
Definition cpu.h:406
void free_cpu(CPU *cpu)
Procedure that free the memory used by the CPU and the memory array.
Definition cpu.c:21
#define EXIT_SUCCESS
Definition macros.h:5
int main(int argc, char **argv)
Definition main.c:15
int opterr
data * memory
Definition mem.h:6
void reset_memory(memory mem)
This procedure set the entire memory to 0.
Definition mem.c:19
memory init_memory()
This function allocate on the heap a memory of exactly TOTAL_MEMORY_SIZE.
Definition mem.c:7
void free_rom(rom *r)
Frees the rom struct.
Definition reader.c:75
void display_rom(rom *r)
Procedure that displays the content of the rom in hexadecimal.
Definition reader.c:59
rom * open_rom(char *filepath)
A function that open the NES rom at a specified path.
Definition reader.c:7
This structure will be used to represent the state of the Central Processing Unit (CPU) of our emulat...
Definition cpu.h:27
memory mem
A direct access to the Emulator Memory.
Definition cpu.h:39
Definition reader.h:8
void printw_cpu_state(CPU *cpu)
Procedure that displays the current state of the cpu in a ncurses window.
Definition tui.c:14
void init_nscreen(const char *local_format)
Procedure that init the ncurses screen.
Definition tui.c:5
void end_nscreen()
Procedure that kill the current ncurses window.
Definition tui.c:23
ui_mode
Definition ui.h:6
@ TUI
Definition ui.h:8
@ OTHER
Definition ui.h:7