Report errors instead of crashing

This commit is contained in:
allkern
2024-09-03 12:31:29 -03:00
parent 4f253a961f
commit 086b920460
14 changed files with 90 additions and 85 deletions
+8 -14
View File
@@ -17,17 +17,14 @@ void psx_bios_init(psx_bios_t* bios) {
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);
exit(1);
}
if (!file)
return 1;
// Almost all PS1 BIOS ROMs are 512 KiB in size.
// There's (at least) one exception, and that is SCPH-5903.
@@ -43,15 +40,12 @@ void psx_bios_load(psx_bios_t* bios, const char* path) {
bios->buf = malloc(size);
bios->io_size = size;
if (!fread(bios->buf, 1, size, file)) {
perror("Error reading BIOS file");
exit(1);
}
log_info("Loaded BIOS file \"%s\"", path);
if (!fread(bios->buf, 1, size, file))
return 2;
fclose(file);
return 0;
}
uint32_t psx_bios_read32(psx_bios_t* bios, uint32_t offset) {
+1 -1
View File
@@ -18,7 +18,7 @@ typedef struct {
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);
+8 -3
View File
@@ -184,17 +184,22 @@ void psx_cdrom_set_region(psx_cdrom_t* cdrom, int region) {
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);
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) {
+1 -1
View File
@@ -295,7 +295,7 @@ void psx_cdrom_init(psx_cdrom_t* cdrom, psx_ic_t* ic);
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);
+14 -11
View File
@@ -9,7 +9,7 @@ psx_exp1_t* psx_exp1_create(void) {
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 @@ void psx_exp1_init(psx_exp1_t* exp1, psx_mc1_t* mc1, const char* path) {
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)) {
perror("Error reading expansion ROM file");
}
if (!fread(exp1->rom, 1, PSX_EXP1_SIZE, file))
return 2;
fclose(file);
return 0;
}
uint32_t psx_exp1_read32(psx_exp1_t* exp1, uint32_t offset) {
+2 -2
View File
@@ -18,8 +18,8 @@ typedef struct {
} 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);
+7 -8
View File
@@ -5,7 +5,7 @@ psx_mcd_t* psx_mcd_create(void) {
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 @@ void psx_mcd_init(psx_mcd_t* mcd, const char* path) {
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");
exit(1);
}
if (!fread(mcd->buf, 1, MCD_MEMORY_SIZE, file))
return 2;
fclose(file);
return 0;
}
uint8_t psx_mcd_read(psx_mcd_t* mcd) {
+1 -1
View File
@@ -54,7 +54,7 @@ typedef struct {
} 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*);
+12 -3
View File
@@ -292,18 +292,27 @@ void psx_pad_detach_joy(psx_pad_t* pad, int slot) {
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) {
+1 -1
View File
@@ -134,7 +134,7 @@ void psx_pad_button_release(psx_pad_t*, int, uint32_t);
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);
+12 -16
View File
@@ -3,23 +3,20 @@
#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);
exit(1);
}
if (!file)
return 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");
exit(1);
}
if (!fread((char*)&hdr, 1, sizeof(psx_exe_hdr_t), file))
return 2;
// Seek to program start
fseek(file, 0x800, SEEK_SET);
@@ -27,11 +24,8 @@ void psx_exe_load(psx_cpu_t* cpu, const char* path) {
// 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");
exit(1);
}
if (!fread(cpu->bus->ram->buf + offset, 1, hdr.filesz, file))
return 3;
// Load initial register values
cpu->pc = hdr.ipc;
@@ -47,4 +41,6 @@ void psx_exe_load(psx_cpu_t* cpu, const char* path) {
log_fatal("PC=%08x SP=%08x (%08x) GP=%08x", cpu->pc, cpu->r[29], hdr.ispb, cpu->r[28]);
fclose(file);
return 0;
}
+1 -1
View File
@@ -48,6 +48,6 @@ typedef struct {
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
+18 -19
View File
@@ -4,8 +4,8 @@ psx_t* psx_create(void) {
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 @@ void* psx_get_vram(psx_t* psx) {
}
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_width(psx_t* psx) {
}
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 @@ void atcons_tx(void* udata, unsigned char c) {
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 @@ void psx_init(psx_t* psx, const char* bios_path, const char* exp_path) {
// 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 @@ void psx_init(psx_t* psx, const char* bios_path, const char* exp_path) {
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 @@ uint32_t* psx_take_screenshot(psx_t* psx) {
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 @@ void psx_swap_disc(psx_t* psx, const char* path) {
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) {
+4 -4
View File
@@ -36,9 +36,9 @@ typedef struct {
} 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_height(psx_t*);
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*);