1 /***************************************************************************
2  *   Copyright (C) 2007 by Sindre Aamås                                    *
3  *   aamas@stud.ntnu.no                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License version 2 as     *
7  *   published by the Free Software Foundation.                            *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License version 2 for more details.                *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   version 2 along with this program; if not, write to the               *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 #ifndef GAMBATTE_H
20 #define GAMBATTE_H
21 
22 #include "inputgetter.h"
23 #ifdef HAVE_NETWORK
24 #include "serial_io.h"
25 #endif
26 #include "gbint.h"
27 #include <string>
28 #include <cstddef>
29 
30 namespace gambatte {
31 #if defined(VIDEO_RGB565) || defined(VIDEO_ABGR1555)
32 typedef uint16_t video_pixel_t;
33 #else
34 typedef uint_least32_t video_pixel_t;
35 #endif
36 enum { BG_PALETTE = 0, SP1_PALETTE = 1, SP2_PALETTE = 2 };
37 
38 class GB {
39 public:
40 	GB();
41 	~GB();
42 
43 	enum LoadFlag {
44       FORCE_DMG        = 1, /**< Treat the ROM as not having CGB support regardless of what its header advertises. */
45       GBA_CGB          = 2, /**< Use GBA intial CPU register values when in CGB mode. */
46       MULTICART_COMPAT = 4,  /**< Use heuristics to detect and support some multicart MBCs disguised as MBC1. */
47       FORCE_CGB        = 8
48 	};
49 
50    int load(const void *romdata, unsigned size, unsigned flags = 0);
51 
52 	/** Emulates until at least 'samples' stereo sound samples are produced in the supplied buffer,
53 	  * or until a video frame has been drawn.
54 	  *
55 	  * There are 35112 stereo sound samples in a video frame.
56 	  * May run for uptil 2064 stereo samples too long.
57 	  * A stereo sample consists of two native endian 2s complement 16-bit PCM samples,
58 	  * with the left sample preceding the right one. Usually casting soundBuf to/from
59 	  * short* is OK and recommended. The reason for not using a short* in the interface
60 	  * is to avoid implementation-defined behaviour without compromising performance.
61 	  *
62 	  * Returns early when a new video frame has finished drawing in the video buffer,
63 	  * such that the caller may update the video output before the frame is overwritten.
64 	  * The return value indicates whether a new video frame has been drawn, and the
65 	  * exact time (in number of samples) at which it was drawn.
66 	  *
67 	  * @param videoBuf 160x144 RGB32 (native endian) video frame buffer or 0
68 	  * @param pitch distance in number of pixels (not bytes) from the start of one line to the next in videoBuf.
69 	  * @param soundBuf buffer with space >= samples + 2064
70 	  * @param samples in: number of stereo samples to produce, out: actual number of samples produced
71 	  * @return sample number at which the video frame was produced. -1 means no frame was produced.
72 	  */
73 	long runFor(gambatte::video_pixel_t *videoBuf, int pitch,
74 			gambatte::uint_least32_t *soundBuf, unsigned &samples);
75 
76 	/** Reset to initial state.
77 	  * Equivalent to reloading a ROM image, or turning a Game Boy Color off and on again.
78 	  */
79 	void reset();
80 
81 	/** @param palNum 0 <= palNum < 3. One of BG_PALETTE, SP1_PALETTE and SP2_PALETTE.
82 	  * @param colorNum 0 <= colorNum < 4
83 	  */
84 	void setDmgPaletteColor(unsigned palNum, unsigned colorNum, unsigned rgb32);
85 
86 	/** Sets the callback used for getting input state. */
87 	void setInputGetter(InputGetter *getInput);
88 
89    /** Sets the callback used for getting the bootloader data. */
90    void setBootloaderGetter(bool (*getter)(void *userdata, bool isgbc, uint8_t *data, uint32_t buf_size));
91 
92 #ifdef HAVE_NETWORK
93 	/** Sets the callback used for transferring serial data. */
94 	void setSerialIO(SerialIO *serial_io);
95 #endif
96 
97 	/** Sets the directory used for storing save data. The default is the same directory as the ROM Image file. */
98 	void setSaveDir(const std::string &sdir);
99 
100    void *savedata_ptr();
101    unsigned savedata_size();
102    void *rtcdata_ptr();
103    unsigned rtcdata_size();
104 
105 	/** Returns true if the currently loaded ROM image is treated as having CGB support. */
106 	bool isCgb() const;
107 
108 	/** Returns true if a ROM image is loaded. */
109 	bool isLoaded() const;
110 
111    void saveState(void *data);
112    void loadState(const void *data);
113    size_t stateSize() const;
114 
115    void setColorCorrection(bool enable);
116    void setColorCorrectionMode(unsigned colorCorrectionMode);
117    void setColorCorrectionBrightness(float colorCorrectionBrightness);
118    void setDarkFilterLevel(unsigned darkFilterLevel);
119    video_pixel_t gbcToRgb32(const unsigned bgr15);
120 
121    /** Set Game Genie codes to apply to currently loaded ROM image. Cleared on ROM load.
122     * @param codes Game Genie codes in format HHH-HHH-HHH;HHH-HHH-HHH;... where H is [0-9]|[A-F]
123     */
124    void setGameGenie(const std::string &codes);
125 
126    /** Set Game Shark codes to apply to currently loaded ROM image. Cleared on ROM load.
127     * @param codes Game Shark codes in format 01HHHHHH;01HHHHHH;... where H is [0-9]|[A-F]
128     */
129    void setGameShark(const std::string &codes);
130 
131    void clearCheats();
132 
133 #ifdef __LIBRETRO__
134    void *vram_ptr() const;
135    void *rambank0_ptr() const;
136    void *rambank1_ptr() const;
137    void *rambank2_ptr() const;
138    void *bankedram_ptr() const;
139    void *rombank0_ptr() const;
140    void *rombank1_ptr() const;
141    void *zeropage_ptr() const;
142    void *oamram_ptr() const;
143 #endif
144 
145 private:
146 	struct Priv;
147 	Priv *const p_;
148 
149 	void loadState(const std::string &filepath, bool osdMessage);
150 	GB(const GB &);
151 	GB & operator=(const GB &);
152 };
153 }
154 
155 #endif
156