libsidplayfp  1.6.0
mos656x.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 MOS656X_H
25 #define MOS656X_H
26 
27 #include <stdint.h>
28 
29 #include "lightpen.h"
30 #include "sprites.h"
31 #include "sidplayfp/event.h"
32 #include "c64/component.h"
33 #include "EventScheduler.h"
34 
35 
36 class MOS656X: public component, private Event
37 {
38 public:
39  typedef enum
40  {
45  } model_t;
46 
47 private:
48  typedef struct
49  {
50  unsigned int rasterLines;
51  unsigned int cyclesPerLine;
52  event_clock_t (MOS656X::*clock)();
53  } model_data_t;
54 
55 private:
56  static const char *credit;
57 
58  static const model_data_t modelData[];
59 
61  static const int IRQ_RASTER = 1 << 0;
62 
64  static const int IRQ_LIGHTPEN = 1 << 3;
65 
67  static const unsigned int FIRST_DMA_LINE = 0x30;
68 
70  static const unsigned int LAST_DMA_LINE = 0xf7;
71 
72 private:
73  event_clock_t (MOS656X::*clock)();
74 
75  event_clock_t rasterClk;
76 
78  EventContext &event_context;
79 
81  unsigned int cyclesPerLine;
82 
84  unsigned int maxRasters;
85 
87  unsigned int lineCycle;
88 
90  unsigned int rasterY;
91 
93  unsigned int yscroll;
94 
96  bool areBadLinesEnabled;
97 
99  bool isBadLine;
100 
102  bool rasterYIRQCondition;
103 
105  bool vblanking;
106 
108  bool lpAsserted;
109 
111  uint8_t irqFlags;
112 
114  uint8_t irqMask;
115 
117  Lightpen lp;
118 
120  Sprites sprites;
121 
123  uint8_t regs[0x40];
124 
125 private:
126  event_clock_t clockPAL();
127  event_clock_t clockNTSC();
128  event_clock_t clockOldNTSC();
129 
133  void handleIrqState();
134 
135  EventCallback<MOS656X> badLineStateChangeEvent;
136 
140  void badLineStateChange() { setBA(!isBadLine); }
141 
142  EventCallback<MOS656X> rasterYIRQEdgeDetectorEvent;
143 
147  void rasterYIRQEdgeDetector()
148  {
149  const bool oldRasterYIRQCondition = rasterYIRQCondition;
150  rasterYIRQCondition = rasterY == readRasterLineIRQ();
151  if (!oldRasterYIRQCondition && rasterYIRQCondition)
152  activateIRQFlag(IRQ_RASTER);
153  }
154 
159  void activateIRQFlag(int flag)
160  {
161  irqFlags |= flag;
162  handleIrqState();
163  }
164 
170  unsigned int readRasterLineIRQ() const
171  {
172  return (regs[0x12] & 0xff) + ((regs[0x11] & 0x80) << 1);
173  }
174 
180  bool readDEN() const { return (regs[0x11] & 0x10) != 0; }
181 
185  inline unsigned int oldRasterY()
186  {
187  const int prevRasterY = rasterY - 1;
188  return prevRasterY >= 0 ? prevRasterY : cyclesPerLine - 1;
189  }
190 
191  inline void sync()
192  {
193  event_context.cancel(*this);
194  event();
195  }
196 
200  inline void checkVblank()
201  {
202  // IRQ occurred (xraster != 0)
203  if (rasterY == (maxRasters - 1))
204  {
205  vblanking = true;
206  }
207 
208  // Check DEN bit on first cycle of the line following the first DMA line
209  if (rasterY == FIRST_DMA_LINE
210  && !areBadLinesEnabled
211  && readDEN())
212  {
213  areBadLinesEnabled = true;
214  }
215 
216  // Disallow bad lines after the last possible one has passed
217  if (rasterY == LAST_DMA_LINE)
218  {
219  areBadLinesEnabled = false;
220  }
221 
222  isBadLine = false;
223 
224  if (!vblanking)
225  {
226  rasterY++;
227  rasterYIRQEdgeDetector();
228  }
229  }
230 
231  inline void vblank()
232  {
233  // Vertical blank (line 0)
234  if (vblanking)
235  {
236  vblanking = false;
237  rasterY = 0;
238  rasterYIRQEdgeDetector();
239  lp.untrigger();
240  if (lpAsserted && lp.retrigger(lineCycle, rasterY))
241  {
242  activateIRQFlag(IRQ_LIGHTPEN);
243  }
244  }
245  }
246 
250  template<int n>
251  inline void startDma()
252  {
253  if (sprites.isDma(0x01 << n))
254  setBA(false);
255  }
256 
260  template<int n>
261  inline void endDma()
262  {
263  if (!sprites.isDma(0x06 << n))
264  setBA(true);
265  }
266 
270  inline void startBadline()
271  {
272  if (isBadLine)
273  setBA(false);
274  }
275 
276 protected:
277  MOS656X(EventContext *context);
278  ~MOS656X() {}
279 
280  // Environment Interface
281  virtual void interrupt (bool state) = 0;
282  virtual void setBA (bool state) = 0;
283 
290  uint8_t read(uint_least8_t addr);
291 
300  void write(uint_least8_t addr, uint8_t data);
301 
302 public:
303  void event();
304 
305  void chip(model_t model);
306 
310  void triggerLightpen();
311 
315  void clearLightpen();
316 
317  // Component Standard Calls
318  void reset();
319 
320  static const char *credits() { return credit; }
321 };
322 
323 // Template specializations
324 
328 template<>
329 inline void MOS656X::startDma<0>()
330 {
331  setBA(!sprites.isDma(0x01));
332 }
333 
337 template<>
338 inline void MOS656X::endDma<7>()
339 {
340  setBA(true);
341 }
342 
343 #endif // MOS656X_H
void untrigger()
Definition: lightpen.h:133
Definition: mos656x.h:36
Definition: sprites.h:33
model_t
Definition: mos656x.h:39
void event()
Definition: mos656x.cpp:246
Definition: event.h:107
PAL-N.
Definition: mos656x.h:44
Definition: c64.cpp:29
bool isDma(unsigned int val)
Definition: sprites.h:159
Definition: component.h:28
Definition: lightpen.h:30
uint8_t read(uint_least8_t addr)
Definition: mos656x.cpp:102
void clearLightpen()
Definition: mos656x.cpp:679
void write(uint_least8_t addr, uint8_t data)
Definition: mos656x.cpp:138
bool retrigger(unsigned int lineCycle, unsigned int rasterY)
Definition: lightpen.h:88
Definition: event.h:50
virtual void cancel(Event &event)=0
OLD NTSC CHIP.
Definition: mos656x.h:41
NTSC-M.
Definition: mos656x.h:42
void triggerLightpen()
Definition: mos656x.cpp:666
PAL-B.
Definition: mos656x.h:43