libsidplayfp  1.8.3
mos6526.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 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 "tod.h"
30 #include "EventScheduler.h"
31 #include "c64/component.h"
32 
33 class EventContext;
34 class MOS6526;
35 
42 class TimerA : public Timer
43 {
44 private:
48  void underFlow();
49 
50  void serialPort();
51 
52 public:
57  Timer("CIA Timer A", context, parent) {}
58 };
59 
66 class TimerB : public Timer
67 {
68 private:
69  void underFlow();
70 
71 public:
76  Timer("CIA Timer B", context, parent) {}
77 
81  void cascade()
82  {
83  /* we pretend that we are CPU doing a write to ctrl register */
84  syncWithCpu();
85  state |= CIAT_STEP;
87  }
88 
94  bool started() const { return (state & CIAT_CR_START) != 0; }
95 };
96 
103 class MOS6526: public component
104 {
105  friend class TimerA;
106  friend class TimerB;
107  friend class Tod;
108 
109 private:
110  static const char *credit;
111 
112 protected:
114  uint8_t regs[0x10];
115 
117 
118  uint8_t &pra, &prb, &ddra, &ddrb;
120 
122 
126 
128 
129  uint8_t sdr_out;
133 
135  uint8_t icr;
136 
138  uint8_t idr;
139 
142 
145 
148 
150 
154 
155 protected:
161  MOS6526(EventContext *context);
162  ~MOS6526() {}
163 
176  void bTick();
177 
181  void trigger();
182 
186  void underflowA();
187 
189  void underflowB();
190 
196  void trigger(uint8_t interruptMask);
197 
201  void clear();
202 
206  void serialPort();
207 
214  virtual void interrupt(bool state) = 0;
215 
216  virtual void portA() {}
217  virtual void portB() {}
218 
225  uint8_t read(uint_least8_t addr);
226 
235  void write(uint_least8_t addr, uint8_t data);
236 
237 private:
238  void todInterrupt();
239 
240 public:
244  virtual void reset();
245 
252  static const char *credits() { return credit; }
253 
259  void setDayOfTimeRate(unsigned int clock) { tod.setPeriod(clock); }
260 };
261 
262 #endif // MOS6526_H
void bTick()
Definition: mos6526.cpp:312
uint8_t regs[0x10]
These are all CIA registers.
Definition: mos6526.h:114
uint8_t & ddrb
Ports.
Definition: mos6526.h:118
Definition: mos6526.h:66
Definition: mos6526.h:103
uint8_t idr
Interrupt data register.
Definition: mos6526.h:138
TimerB(EventContext *context, MOS6526 *parent)
Definition: mos6526.h:75
TimerA timerA
Timers A and B.
Definition: mos6526.h:123
int sdr_count
Serial Data Registers.
Definition: mos6526.h:131
uint8_t sdr_out
Serial Data Registers.
Definition: mos6526.h:129
Definition: mos6526.h:42
bool started() const
Definition: mos6526.h:94
void cascade()
Definition: mos6526.h:81
uint8_t read(uint_least8_t addr)
Definition: mos6526.cpp:154
Definition: event.h:107
void trigger()
Definition: mos6526.cpp:291
void setPeriod(event_clock_t clock)
Definition: tod.h:109
static const char * credits()
Definition: mos6526.h:252
EventCallback< MOS6526 > bTickEvent
Events.
Definition: mos6526.h:151
void clear()
Definition: mos6526.cpp:123
MOS6526 *const parent
Pointer to the MOS6526 which this Timer belongs to.
Definition: timer.h:88
uint8_t & ddra
Ports.
Definition: mos6526.h:118
void underflowA()
Definition: mos6526.cpp:317
EventCallback< MOS6526 > triggerEvent
Events.
Definition: mos6526.h:152
Tod tod
TOD.
Definition: mos6526.h:144
Definition: component.h:28
bool triggerScheduled
Have we already scheduled CIA->CPU interrupt transition?
Definition: mos6526.h:147
void serialPort()
Definition: mos6526.cpp:102
int_least32_t state
CRA/CRB control register / state.
Definition: timer.h:91
void underflowB()
Definition: mos6526.cpp:329
uint8_t & prb
Ports.
Definition: mos6526.h:118
bool sdr_buffered
Serial Data Registers.
Definition: mos6526.h:130
void write(uint_least8_t addr, uint8_t data)
Definition: mos6526.cpp:216
uint8_t icr
Interrupt control register.
Definition: mos6526.h:135
MOS6526(EventContext *context)
Definition: mos6526.cpp:86
virtual void reset()
Definition: mos6526.cpp:131
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:56
EventContext & event_context
Event context.
Definition: mos6526.h:141
uint8_t & pra
Ports.
Definition: mos6526.h:118
Definition: timer.h:38
void setDayOfTimeRate(unsigned int clock)
Definition: mos6526.h:259
Definition: tod.h:36
TimerB timerB
Timers A and B.
Definition: mos6526.h:124