File: NstBoardBmcGamestarA.cpp

package info (click to toggle)
libretro-nestopia 1.52.0%2B20230102.gitcb1e24e-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,960 kB
  • sloc: cpp: 107,513; xml: 27,221; python: 1,329; ansic: 772; makefile: 634
file content (249 lines) | stat: -rw-r--r-- 5,656 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#include "NstBoard.hpp"
#include "../NstDipSwitches.hpp"
#include "NstBoardBmcGamestarA.hpp"
#include "../NstCrc32.hpp"

namespace Nes
{
	namespace Core
	{
		namespace Boards
		{
			namespace Bmc
			{
				#ifdef NST_MSVC_OPTIMIZE
				#pragma optimize("s", on)
				#endif

				class GamestarA::CartSwitches : public DipSwitches
				{
					enum Type
					{
						GKA_6IN1A   = 0x2A80F48F,
						GKA_6IN1B   = 0xF274BF1F,
						GKA_47IN1   = 0x8DA67F2D,
						GKA_54IN1   = 0x38EB6D5A,
						GKA_103IN1  = 0xB1F9BD94
					};

					uint mode;
					const Type type;

					explicit CartSwitches(Type t)
					: mode(0), type(t) {}

				public:

					static CartSwitches* Create(const Context& c)
					{
						switch (const dword crc = Crc32::Compute(c.prg.Mem(),c.prg.Size()))
						{
							case GKA_6IN1A:
							case GKA_6IN1B:
							case GKA_47IN1:
							case GKA_54IN1:
							case GKA_103IN1:

								return new CartSwitches( static_cast<Type>(crc) );
						}

						return NULL;
					}

					void SetMode(uint value)
					{
						mode = value & 0x3;
					}

					uint GetMode() const
					{
						return mode;
					}

				private:

					uint GetValue(uint) const
					{
						return mode;
					}

					void SetValue(uint,uint value)
					{
						mode = value;
					}

					uint NumDips() const
					{
						return 1;
					}

					uint NumValues(uint) const
					{
						return 4;
					}

					cstring GetDipName(uint) const
					{
						return "Mode";
					}

					cstring GetValueName(uint,uint i) const
					{
						static cstring const names[4][4] =
						{
							{ "6-in-1",   "2-in-1 (1)", "2-in-1 (2)", "2-in-1 (3)" },
							{ "47-in-1",  "57-in-1",    "67-in-1",    "77-in-1"    },
							{ "54-in-1",  "74-in-1",    "84-in-1",    "94-in-1"    },
							{ "103-in-1", "105-in-1",   "106-in-1",   "109-in-1"   }
						};

						switch (type)
						{
							case GKA_6IN1A:
							case GKA_6IN1B:
							default:

								return names[0][i];

							case GKA_47IN1:

								return names[1][i];

							case GKA_54IN1:

								return names[2][i];

							case GKA_103IN1:

								return names[3][i];
						}
					}
				};

				GamestarA::GamestarA(const Context& c)
				: Board(c), cartSwitches(CartSwitches::Create(c)) {}

				GamestarA::~GamestarA()
				{
					delete cartSwitches;
				}

				GamestarA::Device GamestarA::QueryDevice(DeviceType type)
				{
					if (type == DEVICE_DIP_SWITCHES && cartSwitches)
						return cartSwitches;
					else
						return Board::QueryDevice( type );
				}

				void GamestarA::SubReset(const bool hard)
				{
					Map( 0x6000U, &GamestarA::Peek_6000 );

					for (uint i=0x0000; i < 0x8000; i += 0x1000)
					{
						Map( 0x8000+i, 0x87FF+i, &GamestarA::Poke_8000 );
						Map( 0x8800+i, 0x8FFF+i, &GamestarA::Poke_8800 );
					}

					if (hard)
					{
						regs[0] = 0;
						regs[1] = 0;

						NES_DO_POKE(8800,0x8800,0x00);
					}
				}

				void GamestarA::SubLoad(State::Loader& state,const dword baseChunk)
				{
					NST_VERIFY( (baseChunk == AsciiId<'B','G','A'>::V) );

					if (baseChunk == AsciiId<'B','G','A'>::V)
					{
						while (const dword chunk = state.Begin())
						{
							if (chunk == AsciiId<'R','E','G'>::V)
							{
								State::Loader::Data<3> data( state );

								regs[0] = data[1];
								regs[1] = data[2];

								if (cartSwitches)
									cartSwitches->SetMode( data[0] );
							}

							state.End();
						}
					}
				}

				void GamestarA::SubSave(State::Saver& state) const
				{
					const byte data[3] =
					{
						cartSwitches ? cartSwitches->GetMode() : 0,
						regs[0],
						regs[1]
					};

					state.Begin( AsciiId<'B','G','A'>::V ).Begin( AsciiId<'R','E','G'>::V ).Write( data ).End().End();
				}

				#ifdef NST_MSVC_OPTIMIZE
				#pragma optimize("", on)
				#endif

				NES_PEEK(GamestarA,6000)
				{
					return cartSwitches ? cartSwitches->GetMode() : 0x00;
				}

				void GamestarA::UpdateChr() const
				{
					chr.SwapBank<SIZE_8K,0x0000>( (regs[0] >> 1 & 0x8) | (regs[0] & 0x7) | (regs[1] & 0x3) );
				}

				NES_POKE_D(GamestarA,8000)
				{
					regs[1] = data;
					ppu.Update();
					UpdateChr();
				}

				NES_POKE_D(GamestarA,8800)
				{
					regs[0] = data;
					prg.SwapBanks<SIZE_16K,0x0000>( (data >> 5) & ~(data >> 7), (data >> 5) | (data >> 7) );
					ppu.SetMirroring( (data & 0x8) ? Ppu::NMT_H : Ppu::NMT_V );
					UpdateChr();
				}
			}
		}
	}
}