ref: 086b9204604757393f779e614e40aeb5e7e2e969
parent: 4f253a961ff2018316c7f280c3d76c85677ab81f
author: allkern <lisandroaalarcon@gmail.com>
date: Tue Sep 3 08:31:29 EDT 2024
Report errors instead of crashing
--- a/psx/dev/bios.c
+++ b/psx/dev/bios.c
@@ -17,18 +17,15 @@
bios->bus_delay = 18;
}
-void psx_bios_load(psx_bios_t* bios, const char* path) {
+int psx_bios_load(psx_bios_t* bios, const char* path) {
if (!path)
- return;
+ return 0;
FILE* file = fopen(path, "rb");
- if (!file) {
- log_error("Couldn't open BIOS file \"%s\"", path);
+ if (!file)
+ return 1;
- exit(1);
- }
-
// Almost all PS1 BIOS ROMs are 512 KiB in size.
// There's (at least) one exception, and that is SCPH-5903.
// This is a special asian model PS1 that had built-in support
@@ -43,15 +40,12 @@
bios->buf = malloc(size);
bios->io_size = size;
- if (!fread(bios->buf, 1, size, file)) {
- perror("Error reading BIOS file");
+ if (!fread(bios->buf, 1, size, file))
+ return 2;
- exit(1);
- }
-
- log_info("Loaded BIOS file \"%s\"", path);
-
fclose(file);
+
+ return 0;
}
uint32_t psx_bios_read32(psx_bios_t* bios, uint32_t offset) {
--- a/psx/dev/bios.h
+++ b/psx/dev/bios.h
@@ -18,7 +18,7 @@
psx_bios_t* psx_bios_create(void);
void psx_bios_init(psx_bios_t*);
-void psx_bios_load(psx_bios_t*, const char*);
+int psx_bios_load(psx_bios_t*, const char*);
uint32_t psx_bios_read32(psx_bios_t*, uint32_t);
uint16_t psx_bios_read16(psx_bios_t*, uint32_t);
uint8_t psx_bios_read8(psx_bios_t*, uint32_t);
--- a/psx/dev/cdrom/cdrom.c
+++ b/psx/dev/cdrom/cdrom.c
@@ -184,9 +184,9 @@
cdrom->region = region;
}
-void psx_cdrom_open(psx_cdrom_t* cdrom, const char* path) {+int psx_cdrom_open(psx_cdrom_t* cdrom, const char* path) {if (!path)
- return;
+ return 1;
cdrom_cmd_reset(cdrom);
@@ -193,8 +193,13 @@
cdrom->disc = psx_disc_create();
cdrom->disc_type = psx_disc_open(cdrom->disc, path);
- if (cdrom->disc_type == CDT_ERROR)
+ if (cdrom->disc_type == CDT_ERROR) {psx_cdrom_close(cdrom);
+
+ return 0;
+ }
+
+ return 1;
}
void psx_cdrom_close(psx_cdrom_t* cdrom) {--- a/psx/dev/cdrom/cdrom.h
+++ b/psx/dev/cdrom/cdrom.h
@@ -295,7 +295,7 @@
void psx_cdrom_reset(psx_cdrom_t* cdrom);
void psx_cdrom_set_version(psx_cdrom_t* cdrom, int version);
void psx_cdrom_set_region(psx_cdrom_t* cdrom, int region);
-void psx_cdrom_open(psx_cdrom_t* cdrom, const char* path);
+int psx_cdrom_open(psx_cdrom_t* cdrom, const char* path);
void psx_cdrom_close(psx_cdrom_t* cdrom);
uint32_t psx_cdrom_read32(psx_cdrom_t* cdrom, uint32_t addr);
uint32_t psx_cdrom_read16(psx_cdrom_t* cdrom, uint32_t addr);
--- a/psx/dev/exp1.c
+++ b/psx/dev/exp1.c
@@ -9,7 +9,7 @@
return (psx_exp1_t*)malloc(sizeof(psx_exp1_t));
}
-void psx_exp1_init(psx_exp1_t* exp1, psx_mc1_t* mc1, const char* path) {
+int psx_exp1_init(psx_exp1_t* exp1, psx_mc1_t* mc1, const char* path) {
memset(exp1, 0, sizeof(psx_exp1_t));
exp1->io_base = PSX_EXP1_BEGIN;
@@ -21,23 +21,26 @@
memset(exp1->rom, 0xff, PSX_EXP1_SIZE);
if (path)
- psx_exp1_load(exp1, path);
+ return psx_exp1_load(exp1, path);
+
+ return 0;
}
-void psx_exp1_load(psx_exp1_t* exp1, const char* path) {
+int psx_exp1_load(psx_exp1_t* exp1, const char* path) {
+ if (!path)
+ return 0;
+
FILE* file = fopen(path, "rb");
- if (!file) {
- perror("Error opening expansion ROM file");
+ if (!file)
+ return 1;
- exit(1);
- }
+ if (!fread(exp1->rom, 1, PSX_EXP1_SIZE, file))
+ return 2;
- if (!fread(exp1->rom, 1, PSX_EXP1_SIZE, file)) {
- perror("Error reading expansion ROM file");
- }
-
fclose(file);
+
+ return 0;
}
uint32_t psx_exp1_read32(psx_exp1_t* exp1, uint32_t offset) {
--- a/psx/dev/exp1.h
+++ b/psx/dev/exp1.h
@@ -18,8 +18,8 @@
} psx_exp1_t;
psx_exp1_t* psx_exp1_create(void);
-void psx_exp1_init(psx_exp1_t*, psx_mc1_t*, const char*);
-void psx_exp1_load(psx_exp1_t*, const char*);
+int psx_exp1_init(psx_exp1_t*, psx_mc1_t*, const char*);
+int psx_exp1_load(psx_exp1_t*, const char*);
uint32_t psx_exp1_read32(psx_exp1_t*, uint32_t);
uint16_t psx_exp1_read16(psx_exp1_t*, uint32_t);
uint8_t psx_exp1_read8(psx_exp1_t*, uint32_t);
--- a/psx/dev/mcd.c
+++ b/psx/dev/mcd.c
@@ -5,7 +5,7 @@
return (psx_mcd_t*)malloc(sizeof(psx_mcd_t));
}
-void psx_mcd_init(psx_mcd_t* mcd, const char* path) {
+int psx_mcd_init(psx_mcd_t* mcd, const char* path) {
memset(mcd, 0, sizeof(psx_mcd_t));
mcd->state = MCD_STATE_TX_HIZ;
@@ -17,20 +17,19 @@
memset(mcd->buf, 0, MCD_MEMORY_SIZE);
if (!path)
- return;
+ return 0;
FILE* file = fopen(path, "rb");
if (!file)
- return;
+ return 1;
- if (!fread(mcd->buf, 1, MCD_MEMORY_SIZE, file)) {
- perror("Error reading memory card data");
+ if (!fread(mcd->buf, 1, MCD_MEMORY_SIZE, file))
+ return 2;
- exit(1);
- }
-
fclose(file);
+
+ return 0;
}
uint8_t psx_mcd_read(psx_mcd_t* mcd) {
--- a/psx/dev/mcd.h
+++ b/psx/dev/mcd.h
@@ -54,7 +54,7 @@
} psx_mcd_t;
psx_mcd_t* psx_mcd_create(void);
-void psx_mcd_init(psx_mcd_t*, const char*);
+int psx_mcd_init(psx_mcd_t*, const char*);
uint8_t psx_mcd_read(psx_mcd_t*);
void psx_mcd_write(psx_mcd_t*, uint8_t);
int psx_mcd_query(psx_mcd_t*);
--- a/psx/dev/pad.c
+++ b/psx/dev/pad.c
@@ -292,18 +292,27 @@
pad->joy_slot[slot] = NULL;
}
-void psx_pad_attach_mcd(psx_pad_t* pad, int slot, const char* path) {
+int psx_pad_attach_mcd(psx_pad_t* pad, int slot, const char* path) {
printf("Memory Card support is disabled\n");
- return;
+ return 0;
if (pad->mcd_slot[slot])
psx_pad_detach_mcd(pad, slot);
psx_mcd_t* mcd = psx_mcd_create();
- psx_mcd_init(mcd, path);
+ int r = psx_mcd_init(mcd, path);
+
+ if (r) {
+ psx_pad_detach_mcd(pad, slot);
+
+ return r;
+ }
+
pad->mcd_slot[slot] = mcd;
+
+ return 0;
}
void psx_pad_detach_mcd(psx_pad_t* pad, int slot) {
--- a/psx/dev/pad.h
+++ b/psx/dev/pad.h
@@ -134,7 +134,7 @@
void psx_pad_analog_change(psx_pad_t*, int, uint32_t, uint16_t);
void psx_pad_attach_joy(psx_pad_t*, int, psx_input_t*);
void psx_pad_detach_joy(psx_pad_t*, int);
-void psx_pad_attach_mcd(psx_pad_t*, int, const char*);
+int psx_pad_attach_mcd(psx_pad_t*, int, const char*);
void psx_pad_detach_mcd(psx_pad_t*, int);
void psx_pad_update(psx_pad_t*, int);
--- a/psx/exe.c
+++ b/psx/exe.c
@@ -3,24 +3,21 @@
#include "exe.h"
#include "log.h"
-void psx_exe_load(psx_cpu_t* cpu, const char* path) {
+int psx_exe_load(psx_cpu_t* cpu, const char* path) {
+ if (!path)
+ return 0;
+
FILE* file = fopen(path, "rb");
- if (!file) {
- log_error("Couldn't open PS-X EXE file \"%s\"", path);
+ if (!file)
+ return 1;
- exit(1);
- }
-
// Read header
psx_exe_hdr_t hdr;
- if (!fread((char*)&hdr, 1, sizeof(psx_exe_hdr_t), file)) {
- perror("Error reading PS-EXE header");
+ if (!fread((char*)&hdr, 1, sizeof(psx_exe_hdr_t), file))
+ return 2;
- exit(1);
- }
-
// Seek to program start
fseek(file, 0x800, SEEK_SET);
@@ -27,12 +24,9 @@
// Read to RAM directly
uint32_t offset = hdr.ramdest & 0x7fffffff;
- if (!fread(cpu->bus->ram->buf + offset, 1, hdr.filesz, file)) {
- perror("Error reading PS-EXE data");
+ if (!fread(cpu->bus->ram->buf + offset, 1, hdr.filesz, file))
+ return 3;
- exit(1);
- }
-
// Load initial register values
cpu->pc = hdr.ipc;
cpu->next_pc = cpu->pc + 4;
@@ -47,4 +41,6 @@
log_fatal("PC=%08x SP=%08x (%08x) GP=%08x", cpu->pc, cpu->r[29], hdr.ispb, cpu->r[28]);
fclose(file);
+
+ return 0;
}
\ No newline at end of file
--- a/psx/exe.h
+++ b/psx/exe.h
@@ -48,6 +48,6 @@
uint32_t ispoff;
} psx_exe_hdr_t;
-void psx_exe_load(psx_cpu_t*, const char*);
+int psx_exe_load(psx_cpu_t*, const char*);
#endif
\ No newline at end of file
--- a/psx/psx.c
+++ b/psx/psx.c
@@ -4,8 +4,8 @@
return (psx_t*)malloc(sizeof(psx_t));
}
-void psx_load_bios(psx_t* psx, const char* path) {
- psx_bios_load(psx->bios, path);
+int psx_load_bios(psx_t* psx, const char* path) {
+ return psx_bios_load(psx->bios, path);
}
void psx_load_state(psx_t* psx, const char* path) {
@@ -54,11 +54,6 @@
}
uint32_t psx_get_display_width(psx_t* psx) {
- // int draw = psx->gpu->draw_x2 - psx->gpu->draw_x1;
- // int dmode = psx_get_dmode_width(psx);
-
- // return (draw > dmode) ? dmode : draw;
-
int width = psx_get_dmode_width(psx);
if (width == 368)
@@ -68,11 +63,6 @@
}
uint32_t psx_get_display_height(psx_t* psx) {
- // int draw = psx->gpu->draw_y2 - psx->gpu->draw_y1;
- // int dmode = psx_get_dmode_height(psx);
-
- // return (draw > dmode) ? dmode : draw;
-
return psx_get_dmode_height(psx);
}
@@ -123,7 +113,7 @@
putchar(c);
}
-void psx_init(psx_t* psx, const char* bios_path, const char* exp_path) {
+int psx_init(psx_t* psx, const char* bios_path, const char* exp_path) {
memset(psx, 0, sizeof(psx_t));
psx->bios = psx_bios_create();
@@ -166,13 +156,19 @@
// Init devices
psx_bios_init(psx->bios);
- psx_bios_load(psx->bios, bios_path);
+
+ if (psx_bios_load(psx->bios, bios_path))
+ return 1;
+
psx_mc1_init(psx->mc1);
psx_mc2_init(psx->mc2);
psx_mc3_init(psx->mc3);
psx_ram_init(psx->ram, psx->mc2, RAM_SIZE_2MB);
psx_dma_init(psx->dma, psx->bus, psx->ic);
- psx_exp1_init(psx->exp1, psx->mc1, exp_path);
+
+ if (psx_exp1_init(psx->exp1, psx->mc1, exp_path))
+ return 2;
+
psx_exp2_init(psx->exp2, atcons_tx, NULL);
psx_ic_init(psx->ic, psx->cpu);
psx_scratchpad_init(psx->scratchpad);
@@ -183,10 +179,12 @@
psx_pad_init(psx->pad, psx->ic);
psx_mdec_init(psx->mdec);
psx_cpu_init(psx->cpu, psx->bus);
+
+ return 0;
}
-void psx_load_expansion(psx_t* psx, const char* path) {
- psx_exp1_init(psx->exp1, psx->mc1, path);
+int psx_load_expansion(psx_t* psx, const char* path) {
+ return psx_exp1_init(psx->exp1, psx->mc1, path);
}
void psx_hard_reset(psx_t* psx) {
@@ -205,7 +203,7 @@
exit(1);
}
-void psx_swap_disc(psx_t* psx, const char* path) {
+int psx_swap_disc(psx_t* psx, const char* path) {
psx_cdrom_destroy(psx->cdrom);
psx->cdrom = psx_cdrom_create();
@@ -213,7 +211,8 @@
psx_bus_init_cdrom(psx->bus, psx->cdrom);
psx_cdrom_init(psx->cdrom, psx->ic);
- psx_cdrom_open(psx->cdrom, path);
+
+ return psx_cdrom_open(psx->cdrom, path);
}
void psx_destroy(psx_t* psx) {
--- a/psx/psx.h
+++ b/psx/psx.h
@@ -36,9 +36,9 @@
} psx_t;
psx_t* psx_create(void);
-void psx_init(psx_t*, const char*, const char*);
-void psx_load_expansion(psx_t*, const char*);
-void psx_load_bios(psx_t*, const char*);
+int psx_init(psx_t*, const char*, const char*);
+int psx_load_expansion(psx_t*, const char*);
+int psx_load_bios(psx_t*, const char*);
void psx_hard_reset(psx_t*);
void psx_soft_reset(psx_t*);
void psx_load_state(psx_t*, const char*);
@@ -55,7 +55,7 @@
uint32_t psx_get_display_format(psx_t*);
double psx_get_display_aspect(psx_t*);
uint32_t* psx_take_screenshot(psx_t*);
-void psx_swap_disc(psx_t*, const char*);
+int psx_swap_disc(psx_t*, const char*);
psx_bios_t* psx_get_bios(psx_t*);
psx_ram_t* psx_get_ram(psx_t*);
psx_dma_t* psx_get_dma(psx_t*);
--
⑨