shithub: psxe

Download patch

ref: 3f91a2038abf10e772efba06dd492c9e314eea06
parent: c7663873ef41dabd7beb740615a64fd42effa572
author: allkern <aurealinbox@gmail.com>
date: Fri Dec 15 20:50:42 EST 2023

Implement XA-ADPCM

--- a/psx/dev/cdrom.c
+++ b/psx/dev/cdrom.c
@@ -1547,21 +1547,26 @@
     cdrom->xa_left_buf = malloc(XA_STEREO_SAMPLES * sizeof(int16_t));
     cdrom->xa_right_buf = malloc(XA_STEREO_SAMPLES * sizeof(int16_t));
     cdrom->xa_mono_buf = malloc(XA_MONO_SAMPLES * sizeof(int16_t));
-    cdrom->xa_decoded_buf = malloc(XA_DECODED_SAMPLES * sizeof(int16_t));
-    cdrom->xa_left_ring_buf = malloc(XA_RINGBUF_SIZE * sizeof(int16_t));
-    cdrom->xa_right_ring_buf = malloc(XA_RINGBUF_SIZE * sizeof(int16_t));
-    cdrom->xa_stereo_resample_buf = malloc(XA_STEREO_RESAMPLE_SIZE * sizeof(int16_t));
-    cdrom->xa_mono_resample_buf = malloc(XA_MONO_RESAMPLE_SIZE * sizeof(int16_t));
+    // cdrom->xa_decoded_buf = malloc(XA_DECODED_SAMPLES * sizeof(int16_t));
+    // cdrom->xa_left_ring_buf = malloc(XA_RINGBUF_SIZE * sizeof(int16_t));
+    // cdrom->xa_right_ring_buf = malloc(XA_RINGBUF_SIZE * sizeof(int16_t));
+    // cdrom->xa_stereo_resample_buf = malloc(XA_STEREO_RESAMPLE_SIZE * sizeof(int16_t));
+    // cdrom->xa_mono_resample_buf = malloc(XA_MONO_RESAMPLE_SIZE * sizeof(int16_t));
+    cdrom->xa_upsample_buf = malloc((14112 + 6) * sizeof(int16_t));
+    cdrom->xa_resample_buf = malloc(2352 * sizeof(int16_t));
     cdrom->xa_step = 6;
 
     memset(cdrom->xa_left_buf, 0, XA_STEREO_SAMPLES * sizeof(int16_t));
     memset(cdrom->xa_right_buf, 0, XA_STEREO_SAMPLES * sizeof(int16_t));
     memset(cdrom->xa_mono_buf, 0, XA_MONO_SAMPLES * sizeof(int16_t));
-    memset(cdrom->xa_decoded_buf, 0, XA_DECODED_SAMPLES * sizeof(int16_t));
-    memset(cdrom->xa_left_ring_buf, 0, XA_RINGBUF_SIZE * sizeof(int16_t));
-    memset(cdrom->xa_right_ring_buf, 0, XA_RINGBUF_SIZE * sizeof(int16_t));
-    memset(cdrom->xa_stereo_resample_buf, 0, XA_STEREO_RESAMPLE_SIZE * sizeof(int16_t));
-    memset(cdrom->xa_mono_resample_buf, 0, XA_MONO_RESAMPLE_SIZE * sizeof(int16_t));
+    memset(cdrom->xa_upsample_buf, 0, (14112 + 6) * sizeof(int16_t));
+    memset(cdrom->xa_resample_buf, 0, 2352 * sizeof(int16_t));
+
+    // memset(cdrom->xa_decoded_buf, 0, XA_DECODED_SAMPLES * sizeof(int16_t));
+    // memset(cdrom->xa_left_ring_buf, 0, XA_RINGBUF_SIZE * sizeof(int16_t));
+    // memset(cdrom->xa_right_ring_buf, 0, XA_RINGBUF_SIZE * sizeof(int16_t));
+    // memset(cdrom->xa_stereo_resample_buf, 0, XA_STEREO_RESAMPLE_SIZE * sizeof(int16_t));
+    // memset(cdrom->xa_mono_resample_buf, 0, XA_MONO_RESAMPLE_SIZE * sizeof(int16_t));
 }
 
 uint32_t psx_cdrom_read32(psx_cdrom_t* cdrom, uint32_t offset) {
@@ -1847,7 +1852,7 @@
     int16_t* right_ptr = cdrom->xa_right_buf;
     int16_t* mono_ptr = cdrom->xa_mono_buf;
 
-    for (int i = 0; i < 12; i++) {
+    for (int i = 0; i < 18; i++) {
         for (int blk = 0; blk < 4; blk++) {
             if (stereo) {
                 cdrom_decode_xa_block(cdrom, src, blk, 0, left, left_h);
@@ -1875,10 +1880,22 @@
 
     if (stereo) {
         for (int i = 0; i < XA_STEREO_SAMPLES; i++) {
-            *ptr++ = cdrom->xa_left_buf[i];
-            *ptr++ = cdrom->xa_right_buf[i];
+            int j = i * 7;
+
+            cdrom->xa_upsample_buf[j] = cdrom->xa_left_buf[i];
+
+            // Nearest neighbor
+            for (int k = 0; k < 7; k++)
+                cdrom->xa_upsample_buf[j+k] = cdrom->xa_left_buf[i];
         }
+
+        for (int i = 0; i < 2352; i++)
+            cdrom->xa_resample_buf[i] = cdrom->xa_upsample_buf[i*6];
+
+        cdrom->xa_remaining_samples = 2352;
     } else {
+        exit(1);
+
         for (int i = 0; i < XA_MONO_SAMPLES; i++) {
             *ptr++ = cdrom->xa_mono_buf[i];
             *ptr++ = cdrom->xa_mono_buf[i];
@@ -1959,17 +1976,21 @@
         return;
 
     if (cdrom->xa_playing) {
-        return;
+        int16_t* ptr = (int16_t*)buf;
 
-        if (!cdrom->xa_remaining_samples) {
-            cdrom_fetch_xa_sector(cdrom);
+        for (int i = 0; i < (size >> 2); i++) {
+            if (!cdrom->xa_remaining_samples) {
+                cdrom_fetch_xa_sector(cdrom);
+                cdrom_decode_xa_sector(cdrom, buf);
 
-            cdrom->xa_remaining_samples = 4;
-        }
+                cdrom->xa_sample_idx = 0;
+            }
 
-        cdrom_decode_xa_sector(cdrom, buf);
+            *ptr++ = cdrom->xa_resample_buf[(cdrom->xa_sample_idx) % 2352];
+            *ptr++ = cdrom->xa_resample_buf[(cdrom->xa_sample_idx++) % 2352];
 
-        --cdrom->xa_remaining_samples;
+            --cdrom->xa_remaining_samples;
+        }
 
         // int16_t* ptr = (int16_t*)buf;
 
--- a/psx/dev/cdrom.h
+++ b/psx/dev/cdrom.h
@@ -221,9 +221,11 @@
     int16_t* xa_stereo_resample_buf;
     int16_t* xa_mono_resample_buf;
     uint32_t xa_sample_idx;
-    uint32_t xa_remaining_samples;
+    int xa_remaining_samples;
     uint32_t xa_step;
     uint32_t xa_ringbuf_pos;
+    int16_t* xa_resample_buf;
+    int16_t* xa_upsample_buf;
 
     const char* path;
     psx_disc_t* disc;
--- a/psx/dev/gpu.c
+++ b/psx/dev/gpu.c
@@ -1745,10 +1745,10 @@
 }
 
 void* psx_gpu_get_display_buffer(psx_gpu_t* gpu) {
-    return gpu->vram; // + (gpu->disp_x + (gpu->disp_y * 1024));
+    return gpu->vram + (gpu->draw_x1 + (gpu->draw_y1 * 1024));
 }
 
 void psx_gpu_destroy(psx_gpu_t* gpu) {
     free(gpu->vram);
     free(gpu);
-}
\ No newline at end of file
+}
--