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-2019 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
18#ifndef CARTRIDGE_3EPLUS_HXX
19#define CARTRIDGE_3EPLUS_HXX
20
21class System;
22
23#include "bspf.hxx"
24#include "Cart.hxx"
25
26#ifdef DEBUGGER_SUPPORT
27class Cartridge3EPlusWidget;
28 #include "Cart3EPlusWidget.hxx"
29#endif
30
31/**
32 Cartridge class from Thomas Jentzsch, mostly based on the 'DASH' scheme
33 with the following changes:
34
35 RAM areas:
36 - read $x000, write $x200
37 - read $x400, write $x600
38 - read $x800, write $xa00
39 - read $xc00, write $xe00
40
41 @author Thomas Jentzsch and Stephen Anthony
42*/
43
44class Cartridge3EPlus: public Cartridge
45{
46 friend class Cartridge3EPlusWidget;
47
48 public:
49 /**
50 Create a new cartridge using the specified image and size
51
52 @param image Pointer to the ROM image
53 @param size The size of the ROM image
54 @param md5 The md5sum of the ROM image
55 @param settings A reference to the various settings (read-only)
56 */
57 Cartridge3EPlus(const ByteBuffer& image, size_t size, const string& md5,
58 const Settings& settings);
59 virtual ~Cartridge3EPlus() = default;
60
61 public:
62 /** Reset device to its power-on state */
63 void reset() override;
64
65 /**
66 Install cartridge in the specified system. Invoked by the system
67 when the cartridge is attached to it.
68
69 @param system The system the device should install itself in
70 */
71 void install(System& system) override;
72
73 /**
74 Get the current bank.
75
76 @param address The address to use when querying the bank
77 */
78 uInt16 getBank(uInt16 address = 0) const override;
79
80 /**
81 Query the number of banks supported by the cartridge.
82 */
83 uInt16 bankCount() const override;
84
85 /**
86 Patch the cartridge ROM.
87
88 @param address The ROM address to patch
89 @param value The value to place into the address
90 @return Success or failure of the patch operation
91 */
92 bool patch(uInt16 address, uInt8 value) override;
93
94 /**
95 Access the internal ROM image for this cartridge.
96
97 @param size Set to the size of the internal ROM image data
98 @return A pointer to the internal ROM image data
99 */
100 const uInt8* getImage(size_t& size) const override;
101
102 /**
103 Save the current state of this cart to the given Serializer.
104
105 @param out The Serializer object to use
106 @return False on any errors, else true
107 */
108 bool save(Serializer& out) const override;
109
110 /**
111 Load the current state of this cart from the given Serializer.
112
113 @param in The Serializer object to use
114 @return False on any errors, else true
115 */
116 bool load(Serializer& in) override;
117
118 /**
119 Get a descriptor for the device name (used in error checking).
120
121 @return The name of the object
122 */
123 string name() const override { return "Cartridge3E+"; }
124
125 #ifdef DEBUGGER_SUPPORT
126 /**
127 Get debugger widget responsible for accessing the inner workings
128 of the cart.
129 */
130 CartDebugWidget* debugWidget(GuiObject* boss, const GUI::Font& lfont,
131 const GUI::Font& nfont, int x, int y, int w, int h) override
132 {
133 return new Cartridge3EPlusWidget(boss, lfont, nfont, x, y, w, h, *this);
134 }
135 #endif
136
137 public:
138 /**
139 Get the byte at the specified address
140
141 @return The byte at the specified address
142 */
143 uInt8 peek(uInt16 address) override;
144
145 /**
146 Change the byte at the specified address to the given value
147
148 @param address The address where the value should be stored
149 @param value The value to be stored at the address
150 @return True if the poke changed the device address space, else false
151 */
152 bool poke(uInt16 address, uInt8 value) override;
153
154 private:
155 bool bankRAM(uInt8 bank); // switch a RAM bank
156 bool bankROM(uInt8 bank); // switch a ROM bank
157
158 void bankRAMSlot(uInt16 bank); // switch in a 512b RAM slot (lower or upper 1/2 bank)
159 void bankROMSlot(uInt16 bank); // switch in a 512b RAM slot (read or write port)
160
161 void initializeBankState(); // set all banks according to current bankInUse state
162
163 // We have an array that indicates for each of the 8 512 byte areas of the address space, which ROM/RAM
164 // bank is used in that area. ROM switches 1K so occupies 2 successive entries for each switch. RAM occupies
165 // two as well, one 512 byte for read and one for write. The RAM locations are +0x800 apart, and the ROM
166 // are consecutive. This allows us to determine on a read/write exactly where the data is.
167
168 static constexpr uInt16 BANK_UNDEFINED = 0x8000; // bank is undefined and inaccessible
169 std::array<uInt16, 8> bankInUse; // bank being used for ROM/RAM (eight 512 byte areas)
170
171 static constexpr uInt16 BANK_SWITCH_HOTSPOT_RAM = 0x3E; // writes to this address cause bankswitching
172 static constexpr uInt16 BANK_SWITCH_HOTSPOT_ROM = 0x3F; // writes to this address cause bankswitching
173
174 static constexpr uInt8 BANK_BITS = 6; // # bits for bank
175 static constexpr uInt8 BIT_BANK_MASK = (1 << BANK_BITS) - 1; // mask for those bits
176 static constexpr uInt16 BITMASK_LOWERUPPER = 0x100; // flags lower or upper section of bank (1==upper)
177 static constexpr uInt16 BITMASK_ROMRAM = 0x200; // flags ROM or RAM bank switching (1==RAM)
178
179 static constexpr uInt16 MAXIMUM_BANK_COUNT = (1 << BANK_BITS);
180 static constexpr uInt16 RAM_BANK_TO_POWER = 9; // 2^n = 512
181 static constexpr uInt16 RAM_BANK_SIZE = (1 << RAM_BANK_TO_POWER);
182 static constexpr uInt16 BITMASK_RAM_BANK = (RAM_BANK_SIZE - 1);
183 static constexpr uInt32 RAM_TOTAL_SIZE = MAXIMUM_BANK_COUNT * RAM_BANK_SIZE;
184
185 static constexpr uInt16 ROM_BANK_TO_POWER = 10; // 2^n = 1024
186 static constexpr uInt16 ROM_BANK_SIZE = (1 << ROM_BANK_TO_POWER);
187 static constexpr uInt16 BITMASK_ROM_BANK = (ROM_BANK_SIZE - 1);
188
189 static constexpr uInt16 ROM_BANK_COUNT = 64;
190
191 static constexpr uInt16 RAM_WRITE_OFFSET = 0x200;
192
193 ByteBuffer myImage; // Pointer to a dynamically allocated ROM image of the cartridge
194 size_t mySize; // Size of the ROM image
195 std::array<uInt8, RAM_TOTAL_SIZE> myRAM;
196
197 private:
198 // Following constructors and assignment operators not supported
199 Cartridge3EPlus() = delete;
200 Cartridge3EPlus(const Cartridge3EPlus&) = delete;
201 Cartridge3EPlus(Cartridge3EPlus&&) = delete;
202 Cartridge3EPlus& operator=(const Cartridge3EPlus&) = delete;
203 Cartridge3EPlus& operator=(Cartridge3EPlus&&) = delete;
204};
205
206#endif
207