cog/Frameworks/GME/gme/Gb_Cpu.h

83 lines
2.0 KiB
C
Raw Normal View History

2007-10-11 23:11:58 +00:00
// Nintendo Game Boy CPU emulator
2013-09-28 03:24:23 +00:00
// Game_Music_Emu $vers
2007-10-11 23:11:58 +00:00
#ifndef GB_CPU_H
#define GB_CPU_H
#include "blargg_common.h"
class Gb_Cpu {
public:
2013-09-28 03:24:23 +00:00
typedef int addr_t;
typedef BOOST::uint8_t byte;
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
enum { mem_size = 0x10000 };
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// Clears registers and map all pages to unmapped
void reset( void* unmapped = NULL );
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// Maps code memory (memory accessed via the program counter). Start and size
// must be multiple of page_size.
enum { page_bits = 13 };
enum { page_size = 1 << page_bits };
void map_code( addr_t start, int size, void* code );
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// Accesses emulated memory as CPU does
byte* get_code( addr_t );
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// Game Boy Z-80 registers. NOT kept updated during emulation.
2007-10-11 23:11:58 +00:00
struct core_regs_t {
2013-09-28 03:24:23 +00:00
BOOST::uint16_t bc, de, hl, fa;
2007-10-11 23:11:58 +00:00
};
struct registers_t : core_regs_t {
2013-09-28 03:24:23 +00:00
int pc; // more than 16 bits to allow overflow detection
2007-10-11 23:11:58 +00:00
BOOST::uint16_t sp;
};
registers_t r;
2013-09-28 03:24:23 +00:00
// Base address for RST vectors, to simplify GBS player (normally 0)
addr_t rst_base;
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// Current time.
int time() const { return cpu_state->time; }
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// Changes time. Must not be called during emulation.
// Should be negative, because emulation stops once it becomes >= 0.
void set_time( int t ) { cpu_state->time = t; }
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// Emulator reads this many bytes past end of a page
2007-10-11 23:11:58 +00:00
enum { cpu_padding = 8 };
2013-09-28 03:24:23 +00:00
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// Implementation
2007-10-11 23:11:58 +00:00
public:
2013-09-28 03:24:23 +00:00
Gb_Cpu() : rst_base( 0 ) { cpu_state = &cpu_state_; }
enum { page_count = mem_size >> page_bits };
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
struct cpu_state_t {
byte* code_map [page_count + 1];
int time;
2007-10-11 23:11:58 +00:00
};
2013-09-28 03:24:23 +00:00
cpu_state_t* cpu_state; // points to state_ or a local copy within run()
cpu_state_t cpu_state_;
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
private:
void set_code_page( int, void* );
2007-10-11 23:11:58 +00:00
};
2013-09-28 03:24:23 +00:00
#define GB_CPU_PAGE( addr ) ((unsigned) (addr) >> Gb_Cpu::page_bits)
#if BLARGG_NONPORTABLE
#define GB_CPU_OFFSET( addr ) (addr)
#else
#define GB_CPU_OFFSET( addr ) ((addr) & (Gb_Cpu::page_size - 1))
#endif
inline BOOST::uint8_t* Gb_Cpu::get_code( addr_t addr )
2007-10-11 23:11:58 +00:00
{
2013-09-28 03:24:23 +00:00
return cpu_state_.code_map [GB_CPU_PAGE( addr )] + GB_CPU_OFFSET( addr );
2007-10-11 23:11:58 +00:00
}
#endif