foxygit / fake-08-xbox360 Log in
commit b756e80312356a05b97770a397c191eff9f446dd
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Thu Jun 18 12:05:54 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Thu Jun 18 12:05:54 2020 -0600

    Update audio structs to match pico 8 memory (from zepto 8)
---
 source/Audio.cpp | 14 +++++++-------
 source/PicoRam.h | 33 ++++++++++++++++++++++-----------
 source/cart.cpp  | 26 ++++++++++++++------------
 source/cart.h    |  4 ++--
 4 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/source/Audio.cpp b/source/Audio.cpp
index 09a6e4e..52dcc46 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].channel1,
-        _memory->_songs[pattern].channel2,
-        _memory->_songs[pattern].channel3,
-        _memory->_songs[pattern].channel4,
+        _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
@@ -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 (BITMASK(2) & _memory->_songs[_memory->_musicChannel.pattern].loop) //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 (BITMASK(1) &_memory->_songs[_memory->_musicChannel.pattern].loop){
-                while (--next_pattern > 0 && !(BITMASK(0) &_memory->_songs[next_pattern].loop))
+            else if (_memory->_songs[_memory->_musicChannel.pattern].loop){
+                while (--next_pattern > 0 && !_memory->_songs[next_pattern].start)
                     ;
             }

diff --git a/source/PicoRam.h b/source/PicoRam.h
index 485cef6..cf00730 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -18,20 +18,31 @@
 */

 struct song {
-    uint8_t loop;
-
-    uint8_t channel1;
-    uint8_t channel2;
-    uint8_t channel3;
-    uint8_t channel4;
-
+    union
+    {
+        struct
+        {
+            uint8_t sfx0  : 7;
+            uint8_t start : 1;
+            uint8_t sfx1  : 7;
+            uint8_t loop  : 1;
+            uint8_t sfx2  : 7;
+            uint8_t stop  : 1;
+            uint8_t sfx3  : 7;
+            uint8_t mode  : 1;
+        };
+
+        // The four song channels that should play, 0…63 (each msb holds a flag)
+        uint8_t data[4];
+    };
 };

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

 struct sfx {
diff --git a/source/cart.cpp b/source/cart.cpp
index 58dffaf..4209b58 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -184,7 +184,6 @@ bool Cart::loadCartFromPng(std::string filename){
     //160x205 == 32800 == 0x8020
     //0x8000 is actual used data size
     size_t imageBytes = image.size();
-    size_t totalPix = width * height;

     uint8_t version = 0;

@@ -484,6 +483,10 @@ void Cart::setMusic(std::string musicString){
         buf[1] = line[1];
         uint8_t flagByte = (uint8_t)strtol(buf, NULL, 16);

+        uint8_t fstop = (flagByte & 4) >> 2;
+        uint8_t frepeat = (flagByte & 2) >> 1;
+        uint8_t fnext = flagByte & 1;
+
         buf[0] = line[3];
         buf[1] = line[4];
         uint8_t channel1byte = (uint8_t)strtol(buf, NULL, 16);
@@ -500,13 +503,12 @@ void Cart::setMusic(std::string musicString){
         buf[1] = line[10];
         uint8_t channel4byte = (uint8_t)strtol(buf, NULL, 16);

-        SongData[musicIdx++] = {
-            flagByte,
-            channel1byte,
-            channel2byte,
-            channel3byte,
-            channel4byte
-        };
+        SongData[musicIdx].data[0] = channel1byte | fnext << 7;
+        SongData[musicIdx].data[1] = channel2byte | frepeat << 7;
+        SongData[musicIdx].data[2] = channel3byte | fstop << 7;
+        SongData[musicIdx].data[3] = channel4byte;
+
+        musicIdx++;
     }

 }
@@ -544,19 +546,19 @@ void Cart::setSfx(std::string sfxString) {
         for (int i = 8; i < 168; i+=5) {
             buf[0] = line[i];
             buf[1] = line[i + 1];
-            uint8_t key = (uint8_t)strtol(buf, NULL, 16);
+            uint16_t key = (uint16_t)strtol(buf, NULL, 16);

             buf[0] = '0';
             buf[1] = line[i + 2];
-            uint8_t waveform = (uint8_t)strtol(buf, NULL, 16);
+            uint16_t waveform = (uint16_t)strtol(buf, NULL, 16);

             buf[0] = '0';
             buf[1] = line[i + 3];
-            uint8_t volume = (uint8_t)strtol(buf, NULL, 16);
+            uint16_t volume = (uint16_t)strtol(buf, NULL, 16);

             buf[0] = '0';
             buf[1] = line[i + 4];
-            uint8_t effect = (uint8_t)strtol(buf, NULL, 16);
+            uint16_t effect = (uint16_t)strtol(buf, NULL, 16);

             SfxData[sfxIdx].notes[noteIdx++] = {
                 key,
diff --git a/source/cart.h b/source/cart.h
index e1fe568..3aec036 100644
--- a/source/cart.h
+++ b/source/cart.h
@@ -39,10 +39,10 @@ class Cart {
     uint8_t MapData[128 * 32];

     std::string SfxString;
-    sfx SfxData[64];
+    struct sfx SfxData[64];

     std::string MusicString;
-    song SongData[64];
+    struct song SongData[64];

     uint8_t CartLuaData[15616];