libsidplayfp  2.2.2
mos652x.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2021 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2009-2014 VICE Project
6  * Copyright 2007-2010 Antti Lankila
7  * Copyright 2000 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 MOS652X_H
25 #define MOS652X_H
26 
27 #include <memory>
28 
29 #include <stdint.h>
30 
31 #include "interrupt.h"
32 #include "timer.h"
33 #include "tod.h"
34 #include "SerialPort.h"
35 #include "EventScheduler.h"
36 
37 #include "sidcxx11.h"
38 
39 class EventContext;
40 
41 namespace libsidplayfp
42 {
43 
44 class MOS652X;
45 
52 class TimerA final : public Timer
53 {
54 private:
58  void underFlow() override;
59 
60  void serialPort() override;
61 
62 public:
67  Timer("CIA Timer A", scheduler, parent) {}
68 };
69 
76 class TimerB final : public Timer
77 {
78 private:
79  void underFlow() override;
80 
81 public:
86  Timer("CIA Timer B", scheduler, parent) {}
87 
91  void cascade()
92  {
93  // we pretend that we are CPU doing a write to ctrl register
94  syncWithCpu();
95  state |= CIAT_STEP;
97  }
98 
104  bool started() const { return (state & CIAT_CR_START) != 0; }
105 };
106 
111 {
112 public:
113  InterruptSource8521(EventScheduler &scheduler, MOS652X &parent) :
114  InterruptSource(scheduler, parent)
115  {}
116 
117  void trigger(uint8_t interruptMask) override;
118 };
119 
124 {
125 private:
127  bool tbBug;
128 
129 private:
130  void triggerBug() { idr &= ~INTERRUPT_UNDERFLOW_B; tbBug = false; }
131 
132 public:
133  InterruptSource6526(EventScheduler &scheduler, MOS652X &parent) :
134  InterruptSource(scheduler, parent),
135  tbBug(false)
136  {}
137 
138  void trigger(uint8_t interruptMask) override;
139 
140  uint8_t clear() override;
141 
142  void reset() override;
143 };
144 
150 class MOS652X
151 {
152  friend class InterruptSource;
153  friend class SerialPort;
154  friend class TimerA;
155  friend class TimerB;
156  friend class Tod;
157 
158 public:
159  typedef enum
160  {
161  MOS6526 = 0
165 
166 private:
167  static const char *credit;
168 
169 protected:
172 
174 
175  uint8_t &pra, &prb, &ddra, &ddrb;
177 
179  uint8_t regs[0x10];
180 
182 
184  TimerB timerB;
186 
188  std::unique_ptr<InterruptSource> interruptSource;
189 
192 
195 
197 
200 
201 private:
205  void todInterrupt();
206 
210  void spInterrupt();
211 
224  void bTick();
225 
229  void underflowA();
230 
232  void underflowB();
233 
237  void handleSerialPort();
238 
239 protected:
245  MOS652X(EventScheduler &scheduler);
246 
253  virtual void interrupt(bool state) = 0;
254 
255  virtual void portA() {}
256  virtual void portB() {}
257 
261  uint8_t adjustDataPort(uint8_t data);
262 
269  uint8_t read(uint_least8_t addr);
270 
279  void write(uint_least8_t addr, uint8_t data);
280 
281 public:
287  void setModel(model_t model);
288 
292  virtual void reset();
293 
299  static const char *credits();
300 
306  void setDayOfTimeRate(unsigned int clock) { tod.setPeriod(clock); }
307 };
308 
309 }
310 
311 #endif // MOS6526_H
Definition: EventCallback.h:36
Definition: EventScheduler.h:62
Definition: mos652x.h:124
void trigger(uint8_t interruptMask) override
Definition: mos652x.cpp:96
uint8_t clear() override
Definition: mos652x.cpp:126
void reset() override
Definition: mos652x.cpp:136
Definition: mos652x.h:111
void trigger(uint8_t interruptMask) override
Definition: mos652x.cpp:75
Definition: interrupt.h:42
uint8_t idr
Interrupt data register.
Definition: interrupt.h:70
InterruptSource(EventScheduler &scheduler, MOS652X &parent)
Definition: interrupt.h:95
@ INTERRUPT_UNDERFLOW_B
underflow Timer B
Definition: interrupt.h:48
Definition: mos652x.h:151
TimerA timerA
Timers A and B.
Definition: mos652x.h:183
void write(uint_least8_t addr, uint8_t data)
Definition: mos652x.cpp:252
uint8_t regs[0x10]
These are all CIA registers.
Definition: mos652x.h:179
uint8_t adjustDataPort(uint8_t data)
Definition: mos652x.cpp:196
EventScheduler & eventScheduler
Event context.
Definition: mos652x.h:171
MOS652X(EventScheduler &scheduler)
Definition: mos652x.cpp:153
uint8_t & pra
Ports.
Definition: mos652x.h:175
Tod tod
TOD.
Definition: mos652x.h:191
model_t
Definition: mos652x.h:160
@ MOS6526W4485
A batch of old CIA model with unique serial port behavior.
Definition: mos652x.h:163
@ MOS6526
Old CIA model, interrupts are delayed by 1 clock.
Definition: mos652x.h:161
@ MOS8521
New CIA model.
Definition: mos652x.h:162
static const char * credits()
Definition: mos652x.cpp:143
void setModel(model_t model)
Definition: mos652x.cpp:353
virtual void reset()
Definition: mos652x.cpp:177
EventCallback< MOS652X > bTickEvent
Events.
Definition: mos652x.h:198
uint8_t read(uint_least8_t addr)
Definition: mos652x.cpp:213
std::unique_ptr< InterruptSource > interruptSource
Interrupt Source.
Definition: mos652x.h:188
SerialPort serialPort
Serial Data Registers.
Definition: mos652x.h:194
void setDayOfTimeRate(unsigned int clock)
Definition: mos652x.h:306
virtual void interrupt(bool state)=0
Definition: SerialPort.h:37
Definition: mos652x.h:53
TimerA(EventScheduler &scheduler, MOS652X &parent)
Definition: mos652x.h:66
Definition: mos652x.h:77
TimerB(EventScheduler &scheduler, MOS652X &parent)
Definition: mos652x.h:85
void cascade()
Definition: mos652x.h:91
bool started() const
Definition: mos652x.h:104
Definition: timer.h:43
void wakeUpAfterSyncWithCpu()
Definition: timer.cpp:60
void syncWithCpu()
Definition: timer.cpp:37
int_least32_t state
CRA/CRB control register / state.
Definition: timer.h:95
MOS652X & parent
Pointer to the MOS6526 which this Timer belongs to.
Definition: timer.h:92
Definition: tod.h:40
void setPeriod(event_clock_t clock)
Definition: tod.h:118