Bus fixes: Reading of CPU MMIO registers does not update MDR
[lsnes.git] / bsnes-patches / v084 / 0004-Save-controller-state-when-savestating.patch
blob42677a3711cfd4456a11cadd8eb4f351e10a4c06
1 From af7fdd9f73a3eb5e9266c59bfb4dd676679b2f7d Mon Sep 17 00:00:00 2001
2 From: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
3 Date: Wed, 9 Nov 2011 01:52:08 +0200
4 Subject: [PATCH 04/10] Save controller state when savestating
6 When savestating, save the controller state and restore it upon loadstate.
7 Prevents libsnes from mixing up buttons.
8 ---
9 snes/controller/controller.cpp | 8 +++++++
10 snes/controller/controller.hpp | 2 ++
11 snes/controller/gamepad/gamepad.cpp | 13 +++++++++++
12 snes/controller/gamepad/gamepad.hpp | 2 +-
13 snes/controller/justifier/justifier.cpp | 36 +++++++++++++++++++++++++++++++
14 snes/controller/justifier/justifier.hpp | 1 +
15 snes/controller/mouse/mouse.cpp | 13 +++++++++++
16 snes/controller/mouse/mouse.hpp | 2 +-
17 snes/controller/multitap/multitap.cpp | 16 ++++++++++++++
18 snes/controller/multitap/multitap.hpp | 2 +-
19 snes/controller/superscope/superscope.cpp | 31 ++++++++++++++++++++++++++
20 snes/controller/superscope/superscope.hpp | 1 +
21 snes/input/input.cpp | 15 +++++++++++++
22 snes/input/input.hpp | 1 +
23 snes/system/serialization.cpp | 1 +
24 15 files changed, 141 insertions(+), 3 deletions(-)
26 diff --git a/snes/controller/controller.cpp b/snes/controller/controller.cpp
27 index 9091b21..f254bed 100755
28 --- a/snes/controller/controller.cpp
29 +++ b/snes/controller/controller.cpp
30 @@ -46,8 +46,16 @@ void Controller::iobit(bool data) {
34 +void Controller::serialize(serializer& s) {
35 + Processor::serialize(s);
36 + //Save a zero block.
37 + unsigned char blockzeroes[SaveSize] = {0};
38 + s.array(blockzeroes, SaveSize);
41 Controller::Controller(bool port) : port(port) {
42 if(!thread) create(Controller::Enter, 1);
47 diff --git a/snes/controller/controller.hpp b/snes/controller/controller.hpp
48 index 7332712..827b2eb 100755
49 --- a/snes/controller/controller.hpp
50 +++ b/snes/controller/controller.hpp
51 @@ -13,12 +13,14 @@
53 struct Controller : Processor {
54 enum : bool { Port1 = 0, Port2 = 1 };
55 + enum { SaveSize = 16 };
56 const bool port;
58 static void Enter();
59 virtual void enter();
60 void step(unsigned clocks);
61 void synchronize_cpu();
62 + virtual void serialize(serializer& s);
64 bool iobit();
65 void iobit(bool data);
66 diff --git a/snes/controller/gamepad/gamepad.cpp b/snes/controller/gamepad/gamepad.cpp
67 index 594020d..4fa1c99 100755
68 --- a/snes/controller/gamepad/gamepad.cpp
69 +++ b/snes/controller/gamepad/gamepad.cpp
70 @@ -13,6 +13,19 @@ void Gamepad::latch(bool data) {
71 counter = 0;
74 +void Gamepad::serialize(serializer& s) {
75 + Processor::serialize(s);
76 + //Save block.
77 + unsigned char block[Controller::SaveSize] = {0};
78 + block[0] = latched ? 1 : 0;
79 + block[1] = counter;
80 + s.array(block, Controller::SaveSize);
81 + if(s.mode() == nall::serializer::Load) {
82 + latched = (block[0] != 0);
83 + counter = block[1];
84 + }
87 Gamepad::Gamepad(bool port) : Controller(port) {
88 latched = 0;
89 counter = 0;
90 diff --git a/snes/controller/gamepad/gamepad.hpp b/snes/controller/gamepad/gamepad.hpp
91 index c5ca69c..a2392d1 100755
92 --- a/snes/controller/gamepad/gamepad.hpp
93 +++ b/snes/controller/gamepad/gamepad.hpp
94 @@ -2,7 +2,7 @@ struct Gamepad : Controller {
95 uint2 data();
96 void latch(bool data);
97 Gamepad(bool port);
99 + void serialize(serializer& s);
100 private:
101 bool latched;
102 unsigned counter;
103 diff --git a/snes/controller/justifier/justifier.cpp b/snes/controller/justifier/justifier.cpp
104 index 8b2d3ee..4b8eca8 100755
105 --- a/snes/controller/justifier/justifier.cpp
106 +++ b/snes/controller/justifier/justifier.cpp
107 @@ -103,6 +103,42 @@ void Justifier::latch(bool data) {
108 if(latched == 0) active = !active; //toggle between both controllers, even when unchained
111 +void Justifier::serialize(serializer& s) {
112 + Processor::serialize(s);
113 + //Save block.
114 + unsigned char block[Controller::SaveSize] = {0};
115 + block[0] = latched ? 1 : 0;
116 + block[1] = counter;
117 + block[2] = active ? 1 : 0;
118 + block[3] = trigger1 ? 1 : 0;
119 + block[4] = trigger2 ? 1 : 0;
120 + block[5] = start1 ? 1 : 0;
121 + block[6] = start2 ? 1 : 0;
122 + block[7] = (unsigned short)x1 >> 8;
123 + block[8] = (unsigned short)x1;
124 + block[9] = (unsigned short)x2 >> 8;
125 + block[10] = (unsigned short)x2;
126 + block[11] = (unsigned short)y1 >> 8;
127 + block[12] = (unsigned short)y1;
128 + block[13] = (unsigned short)y2 >> 8;
129 + block[14] = (unsigned short)y2;
130 + s.array(block, Controller::SaveSize);
131 + if(s.mode() == nall::serializer::Load) {
132 + latched = (block[0] != 0);
133 + counter = block[1];
134 + active = (block[2] != 0);
135 + trigger1 = (block[3] != 0);
136 + trigger2 = (block[4] != 0);
137 + start1 = (block[5] != 0);
138 + start2 = (block[6] != 0);
139 + x1 = (short)(((unsigned short)block[7] << 8) | (unsigned short)block[8]);
140 + x2 = (short)(((unsigned short)block[9] << 8) | (unsigned short)block[10]);
141 + y1 = (short)(((unsigned short)block[11] << 8) | (unsigned short)block[12]);
142 + y2 = (short)(((unsigned short)block[13] << 8) | (unsigned short)block[14]);
147 Justifier::Justifier(bool port, bool chained) : Controller(port), chained(chained) {
148 create(Controller::Enter, 21477272);
149 latched = 0;
150 diff --git a/snes/controller/justifier/justifier.hpp b/snes/controller/justifier/justifier.hpp
151 index 8259147..96e09dc 100755
152 --- a/snes/controller/justifier/justifier.hpp
153 +++ b/snes/controller/justifier/justifier.hpp
154 @@ -2,6 +2,7 @@ struct Justifier : Controller {
155 void enter();
156 uint2 data();
157 void latch(bool data);
158 + void serialize(serializer& s);
159 Justifier(bool port, bool chained);
161 //private:
162 diff --git a/snes/controller/mouse/mouse.cpp b/snes/controller/mouse/mouse.cpp
163 index c9f5d16..6b26fae 100755
164 --- a/snes/controller/mouse/mouse.cpp
165 +++ b/snes/controller/mouse/mouse.cpp
166 @@ -61,6 +61,19 @@ void Mouse::latch(bool data) {
167 counter = 0;
170 +void Mouse::serialize(serializer& s) {
171 + Processor::serialize(s);
172 + //Save block.
173 + unsigned char block[Controller::SaveSize] = {0};
174 + block[0] = latched ? 1 : 0;
175 + block[1] = counter;
176 + s.array(block, Controller::SaveSize);
177 + if(s.mode() == nall::serializer::Load) {
178 + latched = (block[0] != 0);
179 + counter = block[1];
183 Mouse::Mouse(bool port) : Controller(port) {
184 latched = 0;
185 counter = 0;
186 diff --git a/snes/controller/mouse/mouse.hpp b/snes/controller/mouse/mouse.hpp
187 index 95e24b6..b66ea51 100755
188 --- a/snes/controller/mouse/mouse.hpp
189 +++ b/snes/controller/mouse/mouse.hpp
190 @@ -2,7 +2,7 @@ struct Mouse : Controller {
191 uint2 data();
192 void latch(bool data);
193 Mouse(bool port);
195 + void serialize(serializer& s);
196 private:
197 bool latched;
198 unsigned counter;
199 diff --git a/snes/controller/multitap/multitap.cpp b/snes/controller/multitap/multitap.cpp
200 index 3a6eb72..146c41d 100755
201 --- a/snes/controller/multitap/multitap.cpp
202 +++ b/snes/controller/multitap/multitap.cpp
203 @@ -30,6 +30,22 @@ void Multitap::latch(bool data) {
204 counter2 = 0;
207 +void Multitap::serialize(serializer& s) {
208 + Processor::serialize(s);
209 + //Save block.
210 + unsigned char block[Controller::SaveSize] = {0};
211 + block[0] = latched ? 1 : 0;
212 + block[1] = counter1;
213 + block[2] = counter2;
214 + s.array(block, Controller::SaveSize);
215 + if(s.mode() == nall::serializer::Load) {
216 + latched = (block[0] != 0);
217 + counter1 = block[1];
218 + counter2 = block[2];
223 Multitap::Multitap(bool port) : Controller(port) {
224 latched = 0;
225 counter1 = 0;
226 diff --git a/snes/controller/multitap/multitap.hpp b/snes/controller/multitap/multitap.hpp
227 index 0540af7..e6324ac 100755
228 --- a/snes/controller/multitap/multitap.hpp
229 +++ b/snes/controller/multitap/multitap.hpp
230 @@ -2,7 +2,7 @@ struct Multitap : Controller {
231 uint2 data();
232 void latch(bool data);
233 Multitap(bool port);
235 + void serialize(serializer& s);
236 private:
237 bool latched;
238 unsigned counter1;
239 diff --git a/snes/controller/superscope/superscope.cpp b/snes/controller/superscope/superscope.cpp
240 index e97a2ff..bb260b9 100755
241 --- a/snes/controller/superscope/superscope.cpp
242 +++ b/snes/controller/superscope/superscope.cpp
243 @@ -104,6 +104,37 @@ void SuperScope::latch(bool data) {
244 counter = 0;
247 +void SuperScope::serialize(serializer& s) {
248 + Processor::serialize(s);
249 + //Save block.
250 + unsigned char block[Controller::SaveSize] = {0};
251 + block[0] = latched ? 1 : 0;
252 + block[1] = counter;
253 + block[2] = trigger ? 1 : 0;
254 + block[3] = cursor ? 1 : 0;
255 + block[4] = turbo ? 1 : 0;
256 + block[5] = pause ? 1 : 0;
257 + block[6] = offscreen ? 1 : 0;
258 + block[7] = (unsigned short)x >> 8;
259 + block[8] = (unsigned short)x;
260 + block[9] = (unsigned short)y >> 8;
261 + block[10] = (unsigned short)y;
263 + s.array(block, Controller::SaveSize);
264 + if(s.mode() == nall::serializer::Load) {
265 + latched = (block[0] != 0);
266 + counter = block[1];
267 + trigger = (block[2] != 0);
268 + cursor = (block[3] != 0);
269 + turbo = (block[4] != 0);
270 + pause = (block[5] != 0);
271 + offscreen = (block[6] != 0);
272 + x = (short)(((unsigned short)block[7] << 8) | (unsigned short)block[8]);
273 + y = (short)(((unsigned short)block[9] << 8) | (unsigned short)block[10]);
278 SuperScope::SuperScope(bool port) : Controller(port) {
279 create(Controller::Enter, 21477272);
280 latched = 0;
281 diff --git a/snes/controller/superscope/superscope.hpp b/snes/controller/superscope/superscope.hpp
282 index a7a90b7..93509d7 100755
283 --- a/snes/controller/superscope/superscope.hpp
284 +++ b/snes/controller/superscope/superscope.hpp
285 @@ -2,6 +2,7 @@ struct SuperScope : Controller {
286 void enter();
287 uint2 data();
288 void latch(bool data);
289 + void serialize(serializer& s);
290 SuperScope(bool port);
292 //private:
293 diff --git a/snes/input/input.cpp b/snes/input/input.cpp
294 index 9050310..7030495 100755
295 --- a/snes/input/input.cpp
296 +++ b/snes/input/input.cpp
297 @@ -26,6 +26,21 @@ void Input::connect(bool port, Input::Device id) {
301 +void Input::serialize(serializer &s)
303 + int p1, p2;
304 + p1 = (int)config.controller_port1;
305 + p2 = (int)config.controller_port2;
306 + s.integer(p1);
307 + s.integer(p2);
308 + if(s.mode() == nall::serializer::Load) {
309 + connect(Controller::Port1, (Device)p1);
310 + connect(Controller::Port2, (Device)p2);
312 + port1->serialize(s);
313 + port2->serialize(s);
316 Input::Input() : port1(nullptr), port2(nullptr) {
317 connect(Controller::Port1, Input::Device::Joypad);
318 connect(Controller::Port2, Input::Device::Joypad);
319 diff --git a/snes/input/input.hpp b/snes/input/input.hpp
320 index 13ef46e..6832e82 100755
321 --- a/snes/input/input.hpp
322 +++ b/snes/input/input.hpp
323 @@ -31,6 +31,7 @@ struct Input {
324 Controller *port1;
325 Controller *port2;
327 + void serialize(serializer &s);
328 void connect(bool port, Input::Device id);
329 Input();
330 ~Input();
331 diff --git a/snes/system/serialization.cpp b/snes/system/serialization.cpp
332 index f7d6f3b..08e7051 100755
333 --- a/snes/system/serialization.cpp
334 +++ b/snes/system/serialization.cpp
335 @@ -56,6 +56,7 @@ void System::serialize_all(serializer &s) {
336 smp.serialize(s);
337 ppu.serialize(s);
338 dsp.serialize(s);
339 + input.serialize(s);
341 if(cartridge.mode() == Cartridge::Mode::SufamiTurbo) sufamiturbo.serialize(s);
342 if(cartridge.mode() == Cartridge::Mode::SuperGameBoy) icd2.serialize(s);
344 1.8.4.4