-
Notifications
You must be signed in to change notification settings - Fork 20
/
gbcpu.h
151 lines (121 loc) · 2.84 KB
/
gbcpu.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* gbsplay is a Gameboy sound player
*
* 2003-2021 (C) by Tobias Diedrich <[email protected]>
* Christian Garbs <[email protected]>
*
* Licensed under GNU GPL v1 or, at your option, any later version.
*/
#ifndef _GBCPU_H_
#define _GBCPU_H_
#include <inttypes.h>
#include "common.h"
#define ZF 0x80
#define NF 0x40
#define HF 0x20
#define CF 0x10
#define BC 0
#define DE 1
#define HL 2
#define AF 3
#define SP 4
#define PC 5
#define DEBUG 0
#if DEBUG == 1
#define DPRINTF(...) printf(__VA_ARGS__)
#define DEB(x) x
#define OPINFO(name, fn, cycles_1, cycles_2) {name, fn, cycles_1, cycles_2}
#define CYCLES1(op) (op->cycles_1)
#define CYCLES2(op) (op->cycles_2)
#define CYCLES_OK(op, cycles) \
(op->cycles_1 == 0 || cycles == op->cycles_1 || cycles == op->cycles_2)
#else
/*
static inline void foo(void)
{
}
#define DPRINTF(...) foo()
#define DEB(x) foo()
*/
#define DPRINTF(...) do { } while (0)
#define DEB(x)
#define OPINFO(name, fn, cycles_1, cycles_2) {fn}
#define CYCLES1(op) 0
#define CYCLES2(op) 0
#define CYCLES_OK(op, cycles) 1
#endif
#if GBS_BYTE_ORDER == GBS_ORDER_LITTLE_ENDIAN
#define REGS16_R(r, i) (r.rw[i])
#define REGS16_W(r, i, x) (r.rw[i]) = x
#define REGS8_R(r, i) (r.ri[i^1])
#define REGS8_W(r, i, x) (r.ri[i^1]) = x
typedef union {
uint8_t ri[12];
uint16_t rw[6];
struct {
uint8_t c;
uint8_t b;
uint8_t e;
uint8_t d;
uint8_t l;
uint8_t h;
uint8_t a;
uint8_t f;
uint16_t sp;
uint16_t pc;
} rn;
} gbcpu_regs_u;
#else
#define REGS16_R(r, i) (r.rw[i])
#define REGS16_W(r, i, x) (r.rw[i]) = x
#define REGS8_R(r, i) (r.ri[i])
#define REGS8_W(r, i, x) (r.ri[i]) = x
typedef union {
uint8_t ri[12];
uint16_t rw[6];
struct {
uint8_t b;
uint8_t c;
uint8_t d;
uint8_t e;
uint8_t h;
uint8_t l;
uint8_t f;
uint8_t a;
uint16_t sp;
uint16_t pc;
} rn;
} gbcpu_regs_u;
#endif
typedef void (*gbcpu_put_fn)(void *priv, uint32_t addr, uint8_t val);
typedef uint32_t (*gbcpu_get_fn)(void *priv, uint32_t addr);
struct get_entry {
void *priv;
gbcpu_get_fn get;
};
struct put_entry {
void *priv;
gbcpu_put_fn put;
};
#define GBCPU_LOOKUP_SIZE 256
struct gbcpu {
gbcpu_regs_u regs;
long halt_at_pc;
long halted;
long ime;
long stopped;
cycles_t cycles;
#if DEBUG == 1
gbcpu_regs_u oldregs;
#endif
struct get_entry getlookup[GBCPU_LOOKUP_SIZE];
struct put_entry putlookup[GBCPU_LOOKUP_SIZE];
};
void gbcpu_add_mem(struct gbcpu* const gbcpu, uint32_t start, uint32_t end, gbcpu_put_fn putfn, gbcpu_get_fn getfn, void *priv);
void gbcpu_init(struct gbcpu* const gbcpu);
void gbcpu_init_struct(struct gbcpu* const gbcpu);
long gbcpu_step(struct gbcpu* const gbcpu);
void gbcpu_intr(struct gbcpu* const gbcpu, long vec);
uint8_t gbcpu_mem_get(struct gbcpu* const gbcpu, uint16_t addr);
void gbcpu_mem_put(struct gbcpu* const gbcpu, uint16_t addr, uint8_t val);
#endif