foxygit / fake-08-xbox360 Log in
commit 990f3ee6ff3ef25c0fb2f4de69d1072307dff118
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Thu Jun 18 14:48:10 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Thu Jun 18 14:48:10 2020 -0600

    Get png carts to audio feature parity with p8 carts
---
 source/Audio.cpp | 18 +++++++++---------
 source/PicoRam.h | 40 +++++++++++++++++++++++++++-------------
 source/cart.cpp  | 12 +++++++++++-
 source/cart.h    | 14 +++++---------
 source/vm.cpp    |  4 ++--
 5 files changed, 54 insertions(+), 34 deletions(-)

diff --git a/source/Audio.cpp b/source/Audio.cpp
index 52dcc46..3c498eb 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -122,10 +122,10 @@ void Audio::set_music_pattern(int pattern) {

     //array to access song's channels. may be better to have this part of the struct?
     uint8_t channels[] = {
-        _memory->_songs[pattern].sfx0,
-        _memory->_songs[pattern].sfx1,
-        _memory->_songs[pattern].sfx2,
-        _memory->_songs[pattern].sfx3,
+        _memory->songs[pattern].sfx0,
+        _memory->songs[pattern].sfx1,
+        _memory->songs[pattern].sfx2,
+        _memory->songs[pattern].sfx3,
     };

     // Find music speed; it’s the speed of the fastest sfx
@@ -137,7 +137,7 @@ void Audio::set_music_pattern(int pattern) {
         if (n & 0x40)
             continue;

-        auto &sfx = _memory->_sfx[n & 0x3f];
+        auto &sfx = _memory->sfx[n & 0x3f];
         if (_memory->_musicChannel.master == -1 || _memory->_musicChannel.speed > sfx.speed)
         {
             _memory->_musicChannel.master = i;
@@ -225,13 +225,13 @@ int16_t Audio::getSampleForChannel(int channel){
             int16_t next_pattern = _memory->_musicChannel.pattern + 1;
             int16_t next_count = _memory->_musicChannel.count + 1;
             //todo: pull out these flags, get memory storage correct as well
-            if (_memory->_songs[_memory->_musicChannel.pattern].stop) //stop part of the loop flag
+            if (_memory->songs[_memory->_musicChannel.pattern].stop) //stop part of the loop flag
             {
                 next_pattern = -1;
                 next_count = _memory->_musicChannel.count;
             }
-            else if (_memory->_songs[_memory->_musicChannel.pattern].loop){
-                while (--next_pattern > 0 && !_memory->_songs[next_pattern].start)
+            else if (_memory->songs[_memory->_musicChannel.pattern].loop){
+                while (--next_pattern > 0 && !_memory->songs[next_pattern].start)
                     ;
             }

@@ -245,7 +245,7 @@ int16_t Audio::getSampleForChannel(int channel){
         return 0;
     }

-    struct sfx const &sfx = _memory->_sfx[index];
+    struct sfx const &sfx = _memory->sfx[index];

     // Speed must be 1—255 otherwise the SFX is invalid
     int const speed = max(1, (int)sfx.speed);
diff --git a/source/PicoRam.h b/source/PicoRam.h
index cf00730..f41e287 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -39,19 +39,35 @@ struct song {

 //using uint16_t may be necessary since waveform spans two bytes
 struct note {
-    uint16_t key : 6;
-    uint16_t waveform : 3;
-    uint16_t volume : 3;
-    uint16_t effect : 4;
+    union
+    {
+        struct
+        {
+            uint16_t key : 6;
+            uint16_t waveform : 3;
+            uint16_t volume : 3;
+            uint16_t effect : 4;
+        };
+
+        uint8_t data[2];
+    };
 };

 struct sfx {
-    note notes[32];
+    union
+    {
+        struct
+        {
+            note notes[32];

-    uint8_t editorMode;
-    uint8_t speed;
-    uint8_t loopRangeStart;
-    uint8_t loopRangeEnd;
+            uint8_t editorMode;
+            uint8_t speed;
+            uint8_t loopRangeStart;
+            uint8_t loopRangeEnd;
+        };
+
+        uint8_t data[68];
+    };
 };

 struct musicChannel {
@@ -82,11 +98,9 @@ struct PicoRam
 	uint8_t mapData[128 * 32];
 	uint8_t spriteFlags[256];

-	//uint8_t _musicRam[64 * 4];
-    song _songs[64];
+    struct song songs[64];

-    //uint8_t _sfxRam[64 * 68];
-    sfx _sfx[64];
+    struct sfx sfx[64];

     musicChannel _musicChannel;
     sfxChannel _sfxChannels[4];
diff --git a/source/cart.cpp b/source/cart.cpp
index 4209b58..254c3b1 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -202,6 +202,7 @@ bool Cart::loadCartFromPng(std::string filename){

         uint8_t extractedByte = (a << 6) + (r << 4) + (g << 2) + b;

+        //TODO: write this all to one array, then memcpy to picoram?
         //store extracted byte in correct place
         size_t picoDataIdx = i / 4;
         if (picoDataIdx < 0x2000) {
@@ -213,8 +214,17 @@ bool Cart::loadCartFromPng(std::string filename){
         else if (picoDataIdx < 0x3100) {
             SpriteFlagsData[picoDataIdx - 0x3000] = extractedByte;
         }
+        else if (picoDataIdx < 0x3200) {
+            size_t offset = picoDataIdx - 0x3100;
+            size_t songIdx = offset / sizeof(song);
+            size_t channelIdx = offset % sizeof(song);
+            SongData[songIdx].data[channelIdx] = extractedByte;
+        }
         else if (picoDataIdx < 0x4300) {
-            //skipping audio for now
+            size_t offset = picoDataIdx - 0x3200;
+            size_t sfxIdx = offset / sizeof(sfx);
+            size_t byteIdx = offset % sizeof(sfx);
+            SfxData[sfxIdx].data[byteIdx] = extractedByte;
         }
         else if (picoDataIdx < 0x8000) {
             CartLuaData[picoDataIdx - 0x4300] = extractedByte;
diff --git a/source/cart.h b/source/cart.h
index 3aec036..66c4934 100644
--- a/source/cart.h
+++ b/source/cart.h
@@ -30,20 +30,16 @@ class Cart {
     std::string LoadError;

     std::string SpriteSheetString;
-    uint8_t SpriteSheetData[128 * 64];
-
     std::string SpriteFlagsString;
-    uint8_t SpriteFlagsData[256];
-
     std::string MapString;
-    uint8_t MapData[128 * 32];
-
     std::string SfxString;
-    struct sfx SfxData[64];
-
     std::string MusicString;
-    struct song SongData[64];

+    uint8_t SpriteSheetData[128 * 64];
+    uint8_t SpriteFlagsData[256];
+    uint8_t MapData[128 * 32];
+    struct song SongData[64];
+    struct sfx SfxData[64];
     uint8_t CartLuaData[15616];

 };
diff --git a/source/vm.cpp b/source/vm.cpp
index ba88c3f..599d64d 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -95,11 +95,11 @@ bool Vm::loadCart(Cart* cart) {
     }

     for(size_t i = 0; i < 64; i++) {
-        _memory._sfx[i] = cart->SfxData[i];
+        _memory.sfx[i] = cart->SfxData[i];
     }

     for(size_t i = 0; i < 64; i++) {
-        _memory._songs[i] = cart->SongData[i];
+        _memory.songs[i] = cart->SongData[i];
     }

     // initialize Lua interpreter