3 #include "cmdhelp/loadsave.hpp"
4 #include "cmdhelp/mhold.hpp"
5 #include "core/advdumper.hpp"
6 #include "core/command.hpp"
7 #include "core/controller.hpp"
8 #include "core/debug.hpp"
9 #include "core/dispatch.hpp"
10 #include "core/emustatus.hpp"
11 #include "core/framebuffer.hpp"
12 #include "core/framerate.hpp"
13 #include "core/instance.hpp"
14 #include "core/inthread.hpp"
15 #include "core/jukebox.hpp"
16 #include "core/keymapper.hpp"
17 #include "core/mainloop.hpp"
18 #include "core/memorymanip.hpp"
19 #include "core/memorywatch.hpp"
20 #include "core/messages.hpp"
21 #include "core/moviedata.hpp"
22 #include "core/moviefile.hpp"
23 #include "core/multitrack.hpp"
24 #include "core/project.hpp"
25 #include "core/queue.hpp"
26 #include "core/random.hpp"
27 #include "core/rom.hpp"
28 #include "core/runmode.hpp"
29 #include "core/settings.hpp"
30 #include "core/window.hpp"
31 #include "interface/callbacks.hpp"
32 #include "interface/c-interface.hpp"
33 #include "interface/romtype.hpp"
34 #include "library/framebuffer.hpp"
35 #include "library/settingvar.hpp"
36 #include "library/string.hpp"
37 #include "library/zip.hpp"
38 #include "lua/lua.hpp"
49 settingvar::supervariable
<settingvar::model_bool
<settingvar::yes_no
>> movie_dflt_binary(lsnes_setgrp
,
50 "movie-default-binary", "Movie‣Saving‣Movies binary", false);
51 settingvar::supervariable
<settingvar::model_bool
<settingvar::yes_no
>> save_dflt_binary(lsnes_setgrp
,
52 "savestate-default-binary", "Movie‣Saving‣Savestates binary", false);
56 settingvar::supervariable
<settingvar::model_int
<0,999999>> SET_advance_timeout_first(lsnes_setgrp
,
57 "advance-timeout", "Delays‣First frame advance", 500);
58 settingvar::supervariable
<settingvar::model_int
<0,999999>> SET_advance_timeout_subframe(lsnes_setgrp
,
59 "advance-subframe-timeout", "Delays‣Subframe advance", 100);
60 settingvar::supervariable
<settingvar::model_bool
<settingvar::yes_no
>> SET_pause_on_end(lsnes_setgrp
,
61 "pause-on-end", "Movie‣Pause on end", false);
63 //Mode and filename of pending load, one of LOAD_* constants.
65 std::string pending_load
;
66 std::string pending_new_project
;
67 //Queued saves (all savestates).
68 std::set
<std::pair
<std::string
, int>> queued_saves
;
70 bool do_unsafe_rewind
= false;
71 void* unsafe_rewind_obj
= NULL
;
73 bool stop_at_frame_active
= false;
74 uint64_t stop_at_frame
= 0;
80 void mainloop_signal_need_rewind(void* ptr
)
84 core
.runmode
->start_load();
85 do_unsafe_rewind
= true;
86 unsafe_rewind_obj
= ptr
;
89 bool movie_logic::notify_user_poll()
91 return CORE().runmode
->is_skiplag();
94 portctrl::frame
movie_logic::update_controls(bool subframe
, bool forced
)
97 if(core
.lua2
->requests_subframe_paint
)
98 core
.fbuf
->redraw_framebuffer();
101 if(core
.runmode
->is_advance_subframe()) {
102 //Note that platform::wait() may change value of cancel flag.
103 if(!core
.runmode
->test_cancel()) {
104 if(core
.runmode
->set_and_test_advanced())
105 platform::wait(SET_advance_timeout_subframe(*core
.settings
) * 1000);
107 platform::wait(SET_advance_timeout_first(*core
.settings
) * 1000);
108 core
.runmode
->set_and_test_advanced();
110 if(core
.runmode
->clear_and_test_cancel()) {
111 stop_at_frame_active
= false;
112 core
.runmode
->set_pause();
114 platform::set_paused(core
.runmode
->is_paused());
115 } else if(core
.runmode
->is_advance_frame()) {
118 if(core
.runmode
->is_skiplag() && forced
) {
119 stop_at_frame_active
= false;
120 core
.runmode
->set_pause();
122 core
.runmode
->clear_and_test_cancel();
124 platform::set_paused(core
.runmode
->is_paused());
125 core
.runmode
->set_point(emulator_runmode::P_NONE
);
126 core
.supdater
->update();
128 core
.runmode
->decay_skiplag();
129 if(core
.runmode
->is_advance()) {
130 //Note that platform::wait() may change value of cancel flag.
131 if(!core
.runmode
->test_cancel()) {
133 if(!core
.runmode
->test_advanced())
134 wait
= SET_advance_timeout_first(*core
.settings
) * 1000;
135 else if(core
.runmode
->is_advance_subframe())
136 wait
= SET_advance_timeout_subframe(*core
.settings
) * 1000;
138 wait
= core
.framerate
->to_wait_frame(framerate_regulator::get_utime());
139 platform::wait(wait
);
140 core
.runmode
->set_and_test_advanced();
142 if(core
.runmode
->clear_and_test_cancel()) {
143 stop_at_frame_active
= false;
144 core
.runmode
->set_pause();
146 platform::set_paused(core
.runmode
->is_paused());
147 } else if(core
.runmode
->is_freerunning() && core
.mlogic
->get_movie().readonly_mode() &&
148 SET_pause_on_end(*core
.settings
) && !stop_at_frame_active
) {
149 if(core
.mlogic
->get_movie().get_current_frame() ==
150 core
.mlogic
->get_movie().get_frame_count()) {
151 stop_at_frame_active
= false;
152 core
.runmode
->set_pause();
153 platform::set_paused(true);
155 } else if(core
.runmode
->is_freerunning() && stop_at_frame_active
) {
156 if(core
.mlogic
->get_movie().get_current_frame() >= stop_at_frame
) {
157 stop_at_frame_active
= false;
158 core
.runmode
->set_pause();
159 platform::set_paused(true);
162 platform::set_paused(core
.runmode
->is_paused());
164 core
.runmode
->set_point(emulator_runmode::P_START
);
165 core
.supdater
->update();
167 platform::flush_command_queue();
168 portctrl::frame tmp
= core
.controls
->get(core
.mlogic
->get_movie().get_current_frame());
169 core
.rom
->pre_emulate_frame(tmp
); //Preset controls, the lua will override if needed.
170 core
.lua2
->callback_do_input(tmp
, subframe
);
171 core
.mteditor
->process_frame(tmp
);
172 core
.controls
->commit(tmp
);
179 //Do pending load (automatically unpauses).
180 void mark_pending_load(std::string filename
, int lmode
)
183 //Ignore requests to loadstate when loadstate is already in progress. Should fix that "gets stuck
184 //on high speed" bug.
185 if(core
.runmode
->get() == emulator_runmode::LOAD
) return;
186 //Convert break pause to ordinary pause.
188 pending_load
= filename
;
189 core
.runmode
->decay_break();
190 core
.runmode
->start_load();
191 platform::cancel_wait();
192 platform::set_paused(false);
195 void mark_pending_save(std::string filename
, int smode
, int binary
)
199 if(smode
== SAVE_MOVIE
) {
200 //Just do this immediately.
201 do_save_movie(filename
, binary
);
202 core
.slotcache
->flush(translate_name_mprefix(filename
, tmp
, -1));
205 if(core
.runmode
->get_point() == emulator_runmode::P_SAVE
) {
206 //We can save immediately here.
207 do_save_state(filename
, binary
);
208 core
.slotcache
->flush(translate_name_mprefix(filename
, tmp
, -1));
211 queued_saves
.insert(std::make_pair(filename
, binary
));
212 messages
<< "Pending save on '" << filename
<< "'" << std::endl
;
217 struct lsnes_callbacks
: public emucore_callbacks
220 ~lsnes_callbacks() throw()
224 int16_t get_input(unsigned port
, unsigned index
, unsigned control
)
228 x
= core
.mlogic
->input_poll(port
, index
, control
);
229 core
.lua2
->callback_snoop_input(port
, index
, control
, x
);
233 int16_t set_input(unsigned port
, unsigned index
, unsigned control
, int16_t value
)
236 if(!core
.mlogic
->get_movie().readonly_mode()) {
237 portctrl::frame f
= core
.mlogic
->get_movie().get_controls();
238 f
.axis3(port
, index
, control
, value
);
239 core
.mlogic
->get_movie().set_controls(f
);
241 return core
.mlogic
->get_movie().next_input(port
, index
, control
);
244 void notify_latch(std::list
<std::string
>& args
)
246 CORE().lua2
->callback_do_latch(args
);
249 void timer_tick(uint32_t increment
, uint32_t per_second
)
254 auto& m
= core
.mlogic
->get_mfile();
255 m
.dyn
.rtc_subsecond
+= increment
;
256 while(m
.dyn
.rtc_subsecond
>= per_second
) {
258 m
.dyn
.rtc_subsecond
-= per_second
;
262 std::string
get_firmware_path()
264 return SET_firmwarepath(*CORE().settings
);
267 std::string
get_base_path()
269 return CORE().rom
->get_msu1_base();
275 return *core
.mlogic
? core
.mlogic
->get_mfile().dyn
.rtc_second
: 0;
278 time_t get_randomseed()
280 return CORE().random_seed_value
;
283 void output_frame(framebuffer::raw
& screen
, uint32_t fps_n
, uint32_t fps_d
)
286 core
.lua2
->callback_do_frame_emulated();
287 core
.runmode
->set_point(emulator_runmode::P_VIDEO
);
288 core
.fbuf
->redraw_framebuffer(screen
, false, true);
289 auto rate
= core
.rom
->get_audio_rate();
290 uint32_t gv
= gcd(fps_n
, fps_d
);
291 uint32_t ga
= gcd(rate
.first
, rate
.second
);
292 core
.mdumper
->on_rate_change(rate
.first
/ ga
, rate
.second
/ ga
);
293 core
.mdumper
->on_frame(screen
, fps_n
/ gv
, fps_d
/ gv
);
296 void action_state_updated()
298 CORE().dispatch
->action_update();
301 void memory_read(uint64_t addr
, uint64_t value
)
303 CORE().dbg
->do_callback_read(addr
, value
);
306 void memory_write(uint64_t addr
, uint64_t value
)
308 CORE().dbg
->do_callback_write(addr
, value
);
311 void memory_execute(uint64_t addr
, uint64_t proc
)
313 CORE().dbg
->do_callback_exec(addr
, proc
);
316 void memory_trace(uint64_t proc
, const char* str
, bool insn
)
318 CORE().dbg
->do_callback_trace(proc
, str
, insn
);
323 CORE().lua2
->callback_do_reset();
329 lsnes_callbacks lsnes_callbacks_obj
;
330 command::fnptr
<> CMD_segfault(lsnes_cmds
, "segfault", "Trigger SIGSEGV",
331 "segfault\nTrigger segmentation fault",
333 char* ptr
= (char*)0x1234;
337 command::fnptr
<> CMD_div0(lsnes_cmds
, "divide-by-0", "Do div0", "divide-by-0\nDo divide by 0",
344 command::fnptr
<const std::string
&> CMD_test4(lsnes_cmds
, "test4", "test", "test",
345 [](const std::string
& args
) {
347 std::list
<std::string
> _args
;
348 std::string args2
= args
;
349 for(auto& sym
: token_iterator
<char>::foreach(args
, {" ", "\t"}))
350 _args
.push_back(sym
);
351 core
.lua2
->callback_do_latch(_args
);
353 command::fnptr
<> CMD_count_rerecords(lsnes_cmds
, "count-rerecords", "Count rerecords",
354 "Syntax: count-rerecords\nCounts rerecords.\n",
356 std::vector
<char> tmp
;
357 uint64_t x
= CORE().mlogic
->get_rrdata().write(tmp
);
358 messages
<< x
<< " rerecord(s)" << std::endl
;
361 command::fnptr
<const std::string
&> CMD_quit_emulator(lsnes_cmds
, "quit-emulator", "Quit the emulator",
362 "Syntax: quit-emulator [/y]\nQuits emulator (/y => don't ask for confirmation).\n",
363 [](const std::string
& args
) {
364 CORE().runmode
->set_quit();
365 platform::set_paused(false);
366 platform::cancel_wait();
369 command::fnptr
<> CMD_unpause_emulator(lsnes_cmds
, "unpause-emulator", "Unpause the emulator",
370 "Syntax: unpause-emulator\nUnpauses the emulator.\n",
373 if(core
.runmode
->is_special())
375 core
.runmode
->set_freerunning();
376 platform::set_paused(false);
377 platform::cancel_wait();
380 command::fnptr
<> CMD_pause_emulator(lsnes_cmds
, "pause-emulator", "(Un)pause the emulator",
381 "Syntax: pause-emulator\n(Un)pauses the emulator.\n",
384 if(core
.runmode
->is_special())
386 else if(core
.runmode
->is_freerunning()) {
387 platform::cancel_wait();
388 stop_at_frame_active
= false;
389 core
.runmode
->set_pause();
391 core
.runmode
->set_freerunning();
392 platform::set_paused(false);
393 platform::cancel_wait();
397 command::fnptr
<const std::string
&> CMD_start_r16m(lsnes_cmds
, "start-r16m", "Start dumping R16M",
398 "Syntax: start-r16m <filename>\nDump r16m to <filename>.\n",
399 [](const std::string
& args
) {
401 auto fp
= fopen(args
.c_str(), "wb");
403 core
.lua2
->set_r16m_dump(fp
);
405 messages
<< "Failed to open '" << args
<< "'" << std::endl
;
409 command::fnptr
<> CMD_end_r16m(lsnes_cmds
, "end-r16m", "End dumping R16M",
410 "Syntax: end-r16m\nEnd dumping r16m.\n",
413 core
.lua2
->set_r16m_dump(nullptr);
416 command::fnptr
<> CMD_load_jukebox(lsnes_cmds
, CLOADSAVE::ldj
,
419 mark_pending_load(core
.jukebox
->get_slot_name(), LOAD_STATE_CURRENT
);
422 command::fnptr
<> CMD_load_jukebox_readwrite(lsnes_cmds
, CLOADSAVE::ldjrw
,
425 mark_pending_load(core
.jukebox
->get_slot_name(), LOAD_STATE_RW
);
428 command::fnptr
<> CMD_load_jukebox_readonly(lsnes_cmds
, CLOADSAVE::ldjro
,
431 mark_pending_load(core
.jukebox
->get_slot_name(), LOAD_STATE_RO
);
434 command::fnptr
<> CMD_load_jukebox_preserve(lsnes_cmds
, CLOADSAVE::ldjp
,
437 mark_pending_load(core
.jukebox
->get_slot_name(), LOAD_STATE_PRESERVE
);
440 command::fnptr
<> CMD_load_jukebox_movie(lsnes_cmds
, CLOADSAVE::ldjm
,
443 mark_pending_load(core
.jukebox
->get_slot_name(), LOAD_STATE_MOVIE
);
446 command::fnptr
<> CMD_save_jukebox_c(lsnes_cmds
, CLOADSAVE::saj
,
449 mark_pending_save(core
.jukebox
->get_slot_name(), SAVE_STATE
, -1);
452 command::fnptr
<> CMD_padvance_frame(lsnes_cmds
, "+advance-frame", "Advance one frame",
453 "Syntax: +advance-frame\nAdvances the emulation by one frame.\n",
456 if(core
.runmode
->is_special())
458 core
.runmode
->set_frameadvance();
459 platform::cancel_wait();
460 platform::set_paused(false);
463 command::fnptr
<> CMD_nadvance_frame(lsnes_cmds
, "-advance-frame", "Advance one frame",
464 "No help available\n",
466 CORE().runmode
->set_cancel();
467 platform::cancel_wait();
468 platform::set_paused(false);
471 command::fnptr
<> CMD_padvance_poll(lsnes_cmds
, "+advance-poll", "Advance one subframe",
472 "Syntax: +advance-poll\nAdvances the emulation by one subframe.\n",
475 if(core
.runmode
->is_special())
477 core
.runmode
->set_subframeadvance();
478 platform::cancel_wait();
479 platform::set_paused(false);
482 command::fnptr
<> CMD_nadvance_poll(lsnes_cmds
, "-advance-poll", "Advance one subframe",
483 "No help available\n",
486 core
.runmode
->decay_break();
487 core
.runmode
->set_cancel();
488 platform::cancel_wait();
489 platform::set_paused(false);
492 command::fnptr
<> CMD_advance_skiplag(lsnes_cmds
, "advance-skiplag", "Skip to next poll",
493 "Syntax: advance-skiplag\nAdvances the emulation to the next poll.\n",
495 CORE().runmode
->set_skiplag_pending();
496 platform::cancel_wait();
497 platform::set_paused(false);
500 command::fnptr
<> CMD_reset_c(lsnes_cmds
, "reset", "Reset the system",
501 "Syntax: reset\nReset\nResets the system in beginning of the next frame.\n",
504 int sreset_action
= core
.rom
->reset_action(false);
505 if(sreset_action
< 0) {
506 platform::error_message("Core does not support resets");
507 messages
<< "Emulator core does not support resets" << std::endl
;
510 core
.rom
->execute_action(sreset_action
, std::vector
<interface_action_paramval
>());
513 command::fnptr
<> CMD_hreset_c(lsnes_cmds
, "reset-hard", "Reset the system",
514 "Syntax: reset-hard\nReset-hard\nHard resets the system in beginning of the next frame.\n",
517 int hreset_action
= core
.rom
->reset_action(true);
518 if(hreset_action
< 0) {
519 platform::error_message("Core does not support hard resets");
520 messages
<< "Emulator core does not support hard resets" << std::endl
;
523 core
.rom
->execute_action(hreset_action
, std::vector
<interface_action_paramval
>());
526 command::fnptr
<command::arg_filename
> CMD_load_c(lsnes_cmds
, CLOADSAVE::ld
,
527 [](command::arg_filename args
) {
528 mark_pending_load(args
, LOAD_STATE_CURRENT
);
531 command::fnptr
<command::arg_filename
> CMD_load_smart_c(lsnes_cmds
, CLOADSAVE::ldsm
,
532 [](command::arg_filename args
) {
533 mark_pending_load(args
, LOAD_STATE_DEFAULT
);
536 command::fnptr
<command::arg_filename
> CMD_load_state_c(lsnes_cmds
, CLOADSAVE::ldrw
,
537 [](command::arg_filename args
) {
538 mark_pending_load(args
, LOAD_STATE_RW
);
541 command::fnptr
<command::arg_filename
> CMD_load_readonly(lsnes_cmds
, CLOADSAVE::ldro
,
542 [](command::arg_filename args
) {
543 mark_pending_load(args
, LOAD_STATE_RO
);
546 command::fnptr
<command::arg_filename
> CMD_load_preserve(lsnes_cmds
, CLOADSAVE::ldp
,
547 [](command::arg_filename args
) {
548 mark_pending_load(args
, LOAD_STATE_PRESERVE
);
551 command::fnptr
<command::arg_filename
> CMD_load_movie_c(lsnes_cmds
, CLOADSAVE::ldm
,
552 [](command::arg_filename args
) {
553 mark_pending_load(args
, LOAD_STATE_MOVIE
);
556 command::fnptr
<command::arg_filename
> CMD_load_allbr_c(lsnes_cmds
, CLOADSAVE::ldab
,
557 [](command::arg_filename args
) {
558 mark_pending_load(args
, LOAD_STATE_ALLBRANCH
);
561 command::fnptr
<command::arg_filename
> CMD_save_state(lsnes_cmds
, CLOADSAVE::sa
,
562 [](command::arg_filename args
) {
563 mark_pending_save(args
, SAVE_STATE
, -1);
566 command::fnptr
<command::arg_filename
> CMD_save_state2(lsnes_cmds
, CLOADSAVE::sasb
,
567 [](command::arg_filename args
) {
568 mark_pending_save(args
, SAVE_STATE
, 1);
571 command::fnptr
<command::arg_filename
> CMD_save_state3(lsnes_cmds
, CLOADSAVE::sasz
,
572 [](command::arg_filename args
) {
573 mark_pending_save(args
, SAVE_STATE
, 0);
576 command::fnptr
<command::arg_filename
> CMD_save_movie(lsnes_cmds
, CLOADSAVE::sam
,
577 [](command::arg_filename args
) {
578 mark_pending_save(args
, SAVE_MOVIE
, -1);
581 command::fnptr
<command::arg_filename
> CMD_save_movie2(lsnes_cmds
, CLOADSAVE::samb
,
582 [](command::arg_filename args
) {
583 mark_pending_save(args
, SAVE_MOVIE
, 1);
586 command::fnptr
<command::arg_filename
> CMD_save_movie3(lsnes_cmds
, CLOADSAVE::samz
,
587 [](command::arg_filename args
) {
588 mark_pending_save(args
, SAVE_MOVIE
, 0);
591 command::fnptr
<command::arg_filename
> CMD_load_rom(lsnes_cmds
, CLOADSAVE::lrom
,
592 [](command::arg_filename args
) {
598 command::fnptr
<> CMD_reload_rom(lsnes_cmds
, CLOADSAVE::rlrom
,
600 reload_current_rom();
603 command::fnptr
<> CMD_close_rom(lsnes_cmds
, CLOADSAVE::clrom
,
608 command::fnptr
<> CMD_set_rwmode(lsnes_cmds
, "set-rwmode", "Switch to recording mode",
609 "Syntax: set-rwmode\nSwitches to recording mode\n",
612 core
.lua2
->callback_movie_lost("readwrite");
613 core
.mlogic
->get_movie().readonly_mode(false);
614 core
.dispatch
->mode_change(false);
615 core
.lua2
->callback_do_readwrite();
616 core
.supdater
->update();
619 command::fnptr
<> CMD_set_romode(lsnes_cmds
, "set-romode", "Switch to playback mode",
620 "Syntax: set-romode\nSwitches to playback mode\n",
623 core
.mlogic
->get_movie().readonly_mode(true);
624 core
.dispatch
->mode_change(true);
625 core
.supdater
->update();
628 command::fnptr
<> CMD_toggle_rwmode(lsnes_cmds
, "toggle-rwmode", "Toggle recording mode",
629 "Syntax: toggle-rwmode\nToggles recording mode\n",
632 bool c
= core
.mlogic
->get_movie().readonly_mode();
634 core
.lua2
->callback_movie_lost("readwrite");
635 core
.mlogic
->get_movie().readonly_mode(!c
);
636 core
.dispatch
->mode_change(!c
);
638 core
.lua2
->callback_do_readwrite();
639 core
.supdater
->update();
642 command::fnptr
<> CMD_repaint(lsnes_cmds
, "repaint", "Redraw the screen",
643 "Syntax: repaint\nRedraws the screen\n",
645 CORE().fbuf
->redraw_framebuffer();
648 command::fnptr
<> CMD_tpon(lsnes_cmds
, "toggle-pause-on-end", "Toggle pause on end", "Toggle pause on end\n",
651 bool tmp
= SET_pause_on_end(*core
.settings
);
652 SET_pause_on_end(*core
.settings
, !tmp
);
653 messages
<< "Pause-on-end is now " << (tmp
? "OFF" : "ON") << std::endl
;
656 command::fnptr
<> CMD_spon(lsnes_cmds
, "set-pause-on-end", "Set pause on end", "Set pause on end\n",
658 SET_pause_on_end(*CORE().settings
, true);
659 messages
<< "Pause-on-end is now ON" << std::endl
;
662 command::fnptr
<> CMD_cpon(lsnes_cmds
, "clear-pause-on-end", "Clear pause on end", "Clear pause on end\n",
664 SET_pause_on_end(*CORE().settings
, false);
665 messages
<< "Pause-on-end is now OFF" << std::endl
;
668 command::fnptr
<> CMD_rewind_movie(lsnes_cmds
, CLOADSAVE::rewind
,
670 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_BEGINNING
);
673 command::fnptr
<> CMD_cancel_save(lsnes_cmds
, CLOADSAVE::cancel
,
675 queued_saves
.clear();
676 messages
<< "Pending saves canceled." << std::endl
;
679 command::fnptr
<> CMD_mhold1(lsnes_cmds
, CMHOLD::p
, []() {
683 command::fnptr
<> CMD_mhold2(lsnes_cmds
, CMHOLD::r
, []() {
684 macro_hold_1
= false;
687 command::fnptr
<> CMD_mhold3(lsnes_cmds
, CMHOLD::t
, []() {
688 macro_hold_2
= !macro_hold_2
;
690 messages
<< "Macros are held for next frame." << std::endl
;
692 messages
<< "Macros are not held for next frame." << std::endl
;
695 keyboard::invbind_info
IBIND_ipause_emulator(lsnes_invbinds
, "pause-emulator", "Speed‣(Un)pause");
696 keyboard::invbind_info
IBIND_iadvframe(lsnes_invbinds
, "+advance-frame", "Speed‣Advance frame");
697 keyboard::invbind_info
IBIND_iadvsubframe(lsnes_invbinds
, "+advance-poll", "Speed‣Advance subframe");
698 keyboard::invbind_info
IBIND_iskiplag(lsnes_invbinds
, "advance-skiplag", "Speed‣Advance poll");
699 keyboard::invbind_info
IBIND_ireset(lsnes_invbinds
, "reset", "System‣Reset");
700 keyboard::invbind_info
IBIND_iset_rwmode(lsnes_invbinds
, "set-rwmode", "Movie‣Switch to recording");
701 keyboard::invbind_info
IBIND_itoggle_romode(lsnes_invbinds
, "set-romode", "Movie‣Switch to playback");
702 keyboard::invbind_info
IBIND_itoggle_rwmode(lsnes_invbinds
, "toggle-rwmode", "Movie‣Toggle playback");
703 keyboard::invbind_info
IBIND_irepaint(lsnes_invbinds
, "repaint", "System‣Repaint screen");
704 keyboard::invbind_info
IBIND_itogglepause(lsnes_invbinds
, "toggle-pause-on-end", "Movie‣Toggle pause-on-end");
706 class mywindowcallbacks
: public master_dumper::notifier
709 mywindowcallbacks(emulator_dispatch
& dispatch
, emulator_runmode
& runmode
, status_updater
& _supdater
)
710 : supdater(_supdater
)
712 closenotify
.set(dispatch
.close
, [this, &runmode
]() {
715 platform::set_paused(false);
716 platform::cancel_wait();
721 ~mywindowcallbacks() throw() {}
722 void dump_status_change() throw()
727 struct dispatch::target
<> closenotify
;
728 status_updater
& supdater
;
731 //If there is a pending load, perform it. Return 1 on successful load, 0 if nothing to load, -1 on load
736 std::string old_project
= *core
.mlogic
? core
.mlogic
->get_mfile().projectid
: "";
738 if(do_unsafe_rewind
&& unsafe_rewind_obj
) {
741 uint64_t t
= framerate_regulator::get_utime();
742 core
.lua2
->callback_do_unsafe_rewind(core
.mlogic
->get_movie(), unsafe_rewind_obj
);
743 core
.dispatch
->mode_change(false);
744 do_unsafe_rewind
= false;
745 core
.runmode
->set_point(emulator_runmode::P_SAVE
);
746 core
.supdater
->update();
747 core
.runmode
->end_load(); //Restore previous mode.
748 messages
<< "Rewind done in " << (framerate_regulator::get_utime() - t
) << " usec."
752 if(pending_new_project
!= "") {
753 std::string id
= pending_new_project
;
754 pending_new_project
= "";
755 project_info
* old
= core
.project
->get();
756 if(old
&& old
->id
== id
)
759 auto& p
= core
.project
->load(id
);
760 core
.project
->set(&p
);
761 if(core
.project
->get() != old
)
763 core
.slotcache
->flush(); //Wrong movie may be stale.
764 core
.runmode
->end_load(); //Restore previous mode.
765 if(core
.mlogic
->get_mfile().dyn
.save_frame
)
766 core
.runmode
->set_point(emulator_runmode::P_SAVE
);
767 core
.supdater
->update();
769 } catch(std::exception
& e
) {
770 platform::error_message(std::string("Can't switch projects: ") + e
.what());
771 messages
<< "Can't switch projects: " << e
.what() << std::endl
;
775 core
.runmode
->end_load();
776 platform::set_paused(core
.runmode
->is_paused());
777 platform::flush_command_queue();
778 if(core
.runmode
->is_load())
782 if(pending_load
!= "") {
784 if(loadmode
!= LOAD_STATE_BEGINNING
&& loadmode
!= LOAD_STATE_ROMRELOAD
&&
785 !do_load_state(pending_load
, loadmode
)) {
786 core
.runmode
->end_load();
790 if(loadmode
== LOAD_STATE_BEGINNING
)
792 if(loadmode
== LOAD_STATE_ROMRELOAD
)
794 core
.runmode
->clear_corrupt();
795 } catch(std::exception
& e
) {
796 core
.runmode
->set_corrupt();
797 platform::error_message(std::string("Load failed: ") + e
.what());
798 messages
<< "Load failed: " << e
.what() << std::endl
;
801 if(!core
.runmode
->is_corrupt()) {
802 core
.runmode
->end_load();
803 core
.runmode
->set_point(emulator_runmode::P_SAVE
);
804 core
.supdater
->update();
805 platform::flush_command_queue();
806 if(core
.runmode
->is_quit())
808 if(core
.runmode
->is_load())
811 if(old_project
!= (*core
.mlogic
? core
.mlogic
->get_mfile().projectid
: ""))
812 core
.slotcache
->flush(); //Wrong movie may be stale.
818 //If there are pending saves, perform them.
824 if(!queued_saves
.empty() || (do_unsafe_rewind
&& !unsafe_rewind_obj
)) {
825 core
.rom
->runtosave();
826 for(auto i
: queued_saves
) {
827 do_save_state(i
.first
, i
.second
);
829 core
.slotcache
->flush(translate_name_mprefix(i
.first
, tmp
, -1));
831 if(do_unsafe_rewind
&& !unsafe_rewind_obj
) {
832 uint64_t t
= framerate_regulator::get_utime();
833 core
.mlogic
->get_mfile().dyn
.savestate
= core
.rom
->save_core_state(true);
834 core
.lua2
->callback_do_unsafe_rewind(core
.mlogic
->get_movie(), NULL
);
835 do_unsafe_rewind
= false;
836 messages
<< "Rewind point set in " << (framerate_regulator::get_utime() - t
)
837 << " usec." << std::endl
;
840 queued_saves
.clear();
843 bool handle_corrupt()
846 if(!core
.runmode
->is_corrupt())
848 while(core
.runmode
->is_corrupt()) {
849 platform::set_paused(true);
850 platform::flush_command_queue();
852 if(core
.runmode
->is_quit())
859 void init_main_callbacks()
861 ecore_callbacks
= &lsnes_callbacks_obj
;
864 void frob_with_value(unsigned a
, unsigned b
, unsigned c
, short& d
)
867 core
.lua2
->callback_frob_with_value(a
, b
, c
, d
);
871 void main_loop(struct loaded_rom
& rom
, struct moviefile
& initial
, bool load_has_to_succeed
)
873 lsnes_instance
.emu_thread
= threads::id();
875 //Set up the frob with inputs routine.
876 core
.mlogic
->set_frob_with_value(frob_with_value
);
877 mywindowcallbacks
mywcb(*core
.dispatch
, *core
.runmode
, *core
.supdater
);
878 core
.iqueue
->system_thread_available
= true;
879 //Basic initialization.
880 core
.commentary
->init();
881 core
.fbuf
->init_special_screens();
882 core
.jukebox
->set_update([&core
]() { core
.supdater
->update(); });
884 init_main_callbacks();
885 initialize_all_builtin_c_cores();
886 core_core::install_all_handlers();
888 //Load our given movie.
889 bool first_round
= false;
890 bool just_did_loadstate
= false;
893 do_load_state(initial
, LOAD_STATE_INITIAL
, used
);
894 core
.runmode
->set_point(emulator_runmode::P_SAVE
);
895 core
.supdater
->update();
896 first_round
= core
.mlogic
->get_mfile().dyn
.save_frame
;
897 just_did_loadstate
= first_round
;
898 } catch(std::bad_alloc
& e
) {
900 } catch(std::exception
& e
) {
903 platform::error_message(std::string("Can't load initial state: ") + e
.what());
904 messages
<< "ERROR: Can't load initial state: " << e
.what() << std::endl
;
905 if(load_has_to_succeed
) {
906 messages
<< "FATAL: Can't load movie" << std::endl
;
909 core
.runmode
->set_corrupt();
910 core
.fbuf
->redraw_framebuffer(emu_framebuffer::screen_corrupt
);
913 core
.runmode
->set_pause_cond(initial
.start_paused
);
914 platform::set_paused(core
.runmode
->is_paused());
915 stop_at_frame_active
= false;
917 core
.lua2
->run_startup_scripts();
919 while(!core
.runmode
->is_quit() || !queued_saves
.empty()) {
920 if(handle_corrupt()) {
921 first_round
= *core
.mlogic
&& core
.mlogic
->get_mfile().dyn
.save_frame
;
922 just_did_loadstate
= first_round
;
925 core
.framerate
->ack_frame_tick(framerate_regulator::get_utime());
926 core
.runmode
->decay_skiplag();
929 core
.controls
->reset_framehold();
930 if(!macro_hold_1
&& !macro_hold_2
) {
931 core
.controls
->advance_macros();
933 macro_hold_2
= false;
934 core
.mlogic
->get_movie().get_pollcounters().set_framepflag(false);
935 core
.mlogic
->new_frame_starting(core
.runmode
->is_skiplag());
936 core
.mlogic
->get_movie().get_pollcounters().set_framepflag(true);
937 if(core
.runmode
->is_quit() && queued_saves
.empty())
941 if(queued_saves
.empty())
943 if(r
> 0 || core
.runmode
->is_corrupt()) {
944 core
.mlogic
->get_movie().get_pollcounters().set_framepflag(
945 core
.mlogic
->get_mfile().dyn
.save_frame
!= 0);
946 first_round
= core
.mlogic
->get_mfile().dyn
.save_frame
;
947 stop_at_frame_active
= false;
948 just_did_loadstate
= first_round
;
949 core
.controls
->reset_framehold();
950 core
.dbg
->do_callback_frame(core
.mlogic
->get_movie().get_current_frame(), true);
953 //Not exactly desriable, but this at least won't desync.
954 stop_at_frame_active
= false;
955 if(core
.runmode
->is_quit())
957 core
.runmode
->set_pause();
960 if(just_did_loadstate
) {
961 //If we just loadstated, we are up to date.
962 if(core
.runmode
->is_quit())
964 platform::set_paused(core
.runmode
->is_paused());
965 platform::flush_command_queue();
966 //We already have done the reset this frame if we are going to do one at all.
967 core
.mlogic
->get_movie().set_controls(core
.mlogic
->update_controls(true));
968 core
.mlogic
->get_movie().set_all_DRDY();
969 just_did_loadstate
= false;
971 core
.dbg
->do_callback_frame(core
.mlogic
->get_movie().get_current_frame(), false);
973 random_mix_timing_entropy();
974 if(core
.runmode
->is_freerunning())
975 platform::wait(core
.framerate
->to_wait_frame(framerate_regulator::get_utime()));
977 core
.lua2
->callback_do_frame();
980 core
.jukebox
->unset_update();
981 core
.mdumper
->end_dumps();
982 core
.commentary
->kill();
983 core
.iqueue
->system_thread_available
= false;
984 //Kill some things to avoid crashes.
985 core
.dbg
->core_change();
986 core
.project
->set(NULL
, true);
987 core
.mwatch
->clear_multi(core
.mwatch
->enumerate());
991 //Uninstall the handlers. Don't do this with ROM loaded.
992 core_core::uninstall_all_handlers();
995 void set_stop_at_frame(uint64_t frame
)
998 stop_at_frame
= frame
;
999 stop_at_frame_active
= (frame
!= 0);
1000 if(!core
.runmode
->is_special())
1001 core
.runmode
->set_freerunning();
1002 platform::set_paused(false);
1005 void do_flush_slotinfo()
1007 CORE().slotcache
->flush();
1010 void switch_projects(const std::string
& newproj
)
1012 pending_new_project
= newproj
;
1013 CORE().runmode
->start_load();
1014 platform::cancel_wait();
1015 platform::set_paused(false);
1018 void load_new_rom(const romload_request
& req
)
1020 if(_load_new_rom(req
)) {
1021 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD
);
1025 void reload_current_rom()
1027 if(reload_active_rom()) {
1028 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD
);
1034 if(load_null_rom()) {
1035 CORE().runmode
->set_pause();
1036 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD
);
1040 void do_break_pause()
1042 auto& core
= CORE();
1043 core
.runmode
->set_break();
1044 core
.supdater
->update();
1045 core
.fbuf
->redraw_framebuffer();
1046 while(core
.runmode
->is_paused_break()) {
1047 platform::set_paused(true);
1048 platform::flush_command_queue();
1052 void convert_break_to_pause()
1054 auto& core
= CORE();
1055 if(core
.runmode
->is_paused_break()) {
1056 core
.runmode
->set_pause();
1057 core
.supdater
->update();