lsnes rr2-β24
[lsnes.git] / src / core / runmode.cpp
blob11de5484869c7118298f5dc78184b996ca311b26
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 point = 0;
33 magic = 0;
34 advanced = false;
35 cancel = false;
38 uint64_t emulator_runmode::get()
40 revalidate();
41 return mode;
44 void emulator_runmode::set(uint64_t m)
46 if(!m || m & (m - 1) || m > CORRUPT)
47 throw std::logic_error("Trying to set invalid runmode");
48 if(m == QUIT) {
49 magic = QUIT_MAGIC;
50 mode = QUIT;
51 } else
52 mode = m;
53 //If setting state, clear the variables to be at initial state.
54 advanced = false;
55 cancel = false;
58 bool emulator_runmode::is(uint64_t m)
60 revalidate();
61 return ((mode & m) != 0);
64 void emulator_runmode::start_load()
66 if(mode != CORRUPT) {
67 saved_mode = mode;
68 mode = LOAD;
69 saved_advanced = advanced;
70 saved_cancel = cancel;
71 advanced = false;
72 cancel = false;
76 void emulator_runmode::end_load()
78 if(mode != CORRUPT) {
79 mode = saved_mode;
80 advanced = saved_advanced;
81 cancel = saved_cancel;
82 saved_mode = 128;
86 void emulator_runmode::decay_skiplag()
88 revalidate();
89 if(mode == SKIPLAG_PENDING) {
90 mode = SKIPLAG;
94 void emulator_runmode::decay_break()
96 revalidate();
97 if(mode == PAUSE_BREAK) {
98 mode = PAUSE;
102 void emulator_runmode::revalidate()
104 if(!mode || mode & (mode - 1) || (mode == QUIT && magic != QUIT_MAGIC) || mode > CORRUPT) {
105 //Uh, oh.
106 auto& core = CORE();
107 if(core.mlogic)
108 emerg_save_movie(core.mlogic->get_mfile(), core.mlogic->get_rrdata());
109 messages << "WARNING: Emulator runmode undefined, invoked movie dump." << std::endl;
110 mode = PAUSE;
114 bool emulator_runmode::set_and_test_advanced()
116 bool x = advanced;
117 advanced = true;
118 return x;
121 void emulator_runmode::set_cancel()
123 cancel = true;
124 if(mode == LOAD)
125 saved_cancel = true;
128 bool emulator_runmode::clear_and_test_cancel()
130 bool x = cancel;
131 cancel = false;
132 return x;
135 bool emulator_runmode::test_cancel()
137 return cancel;
140 bool emulator_runmode::test_advanced()
142 return advanced;
145 void emulator_runmode::set_point(unsigned _point)
147 point = _point;
150 unsigned emulator_runmode::get_point()
152 return point;