forked from rofl0r/gnuboy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathemu.c
More file actions
208 lines (160 loc) · 3.87 KB
/
emu.c
File metadata and controls
208 lines (160 loc) · 3.87 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "defs.h"
#include "regs.h"
#include "hw.h"
#include "loader.h"
#include "cpu.h"
#include "mem.h"
#include "lcd.h"
#include "rc.h"
#include "rtc.h"
#include "sys.h"
#include "configfile_fk.h"
#include "sound.h"
#include "cpu.h"
static int framelen = 16743;
static int framecount;
char *prog_name;
char *load_state_file = NULL;
int load_state_slot = -1;
char *quick_save_file_extension = "quicksave";
char *mRomName = NULL;
char *mRomPath = NULL;
char *quick_save_file = NULL;
char *cfg_file_default = NULL;
char *cfg_file_rom = NULL;
char *cfg_file_default_name = "default_config";
char *cfg_file_extension = "fkcfg";
int mQuickSaveAndPoweroff=0;
rcvar_t emu_exports[] =
{
RCV_INT("framelen", &framelen),
RCV_INT("framecount", &framecount),
RCV_END
};
/* Quick save and turn off the console */
void quick_save_and_poweroff()
{
FILE *fp;
printf("Save Instant Play file\n");
Uint32 start = SDL_GetTicks();
/* Send command to cancel any previously scheduled powerdown */
fp = popen(SHELL_CMD_POWERDOWN_HANDLE, "r");
if (fp == NULL)
{
/* Countdown is still ticking, so better do nothing
than start writing and get interrupted!
*/
printf("Failed to cancel scheduled shutdown\n");
exit(0);
}
pclose(fp);
printf("============== Cancel time %d\n", SDL_GetTicks() - start);
/* Save */
state_file_save(quick_save_file);
/* Perform Instant Play save and shutdown */
execlp(SHELL_CMD_INSTANT_PLAY, SHELL_CMD_INSTANT_PLAY,
"save", prog_name, "--loadStateFile", quick_save_file, mRomName, NULL);
/* Should not be reached */
printf("Failed to perform Instant Play save and shutdown\n");
/* Exit Emulator */
exit(0);
}
void emu_init()
{
}
/*
* emu_reset is called to initialize the state of the emulated
* system. It should set cpu registers, hardware registers, etc. to
* their appropriate values at powerup time.
*/
void emu_reset()
{
hw_reset();
lcd_reset();
cpu_reset();
mbc_reset();
sound_reset();
}
void emu_step()
{
cpu_emulate(cpu.lcdc);
}
/* This mess needs to be moved to another module; it's just here to
* make things work in the mean time. */
void *sys_timer();
void emu_run()
{
void *timer = sys_timer();
int delay;
vid_begin();
lcd_begin();
/* Load slot */
if (load_state_slot != -1)
{
printf("LOADING FROM SLOT %d...\n", load_state_slot+1);
state_load(load_state_slot);
printf("LOADED FROM SLOT %d\n", load_state_slot+1);
load_state_slot = -1;
}
/* Load file */
else if (load_state_file != NULL)
{
printf("LOADING FROM FILE %s...\n", load_state_file);
state_file_load(load_state_file);
printf("LOADED FROM SLOT %s\n", load_state_file);
load_state_file = NULL;
}
/* Load quick save file */
else if(access( quick_save_file, F_OK ) != -1)
{
printf("Found quick save file: %s\n", quick_save_file);
int resume = launch_resume_menu_loop();
if (resume == RESUME_YES)
{
printf("Resume game from quick save file: %s\n", quick_save_file);
state_file_load(quick_save_file);
}
else {
printf("Reset game\n");
/* Remove quicksave file if present */
if (remove(quick_save_file) == 0)
{
printf("Deleted successfully: %s\n", quick_save_file);
}
else {
printf("Unable to delete the file: %s\n", quick_save_file);
}
}
}
/* Main emulation loop */
for (;;)
{
cpu_emulate(2280);
while (R_LY > 0 && R_LY < 144)
emu_step();
vid_end();
rtc_tick();
sound_mix();
if (!pcm_submit())
{
delay = framelen - sys_elapsed(timer);
sys_sleep(delay);
sys_elapsed(timer);
}
/* Quick save and poweroff */
if(mQuickSaveAndPoweroff){
quick_save_and_poweroff();
mQuickSaveAndPoweroff = 0;
}
doevents();
vid_begin();
if (framecount) { if (!--framecount) die("finished\n"); }
if (!(R_LCDC & 0x80))
cpu_emulate(32832);
while (R_LY > 0) /* wait for next frame */
emu_step();
}
}