Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

1.5 KB
Raw Blame
#ifndef __MEM_H__
#define __MEM_H__


#include "defs.h"



#define MBC_NONE 0
#define MBC_MBC1 1
#define MBC_MBC2 2
#define MBC_MBC3 3
#define MBC_MBC5 5
#define MBC_RUMBLE 15
#define MBC_HUC1 0xC1
#define MBC_HUC3 0xC3

struct mbc
{
	int type;
	int model;
	int rombank;
	int rambank;
	int romsize;
	int ramsize;
	int enableram;
	int batt;
	byte *rmap[0x10], *wmap[0x10];
};

/* The cartridge is read where it is (in the calculator's flash): bank[n] points at its n-th
   16 KB bank, read only; the banks a ROM file lacks repeat the ones it has. */
#define ROM_MAX_BANKS 512
struct rom
{
	const byte *bank[ROM_MAX_BANKS];
	char name[20];
};

/* The work RAM (2 banks of 4 KB on the DMG, 8 on the CGB) and the cartridge's RAM come from
   Retro-Go's memory block. The cartridge's RAM is written through mem_write only, so that its
   battery save knows when it changed. */
struct ram
{
	byte hi[256];
	byte (*ibank)[4096];
	byte (*sbank)[8192];
	int sram_dirty;
};


extern struct mbc mbc;
extern struct rom rom;
extern struct ram ram;



void mem_updatemap();
void ioreg_write(byte r, byte b);
void mbc_write(int a, byte b);
void mem_write(int a, byte b);
byte mem_read(int a);
void mbc_reset();


#define READB(a) ( mbc.rmap[(a)>>12] \
? mbc.rmap[(a)>>12][(a)] \
: mem_read((a)) )
#define READW(a) ( READB((a)) | ((word)READB((a)+1)<<8) )

#define WRITEB(a, b) ( mbc.wmap[(a)>>12] \
? ( mbc.wmap[(a)>>12][(a)] = (b) ) \
: ( mem_write((a), (b)), (b) ) )
#define WRITEW(a, w) ( WRITEB((a), (w)&0xFF), WRITEB((a)+1, (w)>>8) )




#endif