LMMS
Loading...
Searching...
No Matches
Gb_Cpu.h
Go to the documentation of this file.
1// Nintendo Game Boy CPU emulator
2// Treats every instruction as taking 4 cycles
3
4// Game_Music_Emu https://bitbucket.org/mpyne/game-music-emu/
5#ifndef GB_CPU_H
6#define GB_CPU_H
7
8#include "blargg_common.h"
9#include "blargg_endian.h"
10
11typedef unsigned gb_addr_t; // 16-bit CPU address
12
13class Gb_Cpu {
14 enum { clocks_per_instr = 4 };
15public:
16 // Clear registers and map all pages to unmapped
17 void reset( void* unmapped = 0 );
18
19 // Map code memory (memory accessed via the program counter). Start and size
20 // must be multiple of page_size.
21 enum { page_size = 0x2000 };
22 void map_code( gb_addr_t start, unsigned size, void* code );
23
25
26 // Push a byte on the stack
27 void push_byte( int );
28
29 // Game Boy Z80 registers. *Not* kept updated during a call to run().
30 struct core_regs_t {
31 #if BLARGG_BIG_ENDIAN
32 uint8_t b, c, d, e, h, l, flags, a;
33 #else
34 uint8_t c, b, e, d, l, h, a, flags;
35 #endif
36 };
37
39 long pc; // more than 16 bits to allow overflow detection
41 };
43
44 // Interrupt enable flag set by EI and cleared by DI
45 //bool interrupts_enabled; // unused
46
47 // Base address for RST vectors (normally 0)
49
50 // If CPU executes opcode 0xFF at this address, it treats as illegal instruction
51 enum { idle_addr = 0xF00D };
52
53 // Run CPU for at least 'count' cycles and return false, or return true if
54 // illegal instruction is encountered.
55 bool run( blargg_long count );
56
57 // Number of clock cycles remaining for most recent run() call
58 blargg_long remain() const { return state->remain * clocks_per_instr; }
59
60 // Can read this many bytes past end of a page
61 enum { cpu_padding = 8 };
62
63public:
64 Gb_Cpu() : rst_base( 0 ) { state = &state_; }
65 enum { page_shift = 13 };
66 enum { page_count = 0x10000 >> page_shift };
67private:
68 // noncopyable
69 Gb_Cpu( const Gb_Cpu& );
71
72 struct state_t {
74 blargg_long remain;
75 };
76 state_t* state; // points to state_ or a local copy within run()
78
79 void set_code_page( int, uint8_t* );
80};
81
83{
84 return state->code_map [addr >> page_shift] + addr
85 #if !BLARGG_NONPORTABLE
86 % (unsigned) page_size
87 #endif
88 ;
89}
90
91#endif
unsigned gb_addr_t
Definition Gb_Cpu.h:11
int const unmapped
Definition Hes_Emu.cpp:24
@ page_size
Definition Gb_Cpu.h:21
state_t state_
Definition Gb_Cpu.h:77
blargg_long remain() const
Definition Gb_Cpu.h:58
void push_byte(int)
Gb_Cpu & operator=(const Gb_Cpu &)
state_t * state
Definition Gb_Cpu.h:76
registers_t r
Definition Gb_Cpu.h:42
uint8_t * get_code(gb_addr_t)
Definition Gb_Cpu.h:82
@ idle_addr
Definition Gb_Cpu.h:51
@ cpu_padding
Definition Gb_Cpu.h:61
Gb_Cpu()
Definition Gb_Cpu.h:64
void map_code(gb_addr_t start, unsigned size, void *code)
Definition Gb_Cpu.cpp:71
gb_addr_t rst_base
Definition Gb_Cpu.h:48
void reset(void *unmapped=0)
Definition Gb_Cpu.cpp:55
bool run(blargg_long count)
Definition Gb_Cpu.cpp:92
@ clocks_per_instr
Definition Gb_Cpu.h:14
void set_code_page(int, uint8_t *)
Definition Gb_Cpu.cpp:50
@ page_shift
Definition Gb_Cpu.h:65
@ page_count
Definition Gb_Cpu.h:66
Gb_Cpu(const Gb_Cpu &)
virtual ASIOError start()=0
unsigned short uint16_t
Definition mid.cpp:99
unsigned char uint8_t
Definition mid.cpp:98
Definition Gb_Cpu.h:30
uint8_t l
Definition Gb_Cpu.h:34
uint8_t e
Definition Gb_Cpu.h:34
uint8_t a
Definition Gb_Cpu.h:34
uint8_t d
Definition Gb_Cpu.h:34
uint8_t b
Definition Gb_Cpu.h:34
uint8_t c
Definition Gb_Cpu.h:34
uint8_t h
Definition Gb_Cpu.h:34
uint8_t flags
Definition Gb_Cpu.h:34
Definition Gb_Cpu.h:38
long pc
Definition Gb_Cpu.h:39
uint16_t sp
Definition Gb_Cpu.h:40
Definition Gb_Cpu.h:72
uint8_t * code_map[page_count+1]
Definition Gb_Cpu.h:73
blargg_long remain
Definition Gb_Cpu.h:74
Definition inftrees.h:27
ulg size
Definition extract.c:2350
_WDL_CSTRING_PREFIX void INT_PTR count
Definition wdlcstring.h:263