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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
////////////////////////////////////////////////////////////////////////////////////////
//
// 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 <cstdlib>
#include <cstring>
#include "NstLog.hpp"
#include "NstChecksum.hpp"
#include "NstImageDatabase.hpp"
#include "board/NstBoard.hpp"
#include "NstCartridge.hpp"
#include "NstCartridgeRomset.hpp"
#include "NstCartridgeInes.hpp"
#include "NstCartridgeUnif.hpp"
#include "vssystem/NstVsSystem.hpp"
#include "api/NstApiUser.hpp"
namespace Nes
{
namespace Core
{
#ifdef NST_MSVC_OPTIMIZE
#pragma optimize("s", on)
#endif
Cartridge::ProfileEx::ProfileEx()
: nmt(NMT_DEFAULT), battery(false), wramAuto(false) {}
Cartridge::Cartridge(Context& context)
: Image(CARTRIDGE), board(NULL), vs(NULL), favoredSystem(context.favoredSystem)
{
try
{
ProfileEx profileEx;
switch (Stream::In(&context.stream).Peek32())
{
case INES_ID:
Ines::Load
(
context.stream,
context.patch,
context.patchBypassChecksum,
context.patchResult,
prg,
chr,
context.favoredSystem,
profile,
profileEx,
context.database
);
break;
case UNIF_ID:
Unif::Load
(
context.stream,
context.patch,
context.patchBypassChecksum,
context.patchResult,
prg,
chr,
context.favoredSystem,
profile,
profileEx,
context.database
);
break;
default:
Romset::Load
(
context.stream,
context.patch,
context.patchBypassChecksum,
context.patchResult,
prg,
chr,
context.favoredSystem,
context.askProfile,
profile
);
break;
}
if (profile.dump.state == Profile::Dump::BAD)
context.result = RESULT_WARN_BAD_DUMP;
else
context.result = RESULT_OK;
const Result result = SetupBoard( prg, chr, &board, &context, profile, profileEx, &prgCrc );
if (NES_FAILED(result))
throw result;
board->Load( savefile );
if ((profile.system.type) == Profile::System::VS_UNISYSTEM)
{
vs = VsSystem::Create
(
context.cpu,
context.ppu,
static_cast<PpuModel>(profile.system.ppu),
prgCrc
);
profile.system.ppu = static_cast<Profile::System::Ppu>(vs->GetPpuModel());
}
else if ((profile.system.type) == Profile::System::VS_DUALSYSTEM)
{
throw RESULT_ERR_UNSUPPORTED_VSSYSTEM;
}
if (Cartridge::QueryExternalDevice( EXT_DIP_SWITCHES ))
Log::Flush( "Cartridge: DIP Switches present" NST_LINEBREAK );
}
catch (...)
{
Destroy();
throw;
}
}
void Cartridge::Destroy()
{
VsSystem::Destroy( vs );
Boards::Board::Destroy( board );
}
Cartridge::~Cartridge()
{
Destroy();
}
void Cartridge::ReadRomset(std::istream& stream,FavoredSystem favoredSystem,bool askSystem,Profile& profile)
{
Log::Suppressor logSupressor;
Ram prg, chr;
ProfileEx profileEx;
Romset::Load( stream, NULL, false, NULL, prg, chr, favoredSystem, askSystem, profile, true );
SetupBoard( prg, chr, NULL, NULL, profile, profileEx, NULL, true );
}
void Cartridge::ReadInes(std::istream& stream,FavoredSystem favoredSystem,Profile& profile)
{
Log::Suppressor logSupressor;
Ram prg, chr;
ProfileEx profileEx;
Ines::Load( stream, NULL, false, NULL, prg, chr, favoredSystem, profile, profileEx, NULL );
SetupBoard( prg, chr, NULL, NULL, profile, profileEx, NULL );
}
void Cartridge::ReadUnif(std::istream& stream,FavoredSystem favoredSystem,Profile& profile)
{
Log::Suppressor logSupressor;
Ram prg, chr;
ProfileEx profileEx;
Unif::Load( stream, NULL, false, NULL, prg, chr, favoredSystem, profile, profileEx, NULL );
SetupBoard( prg, chr, NULL, NULL, profile, profileEx, NULL );
}
uint Cartridge::GetDesiredController(uint port) const
{
NST_ASSERT( port < Api::Input::NUM_CONTROLLERS );
return profile.game.controllers[port];
}
uint Cartridge::GetDesiredAdapter() const
{
return profile.game.adapter;
}
Cartridge::ExternalDevice Cartridge::QueryExternalDevice(ExternalDeviceType deviceType)
{
switch (deviceType)
{
case EXT_DIP_SWITCHES:
if (vs)
return &vs->GetDipSwiches();
else
return board->QueryDevice( Boards::Board::DEVICE_DIP_SWITCHES );
case EXT_BARCODE_READER:
return board->QueryDevice( Boards::Board::DEVICE_BARCODE_READER );
default:
return Image::QueryExternalDevice( deviceType );
}
}
Result Cartridge::SetupBoard
(
Ram& prg,
Ram& chr,
Boards::Board** board,
const Context* const context,
Profile& profile,
const ProfileEx& profileEx,
dword* const prgCrc,
const bool readOnly
)
{
NST_ASSERT( bool(board) == bool(context) );
Boards::Board::Type::Nmt nmt;
if (profile.board.solderPads & (Profile::Board::SOLDERPAD_H|Profile::Board::SOLDERPAD_V))
{
if (profile.board.solderPads & Profile::Board::SOLDERPAD_H)
nmt = Boards::Board::Type::NMT_VERTICAL;
else
nmt = Boards::Board::Type::NMT_HORIZONTAL;
}
else switch (profileEx.nmt)
{
case ProfileEx::NMT_HORIZONTAL: nmt = Boards::Board::Type::NMT_HORIZONTAL; break;
case ProfileEx::NMT_VERTICAL: nmt = Boards::Board::Type::NMT_VERTICAL; break;
case ProfileEx::NMT_SINGLESCREEN: nmt = Boards::Board::Type::NMT_SINGLESCREEN; break;
case ProfileEx::NMT_FOURSCREEN: nmt = Boards::Board::Type::NMT_FOURSCREEN; break;
default: nmt = Boards::Board::Type::NMT_CONTROLLED; break;
}
Chips chips;
for (Profile::Board::Chips::const_iterator i(profile.board.chips.begin()), end(profile.board.chips.end()); i != end; ++i)
{
Chips::Type& type = chips.Add( i->type.c_str() );
for (Profile::Board::Pins::const_iterator j(i->pins.begin()), end(i->pins.end()); j != end; ++j)
type.Pin(j->number) = j->function.c_str();
for (Profile::Board::Samples::const_iterator j(i->samples.begin()), end(i->samples.end()); j != end; ++j)
type.Sample(j->id) = j->file.c_str();
}
Boards::Board::Context b
(
context ? &context->cpu : NULL,
context ? &context->apu : NULL,
context ? &context->ppu : NULL,
prg,
chr,
profileEx.trainer,
nmt,
profileEx.battery || profile.board.HasWramBattery(),
profile.board.HasMmcBattery(),
chips
);
if (profile.board.type.empty() || !b.DetectBoard( profile.board.type.c_str(), profile.board.GetWram() ))
{
if (profile.board.mapper == Profile::Board::NO_MAPPER || (!b.DetectBoard( profile.board.mapper, profile.board.subMapper, profile.board.chrRam, profile.board.GetWram(), profileEx.wramAuto ) && board))
return RESULT_ERR_UNSUPPORTED_MAPPER;
if (profile.board.type.empty())
profile.board.type = std::wstring( b.name, b.name + std::strlen(b.name) );
}
if (profile.board.mapper == Profile::Board::NO_MAPPER && b.type.GetMapper() != Boards::Board::Type::NMPR)
profile.board.mapper = b.type.GetMapper();
for (uint i=0; i < 2; ++i)
{
dword size = (i ? b.chr : b.prg).Size();
if (size != (i ? profile.board.GetChr() : profile.board.GetPrg()))
{
Profile::Board::Roms& roms = (i ? profile.board.chr : profile.board.prg);
roms.clear();
if (size)
{
Profile::Board::Rom rom;
rom.size = size;
roms.push_back( rom );
}
}
size = (i ? b.type.GetVram() : b.type.GetWram());
if (size != (i ? profile.board.GetVram() : profile.board.GetWram()))
{
Profile::Board::Rams& rams = (i ? profile.board.vram : profile.board.wram);
rams.clear();
for (uint j=0; j < 2; ++j)
{
size = i ? (j ? b.type.GetNonSavableVram() : b.type.GetSavableVram()) :
(j ? b.type.GetNonSavableWram() : b.type.GetSavableWram());
if (size)
{
Profile::Board::Ram ram;
ram.size = size;
ram.battery = (i == 0 && j == 0 && b.wramBattery);
rams.push_back( ram );
}
}
}
Profile::Board::Roms& roms = (i ? profile.board.chr : profile.board.prg);
for (dword j=0, k=0, n=roms.size(); j < n; k += roms[j].size, ++j)
roms[j].hash.Compute( (i ? chr : prg).Mem(k), roms[j].size );
}
if (!readOnly)
{
Checksum checksum;
checksum.Compute( prg.Mem(), prg.Size() );
if (prgCrc)
*prgCrc = checksum.GetCrc();
checksum.Compute( chr.Mem(), chr.Size() );
profile.hash.Assign( checksum.GetSha1(), checksum.GetCrc() );
}
if (board)
*board = Boards::Board::Create( b );
return RESULT_OK;
}
void Cartridge::Reset(const bool hard)
{
board->Reset( hard );
if (vs)
vs->Reset( hard );
}
bool Cartridge::PowerOff()
{
try
{
if (board)
{
board->Sync( Boards::Board::EVENT_POWER_OFF, NULL );
board->Save( savefile );
}
return true;
}
catch (...)
{
return false;
}
}
void Cartridge::SaveState(State::Saver& state,const dword baseChunk) const
{
state.Begin( baseChunk );
board->SaveState( state, AsciiId<'M','P','R'>::V );
if (vs)
vs->SaveState( state, AsciiId<'V','S','S'>::V );
state.End();
}
void Cartridge::LoadState(State::Loader& state)
{
while (const dword chunk = state.Begin())
{
switch (chunk)
{
case AsciiId<'M','P','R'>::V:
board->LoadState( state );
break;
case AsciiId<'V','S','S'>::V:
NST_VERIFY( vs );
if (vs)
vs->LoadState( state );
break;
}
state.End();
}
}
Region Cartridge::GetDesiredRegion() const
{
switch (profile.system.type)
{
case Profile::System::NES_PAL:
case Profile::System::NES_PAL_A:
case Profile::System::NES_PAL_B:
case Profile::System::DENDY:
return REGION_PAL;
case Profile::System::NES_NTSC:
case Profile::System::FAMICOM:
if (favoredSystem == FAVORED_DENDY)
return REGION_PAL;
default:
return REGION_NTSC;
}
}
System Cartridge::GetDesiredSystem(Region region,CpuModel* cpu,PpuModel* ppu) const
{
if (region == Cartridge::GetDesiredRegion())
{
if (favoredSystem == FAVORED_DENDY && region == REGION_PAL)
{
switch (profile.system.type)
{
case Profile::System::NES_NTSC:
case Profile::System::NES_PAL:
case Profile::System::NES_PAL_A:
case Profile::System::NES_PAL_B:
case Profile::System::FAMICOM:
case Profile::System::DENDY:
if (cpu)
*cpu = CPU_DENDY;
if (ppu)
*ppu = PPU_DENDY;
return SYSTEM_DENDY;
case Profile::System::VS_UNISYSTEM:
case Profile::System::VS_DUALSYSTEM:
case Profile::System::PLAYCHOICE_10:
default:
break;
}
}
if (cpu)
*cpu = static_cast<CpuModel>(profile.system.cpu);
if (ppu)
*ppu = static_cast<PpuModel>(profile.system.ppu);
return static_cast<System>(profile.system.type);
}
else
{
return Image::GetDesiredSystem( region, cpu, ppu );
}
}
#ifdef NST_MSVC_OPTIMIZE
#pragma optimize("", on)
#endif
void Cartridge::BeginFrame(const Api::Input& input,Input::Controllers* controllers)
{
board->Sync( Boards::Board::EVENT_BEGIN_FRAME, controllers );
if (vs)
vs->BeginFrame( input, controllers );
}
void Cartridge::VSync()
{
board->Sync( Boards::Board::EVENT_END_FRAME, NULL );
if (vs)
vs->VSync();
}
}
}
|