Skip to content

Commit

Permalink
Basic memory implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Jun 29, 2024
1 parent 64ef65c commit 3f28cba
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/geargrafx_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*
*/

#include <stdlib.h>
#include <time.h>
#include "geargrafx_core.h"
#include "common.h"
#include "cartridge.h"
Expand Down Expand Up @@ -52,6 +54,8 @@ void GeargrafxCore::Init(GG_Pixel_Format pixel_format)
{
Debug("--== %s %s by Ignacio Sanchez ==--", GEARGRAFX_TITLE, GEARGRAFX_VERSION);

srand((unsigned int)time(NULL));

m_pixel_format = pixel_format;

m_cartridge = new Cartridge();
Expand Down
17 changes: 17 additions & 0 deletions src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,39 @@
*
*/

#include <stdlib.h>
#include "memory.h"

Memory::Memory(Cartridge* cartridge)
{
m_cartridge = cartridge;
InitPointer(m_wram);
}

Memory::~Memory()
{
SafeDeleteArray(m_wram);
}

void Memory::Init()
{
m_wram = new u8[0x2000];
Reset();
}

void Memory::Reset()
{
m_mpr[7] = 0x00;

for (int i = 0; i < 7; i++)
{
m_mpr[i] = rand() & 0xFF;
}

for (int i = 0; i < 0x2000; i++)
{
m_wram[i] = rand() & 0xFF;
}
}


7 changes: 7 additions & 0 deletions src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ class Memory
void Reset();
u8 Read(u16 address);
void Write(u16 address, u8 value);
void SetMpr(u8 index, u8 value);
u8 GetMpr(u8 index);

private:
Cartridge* m_cartridge;
u8 m_mpr[8];
u8* m_wram;

private:
u32 GetPhysicalAddress(u16 address);
};

#include "memory_inline.h"
Expand Down
172 changes: 148 additions & 24 deletions src/memory_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,160 @@

inline u8 Memory::Read(u16 address)
{
switch (address & 0xE000)
{
case 0x0000:
case 0x2000:
case 0x4000:
case 0x6000:
case 0x8000:
case 0xA000:
case 0xC000:
case 0xE000:
default:
return 0xFF;
u8 mpr_index = (address >> 13) & 0x07;
u8 mpr_value = m_mpr[mpr_index];
u32 physical_address = (mpr_value << 13) | (address & 0x1FFF);

if (mpr_value < 0x80)
{
// HuCard ROM
u8* rom = m_cartridge->GetROM();
return rom[physical_address];
}
else if (mpr_value < 0xF8)
{
// Unused
Debug("Unused read at %06X", physical_address);
return 0xFF;
}
else if (mpr_value < 0xFC)
{
// Work RAM
return m_wram[physical_address & 0x1FFF];
}
else if (mpr_value < 0xFF)
{
// Unused
Debug("Unused read at %06X", physical_address);
return 0xFF;
}
else
{
// Hardware Page
switch (physical_address & 0x001C00)
{
case 0x0000:
// HuC6270
Debug("HuC6270 read at %06X", physical_address);
return 0xFF;
case 0x0400:
// HuC6260
Debug("HuC6260 read at %06X", physical_address);
return 0xFF;
case 0x0800:
// PSG
Debug("PSG read at %06X", physical_address);
return 0xFF;
case 0x0C00:
// Timer
Debug("Timer read at %06X", physical_address);
return 0xFF;
case 0x1000:
// I/O
Debug("I/O read at %06X", physical_address);
return 0xFF;
case 0x1400:
// CD-ROM
Debug("CD-ROM read at %06X", physical_address);
return 0xFF;
case 0x1800:
// Unused
Debug("Unused read at %06X", physical_address);
return 0xFF;
case 0x1C00:
// Unused
Debug("Unused read at %06X", physical_address);
return 0xFF;
default:
return 0xFF;
}
}
}

inline void Memory::Write(u16 address, u8 value)
{
switch (address & 0xE000)
{
case 0x0000:
case 0x2000:
case 0x4000:
case 0x6000:
case 0x8000:
case 0xA000:
case 0xC000:
case 0xE000:
default:
break;
u8 mpr_index = (address >> 13) & 0x07;
u8 mpr_value = m_mpr[mpr_index];
u32 physical_address = (mpr_value << 13) | (address & 0x1FFF);

if (mpr_value < 0x80)
{
// HuCard ROM
Debug("Attempted write to HuCard ROM at %06X", physical_address);
}
else if (mpr_value < 0xF8)
{
// Unused
Debug("Unused write at %06X", physical_address);
}
else if (mpr_value < 0xFC)
{
// Work RAM
m_wram[physical_address & 0x1FFF] = value;
}
else if (mpr_value < 0xFF)
{
// Unused
Debug("Unused write at %06X", physical_address);
}
else
{
// Hardware Page
switch (physical_address & 0x001C00)
{
case 0x0000:
// HuC6270
Debug("HuC6270 write at %06X", physical_address);
break;
case 0x0400:
// HuC6260
Debug("HuC6260 write at %06X", physical_address);
break;
case 0x0800:
// PSG
Debug("PSG write at %06X", physical_address);
break;
case 0x0C00:
// Timer
Debug("Timer write at %06X", physical_address);
break;
case 0x1000:
// I/O
Debug("I/O write at %06X", physical_address);
break;
case 0x1400:
// CD-ROM
Debug("CD-ROM write at %06X", physical_address);
break;
case 0x1800:
// Unused
Debug("Unused write at %06X", physical_address);
break;
case 0x1C00:
// Unused
Debug("Unused write at %06X", physical_address);
break;
default:
break;
}
}
}

inline void Memory::SetMpr(u8 index, u8 value)
{
m_mpr[index] = value;
}

inline u8 Memory::GetMpr(u8 index)
{
return m_mpr[index];
}

inline u32 Memory::GetPhysicalAddress(u16 address)
{
u8 mpr_index = (address >> 13) & 0x07;
u8 mpr_value = m_mpr[mpr_index];
return (mpr_value << 13) | (address & 0x1FFF);
}

#endif /* MEMORY_INLINE_H */

0 comments on commit 3f28cba

Please sign in to comment.