foxygit / fake-08-xbox360 Log in
commit 7e6a5ac9a80232404cb0fc2b90ece06a1ad102d6
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Tue Apr 27 22:23:34 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Apr 27 22:23:34 2021 -0600

    Change song struct to not use bit field union
---
 source/Audio.cpp   | 14 +++++++-------
 source/PicoRam.h   | 47 ++++++++++++++++++++++++++++++-----------------
 test/carttests.cpp | 32 ++++++++++++++++----------------
 test/vmtests.cpp   | 20 ++++++++++----------
 4 files changed, 63 insertions(+), 50 deletions(-)

diff --git a/source/Audio.cpp b/source/Audio.cpp
index a500f82..f890492 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -145,10 +145,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].getSfx0(),
+        _memory->songs[pattern].getSfx1(),
+        _memory->songs[pattern].getSfx2(),
+        _memory->songs[pattern].getSfx3(),
     };

     // Find music speed; it’s the speed of the fastest sfx
@@ -251,13 +251,13 @@ int16_t Audio::getSampleForChannel(int channel){
             int16_t next_pattern = _audioState._musicChannel.pattern + 1;
             int16_t next_count = _audioState._musicChannel.count + 1;
             //todo: pull out these flags, get memory storage correct as well
-            if (_memory->songs[_audioState._musicChannel.pattern].stop) //stop part of the loop flag
+            if (_memory->songs[_audioState._musicChannel.pattern].getStop()) //stop part of the loop flag
             {
                 next_pattern = -1;
                 next_count = _audioState._musicChannel.count;
             }
-            else if (_memory->songs[_audioState._musicChannel.pattern].loop){
-                while (--next_pattern > 0 && !_memory->songs[next_pattern].start)
+            else if (_memory->songs[_audioState._musicChannel.pattern].getLoop()){
+                while (--next_pattern > 0 && !_memory->songs[next_pattern].getStart())
                     ;
             }

diff --git a/source/PicoRam.h b/source/PicoRam.h
index 78d57b4..750f815 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -18,27 +18,40 @@
 0x6000 	0x7fff 	Screen data (8k)
 */

+
+//used to use a bitfield union for song and sfx. but this caused problems on big endian platforms
+//now use specific getter/setters that make sure bits are in the right spot
 struct song {
-    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];

-        // The four song channels that should play, 0…63 (each msb holds a flag)
-        uint8_t data[4];
-    };
+    uint8_t getSfx0()const {
+        return (data[0] & 0b01111111);
+    }
+    uint8_t getSfx1()const {
+        return (data[1] & 0b01111111);
+    }
+    uint8_t getSfx2()const {
+        return (data[2] & 0b01111111);
+    }
+    uint8_t getSfx3()const {
+        return (data[3] & 0b01111111);
+    }
+
+    uint8_t getStart()const {
+        return (data[0] & 0b10000000) >> 7;
+    }
+    uint8_t getLoop()const {
+        return (data[1] & 0b10000000) >> 7;
+    }
+    uint8_t getStop()const {
+        return (data[2] & 0b10000000) >> 7;
+    }
+    uint8_t getMode()const {
+        return (data[3] & 0b10000000) >> 7;
+    }
 };

-//using uint16_t may be necessary since waveform spans two bytes
 struct note {
     uint8_t data[2];

diff --git a/test/carttests.cpp b/test/carttests.cpp
index f0e2d07..96b1ca7 100644
--- a/test/carttests.cpp
+++ b/test/carttests.cpp
@@ -117,14 +117,14 @@ TEST_CASE("Load simple p8 cart") {
         CHECK(cart->CartRom.SfxData[63].data[65] == 16);
     }
     SUBCASE("Music song data is populated") {
-        CHECK(cart->CartRom.SongData[0].sfx0 == 1);
-        CHECK(cart->CartRom.SongData[0].start == 0);
-        CHECK(cart->CartRom.SongData[0].sfx1 == 66);
-        CHECK(cart->CartRom.SongData[0].loop == 0);
-        CHECK(cart->CartRom.SongData[0].sfx2 == 67);
-        CHECK(cart->CartRom.SongData[0].stop == 0);
-        CHECK(cart->CartRom.SongData[0].sfx3 == 68);
-        CHECK(cart->CartRom.SongData[0].mode == 0);
+        CHECK(cart->CartRom.SongData[0].getSfx0() == 1);
+        CHECK(cart->CartRom.SongData[0].getStart() == 0);
+        CHECK(cart->CartRom.SongData[0].getSfx1() == 66);
+        CHECK(cart->CartRom.SongData[0].getLoop() == 0);
+        CHECK(cart->CartRom.SongData[0].getSfx2() == 67);
+        CHECK(cart->CartRom.SongData[0].getStop() == 0);
+        CHECK(cart->CartRom.SongData[0].getSfx3() == 68);
+        CHECK(cart->CartRom.SongData[0].getMode() == 0);
     }
     SUBCASE("Music data byte array is populated correctly") {
         CHECK(cart->CartRom.SongData[1].data[0] == 155);
@@ -177,14 +177,14 @@ TEST_CASE("Load simple png cart") {
         CHECK(cart->CartRom.SfxData[0].notes[0].data[1] == 6);
     }
     SUBCASE("Music data is populated") {
-        CHECK(cart->CartRom.SongData[0].sfx0 == 1);
-        CHECK(cart->CartRom.SongData[0].start == 0);
-        CHECK(cart->CartRom.SongData[0].sfx1 == 66);
-        CHECK(cart->CartRom.SongData[0].loop == 0);
-        CHECK(cart->CartRom.SongData[0].sfx2 == 67);
-        CHECK(cart->CartRom.SongData[0].stop == 0);
-        CHECK(cart->CartRom.SongData[0].sfx3 == 68);
-        CHECK(cart->CartRom.SongData[0].mode == 0);
+        CHECK(cart->CartRom.SongData[0].getSfx0() == 1);
+        CHECK(cart->CartRom.SongData[0].getStart() == 0);
+        CHECK(cart->CartRom.SongData[0].getSfx1() == 66);
+        CHECK(cart->CartRom.SongData[0].getLoop() == 0);
+        CHECK(cart->CartRom.SongData[0].getSfx2() == 67);
+        CHECK(cart->CartRom.SongData[0].getStop() == 0);
+        CHECK(cart->CartRom.SongData[0].getSfx3() == 68);
+        CHECK(cart->CartRom.SongData[0].getMode() == 0);
     }

     delete cart;
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index a723af2..ddf97f0 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -88,8 +88,8 @@ TEST_CASE("Vm memory functions") {
         //78: 0100 1110
         vm->vm_poke(0x3100, 78);

-        CHECK_EQ(memory->songs[0].sfx0, 78);
-        CHECK_EQ(memory->songs[0].start, 0);
+        CHECK_EQ(memory->songs[0].getSfx0(), 78);
+        CHECK_EQ(memory->songs[0].getStart(), 0);
     }
     SUBCASE("poking sfx"){
         //97: 0110 0001
@@ -352,14 +352,14 @@ TEST_CASE("Vm memory functions") {
             CHECK(memory->sfx[0].notes[0].data[1] == 6);
         }
         SUBCASE("Music data is populated") {
-            CHECK(memory->songs[0].sfx0 == 1);
-            CHECK(memory->songs[0].start == 0);
-            CHECK(memory->songs[0].sfx1 == 66);
-            CHECK(memory->songs[0].loop == 0);
-            CHECK(memory->songs[0].sfx2 == 67);
-            CHECK(memory->songs[0].stop == 0);
-            CHECK(memory->songs[0].sfx3 == 68);
-            CHECK(memory->songs[0].mode == 0);
+            CHECK(memory->songs[0].getSfx0() == 1);
+            CHECK(memory->songs[0].getStart() == 0);
+            CHECK(memory->songs[0].getSfx1() == 66);
+            CHECK(memory->songs[0].getLoop() == 0);
+            CHECK(memory->songs[0].getSfx2() == 67);
+            CHECK(memory->songs[0].getStop() == 0);
+            CHECK(memory->songs[0].getSfx3() == 68);
+            CHECK(memory->songs[0].getMode() == 0);
         }
     }
     SUBCASE("reload writes cart rom over changes") {