libsidplayfp  1.3.0
mos6526.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright 2000 Simon White
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef MOS6526_H
24 #define MOS6526_H
25 
26 #include <stdint.h>
27 
28 #include "timer.h"
29 #include "sidplayfp/EventScheduler.h"
30 #include "sidplayfp/component.h"
31 
32 class EventContext;
33 class MOS6526;
34 
41 class TimerA : public Timer
42 {
43 private:
47  void underFlow();
48 
49  void serialPort();
50 
51 public:
56  Timer("CIA Timer A", context, parent) {}
57 };
58 
65 class TimerB : public Timer
66 {
67 private:
68  void underFlow();
69 
70 public:
75  Timer("CIA Timer B", context, parent) {}
76 
80  void cascade()
81  {
82  /* we pretend that we are CPU doing a write to ctrl register */
83  syncWithCpu();
84  state |= CIAT_STEP;
86  }
87 
93  bool started() const { return (state & CIAT_CR_START) != 0; }
94 };
95 
102 class MOS6526: public component
103 {
104  friend class TimerA;
105  friend class TimerB;
106 
107 private:
108  static const char *credit;
109 
110 protected:
114  uint8_t regs[0x10];
115 
116  // Ports
117  uint8_t &pra, &prb, &ddra, &ddrb;
118 
123  TimerB timerB;
124 
125  // Serial Data Registers
126  uint8_t sdr_out;
127  bool sdr_buffered;
128  int sdr_count;
129 
131  uint8_t icr;
132 
134  uint8_t idr;
135 
140 
141  // TOD
142  bool m_todlatched;
143  bool m_todstopped;
144  uint8_t m_todclock[4], m_todalarm[4], m_todlatch[4];
145  event_clock_t m_todCycles, m_todPeriod;
146 
149 
150  // Events
151  EventCallback<MOS6526> bTickEvent;
152  EventCallback<MOS6526> todEvent;
153  EventCallback<MOS6526> triggerEvent;
154 
155 protected:
161  MOS6526(EventContext *context);
162  ~MOS6526() {}
163 
176  void bTick();
177 
181  void tod();
182 
186  void trigger();
187 
191  void underflowA();
192 
196  void underflowB();
197 
203  void trigger(uint8_t interruptMask);
204 
208  void clear();
209 
213  void serialPort();
214 
221  virtual void interrupt(bool state) = 0;
222 
223  virtual void portA() {}
224  virtual void portB() {}
225 
232  uint8_t read(uint_least8_t addr);
233 
242  void write(uint_least8_t addr, uint8_t data);
243 
244 private:
245  // TOD implementation taken from Vice
246  static uint8_t byte2bcd(uint8_t byte) { return (((byte / 10) << 4) + (byte % 10)) & 0xff; }
247  static uint8_t bcd2byte(uint8_t bcd) { return ((10*((bcd & 0xf0) >> 4)) + (bcd & 0xf)) & 0xff; }
248 
249 public:
253  virtual void reset();
254 
260  const char *credits() const { return credit; }
261 
267  void setDayOfTimeRate(unsigned int clock);
268 };
269 
270 #endif // MOS6526_H
void bTick()
Definition: mos6526.cpp:360
uint8_t regs[0x10]
Definition: mos6526.h:114
Definition: mos6526.h:65
Definition: mos6526.h:102
uint8_t idr
Definition: mos6526.h:134
TimerB(EventContext *context, MOS6526 *parent)
Definition: mos6526.h:74
TimerA timerA
Definition: mos6526.h:122
Definition: mos6526.h:41
bool started() const
Definition: mos6526.h:93
void cascade()
Definition: mos6526.h:80
uint8_t read(uint_least8_t addr)
Definition: mos6526.cpp:166
Definition: event.h:101
void trigger()
Definition: mos6526.cpp:339
void clear()
Definition: mos6526.cpp:123
MOS6526 *const parent
Definition: timer.h:100
void underflowA()
Definition: mos6526.cpp:365
Definition: component.h:28
bool triggerScheduled
Definition: mos6526.h:148
void serialPort()
Definition: mos6526.cpp:102
int_least32_t state
Definition: timer.h:105
void underflowB()
Definition: mos6526.cpp:377
void write(uint_least8_t addr, uint8_t data)
Definition: mos6526.cpp:240
uint8_t icr
Definition: mos6526.h:131
MOS6526(EventContext *context)
Definition: mos6526.cpp:85
void tod()
Definition: mos6526.cpp:382
virtual void reset()
Definition: mos6526.cpp:136
virtual void interrupt(bool state)=0
void syncWithCpu()
Definition: timer.cpp:34
void wakeUpAfterSyncWithCpu()
Definition: timer.cpp:57
TimerA(EventContext *context, MOS6526 *parent)
Definition: mos6526.h:55
EventContext & event_context
Definition: mos6526.h:139
Definition: timer.h:38
void setDayOfTimeRate(unsigned int clock)
Definition: mos6526.cpp:131
const char * credits() const
Definition: mos6526.h:260