libsidplayfp  1.0.1
c64cia.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 2001 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 C64CIA_H
24 #define C64CIA_H
25 
26 // The CIA emulations are very generic and here we need to effectively
27 // wire them into the computer (like adding a chip to a PCB).
28 
29 #include "Banks/Bank.h"
30 #include "sidplayfp/c64/c64env.h"
31 #include "sidplayfp/sidendian.h"
32 #include "CIA/mos6526.h"
33 
39 class c64cia1: public MOS6526, public Bank
40 {
41 private:
42  c64env &m_env;
43  uint_least16_t t1a;
44  uint8_t lp;
45 
46 protected:
47  void interrupt(bool state)
48  {
49  m_env.interruptIRQ (state);
50  }
51 
52  void portB()
53  {
54  const uint8_t lp = (prb | ~ddrb) & 0x10;
55  if (lp != this->lp)
56  {
57  m_env.lightpen();
58  this->lp = lp;
59  }
60  }
61 
62 public:
63  c64cia1(c64env *env) :
64  MOS6526(&(env->context ())),
65  m_env(*env) {}
66 
67  void poke(uint_least16_t address, uint8_t value)
68  {
69  // Save the value written to Timer A
70  if (address == 0xDC04)
71  {
72  endian_16lo8(t1a, value);
73  }
74  else if (address == 0xDC05)
75  {
76  endian_16hi8 (t1a, value);
77  }
78  write(endian_16lo8(address), value);
79  }
80 
81  uint8_t peek(uint_least16_t address)
82  {
83  return read(endian_16lo8(address));
84  }
85 
86  const char *error() const { return ""; }
87 
88  void reset()
89  {
90  lp = 0x10;
91  MOS6526::reset ();
92  }
93 
94  uint_least16_t getTimerA() const { return t1a; }
95 };
96 
102 class c64cia2: public MOS6526, public Bank
103 {
104 private:
105  c64env &m_env;
106 
107 protected:
108  void interrupt(bool state)
109  {
110  if (state)
111  m_env.interruptNMI ();
112  }
113 
114 public:
115  c64cia2(c64env *env) :
116  MOS6526(&(env->context ())),
117  m_env(*env) {}
118 
119  void poke(uint_least16_t address, uint8_t value)
120  {
121  write(address, value);
122  }
123 
124  uint8_t peek(uint_least16_t address)
125  {
126  return read(address);
127  }
128 
129  const char *error() const { return ""; }
130 };
131 
132 #endif // C64CIA_H