3 #include "core/command.hpp"
4 #include "core/controller.hpp"
5 #include "core/dispatch.hpp"
6 #include "core/framebuffer.hpp"
7 #include "core/framerate.hpp"
8 #include "core/inthread.hpp"
9 #include "core/multitrack.hpp"
10 #include "lua/lua.hpp"
11 #include "library/string.hpp"
12 #include "core/mainloop.hpp"
13 #include "core/movie.hpp"
14 #include "core/moviedata.hpp"
15 #include "core/moviefile.hpp"
16 #include "core/memorymanip.hpp"
17 #include "core/memorywatch.hpp"
18 #include "core/project.hpp"
19 #include "core/rom.hpp"
20 #include "core/romloader.hpp"
21 #include "core/rrdata.hpp"
22 #include "core/settings.hpp"
23 #include "core/window.hpp"
24 #include "interface/callbacks.hpp"
25 #include "interface/romtype.hpp"
26 #include "library/framebuffer.hpp"
27 #include "library/pixfmt-lrgb.hpp"
28 #include "library/zip.hpp"
38 #define SPECIAL_FRAME_START 0
39 #define SPECIAL_FRAME_VIDEO 1
40 #define SPECIAL_SAVEPOINT 2
41 #define SPECIAL_NONE 3
43 void update_movie_state();
44 time_t random_seed_value
= 0;
46 setting_var
<setting_var_model_bool
<setting_yes_no
>> jukebox_dflt_binary(lsnes_vset
, "jukebox-default-binary",
47 "Movie‣Saving‣Saveslots binary", true);
48 setting_var
<setting_var_model_bool
<setting_yes_no
>> movie_dflt_binary(lsnes_vset
, "movie-default-binary",
49 "Movie‣Saving‣Movies binary", false);
50 setting_var
<setting_var_model_bool
<setting_yes_no
>> save_dflt_binary(lsnes_vset
, "savestate-default-binary",
51 "Movie‣Saving‣Savestates binary", false);
55 setting_var
<setting_var_model_int
<0,999999>> advance_timeout_first(lsnes_vset
, "advance-timeout",
56 "Delays‣First frame advance", 500);
57 setting_var
<setting_var_model_int
<0,999999>> advance_timeout_subframe(lsnes_vset
, "advance-subframe-timeout",
58 "Delays‣Subframe advance", 100);
59 setting_var
<setting_var_model_bool
<setting_yes_no
>> pause_on_end(lsnes_vset
, "pause-on-end",
60 "Movie‣Pause on end", false);
61 setting_var
<setting_var_model_int
<0,999999999>> jukebox_size(lsnes_vset
, "jukebox-size",
62 "Movie‣Number of save slots", 12);
66 ADVANCE_QUIT
, //Quit the emulator.
67 ADVANCE_AUTO
, //Normal (possibly slowed down play).
68 ADVANCE_LOAD
, //Loading a state.
69 ADVANCE_FRAME
, //Frame advance.
70 ADVANCE_SUBFRAME
, //Subframe advance.
71 ADVANCE_SKIPLAG
, //Skip lag (oneshot, reverts to normal).
72 ADVANCE_SKIPLAG_PENDING
, //Activate skip lag mode at next frame.
73 ADVANCE_PAUSE
, //Unconditional pause.
77 threadid_class emulation_thread
;
78 //Flags related to repeating advance.
81 //Emulator advance mode. Detemines pauses at start of frame / subframe, etc..
82 enum advance_mode amode
;
83 //Mode and filename of pending load, one of LOAD_* constants.
86 std::string pending_load
;
87 std::string pending_new_project
;
88 //Queued saves (all savestates).
89 std::set
<std::pair
<std::string
, int>> queued_saves
;
91 size_t save_jukebox_pointer
;
92 //Special subframe location. One of SPECIAL_* constants.
95 bool last_hires
= false;
96 bool last_interlace
= false;
98 bool do_unsafe_rewind
= false;
99 void* unsafe_rewind_obj
= NULL
;
101 bool stop_at_frame_active
= false;
102 uint64_t stop_at_frame
= 0;
107 enum advance_mode old_mode
;
109 std::string
save_jukebox_name(size_t i
)
111 return (stringfmt() << "${project}" << (i
+ 1) << ".lsmv").str();
114 std::map
<std::string
, std::string
> slotinfo_cache
;
116 std::string
vector_to_string(const std::vector
<char>& x
)
118 std::string
y(x
.begin(), x
.end());
119 while(y
.length() > 0 && y
[y
.length() - 1] < 32)
120 y
= y
.substr(0, y
.length() - 1);
124 std::string
get_slotinfo(const std::string
& _filename
)
126 std::string filename
= resolve_relative_path(_filename
);
127 if(!slotinfo_cache
.count(filename
)) {
128 std::ostringstream out
;
130 moviefile::brief_info
info(filename
);
131 if(our_movie
.projectid
== info
.projectid
)
132 out
<< info
.rerecords
<< "R/" << info
.current_frame
<< "F";
134 out
<< "Wrong movie";
136 out
<< "Nonexistent";
138 slotinfo_cache
[filename
] = out
.str();
140 return slotinfo_cache
[filename
];
143 void flush_slotinfo(const std::string
& filename
)
145 slotinfo_cache
.erase(resolve_relative_path(filename
));
148 void flush_slotinfo()
150 slotinfo_cache
.clear();
153 class _lsnes_pflag_handler
: public movie::poll_flag
156 ~_lsnes_pflag_handler()
161 return our_rom
.rtype
->get_pflag();
163 void set_pflag(int flag
)
165 our_rom
.rtype
->set_pflag(flag
);
167 } lsnes_pflag_handler
;
170 void mainloop_signal_need_rewind(void* ptr
)
174 amode
= ADVANCE_LOAD
;
176 do_unsafe_rewind
= true;
177 unsafe_rewind_obj
= ptr
;
180 controller_frame
movie_logic::update_controls(bool subframe
) throw(std::bad_alloc
, std::runtime_error
)
182 if(lua_requests_subframe_paint
)
183 redraw_framebuffer();
186 if(amode
== ADVANCE_SUBFRAME
) {
187 if(!cancel_advance
) {
189 platform::wait(advance_timeout_first
* 1000);
191 platform::wait(advance_timeout_subframe
* 1000);
192 advanced_once
= true;
195 stop_at_frame_active
= false;
196 amode
= ADVANCE_PAUSE
;
197 cancel_advance
= false;
199 platform::set_paused(amode
== ADVANCE_PAUSE
);
200 } else if(amode
== ADVANCE_FRAME
) {
203 if(amode
== ADVANCE_SKIPLAG
) {
204 stop_at_frame_active
= false;
205 amode
= ADVANCE_PAUSE
;
207 platform::set_paused(amode
== ADVANCE_PAUSE
);
208 cancel_advance
= false;
210 location_special
= SPECIAL_NONE
;
211 update_movie_state();
213 if(amode
== ADVANCE_SKIPLAG_PENDING
)
214 amode
= ADVANCE_SKIPLAG
;
215 if(amode
== ADVANCE_FRAME
|| amode
== ADVANCE_SUBFRAME
) {
216 if(!cancel_advance
) {
219 wait
= advance_timeout_first
* 1000;
220 else if(amode
== ADVANCE_SUBFRAME
)
221 wait
= advance_timeout_subframe
* 1000;
223 wait
= to_wait_frame(get_utime());
224 platform::wait(wait
);
225 advanced_once
= true;
228 stop_at_frame_active
= false;
229 amode
= ADVANCE_PAUSE
;
230 cancel_advance
= false;
232 platform::set_paused(amode
== ADVANCE_PAUSE
);
233 } else if(amode
== ADVANCE_AUTO
&& movb
.get_movie().readonly_mode() && pause_on_end
&&
234 !stop_at_frame_active
) {
235 if(movb
.get_movie().get_current_frame() == movb
.get_movie().get_frame_count()) {
236 stop_at_frame_active
= false;
237 amode
= ADVANCE_PAUSE
;
238 platform::set_paused(true);
240 } else if(amode
== ADVANCE_AUTO
&& stop_at_frame_active
) {
241 if(movb
.get_movie().get_current_frame() >= stop_at_frame
) {
242 stop_at_frame_active
= false;
243 amode
= ADVANCE_PAUSE
;
244 platform::set_paused(true);
247 platform::set_paused((amode
== ADVANCE_PAUSE
));
248 cancel_advance
= false;
250 location_special
= SPECIAL_FRAME_START
;
251 update_movie_state();
253 platform::flush_command_queue();
254 controller_frame tmp
= controls
.get(movb
.get_movie().get_current_frame());
255 our_rom
.rtype
->pre_emulate_frame(tmp
); //Preset controls, the lua will override if needed.
256 lua_callback_do_input(tmp
, subframe
);
257 multitrack_editor
.process_frame(tmp
);
258 controls
.commit(tmp
);
265 //Do pending load (automatically unpauses).
266 void mark_pending_load(const std::string
& filename
, int lmode
)
269 pending_load
= filename
;
271 amode
= ADVANCE_LOAD
;
272 platform::cancel_wait();
273 platform::set_paused(false);
276 void mark_pending_save(const std::string
& filename
, int smode
, int binary
)
279 if(smode
== SAVE_MOVIE
) {
280 //Just do this immediately.
281 do_save_movie(filename
, binary
);
282 flush_slotinfo(translate_name_mprefix(filename
, tmp
, false));
285 if(location_special
== SPECIAL_SAVEPOINT
) {
286 //We can save immediately here.
287 do_save_state(filename
, binary
);
288 flush_slotinfo(translate_name_mprefix(filename
, tmp
, false));
291 queued_saves
.insert(std::make_pair(filename
, binary
));
292 messages
<< "Pending save on '" << filename
<< "'" << std::endl
;
295 struct jukebox_size_listener
: public setting_var_listener
297 jukebox_size_listener() { lsnes_vset
.add_listener(*this); }
298 ~jukebox_size_listener() throw() {lsnes_vset
.remove_listener(*this); };
299 void on_setting_change(setting_var_group
& grp
, const setting_var_base
& val
)
301 if(val
.get_iname() == "jukebox-size") {
302 if(save_jukebox_pointer
>= jukebox_size
)
303 save_jukebox_pointer
= 0;
305 update_movie_state();
310 void update_movie_state()
312 bool readonly
= false;
313 static unsigned last_controllers
= 0;
316 our_rom
.region
->fill_framerate_magic(magic
);
317 voice_frame_number(movb
.get_movie().get_current_frame(), 1.0 * magic
[1] / magic
[0]);
319 auto& _status
= platform::get_emustatus();
320 if(!system_corrupt
) {
321 _status
.set("!frame", (stringfmt() << movb
.get_movie().get_current_frame()).str());
322 _status
.set("!length", (stringfmt() << movb
.get_movie().get_frame_count()).str());
323 _status
.set("!lag", (stringfmt() << movb
.get_movie().get_lag_frames()).str());
324 if(location_special
== SPECIAL_FRAME_START
)
325 _status
.set("!subframe", "0");
326 else if(location_special
== SPECIAL_SAVEPOINT
)
327 _status
.set("!subframe", "S");
328 else if(location_special
== SPECIAL_FRAME_VIDEO
)
329 _status
.set("!subframe", "V");
331 _status
.set("!subframe", (stringfmt() << movb
.get_movie().next_poll_number()).str());
333 _status
.set("!frame", "N/A");
334 _status
.set("!length", "N/A");
335 _status
.set("!lag", "N/A");
336 _status
.set("!subframe", "N/A");
339 _status
.set("!dumping", (information_dispatch::get_dumper_count() ? "Y" : ""));
340 auto& mo
= movb
.get_movie();
341 readonly
= mo
.readonly_mode();
343 _status
.set("!mode", "C");
345 _status
.set("!mode", "R");
346 else if(mo
.get_frame_count() >= mo
.get_current_frame())
347 _status
.set("!mode", "P");
349 _status
.set("!mode", "F");
351 if(jukebox_size
> 0) {
353 std::string sfilen
= translate_name_mprefix(save_jukebox_name(save_jukebox_pointer
), tmp
, false);
354 _status
.set("!saveslot", (stringfmt() << (save_jukebox_pointer
+ 1)).str());
355 _status
.set("!saveslotinfo", get_slotinfo(sfilen
));
357 _status
.erase("!saveslot");
358 _status
.erase("!saveslotinfo");
360 _status
.set("!speed", (stringfmt() << (unsigned)(100 * get_realized_multiplier() + 0.5)).str());
362 if(!system_corrupt
) {
363 time_t timevalue
= static_cast<time_t>(our_movie
.rtc_second
);
364 struct tm
* time_decompose
= gmtime(&timevalue
);
365 char datebuffer
[512];
366 strftime(datebuffer
, 511, "%Y%m%d(%a)T%H%M%S", time_decompose
);
367 _status
.set("RTC", datebuffer
);
369 _status
.set("RTC", "N/A");
372 auto mset
= controls
.active_macro_set();
374 std::ostringstream mss
;
376 if(!mfirst
) mss
<< ",";
380 _status
.set("!macros", mss
.str());
385 if(!multitrack_editor
.any_records())
386 c
= movb
.get_movie().get_controls();
388 c
= controls
.get_committed();
389 for(unsigned i
= 0;; i
++) {
390 auto pindex
= controls
.lcid_to_pcid(i
);
391 if(pindex
.first
< 0 || !controls
.is_present(pindex
.first
, pindex
.second
)) {
392 for(unsigned j
= i
; j
< last_controllers
; j
++)
393 _status
.erase((stringfmt() << "P" << (j
+ 1)).str());
394 last_controllers
= i
;
397 char32_t buffer
[MAX_DISPLAY_LENGTH
];
398 c
.display(pindex
.first
, pindex
.second
, buffer
);
399 std::u32string _buffer
= buffer
;
400 if(readonly
&& multitrack_editor
.is_enabled()) {
401 multitrack_edit::state st
= multitrack_editor
.get(pindex
.first
, pindex
.second
);
402 if(st
== multitrack_edit::MT_PRESERVE
)
403 _buffer
+= U
" (keep)";
404 else if(st
== multitrack_edit::MT_OVERWRITE
)
405 _buffer
+= U
" (rewrite)";
406 else if(st
== multitrack_edit::MT_OR
)
408 else if(st
== multitrack_edit::MT_XOR
)
409 _buffer
+= U
" (XOR)";
411 _buffer
+= U
" (\?\?\?)";
413 _status
.set((stringfmt() << "P" << (i
+ 1)).str(), _buffer
);
415 notify_status_update();
418 uint64_t audio_irq_time
;
419 uint64_t controller_irq_time
;
420 uint64_t frame_irq_time
;
422 struct lsnes_callbacks
: public emucore_callbacks
425 ~lsnes_callbacks() throw()
429 int16_t get_input(unsigned port
, unsigned index
, unsigned control
)
432 x
= movb
.input_poll(port
, index
, control
);
433 lua_callback_snoop_input(port
, index
, control
, x
);
437 int16_t set_input(unsigned port
, unsigned index
, unsigned control
, int16_t value
)
439 if(!movb
.get_movie().readonly_mode()) {
440 controller_frame f
= movb
.get_movie().get_controls();
441 f
.axis3(port
, index
, control
, value
);
442 movb
.get_movie().set_controls(f
);
444 return movb
.get_movie().next_input(port
, index
, control
);
447 void timer_tick(uint32_t increment
, uint32_t per_second
)
449 our_movie
.rtc_subsecond
+= increment
;
450 while(our_movie
.rtc_subsecond
>= per_second
) {
451 our_movie
.rtc_second
++;
452 our_movie
.rtc_subsecond
-= per_second
;
456 std::string
get_firmware_path()
458 return lsnes_vset
["firmwarepath"].str();
461 std::string
get_base_path()
463 return our_rom
.msu1_base
;
468 return our_movie
.rtc_second
;
471 time_t get_randomseed()
473 return random_seed_value
;
476 void output_frame(framebuffer_raw
& screen
, uint32_t fps_n
, uint32_t fps_d
)
478 lua_callback_do_frame_emulated();
479 location_special
= SPECIAL_FRAME_VIDEO
;
480 update_movie_state();
481 redraw_framebuffer(screen
, false, true);
482 uint32_t g
= gcd(fps_n
, fps_d
);
485 information_dispatch::do_frame(screen
, fps_n
, fps_d
);
488 void action_state_updated()
490 graphics_driver_action_updated();
496 function_ptr_command
<> count_rerecords(lsnes_cmd
, "count-rerecords", "Count rerecords",
497 "Syntax: count-rerecords\nCounts rerecords.\n",
498 []() throw(std::bad_alloc
, std::runtime_error
) {
499 std::vector
<char> tmp
;
500 uint64_t x
= rrdata
.write(tmp
);
501 messages
<< x
<< " rerecord(s)" << std::endl
;
504 function_ptr_command
<const std::string
&> quit_emulator(lsnes_cmd
, "quit-emulator", "Quit the emulator",
505 "Syntax: quit-emulator [/y]\nQuits emulator (/y => don't ask for confirmation).\n",
506 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
507 amode
= ADVANCE_QUIT
;
508 platform::set_paused(false);
509 platform::cancel_wait();
512 function_ptr_command
<> unpause_emulator(lsnes_cmd
, "unpause-emulator", "Unpause the emulator",
513 "Syntax: unpause-emulator\nUnpauses the emulator.\n",
514 []() throw(std::bad_alloc
, std::runtime_error
) {
515 amode
= ADVANCE_AUTO
;
516 platform::set_paused(false);
517 platform::cancel_wait();
518 messages
<< "Unpaused" << std::endl
;
521 function_ptr_command
<> pause_emulator(lsnes_cmd
, "pause-emulator", "(Un)pause the emulator",
522 "Syntax: pause-emulator\n(Un)pauses the emulator.\n",
523 []() throw(std::bad_alloc
, std::runtime_error
) {
524 if(amode
!= ADVANCE_AUTO
) {
525 amode
= ADVANCE_AUTO
;
526 platform::set_paused(false);
527 platform::cancel_wait();
528 messages
<< "Unpaused" << std::endl
;
530 platform::cancel_wait();
531 cancel_advance
= false;
532 stop_at_frame_active
= false;
533 amode
= ADVANCE_PAUSE
;
534 messages
<< "Paused" << std::endl
;
538 function_ptr_command
<> save_jukebox_prev(lsnes_cmd
, "cycle-jukebox-backward", "Cycle save jukebox backwards",
539 "Syntax: cycle-jukebox-backward\nCycle save jukebox backwards\n",
540 []() throw(std::bad_alloc
, std::runtime_error
) {
541 if(jukebox_size
== 0)
543 if(save_jukebox_pointer
== 0)
544 save_jukebox_pointer
= jukebox_size
- 1;
546 save_jukebox_pointer
--;
547 if(save_jukebox_pointer
>= jukebox_size
)
548 save_jukebox_pointer
= 0;
549 update_movie_state();
552 function_ptr_command
<> save_jukebox_next(lsnes_cmd
, "cycle-jukebox-forward", "Cycle save jukebox forwards",
553 "Syntax: cycle-jukebox-forward\nCycle save jukebox forwards\n",
554 []() throw(std::bad_alloc
, std::runtime_error
) {
555 if(jukebox_size
== 0)
557 if(save_jukebox_pointer
>= jukebox_size
- 1)
558 save_jukebox_pointer
= 0;
560 save_jukebox_pointer
++;
561 if(save_jukebox_pointer
>= jukebox_size
)
562 save_jukebox_pointer
= 0;
563 update_movie_state();
566 function_ptr_command
<> load_jukebox(lsnes_cmd
, "load-jukebox", "Load save from jukebox",
567 "Syntax: load-jukebox\nLoad save from jukebox\n",
568 []() throw(std::bad_alloc
, std::runtime_error
) {
569 if(jukebox_size
== 0)
570 throw std::runtime_error("No slot selected");
571 mark_pending_load(save_jukebox_name(save_jukebox_pointer
), LOAD_STATE_CURRENT
);
574 function_ptr_command
<> load_jukebox_readwrite(lsnes_cmd
, "load-jukebox-readwrite", "Load save from jukebox in"
575 " read-write mode", "Syntax: load-jukebox-readwrite\nLoad save from jukebox in read-write mode\n",
576 []() throw(std::bad_alloc
, std::runtime_error
) {
577 if(jukebox_size
== 0)
578 throw std::runtime_error("No slot selected");
579 mark_pending_load(save_jukebox_name(save_jukebox_pointer
), LOAD_STATE_RW
);
582 function_ptr_command
<> load_jukebox_readonly(lsnes_cmd
, "load-jukebox-readonly", "Load save from jukebox in "
583 "read-only mode", "Syntax: load-jukebox-readonly\nLoad save from jukebox in read-only mode\n",
584 []() throw(std::bad_alloc
, std::runtime_error
) {
585 if(jukebox_size
== 0)
586 throw std::runtime_error("No slot selected");
587 mark_pending_load(save_jukebox_name(save_jukebox_pointer
), LOAD_STATE_RO
);
590 function_ptr_command
<> load_jukebox_preserve(lsnes_cmd
, "load-jukebox-preserve", "Load save from jukebox, "
591 "preserving input", "Syntax: load-jukebox-preserve\nLoad save from jukebox, preserving input\n",
592 []() throw(std::bad_alloc
, std::runtime_error
) {
593 if(jukebox_size
== 0)
594 throw std::runtime_error("No slot selected");
595 mark_pending_load(save_jukebox_name(save_jukebox_pointer
), LOAD_STATE_PRESERVE
);
598 function_ptr_command
<> load_jukebox_movie(lsnes_cmd
, "load-jukebox-movie", "Load save from jukebox as movie",
599 "Syntax: load-jukebox-movie\nLoad save from jukebox as movie\n",
600 []() throw(std::bad_alloc
, std::runtime_error
) {
601 if(jukebox_size
== 0)
602 throw std::runtime_error("No slot selected");
603 mark_pending_load(save_jukebox_name(save_jukebox_pointer
), LOAD_STATE_MOVIE
);
606 function_ptr_command
<> save_jukebox_c(lsnes_cmd
, "save-jukebox", "Save save to jukebox",
607 "Syntax: save-jukebox\nSave save to jukebox\n",
608 []() throw(std::bad_alloc
, std::runtime_error
) {
609 if(jukebox_size
== 0)
610 throw std::runtime_error("No slot selected");
611 mark_pending_save(save_jukebox_name(save_jukebox_pointer
), SAVE_STATE
, -1);
614 function_ptr_command
<> padvance_frame(lsnes_cmd
, "+advance-frame", "Advance one frame",
615 "Syntax: +advance-frame\nAdvances the emulation by one frame.\n",
616 []() throw(std::bad_alloc
, std::runtime_error
) {
617 amode
= ADVANCE_FRAME
;
618 cancel_advance
= false;
619 advanced_once
= false;
620 platform::cancel_wait();
621 platform::set_paused(false);
624 function_ptr_command
<> nadvance_frame(lsnes_cmd
, "-advance-frame", "Advance one frame",
625 "No help available\n",
626 []() throw(std::bad_alloc
, std::runtime_error
) {
627 cancel_advance
= true;
628 platform::cancel_wait();
629 platform::set_paused(false);
632 function_ptr_command
<> padvance_poll(lsnes_cmd
, "+advance-poll", "Advance one subframe",
633 "Syntax: +advance-poll\nAdvances the emulation by one subframe.\n",
634 []() throw(std::bad_alloc
, std::runtime_error
) {
635 amode
= ADVANCE_SUBFRAME
;
636 cancel_advance
= false;
637 advanced_once
= false;
638 platform::cancel_wait();
639 platform::set_paused(false);
642 function_ptr_command
<> nadvance_poll(lsnes_cmd
, "-advance-poll", "Advance one subframe",
643 "No help available\n",
644 []() throw(std::bad_alloc
, std::runtime_error
) {
645 cancel_advance
= true;
646 platform::cancel_wait();
647 platform::set_paused(false);
650 function_ptr_command
<> advance_skiplag(lsnes_cmd
, "advance-skiplag", "Skip to next poll",
651 "Syntax: advance-skiplag\nAdvances the emulation to the next poll.\n",
652 []() throw(std::bad_alloc
, std::runtime_error
) {
653 amode
= ADVANCE_SKIPLAG_PENDING
;
654 platform::cancel_wait();
655 platform::set_paused(false);
658 function_ptr_command
<> reset_c(lsnes_cmd
, "reset", "Reset the system",
659 "Syntax: reset\nReset\nResets the system in beginning of the next frame.\n",
660 []() throw(std::bad_alloc
, std::runtime_error
) {
661 int sreset_action
= our_rom
.rtype
->reset_action(false);
662 if(sreset_action
< 0) {
663 platform::error_message("Core does not support resets");
664 messages
<< "Emulator core does not support resets" << std::endl
;
667 our_rom
.rtype
->execute_action(sreset_action
, std::vector
<interface_action_paramval
>());
670 function_ptr_command
<> hreset_c(lsnes_cmd
, "reset-hard", "Reset the system",
671 "Syntax: reset-hard\nReset-hard\nHard resets the system in beginning of the next frame.\n",
672 []() throw(std::bad_alloc
, std::runtime_error
) {
673 int hreset_action
= our_rom
.rtype
->reset_action(true);
674 if(hreset_action
< 0) {
675 platform::error_message("Core does not support hard resets");
676 messages
<< "Emulator core does not support hard resets" << std::endl
;
679 our_rom
.rtype
->execute_action(hreset_action
, std::vector
<interface_action_paramval
>());
682 function_ptr_command
<arg_filename
> load_c(lsnes_cmd
, "load", "Load savestate (current mode)",
683 "Syntax: load <file>\nLoads SNES state from <file> in current mode\n",
684 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
685 mark_pending_load(args
, LOAD_STATE_CURRENT
);
688 function_ptr_command
<arg_filename
> load_smart_c(lsnes_cmd
, "load-smart", "Load savestate (heuristic mode)",
689 "Syntax: load <file>\nLoads SNES state from <file> in heuristic mode\n",
690 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
691 mark_pending_load(args
, LOAD_STATE_DEFAULT
);
694 function_ptr_command
<arg_filename
> load_state_c(lsnes_cmd
, "load-state", "Load savestate (R/W)",
695 "Syntax: load-state <file>\nLoads SNES state from <file> in Read/Write mode\n",
696 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
697 mark_pending_load(args
, LOAD_STATE_RW
);
700 function_ptr_command
<arg_filename
> load_readonly(lsnes_cmd
, "load-readonly", "Load savestate (RO)",
701 "Syntax: load-readonly <file>\nLoads SNES state from <file> in read-only mode\n",
702 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
703 mark_pending_load(args
, LOAD_STATE_RO
);
706 function_ptr_command
<arg_filename
> load_preserve(lsnes_cmd
, "load-preserve", "Load savestate (preserve "
707 "input)", "Syntax: load-preserve <file>\nLoads SNES state from <file> preserving input\n",
708 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
709 mark_pending_load(args
, LOAD_STATE_PRESERVE
);
712 function_ptr_command
<arg_filename
> load_movie_c(lsnes_cmd
, "load-movie", "Load movie",
713 "Syntax: load-movie <file>\nLoads SNES movie from <file>\n",
714 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
715 mark_pending_load(args
, LOAD_STATE_MOVIE
);
719 function_ptr_command
<arg_filename
> save_state(lsnes_cmd
, "save-state", "Save state",
720 "Syntax: save-state <file>\nSaves SNES state to <file>\n",
721 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
722 mark_pending_save(args
, SAVE_STATE
, -1);
725 function_ptr_command
<arg_filename
> save_state2(lsnes_cmd
, "save-state-binary", "Save state (binary)",
726 "Syntax: save-state-binary <file>\nSaves binary state to <file>\n",
727 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
728 mark_pending_save(args
, SAVE_STATE
, 1);
731 function_ptr_command
<arg_filename
> save_state3(lsnes_cmd
, "save-state-zip", "Save state (zip)",
732 "Syntax: save-state-zip <file>\nSaves zip state to <file>\n",
733 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
734 mark_pending_save(args
, SAVE_STATE
, 0);
737 function_ptr_command
<arg_filename
> save_movie(lsnes_cmd
, "save-movie", "Save movie",
738 "Syntax: save-movie <file>\nSaves SNES movie to <file>\n",
739 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
740 mark_pending_save(args
, SAVE_MOVIE
, -1);
743 function_ptr_command
<arg_filename
> save_movie2(lsnes_cmd
, "save-movie-binary", "Save movie (binary)",
744 "Syntax: save-movie-binary <file>\nSaves binary movie to <file>\n",
745 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
746 mark_pending_save(args
, SAVE_MOVIE
, 1);
749 function_ptr_command
<arg_filename
> save_movie3(lsnes_cmd
, "save-movie-zip", "Save movie (zip)",
750 "Syntax: save-movie-zip <file>\nSaves zip movie to <file>\n",
751 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
752 mark_pending_save(args
, SAVE_MOVIE
, 0);
755 function_ptr_command
<> set_rwmode(lsnes_cmd
, "set-rwmode", "Switch to read/write mode",
756 "Syntax: set-rwmode\nSwitches to read/write mode\n",
757 []() throw(std::bad_alloc
, std::runtime_error
) {
758 lua_callback_movie_lost("readwrite");
759 movb
.get_movie().readonly_mode(false);
760 notify_mode_change(false);
761 lua_callback_do_readwrite();
762 update_movie_state();
765 function_ptr_command
<> set_romode(lsnes_cmd
, "set-romode", "Switch to read-only mode",
766 "Syntax: set-romode\nSwitches to read-only mode\n",
767 []() throw(std::bad_alloc
, std::runtime_error
) {
768 movb
.get_movie().readonly_mode(true);
769 notify_mode_change(true);
770 update_movie_state();
773 function_ptr_command
<> toggle_rwmode(lsnes_cmd
, "toggle-rwmode", "Toggle read/write mode",
774 "Syntax: toggle-rwmode\nToggles read/write mode\n",
775 []() throw(std::bad_alloc
, std::runtime_error
) {
776 bool c
= movb
.get_movie().readonly_mode();
778 lua_callback_movie_lost("readwrite");
779 movb
.get_movie().readonly_mode(!c
);
780 notify_mode_change(!c
);
782 lua_callback_do_readwrite();
783 update_movie_state();
786 function_ptr_command
<> repaint(lsnes_cmd
, "repaint", "Redraw the screen",
787 "Syntax: repaint\nRedraws the screen\n",
788 []() throw(std::bad_alloc
, std::runtime_error
) {
789 redraw_framebuffer();
792 function_ptr_command
<> tpon(lsnes_cmd
, "toggle-pause-on-end", "Toggle pause on end", "Toggle pause on end\n",
793 []() throw(std::bad_alloc
, std::runtime_error
) {
794 bool tmp
= pause_on_end
;
795 pause_on_end
.set(!tmp
);
796 messages
<< "Pause-on-end is now " << (tmp
? "OFF" : "ON") << std::endl
;
799 function_ptr_command
<> spon(lsnes_cmd
, "set-pause-on-end", "Set pause on end", "Set pause on end\n",
800 []() throw(std::bad_alloc
, std::runtime_error
) {
801 pause_on_end
.set(true);
802 messages
<< "Pause-on-end is now ON" << std::endl
;
805 function_ptr_command
<> cpon(lsnes_cmd
, "clear-pause-on-end", "Clear pause on end", "Clear pause on end\n",
806 []() throw(std::bad_alloc
, std::runtime_error
) {
807 pause_on_end
.set(false);
808 messages
<< "Pause-on-end is now OFF" << std::endl
;
811 function_ptr_command
<> rewind_movie(lsnes_cmd
, "rewind-movie", "Rewind movie to the beginning",
812 "Syntax: rewind-movie\nRewind movie to the beginning\n",
813 []() throw(std::bad_alloc
, std::runtime_error
) {
814 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_BEGINNING
);
817 function_ptr_command
<> cancel_save(lsnes_cmd
, "cancel-saves", "Cancel all pending saves", "Syntax: "
818 "cancel-save\nCancel pending saves\n",
819 []() throw(std::bad_alloc
, std::runtime_error
) {
820 queued_saves
.clear();
821 messages
<< "Pending saves canceled." << std::endl
;
824 function_ptr_command
<> flushslots(lsnes_cmd
, "flush-slotinfo", "Flush slotinfo cache",
825 "Flush slotinfo cache\n",
826 []() throw(std::bad_alloc
, std::runtime_error
) {
830 function_ptr_command
<> mhold1(lsnes_cmd
, "+hold-macro", "Hold macro (hold)",
831 "Hold macros enable\n", []() throw(std::bad_alloc
, std::runtime_error
) {
835 function_ptr_command
<> mhold2(lsnes_cmd
, "-hold-macro", "Hold macro (hold)",
836 "Hold macros disable\n", []() throw(std::bad_alloc
, std::runtime_error
) {
837 macro_hold_1
= false;
840 function_ptr_command
<> mhold3(lsnes_cmd
, "hold-macro", "Hold macro (toggle)",
841 "Hold macros toggle\n", []() throw(std::bad_alloc
, std::runtime_error
) {
842 macro_hold_2
= !macro_hold_2
;
844 messages
<< "Macros are held for next frame." << std::endl
;
846 messages
<< "Macros are not held for next frame." << std::endl
;
849 inverse_bind
imhold1(lsnes_mapper
, "+hold-macro", "Macro‣Hold all macros");
850 inverse_bind
imhold2(lsnes_mapper
, "hold-macro", "Macro‣Hold all macros (typed)");
851 inverse_bind
ipause_emulator(lsnes_mapper
, "pause-emulator", "Speed‣(Un)pause");
852 inverse_bind
ijback(lsnes_mapper
, "cycle-jukebox-backward", "Slot select‣Cycle backwards");
853 inverse_bind
ijforward(lsnes_mapper
, "cycle-jukebox-forward", "Slot select‣Cycle forwards");
854 inverse_bind
iloadj(lsnes_mapper
, "load-jukebox", "Load‣Selected slot");
855 inverse_bind
iloadjrw(lsnes_mapper
, "load-jukebox-readwrite", "Load‣Selected slot (readwrite mode)");
856 inverse_bind
iloadjro(lsnes_mapper
, "load-jukebox-readonly", "Load‣Selected slot (readonly mode)");
857 inverse_bind
iloadjp(lsnes_mapper
, "load-jukebox-preserve", "Load‣Selected slot (preserve input)");
858 inverse_bind
iloadjm(lsnes_mapper
, "load-jukebox-movie", "Load‣Selected slot (as movie)");
859 inverse_bind
isavej(lsnes_mapper
, "save-jukebox", "Save‣Selected slot");
860 inverse_bind
iadvframe(lsnes_mapper
, "+advance-frame", "Speed‣Advance frame");
861 inverse_bind
iadvsubframe(lsnes_mapper
, "+advance-poll", "Speed‣Advance subframe");
862 inverse_bind
iskiplag(lsnes_mapper
, "advance-skiplag", "Speed‣Advance poll");
863 inverse_bind
ireset(lsnes_mapper
, "reset", "System‣Reset");
864 inverse_bind
iset_rwmode(lsnes_mapper
, "set-rwmode", "Movie‣Switch to read/write");
865 inverse_bind
itoggle_romode(lsnes_mapper
, "set-romode", "Movie‣Switch to read-only");
866 inverse_bind
itoggle_rwmode(lsnes_mapper
, "toggle-rwmode", "Movie‣Toggle read-only");
867 inverse_bind
irepaint(lsnes_mapper
, "repaint", "System‣Repaint screen");
868 inverse_bind
itogglepause(lsnes_mapper
, "toggle-pause-on-end", "Movie‣Toggle pause-on-end");
869 inverse_bind
irewind_movie(lsnes_mapper
, "rewind-movie", "Movie‣Rewind movie");
870 inverse_bind
icancel_saves(lsnes_mapper
, "cancel-saves", "Save‣Cancel pending saves");
871 inverse_bind
iload1(lsnes_mapper
, "load ${project}1.lsmv", "Load‣Slot 1");
872 inverse_bind
iload2(lsnes_mapper
, "load ${project}2.lsmv", "Load‣Slot 2");
873 inverse_bind
iload3(lsnes_mapper
, "load ${project}3.lsmv", "Load‣Slot 3");
874 inverse_bind
iload4(lsnes_mapper
, "load ${project}4.lsmv", "Load‣Slot 4");
875 inverse_bind
iload5(lsnes_mapper
, "load ${project}5.lsmv", "Load‣Slot 5");
876 inverse_bind
iload6(lsnes_mapper
, "load ${project}6.lsmv", "Load‣Slot 6");
877 inverse_bind
iload7(lsnes_mapper
, "load ${project}7.lsmv", "Load‣Slot 7");
878 inverse_bind
iload8(lsnes_mapper
, "load ${project}8.lsmv", "Load‣Slot 8");
879 inverse_bind
iload9(lsnes_mapper
, "load ${project}9.lsmv", "Load‣Slot 9");
880 inverse_bind
iload10(lsnes_mapper
, "load ${project}10.lsmv", "Load‣Slot 10");
881 inverse_bind
iload11(lsnes_mapper
, "load ${project}11.lsmv", "Load‣Slot 11");
882 inverse_bind
iload12(lsnes_mapper
, "load ${project}12.lsmv", "Load‣Slot 12");
883 inverse_bind
iload13(lsnes_mapper
, "load ${project}13.lsmv", "Load‣Slot 13");
884 inverse_bind
iload14(lsnes_mapper
, "load ${project}14.lsmv", "Load‣Slot 14");
885 inverse_bind
iload15(lsnes_mapper
, "load ${project}15.lsmv", "Load‣Slot 15");
886 inverse_bind
iload16(lsnes_mapper
, "load ${project}16.lsmv", "Load‣Slot 16");
887 inverse_bind
iload17(lsnes_mapper
, "load ${project}17.lsmv", "Load‣Slot 17");
888 inverse_bind
iload18(lsnes_mapper
, "load ${project}18.lsmv", "Load‣Slot 18");
889 inverse_bind
iload19(lsnes_mapper
, "load ${project}19.lsmv", "Load‣Slot 19");
890 inverse_bind
iload20(lsnes_mapper
, "load ${project}20.lsmv", "Load‣Slot 20");
891 inverse_bind
iload21(lsnes_mapper
, "load ${project}21.lsmv", "Load‣Slot 21");
892 inverse_bind
iload22(lsnes_mapper
, "load ${project}22.lsmv", "Load‣Slot 22");
893 inverse_bind
iload23(lsnes_mapper
, "load ${project}23.lsmv", "Load‣Slot 23");
894 inverse_bind
iload24(lsnes_mapper
, "load ${project}24.lsmv", "Load‣Slot 24");
895 inverse_bind
iload25(lsnes_mapper
, "load ${project}25.lsmv", "Load‣Slot 25");
896 inverse_bind
iload26(lsnes_mapper
, "load ${project}26.lsmv", "Load‣Slot 26");
897 inverse_bind
iload27(lsnes_mapper
, "load ${project}27.lsmv", "Load‣Slot 27");
898 inverse_bind
iload28(lsnes_mapper
, "load ${project}28.lsmv", "Load‣Slot 28");
899 inverse_bind
iload29(lsnes_mapper
, "load ${project}29.lsmv", "Load‣Slot 29");
900 inverse_bind
iload30(lsnes_mapper
, "load ${project}30.lsmv", "Load‣Slot 30");
901 inverse_bind
iload31(lsnes_mapper
, "load ${project}31.lsmv", "Load‣Slot 31");
902 inverse_bind
iload32(lsnes_mapper
, "load ${project}32.lsmv", "Load‣Slot 32");
903 inverse_bind
isave1(lsnes_mapper
, "save-state ${project}1.lsmv", "Save‣Slot 1");
904 inverse_bind
isave2(lsnes_mapper
, "save-state ${project}2.lsmv", "Save‣Slot 2");
905 inverse_bind
isave3(lsnes_mapper
, "save-state ${project}3.lsmv", "Save‣Slot 3");
906 inverse_bind
isave4(lsnes_mapper
, "save-state ${project}4.lsmv", "Save‣Slot 4");
907 inverse_bind
isave5(lsnes_mapper
, "save-state ${project}5.lsmv", "Save‣Slot 5");
908 inverse_bind
isave6(lsnes_mapper
, "save-state ${project}6.lsmv", "Save‣Slot 6");
909 inverse_bind
isave7(lsnes_mapper
, "save-state ${project}7.lsmv", "Save‣Slot 7");
910 inverse_bind
isave8(lsnes_mapper
, "save-state ${project}8.lsmv", "Save‣Slot 8");
911 inverse_bind
isave9(lsnes_mapper
, "save-state ${project}9.lsmv", "Save‣Slot 9");
912 inverse_bind
isave10(lsnes_mapper
, "save-state ${project}10.lsmv", "Save‣Slot 10");
913 inverse_bind
isave11(lsnes_mapper
, "save-state ${project}11.lsmv", "Save‣Slot 11");
914 inverse_bind
isave12(lsnes_mapper
, "save-state ${project}12.lsmv", "Save‣Slot 12");
915 inverse_bind
isave13(lsnes_mapper
, "save-state ${project}13.lsmv", "Save‣Slot 13");
916 inverse_bind
isave14(lsnes_mapper
, "save-state ${project}14.lsmv", "Save‣Slot 14");
917 inverse_bind
isave15(lsnes_mapper
, "save-state ${project}15.lsmv", "Save‣Slot 15");
918 inverse_bind
isave16(lsnes_mapper
, "save-state ${project}16.lsmv", "Save‣Slot 16");
919 inverse_bind
isave17(lsnes_mapper
, "save-state ${project}17.lsmv", "Save‣Slot 17");
920 inverse_bind
isave18(lsnes_mapper
, "save-state ${project}18.lsmv", "Save‣Slot 18");
921 inverse_bind
isave19(lsnes_mapper
, "save-state ${project}19.lsmv", "Save‣Slot 19");
922 inverse_bind
isave20(lsnes_mapper
, "save-state ${project}20.lsmv", "Save‣Slot 20");
923 inverse_bind
isave21(lsnes_mapper
, "save-state ${project}21.lsmv", "Save‣Slot 21");
924 inverse_bind
isave22(lsnes_mapper
, "save-state ${project}22.lsmv", "Save‣Slot 22");
925 inverse_bind
isave23(lsnes_mapper
, "save-state ${project}23.lsmv", "Save‣Slot 23");
926 inverse_bind
isave24(lsnes_mapper
, "save-state ${project}24.lsmv", "Save‣Slot 24");
927 inverse_bind
isave25(lsnes_mapper
, "save-state ${project}25.lsmv", "Save‣Slot 25");
928 inverse_bind
isave26(lsnes_mapper
, "save-state ${project}26.lsmv", "Save‣Slot 26");
929 inverse_bind
isave27(lsnes_mapper
, "save-state ${project}27.lsmv", "Save‣Slot 27");
930 inverse_bind
isave28(lsnes_mapper
, "save-state ${project}28.lsmv", "Save‣Slot 28");
931 inverse_bind
isave29(lsnes_mapper
, "save-state ${project}29.lsmv", "Save‣Slot 29");
932 inverse_bind
isave30(lsnes_mapper
, "save-state ${project}30.lsmv", "Save‣Slot 30");
933 inverse_bind
isave31(lsnes_mapper
, "save-state ${project}31.lsmv", "Save‣Slot 31");
934 inverse_bind
isave32(lsnes_mapper
, "save-state ${project}32.lsmv", "Save‣Slot 32");
936 bool on_quit_prompt
= false;
937 class mywindowcallbacks
: public information_dispatch
940 mywindowcallbacks() : information_dispatch("mainloop-window-callbacks")
942 closenotify
.set(notify_close
, [this]() {
944 amode
= ADVANCE_QUIT
;
945 platform::set_paused(false);
946 platform::cancel_wait();
949 on_quit_prompt
= true;
951 amode
= ADVANCE_QUIT
;
952 platform::set_paused(false);
953 platform::cancel_wait();
956 on_quit_prompt
= false;
959 ~mywindowcallbacks() throw() {}
960 void on_new_dumper(const std::string
& n
)
962 update_movie_state();
964 void on_destroy_dumper(const std::string
& n
)
966 update_movie_state();
969 struct dispatch_target
<> closenotify
;
972 //If there is a pending load, perform it. Return 1 on successful load, 0 if nothing to load, -1 on load
976 std::string old_project
= our_movie
.projectid
;
978 if(do_unsafe_rewind
&& unsafe_rewind_obj
) {
979 uint64_t t
= get_utime();
981 lua_callback_do_unsafe_rewind(s
, 0, 0, movb
.get_movie(), unsafe_rewind_obj
);
982 notify_mode_change(false);
983 do_unsafe_rewind
= false;
984 our_movie
.is_savestate
= true;
985 location_special
= SPECIAL_SAVEPOINT
;
986 update_movie_state();
987 messages
<< "Rewind done in " << (get_utime() - t
) << " usec." << std::endl
;
990 if(pending_new_project
!= "") {
991 std::string id
= pending_new_project
;
992 pending_new_project
= "";
993 project_info
* old
= project_get();
994 if(old
&& old
->id
== id
)
997 auto& p
= project_load(id
);
999 if(project_get() != old
)
1001 flush_slotinfo(); //Wrong movie may be stale.
1003 } catch(std::exception
& e
) {
1004 platform::error_message(std::string("Can't switch projects: ") + e
.what());
1005 messages
<< "Can't switch projects: " << e
.what() << std::endl
;
1010 platform::set_paused(amode
== ADVANCE_PAUSE
);
1011 platform::flush_command_queue();
1012 if(amode
== ADVANCE_LOAD
)
1016 if(pending_load
!= "") {
1017 system_corrupt
= false;
1018 if(loadmode
!= LOAD_STATE_BEGINNING
&& loadmode
!= LOAD_STATE_ROMRELOAD
&&
1019 !do_load_state(pending_load
, loadmode
)) {
1020 movb
.get_movie().set_pflag_handler(&lsnes_pflag_handler
);
1025 if(loadmode
== LOAD_STATE_BEGINNING
)
1026 do_load_beginning(false);
1027 if(loadmode
== LOAD_STATE_ROMRELOAD
)
1028 do_load_beginning(true);
1029 } catch(std::exception
& e
) {
1030 platform::error_message(std::string("Load failed: ") + e
.what());
1031 messages
<< "Load failed: " << e
.what() << std::endl
;
1033 movb
.get_movie().set_pflag_handler(&lsnes_pflag_handler
);
1035 amode
= load_paused
? ADVANCE_PAUSE
: ADVANCE_AUTO
;
1036 platform::set_paused(load_paused
);
1037 load_paused
= false;
1038 if(!system_corrupt
) {
1039 location_special
= SPECIAL_SAVEPOINT
;
1040 update_movie_state();
1041 platform::flush_command_queue();
1042 if(amode
== ADVANCE_QUIT
)
1044 if(amode
== ADVANCE_LOAD
)
1047 if(old_project
!= our_movie
.projectid
)
1048 flush_slotinfo(); //Wrong movie may be stale.
1054 //If there are pending saves, perform them.
1057 if(!queued_saves
.empty() || (do_unsafe_rewind
&& !unsafe_rewind_obj
)) {
1058 our_rom
.rtype
->runtosave();
1059 for(auto i
: queued_saves
) {
1060 do_save_state(i
.first
, i
.second
);
1062 flush_slotinfo(translate_name_mprefix(i
.first
, tmp
, false));
1064 if(do_unsafe_rewind
&& !unsafe_rewind_obj
) {
1065 uint64_t t
= get_utime();
1066 std::vector
<char> s
= our_rom
.save_core_state(true);
1067 uint64_t secs
= our_movie
.rtc_second
;
1068 uint64_t ssecs
= our_movie
.rtc_subsecond
;
1069 lua_callback_do_unsafe_rewind(s
, secs
, ssecs
, movb
.get_movie(), NULL
);
1070 do_unsafe_rewind
= false;
1071 messages
<< "Rewind point set in " << (get_utime() - t
) << " usec." << std::endl
;
1074 queued_saves
.clear();
1077 bool handle_corrupt()
1081 while(system_corrupt
) {
1082 platform::set_paused(true);
1083 platform::flush_command_queue();
1085 if(amode
== ADVANCE_QUIT
)
1092 void main_loop(struct loaded_rom
& rom
, struct moviefile
& initial
, bool load_has_to_succeed
) throw(std::bad_alloc
,
1095 platform::system_thread_available(true);
1096 //Basic initialization.
1097 dispatch_set_error_streams(&messages
.getstream());
1098 emulation_thread
= this_thread_id();
1099 jukebox_size_listener jlistener
;
1101 init_special_screens();
1103 lsnes_callbacks lsnes_callbacks_obj
;
1104 ecore_callbacks
= &lsnes_callbacks_obj
;
1105 movb
.get_movie().set_pflag_handler(&lsnes_pflag_handler
);
1106 core_core::install_all_handlers();
1108 //Load our given movie.
1109 bool first_round
= false;
1110 bool just_did_loadstate
= false;
1112 do_load_state(initial
, LOAD_STATE_INITIAL
);
1113 location_special
= SPECIAL_SAVEPOINT
;
1114 update_movie_state();
1115 first_round
= our_movie
.is_savestate
;
1116 just_did_loadstate
= first_round
;
1117 } catch(std::bad_alloc
& e
) {
1119 } catch(std::exception
& e
) {
1120 platform::error_message(std::string("Can't load initial state: ") + e
.what());
1121 messages
<< "ERROR: Can't load initial state: " << e
.what() << std::endl
;
1122 if(load_has_to_succeed
) {
1123 messages
<< "FATAL: Can't load movie" << std::endl
;
1124 platform::fatal_error();
1126 system_corrupt
= true;
1127 update_movie_state();
1128 redraw_framebuffer(screen_corrupt
);
1131 movb
.get_movie().set_pflag_handler(&lsnes_pflag_handler
);
1132 lua_callback_startup();
1134 platform::set_paused(initial
.start_paused
);
1135 amode
= initial
.start_paused
? ADVANCE_PAUSE
: ADVANCE_AUTO
;
1136 stop_at_frame_active
= false;
1137 uint64_t time_x
= get_utime();
1138 while(amode
!= ADVANCE_QUIT
|| !queued_saves
.empty()) {
1139 if(handle_corrupt()) {
1140 first_round
= our_movie
.is_savestate
;
1141 just_did_loadstate
= first_round
;
1144 ack_frame_tick(get_utime());
1145 if(amode
== ADVANCE_SKIPLAG_PENDING
)
1146 amode
= ADVANCE_SKIPLAG
;
1149 controls
.reset_framehold();
1150 movb
.get_movie().get_pollcounters().set_framepflag(false);
1151 movb
.new_frame_starting(amode
== ADVANCE_SKIPLAG
);
1152 movb
.get_movie().get_pollcounters().set_framepflag(true);
1153 if(!macro_hold_1
&& !macro_hold_2
) {
1154 controls
.advance_macros();
1156 macro_hold_2
= false;
1157 if(amode
== ADVANCE_QUIT
&& queued_saves
.empty())
1161 if(queued_saves
.empty())
1163 if(r
> 0 || system_corrupt
) {
1164 movb
.get_movie().get_pollcounters().set_framepflag(our_movie
.is_savestate
);
1165 first_round
= our_movie
.is_savestate
;
1167 amode
= ADVANCE_PAUSE
;
1170 stop_at_frame_active
= false;
1171 just_did_loadstate
= first_round
;
1172 controls
.reset_framehold();
1175 //Not exactly desriable, but this at least won't desync.
1176 stop_at_frame_active
= false;
1177 if(amode
== ADVANCE_QUIT
)
1179 amode
= ADVANCE_PAUSE
;
1182 if(just_did_loadstate
) {
1183 //If we just loadstated, we are up to date.
1184 if(amode
== ADVANCE_QUIT
)
1186 platform::set_paused(amode
== ADVANCE_PAUSE
);
1187 platform::flush_command_queue();
1188 //We already have done the reset this frame if we are going to do one at all.
1189 movb
.get_movie().set_controls(movb
.update_controls(true));
1190 movb
.get_movie().set_all_DRDY();
1191 just_did_loadstate
= false;
1193 frame_irq_time
= get_utime() - time_x
;
1194 our_rom
.rtype
->emulate();
1195 time_x
= get_utime();
1196 if(amode
== ADVANCE_AUTO
)
1197 platform::wait(to_wait_frame(get_utime()));
1198 first_round
= false;
1199 lua_callback_do_frame();
1201 information_dispatch::do_dump_end();
1202 core_core::uninstall_all_handlers();
1204 platform::system_thread_available(false);
1207 void set_stop_at_frame(uint64_t frame
)
1209 stop_at_frame
= frame
;
1210 stop_at_frame_active
= (frame
!= 0);
1211 amode
= ADVANCE_AUTO
;
1212 platform::set_paused(false);
1215 void do_flush_slotinfo()
1220 void switch_projects(const std::string
& newproj
)
1222 pending_new_project
= newproj
;
1223 amode
= ADVANCE_LOAD
;
1224 old_mode
= ADVANCE_PAUSE
;
1225 platform::cancel_wait();
1226 platform::set_paused(false);
1229 void load_new_rom(const romload_request
& req
)
1231 if(_load_new_rom(req
)) {
1232 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD
);
1236 void reload_current_rom()
1238 if(reload_active_rom()) {
1239 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD
);
1245 if(load_null_rom()) {
1247 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_ROMRELOAD
);