-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
132 lines (106 loc) · 3.37 KB
/
main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <assert.h>
#include <SDL.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern char DEBUG;
extern unsigned long long COUNTDOWN;
typedef struct {
unsigned char memory[64 * 1024];
// Pair orderings are important because you can use these in pairs: AF, BC, DE, HL.
// This assumes little-endian architecture since A should be most significant when reading AF.
unsigned char F;
unsigned char A;
unsigned char C;
unsigned char B;
unsigned char E;
unsigned char D;
unsigned char L;
unsigned char H;
unsigned short SP;
unsigned short PC;
int set_ime_after_n_instructions;
int unset_ime_after_n_instructions;
bool IME;
bool halted;
bool halt_di_bug;
unsigned long long global_simulated_ticks; // In CPU clock ticks.
int ticks_to_next_instruction;
SDL_Window* window;
SDL_Surface* window_surface;
} Gameboy;
#define REG_JOYP (0xFF00)
#define REG_SB (0xFF01)
#define REG_SC (0xFF02)
#define REG_DIV (0xFF04)
#define REG_TIMA (0xFF05)
#define REG_TMA (0xFF06)
#define REG_TAC (0xFF07)
#define REG_IF (0xFF0F)
#define REG_NR10 (0xFF10)
#define REG_NR11 (0xFF11)
#define REG_NR12 (0xFF12)
#define REG_NR14 (0xFF14)
#define REG_NR21 (0xFF16)
#define REG_NR22 (0xFF17)
#define REG_NR24 (0xFF19)
#define REG_NR30 (0xFF1A)
#define REG_NR31 (0xFF1B)
#define REG_NR32 (0xFF1C)
#define REG_NR33 (0xFF1E)
#define REG_NR41 (0xFF20)
#define REG_NR42 (0xFF21)
#define REG_NR43 (0xFF22)
#define REG_NR44 (0xFF23)
#define REG_NR50 (0xFF24)
#define REG_NR51 (0xFF25)
#define REG_NR52 (0xFF26)
#define REG_LCDC (0xFF40)
#define REG_STAT (0xFF41)
#define REG_SCY (0xFF42)
#define REG_SCX (0xFF43)
#define REG_LY (0xFF44)
#define REG_LYC (0xFF45)
#define REG_DMA (0xFF46)
#define REG_BGP (0xFF47)
#define REG_OBP0 (0xFF48)
#define REG_OBP1 (0xFF49)
#define REG_WY (0xFF4A)
#define REG_WX (0xFF4B)
#define REG_IE (0xFFFF)
#define CPU_AF_REF(c) ((unsigned short*)&(c)->F)
#define CPU_BC_REF(c) ((unsigned short*)&(c)->C)
#define CPU_DE_REF(c) ((unsigned short*)&(c)->E)
#define CPU_HL_REF(c) ((unsigned short*)&(c)->L)
#define CPU_AF(c) (*CPU_AF_REF(c))
#define CPU_BC(c) (*CPU_BC_REF(c))
#define CPU_DE(c) (*CPU_DE_REF(c))
#define CPU_HL(c) (*CPU_HL_REF(c))
#define BIT(v, index) (((v) >> (index)) & 1)
#define CPU_FLAG_Z(f) BIT((f), 7)
#define CPU_FLAG_N(f) BIT((f), 6)
#define CPU_FLAG_H(f) BIT((f), 5)
#define CPU_FLAG_C(f) BIT((f), 4)
#define CPU_F(z, n, h, c) ((z) << 7 | (n) << 6 | (h) << 5 | (c) << 4)
#define SET_BIT(v, index) ((v) | (1 << (index)))
#define UNSET_BIT(v, index) ((v) & ~(1 << (index)))
int gameboy_execute_instruction(Gameboy* gb);
void gameboy_read_mem(Gameboy* gb, unsigned short address, unsigned char* output, int len);
void gameboy_write_mem(Gameboy* gb, unsigned short address, unsigned char* data, int len);
#define GB_SCREEN_WIDTH (160)
#define GB_SCREEN_HEIGHT (144)
typedef struct {
unsigned char yplusheight;
unsigned char xpluswidth;
unsigned char tile;
unsigned char attributes;
} sprite;
#define VIDEO_OAM_OBJ_PRIORITY(f) BIT((f), 7)
#define VIDEO_OAM_Y_FLIP(f) BIT((f), 6)
#define VIDEO_OAM_X_FLIP(f) BIT((f), 5)
#define VIDEO_OAM_PALETTE_NUMBER(f) BIT((f), 4)
#define VIDEO_OAM_NUM_SPRITES (40)
#define VIDEO_TILE_0(gb, t) (gb->memory[0x8800 + ((char)t + 128) * 16])
#define VIDEO_TILE_1(gb, t) (gb->memory[0x8000 + (t) * 16])
#define VIDEO_BG_MAP(gb, map, x, y) (gb->memory[((map) ? 0x9c00 : 0x9800) + (y) * 32 + (x)])