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 "NstBoard.hpp"
26 #include "../NstDipSwitches.hpp"
27 #include "../NstLog.hpp"
28 #include "NstBoardFb.hpp"
29 
30 namespace Nes
31 {
32 	namespace Core
33 	{
34 		namespace Boards
35 		{
36 			#ifdef NST_MSVC_OPTIMIZE
37 			#pragma optimize("s", on)
38 			#endif
39 
Fb(const Context & c)40 			Fb::Fb(const Context& c)
41 			: Board(c), cartSwitch(wrk) {}
42 
SubReset(const bool hard)43 			void Fb::SubReset(const bool hard)
44 			{
45 				cartSwitch.Reset( hard );
46 
47 				switch (board.GetWram())
48 				{
49 					case SIZE_8K:
50 
51 						Map( 0x6000U, 0x7FFFU, &Fb::Peek_Wrk_6, &Fb::Poke_Wrk_6 );
52 						break;
53 
54 					case SIZE_4K:
55 
56 						Map( 0x6000U, 0x7000U, &Fb::Peek_Wrk_6, &Fb::Poke_Wrk_6 );
57 						break;
58 
59 					case SIZE_2K:
60 
61 						Map( 0x7000U, 0x7800U, &Fb::Peek_Wrk_7, &Fb::Poke_Wrk_7 );
62 						break;
63 				}
64 			}
65 
Sync(Event event,Input::Controllers * controllers)66 			void Fb::Sync(Event event,Input::Controllers* controllers)
67 			{
68 				if (event == EVENT_POWER_OFF)
69 					cartSwitch.Flush();
70 
71 				Board::Sync( event, controllers );
72 			}
73 
QueryDevice(DeviceType type)74 			Fb::Device Fb::QueryDevice(DeviceType type)
75 			{
76 				if (type == DEVICE_DIP_SWITCHES)
77 					return &cartSwitch;
78 				else
79 					return Board::QueryDevice( type );
80 			}
81 
CartSwitch(Wrk & w)82 			Fb::CartSwitch::CartSwitch(Wrk& w)
83 			: wrk(w), init(true) {}
84 
Reset(bool hard)85 			void Fb::CartSwitch::Reset(bool hard)
86 			{
87 				if (init)
88 				{
89 					init = false;
90 				}
91 				else if (hard)
92 				{
93 					Flush();
94 				}
95 			}
96 
Flush() const97 			void Fb::CartSwitch::Flush() const
98 			{
99 				if (wrk.Source().Writable())
100 				{
101 					wrk.Source().Fill( 0x00 );
102 					Log::Flush( "Fb: battery-switch OFF, discarding W-RAM.." NST_LINEBREAK );
103 				}
104 			}
105 
NumDips() const106 			uint Fb::CartSwitch::NumDips() const
107 			{
108 				return 1;
109 			}
110 
NumValues(uint) const111 			uint Fb::CartSwitch::NumValues(uint) const
112 			{
113 				return 2;
114 			}
115 
GetDipName(uint) const116 			cstring Fb::CartSwitch::GetDipName(uint) const
117 			{
118 				return "Backup Switch";
119 			}
120 
GetValueName(uint,uint value) const121 			cstring Fb::CartSwitch::GetValueName(uint,uint value) const
122 			{
123 				NST_ASSERT( value < 2 );
124 				return value == 0 ? "Off" : "On";
125 			}
126 
GetValue(uint) const127 			uint Fb::CartSwitch::GetValue(uint) const
128 			{
129 				return wrk.Source().Writable() ? 0 : 1;
130 			}
131 
SetValue(uint,uint value)132 			void Fb::CartSwitch::SetValue(uint,uint value)
133 			{
134 				NST_ASSERT( value < 2 );
135 				wrk.Source().SetSecurity( true, !value );
136 			}
137 
138 			#ifdef NST_MSVC_OPTIMIZE
139 			#pragma optimize("", on)
140 			#endif
141 
NES_POKE_AD(Fb,Wrk_6)142 			NES_POKE_AD(Fb,Wrk_6)
143 			{
144 				NST_VERIFY( wrk.Writable(0) );
145 
146 				if (wrk.Writable(0))
147 					wrk[0][address - 0x6000] = data;
148 			}
149 
NES_PEEK_A(Fb,Wrk_6)150 			NES_PEEK_A(Fb,Wrk_6)
151 			{
152 				return wrk[0][address - 0x6000];
153 			}
154 
NES_POKE_AD(Fb,Wrk_7)155 			NES_POKE_AD(Fb,Wrk_7)
156 			{
157 				NST_VERIFY( wrk.Writable(0) );
158 
159 				if (wrk.Writable(0))
160 					wrk[0][address - 0x7000] = data;
161 			}
162 
NES_PEEK_A(Fb,Wrk_7)163 			NES_PEEK_A(Fb,Wrk_7)
164 			{
165 				return wrk[0][address - 0x7000];
166 			}
167 		}
168 	}
169 }
170