Select slot hotkeys
[lsnes.git] / src / core / mainloop.cpp
blob4a254a2e64b5ff231a44cbfb359c0dacf2685cf2
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/movie.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(!movb)
134 out << "No movie";
135 else if(movb.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 && movb.get_movie().readonly_mode() && pause_on_end &&
222 !stop_at_frame_active) {
223 if(movb.get_movie().get_current_frame() == movb.get_movie().get_frame_count()) {
224 stop_at_frame_active = false;
225 amode = ADVANCE_PAUSE;
226 platform::set_paused(true);
228 } else if(amode == ADVANCE_AUTO && stop_at_frame_active) {
229 if(movb.get_movie().get_current_frame() >= stop_at_frame) {
230 stop_at_frame_active = false;
231 amode = ADVANCE_PAUSE;
232 platform::set_paused(true);
234 } else {
235 platform::set_paused((amode == ADVANCE_PAUSE));
236 cancel_advance = false;
238 location_special = SPECIAL_FRAME_START;
239 update_movie_state();
241 platform::flush_command_queue();
242 controller_frame tmp = controls.get(movb.get_movie().get_current_frame());
243 our_rom.rtype->pre_emulate_frame(tmp); //Preset controls, the lua will override if needed.
244 lua_callback_do_input(tmp, subframe);
245 multitrack_editor.process_frame(tmp);
246 controls.commit(tmp);
247 return tmp;
250 namespace
253 //Do pending load (automatically unpauses).
254 void mark_pending_load(std::string filename, int lmode)
256 if(break_pause)
257 break_pause = false;
258 loadmode = lmode;
259 pending_load = filename;
260 old_mode = amode;
261 amode = ADVANCE_LOAD;
262 platform::cancel_wait();
263 platform::set_paused(false);
266 void mark_pending_save(std::string filename, int smode, int binary)
268 int tmp = -1;
269 if(smode == SAVE_MOVIE) {
270 //Just do this immediately.
271 do_save_movie(filename, binary);
272 flush_slotinfo(translate_name_mprefix(filename, tmp, -1));
273 return;
275 if(location_special == SPECIAL_SAVEPOINT) {
276 //We can save immediately here.
277 do_save_state(filename, binary);
278 flush_slotinfo(translate_name_mprefix(filename, tmp, -1));
279 return;
281 queued_saves.insert(std::make_pair(filename, binary));
282 messages << "Pending save on '" << filename << "'" << std::endl;
285 struct jukebox_size_listener : public settingvar::listener
287 jukebox_size_listener() { lsnes_vset.add_listener(*this); }
288 ~jukebox_size_listener() throw() {lsnes_vset.remove_listener(*this); };
289 void on_setting_change(settingvar::group& grp, const settingvar::base& val)
291 if(val.get_iname() == "jukebox-size") {
292 if(save_jukebox_pointer >= (size_t)jukebox_size)
293 save_jukebox_pointer = 0;
295 update_movie_state();
300 void update_movie_state()
302 auto p = project_get();
303 bool readonly = false;
305 uint64_t magic[4];
306 our_rom.region->fill_framerate_magic(magic);
307 if(movb)
308 voice_frame_number(movb.get_movie().get_current_frame(), 1.0 * magic[1] / magic[0]);
309 else
310 voice_frame_number(0, 60.0); //Default.
312 auto& _status = lsnes_status.get_write();
313 try {
314 if(movb && !system_corrupt) {
315 _status.movie_valid = true;
316 _status.curframe = movb.get_movie().get_current_frame();
317 _status.length = movb.get_movie().get_frame_count();
318 _status.lag = movb.get_movie().get_lag_frames();
319 if(location_special == SPECIAL_FRAME_START)
320 _status.subframe = 0;
321 else if(location_special == SPECIAL_SAVEPOINT)
322 _status.subframe = _lsnes_status::subframe_savepoint;
323 else if(location_special == SPECIAL_FRAME_VIDEO)
324 _status.subframe = _lsnes_status::subframe_video;
325 else
326 _status.subframe = movb.get_movie().next_poll_number();
327 } else {
328 _status.movie_valid = false;
329 _status.curframe = 0;
330 _status.length = 0;
331 _status.lag = 0;
332 _status.subframe = 0;
334 _status.dumping = (information_dispatch::get_dumper_count() > 0);
335 if(break_pause)
336 _status.pause = _lsnes_status::pause_break;
337 else if(amode == ADVANCE_PAUSE)
338 _status.pause = _lsnes_status::pause_normal;
339 else
340 _status.pause = _lsnes_status::pause_none;
341 if(movb) {
342 auto& mo = movb.get_movie();
343 readonly = mo.readonly_mode();
344 if(system_corrupt)
345 _status.mode = 'C';
346 else if(!readonly)
347 _status.mode = 'R';
348 else if(mo.get_frame_count() >= mo.get_current_frame())
349 _status.mode = 'P';
350 else
351 _status.mode = 'F';
353 if(jukebox_size > 0) {
354 _status.saveslot_valid = true;
355 int tmp = -1;
356 std::string sfilen = translate_name_mprefix(save_jukebox_name(save_jukebox_pointer), tmp, -1);
357 _status.saveslot = save_jukebox_pointer + 1;
358 _status.slotinfo = utf8::to32(get_slotinfo(sfilen));
359 } else {
360 _status.saveslot_valid = false;
362 _status.branch_valid = (p != NULL);
363 if(p) _status.branch = utf8::to32(p->get_branch_string());
365 std::string cur_branch = movb ? movb.get_mfile().current_branch() : "";
366 _status.mbranch_valid = (cur_branch != "");
367 _status.mbranch = utf8::to32(cur_branch);
369 _status.speed = (unsigned)(100 * get_realized_multiplier() + 0.5);
371 if(movb && !system_corrupt) {
372 time_t timevalue = static_cast<time_t>(movb.get_mfile().rtc_second);
373 struct tm* time_decompose = gmtime(&timevalue);
374 char datebuffer[512];
375 strftime(datebuffer, 511, "%Y%m%d(%a)T%H%M%S", time_decompose);
376 _status.rtc = utf8::to32(datebuffer);
377 _status.rtc_valid = true;
378 } else {
379 _status.rtc_valid = false;
382 auto mset = controls.active_macro_set();
383 bool mfirst = true;
384 std::ostringstream mss;
385 for(auto i: mset) {
386 if(!mfirst) mss << ",";
387 mss << i;
388 mfirst = false;
390 _status.macros = utf8::to32(mss.str());
392 controller_frame c;
393 if(!multitrack_editor.any_records())
394 c = movb.get_movie().get_controls();
395 else
396 c = controls.get_committed();
397 _status.inputs.clear();
398 for(unsigned i = 0;; i++) {
399 auto pindex = controls.lcid_to_pcid(i);
400 if(pindex.first < 0 || !controls.is_present(pindex.first, pindex.second))
401 break;
402 char32_t buffer[MAX_DISPLAY_LENGTH];
403 c.display(pindex.first, pindex.second, buffer);
404 std::u32string _buffer = buffer;
405 if(readonly && multitrack_editor.is_enabled()) {
406 multitrack_edit::state st = multitrack_editor.get(pindex.first, pindex.second);
407 if(st == multitrack_edit::MT_PRESERVE)
408 _buffer += U" (keep)";
409 else if(st == multitrack_edit::MT_OVERWRITE)
410 _buffer += U" (rewrite)";
411 else if(st == multitrack_edit::MT_OR)
412 _buffer += U" (OR)";
413 else if(st == multitrack_edit::MT_XOR)
414 _buffer += U" (XOR)";
415 else
416 _buffer += U" (\?\?\?)";
418 _status.inputs.push_back(_buffer);
420 //Lua variables.
421 _status.lvars = get_lua_watch_vars();
422 //Memory watches.
423 _status.mvars = lsnes_memorywatch.get_window_vars();
425 _status.valid = true;
426 } catch(...) {
428 lsnes_status.put_write();
429 notify_status_update();
432 uint64_t audio_irq_time;
433 uint64_t controller_irq_time;
434 uint64_t frame_irq_time;
437 struct lsnes_callbacks : public emucore_callbacks
439 public:
440 ~lsnes_callbacks() throw()
444 int16_t get_input(unsigned port, unsigned index, unsigned control)
446 int16_t x;
447 x = movb.input_poll(port, index, control);
448 lua_callback_snoop_input(port, index, control, x);
449 return x;
452 int16_t set_input(unsigned port, unsigned index, unsigned control, int16_t value)
454 if(!movb.get_movie().readonly_mode()) {
455 controller_frame f = movb.get_movie().get_controls();
456 f.axis3(port, index, control, value);
457 movb.get_movie().set_controls(f);
459 return movb.get_movie().next_input(port, index, control);
462 void notify_latch(std::list<std::string>& args)
464 lua_callback_do_latch(args);
467 void timer_tick(uint32_t increment, uint32_t per_second)
469 if(!movb)
470 return;
471 auto& m = movb.get_mfile();
472 m.rtc_subsecond += increment;
473 while(m.rtc_subsecond >= per_second) {
474 m.rtc_second++;
475 m.rtc_subsecond -= per_second;
479 std::string get_firmware_path()
481 return lsnes_vset["firmwarepath"].str();
484 std::string get_base_path()
486 return our_rom.msu1_base;
489 time_t get_time()
491 return movb ? movb.get_mfile().rtc_second : 0;
494 time_t get_randomseed()
496 return random_seed_value;
499 void output_frame(framebuffer::raw& screen, uint32_t fps_n, uint32_t fps_d)
501 lua_callback_do_frame_emulated();
502 location_special = SPECIAL_FRAME_VIDEO;
503 redraw_framebuffer(screen, false, true);
504 uint32_t g = gcd(fps_n, fps_d);
505 fps_n /= g;
506 fps_d /= g;
507 information_dispatch::do_frame(screen, fps_n, fps_d);
510 void action_state_updated()
512 graphics_driver_action_updated();
515 void memory_read(uint64_t addr, uint64_t value)
517 debug_fire_callback_read(addr, value);
520 void memory_write(uint64_t addr, uint64_t value)
522 debug_fire_callback_write(addr, value);
525 void memory_execute(uint64_t addr, uint64_t proc)
527 debug_fire_callback_exec(addr, proc);
530 void memory_trace(uint64_t proc, const char* str, bool insn)
532 debug_fire_callback_trace(proc, str, insn);
536 namespace
538 lsnes_callbacks lsnes_callbacks_obj;
540 command::fnptr<const std::string&> test4(lsnes_cmd, "test4", "test", "test",
541 [](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
542 std::list<std::string> _args;
543 std::string args2 = args;
544 for(auto& sym : token_iterator_foreach(args, {" ", "\t"}))
545 _args.push_back(sym);
546 lua_callback_do_latch(_args);
548 command::fnptr<> count_rerecords(lsnes_cmd, "count-rerecords", "Count rerecords",
549 "Syntax: count-rerecords\nCounts rerecords.\n",
550 []() throw(std::bad_alloc, std::runtime_error) {
551 std::vector<char> tmp;
552 uint64_t x = movb.get_rrdata().write(tmp);
553 messages << x << " rerecord(s)" << std::endl;
556 command::fnptr<const std::string&> quit_emulator(lsnes_cmd, "quit-emulator", "Quit the emulator",
557 "Syntax: quit-emulator [/y]\nQuits emulator (/y => don't ask for confirmation).\n",
558 [](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
559 amode = ADVANCE_QUIT;
560 platform::set_paused(false);
561 platform::cancel_wait();
564 command::fnptr<> unpause_emulator(lsnes_cmd, "unpause-emulator", "Unpause the emulator",
565 "Syntax: unpause-emulator\nUnpauses the emulator.\n",
566 []() throw(std::bad_alloc, std::runtime_error) {
567 break_pause = false;
568 amode = ADVANCE_AUTO;
569 platform::set_paused(false);
570 platform::cancel_wait();
573 command::fnptr<> pause_emulator(lsnes_cmd, "pause-emulator", "(Un)pause the emulator",
574 "Syntax: pause-emulator\n(Un)pauses the emulator.\n",
575 []() throw(std::bad_alloc, std::runtime_error) {
576 if(amode != ADVANCE_AUTO || break_pause) {
577 break_pause = false;
578 amode = ADVANCE_AUTO;
579 platform::set_paused(false);
580 platform::cancel_wait();
581 } else {
582 platform::cancel_wait();
583 cancel_advance = false;
584 stop_at_frame_active = false;
585 amode = ADVANCE_PAUSE;
589 command::fnptr<> save_jukebox_prev(lsnes_cmd, "cycle-jukebox-backward", "Cycle save jukebox backwards",
590 "Syntax: cycle-jukebox-backward\nCycle save jukebox backwards\n",
591 []() throw(std::bad_alloc, std::runtime_error) {
592 if(jukebox_size == 0)
593 return;
594 if(save_jukebox_pointer == 0)
595 save_jukebox_pointer = jukebox_size - 1;
596 else
597 save_jukebox_pointer--;
598 if(save_jukebox_pointer >= (size_t)jukebox_size)
599 save_jukebox_pointer = 0;
600 update_movie_state();
603 command::fnptr<> save_jukebox_next(lsnes_cmd, "cycle-jukebox-forward", "Cycle save jukebox forwards",
604 "Syntax: cycle-jukebox-forward\nCycle save jukebox forwards\n",
605 []() throw(std::bad_alloc, std::runtime_error) {
606 if(jukebox_size == 0)
607 return;
608 if(save_jukebox_pointer + 1 >= (size_t)jukebox_size)
609 save_jukebox_pointer = 0;
610 else
611 save_jukebox_pointer++;
612 if(save_jukebox_pointer >= (size_t)jukebox_size)
613 save_jukebox_pointer = 0;
614 update_movie_state();
617 command::fnptr<const std::string&> save_jukebox_set(lsnes_cmd, "set-jukebox-slot", "Set jukebox slot",
618 "Syntax: set-jukebox-slot\nSet jukebox slot\n", [](const std::string& args)
619 throw(std::bad_alloc, std::runtime_error) {
620 if(!regex_match("[1-9][0-9]{0,8}", args))
621 throw std::runtime_error("Bad slot number");
622 uint32_t slot = parse_value<uint32_t>(args);
623 if(slot >= (size_t)jukebox_size)
624 throw std::runtime_error("Bad slot number");
625 save_jukebox_pointer = slot - 1;
626 update_movie_state();
629 command::fnptr<> load_jukebox(lsnes_cmd, "load-jukebox", "Load save from jukebox",
630 "Syntax: load-jukebox\nLoad save from jukebox\n",
631 []() throw(std::bad_alloc, std::runtime_error) {
632 if(jukebox_size == 0)
633 throw std::runtime_error("No slot selected");
634 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_CURRENT);
637 command::fnptr<> load_jukebox_readwrite(lsnes_cmd, "load-jukebox-readwrite", "Load save from jukebox in"
638 " read-write mode", "Syntax: load-jukebox-readwrite\nLoad save from jukebox in read-write mode\n",
639 []() throw(std::bad_alloc, std::runtime_error) {
640 if(jukebox_size == 0)
641 throw std::runtime_error("No slot selected");
642 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_RW);
645 command::fnptr<> load_jukebox_readonly(lsnes_cmd, "load-jukebox-readonly", "Load save from jukebox in "
646 "read-only mode", "Syntax: load-jukebox-readonly\nLoad save from jukebox in read-only mode\n",
647 []() throw(std::bad_alloc, std::runtime_error) {
648 if(jukebox_size == 0)
649 throw std::runtime_error("No slot selected");
650 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_RO);
653 command::fnptr<> load_jukebox_preserve(lsnes_cmd, "load-jukebox-preserve", "Load save from jukebox, "
654 "preserving input", "Syntax: load-jukebox-preserve\nLoad save from jukebox, preserving input\n",
655 []() throw(std::bad_alloc, std::runtime_error) {
656 if(jukebox_size == 0)
657 throw std::runtime_error("No slot selected");
658 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_PRESERVE);
661 command::fnptr<> load_jukebox_movie(lsnes_cmd, "load-jukebox-movie", "Load save from jukebox as movie",
662 "Syntax: load-jukebox-movie\nLoad save from jukebox as movie\n",
663 []() throw(std::bad_alloc, std::runtime_error) {
664 if(jukebox_size == 0)
665 throw std::runtime_error("No slot selected");
666 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_MOVIE);
669 command::fnptr<> save_jukebox_c(lsnes_cmd, "save-jukebox", "Save save to jukebox",
670 "Syntax: save-jukebox\nSave save to jukebox\n",
671 []() throw(std::bad_alloc, std::runtime_error) {
672 if(jukebox_size == 0)
673 throw std::runtime_error("No slot selected");
674 mark_pending_save(save_jukebox_name(save_jukebox_pointer), SAVE_STATE, -1);
677 command::fnptr<> padvance_frame(lsnes_cmd, "+advance-frame", "Advance one frame",
678 "Syntax: +advance-frame\nAdvances the emulation by one frame.\n",
679 []() throw(std::bad_alloc, std::runtime_error) {
680 if(break_pause)
681 break_pause = false;
682 amode = ADVANCE_FRAME;
683 cancel_advance = false;
684 advanced_once = false;
685 platform::cancel_wait();
686 platform::set_paused(false);
689 command::fnptr<> nadvance_frame(lsnes_cmd, "-advance-frame", "Advance one frame",
690 "No help available\n",
691 []() throw(std::bad_alloc, std::runtime_error) {
692 cancel_advance = true;
693 platform::cancel_wait();
694 platform::set_paused(false);
697 command::fnptr<> padvance_poll(lsnes_cmd, "+advance-poll", "Advance one subframe",
698 "Syntax: +advance-poll\nAdvances the emulation by one subframe.\n",
699 []() throw(std::bad_alloc, std::runtime_error) {
700 if(break_pause)
701 break_pause = false;
702 amode = ADVANCE_SUBFRAME;
703 cancel_advance = false;
704 advanced_once = false;
705 platform::cancel_wait();
706 platform::set_paused(false);
709 command::fnptr<> nadvance_poll(lsnes_cmd, "-advance-poll", "Advance one subframe",
710 "No help available\n",
711 []() throw(std::bad_alloc, std::runtime_error) {
712 if(break_pause)
713 break_pause = false;
714 cancel_advance = true;
715 platform::cancel_wait();
716 platform::set_paused(false);
719 command::fnptr<> advance_skiplag(lsnes_cmd, "advance-skiplag", "Skip to next poll",
720 "Syntax: advance-skiplag\nAdvances the emulation to the next poll.\n",
721 []() throw(std::bad_alloc, std::runtime_error) {
722 if(break_pause)
723 break_pause = false;
724 amode = ADVANCE_SKIPLAG_PENDING;
725 platform::cancel_wait();
726 platform::set_paused(false);
729 command::fnptr<> reset_c(lsnes_cmd, "reset", "Reset the system",
730 "Syntax: reset\nReset\nResets the system in beginning of the next frame.\n",
731 []() throw(std::bad_alloc, std::runtime_error) {
732 int sreset_action = our_rom.rtype->reset_action(false);
733 if(sreset_action < 0) {
734 platform::error_message("Core does not support resets");
735 messages << "Emulator core does not support resets" << std::endl;
736 return;
738 our_rom.rtype->execute_action(sreset_action, std::vector<interface_action_paramval>());
741 command::fnptr<> hreset_c(lsnes_cmd, "reset-hard", "Reset the system",
742 "Syntax: reset-hard\nReset-hard\nHard resets the system in beginning of the next frame.\n",
743 []() throw(std::bad_alloc, std::runtime_error) {
744 int hreset_action = our_rom.rtype->reset_action(true);
745 if(hreset_action < 0) {
746 platform::error_message("Core does not support hard resets");
747 messages << "Emulator core does not support hard resets" << std::endl;
748 return;
750 our_rom.rtype->execute_action(hreset_action, std::vector<interface_action_paramval>());
753 command::fnptr<command::arg_filename> load_c(lsnes_cmd, "load", "Load savestate (current mode)",
754 "Syntax: load <file>\nLoads SNES state from <file> in current mode\n",
755 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
756 mark_pending_load(args, LOAD_STATE_CURRENT);
759 command::fnptr<command::arg_filename> load_smart_c(lsnes_cmd, "load-smart", "Load savestate (heuristic mode)",
760 "Syntax: load <file>\nLoads SNES state from <file> in heuristic mode\n",
761 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
762 mark_pending_load(args, LOAD_STATE_DEFAULT);
765 command::fnptr<command::arg_filename> load_state_c(lsnes_cmd, "load-state", "Load savestate (R/W)",
766 "Syntax: load-state <file>\nLoads SNES state from <file> in Read/Write mode\n",
767 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
768 mark_pending_load(args, LOAD_STATE_RW);
771 command::fnptr<command::arg_filename> load_readonly(lsnes_cmd, "load-readonly", "Load savestate (RO)",
772 "Syntax: load-readonly <file>\nLoads SNES state from <file> in read-only mode\n",
773 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
774 mark_pending_load(args, LOAD_STATE_RO);
777 command::fnptr<command::arg_filename> load_preserve(lsnes_cmd, "load-preserve", "Load savestate (preserve "
778 "input)", "Syntax: load-preserve <file>\nLoads SNES state from <file> preserving input\n",
779 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
780 mark_pending_load(args, LOAD_STATE_PRESERVE);
783 command::fnptr<command::arg_filename> load_movie_c(lsnes_cmd, "load-movie", "Load movie",
784 "Syntax: load-movie <file>\nLoads SNES movie from <file>\n",
785 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
786 mark_pending_load(args, LOAD_STATE_MOVIE);
789 command::fnptr<command::arg_filename> load_allbr_c(lsnes_cmd, "load-allbranches", "Load savestate "
790 "(all branches)", "Syntax: load-allbranches <file>\nLoads SNES state from <file> with all "
791 "branches\n",
792 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
793 mark_pending_load(args, LOAD_STATE_ALLBRANCH);
796 command::fnptr<command::arg_filename> save_state(lsnes_cmd, "save-state", "Save state",
797 "Syntax: save-state <file>\nSaves SNES state to <file>\n",
798 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
799 mark_pending_save(args, SAVE_STATE, -1);
802 command::fnptr<command::arg_filename> save_state2(lsnes_cmd, "save-state-binary", "Save state (binary)",
803 "Syntax: save-state-binary <file>\nSaves binary state to <file>\n",
804 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
805 mark_pending_save(args, SAVE_STATE, 1);
808 command::fnptr<command::arg_filename> save_state3(lsnes_cmd, "save-state-zip", "Save state (zip)",
809 "Syntax: save-state-zip <file>\nSaves zip state to <file>\n",
810 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
811 mark_pending_save(args, SAVE_STATE, 0);
814 command::fnptr<command::arg_filename> save_movie(lsnes_cmd, "save-movie", "Save movie",
815 "Syntax: save-movie <file>\nSaves SNES movie to <file>\n",
816 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
817 mark_pending_save(args, SAVE_MOVIE, -1);
820 command::fnptr<command::arg_filename> save_movie2(lsnes_cmd, "save-movie-binary", "Save movie (binary)",
821 "Syntax: save-movie-binary <file>\nSaves binary movie to <file>\n",
822 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
823 mark_pending_save(args, SAVE_MOVIE, 1);
826 command::fnptr<command::arg_filename> save_movie3(lsnes_cmd, "save-movie-zip", "Save movie (zip)",
827 "Syntax: save-movie-zip <file>\nSaves zip movie to <file>\n",
828 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
829 mark_pending_save(args, SAVE_MOVIE, 0);
832 command::fnptr<> set_rwmode(lsnes_cmd, "set-rwmode", "Switch to read/write mode",
833 "Syntax: set-rwmode\nSwitches to read/write mode\n",
834 []() throw(std::bad_alloc, std::runtime_error) {
835 lua_callback_movie_lost("readwrite");
836 movb.get_movie().readonly_mode(false);
837 notify_mode_change(false);
838 lua_callback_do_readwrite();
839 update_movie_state();
842 command::fnptr<> set_romode(lsnes_cmd, "set-romode", "Switch to read-only mode",
843 "Syntax: set-romode\nSwitches to read-only mode\n",
844 []() throw(std::bad_alloc, std::runtime_error) {
845 movb.get_movie().readonly_mode(true);
846 notify_mode_change(true);
847 update_movie_state();
850 command::fnptr<> toggle_rwmode(lsnes_cmd, "toggle-rwmode", "Toggle read/write mode",
851 "Syntax: toggle-rwmode\nToggles read/write mode\n",
852 []() throw(std::bad_alloc, std::runtime_error) {
853 bool c = movb.get_movie().readonly_mode();
854 if(c)
855 lua_callback_movie_lost("readwrite");
856 movb.get_movie().readonly_mode(!c);
857 notify_mode_change(!c);
858 if(c)
859 lua_callback_do_readwrite();
860 update_movie_state();
863 command::fnptr<> repaint(lsnes_cmd, "repaint", "Redraw the screen",
864 "Syntax: repaint\nRedraws the screen\n",
865 []() throw(std::bad_alloc, std::runtime_error) {
866 redraw_framebuffer();
869 command::fnptr<> tpon(lsnes_cmd, "toggle-pause-on-end", "Toggle pause on end", "Toggle pause on end\n",
870 []() throw(std::bad_alloc, std::runtime_error) {
871 bool tmp = pause_on_end;
872 pause_on_end.set(!tmp);
873 messages << "Pause-on-end is now " << (tmp ? "OFF" : "ON") << std::endl;
876 command::fnptr<> spon(lsnes_cmd, "set-pause-on-end", "Set pause on end", "Set pause on end\n",
877 []() throw(std::bad_alloc, std::runtime_error) {
878 pause_on_end.set(true);
879 messages << "Pause-on-end is now ON" << std::endl;
882 command::fnptr<> cpon(lsnes_cmd, "clear-pause-on-end", "Clear pause on end", "Clear pause on end\n",
883 []() throw(std::bad_alloc, std::runtime_error) {
884 pause_on_end.set(false);
885 messages << "Pause-on-end is now OFF" << std::endl;
888 command::fnptr<> rewind_movie(lsnes_cmd, "rewind-movie", "Rewind movie to the beginning",
889 "Syntax: rewind-movie\nRewind movie to the beginning\n",
890 []() throw(std::bad_alloc, std::runtime_error) {
891 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_BEGINNING);
894 command::fnptr<> cancel_save(lsnes_cmd, "cancel-saves", "Cancel all pending saves", "Syntax: "
895 "cancel-save\nCancel pending saves\n",
896 []() throw(std::bad_alloc, std::runtime_error) {
897 queued_saves.clear();
898 messages << "Pending saves canceled." << std::endl;
901 command::fnptr<> flushslots(lsnes_cmd, "flush-slotinfo", "Flush slotinfo cache",
902 "Flush slotinfo cache\n",
903 []() throw(std::bad_alloc, std::runtime_error) {
904 flush_slotinfo();
907 command::fnptr<> mhold1(lsnes_cmd, "+hold-macro", "Hold macro (hold)",
908 "Hold macros enable\n", []() throw(std::bad_alloc, std::runtime_error) {
909 macro_hold_1 = true;
912 command::fnptr<> mhold2(lsnes_cmd, "-hold-macro", "Hold macro (hold)",
913 "Hold macros disable\n", []() throw(std::bad_alloc, std::runtime_error) {
914 macro_hold_1 = false;
917 command::fnptr<> mhold3(lsnes_cmd, "hold-macro", "Hold macro (toggle)",
918 "Hold macros toggle\n", []() throw(std::bad_alloc, std::runtime_error) {
919 macro_hold_2 = !macro_hold_2;
920 if(macro_hold_2)
921 messages << "Macros are held for next frame." << std::endl;
922 else
923 messages << "Macros are not held for next frame." << std::endl;
926 keyboard::invbind imhold1(lsnes_mapper, "+hold-macro", "Macro‣Hold all macros");
927 keyboard::invbind imhold2(lsnes_mapper, "hold-macro", "Macro‣Hold all macros (typed)");
928 keyboard::invbind ipause_emulator(lsnes_mapper, "pause-emulator", "Speed‣(Un)pause");
929 keyboard::invbind ijback(lsnes_mapper, "cycle-jukebox-backward", "Slot select‣Cycle backwards");
930 keyboard::invbind ijforward(lsnes_mapper, "cycle-jukebox-forward", "Slot select‣Cycle forwards");
931 keyboard::invbind iloadj(lsnes_mapper, "load-jukebox", "Load‣Selected slot");
932 keyboard::invbind iloadjrw(lsnes_mapper, "load-jukebox-readwrite", "Load‣Selected slot (readwrite mode)");
933 keyboard::invbind iloadjro(lsnes_mapper, "load-jukebox-readonly", "Load‣Selected slot (readonly mode)");
934 keyboard::invbind iloadjp(lsnes_mapper, "load-jukebox-preserve", "Load‣Selected slot (preserve input)");
935 keyboard::invbind iloadjm(lsnes_mapper, "load-jukebox-movie", "Load‣Selected slot (as movie)");
936 keyboard::invbind isavej(lsnes_mapper, "save-jukebox", "Save‣Selected slot");
937 keyboard::invbind iadvframe(lsnes_mapper, "+advance-frame", "Speed‣Advance frame");
938 keyboard::invbind iadvsubframe(lsnes_mapper, "+advance-poll", "Speed‣Advance subframe");
939 keyboard::invbind iskiplag(lsnes_mapper, "advance-skiplag", "Speed‣Advance poll");
940 keyboard::invbind ireset(lsnes_mapper, "reset", "System‣Reset");
941 keyboard::invbind iset_rwmode(lsnes_mapper, "set-rwmode", "Movie‣Switch to read/write");
942 keyboard::invbind itoggle_romode(lsnes_mapper, "set-romode", "Movie‣Switch to read-only");
943 keyboard::invbind itoggle_rwmode(lsnes_mapper, "toggle-rwmode", "Movie‣Toggle read-only");
944 keyboard::invbind irepaint(lsnes_mapper, "repaint", "System‣Repaint screen");
945 keyboard::invbind itogglepause(lsnes_mapper, "toggle-pause-on-end", "Movie‣Toggle pause-on-end");
946 keyboard::invbind irewind_movie(lsnes_mapper, "rewind-movie", "Movie‣Rewind movie");
947 keyboard::invbind icancel_saves(lsnes_mapper, "cancel-saves", "Save‣Cancel pending saves");
948 keyboard::invbind iload1(lsnes_mapper, "load $SLOT:1", "Load‣Slot 1");
949 keyboard::invbind iload2(lsnes_mapper, "load $SLOT:2", "Load‣Slot 2");
950 keyboard::invbind iload3(lsnes_mapper, "load $SLOT:3", "Load‣Slot 3");
951 keyboard::invbind iload4(lsnes_mapper, "load $SLOT:4", "Load‣Slot 4");
952 keyboard::invbind iload5(lsnes_mapper, "load $SLOT:5", "Load‣Slot 5");
953 keyboard::invbind iload6(lsnes_mapper, "load $SLOT:6", "Load‣Slot 6");
954 keyboard::invbind iload7(lsnes_mapper, "load $SLOT:7", "Load‣Slot 7");
955 keyboard::invbind iload8(lsnes_mapper, "load $SLOT:8", "Load‣Slot 8");
956 keyboard::invbind iload9(lsnes_mapper, "load $SLOT:9", "Load‣Slot 9");
957 keyboard::invbind iload10(lsnes_mapper, "load $SLOT:10", "Load‣Slot 10");
958 keyboard::invbind iload11(lsnes_mapper, "load $SLOT:11", "Load‣Slot 11");
959 keyboard::invbind iload12(lsnes_mapper, "load $SLOT:12", "Load‣Slot 12");
960 keyboard::invbind iload13(lsnes_mapper, "load $SLOT:13", "Load‣Slot 13");
961 keyboard::invbind iload14(lsnes_mapper, "load $SLOT:14", "Load‣Slot 14");
962 keyboard::invbind iload15(lsnes_mapper, "load $SLOT:15", "Load‣Slot 15");
963 keyboard::invbind iload16(lsnes_mapper, "load $SLOT:16", "Load‣Slot 16");
964 keyboard::invbind iload17(lsnes_mapper, "load $SLOT:17", "Load‣Slot 17");
965 keyboard::invbind iload18(lsnes_mapper, "load $SLOT:18", "Load‣Slot 18");
966 keyboard::invbind iload19(lsnes_mapper, "load $SLOT:19", "Load‣Slot 19");
967 keyboard::invbind iload20(lsnes_mapper, "load $SLOT:20", "Load‣Slot 20");
968 keyboard::invbind iload21(lsnes_mapper, "load $SLOT:21", "Load‣Slot 21");
969 keyboard::invbind iload22(lsnes_mapper, "load $SLOT:22", "Load‣Slot 22");
970 keyboard::invbind iload23(lsnes_mapper, "load $SLOT:23", "Load‣Slot 23");
971 keyboard::invbind iload24(lsnes_mapper, "load $SLOT:24", "Load‣Slot 24");
972 keyboard::invbind iload25(lsnes_mapper, "load $SLOT:25", "Load‣Slot 25");
973 keyboard::invbind iload26(lsnes_mapper, "load $SLOT:26", "Load‣Slot 26");
974 keyboard::invbind iload27(lsnes_mapper, "load $SLOT:27", "Load‣Slot 27");
975 keyboard::invbind iload28(lsnes_mapper, "load $SLOT:28", "Load‣Slot 28");
976 keyboard::invbind iload29(lsnes_mapper, "load $SLOT:29", "Load‣Slot 29");
977 keyboard::invbind iload30(lsnes_mapper, "load $SLOT:30", "Load‣Slot 30");
978 keyboard::invbind iload31(lsnes_mapper, "load $SLOT:31", "Load‣Slot 31");
979 keyboard::invbind iload32(lsnes_mapper, "load $SLOT:32", "Load‣Slot 32");
980 keyboard::invbind isave1(lsnes_mapper, "save-state $SLOT:1", "Save‣Slot 1");
981 keyboard::invbind isave2(lsnes_mapper, "save-state $SLOT:2", "Save‣Slot 2");
982 keyboard::invbind isave3(lsnes_mapper, "save-state $SLOT:3", "Save‣Slot 3");
983 keyboard::invbind isave4(lsnes_mapper, "save-state $SLOT:4", "Save‣Slot 4");
984 keyboard::invbind isave5(lsnes_mapper, "save-state $SLOT:5", "Save‣Slot 5");
985 keyboard::invbind isave6(lsnes_mapper, "save-state $SLOT:6", "Save‣Slot 6");
986 keyboard::invbind isave7(lsnes_mapper, "save-state $SLOT:7", "Save‣Slot 7");
987 keyboard::invbind isave8(lsnes_mapper, "save-state $SLOT:8", "Save‣Slot 8");
988 keyboard::invbind isave9(lsnes_mapper, "save-state $SLOT:9", "Save‣Slot 9");
989 keyboard::invbind isave10(lsnes_mapper, "save-state $SLOT:10", "Save‣Slot 10");
990 keyboard::invbind isave11(lsnes_mapper, "save-state $SLOT:11", "Save‣Slot 11");
991 keyboard::invbind isave12(lsnes_mapper, "save-state $SLOT:12", "Save‣Slot 12");
992 keyboard::invbind isave13(lsnes_mapper, "save-state $SLOT:13", "Save‣Slot 13");
993 keyboard::invbind isave14(lsnes_mapper, "save-state $SLOT:14", "Save‣Slot 14");
994 keyboard::invbind isave15(lsnes_mapper, "save-state $SLOT:15", "Save‣Slot 15");
995 keyboard::invbind isave16(lsnes_mapper, "save-state $SLOT:16", "Save‣Slot 16");
996 keyboard::invbind isave17(lsnes_mapper, "save-state $SLOT:17", "Save‣Slot 17");
997 keyboard::invbind isave18(lsnes_mapper, "save-state $SLOT:18", "Save‣Slot 18");
998 keyboard::invbind isave19(lsnes_mapper, "save-state $SLOT:19", "Save‣Slot 19");
999 keyboard::invbind isave20(lsnes_mapper, "save-state $SLOT:20", "Save‣Slot 20");
1000 keyboard::invbind isave21(lsnes_mapper, "save-state $SLOT:21", "Save‣Slot 21");
1001 keyboard::invbind isave22(lsnes_mapper, "save-state $SLOT:22", "Save‣Slot 22");
1002 keyboard::invbind isave23(lsnes_mapper, "save-state $SLOT:23", "Save‣Slot 23");
1003 keyboard::invbind isave24(lsnes_mapper, "save-state $SLOT:24", "Save‣Slot 24");
1004 keyboard::invbind isave25(lsnes_mapper, "save-state $SLOT:25", "Save‣Slot 25");
1005 keyboard::invbind isave26(lsnes_mapper, "save-state $SLOT:26", "Save‣Slot 26");
1006 keyboard::invbind isave27(lsnes_mapper, "save-state $SLOT:27", "Save‣Slot 27");
1007 keyboard::invbind isave28(lsnes_mapper, "save-state $SLOT:28", "Save‣Slot 28");
1008 keyboard::invbind isave29(lsnes_mapper, "save-state $SLOT:29", "Save‣Slot 29");
1009 keyboard::invbind isave30(lsnes_mapper, "save-state $SLOT:30", "Save‣Slot 30");
1010 keyboard::invbind isave31(lsnes_mapper, "save-state $SLOT:31", "Save‣Slot 31");
1011 keyboard::invbind isave32(lsnes_mapper, "save-state $SLOT:32", "Save‣Slot 32");
1012 keyboard::invbind islot1(lsnes_mapper, "set-jukebox-slot 1", "Slot select‣Slot 1");
1013 keyboard::invbind islot2(lsnes_mapper, "set-jukebox-slot 2", "Slot select‣Slot 2");
1014 keyboard::invbind islot3(lsnes_mapper, "set-jukebox-slot 3", "Slot select‣Slot 3");
1015 keyboard::invbind islot4(lsnes_mapper, "set-jukebox-slot 4", "Slot select‣Slot 4");
1016 keyboard::invbind islot5(lsnes_mapper, "set-jukebox-slot 5", "Slot select‣Slot 5");
1017 keyboard::invbind islot6(lsnes_mapper, "set-jukebox-slot 6", "Slot select‣Slot 6");
1018 keyboard::invbind islot7(lsnes_mapper, "set-jukebox-slot 7", "Slot select‣Slot 7");
1019 keyboard::invbind islot8(lsnes_mapper, "set-jukebox-slot 8", "Slot select‣Slot 8");
1020 keyboard::invbind islot9(lsnes_mapper, "set-jukebox-slot 9", "Slot select‣Slot 9");
1021 keyboard::invbind islot10(lsnes_mapper, "set-jukebox-slot 10", "Slot select‣Slot 10");
1022 keyboard::invbind islot11(lsnes_mapper, "set-jukebox-slot 11", "Slot select‣Slot 11");
1023 keyboard::invbind islot12(lsnes_mapper, "set-jukebox-slot 12", "Slot select‣Slot 12");
1024 keyboard::invbind islot13(lsnes_mapper, "set-jukebox-slot 13", "Slot select‣Slot 13");
1025 keyboard::invbind islot14(lsnes_mapper, "set-jukebox-slot 14", "Slot select‣Slot 14");
1026 keyboard::invbind islot15(lsnes_mapper, "set-jukebox-slot 15", "Slot select‣Slot 15");
1027 keyboard::invbind islot16(lsnes_mapper, "set-jukebox-slot 16", "Slot select‣Slot 16");
1028 keyboard::invbind islot17(lsnes_mapper, "set-jukebox-slot 17", "Slot select‣Slot 17");
1029 keyboard::invbind islot18(lsnes_mapper, "set-jukebox-slot 18", "Slot select‣Slot 18");
1030 keyboard::invbind islot19(lsnes_mapper, "set-jukebox-slot 19", "Slot select‣Slot 19");
1031 keyboard::invbind islot20(lsnes_mapper, "set-jukebox-slot 20", "Slot select‣Slot 20");
1032 keyboard::invbind islot21(lsnes_mapper, "set-jukebox-slot 21", "Slot select‣Slot 21");
1033 keyboard::invbind islot22(lsnes_mapper, "set-jukebox-slot 22", "Slot select‣Slot 22");
1034 keyboard::invbind islot23(lsnes_mapper, "set-jukebox-slot 23", "Slot select‣Slot 23");
1035 keyboard::invbind islot24(lsnes_mapper, "set-jukebox-slot 24", "Slot select‣Slot 24");
1036 keyboard::invbind islot25(lsnes_mapper, "set-jukebox-slot 25", "Slot select‣Slot 25");
1037 keyboard::invbind islot26(lsnes_mapper, "set-jukebox-slot 26", "Slot select‣Slot 26");
1038 keyboard::invbind islot27(lsnes_mapper, "set-jukebox-slot 27", "Slot select‣Slot 27");
1039 keyboard::invbind islot28(lsnes_mapper, "set-jukebox-slot 28", "Slot select‣Slot 28");
1040 keyboard::invbind islot29(lsnes_mapper, "set-jukebox-slot 29", "Slot select‣Slot 29");
1041 keyboard::invbind islot30(lsnes_mapper, "set-jukebox-slot 30", "Slot select‣Slot 30");
1042 keyboard::invbind islot31(lsnes_mapper, "set-jukebox-slot 31", "Slot select‣Slot 31");
1043 keyboard::invbind islot32(lsnes_mapper, "set-jukebox-slot 32", "Slot select‣Slot 32");
1045 bool on_quit_prompt = false;
1046 class mywindowcallbacks : public information_dispatch
1048 public:
1049 mywindowcallbacks() : information_dispatch("mainloop-window-callbacks")
1051 closenotify.set(notify_close, [this]() {
1052 if(on_quit_prompt) {
1053 amode = ADVANCE_QUIT;
1054 platform::set_paused(false);
1055 platform::cancel_wait();
1056 return;
1058 on_quit_prompt = true;
1059 try {
1060 amode = ADVANCE_QUIT;
1061 platform::set_paused(false);
1062 platform::cancel_wait();
1063 } catch(...) {
1065 on_quit_prompt = false;
1068 ~mywindowcallbacks() throw() {}
1069 void on_new_dumper(const std::string& n)
1071 update_movie_state();
1073 void on_destroy_dumper(const std::string& n)
1075 update_movie_state();
1077 private:
1078 struct dispatch::target<> closenotify;
1079 } mywcb;
1081 //If there is a pending load, perform it. Return 1 on successful load, 0 if nothing to load, -1 on load
1082 //failing.
1083 int handle_load()
1085 std::string old_project = movb ? movb.get_mfile().projectid : "";
1086 jumpback:
1087 if(do_unsafe_rewind && unsafe_rewind_obj) {
1088 if(!movb)
1089 return 0;
1090 uint64_t t = get_utime();
1091 std::vector<char> s;
1092 lua_callback_do_unsafe_rewind(s, 0, 0, movb.get_movie(), unsafe_rewind_obj);
1093 notify_mode_change(false);
1094 do_unsafe_rewind = false;
1095 movb.get_mfile().is_savestate = true;
1096 location_special = SPECIAL_SAVEPOINT;
1097 update_movie_state();
1098 messages << "Rewind done in " << (get_utime() - t) << " usec." << std::endl;
1099 return 1;
1101 if(pending_new_project != "") {
1102 std::string id = pending_new_project;
1103 pending_new_project = "";
1104 project_info* old = project_get();
1105 if(old && old->id == id)
1106 goto nothing_to_do;
1107 try {
1108 auto& p = project_load(id);
1109 project_set(&p);
1110 if(project_get() != old)
1111 delete old;
1112 flush_slotinfo(); //Wrong movie may be stale.
1113 return 1;
1114 } catch(std::exception& e) {
1115 platform::error_message(std::string("Can't switch projects: ") + e.what());
1116 messages << "Can't switch projects: " << e.what() << std::endl;
1117 goto nothing_to_do;
1119 nothing_to_do:
1120 amode = old_mode;
1121 platform::set_paused(amode == ADVANCE_PAUSE);
1122 platform::flush_command_queue();
1123 if(amode == ADVANCE_LOAD)
1124 goto jumpback;
1125 return 0;
1127 if(pending_load != "") {
1128 bool system_was_corrupt = system_corrupt;
1129 system_corrupt = false;
1130 try {
1131 if(loadmode != LOAD_STATE_BEGINNING && loadmode != LOAD_STATE_ROMRELOAD &&
1132 !do_load_state(pending_load, loadmode)) {
1133 if(system_was_corrupt)
1134 system_corrupt = system_was_corrupt;
1135 pending_load = "";
1136 return -1;
1138 if(loadmode == LOAD_STATE_BEGINNING)
1139 do_load_rewind();
1140 if(loadmode == LOAD_STATE_ROMRELOAD)
1141 do_load_rom();
1142 } catch(std::exception& e) {
1143 if(!system_corrupt && system_was_corrupt)
1144 system_corrupt = true;
1145 platform::error_message(std::string("Load failed: ") + e.what());
1146 messages << "Load failed: " << e.what() << std::endl;
1148 pending_load = "";
1149 amode = load_paused ? ADVANCE_PAUSE : ADVANCE_AUTO;
1150 platform::set_paused(load_paused);
1151 load_paused = false;
1152 if(!system_corrupt) {
1153 location_special = SPECIAL_SAVEPOINT;
1154 update_movie_state();
1155 platform::flush_command_queue();
1156 if(amode == ADVANCE_QUIT)
1157 return -1;
1158 if(amode == ADVANCE_LOAD)
1159 goto jumpback;
1161 if(old_project != (movb ? movb.get_mfile().projectid : ""))
1162 flush_slotinfo(); //Wrong movie may be stale.
1163 return 1;
1165 return 0;
1168 //If there are pending saves, perform them.
1169 void handle_saves()
1171 if(!movb)
1172 return;
1173 if(!queued_saves.empty() || (do_unsafe_rewind && !unsafe_rewind_obj)) {
1174 our_rom.rtype->runtosave();
1175 for(auto i : queued_saves) {
1176 do_save_state(i.first, i.second);
1177 int tmp = -1;
1178 flush_slotinfo(translate_name_mprefix(i.first, tmp, -1));
1180 if(do_unsafe_rewind && !unsafe_rewind_obj) {
1181 uint64_t t = get_utime();
1182 std::vector<char> s = our_rom.save_core_state(true);
1183 uint64_t secs = movb.get_mfile().rtc_second;
1184 uint64_t ssecs = movb.get_mfile().rtc_subsecond;
1185 lua_callback_do_unsafe_rewind(s, secs, ssecs, movb.get_movie(), NULL);
1186 do_unsafe_rewind = false;
1187 messages << "Rewind point set in " << (get_utime() - t) << " usec." << std::endl;
1190 queued_saves.clear();
1193 bool handle_corrupt()
1195 if(!system_corrupt)
1196 return false;
1197 while(system_corrupt) {
1198 platform::set_paused(true);
1199 platform::flush_command_queue();
1200 handle_load();
1201 if(amode == ADVANCE_QUIT)
1202 return true;
1204 return true;
1208 void init_main_callbacks()
1210 ecore_callbacks = &lsnes_callbacks_obj;
1213 void main_loop(struct loaded_rom& rom, struct moviefile& initial, bool load_has_to_succeed) throw(std::bad_alloc,
1214 std::runtime_error)
1216 platform::system_thread_available(true);
1217 //Basic initialization.
1218 dispatch_set_error_streams(&messages.getstream());
1219 emulation_thread = threads::this_id();
1220 jukebox_size_listener jlistener;
1221 voicethread_task();
1222 init_special_screens();
1223 our_rom = rom;
1224 init_main_callbacks();
1225 initialize_all_builtin_c_cores();
1226 core_core::install_all_handlers();
1228 //Load our given movie.
1229 bool first_round = false;
1230 bool just_did_loadstate = false;
1231 bool used = false;
1232 try {
1233 do_load_state(initial, LOAD_STATE_INITIAL, used);
1234 location_special = SPECIAL_SAVEPOINT;
1235 update_movie_state();
1236 first_round = movb.get_mfile().is_savestate;
1237 just_did_loadstate = first_round;
1238 } catch(std::bad_alloc& e) {
1239 OOM_panic();
1240 } catch(std::exception& e) {
1241 if(!used)
1242 delete &initial;
1243 platform::error_message(std::string("Can't load initial state: ") + e.what());
1244 messages << "ERROR: Can't load initial state: " << e.what() << std::endl;
1245 if(load_has_to_succeed) {
1246 messages << "FATAL: Can't load movie" << std::endl;
1247 throw;
1249 system_corrupt = true;
1250 redraw_framebuffer(screen_corrupt);
1253 platform::set_paused(initial.start_paused);
1254 amode = initial.start_paused ? ADVANCE_PAUSE : ADVANCE_AUTO;
1255 stop_at_frame_active = false;
1257 lua_run_startup_scripts();
1259 uint64_t time_x = get_utime();
1260 while(amode != ADVANCE_QUIT || !queued_saves.empty()) {
1261 if(handle_corrupt()) {
1262 first_round = movb && movb.get_mfile().is_savestate;
1263 just_did_loadstate = first_round;
1264 continue;
1266 ack_frame_tick(get_utime());
1267 if(amode == ADVANCE_SKIPLAG_PENDING)
1268 amode = ADVANCE_SKIPLAG;
1270 if(!first_round) {
1271 controls.reset_framehold();
1272 movb.get_movie().get_pollcounters().set_framepflag(false);
1273 movb.new_frame_starting(amode == ADVANCE_SKIPLAG);
1274 movb.get_movie().get_pollcounters().set_framepflag(true);
1275 if(!macro_hold_1 && !macro_hold_2) {
1276 controls.advance_macros();
1278 macro_hold_2 = false;
1279 if(amode == ADVANCE_QUIT && queued_saves.empty())
1280 break;
1281 handle_saves();
1282 int r = 0;
1283 if(queued_saves.empty())
1284 r = handle_load();
1285 if(r > 0 || system_corrupt) {
1286 movb.get_movie().get_pollcounters().set_framepflag(movb.get_mfile().is_savestate);
1287 first_round = movb.get_mfile().is_savestate;
1288 if(system_corrupt)
1289 amode = ADVANCE_PAUSE;
1290 else
1291 amode = old_mode;
1292 stop_at_frame_active = false;
1293 just_did_loadstate = first_round;
1294 controls.reset_framehold();
1295 continue;
1296 } else if(r < 0) {
1297 //Not exactly desriable, but this at least won't desync.
1298 stop_at_frame_active = false;
1299 if(amode == ADVANCE_QUIT)
1300 return;
1301 amode = ADVANCE_PAUSE;
1304 if(just_did_loadstate) {
1305 //If we just loadstated, we are up to date.
1306 if(amode == ADVANCE_QUIT)
1307 break;
1308 platform::set_paused(amode == ADVANCE_PAUSE);
1309 platform::flush_command_queue();
1310 //We already have done the reset this frame if we are going to do one at all.
1311 movb.get_movie().set_controls(movb.update_controls(true));
1312 movb.get_movie().set_all_DRDY();
1313 just_did_loadstate = false;
1315 frame_irq_time = get_utime() - time_x;
1316 our_rom.rtype->emulate();
1317 random_mix_timing_entropy();
1318 time_x = get_utime();
1319 if(amode == ADVANCE_AUTO)
1320 platform::wait(to_wait_frame(get_utime()));
1321 first_round = false;
1322 lua_callback_do_frame();
1324 information_dispatch::do_dump_end();
1325 core_core::uninstall_all_handlers();
1326 voicethread_kill();
1327 platform::system_thread_available(false);
1328 //Kill some things to avoid crashes.
1329 debug_core_change();
1330 project_set(NULL, true);
1331 lsnes_memorywatch.clear_multi(lsnes_memorywatch.enumerate());
1334 void set_stop_at_frame(uint64_t frame)
1336 stop_at_frame = frame;
1337 stop_at_frame_active = (frame != 0);
1338 amode = ADVANCE_AUTO;
1339 platform::set_paused(false);
1342 void do_flush_slotinfo()
1344 flush_slotinfo();
1347 void switch_projects(const std::string& newproj)
1349 pending_new_project = newproj;
1350 amode = ADVANCE_LOAD;
1351 old_mode = ADVANCE_PAUSE;
1352 platform::cancel_wait();
1353 platform::set_paused(false);
1356 void load_new_rom(const romload_request& req)
1358 if(_load_new_rom(req)) {
1359 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1363 void reload_current_rom()
1365 if(reload_active_rom()) {
1366 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1370 void close_rom()
1372 if(load_null_rom()) {
1373 load_paused = true;
1374 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1378 void do_break_pause()
1380 break_pause = true;
1381 update_movie_state();
1382 while(break_pause) {
1383 platform::set_paused(true);
1384 platform::flush_command_queue();
1388 void convert_break_to_pause()
1390 if(break_pause) {
1391 amode = ADVANCE_PAUSE;
1392 break_pause = false;
1393 update_movie_state();