Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libgambatte/src/video/sprite_mapper.cpp
2 views
1
/***************************************************************************
2
* Copyright (C) 2007 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
#include "sprite_mapper.h"
20
#include "counterdef.h"
21
#include "next_m0_time.h"
22
#include "../insertion_sort.h"
23
#include <cstring>
24
#include <algorithm>
25
26
namespace gambatte {
27
28
SpriteMapper::OamReader::OamReader(const LyCounter &lyCounter, const unsigned char *oamram)
29
: lyCounter(lyCounter), oamram(oamram), cgb_(false) {
30
reset(oamram, false);
31
}
32
33
void SpriteMapper::OamReader::reset(const unsigned char *const oamram, const bool cgb) {
34
this->oamram = oamram;
35
this->cgb_ = cgb;
36
setLargeSpritesSrc(false);
37
lu = 0;
38
lastChange = 0xFF;
39
std::fill_n(szbuf, 40, largeSpritesSrc);
40
41
unsigned pos = 0;
42
unsigned distance = 80;
43
44
while (distance--) {
45
buf[pos] = oamram[((pos * 2) & ~3) | (pos & 1)];
46
++pos;
47
}
48
}
49
50
static unsigned toPosCycles(const unsigned long cc, const LyCounter &lyCounter) {
51
unsigned lc = lyCounter.lineCycles(cc) + 3 - lyCounter.isDoubleSpeed() * 3u;
52
53
if (lc >= 456)
54
lc -= 456;
55
56
return lc;
57
}
58
59
void SpriteMapper::OamReader::update(const unsigned long cc) {
60
if (cc > lu) {
61
if (changed()) {
62
const unsigned lulc = toPosCycles(lu, lyCounter);
63
64
unsigned pos = std::min(lulc, 80u);
65
unsigned distance = 80;
66
67
if ((cc - lu) >> lyCounter.isDoubleSpeed() < 456) {
68
const unsigned cclc = toPosCycles(cc, lyCounter);
69
70
distance = std::min(cclc, 80u) - pos + (cclc < lulc ? 80 : 0);
71
}
72
73
{
74
const unsigned targetDistance = lastChange - pos + (lastChange <= pos ? 80 : 0);
75
76
if (targetDistance <= distance) {
77
distance = targetDistance;
78
lastChange = 0xFF;
79
}
80
}
81
82
while (distance--) {
83
if (!(pos & 1)) {
84
if (pos == 80)
85
pos = 0;
86
87
if (cgb_)
88
szbuf[pos >> 1] = largeSpritesSrc;
89
90
buf[pos ] = oamram[pos * 2 ];
91
buf[pos + 1] = oamram[pos * 2 + 1];
92
} else
93
szbuf[pos >> 1] = (szbuf[pos >> 1] & cgb_) | largeSpritesSrc;
94
95
++pos;
96
}
97
}
98
99
lu = cc;
100
}
101
}
102
103
void SpriteMapper::OamReader::change(const unsigned long cc) {
104
update(cc);
105
lastChange = std::min(toPosCycles(lu, lyCounter), 80u);
106
}
107
108
void SpriteMapper::OamReader::setStatePtrs(SaveState &state) {
109
state.ppu.oamReaderBuf.set(buf, sizeof buf);
110
state.ppu.oamReaderSzbuf.set(szbuf, sizeof(szbuf) / sizeof(bool));
111
}
112
113
void SpriteMapper::OamReader::loadState(const SaveState &ss, const unsigned char *const oamram) {
114
this->oamram = oamram;
115
largeSpritesSrc = ss.mem.ioamhram.get()[0x140] >> 2 & 1;
116
lu = ss.ppu.enableDisplayM0Time;
117
change(lu);
118
}
119
120
SYNCFUNC(SpriteMapper::OamReader)
121
{
122
NSS(buf);
123
NSS(szbuf);
124
125
NSS(lu);
126
NSS(lastChange);
127
NSS(largeSpritesSrc);
128
NSS(cgb_);
129
}
130
131
void SpriteMapper::OamReader::enableDisplay(const unsigned long cc) {
132
std::memset(buf, 0x00, sizeof(buf));
133
std::fill(szbuf, szbuf + 40, false);
134
lu = cc + (80 << lyCounter.isDoubleSpeed());
135
lastChange = 80;
136
}
137
138
SpriteMapper::SpriteMapper(NextM0Time &nextM0Time,
139
const LyCounter &lyCounter,
140
const unsigned char *const oamram) :
141
nextM0Time_(nextM0Time),
142
oamReader(lyCounter, oamram)
143
{
144
clearMap();
145
}
146
147
void SpriteMapper::reset(const unsigned char *const oamram, const bool cgb) {
148
oamReader.reset(oamram, cgb);
149
clearMap();
150
}
151
152
void SpriteMapper::clearMap() {
153
std::memset(num, NEED_SORTING_MASK, sizeof num);
154
}
155
156
void SpriteMapper::mapSprites() {
157
clearMap();
158
159
for (unsigned i = 0x00; i < 0x50; i += 2) {
160
const int spriteHeight = 8 << largeSprites(i >> 1);
161
const unsigned bottom_pos = posbuf()[i] - (17u - spriteHeight);
162
163
if (bottom_pos < 143u + spriteHeight) {
164
const unsigned startly = static_cast<int>(bottom_pos) + 1 - spriteHeight >= 0
165
? static_cast<int>(bottom_pos) + 1 - spriteHeight : 0;
166
unsigned char *map = spritemap + startly * 10;
167
unsigned char *n = num + startly;
168
unsigned char *const nend = num + (bottom_pos < 143 ? bottom_pos : 143) + 1;
169
170
do {
171
if (*n < NEED_SORTING_MASK + 10)
172
map[(*n)++ - NEED_SORTING_MASK] = i;
173
174
map += 10;
175
} while (++n != nend);
176
}
177
}
178
179
nextM0Time_.invalidatePredictedNextM0Time();
180
}
181
182
void SpriteMapper::sortLine(const unsigned ly) const {
183
num[ly] &= ~NEED_SORTING_MASK;
184
insertionSort(spritemap + ly * 10, spritemap + ly * 10 + num[ly], SpxLess(posbuf()));
185
}
186
187
unsigned long SpriteMapper::doEvent(const unsigned long time) {
188
oamReader.update(time);
189
mapSprites();
190
return oamReader.changed() ? time + oamReader.lyCounter.lineTime() : static_cast<unsigned long>(DISABLED_TIME);
191
}
192
193
SYNCFUNC(SpriteMapper)
194
{
195
NSS(spritemap);
196
NSS(num);
197
198
SSS(nextM0Time_);
199
SSS(oamReader);
200
}
201
202
}
203
204