Skip to content

Commit

Permalink
Implement saving and loading of states
Browse files Browse the repository at this point in the history
  • Loading branch information
henridd committed Nov 4, 2023
1 parent 8b5ef83 commit d088e20
Show file tree
Hide file tree
Showing 26 changed files with 1,418 additions and 770 deletions.
1,309 changes: 715 additions & 594 deletions ProjectDMG/DMG/CPU.cs

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions ProjectDMG/DMG/GamePak/IGamePak.cs
@@ -1,10 +1,13 @@
namespace ProjectDMG.DMG.GamePak {
using ProjectDMG.DMG.State.DataStructures.GamePak;

namespace ProjectDMG.DMG.GamePak {
interface IGamePak {
byte ReadLoROM(ushort addr);
byte ReadHiROM(ushort addr);
void WriteROM(ushort addr, byte value);
byte ReadERAM(ushort addr);
void WriteERAM(ushort addr, byte value);
void Init(byte[] ROM);
void Init(byte[] ROM, GamePakSavedState savedState);
GamePakSavedState GetSavedState();
}
}
14 changes: 13 additions & 1 deletion ProjectDMG/DMG/GamePak/MBC0.cs
@@ -1,4 +1,6 @@
namespace ProjectDMG.DMG.GamePak {
using ProjectDMG.DMG.State.DataStructures.GamePak;

namespace ProjectDMG.DMG.GamePak {
class MBC0 : IGamePak {

private byte[] ROM;
Expand Down Expand Up @@ -26,5 +28,15 @@ class MBC0 : IGamePak {
public void WriteROM(ushort addr, byte value) {
//MBC0 should ignore writes
}

public GamePakSavedState GetSavedState()
{
throw new System.NotImplementedException();
}

public void Init(byte[] ROM, GamePakSavedState savedState)
{
throw new System.NotImplementedException();
}
}
}
13 changes: 12 additions & 1 deletion ProjectDMG/DMG/GamePak/MBC1.cs
@@ -1,4 +1,5 @@
using System;
using ProjectDMG.DMG.State.DataStructures.GamePak;
using System;

namespace ProjectDMG.DMG.GamePak {
class MBC1 : IGamePak {
Expand Down Expand Up @@ -64,5 +65,15 @@ class MBC1 : IGamePak {
break;
}
}

public GamePakSavedState GetSavedState()
{
throw new NotImplementedException();
}

public void Init(byte[] ROM, GamePakSavedState savedState)
{
throw new NotImplementedException();
}
}
}
12 changes: 11 additions & 1 deletion ProjectDMG/DMG/GamePak/MBC2.cs
@@ -1,4 +1,5 @@
using System;
using ProjectDMG.DMG.State.DataStructures.GamePak;
using System;

namespace ProjectDMG.DMG.GamePak {
class MBC2 : IGamePak {
Expand Down Expand Up @@ -46,5 +47,14 @@ class MBC2 : IGamePak {
}
}

public GamePakSavedState GetSavedState()
{
throw new NotImplementedException();
}

public void Init(byte[] ROM, GamePakSavedState savedState)
{
throw new NotImplementedException();
}
}
}
48 changes: 44 additions & 4 deletions ProjectDMG/DMG/GamePak/MBC3.cs
@@ -1,12 +1,13 @@
using System;
using ProjectDMG.DMG.State.DataStructures.GamePak;
using System;

namespace ProjectDMG.DMG.GamePak {
class MBC3 : IGamePak {

private byte[] ROM;
private byte[] ERAM = new byte[0x8000]; //MBC1 MAX ERAM on 4 banks
private byte[] ERAM; //MBC1 MAX ERAM on 4 banks
private bool ERAM_ENABLED;
private int ROM_BANK = 1; //default as 0 is 0x0000 - 0x3FFF fixed
private int ROM_BANK;
private int RAM_BANK;
private const int ROM_OFFSET = 0x4000;
private const int ERAM_OFFSET = 0x2000;
Expand All @@ -20,8 +21,29 @@ class MBC3 : IGamePak {
private byte RTC_6; //Bit 6 Halt(0=Active, 1=Stop Timer)
private byte RTC_7; //Bit 7 Day Counter Carry Bit(1=Counter Overflow)

public void Init(byte[] ROM) {
public void Init(byte[] ROM, GamePakSavedState savedState) {
this.ROM = ROM;

if(savedState is MBC3SavedState mBC3SavedState)
{
ERAM = mBC3SavedState.ERAM;
ERAM_ENABLED = mBC3SavedState.ERAM_ENABLED;
ROM_BANK = mBC3SavedState.ROM_BANK;
RAM_BANK = mBC3SavedState.RAM_BANK;
RTC_0 = mBC3SavedState.RTC_0;
RTC_6 = mBC3SavedState.RTC_6;
RTC_7 = mBC3SavedState.RTC_7;
RTC_DH = mBC3SavedState.RTC_DH;
RTC_DL = mBC3SavedState.RTC_DL;
RTC_H = mBC3SavedState.RTC_H;
RTC_M = mBC3SavedState.RTC_M;
RTC_S = mBC3SavedState.RTC_S;
}
else
{
ERAM = new byte[0x8000];
ROM_BANK = 1; //default as 0 is 0x0000 - 0x3FFF fixed
}
}

public byte ReadERAM(ushort addr) {
Expand Down Expand Up @@ -109,5 +131,23 @@ class MBC3 : IGamePak {
}
}

public GamePakSavedState GetSavedState()
{
return new MBC3SavedState()
{
ERAM = ERAM,
ERAM_ENABLED = ERAM_ENABLED,
ROM_BANK = ROM_BANK,
RAM_BANK = RAM_BANK,
RTC_S = RTC_S,
RTC_M = RTC_M,
RTC_H = RTC_H,
RTC_DL = RTC_DL,
RTC_DH = RTC_DH,
RTC_0 = RTC_0,
RTC_6 = RTC_6,
RTC_7 = RTC_7
};
}
}
}
12 changes: 11 additions & 1 deletion ProjectDMG/DMG/GamePak/MBC5.cs
@@ -1,4 +1,5 @@
using System;
using ProjectDMG.DMG.State.DataStructures.GamePak;
using System;

namespace ProjectDMG.DMG.GamePak {
class MBC5 : IGamePak {
Expand Down Expand Up @@ -55,5 +56,14 @@ class MBC5 : IGamePak {
}
}

public GamePakSavedState GetSavedState()
{
throw new NotImplementedException();
}

public void Init(byte[] ROM, GamePakSavedState savedState)
{
throw new NotImplementedException();
}
}
}

0 comments on commit d088e20

Please sign in to comment.