1
// Nintendo Game Boy CPU emulator
2
// Treats every instruction as taking 4 cycles
4
// Game_Music_Emu 0.5.2
8
#include "blargg_common.h"
9
#include "blargg_endian.h"
11
typedef unsigned gb_addr_t; // 16-bit CPU address
14
enum { clocks_per_instr = 4 };
16
typedef BOOST::uint8_t uint8_t;
18
// Clear registers and map all pages to unmapped
19
void reset( void* unmapped = 0 );
21
// Map code memory (memory accessed via the program counter). Start and size
22
// must be multiple of page_size.
23
enum { page_size = 0x2000 };
24
void map_code( gb_addr_t start, unsigned size, void* code );
26
uint8_t* get_code( gb_addr_t );
28
// Push a byte on the stack
29
void push_byte( int );
31
// Game Boy Z80 registers. *Not* kept updated during a call to run().
34
uint8_t b, c, d, e, h, l, flags, a;
36
uint8_t c, b, e, d, l, h, a, flags;
40
struct registers_t : core_regs_t {
41
long pc; // more than 16 bits to allow overflow detection
46
// Interrupt enable flag set by EI and cleared by DI
47
//bool interrupts_enabled; // unused
49
// Base address for RST vectors (normally 0)
52
// If CPU executes opcode 0xFF at this address, it treats as illegal instruction
53
enum { idle_addr = 0xF00D };
55
// Run CPU for at least 'count' cycles and return false, or return true if
56
// illegal instruction is encountered.
57
bool run( blargg_long count );
59
// Number of clock cycles remaining for most recent run() call
60
blargg_long remain() const { return state->remain * clocks_per_instr; }
62
// Can read this many bytes past end of a page
63
enum { cpu_padding = 8 };
66
Gb_Cpu() : rst_base( 0 ) { state = &state_; }
67
enum { page_shift = 13 };
68
enum { page_count = 0x10000 >> page_shift };
71
Gb_Cpu( const Gb_Cpu& );
72
Gb_Cpu& operator = ( const Gb_Cpu& );
75
uint8_t* code_map [page_count + 1];
78
state_t* state; // points to state_ or a local copy within run()
81
void set_code_page( int, uint8_t* );
84
inline BOOST::uint8_t* Gb_Cpu::get_code( gb_addr_t addr )
86
return state->code_map [addr >> page_shift] + addr
87
#if !BLARGG_NONPORTABLE
88
% (unsigned) page_size