1 //============================================================================
2 //
3 //   SSSS    tt          lll  lll
4 //  SS  SS   tt           ll   ll
5 //  SS     tttttt  eeee   ll   ll   aaaa
6 //   SSSS    tt   ee  ee  ll   ll      aa
7 //      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator"
8 //  SS  SS   tt   ee      ll   ll  aa  aa
9 //   SSSS     ttt  eeeee llll llll  aaaaa
10 //
11 // Copyright (c) 1995-2014 by Bradford W. Mott, Stephen Anthony
12 // and the Stella Team
13 //
14 // See the file "License.txt" for information on usage and redistribution of
15 // this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 //
17 // $Id: CartFE.cxx 2838 2014-01-17 23:34:03Z stephena $
18 //============================================================================
19 
20 #include <cassert>
21 #include <cstring>
22 
23 #include "System.hxx"
24 #include "CartFE.hxx"
25 
26 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CartridgeFE(const uInt8 * image,uInt32 size,const Settings & settings)27 CartridgeFE::CartridgeFE(const uInt8* image, uInt32 size, const Settings& settings)
28   : Cartridge(settings),
29     myLastAddress1(0),
30     myLastAddress2(0),
31     myLastAddressChanged(false)
32 {
33   // Copy the ROM image into my buffer
34   memcpy(myImage, image, MIN(8192u, size));
35 
36   // We use System::PageAccess.codeAccessBase, but don't allow its use
37   // through a pointer, since the address space of FE carts can change
38   // at the instruction level, and PageAccess is normally defined at an
39   // interval of 64 bytes
40   //
41   // Instead, access will be through the getAccessFlags and setAccessFlags
42   // methods below
43   createCodeAccessBase(8192);
44 }
45 
46 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
~CartridgeFE()47 CartridgeFE::~CartridgeFE()
48 {
49 }
50 
51 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
reset()52 void CartridgeFE::reset()
53 {
54 }
55 
56 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
install(System & system)57 void CartridgeFE::install(System& system)
58 {
59   mySystem = &system;
60   uInt16 shift = mySystem->pageShift();
61   uInt16 mask = mySystem->pageMask();
62 
63   // Make sure the system we're being installed in has a page size that'll work
64   assert((0x1000 & mask) == 0);
65 
66   System::PageAccess access(0, 0, 0, this, System::PA_READ);
67 
68   // Map all of the accesses to call peek and poke
69   for(uInt32 i = 0x1000; i < 0x2000; i += (1 << shift))
70     mySystem->setPageAccess(i >> shift, access);
71 }
72 
73 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
peek(uInt16 address)74 uInt8 CartridgeFE::peek(uInt16 address)
75 {
76   // The bank is determined by A13 of the processor
77   // We keep track of the two most recent accesses to determine which bank
78   // we're in, and when the values actually changed
79   myLastAddress2 = myLastAddress1;
80   myLastAddress1 = address;
81   myLastAddressChanged = true;
82 
83   return myImage[(address & 0x0FFF) + (((address & 0x2000) == 0) ? 4096 : 0)];
84 }
85 
86 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
poke(uInt16,uInt8)87 bool CartridgeFE::poke(uInt16, uInt8)
88 {
89   return false;
90 }
91 
92 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
getAccessFlags(uInt16 address)93 uInt8 CartridgeFE::getAccessFlags(uInt16 address)
94 {
95   return myCodeAccessBase[(address & 0x0FFF) +
96             (((address & 0x2000) == 0) ? 4096 : 0)];
97 }
98 
99 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setAccessFlags(uInt16 address,uInt8 flags)100 void CartridgeFE::setAccessFlags(uInt16 address, uInt8 flags)
101 {
102   myCodeAccessBase[(address & 0x0FFF) +
103       (((address & 0x2000) == 0) ? 4096 : 0)] |= flags;
104 }
105 
106 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bank(uInt16)107 bool CartridgeFE::bank(uInt16)
108 {
109   // Doesn't support bankswitching in the normal sense
110   return false;
111 }
112 
113 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bank() const114 uInt16 CartridgeFE::bank() const
115 {
116   // The current bank depends on the last address accessed
117   return ((myLastAddress1 & 0x2000) == 0) ? 1 : 0;
118 }
119 
120 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bankCount() const121 uInt16 CartridgeFE::bankCount() const
122 {
123   return 2;
124 }
125 
126 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bankChanged()127 bool CartridgeFE::bankChanged()
128 {
129   if(myLastAddressChanged)
130   {
131     // A bankswitch occurs when the addresses transition from state to another
132     myBankChanged = ((myLastAddress1 & 0x2000) == 0) !=
133                     ((myLastAddress2 & 0x2000) == 0);
134     myLastAddressChanged = false;
135   }
136   else
137     myBankChanged = false;
138 
139   // In any event, let the base class know about it
140   return Cartridge::bankChanged();
141 }
142 
143 
144 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
patch(uInt16 address,uInt8 value)145 bool CartridgeFE::patch(uInt16 address, uInt8 value)
146 {
147   myImage[(address & 0x0FFF) + (((address & 0x2000) == 0) ? 4096 : 0)] = value;
148   return myBankChanged = true;
149 }
150 
151 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
getImage(int & size) const152 const uInt8* CartridgeFE::getImage(int& size) const
153 {
154   size = 8192;
155   return myImage;
156 }
157 
158 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
save(Serializer & out) const159 bool CartridgeFE::save(Serializer& out) const
160 {
161    out.putString(name());
162    out.putShort(myLastAddress1);
163    out.putShort(myLastAddress2);
164 
165    return true;
166 }
167 
168 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
load(Serializer & in)169 bool CartridgeFE::load(Serializer& in)
170 {
171    if(in.getString() != name())
172       return false;
173 
174    myLastAddress1 = in.getShort();
175    myLastAddress2 = in.getShort();
176 
177    return true;
178 }
179