1 ////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // Nestopia - NES/Famicom emulator written in C++
4 //
5 // Copyright (C) 2003-2008 Martin Freij
6 //
7 // This file is part of Nestopia.
8 //
9 // Nestopia is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Nestopia is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Nestopia; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 //
23 ////////////////////////////////////////////////////////////////////////////////////////
24 
25 #include "NstStream.hpp"
26 #include "NstCartridge.hpp"
27 #include "NstFds.hpp"
28 #include "NstNsf.hpp"
29 
30 namespace Nes
31 {
32 	namespace Core
33 	{
34 		#ifdef NST_MSVC_OPTIMIZE
35 		#pragma optimize("s", on)
36 		#endif
37 
Image(Type t)38 		Image::Image(Type t)
39 		: type(t) {}
40 
Load(Context & context)41 		Image* Image::Load(Context& context)
42 		{
43 			switch (Stream::In(&context.stream).Peek32())
44 			{
45 				case INES_ID:
46 				case UNIF_ID:
47 				default:
48 
49 					if (context.type == CARTRIDGE || context.type == UNKNOWN)
50 						return new Cartridge (context);
51 
52 					break;
53 
54 				case FDS_ID:
55 				case FDS_RAW_ID:
56 
57 					if (context.type == DISK || context.type == UNKNOWN)
58 						return new Fds (context);
59 
60 					break;
61 
62 				case NSF_ID:
63 
64 					if (context.type == SOUND || context.type == UNKNOWN)
65 						return new Nsf (context);
66 
67 					break;
68 			}
69 
70 			throw RESULT_ERR_INVALID_FILE;
71 		}
72 
Unload(Image * image)73 		void Image::Unload(Image* image)
74 		{
75 			delete image;
76 		}
77 
GetDesiredController(uint port) const78 		uint Image::GetDesiredController(uint port) const
79 		{
80 			switch (port)
81 			{
82 				case Api::Input::PORT_1: return Api::Input::PAD1;
83 				case Api::Input::PORT_2: return Api::Input::PAD2;
84 				default: return Api::Input::UNCONNECTED;
85 			}
86 		}
87 
GetDesiredAdapter() const88 		uint Image::GetDesiredAdapter() const
89 		{
90 			return Api::Input::ADAPTER_NES;
91 		}
92 
GetDesiredSystem(Region region,CpuModel * cpu,PpuModel * ppu) const93 		System Image::GetDesiredSystem(Region region,CpuModel* cpu,PpuModel* ppu) const
94 		{
95 			if (region == REGION_NTSC)
96 			{
97 				if (cpu)
98 					*cpu = CPU_RP2A03;
99 
100 				if (ppu)
101 					*ppu = PPU_RP2C02;
102 
103 				return SYSTEM_NES_NTSC;
104 			}
105 			else
106 			{
107 				if (cpu)
108 					*cpu = CPU_RP2A07;
109 
110 				if (ppu)
111 					*ppu = PPU_RP2C07;
112 
113 				return SYSTEM_NES_PAL;
114 			}
115 		}
116 
117 		#ifdef NST_MSVC_OPTIMIZE
118 		#pragma optimize("", on)
119 		#endif
120 	}
121 }
122