-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
146 lines (120 loc) · 2.84 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdint.h>
#include <stdio.h>
#include "status_code.h"
#include "cpu.h"
#include "file_manager.h"
#include "snapshot.h"
#include "logging.h"
#include "emulator.h"
#include "audio.h"
#include "display.h"
#include "key_input.h"
#include <pthread.h>
#include <unistd.h>
void *cpu_run(void *p)
{
emulator_t *const emulator = (emulator_t *)p;
status_code_t status = STATUS_OK;
while (emulator->state == EMU_MODE_RUNNING)
{
status = emulator_run_frame(emulator);
if (status != STATUS_OK)
{
Log_E("CPU emulation cycle encountered an error: %d", status);
break;
}
else if (emulator->cpu_state.run_mode == RUN_MODE_STOPPED)
{
Log_I("CPU Stopped!");
break;
}
status = handle_snapshot_request(emulator);
if (status != STATUS_OK)
{
Log_E("An error occurred while handling game state request: %d", status);
break;
}
}
emulator->state = EMU_MODE_STOPPED;
return 0;
}
static status_code_t init(emulator_t *const emulator, const char *rom_file)
{
status_code_t status = STATUS_OK;
status = emulator_init(emulator);
if (status != STATUS_OK)
{
Log_E("Failed to init emulator: %d", status);
return status;
}
status = setup_mbc_callbacks(&emulator->mbc);
if (status != STATUS_OK)
{
Log_E("Failed to setup MBC callbacks: %d", status);
return status;
}
status = load_cartridge(&emulator->mbc, rom_file);
if (status != STATUS_OK)
{
Log_E("Failed to load cartridge: %d", status);
return status;
}
status = display_init(emulator->bus_handle.bus_interface, &emulator->ppu);
if (status != STATUS_OK)
{
Log_E("Failed to init display: %d", status);
return status;
}
status = audio_init(&emulator->apu.playback_cb);
if (status != STATUS_OK)
{
Log_E("Failed to init audio device: %d", status);
return status;
}
status = key_input_init(&emulator->joypad.key_update_callback);
if (status != STATUS_OK)
{
Log_E("Failed to init key input: %d", status);
return status;
}
return STATUS_OK;
}
static void cleanup(emulator_t *const emulator)
{
audio_cleanup();
display_cleanup();
unload_cartridge(&emulator->mbc);
}
int main(int __attribute__((unused)) argc, char **argv)
{
status_code_t status;
emulator_t emulator = {0};
status = init(&emulator, argv[1]);
if (status != STATUS_OK)
{
cleanup(&emulator);
return -status;
}
pthread_t t1;
if (pthread_create(&t1, NULL, cpu_run, &emulator))
{
Log_E("Failed to start main thread");
return -1;
}
while (emulator.state == EMU_MODE_RUNNING)
{
usleep(1000);
status = key_input_read();
if (status == STATUS_REQ_EXIT)
{
break;
}
update_display();
}
Log_I("Stopping");
emulator_stop(&emulator);
pthread_join(t1, NULL);
cleanup(&emulator);
Log_I("Exiting: %d", status);
return -status;
}