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

Use Netpacket for Serial communications #266

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ SOURCES_CXX := \

ifeq ($(HAVE_NETWORK),1)
SOURCES_CXX += \
$(CORE_DIR)/../libretro/net_serial.cpp
$(CORE_DIR)/../libretro/netplay_serial.cpp
endif

ifneq ($(STATIC_LINKING), 1)
Expand Down
2 changes: 1 addition & 1 deletion Makefile.libretro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DEBUG = 0
HAVE_NETWORK = 0
HAVE_NETWORK = 1
VIDEO_RGB565 = 1

SPACE :=
Expand Down
261 changes: 143 additions & 118 deletions libgambatte/include/gambatte.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,135 +24,160 @@
#include "serial_io.h"
#endif
#include "gbint.h"
#include <string>
#include <cstddef>
#include <string>

namespace gambatte {
namespace gambatte
{
#if defined(VIDEO_RGB565) || defined(VIDEO_ABGR1555)
typedef uint16_t video_pixel_t;
typedef uint16_t video_pixel_t;
#else
typedef uint_least32_t video_pixel_t;
typedef uint_least32_t video_pixel_t;
#endif
enum { BG_PALETTE = 0, SP1_PALETTE = 1, SP2_PALETTE = 2 };

class GB {
public:
GB();
~GB();

enum LoadFlag {
FORCE_DMG = 1, /**< Treat the ROM as not having CGB support regardless of what its header advertises. */
GBA_CGB = 2, /**< Use GBA intial CPU register values when in CGB mode. */
MULTICART_COMPAT = 4, /**< Use heuristics to detect and support some multicart MBCs disguised as MBC1. */
FORCE_CGB = 8
};

int load(const void *romdata, unsigned size, unsigned flags = 0);

/** Emulates until at least 'samples' stereo sound samples are produced in the supplied buffer,
* or until a video frame has been drawn.
*
* There are 35112 stereo sound samples in a video frame.
* May run for uptil 2064 stereo samples too long.
* EDIT: Due to internal emulator bugs, may in fact run for
* an arbitrary number of samples...
* A stereo sample consists of two native endian 2s complement 16-bit PCM samples,
* with the left sample preceding the right one. Usually casting soundBuf to/from
* short* is OK and recommended. The reason for not using a short* in the interface
* is to avoid implementation-defined behaviour without compromising performance.
*
* Returns early when a new video frame has finished drawing in the video buffer,
* such that the caller may update the video output before the frame is overwritten.
* The return value indicates whether a new video frame has been drawn, and the
* exact time (in number of samples) at which it was drawn.
*
* @param videoBuf 160x144 RGB32 (native endian) video frame buffer or 0
* @param pitch distance in number of pixels (not bytes) from the start of one line to the next in videoBuf.
* @param soundBuf buffer with space >= samples + 2064
* @param soundBufSize actual size of soundBuf buffer
* @param samples in: number of stereo samples to produce, out: actual number of samples produced
* @return sample number at which the video frame was produced. -1 means no frame was produced.
*/
long runFor(gambatte::video_pixel_t *videoBuf, int pitch,
gambatte::uint_least32_t *soundBuf, std::size_t soundBufSize, unsigned &samples);

/** Reset to initial state.
* Equivalent to reloading a ROM image, or turning a Game Boy Color off and on again.
*/
void reset();

/** @param palNum 0 <= palNum < 3. One of BG_PALETTE, SP1_PALETTE and SP2_PALETTE.
* @param colorNum 0 <= colorNum < 4
*/
void setDmgPaletteColor(unsigned palNum, unsigned colorNum, unsigned rgb32);

/** Sets the callback used for getting input state. */
void setInputGetter(InputGetter *getInput);

/** Sets the callback used for getting the bootloader data. */
void setBootloaderGetter(bool (*getter)(void *userdata, bool isgbc, uint8_t *data, uint32_t buf_size));
enum
{
BG_PALETTE = 0,
SP1_PALETTE = 1,
SP2_PALETTE = 2
};

class GB
{
public:
GB();
~GB();

enum LoadFlag
{
FORCE_DMG = 1, /**< Treat the ROM as not having CGB support regardless of
what its header advertises. */
GBA_CGB = 2, /**< Use GBA intial CPU register values when in CGB mode. */
MULTICART_COMPAT = 4, /**< Use heuristics to detect and support some
multicart MBCs disguised as MBC1. */
FORCE_CGB = 8
};

int load(const void *romdata, unsigned size, unsigned flags = 0);

/** Emulates until at least 'samples' stereo sound samples are produced in
* the supplied buffer, or until a video frame has been drawn.
*
* There are 35112 stereo sound samples in a video frame.
* May run for uptil 2064 stereo samples too long.
* EDIT: Due to internal emulator bugs, may in fact run for
* an arbitrary number of samples...
* A stereo sample consists of two native endian 2s complement 16-bit PCM
* samples, with the left sample preceding the right one. Usually casting
* soundBuf to/from short* is OK and recommended. The reason for not using a
* short* in the interface is to avoid implementation-defined behaviour
* without compromising performance.
*
* Returns early when a new video frame has finished drawing in the video
* buffer, such that the caller may update the video output before the frame
* is overwritten. The return value indicates whether a new video frame has
* been drawn, and the exact time (in number of samples) at which it was
* drawn.
*
* @param videoBuf 160x144 RGB32 (native endian) video frame buffer or 0
* @param pitch distance in number of pixels (not bytes) from the start of
* one line to the next in videoBuf.
* @param soundBuf buffer with space >= samples + 2064
* @param soundBufSize actual size of soundBuf buffer
* @param samples in: number of stereo samples to produce, out: actual number
* of samples produced
* @return sample number at which the video frame was produced. -1 means no
* frame was produced.
*/
long runFor(gambatte::video_pixel_t *videoBuf, int pitch,
gambatte::uint_least32_t *soundBuf, std::size_t soundBufSize,
unsigned &samples);

/** Reset to initial state.
* Equivalent to reloading a ROM image, or turning a Game Boy Color off and
* on again.
*/
void reset();

/** @param palNum 0 <= palNum < 3. One of BG_PALETTE, SP1_PALETTE and
* SP2_PALETTE.
* @param colorNum 0 <= colorNum < 4
*/
void setDmgPaletteColor(unsigned palNum, unsigned colorNum, unsigned rgb32);

/** Sets the callback used for getting input state. */
void setInputGetter(InputGetter *getInput);

/** Sets the callback used for getting the bootloader data. */
void setBootloaderGetter(bool (*getter)(void *userdata, bool isgbc,
uint8_t *data, uint32_t buf_size));

#ifdef HAVE_NETWORK
/** Sets the callback used for transferring serial data. */
void setSerialIO(SerialIO *serial_io);
/** Sets the callback used for transferring serial data. */
void setSerialIO(SerialIO *serial_io);
#endif

/** Sets the directory used for storing save data. The default is the same directory as the ROM Image file. */
void setSaveDir(const std::string &sdir);

void *savedata_ptr();
unsigned savedata_size();
void *rtcdata_ptr();
unsigned rtcdata_size();

/** Returns true if the currently loaded ROM image is treated as having CGB support. */
bool isCgb() const;

/** Returns true if a ROM image is loaded. */
bool isLoaded() const;

void saveState(void *data);
void loadState(const void *data);
size_t stateSize() const;

void setColorCorrection(bool enable);
void setColorCorrectionMode(unsigned colorCorrectionMode);
void setColorCorrectionBrightness(float colorCorrectionBrightness);
void setDarkFilterLevel(unsigned darkFilterLevel);
video_pixel_t gbcToRgb32(const unsigned bgr15);

/** Set Game Genie codes to apply to currently loaded ROM image. Cleared on ROM load.
* @param codes Game Genie codes in format HHH-HHH-HHH;HHH-HHH-HHH;... where H is [0-9]|[A-F]
*/
void setGameGenie(const std::string &codes);

/** Set Game Shark codes to apply to currently loaded ROM image. Cleared on ROM load.
* @param codes Game Shark codes in format 01HHHHHH;01HHHHHH;... where H is [0-9]|[A-F]
*/
void setGameShark(const std::string &codes);

void clearCheats();


/** Sets the directory used for storing save data. The default is the same
* directory as the ROM Image file. */
void setSaveDir(const std::string &sdir);

void *savedata_ptr();
unsigned savedata_size();
void *rtcdata_ptr();
unsigned rtcdata_size();

/** Returns true if the currently loaded ROM image is treated as having CGB
* support. */
bool isCgb() const;

/** Returns true if a ROM image is loaded. */
bool isLoaded() const;

void saveState(void *data);
void loadState(const void *data);
size_t stateSize() const;

void setColorCorrection(bool enable);
void setColorCorrectionMode(unsigned colorCorrectionMode);
void setColorCorrectionBrightness(float colorCorrectionBrightness);
void setDarkFilterLevel(unsigned darkFilterLevel);
video_pixel_t gbcToRgb32(const unsigned bgr15);

/** Set Game Genie codes to apply to currently loaded ROM image. Cleared on
* ROM load.
* @param codes Game Genie codes in format HHH-HHH-HHH;HHH-HHH-HHH;... where
* H is [0-9]|[A-F]
*/
void setGameGenie(const std::string &codes);

/** Set Game Shark codes to apply to currently loaded ROM image. Cleared on
* ROM load.
* @param codes Game Shark codes in format 01HHHHHH;01HHHHHH;... where H is
* [0-9]|[A-F]
*/
void setGameShark(const std::string &codes);

void clearCheats();

#ifdef __LIBRETRO__
void *vram_ptr() const;
void *rambank0_ptr() const;
void *rambank1_ptr() const;
void *rambank2_ptr() const;
void *bankedram_ptr() const;
void *rombank0_ptr() const;
void *rombank1_ptr() const;
void *zeropage_ptr() const;
void *oamram_ptr() const;
void *vram_ptr() const;
void *rambank0_ptr() const;
void *rambank1_ptr() const;
void *rambank2_ptr() const;
void *bankedram_ptr() const;
void *rombank0_ptr() const;
void *rombank1_ptr() const;
void *zeropage_ptr() const;
void *oamram_ptr() const;
#endif

private:
struct Priv;
Priv *const p_;
private:
struct Priv;
Priv *const p_;

void loadState(const std::string &filepath, bool osdMessage);
GB(const GB &);
GB & operator=(const GB &);
};
}
void loadState(const std::string &filepath, bool osdMessage);
GB(const GB &);
GB &operator=(const GB &);
};
} // namespace gambatte

#endif
Loading