Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libgambatte/src/video/ppu.h
2 views
1
/***************************************************************************
2
* Copyright (C) 2010 by Sindre AamÄs *
3
* [email protected] *
4
* *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License version 2 as *
7
* published by the Free Software Foundation. *
8
* *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License version 2 for more details. *
13
* *
14
* You should have received a copy of the GNU General Public License *
15
* version 2 along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18
***************************************************************************/
19
#ifndef PPU_H
20
#define PPU_H
21
22
#include "video/ly_counter.h"
23
#include "video/sprite_mapper.h"
24
#include "gbint.h"
25
26
#include "newstate.h"
27
28
namespace gambatte {
29
30
enum { LAYER_MASK_BG = 1, LAYER_MASK_OBJ = 2, LAYER_MASK_WINDOW = 4 };
31
32
class PPUFrameBuf {
33
uint_least32_t *buf_;
34
uint_least32_t *fbline_;
35
int pitch_;
36
37
static uint_least32_t * nullfbline() { static uint_least32_t nullfbline_[160]; return nullfbline_; }
38
39
public:
40
PPUFrameBuf() : buf_(0), fbline_(nullfbline()), pitch_(0) {}
41
uint_least32_t * fb() const { return buf_; }
42
uint_least32_t * fbline() const { return fbline_; }
43
int pitch() const { return pitch_; }
44
void setBuf(uint_least32_t *const buf, const int pitch) { buf_ = buf; pitch_ = pitch; fbline_ = nullfbline(); }
45
void setFbline(const unsigned ly) { fbline_ = buf_ ? buf_ + static_cast<long>(ly) * static_cast<long>(pitch_) : nullfbline(); }
46
};
47
48
struct PPUState {
49
void (*f)(struct PPUPriv &v);
50
unsigned (*predictCyclesUntilXpos_f)(const struct PPUPriv &v, int targetxpos, unsigned cycles);
51
unsigned char id;
52
};
53
54
// The PPU loop accesses a lot of state at once, so it's difficult to split this up much beyond grouping stuff into smaller structs.
55
struct PPUPriv {
56
unsigned long bgPalette[8 * 4];
57
unsigned long spPalette[8 * 4];
58
struct Sprite { unsigned char spx, oampos, line, attrib; } spriteList[11];
59
unsigned short spwordList[11];
60
unsigned char nextSprite;
61
unsigned char currentSprite;
62
unsigned layersMask;
63
64
const unsigned char *vram;
65
const PPUState *nextCallPtr;
66
67
unsigned long now;
68
unsigned long lastM0Time;
69
long cycles;
70
71
unsigned tileword;
72
unsigned ntileword;
73
74
SpriteMapper spriteMapper;
75
LyCounter lyCounter;
76
PPUFrameBuf framebuf;
77
78
unsigned char lcdc;
79
unsigned char scy;
80
unsigned char scx;
81
unsigned char wy;
82
unsigned char wy2;
83
unsigned char wx;
84
unsigned char winDrawState;
85
unsigned char wscx;
86
unsigned char winYPos;
87
unsigned char reg0;
88
unsigned char reg1;
89
unsigned char attrib;
90
unsigned char nattrib;
91
unsigned char xpos;
92
unsigned char endx;
93
94
bool cgb;
95
bool weMaster;
96
97
PPUPriv(NextM0Time &nextM0Time, const unsigned char *oamram, const unsigned char *vram);
98
};
99
100
class PPU {
101
PPUPriv p_;
102
public:
103
PPU(NextM0Time &nextM0Time, const unsigned char *oamram, const unsigned char *vram)
104
: p_(nextM0Time, oamram, vram)
105
{
106
}
107
108
unsigned long * bgPalette() { return p_.bgPalette; }
109
bool cgb() const { return p_.cgb; }
110
void doLyCountEvent() { p_.lyCounter.doEvent(); }
111
unsigned long doSpriteMapEvent(unsigned long time) { return p_.spriteMapper.doEvent(time); }
112
const PPUFrameBuf & frameBuf() const { return p_.framebuf; }
113
bool inactivePeriodAfterDisplayEnable(unsigned long cc) const { return p_.spriteMapper.inactivePeriodAfterDisplayEnable(cc); }
114
unsigned long lastM0Time() const { return p_.lastM0Time; }
115
unsigned lcdc() const { return p_.lcdc; }
116
void loadState(const SaveState &state, const unsigned char *oamram);
117
const LyCounter & lyCounter() const { return p_.lyCounter; }
118
unsigned long now() const { return p_.now; }
119
void oamChange(unsigned long cc) { p_.spriteMapper.oamChange(cc); }
120
void oamChange(const unsigned char *oamram, unsigned long cc) { p_.spriteMapper.oamChange(oamram, cc); }
121
unsigned long predictedNextXposTime(unsigned xpos) const;
122
void reset(const unsigned char *oamram, const unsigned char *vram, bool cgb);
123
void resetCc(unsigned long oldCc, unsigned long newCc);
124
void setFrameBuf(uint_least32_t *buf, unsigned pitch) { p_.framebuf.setBuf(buf, pitch); }
125
void setLcdc(unsigned lcdc, unsigned long cc);
126
void setScx(const unsigned scx) { p_.scx = scx; }
127
void setScy(const unsigned scy) { p_.scy = scy; }
128
void setStatePtrs(SaveState &ss) { p_.spriteMapper.setStatePtrs(ss); }
129
void setWx(const unsigned wx) { p_.wx = wx; }
130
void setWy(const unsigned wy) { p_.wy = wy; }
131
void updateWy2() { p_.wy2 = p_.wy; }
132
void speedChange(unsigned long cycleCounter);
133
unsigned long * spPalette() { return p_.spPalette; }
134
void update(unsigned long cc);
135
void setLayers(unsigned mask) { p_.layersMask = mask; }
136
137
template<bool isReader>void SyncState(NewState *ns);
138
};
139
140
}
141
142
#endif
143
144