shithub: psxe

Download patch

ref: 643a196441b37b1c3dcb0a33645294b3840d0f35
parent: a8b8eafddf537c89f3c7e8834b3bf2a41a4ba4e2
author: allkern <lisandroaalarcon@gmail.com>
date: Fri Sep 1 18:48:48 EDT 2023

CDROM Disc Reader groundwork

--- a/psx/dev/cdrom.c
+++ b/psx/dev/cdrom.c
@@ -962,22 +962,26 @@
 }
 
 void psx_cdrom_open(psx_cdrom_t* cdrom, const char* path) {
-    cdrom->disc = fopen(path, "rb");
+    cdrom->disc = psx_disc_create();
 
-    if (!cdrom->disc) {
-        log_fatal("Couldn't open disc image \"%s\"", path);
+    // To-do: Init disc based on extension.
+    //        e.g. If file extension is .cue then init with
+    //        psxd_cue, if file extension is .bin, then init
+    //        with psxd_bin, etc.
 
-        exit(1);
-    }
+    psxd_cue_t* cue = psxd_cue_create();
 
-    fseek(cdrom->disc, 0, 0);
+    psxd_cue_init_disc(cue, cdrom->disc);
+    psxd_cue_init(cue, 0);
+    psxd_cue_parse(cue, path);
+    psxd_cue_load(cue);
 }
 
 void psx_cdrom_close(psx_cdrom_t* cdrom) {
-    if (cdrom->disc)
-        fclose(cdrom->disc);
 }
 
 void psx_cdrom_destroy(psx_cdrom_t* cdrom) {
+    psx_disc_destroy(cdrom->disc);
+
     free(cdrom);
 }
\ No newline at end of file
--- a/psx/dev/cdrom.h
+++ b/psx/dev/cdrom.h
@@ -5,6 +5,8 @@
 #include <stdio.h>
 
 #include "ic.h"
+#include "../disc.h"
+#include "../disc/cue.h"
 
 #define DELAY_1MS (PSX_CPU_CPS / 1000)
 #define READ_SINGLE_DELAY (PSX_CPU_CPS / 75)
@@ -184,7 +186,7 @@
     int read_ongoing;
 
     const char* path;
-    FILE* disc;
+    psx_disc_t* disc;
 } psx_cdrom_t;
 
 psx_cdrom_t* psx_cdrom_create();
--- a/psx/disc.c
+++ b/psx/disc.c
@@ -37,4 +37,10 @@
 
 int psx_disc_get_track_count(psx_disc_t* disc, int* count) {
     return disc->get_track_count_func(disc->udata, count);
+}
+
+void psx_disc_destroy(psx_disc_t* disc) {
+    disc->destroy_func(disc->udata);
+
+    free(disc);
 }
\ No newline at end of file
--- a/psx/disc.h
+++ b/psx/disc.h
@@ -26,6 +26,7 @@
 typedef int (*disc_read_sector_t)(void*, void*);
 typedef int (*disc_get_track_addr_t)(void*, msf_t*, int);
 typedef int (*disc_get_track_count_t)(void*, int*);
+typedef void (*disc_destroy_t)(void*);
 
 typedef struct {
     void* udata;
@@ -34,6 +35,7 @@
     disc_read_sector_t read_sector_func;
     disc_get_track_addr_t get_track_addr_func;
     disc_get_track_count_t get_track_count_func;
+    disc_destroy_t destroy_func;
 } psx_disc_t;
 
 psx_disc_t* psx_disc_create();
@@ -41,5 +43,6 @@
 int psx_disc_read_sector(psx_disc_t*, void*);
 int psx_disc_get_track_addr(psx_disc_t*, msf_t*, int);
 int psx_disc_get_track_count(psx_disc_t*, int*);
+void psx_disc_destroy(psx_disc_t*);
 
 #endif
\ No newline at end of file
--- a/psx/disc/cue.c
+++ b/psx/disc/cue.c
@@ -377,6 +377,7 @@
     disc->read_sector_func = psxd_cue_read_sector;
     disc->get_track_addr_func = psxd_cue_get_track_addr;
     disc->get_track_count_func = psxd_cue_get_track_count;
+    disc->destroy_func = psxd_cue_destroy;
 }
 
 void psxd_cue_destroy(psxd_cue_t* cue) {
--