movb -> lsnes_instance.mlogic
[lsnes.git] / src / core / mainloop.cpp
blob4fd57c9554d5252bec9aba6a8cd719366c9f74a3
1 #include "lsnes.hpp"
3 #include "core/command.hpp"
4 #include "core/controller.hpp"
5 #include "core/command.hpp"
6 #include "core/debug.hpp"
7 #include "core/dispatch.hpp"
8 #include "core/emustatus.hpp"
9 #include "core/framebuffer.hpp"
10 #include "core/framerate.hpp"
11 #include "core/inthread.hpp"
12 #include "core/keymapper.hpp"
13 #include "core/multitrack.hpp"
14 #include "lua/lua.hpp"
15 #include "library/string.hpp"
16 #include "core/mainloop.hpp"
17 #include "core/instance.hpp"
18 #include "core/moviedata.hpp"
19 #include "core/moviefile.hpp"
20 #include "core/memorymanip.hpp"
21 #include "core/memorywatch.hpp"
22 #include "core/project.hpp"
23 #include "core/rom.hpp"
24 #include "core/romloader.hpp"
25 #include "core/settings.hpp"
26 #include "core/window.hpp"
27 #include "interface/c-interface.hpp"
28 #include "interface/callbacks.hpp"
29 #include "interface/romtype.hpp"
30 #include "library/framebuffer.hpp"
31 #include "library/zip.hpp"
33 #include <iomanip>
34 #include <cassert>
35 #include <sstream>
36 #include <iostream>
37 #include <limits>
38 #include <set>
39 #include <sys/time.h>
41 #define SPECIAL_FRAME_START 0
42 #define SPECIAL_FRAME_VIDEO 1
43 #define SPECIAL_SAVEPOINT 2
44 #define SPECIAL_NONE 3
46 void update_movie_state();
47 time_t random_seed_value = 0;
49 settingvar::variable<settingvar::model_bool<settingvar::yes_no>> jukebox_dflt_binary(lsnes_vset,
50 "jukebox-default-binary", "Movie‣Saving‣Saveslots binary", true);
51 settingvar::variable<settingvar::model_bool<settingvar::yes_no>> movie_dflt_binary(lsnes_vset, "movie-default-binary",
52 "Movie‣Saving‣Movies binary", false);
53 settingvar::variable<settingvar::model_bool<settingvar::yes_no>> save_dflt_binary(lsnes_vset,
54 "savestate-default-binary", "Movie‣Saving‣Savestates binary", false);
56 namespace
58 settingvar::variable<settingvar::model_int<0,999999>> advance_timeout_first(lsnes_vset, "advance-timeout",
59 "Delays‣First frame advance", 500);
60 settingvar::variable<settingvar::model_int<0,999999>> advance_timeout_subframe(lsnes_vset,
61 "advance-subframe-timeout", "Delays‣Subframe advance", 100);
62 settingvar::variable<settingvar::model_bool<settingvar::yes_no>> pause_on_end(lsnes_vset, "pause-on-end",
63 "Movie‣Pause on end", false);
64 settingvar::variable<settingvar::model_int<0,999999999>> jukebox_size(lsnes_vset, "jukebox-size",
65 "Movie‣Number of save slots", 12);
67 enum advance_mode
69 ADVANCE_QUIT, //Quit the emulator.
70 ADVANCE_AUTO, //Normal (possibly slowed down play).
71 ADVANCE_LOAD, //Loading a state.
72 ADVANCE_FRAME, //Frame advance.
73 ADVANCE_SUBFRAME, //Subframe advance.
74 ADVANCE_SKIPLAG, //Skip lag (oneshot, reverts to normal).
75 ADVANCE_SKIPLAG_PENDING, //Activate skip lag mode at next frame.
76 ADVANCE_PAUSE, //Unconditional pause.
79 //Our thread.
80 threads::id emulation_thread;
81 //Flags related to repeating advance.
82 bool advanced_once;
83 bool cancel_advance;
84 //Emulator advance mode. Detemines pauses at start of frame / subframe, etc..
85 enum advance_mode amode;
86 enum advance_mode old_mode;
87 //Mode and filename of pending load, one of LOAD_* constants.
88 bool load_paused;
89 int loadmode;
90 std::string pending_load;
91 std::string pending_new_project;
92 //Queued saves (all savestates).
93 std::set<std::pair<std::string, int>> queued_saves;
94 //Save jukebox.
95 size_t save_jukebox_pointer;
96 //Special subframe location. One of SPECIAL_* constants.
97 int location_special;
98 //Unsafe rewind.
99 bool do_unsafe_rewind = false;
100 void* unsafe_rewind_obj = NULL;
101 //Stop at frame.
102 bool stop_at_frame_active = false;
103 uint64_t stop_at_frame = 0;
104 //Macro hold.
105 bool macro_hold_1;
106 bool macro_hold_2;
107 //In break pause.
108 bool break_pause = false;
111 std::string save_jukebox_name(size_t i)
113 return (stringfmt() << "$SLOT:" << (i + 1)).str();
116 std::map<std::string, std::string> slotinfo_cache;
118 std::string vector_to_string(const std::vector<char>& x)
120 std::string y(x.begin(), x.end());
121 while(y.length() > 0 && y[y.length() - 1] < 32)
122 y = y.substr(0, y.length() - 1);
123 return y;
126 std::string get_slotinfo(const std::string& _filename)
128 std::string filename = resolve_relative_path(_filename);
129 if(!slotinfo_cache.count(filename)) {
130 std::ostringstream out;
131 try {
132 moviefile::brief_info info(filename);
133 if(!lsnes_instance.mlogic)
134 out << "No movie";
135 else if(lsnes_instance.mlogic.get_mfile().projectid == info.projectid)
136 out << info.rerecords << "R/" << info.current_frame << "F";
137 else
138 out << "Wrong movie";
139 } catch(...) {
140 out << "Nonexistent";
142 slotinfo_cache[filename] = out.str();
144 return slotinfo_cache[filename];
147 void flush_slotinfo(const std::string& filename)
149 slotinfo_cache.erase(resolve_relative_path(filename));
152 void flush_slotinfo()
154 slotinfo_cache.clear();
158 void mainloop_signal_need_rewind(void* ptr)
160 if(ptr) {
161 old_mode = amode;
162 amode = ADVANCE_LOAD;
164 do_unsafe_rewind = true;
165 unsafe_rewind_obj = ptr;
168 controller_frame movie_logic::update_controls(bool subframe) throw(std::bad_alloc, std::runtime_error)
170 if(lua_requests_subframe_paint)
171 redraw_framebuffer();
173 if(subframe) {
174 if(amode == ADVANCE_SUBFRAME) {
175 if(!cancel_advance) {
176 if(!advanced_once)
177 platform::wait(advance_timeout_first * 1000);
178 else
179 platform::wait(advance_timeout_subframe * 1000);
180 advanced_once = true;
182 if(cancel_advance) {
183 stop_at_frame_active = false;
184 amode = ADVANCE_PAUSE;
185 cancel_advance = false;
187 platform::set_paused(amode == ADVANCE_PAUSE);
188 } else if(amode == ADVANCE_FRAME) {
190 } else {
191 if(amode == ADVANCE_SKIPLAG) {
192 stop_at_frame_active = false;
193 amode = ADVANCE_PAUSE;
195 platform::set_paused(amode == ADVANCE_PAUSE);
196 cancel_advance = false;
198 location_special = SPECIAL_NONE;
199 update_movie_state();
200 } else {
201 if(amode == ADVANCE_SKIPLAG_PENDING)
202 amode = ADVANCE_SKIPLAG;
203 if(amode == ADVANCE_FRAME || amode == ADVANCE_SUBFRAME) {
204 if(!cancel_advance) {
205 uint64_t wait = 0;
206 if(!advanced_once)
207 wait = advance_timeout_first * 1000;
208 else if(amode == ADVANCE_SUBFRAME)
209 wait = advance_timeout_subframe * 1000;
210 else
211 wait = to_wait_frame(get_utime());
212 platform::wait(wait);
213 advanced_once = true;
215 if(cancel_advance) {
216 stop_at_frame_active = false;
217 amode = ADVANCE_PAUSE;
218 cancel_advance = false;
220 platform::set_paused(amode == ADVANCE_PAUSE);
221 } else if(amode == ADVANCE_AUTO && lsnes_instance.mlogic.get_movie().readonly_mode() &&
222 pause_on_end && !stop_at_frame_active) {
223 if(lsnes_instance.mlogic.get_movie().get_current_frame() ==
224 lsnes_instance.mlogic.get_movie().get_frame_count()) {
225 stop_at_frame_active = false;
226 amode = ADVANCE_PAUSE;
227 platform::set_paused(true);
229 } else if(amode == ADVANCE_AUTO && stop_at_frame_active) {
230 if(lsnes_instance.mlogic.get_movie().get_current_frame() >= stop_at_frame) {
231 stop_at_frame_active = false;
232 amode = ADVANCE_PAUSE;
233 platform::set_paused(true);
235 } else {
236 platform::set_paused((amode == ADVANCE_PAUSE));
237 cancel_advance = false;
239 location_special = SPECIAL_FRAME_START;
240 update_movie_state();
242 platform::flush_command_queue();
243 controller_frame tmp = controls.get(lsnes_instance.mlogic.get_movie().get_current_frame());
244 our_rom.rtype->pre_emulate_frame(tmp); //Preset controls, the lua will override if needed.
245 lua_callback_do_input(tmp, subframe);
246 multitrack_editor.process_frame(tmp);
247 controls.commit(tmp);
248 return tmp;
251 namespace
254 //Do pending load (automatically unpauses).
255 void mark_pending_load(std::string filename, int lmode)
257 if(break_pause)
258 break_pause = false;
259 loadmode = lmode;
260 pending_load = filename;
261 old_mode = amode;
262 amode = ADVANCE_LOAD;
263 platform::cancel_wait();
264 platform::set_paused(false);
267 void mark_pending_save(std::string filename, int smode, int binary)
269 int tmp = -1;
270 if(smode == SAVE_MOVIE) {
271 //Just do this immediately.
272 do_save_movie(filename, binary);
273 flush_slotinfo(translate_name_mprefix(filename, tmp, -1));
274 return;
276 if(location_special == SPECIAL_SAVEPOINT) {
277 //We can save immediately here.
278 do_save_state(filename, binary);
279 flush_slotinfo(translate_name_mprefix(filename, tmp, -1));
280 return;
282 queued_saves.insert(std::make_pair(filename, binary));
283 messages << "Pending save on '" << filename << "'" << std::endl;
286 struct jukebox_size_listener : public settingvar::listener
288 jukebox_size_listener() { lsnes_vset.add_listener(*this); }
289 ~jukebox_size_listener() throw() {lsnes_vset.remove_listener(*this); };
290 void on_setting_change(settingvar::group& grp, const settingvar::base& val)
292 if(val.get_iname() == "jukebox-size") {
293 if(save_jukebox_pointer >= (size_t)jukebox_size)
294 save_jukebox_pointer = 0;
296 update_movie_state();
301 void update_movie_state()
303 auto p = project_get();
304 bool readonly = false;
306 uint64_t magic[4];
307 our_rom.region->fill_framerate_magic(magic);
308 if(lsnes_instance.mlogic)
309 voice_frame_number(lsnes_instance.mlogic.get_movie().get_current_frame(),
310 1.0 * magic[1] / magic[0]);
311 else
312 voice_frame_number(0, 60.0); //Default.
314 auto& _status = lsnes_status.get_write();
315 try {
316 if(lsnes_instance.mlogic && !system_corrupt) {
317 _status.movie_valid = true;
318 _status.curframe = lsnes_instance.mlogic.get_movie().get_current_frame();
319 _status.length = lsnes_instance.mlogic.get_movie().get_frame_count();
320 _status.lag = lsnes_instance.mlogic.get_movie().get_lag_frames();
321 if(location_special == SPECIAL_FRAME_START)
322 _status.subframe = 0;
323 else if(location_special == SPECIAL_SAVEPOINT)
324 _status.subframe = _lsnes_status::subframe_savepoint;
325 else if(location_special == SPECIAL_FRAME_VIDEO)
326 _status.subframe = _lsnes_status::subframe_video;
327 else
328 _status.subframe = lsnes_instance.mlogic.get_movie().next_poll_number();
329 } else {
330 _status.movie_valid = false;
331 _status.curframe = 0;
332 _status.length = 0;
333 _status.lag = 0;
334 _status.subframe = 0;
336 _status.dumping = (information_dispatch::get_dumper_count() > 0);
337 if(break_pause)
338 _status.pause = _lsnes_status::pause_break;
339 else if(amode == ADVANCE_PAUSE)
340 _status.pause = _lsnes_status::pause_normal;
341 else
342 _status.pause = _lsnes_status::pause_none;
343 if(lsnes_instance.mlogic) {
344 auto& mo = lsnes_instance.mlogic.get_movie();
345 readonly = mo.readonly_mode();
346 if(system_corrupt)
347 _status.mode = 'C';
348 else if(!readonly)
349 _status.mode = 'R';
350 else if(mo.get_frame_count() >= mo.get_current_frame())
351 _status.mode = 'P';
352 else
353 _status.mode = 'F';
355 if(jukebox_size > 0) {
356 _status.saveslot_valid = true;
357 int tmp = -1;
358 std::string sfilen = translate_name_mprefix(save_jukebox_name(save_jukebox_pointer), tmp, -1);
359 _status.saveslot = save_jukebox_pointer + 1;
360 _status.slotinfo = utf8::to32(get_slotinfo(sfilen));
361 } else {
362 _status.saveslot_valid = false;
364 _status.branch_valid = (p != NULL);
365 if(p) _status.branch = utf8::to32(p->get_branch_string());
367 std::string cur_branch = lsnes_instance.mlogic ? lsnes_instance.mlogic.get_mfile().current_branch() :
369 _status.mbranch_valid = (cur_branch != "");
370 _status.mbranch = utf8::to32(cur_branch);
372 _status.speed = (unsigned)(100 * get_realized_multiplier() + 0.5);
374 if(lsnes_instance.mlogic && !system_corrupt) {
375 time_t timevalue = static_cast<time_t>(lsnes_instance.mlogic.get_mfile().rtc_second);
376 struct tm* time_decompose = gmtime(&timevalue);
377 char datebuffer[512];
378 strftime(datebuffer, 511, "%Y%m%d(%a)T%H%M%S", time_decompose);
379 _status.rtc = utf8::to32(datebuffer);
380 _status.rtc_valid = true;
381 } else {
382 _status.rtc_valid = false;
385 auto mset = controls.active_macro_set();
386 bool mfirst = true;
387 std::ostringstream mss;
388 for(auto i: mset) {
389 if(!mfirst) mss << ",";
390 mss << i;
391 mfirst = false;
393 _status.macros = utf8::to32(mss.str());
395 controller_frame c;
396 if(!multitrack_editor.any_records())
397 c = lsnes_instance.mlogic.get_movie().get_controls();
398 else
399 c = controls.get_committed();
400 _status.inputs.clear();
401 for(unsigned i = 0;; i++) {
402 auto pindex = controls.lcid_to_pcid(i);
403 if(pindex.first < 0 || !controls.is_present(pindex.first, pindex.second))
404 break;
405 char32_t buffer[MAX_DISPLAY_LENGTH];
406 c.display(pindex.first, pindex.second, buffer);
407 std::u32string _buffer = buffer;
408 if(readonly && multitrack_editor.is_enabled()) {
409 multitrack_edit::state st = multitrack_editor.get(pindex.first, pindex.second);
410 if(st == multitrack_edit::MT_PRESERVE)
411 _buffer += U" (keep)";
412 else if(st == multitrack_edit::MT_OVERWRITE)
413 _buffer += U" (rewrite)";
414 else if(st == multitrack_edit::MT_OR)
415 _buffer += U" (OR)";
416 else if(st == multitrack_edit::MT_XOR)
417 _buffer += U" (XOR)";
418 else
419 _buffer += U" (\?\?\?)";
421 _status.inputs.push_back(_buffer);
423 //Lua variables.
424 _status.lvars = get_lua_watch_vars();
425 //Memory watches.
426 _status.mvars = lsnes_memorywatch.get_window_vars();
428 _status.valid = true;
429 } catch(...) {
431 lsnes_status.put_write();
432 notify_status_update();
435 uint64_t audio_irq_time;
436 uint64_t controller_irq_time;
437 uint64_t frame_irq_time;
440 struct lsnes_callbacks : public emucore_callbacks
442 public:
443 ~lsnes_callbacks() throw()
447 int16_t get_input(unsigned port, unsigned index, unsigned control)
449 int16_t x;
450 x = lsnes_instance.mlogic.input_poll(port, index, control);
451 lua_callback_snoop_input(port, index, control, x);
452 return x;
455 int16_t set_input(unsigned port, unsigned index, unsigned control, int16_t value)
457 if(!lsnes_instance.mlogic.get_movie().readonly_mode()) {
458 controller_frame f = lsnes_instance.mlogic.get_movie().get_controls();
459 f.axis3(port, index, control, value);
460 lsnes_instance.mlogic.get_movie().set_controls(f);
462 return lsnes_instance.mlogic.get_movie().next_input(port, index, control);
465 void notify_latch(std::list<std::string>& args)
467 lua_callback_do_latch(args);
470 void timer_tick(uint32_t increment, uint32_t per_second)
472 if(!lsnes_instance.mlogic)
473 return;
474 auto& m = lsnes_instance.mlogic.get_mfile();
475 m.rtc_subsecond += increment;
476 while(m.rtc_subsecond >= per_second) {
477 m.rtc_second++;
478 m.rtc_subsecond -= per_second;
482 std::string get_firmware_path()
484 return lsnes_vset["firmwarepath"].str();
487 std::string get_base_path()
489 return our_rom.msu1_base;
492 time_t get_time()
494 return lsnes_instance.mlogic ? lsnes_instance.mlogic.get_mfile().rtc_second : 0;
497 time_t get_randomseed()
499 return random_seed_value;
502 void output_frame(framebuffer::raw& screen, uint32_t fps_n, uint32_t fps_d)
504 lua_callback_do_frame_emulated();
505 location_special = SPECIAL_FRAME_VIDEO;
506 redraw_framebuffer(screen, false, true);
507 uint32_t g = gcd(fps_n, fps_d);
508 fps_n /= g;
509 fps_d /= g;
510 information_dispatch::do_frame(screen, fps_n, fps_d);
513 void action_state_updated()
515 graphics_driver_action_updated();
518 void memory_read(uint64_t addr, uint64_t value)
520 debug_fire_callback_read(addr, value);
523 void memory_write(uint64_t addr, uint64_t value)
525 debug_fire_callback_write(addr, value);
528 void memory_execute(uint64_t addr, uint64_t proc)
530 debug_fire_callback_exec(addr, proc);
533 void memory_trace(uint64_t proc, const char* str, bool insn)
535 debug_fire_callback_trace(proc, str, insn);
539 namespace
541 lsnes_callbacks lsnes_callbacks_obj;
543 command::fnptr<const std::string&> test4(lsnes_cmd, "test4", "test", "test",
544 [](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
545 std::list<std::string> _args;
546 std::string args2 = args;
547 for(auto& sym : token_iterator_foreach(args, {" ", "\t"}))
548 _args.push_back(sym);
549 lua_callback_do_latch(_args);
551 command::fnptr<> count_rerecords(lsnes_cmd, "count-rerecords", "Count rerecords",
552 "Syntax: count-rerecords\nCounts rerecords.\n",
553 []() throw(std::bad_alloc, std::runtime_error) {
554 std::vector<char> tmp;
555 uint64_t x = lsnes_instance.mlogic.get_rrdata().write(tmp);
556 messages << x << " rerecord(s)" << std::endl;
559 command::fnptr<const std::string&> quit_emulator(lsnes_cmd, "quit-emulator", "Quit the emulator",
560 "Syntax: quit-emulator [/y]\nQuits emulator (/y => don't ask for confirmation).\n",
561 [](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
562 amode = ADVANCE_QUIT;
563 platform::set_paused(false);
564 platform::cancel_wait();
567 command::fnptr<> unpause_emulator(lsnes_cmd, "unpause-emulator", "Unpause the emulator",
568 "Syntax: unpause-emulator\nUnpauses the emulator.\n",
569 []() throw(std::bad_alloc, std::runtime_error) {
570 break_pause = false;
571 amode = ADVANCE_AUTO;
572 platform::set_paused(false);
573 platform::cancel_wait();
576 command::fnptr<> pause_emulator(lsnes_cmd, "pause-emulator", "(Un)pause the emulator",
577 "Syntax: pause-emulator\n(Un)pauses the emulator.\n",
578 []() throw(std::bad_alloc, std::runtime_error) {
579 if(amode != ADVANCE_AUTO || break_pause) {
580 break_pause = false;
581 amode = ADVANCE_AUTO;
582 platform::set_paused(false);
583 platform::cancel_wait();
584 } else {
585 platform::cancel_wait();
586 cancel_advance = false;
587 stop_at_frame_active = false;
588 amode = ADVANCE_PAUSE;
592 command::fnptr<> save_jukebox_prev(lsnes_cmd, "cycle-jukebox-backward", "Cycle save jukebox backwards",
593 "Syntax: cycle-jukebox-backward\nCycle save jukebox backwards\n",
594 []() throw(std::bad_alloc, std::runtime_error) {
595 if(jukebox_size == 0)
596 return;
597 if(save_jukebox_pointer == 0)
598 save_jukebox_pointer = jukebox_size - 1;
599 else
600 save_jukebox_pointer--;
601 if(save_jukebox_pointer >= (size_t)jukebox_size)
602 save_jukebox_pointer = 0;
603 update_movie_state();
606 command::fnptr<> save_jukebox_next(lsnes_cmd, "cycle-jukebox-forward", "Cycle save jukebox forwards",
607 "Syntax: cycle-jukebox-forward\nCycle save jukebox forwards\n",
608 []() throw(std::bad_alloc, std::runtime_error) {
609 if(jukebox_size == 0)
610 return;
611 if(save_jukebox_pointer + 1 >= (size_t)jukebox_size)
612 save_jukebox_pointer = 0;
613 else
614 save_jukebox_pointer++;
615 if(save_jukebox_pointer >= (size_t)jukebox_size)
616 save_jukebox_pointer = 0;
617 update_movie_state();
620 command::fnptr<const std::string&> save_jukebox_set(lsnes_cmd, "set-jukebox-slot", "Set jukebox slot",
621 "Syntax: set-jukebox-slot\nSet jukebox slot\n", [](const std::string& args)
622 throw(std::bad_alloc, std::runtime_error) {
623 if(!regex_match("[1-9][0-9]{0,8}", args))
624 throw std::runtime_error("Bad slot number");
625 uint32_t slot = parse_value<uint32_t>(args);
626 if(slot >= (size_t)jukebox_size)
627 throw std::runtime_error("Bad slot number");
628 save_jukebox_pointer = slot - 1;
629 update_movie_state();
632 command::fnptr<> load_jukebox(lsnes_cmd, "load-jukebox", "Load save from jukebox",
633 "Syntax: load-jukebox\nLoad save from jukebox\n",
634 []() throw(std::bad_alloc, std::runtime_error) {
635 if(jukebox_size == 0)
636 throw std::runtime_error("No slot selected");
637 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_CURRENT);
640 command::fnptr<> load_jukebox_readwrite(lsnes_cmd, "load-jukebox-readwrite", "Load save from jukebox in"
641 " read-write mode", "Syntax: load-jukebox-readwrite\nLoad save from jukebox in read-write mode\n",
642 []() throw(std::bad_alloc, std::runtime_error) {
643 if(jukebox_size == 0)
644 throw std::runtime_error("No slot selected");
645 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_RW);
648 command::fnptr<> load_jukebox_readonly(lsnes_cmd, "load-jukebox-readonly", "Load save from jukebox in "
649 "read-only mode", "Syntax: load-jukebox-readonly\nLoad save from jukebox in read-only mode\n",
650 []() throw(std::bad_alloc, std::runtime_error) {
651 if(jukebox_size == 0)
652 throw std::runtime_error("No slot selected");
653 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_RO);
656 command::fnptr<> load_jukebox_preserve(lsnes_cmd, "load-jukebox-preserve", "Load save from jukebox, "
657 "preserving input", "Syntax: load-jukebox-preserve\nLoad save from jukebox, preserving input\n",
658 []() throw(std::bad_alloc, std::runtime_error) {
659 if(jukebox_size == 0)
660 throw std::runtime_error("No slot selected");
661 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_PRESERVE);
664 command::fnptr<> load_jukebox_movie(lsnes_cmd, "load-jukebox-movie", "Load save from jukebox as movie",
665 "Syntax: load-jukebox-movie\nLoad save from jukebox as movie\n",
666 []() throw(std::bad_alloc, std::runtime_error) {
667 if(jukebox_size == 0)
668 throw std::runtime_error("No slot selected");
669 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_MOVIE);
672 command::fnptr<> save_jukebox_c(lsnes_cmd, "save-jukebox", "Save save to jukebox",
673 "Syntax: save-jukebox\nSave save to jukebox\n",
674 []() throw(std::bad_alloc, std::runtime_error) {
675 if(jukebox_size == 0)
676 throw std::runtime_error("No slot selected");
677 mark_pending_save(save_jukebox_name(save_jukebox_pointer), SAVE_STATE, -1);
680 command::fnptr<> padvance_frame(lsnes_cmd, "+advance-frame", "Advance one frame",
681 "Syntax: +advance-frame\nAdvances the emulation by one frame.\n",
682 []() throw(std::bad_alloc, std::runtime_error) {
683 if(break_pause)
684 break_pause = false;
685 amode = ADVANCE_FRAME;
686 cancel_advance = false;
687 advanced_once = false;
688 platform::cancel_wait();
689 platform::set_paused(false);
692 command::fnptr<> nadvance_frame(lsnes_cmd, "-advance-frame", "Advance one frame",
693 "No help available\n",
694 []() throw(std::bad_alloc, std::runtime_error) {
695 cancel_advance = true;
696 platform::cancel_wait();
697 platform::set_paused(false);
700 command::fnptr<> padvance_poll(lsnes_cmd, "+advance-poll", "Advance one subframe",
701 "Syntax: +advance-poll\nAdvances the emulation by one subframe.\n",
702 []() throw(std::bad_alloc, std::runtime_error) {
703 if(break_pause)
704 break_pause = false;
705 amode = ADVANCE_SUBFRAME;
706 cancel_advance = false;
707 advanced_once = false;
708 platform::cancel_wait();
709 platform::set_paused(false);
712 command::fnptr<> nadvance_poll(lsnes_cmd, "-advance-poll", "Advance one subframe",
713 "No help available\n",
714 []() throw(std::bad_alloc, std::runtime_error) {
715 if(break_pause)
716 break_pause = false;
717 cancel_advance = true;
718 platform::cancel_wait();
719 platform::set_paused(false);
722 command::fnptr<> advance_skiplag(lsnes_cmd, "advance-skiplag", "Skip to next poll",
723 "Syntax: advance-skiplag\nAdvances the emulation to the next poll.\n",
724 []() throw(std::bad_alloc, std::runtime_error) {
725 if(break_pause)
726 break_pause = false;
727 amode = ADVANCE_SKIPLAG_PENDING;
728 platform::cancel_wait();
729 platform::set_paused(false);
732 command::fnptr<> reset_c(lsnes_cmd, "reset", "Reset the system",
733 "Syntax: reset\nReset\nResets the system in beginning of the next frame.\n",
734 []() throw(std::bad_alloc, std::runtime_error) {
735 int sreset_action = our_rom.rtype->reset_action(false);
736 if(sreset_action < 0) {
737 platform::error_message("Core does not support resets");
738 messages << "Emulator core does not support resets" << std::endl;
739 return;
741 our_rom.rtype->execute_action(sreset_action, std::vector<interface_action_paramval>());
744 command::fnptr<> hreset_c(lsnes_cmd, "reset-hard", "Reset the system",
745 "Syntax: reset-hard\nReset-hard\nHard resets the system in beginning of the next frame.\n",
746 []() throw(std::bad_alloc, std::runtime_error) {
747 int hreset_action = our_rom.rtype->reset_action(true);
748 if(hreset_action < 0) {
749 platform::error_message("Core does not support hard resets");
750 messages << "Emulator core does not support hard resets" << std::endl;
751 return;
753 our_rom.rtype->execute_action(hreset_action, std::vector<interface_action_paramval>());
756 command::fnptr<command::arg_filename> load_c(lsnes_cmd, "load", "Load savestate (current mode)",
757 "Syntax: load <file>\nLoads SNES state from <file> in current mode\n",
758 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
759 mark_pending_load(args, LOAD_STATE_CURRENT);
762 command::fnptr<command::arg_filename> load_smart_c(lsnes_cmd, "load-smart", "Load savestate (heuristic mode)",
763 "Syntax: load <file>\nLoads SNES state from <file> in heuristic mode\n",
764 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
765 mark_pending_load(args, LOAD_STATE_DEFAULT);
768 command::fnptr<command::arg_filename> load_state_c(lsnes_cmd, "load-state", "Load savestate (R/W)",
769 "Syntax: load-state <file>\nLoads SNES state from <file> in Read/Write mode\n",
770 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
771 mark_pending_load(args, LOAD_STATE_RW);
774 command::fnptr<command::arg_filename> load_readonly(lsnes_cmd, "load-readonly", "Load savestate (RO)",
775 "Syntax: load-readonly <file>\nLoads SNES state from <file> in read-only mode\n",
776 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
777 mark_pending_load(args, LOAD_STATE_RO);
780 command::fnptr<command::arg_filename> load_preserve(lsnes_cmd, "load-preserve", "Load savestate (preserve "
781 "input)", "Syntax: load-preserve <file>\nLoads SNES state from <file> preserving input\n",
782 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
783 mark_pending_load(args, LOAD_STATE_PRESERVE);
786 command::fnptr<command::arg_filename> load_movie_c(lsnes_cmd, "load-movie", "Load movie",
787 "Syntax: load-movie <file>\nLoads SNES movie from <file>\n",
788 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
789 mark_pending_load(args, LOAD_STATE_MOVIE);
792 command::fnptr<command::arg_filename> load_allbr_c(lsnes_cmd, "load-allbranches", "Load savestate "
793 "(all branches)", "Syntax: load-allbranches <file>\nLoads SNES state from <file> with all "
794 "branches\n",
795 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
796 mark_pending_load(args, LOAD_STATE_ALLBRANCH);
799 command::fnptr<command::arg_filename> save_state(lsnes_cmd, "save-state", "Save state",
800 "Syntax: save-state <file>\nSaves SNES state to <file>\n",
801 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
802 mark_pending_save(args, SAVE_STATE, -1);
805 command::fnptr<command::arg_filename> save_state2(lsnes_cmd, "save-state-binary", "Save state (binary)",
806 "Syntax: save-state-binary <file>\nSaves binary state to <file>\n",
807 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
808 mark_pending_save(args, SAVE_STATE, 1);
811 command::fnptr<command::arg_filename> save_state3(lsnes_cmd, "save-state-zip", "Save state (zip)",
812 "Syntax: save-state-zip <file>\nSaves zip state to <file>\n",
813 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
814 mark_pending_save(args, SAVE_STATE, 0);
817 command::fnptr<command::arg_filename> save_movie(lsnes_cmd, "save-movie", "Save movie",
818 "Syntax: save-movie <file>\nSaves SNES movie to <file>\n",
819 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
820 mark_pending_save(args, SAVE_MOVIE, -1);
823 command::fnptr<command::arg_filename> save_movie2(lsnes_cmd, "save-movie-binary", "Save movie (binary)",
824 "Syntax: save-movie-binary <file>\nSaves binary movie to <file>\n",
825 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
826 mark_pending_save(args, SAVE_MOVIE, 1);
829 command::fnptr<command::arg_filename> save_movie3(lsnes_cmd, "save-movie-zip", "Save movie (zip)",
830 "Syntax: save-movie-zip <file>\nSaves zip movie to <file>\n",
831 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
832 mark_pending_save(args, SAVE_MOVIE, 0);
835 command::fnptr<> set_rwmode(lsnes_cmd, "set-rwmode", "Switch to read/write mode",
836 "Syntax: set-rwmode\nSwitches to read/write mode\n",
837 []() throw(std::bad_alloc, std::runtime_error) {
838 lua_callback_movie_lost("readwrite");
839 lsnes_instance.mlogic.get_movie().readonly_mode(false);
840 notify_mode_change(false);
841 lua_callback_do_readwrite();
842 update_movie_state();
845 command::fnptr<> set_romode(lsnes_cmd, "set-romode", "Switch to read-only mode",
846 "Syntax: set-romode\nSwitches to read-only mode\n",
847 []() throw(std::bad_alloc, std::runtime_error) {
848 lsnes_instance.mlogic.get_movie().readonly_mode(true);
849 notify_mode_change(true);
850 update_movie_state();
853 command::fnptr<> toggle_rwmode(lsnes_cmd, "toggle-rwmode", "Toggle read/write mode",
854 "Syntax: toggle-rwmode\nToggles read/write mode\n",
855 []() throw(std::bad_alloc, std::runtime_error) {
856 bool c = lsnes_instance.mlogic.get_movie().readonly_mode();
857 if(c)
858 lua_callback_movie_lost("readwrite");
859 lsnes_instance.mlogic.get_movie().readonly_mode(!c);
860 notify_mode_change(!c);
861 if(c)
862 lua_callback_do_readwrite();
863 update_movie_state();
866 command::fnptr<> repaint(lsnes_cmd, "repaint", "Redraw the screen",
867 "Syntax: repaint\nRedraws the screen\n",
868 []() throw(std::bad_alloc, std::runtime_error) {
869 redraw_framebuffer();
872 command::fnptr<> tpon(lsnes_cmd, "toggle-pause-on-end", "Toggle pause on end", "Toggle pause on end\n",
873 []() throw(std::bad_alloc, std::runtime_error) {
874 bool tmp = pause_on_end;
875 pause_on_end.set(!tmp);
876 messages << "Pause-on-end is now " << (tmp ? "OFF" : "ON") << std::endl;
879 command::fnptr<> spon(lsnes_cmd, "set-pause-on-end", "Set pause on end", "Set pause on end\n",
880 []() throw(std::bad_alloc, std::runtime_error) {
881 pause_on_end.set(true);
882 messages << "Pause-on-end is now ON" << std::endl;
885 command::fnptr<> cpon(lsnes_cmd, "clear-pause-on-end", "Clear pause on end", "Clear pause on end\n",
886 []() throw(std::bad_alloc, std::runtime_error) {
887 pause_on_end.set(false);
888 messages << "Pause-on-end is now OFF" << std::endl;
891 command::fnptr<> rewind_movie(lsnes_cmd, "rewind-movie", "Rewind movie to the beginning",
892 "Syntax: rewind-movie\nRewind movie to the beginning\n",
893 []() throw(std::bad_alloc, std::runtime_error) {
894 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_BEGINNING);
897 command::fnptr<> cancel_save(lsnes_cmd, "cancel-saves", "Cancel all pending saves", "Syntax: "
898 "cancel-save\nCancel pending saves\n",
899 []() throw(std::bad_alloc, std::runtime_error) {
900 queued_saves.clear();
901 messages << "Pending saves canceled." << std::endl;
904 command::fnptr<> flushslots(lsnes_cmd, "flush-slotinfo", "Flush slotinfo cache",
905 "Flush slotinfo cache\n",
906 []() throw(std::bad_alloc, std::runtime_error) {
907 flush_slotinfo();
910 command::fnptr<> mhold1(lsnes_cmd, "+hold-macro", "Hold macro (hold)",
911 "Hold macros enable\n", []() throw(std::bad_alloc, std::runtime_error) {
912 macro_hold_1 = true;
915 command::fnptr<> mhold2(lsnes_cmd, "-hold-macro", "Hold macro (hold)",
916 "Hold macros disable\n", []() throw(std::bad_alloc, std::runtime_error) {
917 macro_hold_1 = false;
920 command::fnptr<> mhold3(lsnes_cmd, "hold-macro", "Hold macro (toggle)",
921 "Hold macros toggle\n", []() throw(std::bad_alloc, std::runtime_error) {
922 macro_hold_2 = !macro_hold_2;
923 if(macro_hold_2)
924 messages << "Macros are held for next frame." << std::endl;
925 else
926 messages << "Macros are not held for next frame." << std::endl;
929 keyboard::invbind imhold1(lsnes_mapper, "+hold-macro", "Macro‣Hold all macros");
930 keyboard::invbind imhold2(lsnes_mapper, "hold-macro", "Macro‣Hold all macros (typed)");
931 keyboard::invbind ipause_emulator(lsnes_mapper, "pause-emulator", "Speed‣(Un)pause");
932 keyboard::invbind ijback(lsnes_mapper, "cycle-jukebox-backward", "Slot select‣Cycle backwards");
933 keyboard::invbind ijforward(lsnes_mapper, "cycle-jukebox-forward", "Slot select‣Cycle forwards");
934 keyboard::invbind iloadj(lsnes_mapper, "load-jukebox", "Load‣Selected slot");
935 keyboard::invbind iloadjrw(lsnes_mapper, "load-jukebox-readwrite", "Load‣Selected slot (readwrite mode)");
936 keyboard::invbind iloadjro(lsnes_mapper, "load-jukebox-readonly", "Load‣Selected slot (readonly mode)");
937 keyboard::invbind iloadjp(lsnes_mapper, "load-jukebox-preserve", "Load‣Selected slot (preserve input)");
938 keyboard::invbind iloadjm(lsnes_mapper, "load-jukebox-movie", "Load‣Selected slot (as movie)");
939 keyboard::invbind isavej(lsnes_mapper, "save-jukebox", "Save‣Selected slot");
940 keyboard::invbind iadvframe(lsnes_mapper, "+advance-frame", "Speed‣Advance frame");
941 keyboard::invbind iadvsubframe(lsnes_mapper, "+advance-poll", "Speed‣Advance subframe");
942 keyboard::invbind iskiplag(lsnes_mapper, "advance-skiplag", "Speed‣Advance poll");
943 keyboard::invbind ireset(lsnes_mapper, "reset", "System‣Reset");
944 keyboard::invbind iset_rwmode(lsnes_mapper, "set-rwmode", "Movie‣Switch to read/write");
945 keyboard::invbind itoggle_romode(lsnes_mapper, "set-romode", "Movie‣Switch to read-only");
946 keyboard::invbind itoggle_rwmode(lsnes_mapper, "toggle-rwmode", "Movie‣Toggle read-only");
947 keyboard::invbind irepaint(lsnes_mapper, "repaint", "System‣Repaint screen");
948 keyboard::invbind itogglepause(lsnes_mapper, "toggle-pause-on-end", "Movie‣Toggle pause-on-end");
949 keyboard::invbind irewind_movie(lsnes_mapper, "rewind-movie", "Movie‣Rewind movie");
950 keyboard::invbind icancel_saves(lsnes_mapper, "cancel-saves", "Save‣Cancel pending saves");
951 keyboard::invbind iload1(lsnes_mapper, "load $SLOT:1", "Load‣Slot 1");
952 keyboard::invbind iload2(lsnes_mapper, "load $SLOT:2", "Load‣Slot 2");
953 keyboard::invbind iload3(lsnes_mapper, "load $SLOT:3", "Load‣Slot 3");
954 keyboard::invbind iload4(lsnes_mapper, "load $SLOT:4", "Load‣Slot 4");
955 keyboard::invbind iload5(lsnes_mapper, "load $SLOT:5", "Load‣Slot 5");
956 keyboard::invbind iload6(lsnes_mapper, "load $SLOT:6", "Load‣Slot 6");
957 keyboard::invbind iload7(lsnes_mapper, "load $SLOT:7", "Load‣Slot 7");
958 keyboard::invbind iload8(lsnes_mapper, "load $SLOT:8", "Load‣Slot 8");
959 keyboard::invbind iload9(lsnes_mapper, "load $SLOT:9", "Load‣Slot 9");
960 keyboard::invbind iload10(lsnes_mapper, "load $SLOT:10", "Load‣Slot 10");
961 keyboard::invbind iload11(lsnes_mapper, "load $SLOT:11", "Load‣Slot 11");
962 keyboard::invbind iload12(lsnes_mapper, "load $SLOT:12", "Load‣Slot 12");
963 keyboard::invbind iload13(lsnes_mapper, "load $SLOT:13", "Load‣Slot 13");
964 keyboard::invbind iload14(lsnes_mapper, "load $SLOT:14", "Load‣Slot 14");
965 keyboard::invbind iload15(lsnes_mapper, "load $SLOT:15", "Load‣Slot 15");
966 keyboard::invbind iload16(lsnes_mapper, "load $SLOT:16", "Load‣Slot 16");
967 keyboard::invbind iload17(lsnes_mapper, "load $SLOT:17", "Load‣Slot 17");
968 keyboard::invbind iload18(lsnes_mapper, "load $SLOT:18", "Load‣Slot 18");
969 keyboard::invbind iload19(lsnes_mapper, "load $SLOT:19", "Load‣Slot 19");
970 keyboard::invbind iload20(lsnes_mapper, "load $SLOT:20", "Load‣Slot 20");
971 keyboard::invbind iload21(lsnes_mapper, "load $SLOT:21", "Load‣Slot 21");
972 keyboard::invbind iload22(lsnes_mapper, "load $SLOT:22", "Load‣Slot 22");
973 keyboard::invbind iload23(lsnes_mapper, "load $SLOT:23", "Load‣Slot 23");
974 keyboard::invbind iload24(lsnes_mapper, "load $SLOT:24", "Load‣Slot 24");
975 keyboard::invbind iload25(lsnes_mapper, "load $SLOT:25", "Load‣Slot 25");
976 keyboard::invbind iload26(lsnes_mapper, "load $SLOT:26", "Load‣Slot 26");
977 keyboard::invbind iload27(lsnes_mapper, "load $SLOT:27", "Load‣Slot 27");
978 keyboard::invbind iload28(lsnes_mapper, "load $SLOT:28", "Load‣Slot 28");
979 keyboard::invbind iload29(lsnes_mapper, "load $SLOT:29", "Load‣Slot 29");
980 keyboard::invbind iload30(lsnes_mapper, "load $SLOT:30", "Load‣Slot 30");
981 keyboard::invbind iload31(lsnes_mapper, "load $SLOT:31", "Load‣Slot 31");
982 keyboard::invbind iload32(lsnes_mapper, "load $SLOT:32", "Load‣Slot 32");
983 keyboard::invbind isave1(lsnes_mapper, "save-state $SLOT:1", "Save‣Slot 1");
984 keyboard::invbind isave2(lsnes_mapper, "save-state $SLOT:2", "Save‣Slot 2");
985 keyboard::invbind isave3(lsnes_mapper, "save-state $SLOT:3", "Save‣Slot 3");
986 keyboard::invbind isave4(lsnes_mapper, "save-state $SLOT:4", "Save‣Slot 4");
987 keyboard::invbind isave5(lsnes_mapper, "save-state $SLOT:5", "Save‣Slot 5");
988 keyboard::invbind isave6(lsnes_mapper, "save-state $SLOT:6", "Save‣Slot 6");
989 keyboard::invbind isave7(lsnes_mapper, "save-state $SLOT:7", "Save‣Slot 7");
990 keyboard::invbind isave8(lsnes_mapper, "save-state $SLOT:8", "Save‣Slot 8");
991 keyboard::invbind isave9(lsnes_mapper, "save-state $SLOT:9", "Save‣Slot 9");
992 keyboard::invbind isave10(lsnes_mapper, "save-state $SLOT:10", "Save‣Slot 10");
993 keyboard::invbind isave11(lsnes_mapper, "save-state $SLOT:11", "Save‣Slot 11");
994 keyboard::invbind isave12(lsnes_mapper, "save-state $SLOT:12", "Save‣Slot 12");
995 keyboard::invbind isave13(lsnes_mapper, "save-state $SLOT:13", "Save‣Slot 13");
996 keyboard::invbind isave14(lsnes_mapper, "save-state $SLOT:14", "Save‣Slot 14");
997 keyboard::invbind isave15(lsnes_mapper, "save-state $SLOT:15", "Save‣Slot 15");
998 keyboard::invbind isave16(lsnes_mapper, "save-state $SLOT:16", "Save‣Slot 16");
999 keyboard::invbind isave17(lsnes_mapper, "save-state $SLOT:17", "Save‣Slot 17");
1000 keyboard::invbind isave18(lsnes_mapper, "save-state $SLOT:18", "Save‣Slot 18");
1001 keyboard::invbind isave19(lsnes_mapper, "save-state $SLOT:19", "Save‣Slot 19");
1002 keyboard::invbind isave20(lsnes_mapper, "save-state $SLOT:20", "Save‣Slot 20");
1003 keyboard::invbind isave21(lsnes_mapper, "save-state $SLOT:21", "Save‣Slot 21");
1004 keyboard::invbind isave22(lsnes_mapper, "save-state $SLOT:22", "Save‣Slot 22");
1005 keyboard::invbind isave23(lsnes_mapper, "save-state $SLOT:23", "Save‣Slot 23");
1006 keyboard::invbind isave24(lsnes_mapper, "save-state $SLOT:24", "Save‣Slot 24");
1007 keyboard::invbind isave25(lsnes_mapper, "save-state $SLOT:25", "Save‣Slot 25");
1008 keyboard::invbind isave26(lsnes_mapper, "save-state $SLOT:26", "Save‣Slot 26");
1009 keyboard::invbind isave27(lsnes_mapper, "save-state $SLOT:27", "Save‣Slot 27");
1010 keyboard::invbind isave28(lsnes_mapper, "save-state $SLOT:28", "Save‣Slot 28");
1011 keyboard::invbind isave29(lsnes_mapper, "save-state $SLOT:29", "Save‣Slot 29");
1012 keyboard::invbind isave30(lsnes_mapper, "save-state $SLOT:30", "Save‣Slot 30");
1013 keyboard::invbind isave31(lsnes_mapper, "save-state $SLOT:31", "Save‣Slot 31");
1014 keyboard::invbind isave32(lsnes_mapper, "save-state $SLOT:32", "Save‣Slot 32");
1015 keyboard::invbind islot1(lsnes_mapper, "set-jukebox-slot 1", "Slot select‣Slot 1");
1016 keyboard::invbind islot2(lsnes_mapper, "set-jukebox-slot 2", "Slot select‣Slot 2");
1017 keyboard::invbind islot3(lsnes_mapper, "set-jukebox-slot 3", "Slot select‣Slot 3");
1018 keyboard::invbind islot4(lsnes_mapper, "set-jukebox-slot 4", "Slot select‣Slot 4");
1019 keyboard::invbind islot5(lsnes_mapper, "set-jukebox-slot 5", "Slot select‣Slot 5");
1020 keyboard::invbind islot6(lsnes_mapper, "set-jukebox-slot 6", "Slot select‣Slot 6");
1021 keyboard::invbind islot7(lsnes_mapper, "set-jukebox-slot 7", "Slot select‣Slot 7");
1022 keyboard::invbind islot8(lsnes_mapper, "set-jukebox-slot 8", "Slot select‣Slot 8");
1023 keyboard::invbind islot9(lsnes_mapper, "set-jukebox-slot 9", "Slot select‣Slot 9");
1024 keyboard::invbind islot10(lsnes_mapper, "set-jukebox-slot 10", "Slot select‣Slot 10");
1025 keyboard::invbind islot11(lsnes_mapper, "set-jukebox-slot 11", "Slot select‣Slot 11");
1026 keyboard::invbind islot12(lsnes_mapper, "set-jukebox-slot 12", "Slot select‣Slot 12");
1027 keyboard::invbind islot13(lsnes_mapper, "set-jukebox-slot 13", "Slot select‣Slot 13");
1028 keyboard::invbind islot14(lsnes_mapper, "set-jukebox-slot 14", "Slot select‣Slot 14");
1029 keyboard::invbind islot15(lsnes_mapper, "set-jukebox-slot 15", "Slot select‣Slot 15");
1030 keyboard::invbind islot16(lsnes_mapper, "set-jukebox-slot 16", "Slot select‣Slot 16");
1031 keyboard::invbind islot17(lsnes_mapper, "set-jukebox-slot 17", "Slot select‣Slot 17");
1032 keyboard::invbind islot18(lsnes_mapper, "set-jukebox-slot 18", "Slot select‣Slot 18");
1033 keyboard::invbind islot19(lsnes_mapper, "set-jukebox-slot 19", "Slot select‣Slot 19");
1034 keyboard::invbind islot20(lsnes_mapper, "set-jukebox-slot 20", "Slot select‣Slot 20");
1035 keyboard::invbind islot21(lsnes_mapper, "set-jukebox-slot 21", "Slot select‣Slot 21");
1036 keyboard::invbind islot22(lsnes_mapper, "set-jukebox-slot 22", "Slot select‣Slot 22");
1037 keyboard::invbind islot23(lsnes_mapper, "set-jukebox-slot 23", "Slot select‣Slot 23");
1038 keyboard::invbind islot24(lsnes_mapper, "set-jukebox-slot 24", "Slot select‣Slot 24");
1039 keyboard::invbind islot25(lsnes_mapper, "set-jukebox-slot 25", "Slot select‣Slot 25");
1040 keyboard::invbind islot26(lsnes_mapper, "set-jukebox-slot 26", "Slot select‣Slot 26");
1041 keyboard::invbind islot27(lsnes_mapper, "set-jukebox-slot 27", "Slot select‣Slot 27");
1042 keyboard::invbind islot28(lsnes_mapper, "set-jukebox-slot 28", "Slot select‣Slot 28");
1043 keyboard::invbind islot29(lsnes_mapper, "set-jukebox-slot 29", "Slot select‣Slot 29");
1044 keyboard::invbind islot30(lsnes_mapper, "set-jukebox-slot 30", "Slot select‣Slot 30");
1045 keyboard::invbind islot31(lsnes_mapper, "set-jukebox-slot 31", "Slot select‣Slot 31");
1046 keyboard::invbind islot32(lsnes_mapper, "set-jukebox-slot 32", "Slot select‣Slot 32");
1048 bool on_quit_prompt = false;
1049 class mywindowcallbacks : public information_dispatch
1051 public:
1052 mywindowcallbacks() : information_dispatch("mainloop-window-callbacks")
1054 closenotify.set(notify_close, [this]() {
1055 if(on_quit_prompt) {
1056 amode = ADVANCE_QUIT;
1057 platform::set_paused(false);
1058 platform::cancel_wait();
1059 return;
1061 on_quit_prompt = true;
1062 try {
1063 amode = ADVANCE_QUIT;
1064 platform::set_paused(false);
1065 platform::cancel_wait();
1066 } catch(...) {
1068 on_quit_prompt = false;
1071 ~mywindowcallbacks() throw() {}
1072 void on_new_dumper(const std::string& n)
1074 update_movie_state();
1076 void on_destroy_dumper(const std::string& n)
1078 update_movie_state();
1080 private:
1081 struct dispatch::target<> closenotify;
1082 } mywcb;
1084 //If there is a pending load, perform it. Return 1 on successful load, 0 if nothing to load, -1 on load
1085 //failing.
1086 int handle_load()
1088 std::string old_project = lsnes_instance.mlogic ? lsnes_instance.mlogic.get_mfile().projectid : "";
1089 jumpback:
1090 if(do_unsafe_rewind && unsafe_rewind_obj) {
1091 if(!lsnes_instance.mlogic)
1092 return 0;
1093 uint64_t t = get_utime();
1094 std::vector<char> s;
1095 lua_callback_do_unsafe_rewind(s, 0, 0, lsnes_instance.mlogic.get_movie(), unsafe_rewind_obj);
1096 notify_mode_change(false);
1097 do_unsafe_rewind = false;
1098 lsnes_instance.mlogic.get_mfile().is_savestate = true;
1099 location_special = SPECIAL_SAVEPOINT;
1100 update_movie_state();
1101 messages << "Rewind done in " << (get_utime() - t) << " usec." << std::endl;
1102 return 1;
1104 if(pending_new_project != "") {
1105 std::string id = pending_new_project;
1106 pending_new_project = "";
1107 project_info* old = project_get();
1108 if(old && old->id == id)
1109 goto nothing_to_do;
1110 try {
1111 auto& p = project_load(id);
1112 project_set(&p);
1113 if(project_get() != old)
1114 delete old;
1115 flush_slotinfo(); //Wrong movie may be stale.
1116 return 1;
1117 } catch(std::exception& e) {
1118 platform::error_message(std::string("Can't switch projects: ") + e.what());
1119 messages << "Can't switch projects: " << e.what() << std::endl;
1120 goto nothing_to_do;
1122 nothing_to_do:
1123 amode = old_mode;
1124 platform::set_paused(amode == ADVANCE_PAUSE);
1125 platform::flush_command_queue();
1126 if(amode == ADVANCE_LOAD)
1127 goto jumpback;
1128 return 0;
1130 if(pending_load != "") {
1131 bool system_was_corrupt = system_corrupt;
1132 system_corrupt = false;
1133 try {
1134 if(loadmode != LOAD_STATE_BEGINNING && loadmode != LOAD_STATE_ROMRELOAD &&
1135 !do_load_state(pending_load, loadmode)) {
1136 if(system_was_corrupt)
1137 system_corrupt = system_was_corrupt;
1138 pending_load = "";
1139 return -1;
1141 if(loadmode == LOAD_STATE_BEGINNING)
1142 do_load_rewind();
1143 if(loadmode == LOAD_STATE_ROMRELOAD)
1144 do_load_rom();
1145 } catch(std::exception& e) {
1146 if(!system_corrupt && system_was_corrupt)
1147 system_corrupt = true;
1148 platform::error_message(std::string("Load failed: ") + e.what());
1149 messages << "Load failed: " << e.what() << std::endl;
1151 pending_load = "";
1152 amode = load_paused ? ADVANCE_PAUSE : ADVANCE_AUTO;
1153 platform::set_paused(load_paused);
1154 load_paused = false;
1155 if(!system_corrupt) {
1156 location_special = SPECIAL_SAVEPOINT;
1157 update_movie_state();
1158 platform::flush_command_queue();
1159 if(amode == ADVANCE_QUIT)
1160 return -1;
1161 if(amode == ADVANCE_LOAD)
1162 goto jumpback;
1164 if(old_project != (lsnes_instance.mlogic ? lsnes_instance.mlogic.get_mfile().projectid : ""))
1165 flush_slotinfo(); //Wrong movie may be stale.
1166 return 1;
1168 return 0;
1171 //If there are pending saves, perform them.
1172 void handle_saves()
1174 if(!lsnes_instance.mlogic)
1175 return;
1176 if(!queued_saves.empty() || (do_unsafe_rewind && !unsafe_rewind_obj)) {
1177 our_rom.rtype->runtosave();
1178 for(auto i : queued_saves) {
1179 do_save_state(i.first, i.second);
1180 int tmp = -1;
1181 flush_slotinfo(translate_name_mprefix(i.first, tmp, -1));
1183 if(do_unsafe_rewind && !unsafe_rewind_obj) {
1184 uint64_t t = get_utime();
1185 std::vector<char> s = our_rom.save_core_state(true);
1186 uint64_t secs = lsnes_instance.mlogic.get_mfile().rtc_second;
1187 uint64_t ssecs = lsnes_instance.mlogic.get_mfile().rtc_subsecond;
1188 lua_callback_do_unsafe_rewind(s, secs, ssecs, lsnes_instance.mlogic.get_movie(),
1189 NULL);
1190 do_unsafe_rewind = false;
1191 messages << "Rewind point set in " << (get_utime() - t) << " usec." << std::endl;
1194 queued_saves.clear();
1197 bool handle_corrupt()
1199 if(!system_corrupt)
1200 return false;
1201 while(system_corrupt) {
1202 platform::set_paused(true);
1203 platform::flush_command_queue();
1204 handle_load();
1205 if(amode == ADVANCE_QUIT)
1206 return true;
1208 return true;
1212 void init_main_callbacks()
1214 ecore_callbacks = &lsnes_callbacks_obj;
1217 void main_loop(struct loaded_rom& rom, struct moviefile& initial, bool load_has_to_succeed) throw(std::bad_alloc,
1218 std::runtime_error)
1220 platform::system_thread_available(true);
1221 //Basic initialization.
1222 dispatch_set_error_streams(&messages.getstream());
1223 emulation_thread = threads::this_id();
1224 jukebox_size_listener jlistener;
1225 voicethread_task();
1226 init_special_screens();
1227 our_rom = rom;
1228 init_main_callbacks();
1229 initialize_all_builtin_c_cores();
1230 core_core::install_all_handlers();
1232 //Load our given movie.
1233 bool first_round = false;
1234 bool just_did_loadstate = false;
1235 bool used = false;
1236 try {
1237 do_load_state(initial, LOAD_STATE_INITIAL, used);
1238 location_special = SPECIAL_SAVEPOINT;
1239 update_movie_state();
1240 first_round = lsnes_instance.mlogic.get_mfile().is_savestate;
1241 just_did_loadstate = first_round;
1242 } catch(std::bad_alloc& e) {
1243 OOM_panic();
1244 } catch(std::exception& e) {
1245 if(!used)
1246 delete &initial;
1247 platform::error_message(std::string("Can't load initial state: ") + e.what());
1248 messages << "ERROR: Can't load initial state: " << e.what() << std::endl;
1249 if(load_has_to_succeed) {
1250 messages << "FATAL: Can't load movie" << std::endl;
1251 throw;
1253 system_corrupt = true;
1254 redraw_framebuffer(screen_corrupt);
1257 platform::set_paused(initial.start_paused);
1258 amode = initial.start_paused ? ADVANCE_PAUSE : ADVANCE_AUTO;
1259 stop_at_frame_active = false;
1261 lua_run_startup_scripts();
1263 uint64_t time_x = get_utime();
1264 while(amode != ADVANCE_QUIT || !queued_saves.empty()) {
1265 if(handle_corrupt()) {
1266 first_round = lsnes_instance.mlogic && lsnes_instance.mlogic.get_mfile().is_savestate;
1267 just_did_loadstate = first_round;
1268 continue;
1270 ack_frame_tick(get_utime());
1271 if(amode == ADVANCE_SKIPLAG_PENDING)
1272 amode = ADVANCE_SKIPLAG;
1274 if(!first_round) {
1275 controls.reset_framehold();
1276 if(!macro_hold_1 && !macro_hold_2) {
1277 controls.advance_macros();
1279 macro_hold_2 = false;
1280 lsnes_instance.mlogic.get_movie().get_pollcounters().set_framepflag(false);
1281 lsnes_instance.mlogic.new_frame_starting(amode == ADVANCE_SKIPLAG);
1282 lsnes_instance.mlogic.get_movie().get_pollcounters().set_framepflag(true);
1283 if(amode == ADVANCE_QUIT && queued_saves.empty())
1284 break;
1285 handle_saves();
1286 int r = 0;
1287 if(queued_saves.empty())
1288 r = handle_load();
1289 if(r > 0 || system_corrupt) {
1290 lsnes_instance.mlogic.get_movie().get_pollcounters().set_framepflag(
1291 lsnes_instance.mlogic.get_mfile().is_savestate);
1292 first_round = lsnes_instance.mlogic.get_mfile().is_savestate;
1293 if(system_corrupt)
1294 amode = ADVANCE_PAUSE;
1295 else
1296 amode = old_mode;
1297 stop_at_frame_active = false;
1298 just_did_loadstate = first_round;
1299 controls.reset_framehold();
1300 debug_fire_callback_frame(lsnes_instance.mlogic.get_movie().get_current_frame(),
1301 true);
1302 continue;
1303 } else if(r < 0) {
1304 //Not exactly desriable, but this at least won't desync.
1305 stop_at_frame_active = false;
1306 if(amode == ADVANCE_QUIT)
1307 return;
1308 amode = ADVANCE_PAUSE;
1311 if(just_did_loadstate) {
1312 //If we just loadstated, we are up to date.
1313 if(amode == ADVANCE_QUIT)
1314 break;
1315 platform::set_paused(amode == ADVANCE_PAUSE);
1316 platform::flush_command_queue();
1317 //We already have done the reset this frame if we are going to do one at all.
1318 lsnes_instance.mlogic.get_movie().set_controls(lsnes_instance.mlogic.update_controls(true));
1319 lsnes_instance.mlogic.get_movie().set_all_DRDY();
1320 just_did_loadstate = false;
1322 frame_irq_time = get_utime() - time_x;
1323 debug_fire_callback_frame(lsnes_instance.mlogic.get_movie().get_current_frame(), false);
1324 our_rom.rtype->emulate();
1325 random_mix_timing_entropy();
1326 time_x = get_utime();
1327 if(amode == ADVANCE_AUTO)
1328 platform::wait(to_wait_frame(get_utime()));
1329 first_round = false;
1330 lua_callback_do_frame();
1332 information_dispatch::do_dump_end();
1333 core_core::uninstall_all_handlers();
1334 voicethread_kill();
1335 platform::system_thread_available(false);
1336 //Kill some things to avoid crashes.
1337 debug_core_change();
1338 project_set(NULL, true);
1339 lsnes_memorywatch.clear_multi(lsnes_memorywatch.enumerate());
1342 void set_stop_at_frame(uint64_t frame)
1344 stop_at_frame = frame;
1345 stop_at_frame_active = (frame != 0);
1346 amode = ADVANCE_AUTO;
1347 platform::set_paused(false);
1350 void do_flush_slotinfo()
1352 flush_slotinfo();
1355 void switch_projects(const std::string& newproj)
1357 pending_new_project = newproj;
1358 amode = ADVANCE_LOAD;
1359 old_mode = ADVANCE_PAUSE;
1360 platform::cancel_wait();
1361 platform::set_paused(false);
1364 void load_new_rom(const romload_request& req)
1366 if(_load_new_rom(req)) {
1367 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1371 void reload_current_rom()
1373 if(reload_active_rom()) {
1374 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1378 void close_rom()
1380 if(load_null_rom()) {
1381 load_paused = true;
1382 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1386 void do_break_pause()
1388 break_pause = true;
1389 update_movie_state();
1390 while(break_pause) {
1391 platform::set_paused(true);
1392 platform::flush_command_queue();
1396 void convert_break_to_pause()
1398 if(break_pause) {
1399 amode = ADVANCE_PAUSE;
1400 break_pause = false;
1401 update_movie_state();