Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing PS2 port #79

Merged
merged 3 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,11 @@ ifeq ($(TARGET_WEB),1)
PLATFORM_LDFLAGS := -lm -no-pie -s TOTAL_MEMORY=20MB -g4 --source-map-base http://localhost:8080/ -s "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain']"
endif
ifeq ($(TARGET_PS2),1)
AUDSRV_IRX := $(BUILD_DIR)/audsrv_irx
FREESD_IRX := $(BUILD_DIR)/freesd_irx
PS2_ICON := ps2/sm64.icn
C_FILES += $(AUDSRV_IRX).c $(FREESD_IRX).c $(BUILD_DIR)/ps2_icon.c
O_FILES += $(AUDSRV_IRX).o $(FREESD_IRX).o $(BUILD_DIR)/ps2_icon.o
PLATFORM_CFLAGS := -DTARGET_PS2 -D_EE -G0 -I$(PS2SDK)/ee/include -I$(PS2SDK)/common/include -I$(GSKIT)/include
PLATFORM_LDFLAGS := -Wl,-zmax-page-size=128 -L$(GSKIT)/lib -lgskit -ldmakit -L$(PS2SDK)/ee/lib -laudsrv -lpad -lmc -ldma -lcdvd -lpatches
C_FILES += $(BUILD_DIR)/ps2_icon.c
O_FILES += $(BUILD_DIR)/ps2_icon.o
PLATFORM_CFLAGS := -DTARGET_PS2 -D_EE -G0 -I$(PS2SDK)/ee/include -I$(PS2SDK)/common/include -I$(PS2SDK)/ports/include -I$(GSKIT)/include
PLATFORM_LDFLAGS := -Wl,-zmax-page-size=128 -T$(PS2SDK)/ee/startup/linkfile -L$(GSKIT)/lib -L$(PS2SDK)/ee/lib -L$(PS2SDK)/ports/lib -lgskit -ldmakit -lps2_drivers -lmc -lpatches
endif

PLATFORM_CFLAGS += -DNO_SEGMENTED_MEMORY
Expand Down Expand Up @@ -571,17 +569,9 @@ endif

ifeq ($(TARGET_PS2),1)

$(FREESD_IRX).c:
$(PS2SDK)/bin/bin2c $(PS2SDK)/iop/irx/freesd.irx $@ ps2_freesd_irx

$(AUDSRV_IRX).c:
$(PS2SDK)/bin/bin2c $(PS2SDK)/iop/irx/audsrv.irx $@ ps2_audsrv_irx

$(BUILD_DIR)/ps2_icon.c: $(PS2_ICON)
$(PS2SDK)/bin/bin2c $^ $@ ps2_icon_data

$(BUILD_DIR)/src/pc/audio/audio_ps2.o: $(AUDSRV_IRX).o $(FREESD_IRX).o

endif

clean:
Expand Down
40 changes: 2 additions & 38 deletions src/pc/audio/audio_ps2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,15 @@

#include <stdio.h>
#include <kernel.h>
#include <sifrpc.h>
#include <iopheap.h>
#include <loadfile.h>
#include <ps2_audio_driver.h>

#include "audsrv.h"

#include "macros.h"
#include "audio_api.h"

extern unsigned int size_ps2_freesd_irx;
extern unsigned char ps2_freesd_irx;

extern unsigned int size_ps2_audsrv_irx;
extern unsigned char ps2_audsrv_irx;

static int spu2_init(void) {
int id, error;

// load freesd
id = SifExecModuleBuffer(&ps2_freesd_irx, size_ps2_freesd_irx, 0, NULL, &error);
if (id < 0 || error < 0) {
printf("audio_ps2: failed to load freesd: id %d error %d\n", id, error);
return -1;
}

printf("audio_ps2: load freesd id %d\n", id);

// load audsrv
id = SifExecModuleBuffer(&ps2_audsrv_irx, size_ps2_audsrv_irx, 0, NULL, &error);
if (id < 0 || error < 0) {
printf("audio_ps2: failed to load audsrv: id %d error %d\n", id, error);
return -1;
}

printf("audio_ps2: load audsrv id %d\n", id);

audsrv_init();

printf("audio_ps2: init audsrv\n");

return 0;
}

static bool audio_ps2_init(void) {
if (spu2_init()) return false;
if (init_audio_driver() != 0) return false;

audsrv_fmt_t fmt;

Expand Down
35 changes: 17 additions & 18 deletions src/pc/controller/controller_ps2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

#include <tamtypes.h>
#include <kernel.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <ps2_joystick_driver.h>
#include <libpad.h>

#include "controller_api.h"
Expand Down Expand Up @@ -70,28 +69,28 @@ static int detect_pad(void) {
}

static void controller_ps2_init(void) {
if (SifLoadModule("rom0:SIO2MAN", 0, NULL) < 0) {
printf("controller_ps2: SIO2MAN failed to load\n");
return;
}
int ret = -1;

// MEMORY CARD already initied SIO2MAN
ret = init_joystick_driver(false);

if (SifLoadModule("rom0:PADMAN", 0, NULL) < 0) {
printf("controller_ps2: PADMAN failed to load\n");
if (ret != 0) {
printf("controller_ps2: failed to init joystick driver: %d\n", ret);
return;
}

padInit(0);

const int numports = padGetPortMax();

// Find the first device connected
for (int port = 0; port < numports && joy_port < 0; ++port) {
const int maxslots = padGetSlotMax(port);
for (int slot = 0; slot < maxslots; ++slot) {
if (padPortOpen(port, slot, padbuf) >= 0) {
joy_port = port;
joy_slot = slot;
printf("controller_ps2: using pad (%d, %d)\n", port, slot);
break;
if (joy_port == -1 && joy_slot == -1 && mtapPortOpen(port)) {
const int maxslots = padGetSlotMax(port);
for (int slot = 0; slot < maxslots; ++slot) {
if (joy_port == -1 && joy_slot == -1 && padPortOpen(port, slot, padbuf) >= 0) {
joy_port = port;
joy_slot = slot;
printf("controller_ps2: using pad (%d, %d)\n", port, slot);
break;
}
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/pc/gfx/gfx_ps2_wapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ GSGLOBAL *gs_global;

static int vsync_sema_1st_id;
static int vsync_sema_2nd_id;
static int vsync_sema_id = 0;
static int vsync_sema_id = -1;

static const struct VidMode *vid_mode;

Expand Down Expand Up @@ -99,8 +99,6 @@ static void prepare_sema() {
}

static void gfx_ps2_init(const char *game_name, bool start_in_fullscreen) {
prepare_sema();

gs_global = gsKit_init_global();

dmaKit_init(D_CTRL_RELE_OFF, D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC,
Expand All @@ -127,7 +125,6 @@ static void gfx_ps2_init(const char *game_name, bool start_in_fullscreen) {

gsKit_init_screen(gs_global);
gsKit_TexManager_init(gs_global);
gsKit_add_vsync_handler(vsync_handler);
}

static void gfx_ps2_set_fullscreen_changed_callback(void (*on_fullscreen_changed)(bool is_now_fullscreen)) {
Expand Down Expand Up @@ -160,6 +157,11 @@ static bool gfx_ps2_start_frame(void) {
}

static void gfx_ps2_swap_buffers_begin(void) {
if (vsync_sema_id != -1) return;

prepare_sema();
vsync_sema_id = 0;
gsKit_add_vsync_handler(vsync_handler);
}

static void gfx_ps2_swap_buffers_end(void) {
Expand Down
45 changes: 29 additions & 16 deletions src/pc/pc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#ifdef TARGET_PS2
# include <tamtypes.h>
# include <kernel.h>
# include <iopheap.h>
# include <iopcontrol.h>
# include <sifrpc.h>
# include <loadfile.h>
Expand Down Expand Up @@ -140,6 +139,30 @@ static void on_anim_frame(double time) {
}
#endif

#ifdef TARGET_PS2
void reset_IOP() {
SifInitRpc(0);
while (!SifIopReset(NULL, 0)) {} // Comment this line if you want to "debug" through ps2link
while (!SifIopSync()) {}
}

static void prepare_IOP() {
reset_IOP();
SifInitRpc(0);
sbv_patch_enable_lmb();
sbv_patch_disable_prefix_check();
}

static void init_drivers() {
init_ps2_filesystem_driver();
ps2_memcard_init();
}

static void deinit_drivers() {
deinit_ps2_filesystem_driver();
}
#endif

static void save_config(void) {
configfile_save(CONFIG_FILE);
}
Expand All @@ -152,21 +175,8 @@ void main_func(void) {
static u64 pool[0x165000/8 / 4 * sizeof(void *)];

#ifdef TARGET_PS2
// reset IOP and the RPC service
SifInitRpc(0);
while (!SifIopReset("", 0)) { };
while (!SifIopSync()) { };
SifInitRpc(0);

// initialize SIF services
SifLoadFileInit();
SifInitIopHeap();

// enable patch to make SifExecModuleBuffer work with the official LOADFILE module
sbv_patch_enable_lmb();

// init memory cards
ps2_memcard_init();
prepare_IOP();
init_drivers();
#endif

main_pool_init(pool, pool + sizeof(pool) / sizeof(pool[0]));
Expand Down Expand Up @@ -247,6 +257,9 @@ void main_func(void) {
wm_api->main_loop(produce_one_frame);
}
#endif
#ifdef TARGET_PS2
deinit_drivers();
#endif
}

#if defined(_WIN32) || defined(_WIN64)
Expand Down
19 changes: 4 additions & 15 deletions src/pc/ps2_memcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,10 @@ static inline bool memcard_check(void) {
}

bool ps2_memcard_init(void) {
int ret = SifLoadModule("rom0:SIO2MAN", 0, NULL);
if (ret < 0) {
printf("ps2_memcard: failed to load SIO2MAN: %d\n", ret);
return false;
}

ret = SifLoadModule("rom0:MCMAN", 0, NULL);
if (ret < 0) {
printf("ps2_memcard: failed to load MCMAN: %d\n", ret);
return false;
}

ret = SifLoadModule("rom0:MCSERV", 0, NULL);
if (ret < 0) {
printf("ps2_memcard: failed to load MCSERV: %d\n", ret);
int ret = -1;
ret = init_memcard_driver(true);
if(ret != 0) {
printf("ps2_memcard: failed to init memcard driver: %d\n", ret);
return false;
}

Expand Down