forked from meetpatty/basiliskii-vita
/
main_psp.cpp
605 lines (487 loc) · 13.9 KB
/
main_psp.cpp
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
* main_psp.cpp - Startup code for PSP
*
* Basilisk II (C) 1997-2005 Christian Bauer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <vita2d.h>
#include <psp2/apputil.h>
#include <psp2/common_dialog.h>
#include <psp2/ctrl.h>
#include <psp2/touch.h>
#include <psp2/io/dirent.h>
#include <psp2/power.h>
#include <psp2/kernel/processmgr.h>
#include <psp2/kernel/threadmgr.h>
#include "gui_psp.h"
#include "debugScreen.h"
#include "danzeff/danzeff.h"
#include "sysdeps.h"
#include <string>
using std::string;
#include "cpu_emulation.h"
#include "sys.h"
#include "rom_patches.h"
#include "xpram.h"
#include "timer.h"
#include "video.h"
#include "emul_op.h"
#include "prefs.h"
#include "prefs_editor.h"
#include "macos_util.h"
#include "user_strings.h"
#include "version.h"
#include "main.h"
#define Get_String(x) const_cast<char*>(GetString(x))
#define PSP_VERS 1
#define PSP_REVS 0
#define DEBUG 0
#include "debug.h"
void gui_font_init(void);
void do_gui(struct gui_menu *menu, void *menufn, char *exit_msg);
// Constants
const char ROM_FILE_NAME[] = "roms/mac.rom"; // try to open this file if "rom" not in prefs
const int SCRATCH_MEM_SIZE = 0x10000; // Size of scratch memory area
// Global variables
// CPU and FPU type, addressing mode
int CPUType;
bool CPUIs68060;
int FPUType;
bool TwentyFourBitAddressing;
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes
static bool tick_thread_active = false; // Flag: 60Hz thread installed
static volatile bool tick_thread_cancel = false; // Flag: Cancel 60Hz thread
//static int tick_thread; // 60Hz thread
#if USE_SCRATCHMEM_SUBTERFUGE
uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes
#endif
bool emerg_quit = false; // Flag: emergency quit requested
char psp_home[256];
char temp[256];
char *partitions[] = { "ux0:", "uma0:" };
char *data_dir = "data/BasiliskII/";
char *data_sub_dirs[] = { "cdroms", "disks", "files", "hardfiles", "imaps", "roms" };
int num_partitions = 2;
int num_data_sub_dirs = 6;
int psp_net_error = 0;
int psp_net_available = 0;
char szMyIPAddr[32];
char *psp_local_ip_addr = NULL; // String: the local IP address for the PSP
#define KERNELMODE 0 /* 1 is untested but required for some keyboards to change baudrate */
extern bool refresh_okay;
extern void psp_video_setup(void);
extern void Sys_drive_stop(void);
extern void Sys_drive_restart(void);
// Prototypes
static int tick_func(SceSize args, void *argp);
static void one_tick(void);
void psp_time_init(void)
{
/*
int tzOffset = 0;
sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_TIMEZONE, &tzOffset);
int tzOffsetAbs = tzOffset < 0 ? -tzOffset : tzOffset;
int hours = tzOffsetAbs / 60;
int minutes = tzOffsetAbs - hours * 60;
int pspDaylight = 0;
sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_DAYLIGHTSAVINGS, &pspDaylight);
static char tz[18];
sprintf(tz, "GMT%s%02i:%02i%s", tzOffset < 0 ? "+" : "-", hours, minutes, pspDaylight ? " DST" : "");
setenv("TZ", tz, 1);
tzset();
*/
}
/*
* Main program
*/
int main(int argc, char **argv)
{
// Set common dialog config
sceAppUtilInit(&(SceAppUtilInitParam){}, &(SceAppUtilBootParam){});
sceCommonDialogSetConfigParam(&(SceCommonDialogConfigParam){});
char str[256];
psvDebugScreenInit();
psvDebugScreenSetBgColor(0xFF000000);
psvDebugScreenSetFgColor(0xFFFFFFFF);
psvDebugScreenClear(0xFF000000);
sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG_WIDE);
sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START);
// Print some info
psvDebugScreenClear(0xFF000000);
psvDebugScreenPrintf("\n\n ");
psvDebugScreenPrintf(Get_String(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
psvDebugScreenPrintf("\n %s\n", Get_String(STR_ABOUT_TEXT2));
sceKernelDelayThread(2*1000*1000);
int i, d, found = 0;
for (i = 0; i < num_partitions; i++) {
strcpy(psp_home, partitions[i]);
strcat(psp_home, data_dir);
d = sceIoDopen(psp_home);
if (d >= 0) {
sceIoDclose(d);
found = 1;
break;
}
}
if (!found) {
psvDebugScreenPrintf("\n\n ");
psvDebugScreenPrintf("Data directory not found! Exiting...");
sceKernelDelayThread(2*1000*1000);
return 0;
}
for (i = 0; i < num_data_sub_dirs; i++) {
strcpy(temp, psp_home);
strcat(temp, data_sub_dirs[i]);
d = sceIoDopen(temp);
if (d >= 0)
sceIoDclose(d);
else
sceIoMkdir(temp, 0777);
}
psp_time_init();
vita2d_init();
gui_font_init();
danzeff_load();
// Initialize variables
RAMBaseHost = NULL;
ROMBaseHost = NULL;
// Read preferences
PrefsInit(argc, argv);
// Init system routines
SysInit();
// Show preferences editor
if (!PrefsFindBool("nogui"))
if (!PrefsEditor())
QuitEmulator();
// set CPU frequency
int freq = PrefsFindInt32("pspspeed");
switch (freq)
{
case 1:
scePowerSetArmClockFrequency(111);
break;
case 2:
scePowerSetArmClockFrequency(166);
break;
case 3:
scePowerSetArmClockFrequency(222);
break;
case 4:
scePowerSetArmClockFrequency(266);
break;
case 5:
scePowerSetArmClockFrequency(333);
break;
case 6:
scePowerSetArmClockFrequency(444);
break;
}
// Read RAM size
RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
if (RAMSize < 1024*1024) {
WarningAlert(Get_String(STR_SMALL_RAM_WARN));
RAMSize = 1024*1024;
}
// Create areas for Mac RAM and ROM
uint8 *ram_rom_area = (uint8 *)memalign(64, RAMSize + 0x100000);
if (ram_rom_area == NULL) {
ErrorAlert(STR_NO_MEM_ERR);
QuitEmulator();
}
RAMBaseHost = ram_rom_area;
ROMBaseHost = RAMBaseHost + RAMSize;
#if USE_SCRATCHMEM_SUBTERFUGE
// Allocate scratch memory
ScratchMem = (uint8 *)memalign(64, SCRATCH_MEM_SIZE);
if (ScratchMem == NULL) {
ErrorAlert(STR_NO_MEM_ERR);
QuitEmulator();
}
ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
#endif
#if DIRECT_ADDRESSING
// RAMBaseMac shall always be zero
MEMBaseDiff = (uintptr)RAMBaseHost;
RAMBaseMac = 0;
ROMBaseMac = Host2MacAddr(ROMBaseHost);
#endif
D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
// Get rom file path from preferences
const char *rom_path = PrefsFindString("rom");
// Load Mac ROM
int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
if (rom_fd < 0) {
ErrorAlert(STR_NO_ROM_FILE_ERR);
QuitEmulator();
}
ROMSize = lseek(rom_fd, 0, SEEK_END);
uint32 rom_start = ROMSize & 65535; // any odd number of bytes to 64K should be just a header
if ((ROMSize-rom_start) != 64*1024 && (ROMSize-rom_start) != 128*1024 && (ROMSize-rom_start) != 256*1024 && (ROMSize-rom_start) != 512*1024 && (ROMSize-rom_start) != 1024*1024) {
ErrorAlert(STR_ROM_SIZE_ERR);
close(rom_fd);
QuitEmulator();
}
lseek(rom_fd, rom_start, SEEK_SET);
if (read(rom_fd, ROMBaseHost, ROMSize-rom_start) != (ssize_t)(ROMSize-rom_start)) {
ErrorAlert(STR_ROM_FILE_READ_ERR);
close(rom_fd);
QuitEmulator();
}
// Initialize everything
if (!InitAll())
QuitEmulator();
D(bug("Initialization complete\n"));
#ifndef USE_CPU_EMUL_SERVICES
// start 60Hz thread
int thid = sceKernelCreateThread("tick_thread", tick_func, 0x10000100, 0x1800, 0, 0, NULL);
if(thid < 0) {
sprintf(str, Get_String(STR_TICK_THREAD_ERR));
ErrorAlert(str);
QuitEmulator();
}
sceKernelStartThread(thid, 0, 0);
tick_thread_active = true;
D(bug("60Hz thread started\n"));
#endif
// Start 68k and jump to ROM boot routine
D(bug("Starting emulation...\n"));
Start680x0();
QuitEmulator();
return 0;
}
/*
* Quit emulator
*/
void QuitEmulator(void)
{
D(bug("QuitEmulator\n"));
#if EMULATED_68K
// Exit 680x0 emulation
Exit680x0();
#endif
#if defined(USE_CPU_EMUL_SERVICES)
// Show statistics
uint64 emulated_ticks_end = GetTicks_usec();
D(bug("%ld ticks in %ld usec = %f ticks/sec [%ld tick checks]\n",
(long)emulated_ticks_count, (long)(emulated_ticks_end - emulated_ticks_start),
emulated_ticks_count * 1000000.0 / (emulated_ticks_end - emulated_ticks_start), (long)n_check_ticks));
#else
// Stop 60Hz thread
if (tick_thread_active) {
tick_thread_cancel = true;
sceKernelDelayThread(100*1000);
}
#endif
// Deinitialize everything
ExitAll();
// Free ROM/RAM areas
if (RAMBaseHost != NULL) {
free(RAMBaseHost);
RAMBaseHost = NULL;
ROMBaseHost = NULL;
}
#if USE_SCRATCHMEM_SUBTERFUGE
// Delete scratch memory area
if (ScratchMem != NULL) {
free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2));
ScratchMem = NULL;
}
#endif
// Exit system routines
SysExit();
// Exit preferences
PrefsExit();
sceKernelExitProcess(0);
exit(0);
}
/*
* Code was patched, flush caches if neccessary (i.e. when using a real 680x0
* or a dynamically recompiling emulator)
*/
void FlushCodeCache(void *start, uint32 size)
{
// not needed yet
//sceKernelDcacheWritebackAll();
//sceKernelIcacheClearAll();
}
/*
* Mutexes
*/
struct B2_mutex {
int dummy;
};
B2_mutex *B2_create_mutex(void)
{
return new B2_mutex;
}
void B2_lock_mutex(B2_mutex *mutex)
{
}
void B2_unlock_mutex(B2_mutex *mutex)
{
}
void B2_delete_mutex(B2_mutex *mutex)
{
delete mutex;
}
/*
* Interrupt flags (must be handled atomically!)
*/
uint32 InterruptFlags = 0;
#if EMULATED_68K
void SetInterruptFlag(uint32 flag)
{
// PSP uses cooperative multitasking, so no need to worry
InterruptFlags |= flag;
}
void ClearInterruptFlag(uint32 flag)
{
// PSP uses cooperative multitasking, so no need to worry
InterruptFlags &= ~flag;
}
#endif
/*
* XPRAM watchdog thread (saves XPRAM every minute)
*/
static void xpram_watchdog(void)
{
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
memcpy(last_xpram, XPRAM, XPRAM_SIZE);
SaveXPRAM();
}
}
static void one_second(void)
{
// Pseudo Mac 1Hz interrupt, update local time
WriteMacInt32(0x20c, TimerDateTime());
SetInterruptFlag(INTFLAG_1HZ);
TriggerInterrupt();
static int second_counter = 0;
if (++second_counter > 60) {
second_counter = 0;
xpram_watchdog();
}
}
static void one_tick(void)
{
static int tick_counter = 0;
if (++tick_counter > 60) {
tick_counter = 0;
one_second();
}
// Trigger 60Hz interrupt
if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
SetInterruptFlag(INTFLAG_60HZ);
TriggerInterrupt();
}
}
/*
* 60Hz thread (really 60.15 Hz or 59.94 Hz if wait on vblank)
*/
static int tick_func(SceSize args, void *argp)
{
bool timing = PrefsFindBool("relaxed60hz");
if (timing)
{
while (!tick_thread_cancel)
{
one_tick();
sceKernelDelayThread(16625);
}
}
else
{
uint64 next = GetTicks_usec();
while (!tick_thread_cancel)
{
one_tick();
next += 16625;
int64 delay = next - GetTicks_usec();
if (delay > 0)
Delay_usec(delay);
else if (delay < -16625)
next = GetTicks_usec();
}
}
sceKernelExitDeleteThread(0);
return 0;
}
/*
* Display alert
*/
void display_alert(char *title, char *prefix, char *button, char *text)
{
struct gui_menu AlertMenu[] = {
{ "", GUI_CENTER | GUI_TEXT, (void *)0xFFFFFF, (void *)0x1020FF, GUI_DISABLED }, // title
{ "", GUI_CENTER | GUI_DIVIDER, 0, 0, GUI_DISABLED },
{ "", GUI_CENTER | GUI_STRING, 0, 0, GUI_DISABLED }, // prefix: text
{ 0, GUI_END_OF_MENU, 0, 0, 0 } // end of menu
};
AlertMenu[0].text = title;
AlertMenu[2].text = prefix;
AlertMenu[2].field1 = (void *)&text;
do_gui(AlertMenu, NULL, button);
}
/*
* Display error alert
*/
void ErrorAlert(const char *text)
{
if (PrefsFindBool("nogui"))
printf(Get_String(STR_SHELL_ERROR_PREFIX), text);
else
display_alert(Get_String(STR_ERROR_ALERT_TITLE), Get_String(STR_GUI_ERROR_PREFIX), Get_String(STR_QUIT_BUTTON), const_cast<char*>(text));
}
/*
* Display warning alert
*/
void WarningAlert(const char *text)
{
if (PrefsFindBool("nogui"))
printf(Get_String(STR_SHELL_WARNING_PREFIX), text);
else
display_alert(Get_String(STR_WARNING_ALERT_TITLE), Get_String(STR_GUI_WARNING_PREFIX), Get_String(STR_OK_BUTTON), const_cast<char*>(text));
}
/*
* Display choice alert
*/
bool ChoiceAlert(const char *text, const char *pos, const char *neg)
{
int choice;
struct gui_list choice_list[] = {
{ "", 0 },
{ "", 1 },
{ 0, GUI_END_OF_LIST }
};
struct gui_menu ChoiceMenu[] = {
{ "Alert! Please make a choice.", GUI_CENTER | GUI_TEXT, (void *)0xFFFFFF, (void *)0x1020FF, GUI_DISABLED },
{ "", GUI_CENTER | GUI_DIVIDER, 0, 0, GUI_DISABLED },
{ "", GUI_CENTER | GUI_SELECT, &choice_list, &choice, GUI_ENABLED }, // text: pos/neg
{ 0, GUI_END_OF_MENU, 0, 0, 0 } // end of menu
};
ChoiceMenu[2].text = const_cast<char*>(text);
choice_list[0].text = const_cast<char*>(pos);
choice_list[1].text = const_cast<char*>(neg);
choice = 0; // default to pos choice
do_gui(ChoiceMenu, NULL, "Select current choice");
return choice ? false : true;
}