foxygit / fake-08-xbox360 Log in
commit 40adc4d35fd463fddca955f666101037d932f610
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Fri Aug 21 16:58:37 2020 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Sep 16 17:34:43 2020 -0600

    Untested WIP on refactoring PicoRam to reflect actual layout
---
 source/Audio.cpp    | 133 ++++++++++++++++++++-------------------
 source/Audio.h      |   1 +
 source/PicoRam.h    | 157 +++++++++++++++++++++++++++++++++++++--------
 source/graphics.cpp | 178 +++++++++++++++++++++++++++-------------------------
 4 files changed, 291 insertions(+), 178 deletions(-)

diff --git a/source/Audio.cpp b/source/Audio.cpp
index ffc23e9..f63d0e5 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -13,9 +13,10 @@

 Audio::Audio(PicoRam* memory){
     _memory = memory;
+    _audioState = {0};

     for(int i = 0; i < 4; i++) {
-        _memory->_sfxChannels[i].sfxId = -1;
+        _audioState._sfxChannels[i].sfxId = -1;
     }
 }

@@ -29,14 +30,14 @@ void Audio::api_sfx(int sfx, int channel, int offset){
     {
         // Stop playing the current channel
         if (channel != -1) {
-            _memory->_sfxChannels[channel].sfxId = -1;
+            _audioState._sfxChannels[channel].sfxId = -1;
         }
     }
     else if (sfx == -2)
     {
         // Stop looping the current channel
         if (channel != -1) {
-            _memory->_sfxChannels[channel].can_loop = false;
+            _audioState._sfxChannels[channel].can_loop = false;
         }
     }
     else
@@ -48,8 +49,8 @@ void Audio::api_sfx(int sfx, int channel, int offset){
         if (channel == -1)
         {
             for (int i = 0; i < 4; ++i)
-                if (_memory->_sfxChannels[i].sfxId == -1 ||
-                    _memory->_sfxChannels[i].sfxId == sfx)
+                if (_audioState._sfxChannels[i].sfxId == -1 ||
+                    _audioState._sfxChannels[i].sfxId == sfx)
                 {
                     channel = i;
                     break;
@@ -61,7 +62,7 @@ void Audio::api_sfx(int sfx, int channel, int offset){
         if (channel == -1)
         {
             for (int i = 0; i < 4; ++i) {
-               if (channel == -1 || _memory->_sfxChannels[i].sfxId < _memory->_sfxChannels[channel].sfxId) {
+               if (channel == -1 || _audioState._sfxChannels[i].sfxId < _audioState._sfxChannels[channel].sfxId) {
                    channel = i;
                }
             }
@@ -69,23 +70,23 @@ void Audio::api_sfx(int sfx, int channel, int offset){

         // Stop any channel playing the same sfx
         for (int i = 0; i < 4; ++i) {
-            if (_memory->_sfxChannels[i].sfxId == sfx) {
-                _memory->_sfxChannels[i].sfxId = -1;
+            if (_audioState._sfxChannels[i].sfxId == sfx) {
+                _audioState._sfxChannels[i].sfxId = -1;
             }
         }

         // Play this sound!
-        _memory->_sfxChannels[channel].sfxId = sfx;
-        _memory->_sfxChannels[channel].offset = std::max(0.f, (float)offset);
-        _memory->_sfxChannels[channel].phi = 0.f;
-        _memory->_sfxChannels[channel].can_loop = true;
-        _memory->_sfxChannels[channel].is_music = false;
+        _audioState._sfxChannels[channel].sfxId = sfx;
+        _audioState._sfxChannels[channel].offset = std::max(0.f, (float)offset);
+        _audioState._sfxChannels[channel].phi = 0.f;
+        _audioState._sfxChannels[channel].can_loop = true;
+        _audioState._sfxChannels[channel].is_music = false;
         // Playing an instrument starting with the note C-2 and the
         // slide effect causes no noticeable pitch variation in PICO-8,
         // so I assume this is the default value for “previous key”.
-        _memory->_sfxChannels[channel].prev_key = 24;
+        _audioState._sfxChannels[channel].prev_key = 24;
         // There is no default value for “previous volume”.
-        _memory->_sfxChannels[channel].prev_vol = 0.f;
+        _audioState._sfxChannels[channel].prev_vol = 0.f;
     }
 }

@@ -97,28 +98,28 @@ void Audio::api_music(int pattern, int16_t fade_len, int16_t mask){
     if (pattern == -1)
     {
         // Music will stop when fade out is finished
-        _memory->_musicChannel.volume_step = fade_len <= 0 ? -FLT_MAX
-                                  : -_memory->_musicChannel.volume * (1000.f / fade_len);
+        _audioState._musicChannel.volume_step = fade_len <= 0 ? -FLT_MAX
+                                  : -_audioState._musicChannel.volume * (1000.f / fade_len);
         return;
     }

-    _memory->_musicChannel.count = 0;
-    _memory->_musicChannel.mask = mask ? mask & 0xf : 0xf;
+    _audioState._musicChannel.count = 0;
+    _audioState._musicChannel.mask = mask ? mask & 0xf : 0xf;

-    _memory->_musicChannel.volume = 1.f;
-    _memory->_musicChannel.volume_step = 0.f;
+    _audioState._musicChannel.volume = 1.f;
+    _audioState._musicChannel.volume_step = 0.f;
     if (fade_len > 0)
     {
-        _memory->_musicChannel.volume = 0.f;
-        _memory->_musicChannel.volume_step = 1000.f / fade_len;
+        _audioState._musicChannel.volume = 0.f;
+        _audioState._musicChannel.volume_step = 1000.f / fade_len;
     }

     set_music_pattern(pattern);
 }

 void Audio::set_music_pattern(int pattern) {
-    _memory->_musicChannel.pattern = pattern;
-    _memory->_musicChannel.offset = 0;
+    _audioState._musicChannel.pattern = pattern;
+    _audioState._musicChannel.offset = 0;

     //array to access song's channels. may be better to have this part of the struct?
     uint8_t channels[] = {
@@ -129,7 +130,7 @@ void Audio::set_music_pattern(int pattern) {
     };

     // Find music speed; it’s the speed of the fastest sfx
-    _memory->_musicChannel.master = _memory->_musicChannel.speed = -1;
+    _audioState._musicChannel.master = _audioState._musicChannel.speed = -1;
     for (int i = 0; i < 4; ++i)
     {
         uint8_t n = channels[i];
@@ -138,30 +139,30 @@ void Audio::set_music_pattern(int pattern) {
             continue;

         auto &sfx = _memory->sfx[n & 0x3f];
-        if (_memory->_musicChannel.master == -1 || _memory->_musicChannel.speed > sfx.speed)
+        if (_audioState._musicChannel.master == -1 || _audioState._musicChannel.speed > sfx.speed)
         {
-            _memory->_musicChannel.master = i;
-            _memory->_musicChannel.speed = std::max(1, (int)sfx.speed);
+            _audioState._musicChannel.master = i;
+            _audioState._musicChannel.speed = std::max(1, (int)sfx.speed);
         }
     }

     // Play music sfx on active channels
     for (int i = 0; i < 4; ++i)
     {
-        if (((1 << i) & _memory->_musicChannel.mask) == 0)
+        if (((1 << i) & _audioState._musicChannel.mask) == 0)
             continue;

         uint8_t n = channels[i];
         if (n & 0x40)
             continue;

-        _memory->_sfxChannels[i].sfxId = n;
-        _memory->_sfxChannels[i].offset = 0.f;
-        _memory->_sfxChannels[i].phi = 0.f;
-        _memory->_sfxChannels[i].can_loop = false;
-        _memory->_sfxChannels[i].is_music = true;
-        _memory->_sfxChannels[i].prev_key = 24;
-        _memory->_sfxChannels[i].prev_vol = 0.f;
+        _audioState._sfxChannels[i].sfxId = n;
+        _audioState._sfxChannels[i].offset = 0.f;
+        _audioState._sfxChannels[i].phi = 0.f;
+        _audioState._sfxChannels[i].can_loop = false;
+        _audioState._sfxChannels[i].is_music = true;
+        _audioState._sfxChannels[i].prev_key = 24;
+        _audioState._sfxChannels[i].prev_vol = 0.f;
     }
 }

@@ -199,43 +200,43 @@ int16_t Audio::getSampleForChannel(int channel){

     int16_t sample = 0;

-    const int index = _memory->_sfxChannels[channel].sfxId;
+    const int index = _audioState._sfxChannels[channel].sfxId;

     // Advance music using the master channel
-    if (channel == _memory->_musicChannel.master && _memory->_musicChannel.pattern != -1)
+    if (channel == _audioState._musicChannel.master && _audioState._musicChannel.pattern != -1)
     {
-        float const offset_per_second = 22050.f / (183.f * _memory->_musicChannel.speed);
+        float const offset_per_second = 22050.f / (183.f * _audioState._musicChannel.speed);
         float const offset_per_sample = offset_per_second / samples_per_second;
-        _memory->_musicChannel.offset += offset_per_sample;
-        _memory->_musicChannel.volume += _memory->_musicChannel.volume_step / samples_per_second;
-        _memory->_musicChannel.volume = std::clamp(_memory->_musicChannel.volume, 0.f, 1.f);
+        _audioState._musicChannel.offset += offset_per_sample;
+        _audioState._musicChannel.volume += _audioState._musicChannel.volume_step / samples_per_second;
+        _audioState._musicChannel.volume = std::clamp(_audioState._musicChannel.volume, 0.f, 1.f);

-        if (_memory->_musicChannel.volume_step < 0 && _memory->_musicChannel.volume <= 0)
+        if (_audioState._musicChannel.volume_step < 0 && _audioState._musicChannel.volume <= 0)
         {
             // Fade out is finished, stop playing the current song
             for (int i = 0; i < 4; ++i) {
-                if (_memory->_sfxChannels[i].is_music) {
-                    _memory->_sfxChannels[i].sfxId = -1;
+                if (_audioState._sfxChannels[i].is_music) {
+                    _audioState._sfxChannels[i].sfxId = -1;
                 }
             }
-            _memory->_musicChannel.pattern = -1;
+            _audioState._musicChannel.pattern = -1;
         }
-        else if (_memory->_musicChannel.offset >= 32.f)
+        else if (_audioState._musicChannel.offset >= 32.f)
         {
-            int16_t next_pattern = _memory->_musicChannel.pattern + 1;
-            int16_t next_count = _memory->_musicChannel.count + 1;
+            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[_memory->_musicChannel.pattern].stop) //stop part of the loop flag
+            if (_memory->songs[_audioState._musicChannel.pattern].stop) //stop part of the loop flag
             {
                 next_pattern = -1;
-                next_count = _memory->_musicChannel.count;
+                next_count = _audioState._musicChannel.count;
             }
-            else if (_memory->songs[_memory->_musicChannel.pattern].loop){
+            else if (_memory->songs[_audioState._musicChannel.pattern].loop){
                 while (--next_pattern > 0 && !_memory->songs[next_pattern].start)
                     ;
             }

-            _memory->_musicChannel.count = next_count;
+            _audioState._musicChannel.count = next_count;
             set_music_pattern(next_pattern);
         }
     }
@@ -250,8 +251,8 @@ int16_t Audio::getSampleForChannel(int channel){
     // Speed must be 1—255 otherwise the SFX is invalid
     int const speed = max(1, (int)sfx.speed);

-    float const offset = _memory->_sfxChannels[channel].offset;
-    float const phi = _memory->_sfxChannels[channel].phi;
+    float const offset = _audioState._sfxChannels[channel].offset;
+    float const phi = _audioState._sfxChannels[channel].phi;

     // PICO-8 exports instruments as 22050 Hz WAV files with 183 samples
     // per speed unit per note, so this is how much we should advance
@@ -262,7 +263,7 @@ int16_t Audio::getSampleForChannel(int channel){
     // Handle SFX loops. From the documentation: “Looping is turned
     // off when the start index >= end index”.
     float const loop_range = float(sfx.loopRangeEnd - sfx.loopRangeStart);
-    if (loop_range > 0.f && next_offset >= sfx.loopRangeStart && _memory->_sfxChannels[channel].can_loop) {
+    if (loop_range > 0.f && next_offset >= sfx.loopRangeStart && _audioState._sfxChannels[channel].can_loop) {
         next_offset = fmod(next_offset - sfx.loopRangeStart, loop_range)
                     + sfx.loopRangeStart;
     }
@@ -276,14 +277,14 @@ int16_t Audio::getSampleForChannel(int channel){

     if (volume == 0.f){
         //volume all the way off. return silence, but make sure to set stuff
-        _memory->_sfxChannels[channel].offset = next_offset;
+        _audioState._sfxChannels[channel].offset = next_offset;

         if (next_offset >= 32.f){
-            _memory->_sfxChannels[channel].sfxId = -1;
+            _audioState._sfxChannels[channel].sfxId = -1;
         }
         else if (next_note_idx != note_idx){
-            _memory->_sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
-            _memory->_sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
+            _audioState._sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
+            _audioState._sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
         }

         return 0;
@@ -308,16 +309,16 @@ int16_t Audio::getSampleForChannel(int channel){
     //    sample = sample / 0x1000 * 0x1249;
     //}

-    _memory->_sfxChannels[channel].phi = phi + freq / samples_per_second;
+    _audioState._sfxChannels[channel].phi = phi + freq / samples_per_second;

-    _memory->_sfxChannels[channel].offset = next_offset;
+    _audioState._sfxChannels[channel].offset = next_offset;

     if (next_offset >= 32.f){
-        _memory->_sfxChannels[channel].sfxId = -1;
+        _audioState._sfxChannels[channel].sfxId = -1;
     }
     else if (next_note_idx != note_idx){
-        _memory->_sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
-        _memory->_sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
+        _audioState._sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
+        _audioState._sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
     }

     return sample;
diff --git a/source/Audio.h b/source/Audio.h
index 2659fbc..78b6a0d 100644
--- a/source/Audio.h
+++ b/source/Audio.h
@@ -76,6 +76,7 @@ note is encoded in 16 bits, LSB first, like so:

 class Audio {
     PicoRam* _memory;
+    audioState _audioState;

     int16_t getSampleForChannel(int channel);

diff --git a/source/PicoRam.h b/source/PicoRam.h
index f41e287..1d1bb3d 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -91,42 +91,145 @@ struct sfxChannel {
     float prev_vol = 0;
 };

+struct audioState {
+    musicChannel _musicChannel;
+    sfxChannel _sfxChannels[4];
+};

-struct PicoRam
-{
-    uint8_t spriteSheetData[128 * 64];
-	uint8_t mapData[128 * 32];
-	uint8_t spriteFlags[256];
+struct drawState {
+    uint8_t drawPaletteMap[16];
+	uint8_t screenPaletteMap[16];

-    struct song songs[64];
+    uint8_t clip_xb;
+	uint8_t clip_yb;
+	uint8_t clip_xe;
+	uint8_t clip_ye;

-    struct sfx sfx[64];
+    uint8_t unknown05f24;

-    musicChannel _musicChannel;
-    sfxChannel _sfxChannels[4];
-
+    uint8_t color;
+
+    uint8_t text_x;
+	uint8_t text_y;
+
+    uint8_t camera_x;
+	uint8_t camera_y;
+
+    uint8_t drawMode;
+
+    uint8_t devkitMode;
+
+    uint8_t persistPalette;
+
+    uint8_t suppressPause;
+
+    uint16_t fillPattern;
+
+    uint8_t fillPatternTransparencyBit;
+
+    uint8_t colorSettingFlag;
+
+    uint8_t lineInvalid;
+
+    uint8_t unknown05f36;
+    uint8_t unknown05f37;
+
+    uint8_t tlineMapWidth;
+    uint8_t tlineMapHeight;
+    uint8_t tlineMapXOffset;
+    uint8_t tlineMapYOffset;
+
+    int16_t line_x;
+    int16_t line_y;
+};
+
+
+struct hwState {
+    uint8_t audioHardwareState[4];
+
+    uint8_t rngState[8];
+
+    uint8_t buttonStates[8];
+
+    uint8_t unknownInputBlock[8];
+
+    uint8_t btnpRepeatDelay;
+
+    uint8_t btnpRepeatInterval;
+
+    uint8_t colorBitmask;
+
+    uint8_t alternatePaletteFlag;

-	uint8_t _gfxState_color;
-    uint8_t _gfxState_bgColor;
+    uint8_t alternatePaletteMap[16];

-    int _gfxState_text_x;
-	int _gfxState_text_y;
+    uint8_t alternatePaletteScreenLineBitfield[16];

-	int _gfxState_camera_x;
-	int _gfxState_camera_y;
+    uint8_t gpioPins[128];
+
+};

-	int _gfxState_clip_xb;
-	int _gfxState_clip_yb;
-	int _gfxState_clip_xe;
-	int _gfxState_clip_ye;

-	uint8_t _gfxState_drawPaletteMap[16];
-	uint8_t _gfxState_screenPaletteMap[16];
-	bool _gfxState_transparencyPalette[16];

-	//not actually part of graphics state memory?
-	int _gfxState_line_x;
-	int _gfxState_line_y;
-	bool _gfxState_line_valid;
+struct PicoRam
+{
+    union
+    {
+        struct
+        {
+            //0x0     0x0fff    Sprite sheet (0-127)
+            //0x1000  0x1fff    Sprite sheet (128-255) / Map (rows 32-63) (shared)
+            uint8_t spriteSheetData[128 * 64];
+            //0x2000  0x2fff    Map (rows 0-31)
+            uint8_t mapData[128 * 32];
+            //0x3000  0x30ff    Sprite flags
+            uint8_t spriteFlags[256];
+            //0x3100  0x31ff    Music
+            struct song songs[64];
+            //0x3200  0x42ff    Sound effects
+            struct sfx sfx[64];
+            //0x4300  0x5dff    General use (or work RAM)
+            uint8_t generalUseRam[6912];
+            //0x5e00  0x5eff    Persistent cart data (64 numbers = 256 bytes)
+            uint8_t cartData[256];
+            musicChannel _musicChannel;
+            sfxChannel _sfxChannels[4];
+
+            drawState drawState;
+
+            hwState hwState;
+
+            //TODO use draw state struct here
+            /*
+            uint8_t _gfxState_color;
+            uint8_t _gfxState_bgColor;
+
+            int _gfxState_text_x;
+            int _gfxState_text_y;
+
+            int _gfxState_camera_x;
+            int _gfxState_camera_y;
+
+            int _gfxState_clip_xb;
+            int _gfxState_clip_yb;
+            int _gfxState_clip_xe;
+            int _gfxState_clip_ye;
+
+            uint8_t _gfxState_drawPaletteMap[16];
+            uint8_t _gfxState_screenPaletteMap[16];
+            bool _gfxState_transparencyPalette[16];
+
+            //not actually part of graphics state memory?
+            int _gfxState_line_x;
+            int _gfxState_line_y;
+            bool _gfxState_line_valid;
+            */
+
+
+
+            uint8_t screenBuffer[128 * 64];
+        };

+        uint8_t data[0x8000];
+    };
 };
\ No newline at end of file
diff --git a/source/graphics.cpp b/source/graphics.cpp
index a3c9815..f8dd406 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -49,11 +49,12 @@ Graphics::Graphics(std::string fontdata, PicoRam* memory) {


 uint8_t* Graphics::GetP8FrameBuffer(){
+	//TODO: replace with ram's screen buffer
 	return this->_pico8_fb;
 }

 uint8_t* Graphics::GetScreenPaletteMap(){
-	return _memory->_gfxState_screenPaletteMap;
+	return _memory->drawState.screenPaletteMap;
 }

 Color* Graphics::GetPaletteColors(){
@@ -81,9 +82,9 @@ void Graphics::copySpriteToScreen(
 	applyCameraToPoint(&scr_x, &scr_y);

 	// left clip
-	if (scr_x < _memory->_gfxState_clip_xb) {
-		int nclip = _memory->_gfxState_clip_xb - scr_x;
-		scr_x = _memory->_gfxState_clip_xb;
+	if (scr_x < _memory->drawState.clip_xb) {
+		int nclip = _memory->drawState.clip_xb - scr_x;
+		scr_x = _memory->drawState.clip_xb;
 		scr_w -= nclip;
 		if (!flip_x) {
 			spr_x += nclip;
@@ -93,15 +94,15 @@ void Graphics::copySpriteToScreen(
 	}

 	// right clip
-	if (scr_x + scr_w > _memory->_gfxState_clip_xe) {
-		int nclip = (scr_x + scr_w) - _memory->_gfxState_clip_xe;
+	if (scr_x + scr_w > _memory->drawState.clip_xe) {
+		int nclip = (scr_x + scr_w) - _memory->drawState.clip_xe;
 		scr_w -= nclip;
 	}

 	// top clip
-	if (scr_y < _memory->_gfxState_clip_yb) {
-		int nclip = _memory->_gfxState_clip_yb - scr_y;
-		scr_y = _memory->_gfxState_clip_yb;
+	if (scr_y < _memory->drawState.clip_yb) {
+		int nclip = _memory->drawState.clip_yb - scr_y;
+		scr_y = _memory->drawState.clip_yb;
 		scr_h -= nclip;
 		if (!flip_y) {
 			spr_y += nclip;
@@ -111,8 +112,8 @@ void Graphics::copySpriteToScreen(
 	}

 	// bottom clip
-	if (scr_y + scr_h > _memory->_gfxState_clip_ye) {
-		int nclip = (scr_y + scr_h) - _memory->_gfxState_clip_ye;
+	if (scr_y + scr_h > _memory->drawState.clip_ye) {
+		int nclip = (scr_y + scr_h) - _memory->drawState.clip_ye;
 		scr_h -= nclip;
 	}

@@ -190,9 +191,9 @@ void Graphics::copyStretchSpriteToScreen(
 	int dy = spr_h / scr_h;

 	// left clip
-	if (scr_x < _memory->_gfxState_clip_xb) {
-		int nclip = _memory->_gfxState_clip_xb - scr_x;
-		scr_x = _memory->_gfxState_clip_xb;
+	if (scr_x < _memory->drawState.clip_xb) {
+		int nclip = _memory->drawState.clip_xb - scr_x;
+		scr_x = _memory->drawState.clip_xb;
 		scr_w -= nclip;
 		if (!flip_x) {
 			spr_x += nclip * dx;
@@ -202,15 +203,15 @@ void Graphics::copyStretchSpriteToScreen(
 	}

 	// right clip
-	if (scr_x + scr_w > _memory->_gfxState_clip_xe) {
-		int nclip = (scr_x + scr_w) - _memory->_gfxState_clip_xe;
+	if (scr_x + scr_w > _memory->drawState.clip_xe) {
+		int nclip = (scr_x + scr_w) - _memory->drawState.clip_xe;
 		scr_w -= nclip;
 	}

 	// top clip
-	if (scr_y < _memory->_gfxState_clip_yb) {
-		int nclip = _memory->_gfxState_clip_yb - scr_y;
-		scr_y = _memory->_gfxState_clip_yb;
+	if (scr_y < _memory->drawState.clip_yb) {
+		int nclip = _memory->drawState.clip_yb - scr_y;
+		scr_y = _memory->drawState.clip_yb;
 		scr_h -= nclip;
 		if (!flip_y) {
 			spr_y += nclip * dy;
@@ -220,8 +221,8 @@ void Graphics::copyStretchSpriteToScreen(
 	}

 	// bottom clip
-	if (scr_y + scr_h > _memory->_gfxState_clip_ye) {
-		int nclip = (scr_y + scr_h) - _memory->_gfxState_clip_ye;
+	if (scr_y + scr_h > _memory->drawState.clip_ye) {
+		int nclip = (scr_y + scr_h) - _memory->drawState.clip_ye;
 		scr_h -= nclip;
 	}

@@ -271,8 +272,8 @@ void Graphics::swap(int *x, int *y) {
 }

 void Graphics::applyCameraToPoint(int *x, int *y) {
-	*x -= _memory->_gfxState_camera_x;
-	*y -= _memory->_gfxState_camera_y;
+	*x -= _memory->drawState.camera_x;
+	*y -= _memory->drawState.camera_y;
 }

 void Graphics::sortPointsLtoR(int *x1, int *y1, int *x2, int *y2){
@@ -302,37 +303,37 @@ bool Graphics::isOnScreen(int x, int y) {

 bool Graphics::isColorTransparent(uint8_t color) {
 	color = color & 15;
-	return _memory->_gfxState_transparencyPalette[color];
+	return _memory->drawState.drawPaletteMap[color] | BITMASK(4); //4th bit is transparency
 }

 uint8_t Graphics::getDrawPalMappedColor(uint8_t color) {
 	color = color & 15;
-	return _memory->_gfxState_drawPaletteMap[color];
+	return _memory->drawState.drawPaletteMap[color] & 15; //bits 0-3 are color
 }

 uint8_t Graphics::getScreenPalMappedColor(uint8_t color) {
 	color = color & 15;
-	return _memory->_gfxState_screenPaletteMap[color];
+	return _memory->drawState.screenPaletteMap[color] & 15;
 }

 bool Graphics::isWithinClip(int x, int y) {
 	return
-		x >= _memory->_gfxState_clip_xb &&
-		x < _memory->_gfxState_clip_xe &&
-		y >= _memory->_gfxState_clip_yb &&
-		y < _memory->_gfxState_clip_ye;
+		x >= _memory->drawState.clip_xb &&
+		x < _memory->drawState.clip_xe &&
+		y >= _memory->drawState.clip_yb &&
+		y < _memory->drawState.clip_ye;
 }

 bool Graphics::isXWithinClip(int x) {
 	return
-		x >= _memory->_gfxState_clip_xb &&
-		x < _memory->_gfxState_clip_xe;
+		x >= _memory->drawState.clip_xb &&
+		x < _memory->drawState.clip_xe;
 }

 bool Graphics::isYWithinClip(int y) {
 	return
-		y >= _memory->_gfxState_clip_yb &&
-		y < _memory->_gfxState_clip_ye;
+		y >= _memory->drawState.clip_yb &&
+		y < _memory->drawState.clip_ye;
 }


@@ -343,21 +344,21 @@ int clampCoordToScreenDims(int val) {
 int Graphics::clampXCoordToClip(int x) {
 	return std::clamp(
 		x,
-		_memory->_gfxState_clip_xb,
-		_memory->_gfxState_clip_xe - 1);
+		_memory->drawState.clip_xb,
+		_memory->drawState.clip_xe - 1);
 }

 int Graphics::clampYCoordToCLip(int y) {
 	return std::clamp(
 		y,
-		_memory->_gfxState_clip_yb,
-		_memory->_gfxState_clip_ye - 1);
+		_memory->drawState.clip_yb,
+		_memory->drawState.clip_ye - 1);
 }


 void Graphics::_private_safe_pset(int x, int y, uint8_t col) {
 	if (isWithinClip(x, y)){
-		_pico8_fb[(x * 128) + y] = _memory->_gfxState_drawPaletteMap[col];
+		_pico8_fb[(x * 128) + y] = getDrawPalMappedColor(col);
 	}
 }

@@ -365,7 +366,7 @@ void Graphics::_private_pset(int x, int y, uint8_t col) {
 	x = x & 127;
 	y = y & 127;

-	_pico8_fb[(x * 128) + y] = _memory->_gfxState_drawPaletteMap[col];
+	_pico8_fb[(x * 128) + y] = getDrawPalMappedColor(col);
 }
 //end helper methods

@@ -376,12 +377,12 @@ void Graphics::cls() {
 void Graphics::cls(uint8_t color) {
 	memset(_pico8_fb, color, sizeof(_pico8_fb));

-	_memory->_gfxState_text_x = 0;
-	_memory->_gfxState_text_y = 0;
+	_memory->drawState.text_x = 0;
+	_memory->drawState.text_y = 0;
 }

 void Graphics::pset(int x, int y){
-	this->pset(x, y, _memory->_gfxState_color);
+	this->pset(x, y, _memory->drawState.color);
 }

 void Graphics::pset(int x, int y, uint8_t col){
@@ -405,14 +406,14 @@ uint8_t Graphics::pget(int x, int y){
 }

 void Graphics::color(uint8_t col){
-	this->_memory->_gfxState_color = col;
+	this->_memory->drawState.color = col;
 }

 void Graphics::line () {
 	//just invalidate line state
-	this->_memory->_gfxState_line_x = 0;
-	this->_memory->_gfxState_line_y = 0;
-	this->_memory->_gfxState_line_valid = false;
+	this->_memory->drawState.line_x = 0;
+	this->_memory->drawState.line_y = 0;
+	this->_memory->drawState.lineInvalid = 1;
 }

 void Graphics::line (uint8_t col){
@@ -422,19 +423,19 @@ void Graphics::line (uint8_t col){
 }

 void Graphics::line (int x1, int y1){
-	if (this->_memory->_gfxState_line_valid){
-		this->line(_memory->_gfxState_line_x, _memory->_gfxState_line_y, x1, y1, _memory->_gfxState_color);
+	if (this->_memory->drawState.lineInvalid == false){
+		this->line(_memory->drawState.line_x, _memory->drawState.line_y, x1, y1, _memory->drawState.color);
 	}
 }

 void Graphics::line (int x1, int y1, uint8_t col){
-	if (_memory->_gfxState_line_valid){
-		this->line(_memory->_gfxState_line_x, _memory->_gfxState_line_y, x1, y1, col);
+	if (_memory->drawState.lineInvalid == false){
+		this->line(_memory->drawState.line_x, _memory->drawState.line_y, x1, y1, col);
 	}
 }

 void Graphics::line (int x1, int y1, int x2, int y2){
-	this->line(x1, y1, x2, y2, _memory->_gfxState_color);
+	this->line(x1, y1, x2, y2, _memory->drawState.color);
 }

 void Graphics::_private_h_line (int x1, int x2, int y, uint8_t col){
@@ -473,9 +474,9 @@ void Graphics::_private_v_line (int y1, int y2, int x, uint8_t col){
 }

 void Graphics::line(int x0, int y0, int x1, int y1, uint8_t col) {
-	_memory->_gfxState_line_x = x1;
-	_memory->_gfxState_line_y = y1;
-	_memory->_gfxState_line_valid = true;
+	_memory->drawState.line_x = x1;
+	_memory->drawState.line_y = y1;
+	_memory->drawState.lineInvalid = 0;

 	applyCameraToPoint(&x0, &y0);
 	applyCameraToPoint(&x1, &y1);
@@ -518,7 +519,7 @@ void Graphics::circ(int ox, int oy){
 }

 void Graphics::circ(int ox, int oy, int r){
-	this->circ(ox, oy, r, _memory->_gfxState_color);
+	this->circ(ox, oy, r, _memory->drawState.color);
 }

 void Graphics::circ(int ox, int oy, int r, uint8_t col){
@@ -558,7 +559,7 @@ void Graphics::circfill(int ox, int oy){
 }

 void Graphics::circfill(int ox, int oy, int r){
-	this->circfill(ox, oy, r, _memory->_gfxState_color);
+	this->circfill(ox, oy, r, _memory->drawState.color);
 }

 void Graphics::circfill(int ox, int oy, int r, uint8_t col){
@@ -590,7 +591,7 @@ void Graphics::circfill(int ox, int oy, int r, uint8_t col){
 }

 void Graphics::rect(int x1, int y1, int x2, int y2) {
-	this->rect(x1, y1, x2, y2, _memory->_gfxState_color);
+	this->rect(x1, y1, x2, y2, _memory->drawState.color);
 }

 void Graphics::rect(int x1, int y1, int x2, int y2, uint8_t col) {
@@ -609,7 +610,7 @@ void Graphics::rect(int x1, int y1, int x2, int y2, uint8_t col) {
 }

 void Graphics::rectfill(int x1, int y1, int x2, int y2) {
-	this->rectfill(x1, y1, x2, y2, _memory->_gfxState_color);
+	this->rectfill(x1, y1, x2, y2, _memory->drawState.color);
 }

 void Graphics::rectfill(int x1, int y1, int x2, int y2, uint8_t col) {
@@ -626,31 +627,31 @@ void Graphics::rectfill(int x1, int y1, int x2, int y2, uint8_t col) {
 }

 int Graphics::print(std::string str) {
-	int result = this->print(str, _memory->_gfxState_text_x, _memory->_gfxState_text_y);
+	int result = this->print(str, _memory->drawState.text_x, _memory->drawState.text_y);

-	_memory->_gfxState_text_y += 6;
+	_memory->drawState.text_y += 6;

 	return result;
 }

 int Graphics::print(std::string str, int x, int y) {
-	return this->print(str, x, y, _memory->_gfxState_color);
+	return this->print(str, x, y, _memory->drawState.color);
 }

 //based on tac08 impl
 int Graphics::print(std::string str, int x, int y, uint16_t c) {
 	color(c);

-	_memory->_gfxState_text_x = x;
-	_memory->_gfxState_text_y = y;
+	_memory->drawState.text_x = x;
+	_memory->drawState.text_y = y;

 	//font sprite sheet has text as color 7, with 0 as transparent. We need to override
 	//these values and restore them after
-	uint8_t prevCol7Map = _memory->_gfxState_drawPaletteMap[7];
+	uint8_t prevCol7Map = _memory->drawState.drawPaletteMap[7];
 	bool prevCol0Transp = isColorTransparent(0);

-	_memory->_gfxState_drawPaletteMap[7] = c;
-	_memory->_gfxState_transparencyPalette[0] = true;
+	_memory->drawState.drawPaletteMap[7] = c | BITMASK(4);
+	palt(0, true);


 	for (size_t n = 0; n < str.length(); n++) {
@@ -664,13 +665,13 @@ int Graphics::print(std::string str, int x, int y, uint16_t c) {
 			copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5, false, false);
 			x += 8;
 		} else if (ch == '\n') {
-			x = _memory->_gfxState_text_x;
+			x = _memory->drawState.text_x;
 			y += 6;
 		}
 	}

-	_memory->_gfxState_drawPaletteMap[7] = prevCol7Map;
-	_memory->_gfxState_transparencyPalette[0] = prevCol0Transp;
+	_memory->drawState.drawPaletteMap[7] = prevCol7Map;
+	palt(0, prevCol0Transp);

 	//todo: auto scrolling

@@ -761,8 +762,8 @@ void Graphics::camera() {
 }

 void Graphics::camera(int x, int y) {
-	_memory->_gfxState_camera_x = x;
-	_memory->_gfxState_camera_y = y;
+	_memory->drawState.camera_x = x;
+	_memory->drawState.camera_y = y;
 }

 void Graphics::clip() {
@@ -772,10 +773,10 @@ void Graphics::clip() {
 void Graphics::clip(int x, int y, int w, int h) {
 	int xe = x + w;
 	int ye = y + h;
-	_memory->_gfxState_clip_xb = clampCoordToScreenDims(x);
-	_memory->_gfxState_clip_yb = clampCoordToScreenDims(y);
-	_memory->_gfxState_clip_xe = clampCoordToScreenDims(xe);
-	_memory->_gfxState_clip_ye = clampCoordToScreenDims(ye);
+	_memory->drawState.clip_xb = clampCoordToScreenDims(x);
+	_memory->drawState.clip_yb = clampCoordToScreenDims(y);
+	_memory->drawState.clip_xe = clampCoordToScreenDims(xe);
+	_memory->drawState.clip_ye = clampCoordToScreenDims(ye);
 }


@@ -817,8 +818,8 @@ void Graphics::map(int celx, int cely, int sx, int sy, int celw, int celh, uint8

 void Graphics::pal() {
 	for (uint8_t c = 0; c < 16; c++) {
-		_memory->_gfxState_drawPaletteMap[c] = c;
-		_memory->_gfxState_screenPaletteMap[c] = c;
+		_memory->drawState.drawPaletteMap[c] = c;
+		_memory->drawState.screenPaletteMap[c] = c;
 	}

 	this->palt();
@@ -827,22 +828,29 @@ void Graphics::pal() {
 void Graphics::pal(uint8_t c0, uint8_t c1, uint8_t p){
 	if (c0 < 16 && c1 < 16) {
 		if (p == 0) {
-			_memory->_gfxState_drawPaletteMap[c0] = c1;
+			_memory->drawState.drawPaletteMap[c0] = c1;
 		} else if (p == 1) {
-			_memory->_gfxState_screenPaletteMap[c0] = c1;
+			_memory->drawState.screenPaletteMap[c0] = c1;
 		}
 	}
 }

 void Graphics::palt() {
-	for (uint8_t c = 0; c < 16; c++) {
-		_memory->_gfxState_transparencyPalette[c] = c == 0 ? true : false;
+	_memory->drawState.drawPaletteMap[0] &= ~(1UL << 4);
+	for (uint8_t c = 1; c < 16; c++) {
+		_memory->drawState.drawPaletteMap[c] |= BITMASK(4);
 	}
 }

 void Graphics::palt(uint8_t c, bool t){
 	if (c < 16) {
-		_memory->_gfxState_transparencyPalette[c] = t;
+		if (t) {
+			_memory->drawState.drawPaletteMap[c] |= BITMASK(4);
+		}
+		else {
+			_memory->drawState.drawPaletteMap[c] &= ~(1UL << 4);
+
+		}
 	}
 }

@@ -852,8 +860,8 @@ void Graphics::cursor() {
 }

 void Graphics::cursor(int x, int y) {
-	_memory->_gfxState_text_x = x;
-	_memory->_gfxState_text_y = y;
+	_memory->drawState.text_x = x;
+	_memory->drawState.text_y = y;
 }

 void Graphics::cursor(int x, int y, uint8_t col) {