-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnullDC.cpp
More file actions
275 lines (240 loc) · 8.42 KB
/
Copy pathnullDC.cpp
File metadata and controls
275 lines (240 loc) · 8.42 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// nullDC.cpp : Defines the entry point for the console application.
//
//initialse Emu
#include "types.h"
#include "dc/mem/_vmem.h"
#include "dc/sh4/sh4_opcode_list.h"
#include "stdclass.h"
#include "dc/dc.h"
#include "config/config.h"
#include "plugins/plugin_manager.h"
#include "cl/cl.h"
#include "plugs/ImgReader/elf_loader.h" // ELF boot support
// Frameskipping (preparation : working on it)
int g_current_frameskip = 0; // 0 = no skip, 1 = skip 1 frame, 2 = skip 2 frame
int g_frame_counter = 0;
__settings settings;
// ---------------------------------------------------------------------------
// ELF boot state
// Set by main___() if the selected file is a .elf.
// Consumed by RunDC() after Reset() so the entry point wins over the BIOS vector.
// ---------------------------------------------------------------------------
static bool g_elf_pending = false;
static u32 g_elf_entry = 0;
// ---------------------------------------------------------------------------
// Helper: case-insensitive check for a file extension
// ---------------------------------------------------------------------------
static bool HasExtension(const char* path, const char* ext)
{
if (!path || !ext) return false;
size_t plen = strlen(path);
size_t elen = strlen(ext);
if (plen < elen) return false;
const char* tail = path + plen - elen;
// Compare ignoring case
for (size_t i = 0; i < elen; i++)
{
char a = tail[i] >= 'A' && tail[i] <= 'Z' ? tail[i] + 32 : tail[i];
char b = ext[i] >= 'A' && ext[i] <= 'Z' ? ext[i] + 32 : ext[i];
if (a != b) return false;
}
return true;
}
//mainloop
int RunDC()
{
if(settings.dynarec.Enable)
{
Get_Sh4Recompiler(&sh4_cpu);
printf("[nullDC.cpp] Using Recompiler (Dynarec Enable)\n");
}
else
{
Get_Sh4Interpreter(&sh4_cpu);
printf("[nullDC.cpp] Using Interpreter\n");
}
// -----------------------------------------------------------------------
// ELF boot: patch SH4 context AFTER the CPU back-end is chosen but
// BEFORE Start_DC() begins executing instructions.
//
// Start_DC() calls sh4_cpu.Reset() internally which sets nextpc to the
// BIOS reset vector (0xA0000000). We need to override that after Reset
// but before the first instruction fetch.
//
// The hook we use: elf_apply_entry() is called from inside dc_run_hook()
// which is a weak/callback called by dc.cpp right after Reset() and
// before the main execution loop starts. If your dc.cpp does not have
// that hook yet, see the comment block below for the alternative.
// -----------------------------------------------------------------------
if (g_elf_pending)
{
// dc.cpp must call dc_elf_pre_run_hook() between Reset() and Run().
// We register our intent here; the actual register write happens in
// the hook (see dc_elf_pre_run_hook() below).
printf("[nullDC.cpp] [ELF] Boot mode active – entry will be applied after CPU reset\n");
}
Start_DC(); //this call is blocking ...
return 0;
}
// ---------------------------------------------------------------------------
// dc_elf_pre_run_hook – called by dc.cpp after sh4_cpu.Reset() and before
// the execution loop starts.
//
// ADD ONE LINE to your dc.cpp Start_DC() function, right after sh4_cpu.Reset()
// is called:
//
// extern void dc_elf_pre_run_hook();
// dc_elf_pre_run_hook();
//
// That's the only change needed in dc.cpp.
// ---------------------------------------------------------------------------
extern "C" void dc_elf_pre_run_hook()
{
if (g_elf_pending)
{
elf_apply_entry(g_elf_entry);
g_elf_pending = false;
// Flush the dynarec block cache AND the PPC code cache.
// ngen_ResetBlocks() is empty in wii_driver.cpp, so bm_Reset() alone
// only clears the hash table — the compiled PPC code stays in memory
// and can still be jumped to via direct pointers held by BIOS blocks.
// sh4_cpu.ResetCache() flushes the actual PPC emit buffer, making
// those stale addresses unreachable.
extern void bm_Reset();
bm_Reset();
sh4_cpu.ResetCache();
}
}
//////////////////////////////////////
//command line parsing & init
int main___(int argc,char* argv[])
{
if(ParseCommandLine(argc,argv))
{
printf("\n\n[nullDC.cpp] (Exiting due to command line, without starting nullDC)\n");
return 69;
}
if(!cfgOpen())
{
msgboxf(_T("[nullDC.cpp] Unable to open config file"),MBX_ICONERROR);
return -4;
}
printf("[nullDC] cfgOpen OK\n");
LoadSettings();
printf("[nullDC] LoadSettings done\n");
int rv= 0;
char* temp_path=GetEmuPath(_T("data/dc_boot.bin"));
printf("[nullDC] checking BIOS: %s\n", temp_path);
FILE* fr=fopen(temp_path,"r");
if (!fr)
{
msgboxf(_T("Unable to find bios -- exiting\n%s"),MBX_ICONERROR,temp_path);
rv=-3;
goto cleanup;
}
fclose(fr);
printf("[nullDC] BIOS found OK\n");
free(temp_path);
temp_path=GetEmuPath(_T("data/dc_flash.bin"));
printf("[nullDC] checking flash: %s\n", temp_path);
fr=fopen(temp_path,"r");
if (!fr)
{
msgboxf(_T("Unable to find flash -- exiting\n%s"),MBX_ICONERROR,temp_path);
rv=-6;
goto cleanup;
}
fclose(fr);
printf("[nullDC] flash found OK\n");
free(temp_path);
printf("[nullDC] Loading plugins!\n");
while (!plugins_Load())
{
//if (!plugins_Select())
{
msgboxf("Unable to load plugins -- exiting\n",MBX_ICONERROR);
rv = -2;
goto cleanup;
}
}
printf("[nullDC] plugins_Load OK\n");
// -----------------------------------------------------------------------
// ELF detection: if the selected file is a .elf, load its segments into
// emulated RAM now (memory is reserved, plugins are loaded).
// The actual PC override happens later in dc_elf_pre_run_hook(), after
// the CPU back-end has been reset.
// -----------------------------------------------------------------------
{
// Read the selected path directly – we cannot use os_GetFile() here because
// we already patched it to return false for .elf files (to block the disc plugin).
extern char selectedFilePath[512];
const char* elfPath = selectedFilePath;
if (HasExtension(elfPath, ".elf"))
{
printf("[ELF] Detected ELF file: %s\n", elfPath);
u32 entry = 0;
if (elf_load(elfPath, &entry))
{
g_elf_entry = entry;
g_elf_pending = true;
printf("[ELF] Segments loaded, will boot to 0x%08X\n", entry);
}
else
{
printf("[ELF] Failed to load ELF – falling back to BIOS\n");
// g_elf_pending stays false → normal BIOS boot
}
}
}
printf("[nullDC] calling RunDC()...\n");
rv = RunDC();
printf("[nullDC] RunDC() returned rv=%d\n", rv);
cleanup:
printf("__Cleanup\n");
SaveSettings();
return rv;
}
//entry point, platform specific main() calls this when done with init/stuff
int EmuMain(int argc, char* argv[])
{
printf("[nullDC] EmuMain: start\n");
printf(VER_FULLNAME " starting up ..\n");
printf("[nullDC] EmuMain: _vmem_reserve()...\n");
if (!_vmem_reserve())
{
msgboxf("Unable to reserve nullDC memory ...",MBX_OK | MBX_ICONERROR);
return -5;
}
printf("[nullDC] EmuMain: _vmem_reserve OK\n");
printf("[nullDC] EmuMain: calling main___()...\n");
int rv=main___(argc,argv);
printf("[nullDC] EmuMain: main___() returned rv=%d\n", rv);
#ifndef _ANDROID
_vmem_release();
#endif
return rv;
}
void LoadSettings()
{
printf("[nullDC.cpp] Loading settings\n");
settings.dynarec.Enable = 1 | (cfgLoadInt("nullDC", "Dynarec.Enabled", 1) != 0);
settings.dynarec.CPpass=cfgLoadInt("nullDC","Dynarec.DoConstantPropagation",1)!=0;
settings.dynarec.UnderclockFpu=cfgLoadInt("nullDC","Dynarec.UnderclockFpu",0)!=0;
settings.dynarec.safemode=cfgLoadInt("nullDC","Dynarec.SafeMode",0)!=0;
settings.dreamcast.cable=cfgLoadInt("nullDC","Dreamcast.Cable",3);
settings.dreamcast.RTC=cfgLoadInt("nullDC","Dreamcast.RTC",GetRTC_now());
settings.emulator.AutoStart=cfgLoadInt("nullDC","Emulator.AutoStart",0)!=0;
settings.emulator.NoConsole=cfgLoadInt("nullDC","Emulator.NoConsole",0)!=0;
printf("[nullDC.cpp] Loaded settings\n");
}
void SaveSettings()
{
cfgSaveInt("nullDC","Dynarec.Enabled",settings.dynarec.Enable);
cfgSaveInt("nullDC","Dynarec.DoConstantPropagation",settings.dynarec.CPpass);
cfgSaveInt("nullDC","Dynarec.UnderclockFpu",settings.dynarec.UnderclockFpu);
cfgSaveInt("nullDC","Dynarec.SafeMode",settings.dynarec.safemode);
cfgSaveInt("nullDC","Dreamcast.Cable",settings.dreamcast.cable);
cfgSaveInt("nullDC","Dreamcast.RTC",settings.dreamcast.RTC);
cfgSaveInt("nullDC","Emulator.AutoStart",settings.emulator.AutoStart);
cfgSaveInt("nullDC","Emulator.NoConsole",settings.emulator.NoConsole);
}