Reinitialize gamepads command and fix EVDEV going bonkers on gamepad suddenly disconn...
[lsnes.git] / src / core / runmode.cpp
blobeca182471870cbc435d3e59912beada494e399f2
1 #include "core/instance.hpp"
2 #include "core/messages.hpp"
3 #include "core/movie.hpp"
4 #include "core/runmode.hpp"
5 #include <stdexcept>
7 namespace
9 const uint64_t QUIT_MAGIC = 0x87dd4df349e5eff7ULL;
12 const uint64_t emulator_runmode::QUIT = 1;
13 const uint64_t emulator_runmode::NORMAL = 2;
14 const uint64_t emulator_runmode::LOAD = 4;
15 const uint64_t emulator_runmode::ADVANCE_FRAME = 8;
16 const uint64_t emulator_runmode::ADVANCE_SUBFRAME = 16;
17 const uint64_t emulator_runmode::SKIPLAG = 32;
18 const uint64_t emulator_runmode::SKIPLAG_PENDING = 64;
19 const uint64_t emulator_runmode::PAUSE = 128;
20 const uint64_t emulator_runmode::PAUSE_BREAK = 256;
21 const uint64_t emulator_runmode::CORRUPT = 512;
23 const unsigned emulator_runmode::P_START = 0;
24 const unsigned emulator_runmode::P_VIDEO = 1;
25 const unsigned emulator_runmode::P_SAVE = 2;
26 const unsigned emulator_runmode::P_NONE = 3;
28 emulator_runmode::emulator_runmode()
30 mode = PAUSE;
31 saved_mode = PAUSE;
32 magic = 0;
33 advanced = false;
34 cancel = false;
37 uint64_t emulator_runmode::get()
39 revalidate();
40 return mode;
43 void emulator_runmode::set(uint64_t m)
45 if(!m || m & (m - 1) || m > CORRUPT)
46 throw std::logic_error("Trying to set invalid runmode");
47 if(m == QUIT) {
48 magic = QUIT_MAGIC;
49 mode = QUIT;
50 } else
51 mode = m;
52 //If setting state, clear the variables to be at initial state.
53 advanced = false;
54 cancel = false;
57 bool emulator_runmode::is(uint64_t m)
59 revalidate();
60 return ((mode & m) != 0);
63 void emulator_runmode::start_load()
65 if(mode != CORRUPT) {
66 saved_mode = mode;
67 mode = LOAD;
68 saved_advanced = advanced;
69 saved_cancel = cancel;
70 advanced = false;
71 cancel = false;
75 void emulator_runmode::end_load()
77 if(mode != CORRUPT) {
78 mode = saved_mode;
79 advanced = saved_advanced;
80 cancel = saved_cancel;
81 saved_mode = 128;
85 void emulator_runmode::decay_skiplag()
87 revalidate();
88 if(mode == SKIPLAG_PENDING) {
89 mode = SKIPLAG;
93 void emulator_runmode::decay_break()
95 revalidate();
96 if(mode == PAUSE_BREAK) {
97 mode = PAUSE;
101 void emulator_runmode::revalidate()
103 if(!mode || mode & (mode - 1) || (mode == QUIT && magic != QUIT_MAGIC) || mode > CORRUPT) {
104 //Uh, oh.
105 auto& core = CORE();
106 if(core.mlogic)
107 emerg_save_movie(core.mlogic->get_mfile(), core.mlogic->get_rrdata());
108 messages << "WARNING: Emulator runmode undefined, invoked movie dump." << std::endl;
109 mode = PAUSE;
113 bool emulator_runmode::set_and_test_advanced()
115 bool x = advanced;
116 advanced = true;
117 return x;
120 void emulator_runmode::set_cancel()
122 cancel = true;
123 if(mode == LOAD)
124 saved_cancel = true;
127 bool emulator_runmode::clear_and_test_cancel()
129 bool x = cancel;
130 cancel = false;
131 return x;
134 bool emulator_runmode::test_cancel()
136 return cancel;
139 bool emulator_runmode::test_advanced()
141 return advanced;
144 void emulator_runmode::set_point(unsigned _point)
146 point = _point;
149 unsigned emulator_runmode::get_point()
151 return point;