Move action_update to be via dispatch
[lsnes.git] / src / core / mainloop.cpp
blob460390719436da534af7ef995ffeb748bb9bf322
1 #include "lsnes.hpp"
3 #include "core/advdumper.hpp"
4 #include "core/command.hpp"
5 #include "core/controller.hpp"
6 #include "core/command.hpp"
7 #include "core/debug.hpp"
8 #include "core/dispatch.hpp"
9 #include "core/emustatus.hpp"
10 #include "core/framebuffer.hpp"
11 #include "core/framerate.hpp"
12 #include "core/inthread.hpp"
13 #include "core/keymapper.hpp"
14 #include "core/multitrack.hpp"
15 #include "lua/lua.hpp"
16 #include "core/mainloop.hpp"
17 #include "core/messages.hpp"
18 #include "core/instance.hpp"
19 #include "core/moviedata.hpp"
20 #include "core/moviefile.hpp"
21 #include "core/memorymanip.hpp"
22 #include "core/memorywatch.hpp"
23 #include "core/project.hpp"
24 #include "core/queue.hpp"
25 #include "core/random.hpp"
26 #include "core/rom.hpp"
27 #include "core/settings.hpp"
28 #include "core/window.hpp"
29 #include "interface/c-interface.hpp"
30 #include "interface/callbacks.hpp"
31 #include "interface/romtype.hpp"
32 #include "library/framebuffer.hpp"
33 #include "library/settingvar.hpp"
34 #include "library/string.hpp"
35 #include "library/zip.hpp"
37 #include <iomanip>
38 #include <cassert>
39 #include <sstream>
40 #include <iostream>
41 #include <limits>
42 #include <set>
43 #include <sys/time.h>
45 #define QUIT_MAGIC 0x5a8c4bef
47 #define SPECIAL_FRAME_START 0
48 #define SPECIAL_FRAME_VIDEO 1
49 #define SPECIAL_SAVEPOINT 2
50 #define SPECIAL_NONE 3
52 void update_movie_state();
53 time_t random_seed_value = 0;
55 settingvar::supervariable<settingvar::model_bool<settingvar::yes_no>> jukebox_dflt_binary(lsnes_setgrp,
56 "jukebox-default-binary", "Movie‣Saving‣Saveslots binary", true);
57 settingvar::supervariable<settingvar::model_bool<settingvar::yes_no>> movie_dflt_binary(lsnes_setgrp,
58 "movie-default-binary", "Movie‣Saving‣Movies binary", false);
59 settingvar::supervariable<settingvar::model_bool<settingvar::yes_no>> save_dflt_binary(lsnes_setgrp,
60 "savestate-default-binary", "Movie‣Saving‣Savestates binary", false);
62 namespace
64 settingvar::supervariable<settingvar::model_int<0,999999>> advance_timeout_first(lsnes_setgrp,
65 "advance-timeout", "Delays‣First frame advance", 500);
66 settingvar::supervariable<settingvar::model_int<0,999999>> advance_timeout_subframe(lsnes_setgrp,
67 "advance-subframe-timeout", "Delays‣Subframe advance", 100);
68 settingvar::supervariable<settingvar::model_bool<settingvar::yes_no>> pause_on_end(lsnes_setgrp,
69 "pause-on-end", "Movie‣Pause on end", false);
70 settingvar::supervariable<settingvar::model_int<0,999999999>> jukebox_size(lsnes_setgrp, "jukebox-size",
71 "Movie‣Number of save slots", 12);
73 enum advance_mode
75 ADVANCE_INVALID, //In case someone trashes this.
76 ADVANCE_QUIT, //Quit the emulator.
77 ADVANCE_AUTO, //Normal (possibly slowed down play).
78 ADVANCE_LOAD, //Loading a state.
79 ADVANCE_FRAME, //Frame advance.
80 ADVANCE_SUBFRAME, //Subframe advance.
81 ADVANCE_SKIPLAG, //Skip lag (oneshot, reverts to normal).
82 ADVANCE_SKIPLAG_PENDING, //Activate skip lag mode at next frame.
83 ADVANCE_PAUSE, //Unconditional pause.
84 ADVANCE_BREAK_PAUSE, //Break pause.
87 //Our thread.
88 threads::id emulation_thread;
89 //Flags related to repeating advance.
90 bool advanced_once;
91 bool cancel_advance;
92 //Emulator advance mode. Detemines pauses at start of frame / subframe, etc..
93 enum advance_mode amode;
94 enum advance_mode old_mode;
95 //Mode and filename of pending load, one of LOAD_* constants.
96 bool load_paused;
97 int loadmode;
98 std::string pending_load;
99 std::string pending_new_project;
100 //Queued saves (all savestates).
101 std::set<std::pair<std::string, int>> queued_saves;
102 //Save jukebox.
103 size_t save_jukebox_pointer;
104 //Special subframe location. One of SPECIAL_* constants.
105 int location_special;
106 //Unsafe rewind.
107 bool do_unsafe_rewind = false;
108 void* unsafe_rewind_obj = NULL;
109 //Stop at frame.
110 bool stop_at_frame_active = false;
111 uint64_t stop_at_frame = 0;
112 //Macro hold.
113 bool macro_hold_1;
114 bool macro_hold_2;
115 //Quit magic.
116 unsigned quit_magic;
118 bool is_quitting()
120 if(amode == ADVANCE_QUIT && quit_magic == QUIT_MAGIC)
121 return true;
122 if(amode == ADVANCE_INVALID || (amode == ADVANCE_QUIT && quit_magic != QUIT_MAGIC) ||
123 amode > ADVANCE_BREAK_PAUSE) {
124 //Ouch.
125 if(lsnes_instance.mlogic)
126 emerg_save_movie(lsnes_instance.mlogic->get_mfile(),
127 lsnes_instance.mlogic->get_rrdata());
128 messages << "WARNING: Emulator runmode undefined, invoked movie dump." << std::endl;
129 amode = ADVANCE_PAUSE;
131 return false;
134 std::string save_jukebox_name(size_t i)
136 return (stringfmt() << "$SLOT:" << (i + 1)).str();
139 std::map<std::string, std::string> slotinfo_cache;
141 std::string vector_to_string(const std::vector<char>& x)
143 std::string y(x.begin(), x.end());
144 while(y.length() > 0 && y[y.length() - 1] < 32)
145 y = y.substr(0, y.length() - 1);
146 return y;
149 std::string get_slotinfo(const std::string& _filename)
151 std::string filename = resolve_relative_path(_filename);
152 if(!slotinfo_cache.count(filename)) {
153 std::ostringstream out;
154 try {
155 moviefile::brief_info info(filename);
156 if(!*CORE().mlogic)
157 out << "No movie";
158 else if(CORE().mlogic->get_mfile().projectid == info.projectid)
159 out << info.rerecords << "R/" << info.current_frame << "F";
160 else
161 out << "Wrong movie";
162 } catch(...) {
163 out << "Nonexistent";
165 slotinfo_cache[filename] = out.str();
167 return slotinfo_cache[filename];
170 void flush_slotinfo(const std::string& filename)
172 slotinfo_cache.erase(resolve_relative_path(filename));
175 void flush_slotinfo()
177 slotinfo_cache.clear();
181 void mainloop_signal_need_rewind(void* ptr)
183 if(ptr) {
184 old_mode = amode;
185 amode = ADVANCE_LOAD;
187 do_unsafe_rewind = true;
188 unsafe_rewind_obj = ptr;
191 controller_frame movie_logic::update_controls(bool subframe) throw(std::bad_alloc, std::runtime_error)
193 if(lua_requests_subframe_paint)
194 CORE().fbuf->redraw_framebuffer();
196 if(subframe) {
197 if(amode == ADVANCE_SUBFRAME) {
198 if(!cancel_advance) {
199 if(!advanced_once)
200 platform::wait(advance_timeout_first(*CORE().settings) * 1000);
201 else
202 platform::wait(advance_timeout_subframe(*CORE().settings) * 1000);
203 advanced_once = true;
205 if(cancel_advance) {
206 stop_at_frame_active = false;
207 amode = ADVANCE_PAUSE;
208 cancel_advance = false;
210 platform::set_paused(amode == ADVANCE_PAUSE);
211 } else if(amode == ADVANCE_FRAME) {
213 } else {
214 if(amode == ADVANCE_SKIPLAG) {
215 stop_at_frame_active = false;
216 amode = ADVANCE_PAUSE;
218 platform::set_paused(amode == ADVANCE_PAUSE);
219 cancel_advance = false;
221 location_special = SPECIAL_NONE;
222 update_movie_state();
223 } else {
224 if(amode == ADVANCE_SKIPLAG_PENDING)
225 amode = ADVANCE_SKIPLAG;
226 if(amode == ADVANCE_FRAME || amode == ADVANCE_SUBFRAME) {
227 if(!cancel_advance) {
228 uint64_t wait = 0;
229 if(!advanced_once)
230 wait = advance_timeout_first(*CORE().settings) * 1000;
231 else if(amode == ADVANCE_SUBFRAME)
232 wait = advance_timeout_subframe(*CORE().settings) * 1000;
233 else
234 wait = CORE().framerate->to_wait_frame(framerate_regulator::get_utime());
235 platform::wait(wait);
236 advanced_once = true;
238 if(cancel_advance) {
239 stop_at_frame_active = false;
240 amode = ADVANCE_PAUSE;
241 cancel_advance = false;
243 platform::set_paused(amode == ADVANCE_PAUSE);
244 } else if(amode == ADVANCE_AUTO && CORE().mlogic->get_movie().readonly_mode() &&
245 pause_on_end(*CORE().settings) && !stop_at_frame_active) {
246 if(CORE().mlogic->get_movie().get_current_frame() ==
247 CORE().mlogic->get_movie().get_frame_count()) {
248 stop_at_frame_active = false;
249 amode = ADVANCE_PAUSE;
250 platform::set_paused(true);
252 } else if(amode == ADVANCE_AUTO && stop_at_frame_active) {
253 if(CORE().mlogic->get_movie().get_current_frame() >= stop_at_frame) {
254 stop_at_frame_active = false;
255 amode = ADVANCE_PAUSE;
256 platform::set_paused(true);
258 } else {
259 platform::set_paused((amode == ADVANCE_PAUSE));
260 cancel_advance = false;
262 location_special = SPECIAL_FRAME_START;
263 update_movie_state();
265 platform::flush_command_queue();
266 controller_frame tmp = CORE().controls->get(CORE().mlogic->get_movie().get_current_frame());
267 our_rom.rtype->pre_emulate_frame(tmp); //Preset controls, the lua will override if needed.
268 lua_callback_do_input(tmp, subframe);
269 CORE().mteditor->process_frame(tmp);
270 CORE().controls->commit(tmp);
271 return tmp;
274 namespace
277 //Do pending load (automatically unpauses).
278 void mark_pending_load(std::string filename, int lmode)
280 //Convert break pause to ordinary pause.
281 if(amode == ADVANCE_BREAK_PAUSE)
282 amode = ADVANCE_PAUSE;
283 loadmode = lmode;
284 pending_load = filename;
285 old_mode = amode;
286 amode = ADVANCE_LOAD;
287 platform::cancel_wait();
288 platform::set_paused(false);
291 void mark_pending_save(std::string filename, int smode, int binary)
293 int tmp = -1;
294 if(smode == SAVE_MOVIE) {
295 //Just do this immediately.
296 do_save_movie(filename, binary);
297 flush_slotinfo(translate_name_mprefix(filename, tmp, -1));
298 return;
300 if(location_special == SPECIAL_SAVEPOINT) {
301 //We can save immediately here.
302 do_save_state(filename, binary);
303 flush_slotinfo(translate_name_mprefix(filename, tmp, -1));
304 return;
306 queued_saves.insert(std::make_pair(filename, binary));
307 messages << "Pending save on '" << filename << "'" << std::endl;
310 struct jukebox_size_listener : public settingvar::listener
312 jukebox_size_listener(settingvar::group& _grp) : grp(_grp) { grp.add_listener(*this); }
313 ~jukebox_size_listener() throw() { grp.remove_listener(*this); };
314 void on_setting_change(settingvar::group& _grp, const settingvar::base& val)
316 if(val.get_iname() == "jukebox-size") {
317 if(save_jukebox_pointer >= (size_t)jukebox_size(_grp))
318 save_jukebox_pointer = 0;
320 update_movie_state();
322 private:
323 settingvar::group& grp;
327 void update_movie_state()
329 auto p = CORE().project->get();
330 bool readonly = false;
332 uint64_t magic[4];
333 our_rom.region->fill_framerate_magic(magic);
334 if(*CORE().mlogic)
335 CORE().commentary->frame_number(CORE().mlogic->get_movie().get_current_frame(),
336 1.0 * magic[1] / magic[0]);
337 else
338 CORE().commentary->frame_number(0, 60.0); //Default.
340 auto& _status = CORE().status->get_write();
341 try {
342 if(*CORE().mlogic && !system_corrupt) {
343 _status.movie_valid = true;
344 _status.curframe = CORE().mlogic->get_movie().get_current_frame();
345 _status.length = CORE().mlogic->get_movie().get_frame_count();
346 _status.lag = CORE().mlogic->get_movie().get_lag_frames();
347 if(location_special == SPECIAL_FRAME_START)
348 _status.subframe = 0;
349 else if(location_special == SPECIAL_SAVEPOINT)
350 _status.subframe = _lsnes_status::subframe_savepoint;
351 else if(location_special == SPECIAL_FRAME_VIDEO)
352 _status.subframe = _lsnes_status::subframe_video;
353 else
354 _status.subframe = CORE().mlogic->get_movie().next_poll_number();
355 } else {
356 _status.movie_valid = false;
357 _status.curframe = 0;
358 _status.length = 0;
359 _status.lag = 0;
360 _status.subframe = 0;
362 _status.dumping = (CORE().mdumper->get_dumper_count() > 0);
363 if(amode == ADVANCE_BREAK_PAUSE)
364 _status.pause = _lsnes_status::pause_break;
365 else if(amode == ADVANCE_PAUSE)
366 _status.pause = _lsnes_status::pause_normal;
367 else
368 _status.pause = _lsnes_status::pause_none;
369 if(*CORE().mlogic) {
370 auto& mo = CORE().mlogic->get_movie();
371 readonly = mo.readonly_mode();
372 if(system_corrupt)
373 _status.mode = 'C';
374 else if(!readonly)
375 _status.mode = 'R';
376 else if(mo.get_frame_count() >= mo.get_current_frame())
377 _status.mode = 'P';
378 else
379 _status.mode = 'F';
381 if(jukebox_size(*CORE().settings) > 0) {
382 _status.saveslot_valid = true;
383 int tmp = -1;
384 std::string sfilen = translate_name_mprefix(save_jukebox_name(save_jukebox_pointer), tmp, -1);
385 _status.saveslot = save_jukebox_pointer + 1;
386 _status.slotinfo = utf8::to32(get_slotinfo(sfilen));
387 } else {
388 _status.saveslot_valid = false;
390 _status.branch_valid = (p != NULL);
391 if(p) _status.branch = utf8::to32(p->get_branch_string());
393 std::string cur_branch = *CORE().mlogic ? CORE().mlogic->get_mfile().current_branch() :
395 _status.mbranch_valid = (cur_branch != "");
396 _status.mbranch = utf8::to32(cur_branch);
398 _status.speed = (unsigned)(100 * CORE().framerate->get_realized_multiplier() + 0.5);
400 if(*CORE().mlogic && !system_corrupt) {
401 time_t timevalue = static_cast<time_t>(CORE().mlogic->get_mfile().rtc_second);
402 struct tm* time_decompose = gmtime(&timevalue);
403 char datebuffer[512];
404 strftime(datebuffer, 511, "%Y%m%d(%a)T%H%M%S", time_decompose);
405 _status.rtc = utf8::to32(datebuffer);
406 _status.rtc_valid = true;
407 } else {
408 _status.rtc_valid = false;
411 auto mset = CORE().controls->active_macro_set();
412 bool mfirst = true;
413 std::ostringstream mss;
414 for(auto i: mset) {
415 if(!mfirst) mss << ",";
416 mss << i;
417 mfirst = false;
419 _status.macros = utf8::to32(mss.str());
421 controller_frame c;
422 if(!CORE().mteditor->any_records())
423 c = CORE().mlogic->get_movie().get_controls();
424 else
425 c = CORE().controls->get_committed();
426 _status.inputs.clear();
427 for(unsigned i = 0;; i++) {
428 auto pindex = CORE().controls->lcid_to_pcid(i);
429 if(pindex.first < 0 || !CORE().controls->is_present(pindex.first, pindex.second))
430 break;
431 char32_t buffer[MAX_DISPLAY_LENGTH];
432 c.display(pindex.first, pindex.second, buffer);
433 std::u32string _buffer = buffer;
434 if(readonly && CORE().mteditor->is_enabled()) {
435 multitrack_edit::state st = CORE().mteditor->get(pindex.first, pindex.second);
436 if(st == multitrack_edit::MT_PRESERVE)
437 _buffer += U" (keep)";
438 else if(st == multitrack_edit::MT_OVERWRITE)
439 _buffer += U" (rewrite)";
440 else if(st == multitrack_edit::MT_OR)
441 _buffer += U" (OR)";
442 else if(st == multitrack_edit::MT_XOR)
443 _buffer += U" (XOR)";
444 else
445 _buffer += U" (\?\?\?)";
447 _status.inputs.push_back(_buffer);
449 //Lua variables.
450 _status.lvars = get_lua_watch_vars();
451 //Memory watches.
452 _status.mvars = CORE().mwatch->get_window_vars();
454 _status.valid = true;
455 } catch(...) {
457 CORE().status->put_write();
458 CORE().dispatch->status_update();
461 uint64_t audio_irq_time;
462 uint64_t controller_irq_time;
463 uint64_t frame_irq_time;
466 struct lsnes_callbacks : public emucore_callbacks
468 public:
469 ~lsnes_callbacks() throw()
473 int16_t get_input(unsigned port, unsigned index, unsigned control)
475 int16_t x;
476 x = CORE().mlogic->input_poll(port, index, control);
477 lua_callback_snoop_input(port, index, control, x);
478 return x;
481 int16_t set_input(unsigned port, unsigned index, unsigned control, int16_t value)
483 if(!CORE().mlogic->get_movie().readonly_mode()) {
484 controller_frame f = CORE().mlogic->get_movie().get_controls();
485 f.axis3(port, index, control, value);
486 CORE().mlogic->get_movie().set_controls(f);
488 return CORE().mlogic->get_movie().next_input(port, index, control);
491 void notify_latch(std::list<std::string>& args)
493 lua_callback_do_latch(args);
496 void timer_tick(uint32_t increment, uint32_t per_second)
498 if(!*CORE().mlogic)
499 return;
500 auto& m = CORE().mlogic->get_mfile();
501 m.rtc_subsecond += increment;
502 while(m.rtc_subsecond >= per_second) {
503 m.rtc_second++;
504 m.rtc_subsecond -= per_second;
508 std::string get_firmware_path()
510 return CORE().setcache->get("firmwarepath");
513 std::string get_base_path()
515 return our_rom.msu1_base;
518 time_t get_time()
520 return *CORE().mlogic ? CORE().mlogic->get_mfile().rtc_second : 0;
523 time_t get_randomseed()
525 return random_seed_value;
528 void output_frame(framebuffer::raw& screen, uint32_t fps_n, uint32_t fps_d)
530 lua_callback_do_frame_emulated();
531 location_special = SPECIAL_FRAME_VIDEO;
532 CORE().fbuf->redraw_framebuffer(screen, false, true);
533 auto rate = our_rom.rtype->get_audio_rate();
534 uint32_t gv = gcd(fps_n, fps_d);
535 uint32_t ga = gcd(rate.first, rate.second);
536 CORE().mdumper->on_rate_change(rate.first / ga, rate.second / ga);
537 CORE().mdumper->on_frame(screen, fps_n / gv, fps_d / gv);
540 void action_state_updated()
542 CORE().dispatch->action_update();
545 void memory_read(uint64_t addr, uint64_t value)
547 CORE().dbg->do_callback_read(addr, value);
550 void memory_write(uint64_t addr, uint64_t value)
552 CORE().dbg->do_callback_write(addr, value);
555 void memory_execute(uint64_t addr, uint64_t proc)
557 CORE().dbg->do_callback_exec(addr, proc);
560 void memory_trace(uint64_t proc, const char* str, bool insn)
562 CORE().dbg->do_callback_trace(proc, str, insn);
566 namespace
568 lsnes_callbacks lsnes_callbacks_obj;
569 command::fnptr<> segfault(lsnes_cmds, "segfault", "Trigger SIGSEGV", "segfault\nTrigger segmentation fault",
570 []() throw(std::bad_alloc, std::runtime_error) {
571 char* ptr = (char*)0x1234;
572 *ptr = 0;
575 command::fnptr<> div0(lsnes_cmds, "divide-by-0", "Do div0", "divide-by-0\nDo divide by 0",
576 []() throw(std::bad_alloc, std::runtime_error) {
577 static int ptr = 1;
578 static int ptr2 = 0;
579 ptr = ptr / ptr2;
582 command::fnptr<const std::string&> test4(lsnes_cmds, "test4", "test", "test",
583 [](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
584 std::list<std::string> _args;
585 std::string args2 = args;
586 for(auto& sym : token_iterator_foreach(args, {" ", "\t"}))
587 _args.push_back(sym);
588 lua_callback_do_latch(_args);
590 command::fnptr<> count_rerecords(lsnes_cmds, "count-rerecords", "Count rerecords",
591 "Syntax: count-rerecords\nCounts rerecords.\n",
592 []() throw(std::bad_alloc, std::runtime_error) {
593 std::vector<char> tmp;
594 uint64_t x = CORE().mlogic->get_rrdata().write(tmp);
595 messages << x << " rerecord(s)" << std::endl;
598 command::fnptr<const std::string&> quit_emulator(lsnes_cmds, "quit-emulator", "Quit the emulator",
599 "Syntax: quit-emulator [/y]\nQuits emulator (/y => don't ask for confirmation).\n",
600 [](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
601 amode = ADVANCE_QUIT;
602 quit_magic = QUIT_MAGIC;
603 platform::set_paused(false);
604 platform::cancel_wait();
607 command::fnptr<> unpause_emulator(lsnes_cmds, "unpause-emulator", "Unpause the emulator",
608 "Syntax: unpause-emulator\nUnpauses the emulator.\n",
609 []() throw(std::bad_alloc, std::runtime_error) {
610 amode = ADVANCE_AUTO;
611 platform::set_paused(false);
612 platform::cancel_wait();
615 command::fnptr<> pause_emulator(lsnes_cmds, "pause-emulator", "(Un)pause the emulator",
616 "Syntax: pause-emulator\n(Un)pauses the emulator.\n",
617 []() throw(std::bad_alloc, std::runtime_error) {
618 if(amode != ADVANCE_AUTO) {
619 amode = ADVANCE_AUTO;
620 platform::set_paused(false);
621 platform::cancel_wait();
622 } else {
623 platform::cancel_wait();
624 cancel_advance = false;
625 stop_at_frame_active = false;
626 amode = ADVANCE_PAUSE;
630 command::fnptr<> save_jukebox_prev(lsnes_cmds, "cycle-jukebox-backward", "Cycle save jukebox backwards",
631 "Syntax: cycle-jukebox-backward\nCycle save jukebox backwards\n",
632 []() throw(std::bad_alloc, std::runtime_error) {
633 size_t jbsize = jukebox_size(*CORE().settings);
634 if(jbsize == 0)
635 return;
636 if(save_jukebox_pointer == 0)
637 save_jukebox_pointer = jbsize - 1;
638 else
639 save_jukebox_pointer--;
640 if(save_jukebox_pointer >= (size_t)jbsize)
641 save_jukebox_pointer = 0;
642 update_movie_state();
645 command::fnptr<> save_jukebox_next(lsnes_cmds, "cycle-jukebox-forward", "Cycle save jukebox forwards",
646 "Syntax: cycle-jukebox-forward\nCycle save jukebox forwards\n",
647 []() throw(std::bad_alloc, std::runtime_error) {
648 size_t jbsize = jukebox_size(*CORE().settings);
649 if(jbsize == 0)
650 return;
651 if(save_jukebox_pointer + 1 >= (size_t)jbsize)
652 save_jukebox_pointer = 0;
653 else
654 save_jukebox_pointer++;
655 if(save_jukebox_pointer >= (size_t)jbsize)
656 save_jukebox_pointer = 0;
657 update_movie_state();
660 command::fnptr<const std::string&> save_jukebox_set(lsnes_cmds, "set-jukebox-slot", "Set jukebox slot",
661 "Syntax: set-jukebox-slot\nSet jukebox slot\n", [](const std::string& args)
662 throw(std::bad_alloc, std::runtime_error) {
663 if(!regex_match("[1-9][0-9]{0,8}", args))
664 throw std::runtime_error("Bad slot number");
665 uint32_t slot = parse_value<uint32_t>(args);
666 if(slot >= (size_t)jukebox_size(*CORE().settings))
667 throw std::runtime_error("Bad slot number");
668 save_jukebox_pointer = slot - 1;
669 update_movie_state();
672 command::fnptr<> load_jukebox(lsnes_cmds, "load-jukebox", "Load save from jukebox",
673 "Syntax: load-jukebox\nLoad save from jukebox\n",
674 []() throw(std::bad_alloc, std::runtime_error) {
675 if(jukebox_size(*CORE().settings) == 0)
676 throw std::runtime_error("No slot selected");
677 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_CURRENT);
680 command::fnptr<> load_jukebox_readwrite(lsnes_cmds, "load-jukebox-readwrite", "Load save from jukebox in"
681 " recording mode", "Syntax: load-jukebox-readwrite\nLoad save from jukebox in recording mode\n",
682 []() throw(std::bad_alloc, std::runtime_error) {
683 if(jukebox_size(*CORE().settings) == 0)
684 throw std::runtime_error("No slot selected");
685 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_RW);
688 command::fnptr<> load_jukebox_readonly(lsnes_cmds, "load-jukebox-readonly", "Load save from jukebox in "
689 "playback mode", "Syntax: load-jukebox-readonly\nLoad save from jukebox in playback mode\n",
690 []() throw(std::bad_alloc, std::runtime_error) {
691 if(jukebox_size(*CORE().settings) == 0)
692 throw std::runtime_error("No slot selected");
693 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_RO);
696 command::fnptr<> load_jukebox_preserve(lsnes_cmds, "load-jukebox-preserve", "Load save from jukebox, "
697 "preserving input", "Syntax: load-jukebox-preserve\nLoad save from jukebox, preserving input\n",
698 []() throw(std::bad_alloc, std::runtime_error) {
699 if(jukebox_size(*CORE().settings) == 0)
700 throw std::runtime_error("No slot selected");
701 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_PRESERVE);
704 command::fnptr<> load_jukebox_movie(lsnes_cmds, "load-jukebox-movie", "Load save from jukebox as movie",
705 "Syntax: load-jukebox-movie\nLoad save from jukebox as movie\n",
706 []() throw(std::bad_alloc, std::runtime_error) {
707 if(jukebox_size(*CORE().settings) == 0)
708 throw std::runtime_error("No slot selected");
709 mark_pending_load(save_jukebox_name(save_jukebox_pointer), LOAD_STATE_MOVIE);
712 command::fnptr<> save_jukebox_c(lsnes_cmds, "save-jukebox", "Save save to jukebox",
713 "Syntax: save-jukebox\nSave save to jukebox\n",
714 []() throw(std::bad_alloc, std::runtime_error) {
715 if(jukebox_size(*CORE().settings) == 0)
716 throw std::runtime_error("No slot selected");
717 mark_pending_save(save_jukebox_name(save_jukebox_pointer), SAVE_STATE, -1);
720 command::fnptr<> padvance_frame(lsnes_cmds, "+advance-frame", "Advance one frame",
721 "Syntax: +advance-frame\nAdvances the emulation by one frame.\n",
722 []() throw(std::bad_alloc, std::runtime_error) {
723 amode = ADVANCE_FRAME;
724 cancel_advance = false;
725 advanced_once = false;
726 platform::cancel_wait();
727 platform::set_paused(false);
730 command::fnptr<> nadvance_frame(lsnes_cmds, "-advance-frame", "Advance one frame",
731 "No help available\n",
732 []() throw(std::bad_alloc, std::runtime_error) {
733 cancel_advance = true;
734 platform::cancel_wait();
735 platform::set_paused(false);
738 command::fnptr<> padvance_poll(lsnes_cmds, "+advance-poll", "Advance one subframe",
739 "Syntax: +advance-poll\nAdvances the emulation by one subframe.\n",
740 []() throw(std::bad_alloc, std::runtime_error) {
741 amode = ADVANCE_SUBFRAME;
742 cancel_advance = false;
743 advanced_once = false;
744 platform::cancel_wait();
745 platform::set_paused(false);
748 command::fnptr<> nadvance_poll(lsnes_cmds, "-advance-poll", "Advance one subframe",
749 "No help available\n",
750 []() throw(std::bad_alloc, std::runtime_error) {
751 if(amode == ADVANCE_BREAK_PAUSE)
752 amode = ADVANCE_PAUSE;
753 cancel_advance = true;
754 platform::cancel_wait();
755 platform::set_paused(false);
758 command::fnptr<> advance_skiplag(lsnes_cmds, "advance-skiplag", "Skip to next poll",
759 "Syntax: advance-skiplag\nAdvances the emulation to the next poll.\n",
760 []() throw(std::bad_alloc, std::runtime_error) {
761 amode = ADVANCE_SKIPLAG_PENDING;
762 platform::cancel_wait();
763 platform::set_paused(false);
766 command::fnptr<> reset_c(lsnes_cmds, "reset", "Reset the system",
767 "Syntax: reset\nReset\nResets the system in beginning of the next frame.\n",
768 []() throw(std::bad_alloc, std::runtime_error) {
769 int sreset_action = our_rom.rtype->reset_action(false);
770 if(sreset_action < 0) {
771 platform::error_message("Core does not support resets");
772 messages << "Emulator core does not support resets" << std::endl;
773 return;
775 our_rom.rtype->execute_action(sreset_action, std::vector<interface_action_paramval>());
778 command::fnptr<> hreset_c(lsnes_cmds, "reset-hard", "Reset the system",
779 "Syntax: reset-hard\nReset-hard\nHard resets the system in beginning of the next frame.\n",
780 []() throw(std::bad_alloc, std::runtime_error) {
781 int hreset_action = our_rom.rtype->reset_action(true);
782 if(hreset_action < 0) {
783 platform::error_message("Core does not support hard resets");
784 messages << "Emulator core does not support hard resets" << std::endl;
785 return;
787 our_rom.rtype->execute_action(hreset_action, std::vector<interface_action_paramval>());
790 command::fnptr<command::arg_filename> load_c(lsnes_cmds, "load", "Load savestate (current mode)",
791 "Syntax: load <file>\nLoads SNES state from <file> in current mode\n",
792 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
793 mark_pending_load(args, LOAD_STATE_CURRENT);
796 command::fnptr<command::arg_filename> load_smart_c(lsnes_cmds, "load-smart",
797 "Load savestate (heuristic mode)",
798 "Syntax: load <file>\nLoads SNES state from <file> in heuristic mode\n",
799 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
800 mark_pending_load(args, LOAD_STATE_DEFAULT);
803 command::fnptr<command::arg_filename> load_state_c(lsnes_cmds, "load-state", "Load savestate (R/W)",
804 "Syntax: load-state <file>\nLoads SNES state from <file> in Read/Write mode\n",
805 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
806 mark_pending_load(args, LOAD_STATE_RW);
809 command::fnptr<command::arg_filename> load_readonly(lsnes_cmds, "load-readonly", "Load savestate (RO)",
810 "Syntax: load-readonly <file>\nLoads SNES state from <file> in playback mode\n",
811 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
812 mark_pending_load(args, LOAD_STATE_RO);
815 command::fnptr<command::arg_filename> load_preserve(lsnes_cmds, "load-preserve", "Load savestate (preserve "
816 "input)", "Syntax: load-preserve <file>\nLoads SNES state from <file> preserving input\n",
817 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
818 mark_pending_load(args, LOAD_STATE_PRESERVE);
821 command::fnptr<command::arg_filename> load_movie_c(lsnes_cmds, "load-movie", "Load movie",
822 "Syntax: load-movie <file>\nLoads SNES movie from <file>\n",
823 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
824 mark_pending_load(args, LOAD_STATE_MOVIE);
827 command::fnptr<command::arg_filename> load_allbr_c(lsnes_cmds, "load-allbranches", "Load savestate "
828 "(all branches)", "Syntax: load-allbranches <file>\nLoads SNES state from <file> with all "
829 "branches\n",
830 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
831 mark_pending_load(args, LOAD_STATE_ALLBRANCH);
834 command::fnptr<command::arg_filename> save_state(lsnes_cmds, "save-state", "Save state",
835 "Syntax: save-state <file>\nSaves SNES state to <file>\n",
836 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
837 mark_pending_save(args, SAVE_STATE, -1);
840 command::fnptr<command::arg_filename> save_state2(lsnes_cmds, "save-state-binary", "Save state (binary)",
841 "Syntax: save-state-binary <file>\nSaves binary state to <file>\n",
842 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
843 mark_pending_save(args, SAVE_STATE, 1);
846 command::fnptr<command::arg_filename> save_state3(lsnes_cmds, "save-state-zip", "Save state (zip)",
847 "Syntax: save-state-zip <file>\nSaves zip state to <file>\n",
848 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
849 mark_pending_save(args, SAVE_STATE, 0);
852 command::fnptr<command::arg_filename> save_movie(lsnes_cmds, "save-movie", "Save movie",
853 "Syntax: save-movie <file>\nSaves SNES movie to <file>\n",
854 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
855 mark_pending_save(args, SAVE_MOVIE, -1);
858 command::fnptr<command::arg_filename> save_movie2(lsnes_cmds, "save-movie-binary", "Save movie (binary)",
859 "Syntax: save-movie-binary <file>\nSaves binary movie to <file>\n",
860 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
861 mark_pending_save(args, SAVE_MOVIE, 1);
864 command::fnptr<command::arg_filename> save_movie3(lsnes_cmds, "save-movie-zip", "Save movie (zip)",
865 "Syntax: save-movie-zip <file>\nSaves zip movie to <file>\n",
866 [](command::arg_filename args) throw(std::bad_alloc, std::runtime_error) {
867 mark_pending_save(args, SAVE_MOVIE, 0);
870 command::fnptr<> set_rwmode(lsnes_cmds, "set-rwmode", "Switch to recording mode",
871 "Syntax: set-rwmode\nSwitches to recording mode\n",
872 []() throw(std::bad_alloc, std::runtime_error) {
873 lua_callback_movie_lost("readwrite");
874 CORE().mlogic->get_movie().readonly_mode(false);
875 CORE().dispatch->mode_change(false);
876 lua_callback_do_readwrite();
877 update_movie_state();
880 command::fnptr<> set_romode(lsnes_cmds, "set-romode", "Switch to playback mode",
881 "Syntax: set-romode\nSwitches to playback mode\n",
882 []() throw(std::bad_alloc, std::runtime_error) {
883 CORE().mlogic->get_movie().readonly_mode(true);
884 CORE().dispatch->mode_change(true);
885 update_movie_state();
888 command::fnptr<> toggle_rwmode(lsnes_cmds, "toggle-rwmode", "Toggle recording mode",
889 "Syntax: toggle-rwmode\nToggles recording mode\n",
890 []() throw(std::bad_alloc, std::runtime_error) {
891 bool c = CORE().mlogic->get_movie().readonly_mode();
892 if(c)
893 lua_callback_movie_lost("readwrite");
894 CORE().mlogic->get_movie().readonly_mode(!c);
895 CORE().dispatch->mode_change(!c);
896 if(c)
897 lua_callback_do_readwrite();
898 update_movie_state();
901 command::fnptr<> repaint(lsnes_cmds, "repaint", "Redraw the screen",
902 "Syntax: repaint\nRedraws the screen\n",
903 []() throw(std::bad_alloc, std::runtime_error) {
904 CORE().fbuf->redraw_framebuffer();
907 command::fnptr<> tpon(lsnes_cmds, "toggle-pause-on-end", "Toggle pause on end", "Toggle pause on end\n",
908 []() throw(std::bad_alloc, std::runtime_error) {
909 bool tmp = pause_on_end(*CORE().settings);
910 pause_on_end(*CORE().settings, !tmp);
911 messages << "Pause-on-end is now " << (tmp ? "OFF" : "ON") << std::endl;
914 command::fnptr<> spon(lsnes_cmds, "set-pause-on-end", "Set pause on end", "Set pause on end\n",
915 []() throw(std::bad_alloc, std::runtime_error) {
916 pause_on_end(*CORE().settings, true);
917 messages << "Pause-on-end is now ON" << std::endl;
920 command::fnptr<> cpon(lsnes_cmds, "clear-pause-on-end", "Clear pause on end", "Clear pause on end\n",
921 []() throw(std::bad_alloc, std::runtime_error) {
922 pause_on_end(*CORE().settings, false);
923 messages << "Pause-on-end is now OFF" << std::endl;
926 command::fnptr<> rewind_movie(lsnes_cmds, "rewind-movie", "Rewind movie to the beginning",
927 "Syntax: rewind-movie\nRewind movie to the beginning\n",
928 []() throw(std::bad_alloc, std::runtime_error) {
929 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_BEGINNING);
932 command::fnptr<> cancel_save(lsnes_cmds, "cancel-saves", "Cancel all pending saves", "Syntax: "
933 "cancel-save\nCancel pending saves\n",
934 []() throw(std::bad_alloc, std::runtime_error) {
935 queued_saves.clear();
936 messages << "Pending saves canceled." << std::endl;
939 command::fnptr<> flushslots(lsnes_cmds, "flush-slotinfo", "Flush slotinfo cache",
940 "Flush slotinfo cache\n",
941 []() throw(std::bad_alloc, std::runtime_error) {
942 flush_slotinfo();
945 command::fnptr<> mhold1(lsnes_cmds, "+hold-macro", "Hold macro (hold)",
946 "Hold macros enable\n", []() throw(std::bad_alloc, std::runtime_error) {
947 macro_hold_1 = true;
950 command::fnptr<> mhold2(lsnes_cmds, "-hold-macro", "Hold macro (hold)",
951 "Hold macros disable\n", []() throw(std::bad_alloc, std::runtime_error) {
952 macro_hold_1 = false;
955 command::fnptr<> mhold3(lsnes_cmds, "hold-macro", "Hold macro (toggle)",
956 "Hold macros toggle\n", []() throw(std::bad_alloc, std::runtime_error) {
957 macro_hold_2 = !macro_hold_2;
958 if(macro_hold_2)
959 messages << "Macros are held for next frame." << std::endl;
960 else
961 messages << "Macros are not held for next frame." << std::endl;
964 keyboard::invbind_info imhold1(lsnes_invbinds, "+hold-macro", "Macro‣Hold all macros");
965 keyboard::invbind_info imhold2(lsnes_invbinds, "hold-macro", "Macro‣Hold all macros (typed)");
966 keyboard::invbind_info ipause_emulator(lsnes_invbinds, "pause-emulator", "Speed‣(Un)pause");
967 keyboard::invbind_info ijback(lsnes_invbinds, "cycle-jukebox-backward", "Slot select‣Cycle backwards");
968 keyboard::invbind_info ijforward(lsnes_invbinds, "cycle-jukebox-forward", "Slot select‣Cycle forwards");
969 keyboard::invbind_info iloadj(lsnes_invbinds, "load-jukebox", "Load‣Selected slot");
970 keyboard::invbind_info iloadjrw(lsnes_invbinds, "load-jukebox-readwrite",
971 "Load‣Selected slot (recording mode)");
972 keyboard::invbind_info iloadjro(lsnes_invbinds, "load-jukebox-readonly",
973 "Load‣Selected slot (playback mode)");
974 keyboard::invbind_info iloadjp(lsnes_invbinds, "load-jukebox-preserve",
975 "Load‣Selected slot (preserve input)");
976 keyboard::invbind_info iloadjm(lsnes_invbinds, "load-jukebox-movie", "Load‣Selected slot (as movie)");
977 keyboard::invbind_info isavej(lsnes_invbinds, "save-jukebox", "Save‣Selected slot");
978 keyboard::invbind_info iadvframe(lsnes_invbinds, "+advance-frame", "Speed‣Advance frame");
979 keyboard::invbind_info iadvsubframe(lsnes_invbinds, "+advance-poll", "Speed‣Advance subframe");
980 keyboard::invbind_info iskiplag(lsnes_invbinds, "advance-skiplag", "Speed‣Advance poll");
981 keyboard::invbind_info ireset(lsnes_invbinds, "reset", "System‣Reset");
982 keyboard::invbind_info iset_rwmode(lsnes_invbinds, "set-rwmode", "Movie‣Switch to recording");
983 keyboard::invbind_info itoggle_romode(lsnes_invbinds, "set-romode", "Movie‣Switch to playback");
984 keyboard::invbind_info itoggle_rwmode(lsnes_invbinds, "toggle-rwmode", "Movie‣Toggle playback");
985 keyboard::invbind_info irepaint(lsnes_invbinds, "repaint", "System‣Repaint screen");
986 keyboard::invbind_info itogglepause(lsnes_invbinds, "toggle-pause-on-end", "Movie‣Toggle pause-on-end");
987 keyboard::invbind_info irewind_movie(lsnes_invbinds, "rewind-movie", "Movie‣Rewind movie");
988 keyboard::invbind_info icancel_saves(lsnes_invbinds, "cancel-saves", "Save‣Cancel pending saves");
989 keyboard::invbind_info iload1(lsnes_invbinds, "load $SLOT:1", "Load‣Slot 1");
990 keyboard::invbind_info iload2(lsnes_invbinds, "load $SLOT:2", "Load‣Slot 2");
991 keyboard::invbind_info iload3(lsnes_invbinds, "load $SLOT:3", "Load‣Slot 3");
992 keyboard::invbind_info iload4(lsnes_invbinds, "load $SLOT:4", "Load‣Slot 4");
993 keyboard::invbind_info iload5(lsnes_invbinds, "load $SLOT:5", "Load‣Slot 5");
994 keyboard::invbind_info iload6(lsnes_invbinds, "load $SLOT:6", "Load‣Slot 6");
995 keyboard::invbind_info iload7(lsnes_invbinds, "load $SLOT:7", "Load‣Slot 7");
996 keyboard::invbind_info iload8(lsnes_invbinds, "load $SLOT:8", "Load‣Slot 8");
997 keyboard::invbind_info iload9(lsnes_invbinds, "load $SLOT:9", "Load‣Slot 9");
998 keyboard::invbind_info iload10(lsnes_invbinds, "load $SLOT:10", "Load‣Slot 10");
999 keyboard::invbind_info iload11(lsnes_invbinds, "load $SLOT:11", "Load‣Slot 11");
1000 keyboard::invbind_info iload12(lsnes_invbinds, "load $SLOT:12", "Load‣Slot 12");
1001 keyboard::invbind_info iload13(lsnes_invbinds, "load $SLOT:13", "Load‣Slot 13");
1002 keyboard::invbind_info iload14(lsnes_invbinds, "load $SLOT:14", "Load‣Slot 14");
1003 keyboard::invbind_info iload15(lsnes_invbinds, "load $SLOT:15", "Load‣Slot 15");
1004 keyboard::invbind_info iload16(lsnes_invbinds, "load $SLOT:16", "Load‣Slot 16");
1005 keyboard::invbind_info iload17(lsnes_invbinds, "load $SLOT:17", "Load‣Slot 17");
1006 keyboard::invbind_info iload18(lsnes_invbinds, "load $SLOT:18", "Load‣Slot 18");
1007 keyboard::invbind_info iload19(lsnes_invbinds, "load $SLOT:19", "Load‣Slot 19");
1008 keyboard::invbind_info iload20(lsnes_invbinds, "load $SLOT:20", "Load‣Slot 20");
1009 keyboard::invbind_info iload21(lsnes_invbinds, "load $SLOT:21", "Load‣Slot 21");
1010 keyboard::invbind_info iload22(lsnes_invbinds, "load $SLOT:22", "Load‣Slot 22");
1011 keyboard::invbind_info iload23(lsnes_invbinds, "load $SLOT:23", "Load‣Slot 23");
1012 keyboard::invbind_info iload24(lsnes_invbinds, "load $SLOT:24", "Load‣Slot 24");
1013 keyboard::invbind_info iload25(lsnes_invbinds, "load $SLOT:25", "Load‣Slot 25");
1014 keyboard::invbind_info iload26(lsnes_invbinds, "load $SLOT:26", "Load‣Slot 26");
1015 keyboard::invbind_info iload27(lsnes_invbinds, "load $SLOT:27", "Load‣Slot 27");
1016 keyboard::invbind_info iload28(lsnes_invbinds, "load $SLOT:28", "Load‣Slot 28");
1017 keyboard::invbind_info iload29(lsnes_invbinds, "load $SLOT:29", "Load‣Slot 29");
1018 keyboard::invbind_info iload30(lsnes_invbinds, "load $SLOT:30", "Load‣Slot 30");
1019 keyboard::invbind_info iload31(lsnes_invbinds, "load $SLOT:31", "Load‣Slot 31");
1020 keyboard::invbind_info iload32(lsnes_invbinds, "load $SLOT:32", "Load‣Slot 32");
1021 keyboard::invbind_info isave1(lsnes_invbinds, "save-state $SLOT:1", "Save‣Slot 1");
1022 keyboard::invbind_info isave2(lsnes_invbinds, "save-state $SLOT:2", "Save‣Slot 2");
1023 keyboard::invbind_info isave3(lsnes_invbinds, "save-state $SLOT:3", "Save‣Slot 3");
1024 keyboard::invbind_info isave4(lsnes_invbinds, "save-state $SLOT:4", "Save‣Slot 4");
1025 keyboard::invbind_info isave5(lsnes_invbinds, "save-state $SLOT:5", "Save‣Slot 5");
1026 keyboard::invbind_info isave6(lsnes_invbinds, "save-state $SLOT:6", "Save‣Slot 6");
1027 keyboard::invbind_info isave7(lsnes_invbinds, "save-state $SLOT:7", "Save‣Slot 7");
1028 keyboard::invbind_info isave8(lsnes_invbinds, "save-state $SLOT:8", "Save‣Slot 8");
1029 keyboard::invbind_info isave9(lsnes_invbinds, "save-state $SLOT:9", "Save‣Slot 9");
1030 keyboard::invbind_info isave10(lsnes_invbinds, "save-state $SLOT:10", "Save‣Slot 10");
1031 keyboard::invbind_info isave11(lsnes_invbinds, "save-state $SLOT:11", "Save‣Slot 11");
1032 keyboard::invbind_info isave12(lsnes_invbinds, "save-state $SLOT:12", "Save‣Slot 12");
1033 keyboard::invbind_info isave13(lsnes_invbinds, "save-state $SLOT:13", "Save‣Slot 13");
1034 keyboard::invbind_info isave14(lsnes_invbinds, "save-state $SLOT:14", "Save‣Slot 14");
1035 keyboard::invbind_info isave15(lsnes_invbinds, "save-state $SLOT:15", "Save‣Slot 15");
1036 keyboard::invbind_info isave16(lsnes_invbinds, "save-state $SLOT:16", "Save‣Slot 16");
1037 keyboard::invbind_info isave17(lsnes_invbinds, "save-state $SLOT:17", "Save‣Slot 17");
1038 keyboard::invbind_info isave18(lsnes_invbinds, "save-state $SLOT:18", "Save‣Slot 18");
1039 keyboard::invbind_info isave19(lsnes_invbinds, "save-state $SLOT:19", "Save‣Slot 19");
1040 keyboard::invbind_info isave20(lsnes_invbinds, "save-state $SLOT:20", "Save‣Slot 20");
1041 keyboard::invbind_info isave21(lsnes_invbinds, "save-state $SLOT:21", "Save‣Slot 21");
1042 keyboard::invbind_info isave22(lsnes_invbinds, "save-state $SLOT:22", "Save‣Slot 22");
1043 keyboard::invbind_info isave23(lsnes_invbinds, "save-state $SLOT:23", "Save‣Slot 23");
1044 keyboard::invbind_info isave24(lsnes_invbinds, "save-state $SLOT:24", "Save‣Slot 24");
1045 keyboard::invbind_info isave25(lsnes_invbinds, "save-state $SLOT:25", "Save‣Slot 25");
1046 keyboard::invbind_info isave26(lsnes_invbinds, "save-state $SLOT:26", "Save‣Slot 26");
1047 keyboard::invbind_info isave27(lsnes_invbinds, "save-state $SLOT:27", "Save‣Slot 27");
1048 keyboard::invbind_info isave28(lsnes_invbinds, "save-state $SLOT:28", "Save‣Slot 28");
1049 keyboard::invbind_info isave29(lsnes_invbinds, "save-state $SLOT:29", "Save‣Slot 29");
1050 keyboard::invbind_info isave30(lsnes_invbinds, "save-state $SLOT:30", "Save‣Slot 30");
1051 keyboard::invbind_info isave31(lsnes_invbinds, "save-state $SLOT:31", "Save‣Slot 31");
1052 keyboard::invbind_info isave32(lsnes_invbinds, "save-state $SLOT:32", "Save‣Slot 32");
1053 keyboard::invbind_info islot1(lsnes_invbinds, "set-jukebox-slot 1", "Slot select‣Slot 1");
1054 keyboard::invbind_info islot2(lsnes_invbinds, "set-jukebox-slot 2", "Slot select‣Slot 2");
1055 keyboard::invbind_info islot3(lsnes_invbinds, "set-jukebox-slot 3", "Slot select‣Slot 3");
1056 keyboard::invbind_info islot4(lsnes_invbinds, "set-jukebox-slot 4", "Slot select‣Slot 4");
1057 keyboard::invbind_info islot5(lsnes_invbinds, "set-jukebox-slot 5", "Slot select‣Slot 5");
1058 keyboard::invbind_info islot6(lsnes_invbinds, "set-jukebox-slot 6", "Slot select‣Slot 6");
1059 keyboard::invbind_info islot7(lsnes_invbinds, "set-jukebox-slot 7", "Slot select‣Slot 7");
1060 keyboard::invbind_info islot8(lsnes_invbinds, "set-jukebox-slot 8", "Slot select‣Slot 8");
1061 keyboard::invbind_info islot9(lsnes_invbinds, "set-jukebox-slot 9", "Slot select‣Slot 9");
1062 keyboard::invbind_info islot10(lsnes_invbinds, "set-jukebox-slot 10", "Slot select‣Slot 10");
1063 keyboard::invbind_info islot11(lsnes_invbinds, "set-jukebox-slot 11", "Slot select‣Slot 11");
1064 keyboard::invbind_info islot12(lsnes_invbinds, "set-jukebox-slot 12", "Slot select‣Slot 12");
1065 keyboard::invbind_info islot13(lsnes_invbinds, "set-jukebox-slot 13", "Slot select‣Slot 13");
1066 keyboard::invbind_info islot14(lsnes_invbinds, "set-jukebox-slot 14", "Slot select‣Slot 14");
1067 keyboard::invbind_info islot15(lsnes_invbinds, "set-jukebox-slot 15", "Slot select‣Slot 15");
1068 keyboard::invbind_info islot16(lsnes_invbinds, "set-jukebox-slot 16", "Slot select‣Slot 16");
1069 keyboard::invbind_info islot17(lsnes_invbinds, "set-jukebox-slot 17", "Slot select‣Slot 17");
1070 keyboard::invbind_info islot18(lsnes_invbinds, "set-jukebox-slot 18", "Slot select‣Slot 18");
1071 keyboard::invbind_info islot19(lsnes_invbinds, "set-jukebox-slot 19", "Slot select‣Slot 19");
1072 keyboard::invbind_info islot20(lsnes_invbinds, "set-jukebox-slot 20", "Slot select‣Slot 20");
1073 keyboard::invbind_info islot21(lsnes_invbinds, "set-jukebox-slot 21", "Slot select‣Slot 21");
1074 keyboard::invbind_info islot22(lsnes_invbinds, "set-jukebox-slot 22", "Slot select‣Slot 22");
1075 keyboard::invbind_info islot23(lsnes_invbinds, "set-jukebox-slot 23", "Slot select‣Slot 23");
1076 keyboard::invbind_info islot24(lsnes_invbinds, "set-jukebox-slot 24", "Slot select‣Slot 24");
1077 keyboard::invbind_info islot25(lsnes_invbinds, "set-jukebox-slot 25", "Slot select‣Slot 25");
1078 keyboard::invbind_info islot26(lsnes_invbinds, "set-jukebox-slot 26", "Slot select‣Slot 26");
1079 keyboard::invbind_info islot27(lsnes_invbinds, "set-jukebox-slot 27", "Slot select‣Slot 27");
1080 keyboard::invbind_info islot28(lsnes_invbinds, "set-jukebox-slot 28", "Slot select‣Slot 28");
1081 keyboard::invbind_info islot29(lsnes_invbinds, "set-jukebox-slot 29", "Slot select‣Slot 29");
1082 keyboard::invbind_info islot30(lsnes_invbinds, "set-jukebox-slot 30", "Slot select‣Slot 30");
1083 keyboard::invbind_info islot31(lsnes_invbinds, "set-jukebox-slot 31", "Slot select‣Slot 31");
1084 keyboard::invbind_info islot32(lsnes_invbinds, "set-jukebox-slot 32", "Slot select‣Slot 32");
1086 class mywindowcallbacks : public master_dumper::notifier
1088 public:
1089 mywindowcallbacks(emulator_dispatch& dispatch)
1091 closenotify.set(dispatch.close, [this]() {
1092 try {
1093 amode = ADVANCE_QUIT;
1094 quit_magic = QUIT_MAGIC;
1095 platform::set_paused(false);
1096 platform::cancel_wait();
1097 } catch(...) {
1101 ~mywindowcallbacks() throw() {}
1102 void dump_status_change() throw()
1104 update_movie_state();
1106 private:
1107 struct dispatch::target<> closenotify;
1110 //If there is a pending load, perform it. Return 1 on successful load, 0 if nothing to load, -1 on load
1111 //failing.
1112 int handle_load()
1114 std::string old_project = *CORE().mlogic ? CORE().mlogic->get_mfile().projectid : "";
1115 jumpback:
1116 if(do_unsafe_rewind && unsafe_rewind_obj) {
1117 if(!*CORE().mlogic)
1118 return 0;
1119 uint64_t t = framerate_regulator::get_utime();
1120 std::vector<char> s;
1121 lua_callback_do_unsafe_rewind(s, 0, 0, CORE().mlogic->get_movie(), unsafe_rewind_obj);
1122 CORE().dispatch->mode_change(false);
1123 do_unsafe_rewind = false;
1124 CORE().mlogic->get_mfile().is_savestate = true;
1125 location_special = SPECIAL_SAVEPOINT;
1126 update_movie_state();
1127 messages << "Rewind done in " << (framerate_regulator::get_utime() - t) << " usec."
1128 << std::endl;
1129 return 1;
1131 if(pending_new_project != "") {
1132 std::string id = pending_new_project;
1133 pending_new_project = "";
1134 project_info* old = CORE().project->get();
1135 if(old && old->id == id)
1136 goto nothing_to_do;
1137 try {
1138 auto& p = CORE().project->load(id);
1139 CORE().project->set(&p);
1140 if(CORE().project->get() != old)
1141 delete old;
1142 flush_slotinfo(); //Wrong movie may be stale.
1143 return 1;
1144 } catch(std::exception& e) {
1145 platform::error_message(std::string("Can't switch projects: ") + e.what());
1146 messages << "Can't switch projects: " << e.what() << std::endl;
1147 goto nothing_to_do;
1149 nothing_to_do:
1150 amode = old_mode;
1151 platform::set_paused(amode == ADVANCE_PAUSE);
1152 platform::flush_command_queue();
1153 if(amode == ADVANCE_LOAD)
1154 goto jumpback;
1155 return 0;
1157 if(pending_load != "") {
1158 bool system_was_corrupt = system_corrupt;
1159 system_corrupt = false;
1160 try {
1161 if(loadmode != LOAD_STATE_BEGINNING && loadmode != LOAD_STATE_ROMRELOAD &&
1162 !do_load_state(pending_load, loadmode)) {
1163 if(system_was_corrupt)
1164 system_corrupt = system_was_corrupt;
1165 pending_load = "";
1166 return -1;
1168 if(loadmode == LOAD_STATE_BEGINNING)
1169 do_load_rewind();
1170 if(loadmode == LOAD_STATE_ROMRELOAD)
1171 do_load_rom();
1172 } catch(std::exception& e) {
1173 if(!system_corrupt && system_was_corrupt)
1174 system_corrupt = true;
1175 platform::error_message(std::string("Load failed: ") + e.what());
1176 messages << "Load failed: " << e.what() << std::endl;
1178 pending_load = "";
1179 amode = load_paused ? ADVANCE_PAUSE : ADVANCE_AUTO;
1180 platform::set_paused(load_paused);
1181 load_paused = false;
1182 if(!system_corrupt) {
1183 location_special = SPECIAL_SAVEPOINT;
1184 update_movie_state();
1185 platform::flush_command_queue();
1186 if(is_quitting())
1187 return -1;
1188 if(amode == ADVANCE_LOAD)
1189 goto jumpback;
1191 if(old_project != (*CORE().mlogic ? CORE().mlogic->get_mfile().projectid : ""))
1192 flush_slotinfo(); //Wrong movie may be stale.
1193 return 1;
1195 return 0;
1198 //If there are pending saves, perform them.
1199 void handle_saves()
1201 if(!*CORE().mlogic)
1202 return;
1203 if(!queued_saves.empty() || (do_unsafe_rewind && !unsafe_rewind_obj)) {
1204 our_rom.rtype->runtosave();
1205 for(auto i : queued_saves) {
1206 do_save_state(i.first, i.second);
1207 int tmp = -1;
1208 flush_slotinfo(translate_name_mprefix(i.first, tmp, -1));
1210 if(do_unsafe_rewind && !unsafe_rewind_obj) {
1211 uint64_t t = framerate_regulator::get_utime();
1212 std::vector<char> s = our_rom.save_core_state(true);
1213 uint64_t secs = CORE().mlogic->get_mfile().rtc_second;
1214 uint64_t ssecs = CORE().mlogic->get_mfile().rtc_subsecond;
1215 lua_callback_do_unsafe_rewind(s, secs, ssecs, CORE().mlogic->get_movie(),
1216 NULL);
1217 do_unsafe_rewind = false;
1218 messages << "Rewind point set in " << (framerate_regulator::get_utime() - t)
1219 << " usec." << std::endl;
1222 queued_saves.clear();
1225 bool handle_corrupt()
1227 if(!system_corrupt)
1228 return false;
1229 while(system_corrupt) {
1230 platform::set_paused(true);
1231 platform::flush_command_queue();
1232 handle_load();
1233 if(is_quitting())
1234 return true;
1236 return true;
1240 void init_main_callbacks()
1242 ecore_callbacks = &lsnes_callbacks_obj;
1245 void main_loop(struct loaded_rom& rom, struct moviefile& initial, bool load_has_to_succeed) throw(std::bad_alloc,
1246 std::runtime_error)
1248 lsnes_instance.emu_thread = threads::id();
1249 mywindowcallbacks mywcb(*CORE().dispatch);
1250 CORE().iqueue->system_thread_available = true;
1251 //Basic initialization.
1252 emulation_thread = threads::this_id();
1253 jukebox_size_listener jlistener(*CORE().settings);
1254 CORE().commentary->init();
1255 CORE().fbuf->init_special_screens();
1256 our_rom = rom;
1257 init_main_callbacks();
1258 initialize_all_builtin_c_cores();
1259 core_core::install_all_handlers();
1261 //Load our given movie.
1262 bool first_round = false;
1263 bool just_did_loadstate = false;
1264 bool used = false;
1265 try {
1266 do_load_state(initial, LOAD_STATE_INITIAL, used);
1267 location_special = SPECIAL_SAVEPOINT;
1268 update_movie_state();
1269 first_round = CORE().mlogic->get_mfile().is_savestate;
1270 just_did_loadstate = first_round;
1271 } catch(std::bad_alloc& e) {
1272 OOM_panic();
1273 } catch(std::exception& e) {
1274 if(!used)
1275 delete &initial;
1276 platform::error_message(std::string("Can't load initial state: ") + e.what());
1277 messages << "ERROR: Can't load initial state: " << e.what() << std::endl;
1278 if(load_has_to_succeed) {
1279 messages << "FATAL: Can't load movie" << std::endl;
1280 throw;
1282 system_corrupt = true;
1283 CORE().fbuf->redraw_framebuffer(emu_framebuffer::screen_corrupt);
1286 platform::set_paused(initial.start_paused);
1287 amode = initial.start_paused ? ADVANCE_PAUSE : ADVANCE_AUTO;
1288 stop_at_frame_active = false;
1290 lua_run_startup_scripts();
1292 uint64_t time_x = framerate_regulator::get_utime();
1293 while(!is_quitting() || !queued_saves.empty()) {
1294 if(handle_corrupt()) {
1295 first_round = *CORE().mlogic && CORE().mlogic->get_mfile().is_savestate;
1296 just_did_loadstate = first_round;
1297 continue;
1299 CORE().framerate->ack_frame_tick(framerate_regulator::get_utime());
1300 if(amode == ADVANCE_SKIPLAG_PENDING)
1301 amode = ADVANCE_SKIPLAG;
1303 if(!first_round) {
1304 CORE().controls->reset_framehold();
1305 if(!macro_hold_1 && !macro_hold_2) {
1306 CORE().controls->advance_macros();
1308 macro_hold_2 = false;
1309 CORE().mlogic->get_movie().get_pollcounters().set_framepflag(false);
1310 CORE().mlogic->new_frame_starting(amode == ADVANCE_SKIPLAG);
1311 CORE().mlogic->get_movie().get_pollcounters().set_framepflag(true);
1312 if(is_quitting() && queued_saves.empty())
1313 break;
1314 handle_saves();
1315 int r = 0;
1316 if(queued_saves.empty())
1317 r = handle_load();
1318 if(r > 0 || system_corrupt) {
1319 CORE().mlogic->get_movie().get_pollcounters().set_framepflag(
1320 CORE().mlogic->get_mfile().is_savestate);
1321 first_round = CORE().mlogic->get_mfile().is_savestate;
1322 if(system_corrupt)
1323 amode = ADVANCE_PAUSE;
1324 else
1325 amode = old_mode;
1326 stop_at_frame_active = false;
1327 just_did_loadstate = first_round;
1328 CORE().controls->reset_framehold();
1329 CORE().dbg->do_callback_frame(CORE().mlogic->get_movie().get_current_frame(), true);
1330 continue;
1331 } else if(r < 0) {
1332 //Not exactly desriable, but this at least won't desync.
1333 stop_at_frame_active = false;
1334 if(is_quitting())
1335 goto out;
1336 amode = ADVANCE_PAUSE;
1339 if(just_did_loadstate) {
1340 //If we just loadstated, we are up to date.
1341 if(is_quitting())
1342 break;
1343 platform::set_paused(amode == ADVANCE_PAUSE);
1344 platform::flush_command_queue();
1345 //We already have done the reset this frame if we are going to do one at all.
1346 CORE().mlogic->get_movie().set_controls(CORE().mlogic->update_controls(true));
1347 CORE().mlogic->get_movie().set_all_DRDY();
1348 just_did_loadstate = false;
1350 frame_irq_time = framerate_regulator::get_utime() - time_x;
1351 CORE().dbg->do_callback_frame(CORE().mlogic->get_movie().get_current_frame(), false);
1352 our_rom.rtype->emulate();
1353 random_mix_timing_entropy();
1354 time_x = framerate_regulator::get_utime();
1355 if(amode == ADVANCE_AUTO)
1356 platform::wait(CORE().framerate->to_wait_frame(framerate_regulator::get_utime()));
1357 first_round = false;
1358 lua_callback_do_frame();
1360 out:
1361 CORE().mdumper->end_dumps();
1362 core_core::uninstall_all_handlers();
1363 CORE().commentary->kill();
1364 CORE().iqueue->system_thread_available = false;
1365 //Kill some things to avoid crashes.
1366 CORE().dbg->core_change();
1367 CORE().project->set(NULL, true);
1368 CORE().mwatch->clear_multi(CORE().mwatch->enumerate());
1371 void set_stop_at_frame(uint64_t frame)
1373 stop_at_frame = frame;
1374 stop_at_frame_active = (frame != 0);
1375 amode = ADVANCE_AUTO;
1376 platform::set_paused(false);
1379 void do_flush_slotinfo()
1381 flush_slotinfo();
1384 void switch_projects(const std::string& newproj)
1386 pending_new_project = newproj;
1387 amode = ADVANCE_LOAD;
1388 old_mode = ADVANCE_PAUSE;
1389 platform::cancel_wait();
1390 platform::set_paused(false);
1393 void load_new_rom(const romload_request& req)
1395 if(_load_new_rom(req)) {
1396 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1400 void reload_current_rom()
1402 if(reload_active_rom()) {
1403 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1407 void close_rom()
1409 if(load_null_rom()) {
1410 load_paused = true;
1411 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD);
1415 void do_break_pause()
1417 amode = ADVANCE_BREAK_PAUSE;
1418 update_movie_state();
1419 while(amode == ADVANCE_BREAK_PAUSE) {
1420 platform::set_paused(true);
1421 platform::flush_command_queue();
1425 void convert_break_to_pause()
1427 if(amode == ADVANCE_BREAK_PAUSE) {
1428 amode = ADVANCE_PAUSE;
1429 update_movie_state();
1433 void debug_trash_memory(uint8_t* addr, uint8_t byte)
1435 *addr = byte;