Skip to content

Commit

Permalink
Merge pull request #98 from jtothebell/GraphicsMemLocations
Browse files Browse the repository at this point in the history
Graphics mem locations
  • Loading branch information
jtothebell authored Jan 24, 2022
2 parents ef749ed + 349b6d9 commit 315e3d0
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 58 deletions.
45 changes: 32 additions & 13 deletions source/PicoRam.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ struct drawState_t {

uint8_t lineInvalid;

//hardware extension
uint8_t unknown05f36;
uint8_t unknown05f37;

Expand All @@ -196,29 +197,41 @@ struct drawState_t {

struct hwState_t {
//audio hardware mods
//0x5f40
uint8_t half_rate;
//0x5f41
uint8_t reverb;
//0x5f42
uint8_t distort;
//0x5f43
uint8_t lowpass;

//0x5f44..0x5f4b
uint32_t rngState[2];

//0x5f4c..0x5f53
uint8_t buttonStates[8];

uint8_t unknownInputBlock[8];

//0x5f54
uint8_t spriteSheetMemMapping;
//0x5f55
uint8_t screenDataMemMapping;
//0x5f56
uint8_t mapMemMapping;
//0x5f57
uint8_t widthOfTheMap;
//0x5f58..0x5f5b
uint8_t printAttributes[4];
//0x5f5c
uint8_t btnpRepeatDelay;

//0x5f5d
uint8_t btnpRepeatInterval;

//0x5f5e
uint8_t colorBitmask;

//0x5f5f
uint8_t alternatePaletteFlag;

//0x5f60..0x5f6f
uint8_t alternatePaletteMap[16];

//0x5f70..0x5f7f
uint8_t alternatePaletteScreenLineBitfield[16];

//0x5f80..0x5fff
uint8_t gpioPins[128];

};
Expand All @@ -230,9 +243,13 @@ struct PicoRam
void Reset() {
memset(data, 0, 0x4300);
//leave general use memory
memset(data + 0x5600, 0, 0x8000 - 0x5600);
memset(data + 0x5600, 0, 0x10000 - 0x5600);
//colorBitmask starts at 255
hwState.colorBitmask = 0xff;
hwState.spriteSheetMemMapping = 0x00;
hwState.screenDataMemMapping = 0x60;
hwState.mapMemMapping = 0x20;
hwState.widthOfTheMap = 128;
}

union
Expand Down Expand Up @@ -260,8 +277,10 @@ struct PicoRam
hwState_t hwState;

uint8_t screenBuffer[128 * 64];

uint8_t userData[0x8000];
};

uint8_t data[0x8000];
uint8_t data[0x10000];
};
};
89 changes: 64 additions & 25 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ Graphics::Graphics(std::string fontdata, PicoRam* memory) {


uint8_t* Graphics::GetP8FrameBuffer(){
//TODO: replace with ram's screen buffer
return _memory->screenBuffer;
return _memory->hwState.screenDataMemMapping == 0
? _memory->spriteSheetData
: _memory->screenBuffer;
}

uint8_t* Graphics::GetP8SpriteSheetBuffer(){
return _memory->hwState.spriteSheetMemMapping == 0x60
? _memory->screenBuffer
: _memory->spriteSheetData;
}

uint8_t* Graphics::GetScreenPaletteMap(){
Expand Down Expand Up @@ -105,7 +112,7 @@ void Graphics::copySpriteToScreen(

auto &drawState = _memory->drawState;
auto &hwState = _memory->hwState;
auto &screenBuffer = _memory->screenBuffer;
uint8_t *screenBuffer = GetP8FrameBuffer();

const uint8_t writeMask = hwState.colorBitmask & 15;
const uint8_t readMask = hwState.colorBitmask >> 4;
Expand Down Expand Up @@ -256,7 +263,7 @@ void Graphics::copyStretchSpriteToScreen(

auto &drawState = _memory->drawState;
auto &hwState = _memory->hwState;
auto &screenBuffer = _memory->screenBuffer;
uint8_t *screenBuffer = GetP8FrameBuffer();

const uint8_t writeMask = hwState.colorBitmask & 15;
const uint8_t readMask = hwState.colorBitmask >> 4;
Expand Down Expand Up @@ -529,7 +536,7 @@ void Graphics::_setPixelFromSprite(int x, int y, uint8_t col) {

auto &drawState = _memory->drawState;
auto &hwState = _memory->hwState;
auto &screenBuffer = _memory->screenBuffer;
uint8_t *screenBuffer = GetP8FrameBuffer();

//col = getDrawPalMappedColor(col);
col = drawState.drawPaletteMap[col & 0x0f] & 0x0f;
Expand All @@ -555,7 +562,7 @@ void Graphics::_setPixelFromPen(int x, int y) {

auto &drawState = _memory->drawState;
auto &hwState = _memory->hwState;
auto &screenBuffer = _memory->screenBuffer;
uint8_t *screenBuffer = GetP8FrameBuffer();

uint8_t col = drawState.color;

Expand Down Expand Up @@ -609,7 +616,7 @@ void Graphics::cls() {
void Graphics::cls(uint8_t color) {
color = color & 15;
uint8_t val = color << 4 | color;
memset(_memory->screenBuffer, val, sizeof(_memory->screenBuffer));
memset(GetP8FrameBuffer(), val, sizeof(_memory->screenBuffer));

_memory->drawState.text_x = 0;
_memory->drawState.text_y = 0;
Expand Down Expand Up @@ -638,7 +645,7 @@ uint8_t Graphics::pget(int x, int y){
applyCameraToPoint(&x, &y);

if (isOnScreen(x, y)){
return getPixelNibble(x, y, _memory->screenBuffer);
return getPixelNibble(x, y, GetP8FrameBuffer());
}

return 0;
Expand Down Expand Up @@ -744,6 +751,7 @@ void Graphics::_private_h_line(int x1, int x2, int y){
void Graphics::_private_v_line (int y1, int y2, int x){
auto &drawState = _memory->drawState;
auto &hwState = _memory->hwState;
uint8_t * screenBuffer = GetP8FrameBuffer();

if (!(x >= drawState.clip_xb && x < drawState.clip_xe)) {
return;
Expand Down Expand Up @@ -776,7 +784,7 @@ void Graphics::_private_v_line (int y1, int y2, int x){
for (int16_t y = miny; y <= maxy; ++y)
{
int pixIdx = COMBINED_IDX(x, y);
auto &data = _memory->screenBuffer[pixIdx];
auto &data = screenBuffer[pixIdx];
data = (data & mask) | nibble;
}
}
Expand Down Expand Up @@ -1354,7 +1362,7 @@ void Graphics::spr(
int spr_y = (n / 16) * 8;
int16_t spr_w = (int16_t)(w * (fix32)8);
int16_t spr_h = (int16_t)(h * (fix32)8);
copySpriteToScreen(_memory->spriteSheetData, x, y, spr_x, spr_y, spr_w, spr_h, flip_x, flip_y);
copySpriteToScreen(GetP8SpriteSheetBuffer(), x, y, spr_x, spr_y, spr_w, spr_h, flip_x, flip_y);
}

void Graphics::sspr(
Expand All @@ -1369,7 +1377,7 @@ void Graphics::sspr(
bool flip_x = false,
bool flip_y = false)
{
copyStretchSpriteToScreen(_memory->spriteSheetData, sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y);
copyStretchSpriteToScreen(GetP8SpriteSheetBuffer(), sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y);
}

bool Graphics::fget(uint8_t n, uint8_t f){
Expand All @@ -1394,11 +1402,11 @@ void Graphics::fset(uint8_t n, uint8_t v){
}

uint8_t Graphics::sget(uint8_t x, uint8_t y){
return getPixelNibble(x, y, _memory->spriteSheetData);
return getPixelNibble(x, y, GetP8SpriteSheetBuffer());
}

void Graphics::sset(uint8_t x, uint8_t y, uint8_t c){
setPixelNibble(x, y, c, _memory->spriteSheetData);
setPixelNibble(x, y, c, GetP8SpriteSheetBuffer());
}

std::tuple<int16_t, int16_t> Graphics::camera() {
Expand Down Expand Up @@ -1437,30 +1445,61 @@ std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> Graphics::clip(int x, int y, int
}


//map methods heavily based on tac08 implementation
uint8_t Graphics::mget(int celx, int cely){
if (celx < 0 || celx >= 128 || cely < 0 || cely >= 64)
return 0;
const bool bigMap = _memory->hwState.mapMemMapping >= 0x80;
const int mapW = _memory->hwState.widthOfTheMap;
const int idx = cely * mapW + celx;

if (cely < 32) {
return _memory->mapData[cely * 128 + celx];
if (idx < 0) {
return 0;
}
else if (cely < 64){
return _memory->spriteSheetData[cely* 128 + celx];

if (bigMap){
const int mapLocation = _memory->hwState.mapMemMapping << 8;
const int mapSize = 0x10000 - mapLocation;
if (idx >= mapSize){
return 0;
}
const int offset = 0x8000 - mapSize;
return _memory->userData[offset + idx];
}
else {
if (idx < 4096) {
return _memory->mapData[idx];
}
else if (idx < 8192){
return _memory->spriteSheetData[idx];
}
}

return 0;
}

void Graphics::mset(int celx, int cely, uint8_t snum){
if (celx < 0 || celx >= 128 || cely < 0 || cely >= 64)
const bool bigMap = _memory->hwState.mapMemMapping >= 0x80;
const int mapW = _memory->hwState.widthOfTheMap;
const int idx = cely * mapW + celx;

if (idx < 0) {
return;
}

if (cely < 32) {
_memory->mapData[cely * 128 + celx] = snum;
if (bigMap){
const int mapLocation = _memory->hwState.mapMemMapping << 8;
const int mapSize = 0x10000 - mapLocation;
if (idx >= mapSize){
return;
}
const int offset = 0x8000 - mapSize;
_memory->userData[offset + idx] = snum;
}
else if (cely < 64){
_memory->spriteSheetData[cely* 128 + celx] = snum;
else {
if (idx < 4096) {
_memory->mapData[idx] = snum;
}
else if (idx < 8192) {
_memory->spriteSheetData[idx] = snum;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions source/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Graphics {
Graphics(std::string fontdata, PicoRam* memory);

uint8_t* GetP8FrameBuffer();
uint8_t* GetP8SpriteSheetBuffer();
uint8_t* GetScreenPaletteMap();
Color* GetPaletteColors();

Expand Down
21 changes: 11 additions & 10 deletions source/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,10 @@ bool Vm::ExecuteLua(string luaString, string callbackFunction){
return true;
}

const int MemoryUpperBound = 0x10000;

uint8_t Vm::vm_peek(int addr){
if (addr < 0 || addr > 0x8000){
if (addr < 0 || addr > MemoryUpperBound){
return 0;
}

Expand All @@ -634,10 +635,10 @@ int16_t Vm::vm_peek2(int addr){
for (int i = 0; i < 2; ++i)
{
/* This code handles partial reads by adding zeroes */
if (addr + i < 0x8000)
if (addr + i < MemoryUpperBound)
bits |= _memory->data[addr + i] << (8 * i);
else if (addr + i >= 0x8000)
bits |= _memory->data[addr + i - 0x8000] << (8 * i);
else if (addr + i >= MemoryUpperBound)
bits |= _memory->data[addr + i - MemoryUpperBound] << (8 * i);
}

return bits;
Expand All @@ -650,26 +651,26 @@ fix32 Vm::vm_peek4(int addr){
for (int i = 0; i < 4; ++i)
{
/* This code handles partial reads by adding zeroes */
if (addr + i < 0x8000)
if (addr + i < MemoryUpperBound)
bits |= _memory->data[addr + i] << (8 * i);
else if (addr + i >= 0x8000)
bits |= _memory->data[addr + i - 0x8000] << (8 * i);
else if (addr + i >= MemoryUpperBound)
bits |= _memory->data[addr + i - MemoryUpperBound] << (8 * i);
}

return fix32::frombits(bits);
}

void Vm::vm_poke(int addr, uint8_t value){
//todo: check how pico 8 handles out of bounds
if (addr < 0 || addr > 0x8000){
if (addr < 0 || addr > MemoryUpperBound){
return;
}

_memory->data[addr] = value;
}

void Vm::vm_poke2(int addr, int16_t value){
if (addr < 0 || addr > 0x8000 - 1){
if (addr < 0 || addr > MemoryUpperBound - 1){
return;
}

Expand All @@ -679,7 +680,7 @@ void Vm::vm_poke2(int addr, int16_t value){
}

void Vm::vm_poke4(int addr, fix32 value){
if (addr < 0 || addr > 0x8000 - 3){
if (addr < 0 || addr > MemoryUpperBound - 3){
return;
}

Expand Down
Loading

0 comments on commit 315e3d0

Please sign in to comment.