Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory hook improvements #3821

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Assets/dll/bsnes.wbx.zst
Binary file not shown.
Binary file modified Assets/dll/gpgx.wbx.zst
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void Dispose()
public delegate void snes_no_lag_t(bool sgb_poll);
public delegate string snes_path_request_t(int slot, string hint, bool required);
public delegate void snes_trace_t(string disassembly, string register_info);
public delegate void snes_read_hook_t(uint address);
public delegate void snes_read_hook_t(uint address, byte value);
public delegate void snes_write_hook_t(uint address, byte value);
public delegate void snes_exec_hook_t(uint address);
public delegate long snes_time_t();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,11 @@ private void InitAudio()
private void snes_trace(string disassembly, string registerInfo)
=> _tracer.Put(new(disassembly: disassembly, registerInfo: registerInfo));

private void ReadHook(uint addr)
private void ReadHook(uint addr, byte value)
{
if (MemoryCallbacks.HasReads)
{
MemoryCallbacks.CallMemoryCallbacks(addr, 0, (uint) MemoryCallbackFlags.AccessRead, "System Bus");
MemoryCallbacks.CallMemoryCallbacks(addr, value, (uint) MemoryCallbackFlags.AccessRead, "System Bus");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,28 @@ public void SetCpuRegister(string register, int value)

private void InitMemCallbacks()
{
ExecCallback = new LibGPGX.mem_cb(a =>
ExecCallback = new LibGPGX.mem_cb((a, val) =>
{
if (MemoryCallbacks.HasExecutes)
{
uint flags = (uint)MemoryCallbackFlags.AccessExecute;
MemoryCallbacks.CallMemoryCallbacks(a, 0, flags, "M68K BUS");
MemoryCallbacks.CallMemoryCallbacks(a, val, flags, "M68K BUS");
}
});
ReadCallback = new LibGPGX.mem_cb(a =>
ReadCallback = new LibGPGX.mem_cb((a, val) =>
{
if (MemoryCallbacks.HasReads)
{
uint flags = (uint)MemoryCallbackFlags.AccessRead;
MemoryCallbacks.CallMemoryCallbacks(a, 0, flags, "M68K BUS");
MemoryCallbacks.CallMemoryCallbacks(a, val, flags, "M68K BUS");
}
});
WriteCallback = new LibGPGX.mem_cb(a =>
WriteCallback = new LibGPGX.mem_cb((a, val) =>
{
if (MemoryCallbacks.HasWrites)
{
uint flags = (uint)MemoryCallbackFlags.AccessWrite;
MemoryCallbacks.CallMemoryCallbacks(a, 0, flags, "M68K BUS");
MemoryCallbacks.CallMemoryCallbacks(a, val, flags, "M68K BUS");
}
});
_memoryCallbacks.ActiveChanged += RefreshMemCallbacks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public enum CDLog_Flags
public abstract void gpgx_set_input_callback(input_cb cb);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void mem_cb(uint addr);
public delegate void mem_cb(uint addr, uint value); //value MUST be uint, since the value will be trimmed if the type is byte (8-bit) or ushort (16-bit) and the value read/written/executed is bigger than that (i.e 32 bits).

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CDCallback(int addr, CDLog_AddrType addrtype, CDLog_Flags flags);
Expand Down
2 changes: 1 addition & 1 deletion waterbox/bsnescore/bsnes/emulator/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Platform {
bool writeHookEnabled = false;
bool executeHookEnabled = false;
virtual auto cpuTrace(vector<string>) -> void {}
virtual auto readHook(uint address) -> void {}
virtual auto readHook(uint address, uint8 value) -> void {}
virtual auto writeHook(uint address, uint8 value) -> void {}
virtual auto execHook(uint address) -> void {}
virtual auto time() -> int64 { return ::time(0); }
Expand Down
5 changes: 2 additions & 3 deletions waterbox/bsnescore/bsnes/sfc/cpu/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ auto CPU::idle() -> void {
}

auto CPU::read(uint address) -> uint8 {
if (__builtin_expect(platform->readHookEnabled, 0))
platform->readHook(address);

if(address & 0x408000) {
if(address & 0x800000 && io.fastROM) {
status.clockCount = 6;
Expand Down Expand Up @@ -41,6 +38,8 @@ auto CPU::read(uint address) -> uint8 {

status.irqLock = 0;
auto data = bus.read(address, r.mdr);
if (__builtin_expect(platform->readHookEnabled, 0))
platform->readHook(address, data);
step<4,0>();
aluEdge();
//$00-3f,80-bf:4000-43ff reads are internal to CPU, and do not update the MDR
Expand Down
2 changes: 1 addition & 1 deletion waterbox/bsnescore/bsnes/target-bsnescore/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ typedef void (*snes_controller_latch_t)(void);
typedef void (*snes_no_lag_t)(bool sgb_poll);
typedef char* (*snes_path_request_t)(int slot, const char* hint, bool required);
typedef void (*snes_trace_t)(const char* disassembly, const char* register_info);
typedef void (*snes_read_hook_t)(uint32_t address);
typedef void (*snes_read_hook_t)(uint32_t address, uint8_t value);
typedef void (*snes_write_hook_t)(uint32_t address, uint8_t value);
typedef void (*snes_exec_hook_t)(uint32_t address);
typedef int64_t (*snes_time_t)(void);
Expand Down
6 changes: 3 additions & 3 deletions waterbox/bsnescore/bsnes/target-bsnescore/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Program : Emulator::Platform
auto notify(string text) -> void override;
auto getBackdropColor() -> uint16 override;
auto cpuTrace(vector<string>) -> void override;
auto readHook(uint address) -> void override;
auto readHook(uint address, uint8 value) -> void override;
auto writeHook(uint address, uint8 value) -> void override;
auto execHook(uint address) -> void override;
auto time() -> int64 override;
Expand Down Expand Up @@ -482,9 +482,9 @@ auto Program::cpuTrace(vector<string> parts) -> void
snesCallbacks.snes_trace(parts[0], parts[1]);
}

auto Program::readHook(uint address) -> void
auto Program::readHook(uint address, uint8 value) -> void
{
snesCallbacks.snes_read_hook(address);
snesCallbacks.snes_read_hook(address, value);
}

auto Program::writeHook(uint address, uint8 value) -> void
Expand Down
6 changes: 3 additions & 3 deletions waterbox/gpgx/cinterface/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

typedef ECL_ENTRY void (*CDCallback)(int32 addr, int32 addrtype, int32 flags);

extern ECL_ENTRY void (*biz_execcb)(unsigned addr);
extern ECL_ENTRY void (*biz_readcb)(unsigned addr);
extern ECL_ENTRY void (*biz_writecb)(unsigned addr);
extern ECL_ENTRY void (*biz_execcb)(unsigned addr, unsigned int value);
extern ECL_ENTRY void (*biz_readcb)(unsigned addr, unsigned int value);
extern ECL_ENTRY void (*biz_writecb)(unsigned addr, unsigned int value);
extern CDCallback biz_cdcallback;
extern unsigned biz_lastpc;

Expand Down
6 changes: 3 additions & 3 deletions waterbox/gpgx/cinterface/cinterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ static uint8_t brm_format[0x40] =
0x52,0x41,0x4d,0x5f,0x43,0x41,0x52,0x54,0x52,0x49,0x44,0x47,0x45,0x5f,0x5f,0x5f
};

ECL_ENTRY void (*biz_execcb)(unsigned addr);
ECL_ENTRY void (*biz_readcb)(unsigned addr);
ECL_ENTRY void (*biz_writecb)(unsigned addr);
ECL_ENTRY void (*biz_execcb)(unsigned addr, unsigned int value);
ECL_ENTRY void (*biz_readcb)(unsigned addr, unsigned int value);
ECL_ENTRY void (*biz_writecb)(unsigned addr, unsigned int value);
CDCallback biz_cdcallback = NULL;
unsigned biz_lastpc = 0;
ECL_ENTRY void (*cdd_readcallback)(int lba, void *dest, int audio);
Expand Down
2 changes: 1 addition & 1 deletion waterbox/gpgx/core/m68k/m68kcpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void m68k_run(unsigned int cycles)
m68ki_use_data_space() /* auto-disable (see m68kcpu.h) */

if (biz_execcb)
biz_execcb(REG_PC);
biz_execcb(REG_PC, 0); //All read/write/exec callbacks require address and value, but for exec the functionality is not fully implemented, so I am passing 0 as value.

if(biz_cdcallback)
{
Expand Down
44 changes: 27 additions & 17 deletions waterbox/gpgx/core/m68k/m68kcpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -870,24 +870,25 @@ INLINE uint m68ki_read_imm_32(void)
INLINE uint m68ki_read_8_fc(uint address, uint fc)
{
cpu_memory_map *temp = &m68ki_cpu.memory_map[((address)>>16)&0xff];;
if (biz_readcb)
biz_readcb(address);

if(biz_cdcallback)
CDLog68k(address,eCDLog_Flags_Data68k);

m68ki_set_fc(fc) /* auto-disable (see m68kcpu.h) */

if (temp->read8) return (*temp->read8)(ADDRESS_68K(address));
else return READ_BYTE(temp->base, (address) & 0xffff);
uint value;
if (temp->read8)
value = (*temp->read8)(ADDRESS_68K(address));
else
value = READ_BYTE(temp->base, (address) & 0xffff);
if (biz_readcb)
biz_readcb(address, value);
return value;
}

INLINE uint m68ki_read_16_fc(uint address, uint fc)
{
cpu_memory_map *temp;
if (biz_readcb)
biz_readcb(address);

if(biz_cdcallback)
{
CDLog68k(address,eCDLog_Flags_Data68k);
Expand All @@ -898,16 +899,19 @@ INLINE uint m68ki_read_16_fc(uint address, uint fc)
m68ki_check_address_error(address, MODE_READ, fc) /* auto-disable (see m68kcpu.h) */

temp = &m68ki_cpu.memory_map[((address)>>16)&0xff];
if (temp->read16) return (*temp->read16)(ADDRESS_68K(address));
else return *(uint16 *)(temp->base + ((address) & 0xffff));
uint value;
if (temp->read16)
value = (*temp->read16)(ADDRESS_68K(address));
else
value = *(uint16 *)(temp->base + ((address) & 0xffff));
if (biz_readcb)
biz_readcb(address, value);
return value;
}

INLINE uint m68ki_read_32_fc(uint address, uint fc)
{
cpu_memory_map *temp;
if (biz_readcb)
biz_readcb(address);

if(biz_cdcallback)
{
CDLog68k(address,eCDLog_Flags_Data68k);
Expand All @@ -920,15 +924,21 @@ INLINE uint m68ki_read_32_fc(uint address, uint fc)
m68ki_check_address_error(address, MODE_READ, fc) /* auto-disable (see m68kcpu.h) */

temp = &m68ki_cpu.memory_map[((address)>>16)&0xff];
if (temp->read16) return ((*temp->read16)(ADDRESS_68K(address)) << 16) | ((*temp->read16)(ADDRESS_68K(address + 2)));
else return m68k_read_immediate_32(address);
uint value;
if (temp->read16)
value = ((*temp->read16)(ADDRESS_68K(address)) << 16) | ((*temp->read16)(ADDRESS_68K(address + 2)));
else
value = m68k_read_immediate_32(address);
if (biz_readcb)
biz_readcb(address, value);
return value;
}

INLINE void m68ki_write_8_fc(uint address, uint fc, uint value)
{
cpu_memory_map *temp;
if (biz_writecb)
biz_writecb(address);
biz_writecb(address, value);

m68ki_set_fc(fc) /* auto-disable (see m68kcpu.h) */

Expand All @@ -941,7 +951,7 @@ INLINE void m68ki_write_16_fc(uint address, uint fc, uint value)
{
cpu_memory_map *temp;
if (biz_writecb)
biz_writecb(address);
biz_writecb(address, value);

m68ki_set_fc(fc) /* auto-disable (see m68kcpu.h) */
m68ki_check_address_error(address, MODE_WRITE, fc); /* auto-disable (see m68kcpu.h) */
Expand All @@ -955,7 +965,7 @@ INLINE void m68ki_write_32_fc(uint address, uint fc, uint value)
{
cpu_memory_map *temp;
if (biz_writecb)
biz_writecb(address);
biz_writecb(address, value);

m68ki_set_fc(fc) /* auto-disable (see m68kcpu.h) */
m68ki_check_address_error(address, MODE_WRITE, fc) /* auto-disable (see m68kcpu.h) */
Expand Down