/***************************************************************************1* Copyright (C) 2007 by Sindre Aamås *2* [email protected] *3* *4* This program is free software; you can redistribute it and/or modify *5* it under the terms of the GNU General Public License version 2 as *6* published by the Free Software Foundation. *7* *8* This program is distributed in the hope that it will be useful, *9* but WITHOUT ANY WARRANTY; without even the implied warranty of *10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *11* GNU General Public License version 2 for more details. *12* *13* You should have received a copy of the GNU General Public License *14* version 2 along with this program; if not, write to the *15* Free Software Foundation, Inc., *16* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *17***************************************************************************/18#ifndef GAMBATTE_H19#define GAMBATTE_H2021#include "gbint.h"22#include <string>23#include <sstream>24#include <cstdint>25#include "newstate.h"2627namespace gambatte {28enum { BG_PALETTE = 0, SP1_PALETTE = 1, SP2_PALETTE = 2 };2930typedef void (*CDCallback)(int32_t addr, int32_t addrtype, int32_t flags);3132enum eCDLog_AddrType33{34eCDLog_AddrType_ROM, eCDLog_AddrType_HRAM, eCDLog_AddrType_WRAM, eCDLog_AddrType_CartRAM,35eCDLog_AddrType_None36};3738enum eCDLog_Flags39{40eCDLog_Flags_ExecFirst = 1,41eCDLog_Flags_ExecOperand = 2,42eCDLog_Flags_Data = 4,43};4445class GB {46public:47GB();48~GB();4950enum LoadFlag {51FORCE_DMG = 1, /**< Treat the ROM as not having CGB support regardless of what its header advertises. */52GBA_CGB = 2, /**< Use GBA intial CPU register values when in CGB mode. */53MULTICART_COMPAT = 4 /**< Use heuristics to detect and support some multicart MBCs disguised as MBC1. */54};5556/** Load ROM image.57*58* @param romfile Path to rom image file. Typically a .gbc, .gb, or .zip-file (if zip-support is compiled in).59* @param flags ORed combination of LoadFlags.60* @return 0 on success, negative value on failure.61*/62int load(const char *romfiledata, unsigned romfilelength, std::uint32_t now, unsigned flags = 0);6364/** Emulates until at least 'samples' stereo sound samples are produced in the supplied buffer,65* or until a video frame has been drawn.66*67* There are 35112 stereo sound samples in a video frame.68* May run for up to 2064 stereo samples too long.69* A stereo sample consists of two native endian 2s complement 16-bit PCM samples,70* with the left sample preceding the right one. Usually casting soundBuf to/from71* short* is OK and recommended. The reason for not using a short* in the interface72* is to avoid implementation-defined behaviour without compromising performance.73*74* Returns early when a new video frame has finished drawing in the video buffer,75* such that the caller may update the video output before the frame is overwritten.76* The return value indicates whether a new video frame has been drawn, and the77* exact time (in number of samples) at which it was drawn.78*79* @param soundBuf buffer with space >= samples + 206480* @param samples in: number of stereo samples to produce, out: actual number of samples produced81* @return sample number at which the video frame was produced. -1 means no frame was produced.82*/83long runFor(gambatte::uint_least32_t *soundBuf, unsigned &samples);8485void blitTo(gambatte::uint_least32_t *videoBuf, int pitch);8687void setLayers(unsigned mask);8889/** Reset to initial state.90* Equivalent to reloading a ROM image, or turning a Game Boy Color off and on again.91*/92void reset(std::uint32_t now);9394/** @param palNum 0 <= palNum < 3. One of BG_PALETTE, SP1_PALETTE and SP2_PALETTE.95* @param colorNum 0 <= colorNum < 496*/97void setDmgPaletteColor(unsigned palNum, unsigned colorNum, unsigned rgb32);9899void setCgbPalette(unsigned *lut);100101/** Sets the callback used for getting input state. */102void setInputGetter(unsigned (*getInput)());103104void setReadCallback(void (*callback)(unsigned));105void setWriteCallback(void (*callback)(unsigned));106void setExecCallback(void (*callback)(unsigned));107void setCDCallback(CDCallback);108void setTraceCallback(void (*callback)(void *));109void setScanlineCallback(void (*callback)(), int sl);110void setRTCCallback(std::uint32_t (*callback)());111112/** Returns true if the currently loaded ROM image is treated as having CGB support. */113bool isCgb() const;114115/** Returns true if a ROM image is loaded. */116bool isLoaded() const;117118/** Writes persistent cartridge data to disk. NOT Done implicitly on ROM close. */119void loadSavedata(const char *data);120int saveSavedataLength();121void saveSavedata(char *dest);122123// 0 = vram, 1 = rom, 2 = wram, 3 = cartram, 4 = oam, 5 = hram124bool getMemoryArea(int which, unsigned char **data, int *length);125126/** ROM header title of currently loaded ROM image. */127const std::string romTitle() const;128129unsigned char ExternalRead(unsigned short addr);130void ExternalWrite(unsigned short addr, unsigned char val);131132int LinkStatus(int which);133134void GetRegs(int *dest);135136template<bool isReader>void SyncState(NewState *ns);137138private:139struct Priv;140Priv *const p_;141142GB(const GB &);143GB & operator=(const GB &);144};145}146147#endif148149150