Skip to content

Commit

Permalink
Hackadoodle to change the initial value of the DIV register? This is …
Browse files Browse the repository at this point in the history
…probably a bad idea
  • Loading branch information
nattthebear committed Feb 20, 2018
1 parent 7ae374d commit bd23975
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public bool EqualLengthFrames
[DeepEqualsIgnore]
private bool _equalLengthFrames;

[DisplayName("Initial DIV offset")]
[Description("Internal. Probably doesn't work. Leave this set to 0. Accepts values from 0 to 65532 in steps of 4")]
[DefaultValue(0)]
public int InitialDiv { get; set; }

public GambatteSyncSettings()
{
SettingsUtil.SetDefaultValues(this);
Expand All @@ -146,6 +151,11 @@ public static bool NeedsReboot(GambatteSyncSettings x, GambatteSyncSettings y)
{
return !DeepEquality.DeepEquals(x, y);
}

public uint GetInitialDivInternal()
{
return (uint)(InitialDiv & 0xfffc);
}
}
}
}
8 changes: 6 additions & 2 deletions BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public Gameboy(CoreComm comm, GameInfo game, byte[] file, object settings, objec

real_rtc_time = !DeterministicEmulation && _syncSettings.RealTimeRTC;

DivInternal = _syncSettings.GetInitialDivInternal();

LibGambatte.LoadFlags flags = 0;

switch (_syncSettings.ConsoleMode)
Expand All @@ -88,7 +90,7 @@ public Gameboy(CoreComm comm, GameInfo game, byte[] file, object settings, objec
flags |= LibGambatte.LoadFlags.MULTICART_COMPAT;
}

if (LibGambatte.gambatte_load(GambatteState, file, (uint)file.Length, GetCurrentTime(), flags) != 0)
if (LibGambatte.gambatte_load(GambatteState, file, (uint)file.Length, GetCurrentTime(), flags, DivInternal) != 0)
{
throw new InvalidOperationException("gambatte_load() returned non-zero (is this not a gb or gbc rom?)");
}
Expand Down Expand Up @@ -172,6 +174,8 @@ public Gameboy(CoreComm comm, GameInfo game, byte[] file, object settings, objec
/// </summary>
private LibGambatte.Buttons CurrentButtons = 0;

private uint DivInternal = 0;

#region RTC

/// <summary>
Expand Down Expand Up @@ -322,7 +326,7 @@ internal void FrameAdvancePrep(IController controller)

if (controller.IsPressed("Power"))
{
LibGambatte.gambatte_reset(GambatteState, GetCurrentTime());
LibGambatte.gambatte_reset(GambatteState, GetCurrentTime(), DivInternal);
}

if (Tracer.Enabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public enum CDLog_Flags : int
/// <param name="flags">ORed combination of LoadFlags.</param>
/// <returns>0 on success, negative value on failure.</returns>
[DllImport("libgambatte.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int gambatte_load(IntPtr core, byte[] romdata, uint length, long now, LoadFlags flags);
public static extern int gambatte_load(IntPtr core, byte[] romdata, uint length, long now, LoadFlags flags, uint div);

/// <summary>
/// Load GB BIOS image.
Expand Down Expand Up @@ -124,7 +124,7 @@ public enum CDLog_Flags : int
/// <param name="core">opaque state pointer</param>
/// <param name="now">RTC time when the reset occurs</param>
[DllImport("libgambatte.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void gambatte_reset(IntPtr core, long now);
public static extern void gambatte_reset(IntPtr core, long now, uint div);

/// <summary>
/// palette type for gambatte_setdmgpalettecolor
Expand Down
4 changes: 2 additions & 2 deletions libgambatte/include/gambatte.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class GB {
* @param flags ORed combination of LoadFlags.
* @return 0 on success, negative value on failure.
*/
int load(const char *romfiledata, unsigned romfilelength, std::uint32_t now, unsigned flags = 0);
int load(const char *romfiledata, unsigned romfilelength, std::uint32_t now, unsigned flags, unsigned div);

int loadGBCBios(const char* biosfiledata);
int loadDMGBios(const char* biosfiledata);
Expand Down Expand Up @@ -93,7 +93,7 @@ class GB {
/** Reset to initial state.
* Equivalent to reloading a ROM image, or turning a Game Boy Color off and on again.
*/
void reset(std::uint32_t now);
void reset(std::uint32_t now, unsigned div);

/** @param palNum 0 <= palNum < 3. One of BG_PALETTE, SP1_PALETTE and SP2_PALETTE.
* @param colorNum 0 <= colorNum < 4
Expand Down
8 changes: 4 additions & 4 deletions libgambatte/src/cinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ GBEXPORT void gambatte_destroy(GB *g)
delete g;
}

GBEXPORT int gambatte_load(GB *g, const char *romfiledata, unsigned romfilelength, long long now, unsigned flags)
GBEXPORT int gambatte_load(GB *g, const char *romfiledata, unsigned romfilelength, long long now, unsigned flags, unsigned div)
{
int ret = g->load(romfiledata, romfilelength, now, flags);
int ret = g->load(romfiledata, romfilelength, now, flags, div);
return ret;
}

Expand Down Expand Up @@ -65,9 +65,9 @@ GBEXPORT void gambatte_setlayers(GB *g, unsigned mask)
g->setLayers(mask);
}

GBEXPORT void gambatte_reset(GB *g, long long now)
GBEXPORT void gambatte_reset(GB *g, long long now, unsigned div)
{
g->reset(now);
g->reset(now, div);
}

GBEXPORT void gambatte_setdmgpalettecolor(GB *g, unsigned palnum, unsigned colornum, unsigned rgb32)
Expand Down
8 changes: 4 additions & 4 deletions libgambatte/src/gambatte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void GB::blitTo(gambatte::uint_least32_t *videoBuf, int pitch)
}
}

void GB::reset(const std::uint32_t now) {
void GB::reset(const std::uint32_t now, const unsigned div) {
if (p_->cpu.loaded()) {

int length = p_->cpu.saveSavedataLength();
Expand All @@ -94,7 +94,7 @@ void GB::reset(const std::uint32_t now) {

SaveState state;
p_->cpu.setStatePtrs(state);
setInitState(state, !(p_->loadflags & FORCE_DMG), p_->loadflags & GBA_CGB, now);
setInitState(state, !(p_->loadflags & FORCE_DMG), p_->loadflags & GBA_CGB, now, div);
p_->cpu.loadState(state);
if (length > 0)
{
Expand Down Expand Up @@ -140,7 +140,7 @@ void GB::setLinkCallback(void(*callback)()) {
p_->cpu.setLinkCallback(callback);
}

int GB::load(const char *romfiledata, unsigned romfilelength, const std::uint32_t now, const unsigned flags) {
int GB::load(const char *romfiledata, unsigned romfilelength, const std::uint32_t now, const unsigned flags, const unsigned div) {
//if (p_->cpu.loaded())
// p_->cpu.saveSavedata();

Expand All @@ -150,7 +150,7 @@ int GB::load(const char *romfiledata, unsigned romfilelength, const std::uint32_
SaveState state;
p_->cpu.setStatePtrs(state);
p_->loadflags = flags;
setInitState(state, !(flags & FORCE_DMG), flags & GBA_CGB, now);
setInitState(state, !(flags & FORCE_DMG), flags & GBA_CGB, now, div);
p_->cpu.loadState(state);
//p_->cpu.loadSavedata();
}
Expand Down
4 changes: 2 additions & 2 deletions libgambatte/src/initstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ static void setInitialDmgIoamhram(unsigned char *const ioamhram) {

} // anon namespace

void gambatte::setInitState(SaveState &state, const bool cgb, const bool gbaCgbMode, const std::uint32_t now) {
void gambatte::setInitState(SaveState &state, const bool cgb, const bool gbaCgbMode, const std::uint32_t now, const unsigned div) {
static const unsigned char cgbObjpDump[0x40] = {
0x00, 0x00, 0xF2, 0xAB,
0x61, 0xC2, 0xD9, 0xBA,
Expand Down Expand Up @@ -1198,7 +1198,7 @@ void gambatte::setInitState(SaveState &state, const bool cgb, const bool gbaCgbM
state.mem.ioamhram.ptr[0x140] = 0;
state.mem.ioamhram.ptr[0x144] = 0x00;

state.mem.divLastUpdate = 0;
state.mem.divLastUpdate = 0 - div;
state.mem.timaLastUpdate = 0;
state.mem.tmatime = DISABLED_TIME;
state.mem.nextSerialtime = DISABLED_TIME;
Expand Down
2 changes: 1 addition & 1 deletion libgambatte/src/initstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <cstdint>

namespace gambatte {
void setInitState(struct SaveState &state, bool cgb, bool gbaCgbMode, std::uint32_t now);
void setInitState(struct SaveState &state, bool cgb, bool gbaCgbMode, std::uint32_t now, unsigned div);
}

#endif
2 changes: 1 addition & 1 deletion libgambatte/src/savestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct SaveState {
void set(T *ptr, const unsigned long sz) { this->ptr = ptr; this->sz = sz; }

friend class SaverList;
friend void setInitState(SaveState &, bool, bool, std::uint32_t);
friend void setInitState(SaveState &, bool, bool, std::uint32_t, unsigned);
};

struct CPU {
Expand Down
Binary file modified output/dll/libgambatte.dll
Binary file not shown.

0 comments on commit bd23975

Please sign in to comment.