Skip to content

Commit

Permalink
Added methods of getting mapper and bank for bsnes sgb and bsnes 115+…
Browse files Browse the repository at this point in the history
… sgb.
  • Loading branch information
roydmerkel committed Apr 23, 2024
1 parent 130f0f7 commit bf75ab8
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public abstract class BsnesCoreImpl
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr snes_get_sgb_memory_region(int id, out int size);
[BizImport(CallingConvention.Cdecl)]
public abstract ushort snes_get_sgb_bank(int id);
[BizImport(CallingConvention.Cdecl)]
public abstract IntPtr snes_get_sgb_board();
[BizImport(CallingConvention.Cdecl)]
public abstract byte snes_sgb_bus_read(ushort address);
[BizImport(CallingConvention.Cdecl)]
public abstract void snes_sgb_bus_write(ushort address, byte value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
using System;
using System.Collections.Generic;
using BizHawk.Common;
using BizHawk.Emulation.Common;

namespace BizHawk.Emulation.Cores.Nintendo.BSNES
{
public class BSNESMemoryDomainIntPtrMonitor : MemoryDomainIntPtrMonitor
{
private BsnesApi _api;
private int _regionId;

public BSNESMemoryDomainIntPtrMonitor(BsnesApi api, int regionId, string name, Endian endian, IntPtr data, long size, bool writable, int wordSize, IMonitor monitor)
: base(name, endian, data, size, writable, wordSize, monitor)
{
_api = api;
_regionId = regionId;
}

public int GetBank()
{
return _api.core.snes_get_sgb_bank(_regionId);
}
}

public partial class BsnesCore
{
private MemoryDomainList _memoryDomains;
Expand All @@ -27,7 +46,7 @@ private void SetMemoryDomains()
_saveRam = data;
_saveRamSize = size;
}
mm.Add(new MemoryDomainIntPtrMonitor(Enum.GetName(typeof(BsnesApi.SNES_MEMORY), i)!.Replace('_', ' '), MemoryDomain.Endian.Little, data, size, true, wordSize, Api));
mm.Add(new BSNESMemoryDomainIntPtrMonitor(Api, i, Enum.GetName(typeof(BsnesApi.SNES_MEMORY), i)!.Replace('_', ' '), MemoryDomain.Endian.Little, data, size, true, wordSize, Api));
}

mm.Add(new MemoryDomainDelegate(
Expand All @@ -43,7 +62,7 @@ private void SetMemoryDomains()
{
var data = Api.core.snes_get_sgb_memory_region(i, out var size);
if (data == IntPtr.Zero || size == 0) continue;
mm.Add(new MemoryDomainIntPtrMonitor("SGB " + Enum.GetName(typeof(BsnesApi.SGB_MEMORY), i), MemoryDomain.Endian.Little, data, size, true, 1, Api));
mm.Add(new BSNESMemoryDomainIntPtrMonitor(Api, i, "SGB " + Enum.GetName(typeof(BsnesApi.SGB_MEMORY), i), MemoryDomain.Endian.Little, data, size, true, 1, Api));
}

mm.Add(new MemoryDomainDelegate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public BsnesCore(CoreLoadParameters<SnesSettings, SnesSyncSettings> loadParamete
}

using (Api.EnterExit()) this.BoardName = Marshal.PtrToStringAnsi(Api.core.snes_get_board());
this.SdbBoardName = "";
if (_isSGB)
{
using (Api.EnterExit()) this.SdbBoardName = Marshal.PtrToStringAnsi(Api.core.snes_get_sgb_board());
}
_region = Api.core.snes_get_region();
if (_region == BsnesApi.SNES_REGION.NTSC)
{
Expand Down Expand Up @@ -161,6 +166,7 @@ public BsnesCore(CoreLoadParameters<SnesSettings, SnesSyncSettings> loadParamete
private bool _disposed;

public string BoardName { get; }
public string SdbBoardName { get; }

internal BsnesApi Api { get; }

Expand Down
32 changes: 28 additions & 4 deletions src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public unsafe partial class LibsnesApi : IDisposable, IMonitor, IStatable
{
static LibsnesApi()
{
if (sizeof(CommStruct) != 368)
if (sizeof(CommStruct) != 384)
{
throw new InvalidOperationException("sizeof(comm)");
}
Expand Down Expand Up @@ -213,6 +213,13 @@ public struct CPURegs
public ushort v, h;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MemoryBanksComm
{
public ushort rom0, romx, sram;
public byte wram, vram;
}

[StructLayout(LayoutKind.Sequential)]
public struct LayerEnables
{
Expand Down Expand Up @@ -346,14 +353,18 @@ private struct CommStruct
public CPURegs cpuregs;
[FieldOffset(344)]
public LayerEnables layerEnables;

[FieldOffset(356)]
public MemoryBanksComm memorybanks;

[FieldOffset(364)]
//static configuration-type information which can be grabbed off the core at any time without even needing a QUERY command
public SNES_REGION region;
[FieldOffset(360)]
[FieldOffset(368)]
public SNES_MAPPER mapper;
[FieldOffset(372)]
public GameBoy_MAPPER gameboy_mapper;

[FieldOffset(364)] private uint BLANK0;
[FieldOffset(376)] private uint BLANK0;


//utilities
Expand Down Expand Up @@ -392,6 +403,19 @@ public SNES_MAPPER Mapper
}
}

public GameBoy_MAPPER GameboyMapper
{
get
{
using (_exe.EnterExit())
{
LibsnesApi.GameBoy_MAPPER mapper;
QUERY_get_gb_mapper(out mapper);
return mapper;
}
}
}

public void SetLayerEnables(ref LayerEnables enables)
{
using (_exe.EnterExit())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public enum eMessage : int
eMessage_QUERY_peek_logical_register,
eMessage_QUERY_peek_cpu_regs,
eMessage_QUERY_set_cdl,
eMessage_QUERY_peek_gb_banks,
eMessage_QUERY_get_gb_mapper,
eMessage_QUERY_LAST,

eMessage_CMD_FIRST,
Expand Down Expand Up @@ -213,6 +215,19 @@ public enum SNES_MAPPER : byte
STROM = 10
}

public enum GameBoy_MAPPER : uint
{
MBC0 = 0,
MBC1 = 1,
MBC2 = 2,
MBC3 = 3,
MBC5 = 4,
MMM01 = 5,
HuC1 = 6,
HuC3 = 7,
Unknown = 8,
}

public enum SNES_REGION : uint
{
NTSC = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ public void QUERY_peek_cpu_regs(out CPURegs ret)
}
}

public void QUERY_peek_gb_banks(out MemoryBanksComm ret)
{
using (_exe.EnterExit())
{
_core.Message(eMessage.eMessage_QUERY_peek_gb_banks);
ret = _comm->memorybanks;
}
}

public void QUERY_get_gb_mapper(out GameBoy_MAPPER ret)
{
using (_exe.EnterExit())
{
_core.Message(eMessage.eMessage_QUERY_get_gb_mapper);
ret = _comm->gameboy_mapper;
}
}

public void QUERY_set_cdl(ICodeDataLog cdl)
{
if (_exe == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public IDictionary<string, RegisterValue> GetCpuFlagsAndRegisters()
bool fz = (regs.p & 0x02) != 0;
bool fc = (regs.p & 0x01) != 0;

Api.QUERY_peek_gb_banks(out var banks);

return new Dictionary<string, RegisterValue>
{
["PC"] = regs.pc,
Expand All @@ -39,7 +41,12 @@ public IDictionary<string, RegisterValue> GetCpuFlagsAndRegisters()
["Flag Z"] = fz,
["Flag C"] = fc,
["V"] = regs.v,
["H"] = regs.h
["H"] = regs.h,
["ROM0 BANK"] = banks.rom0,
["ROMX BANK"] = banks.romx,
["VRAM BANK"] = banks.vram,
["SRAM BANK"] = banks.sram,
["WRAM BANK"] = banks.wram
};
}

Expand Down
19 changes: 19 additions & 0 deletions waterbox/bsnescore/bsnes/target-bsnescore/bsnescore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,25 @@ EXPORT void* snes_get_sgb_memory_region(int id, int* size)
return ret;
}

const char* sgb_board;
EXPORT const char* snes_get_sgb_board(void)
{
if (!sgb_board) sgb_board = strdup(program->gameBoy.document["game/board"].text().data());

return sgb_board;
}

EXPORT uint16_t snes_get_sgb_bank(int id)
{
if(!emulator->loaded()) return 0;
if(!GB_is_inited(&icd.sameboy)) return 0;

size_t s = 0;
uint16_t bank = 0;
void* ret = GB_get_direct_access(&icd.sameboy, (GB_direct_access_t)id, &s, &bank);
return bank;
}

EXPORT uint8_t snes_sgb_bus_read(uint16_t addr)
{
return GB_safe_read_memory(&icd.sameboy, addr);
Expand Down
47 changes: 47 additions & 0 deletions waterbox/libsnes/bsnes/gameboy/cartridge/cartridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,53 @@ void Cartridge::power() {
bus.mmio[0xff50] = this;
}

uint16_t Cartridge::GetRom0Bank()
{
switch(info.mapper) { default:
case Mapper::MBC0: return 0;
case Mapper::MBC1: return 0;
case Mapper::MBC2: return 0;
case Mapper::MBC3: return 0;
case Mapper::MBC5: return 0;
case Mapper::MMM01: return 0;
case Mapper::HuC1: return 0;
case Mapper::HuC3: return 0;
}
}

uint16_t Cartridge::GetRomXBank()
{
switch(info.mapper) { default:
case Mapper::MBC0: return 0;
case Mapper::MBC1: return mbc1.rom_select;
case Mapper::MBC2: return mbc2.rom_select;
case Mapper::MBC3: return mbc3.rom_select;
case Mapper::MBC5: return mbc5.rom_select;
case Mapper::MMM01: return mmm01.rom_select;
case Mapper::HuC1: return huc1.rom_select;
case Mapper::HuC3: return huc3.rom_select;
}
}

uint16_t Cartridge::GetCartRamBank()
{
switch(info.mapper) { default:
case Mapper::MBC0: return 0;
case Mapper::MBC1: return mbc1.ram_select;
case Mapper::MBC2: return 0;
case Mapper::MBC3: return mbc3.ram_select;
case Mapper::MBC5: return mbc5.ram_select;
case Mapper::MMM01: return mmm01.ram_select;
case Mapper::HuC1: return huc1.ram_select;
case Mapper::HuC3: return huc3.ram_select;
}
}

Cartridge::Mapper Cartridge::GetCartMapper()
{
return info.mapper;
}

Cartridge::Cartridge() {
loaded = false;
romdata = 0;
Expand Down
6 changes: 6 additions & 0 deletions waterbox/libsnes/bsnes/gameboy/cartridge/cartridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ struct Cartridge : MMIO, property<Cartridge> {

void power();

uint16_t GetRom0Bank();
uint16_t GetRomXBank();
uint16_t GetCartRamBank();

Cartridge::Mapper GetCartMapper();

Cartridge();
~Cartridge();
};
Expand Down
Loading

0 comments on commit bf75ab8

Please sign in to comment.