libsidplayfp  2.0.2
sprites.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2014 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2009-2014 VICE Project
6  * Copyright 2007-2010 Antti Lankila
7  * Copyright 2001 Simon White
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #ifndef SPRITES_H
25 #define SPRITES_H
26 
27 #include <stdint.h>
28 
29 #include <cstring>
30 
31 #define SPRITES 8
32 
33 namespace libsidplayfp
34 {
35 
39 class Sprites
40 {
41 private:
42  const uint8_t &enable, &y_expansion;
43 
44  uint8_t exp_flop;
45  uint8_t dma;
46  uint8_t mc_base[SPRITES];
47  uint8_t mc[SPRITES];
48 
49 public:
50  Sprites(uint8_t regs[0x40]) :
51  enable(regs[0x15]),
52  y_expansion(regs[0x17]) {}
53 
54  void reset()
55  {
56  exp_flop = 0xff;
57  dma = 0;
58 
59  memset(mc_base, 0, sizeof(mc_base));
60  memset(mc, 0, sizeof(mc));
61  }
62 
67  void updateMc()
68  {
69  uint8_t mask = 1;
70  for (unsigned int i = 0; i < SPRITES; i++, mask <<= 1)
71  {
72  if (dma & mask)
73  mc[i] = (mc[i] + 3) & 0x3f;
74  }
75  }
76 
80  void updateMcBase()
81  {
82  uint8_t mask = 1;
83  for (unsigned int i = 0; i < SPRITES; i++, mask <<= 1)
84  {
85  if (exp_flop & mask)
86  {
87  mc_base[i] = mc[i];
88  if (mc_base[i] == 0x3f)
89  dma &= ~mask;
90  }
91  }
92  }
93 
97  void checkExp()
98  {
99  exp_flop ^= dma & y_expansion;
100  }
101 
106  {
107  for (unsigned int i = 0; i < SPRITES; i++)
108  {
109  mc[i] = mc_base[i];
110  }
111  }
112 
119  void checkDma(unsigned int rasterY, uint8_t regs[0x40])
120  {
121  const uint8_t y = rasterY & 0xff;
122  uint8_t mask = 1;
123  for (unsigned int i = 0; i < SPRITES; i++, mask <<= 1)
124  {
125  if ((enable & mask) && (y == regs[(i << 1) + 1]) && !(dma & mask))
126  {
127  dma |= mask;
128  mc_base[i] = 0;
129  exp_flop |= mask;
130  }
131  }
132  }
133 
140  void lineCrunch(uint8_t data, unsigned int lineCycle)
141  {
142  uint8_t mask = 1;
143  for (unsigned int i = 0; i < SPRITES; i++, mask <<= 1)
144  {
145  if (!(data & mask) && !(exp_flop & mask))
146  {
147  // sprite crunch
148  if (lineCycle == 14)
149  {
150  const uint8_t mc_i = mc[i];
151  const uint8_t mcBase_i = mc_base[i];
152 
153  mc[i] = (0x2a & (mcBase_i & mc_i)) | (0x15 & (mcBase_i | mc_i));
154 
155  // mcbase will be set from mc on the following clock call
156  }
157 
158  exp_flop |= mask;
159  }
160  }
161  }
162 
168  inline bool isDma(unsigned int val) const
169  {
170  return dma & val;
171  }
172 };
173 
174 }
175 
176 #endif
libsidplayfp::Sprites::checkDisplay
void checkDisplay()
Definition: sprites.h:105
libsidplayfp::Sprites::isDma
bool isDma(unsigned int val) const
Definition: sprites.h:168
libsidplayfp::Sprites::checkExp
void checkExp()
Definition: sprites.h:97
libsidplayfp::Sprites::updateMc
void updateMc()
Definition: sprites.h:67
libsidplayfp::Sprites::lineCrunch
void lineCrunch(uint8_t data, unsigned int lineCycle)
Definition: sprites.h:140
libsidplayfp::Sprites::updateMcBase
void updateMcBase()
Definition: sprites.h:80
libsidplayfp::Sprites::checkDma
void checkDma(unsigned int rasterY, uint8_t regs[0x40])
Definition: sprites.h:119
libsidplayfp::Sprites
Definition: sprites.h:40