2 #include <snes/snes.hpp>
3 #include <ui-libsnes/libsnes.hpp>
5 #include "core/command.hpp"
6 #include "core/controller.hpp"
7 #include "core/dispatch.hpp"
8 #include "core/framebuffer.hpp"
9 #include "core/framerate.hpp"
10 #include "core/lua.hpp"
11 #include "core/mainloop.hpp"
12 #include "core/movie.hpp"
13 #include "core/moviedata.hpp"
14 #include "core/moviefile.hpp"
15 #include "core/memorymanip.hpp"
16 #include "core/memorywatch.hpp"
17 #include "core/render.hpp"
18 #include "core/rom.hpp"
19 #include "core/rrdata.hpp"
20 #include "core/settings.hpp"
21 #include "core/window.hpp"
30 #define SPECIAL_FRAME_START 0
31 #define SPECIAL_FRAME_VIDEO 1
32 #define SPECIAL_SAVEPOINT 2
33 #define SPECIAL_NONE 3
35 void update_movie_state();
36 time_t random_seed_value
= 0;
42 ADVANCE_QUIT
, //Quit the emulator.
43 ADVANCE_AUTO
, //Normal (possibly slowed down play).
44 ADVANCE_LOAD
, //Loading a state.
45 ADVANCE_FRAME
, //Frame advance.
46 ADVANCE_SUBFRAME
, //Subframe advance.
47 ADVANCE_SKIPLAG
, //Skip lag (oneshot, reverts to normal).
48 ADVANCE_SKIPLAG_PENDING
, //Activate skip lag mode at next frame.
49 ADVANCE_PAUSE
, //Unconditional pause.
52 //Previous mouse mask.
53 int prev_mouse_mask
= 0;
54 //Flags related to repeating advance.
57 //Emulator advance mode. Detemines pauses at start of frame / subframe, etc..
58 enum advance_mode amode
;
59 //Mode and filename of pending load, one of LOAD_* constants.
61 std::string pending_load
;
62 //Queued saves (all savestates).
63 std::set
<std::string
> queued_saves
;
64 bool stepping_into_save
;
66 std::vector
<std::string
> save_jukebox
;
67 size_t save_jukebox_pointer
;
68 //Pending reset cycles. -1 if no reset pending, otherwise, cycle count for reset.
69 long pending_reset_cycles
= -1;
70 //Set by every video refresh.
71 bool video_refresh_done
;
72 //Special subframe location. One of SPECIAL_* constants.
75 numeric_setting
advance_timeout_first("advance-timeout", 0, 999999999, 500);
76 boolean_setting
pause_on_end("pause-on-end", false);
78 bool last_hires
= false;
79 bool last_interlace
= false;
82 class firmware_path_setting
: public setting
85 firmware_path_setting() : setting("firmwarepath") { _firmwarepath
= "."; default_firmware
= true; }
86 void blank() throw(std::bad_alloc
, std::runtime_error
)
89 default_firmware
= true;
94 return !default_firmware
;
97 void set(const std::string
& value
) throw(std::bad_alloc
, std::runtime_error
)
99 _firmwarepath
= value
;
100 default_firmware
= false;
103 std::string
get() throw(std::bad_alloc
)
105 return _firmwarepath
;
108 operator std::string() throw(std::bad_alloc
)
110 return _firmwarepath
;
113 std::string _firmwarepath
;
114 bool default_firmware
;
115 } firmwarepath_setting
;
117 controller_frame
movie_logic::update_controls(bool subframe
) throw(std::bad_alloc
, std::runtime_error
)
119 if(lua_requests_subframe_paint
)
120 redraw_framebuffer();
123 if(amode
== ADVANCE_SUBFRAME
) {
124 if(!cancel_advance
&& !advanced_once
) {
125 platform::wait(advance_timeout_first
* 1000);
126 advanced_once
= true;
129 amode
= ADVANCE_PAUSE
;
130 cancel_advance
= false;
132 platform::set_paused(amode
== ADVANCE_PAUSE
);
133 } else if(amode
== ADVANCE_FRAME
) {
136 if(amode
== ADVANCE_SKIPLAG
)
137 amode
= ADVANCE_PAUSE
;
138 platform::set_paused(amode
== ADVANCE_PAUSE
);
139 cancel_advance
= false;
141 if(amode
== ADVANCE_SKIPLAG
)
142 amode
= ADVANCE_AUTO
;
143 location_special
= SPECIAL_NONE
;
144 update_movie_state();
146 if(amode
== ADVANCE_SKIPLAG_PENDING
)
147 amode
= ADVANCE_SKIPLAG
;
148 if(amode
== ADVANCE_FRAME
|| amode
== ADVANCE_SUBFRAME
) {
149 if(!cancel_advance
) {
150 platform::wait(advanced_once
? to_wait_frame(get_utime()) :
151 (advance_timeout_first
* 1000));
152 advanced_once
= true;
155 amode
= ADVANCE_PAUSE
;
156 cancel_advance
= false;
158 platform::set_paused(amode
== ADVANCE_PAUSE
);
159 } else if(amode
== ADVANCE_AUTO
&& movb
.get_movie().readonly_mode() && pause_on_end
) {
160 if(movb
.get_movie().get_current_frame() == movb
.get_movie().get_frame_count() + 1) {
161 amode
= ADVANCE_PAUSE
;
162 platform::set_paused(true);
165 platform::set_paused((amode
== ADVANCE_PAUSE
));
166 cancel_advance
= false;
168 location_special
= SPECIAL_FRAME_START
;
169 update_movie_state();
172 information_dispatch::do_status_update();
173 platform::flush_command_queue();
174 if(!subframe
&& pending_reset_cycles
>= 0)
175 controls
.reset(pending_reset_cycles
);
178 controller_frame tmp
= controls
.commit(movb
.get_movie().get_current_frame());
179 lua_callback_do_input(tmp
, subframe
);
185 enum advance_mode old_mode
;
187 //Do pending load (automatically unpauses).
188 void mark_pending_load(const std::string
& filename
, int lmode
)
191 pending_load
= filename
;
193 amode
= ADVANCE_LOAD
;
194 platform::cancel_wait();
195 platform::set_paused(false);
198 void mark_pending_save(const std::string
& filename
, int smode
)
200 if(smode
== SAVE_MOVIE
) {
201 //Just do this immediately.
202 do_save_movie(filename
);
205 queued_saves
.insert(filename
);
206 messages
<< "Pending save on '" << filename
<< "'" << std::endl
;
209 uint32_t lpalette
[0x80000];
212 static bool palette_init
= false;
216 for(unsigned i
= 0; i
< 0x80000; i
++) {
217 unsigned l
= (i
>> 15) & 0xF;
218 unsigned r
= (i
>> 0) & 0x1F;
219 unsigned g
= (i
>> 5) & 0x1F;
220 unsigned b
= (i
>> 10) & 0x1F;
221 double _l
= static_cast<double>(l
);
222 double m
= 17.0 / 31.0;
223 r
= floor(m
* r
* _l
+ 0.5);
224 g
= floor(m
* g
* _l
+ 0.5);
225 b
= floor(m
* b
* _l
+ 0.5);
226 lpalette
[i
] = r
* 65536 + g
* 256 + b
;
231 void update_movie_state()
233 auto& _status
= platform::get_emustatus();
234 if(!system_corrupt
) {
235 std::ostringstream x
;
236 x
<< movb
.get_movie().get_current_frame() << "(";
237 if(location_special
== SPECIAL_FRAME_START
)
239 else if(location_special
== SPECIAL_SAVEPOINT
)
241 else if(location_special
== SPECIAL_FRAME_VIDEO
)
244 x
<< movb
.get_movie().next_poll_number();
245 x
<< ";" << movb
.get_movie().get_lag_frames() << ")/" << movb
.get_movie().get_frame_count();
246 _status
.set("Frame", x
.str());
248 _status
.set("Frame", "N/A");
249 if(!system_corrupt
) {
250 time_t timevalue
= static_cast<time_t>(our_movie
.rtc_second
);
251 struct tm
* time_decompose
= gmtime(&timevalue
);
252 char datebuffer
[512];
253 strftime(datebuffer
, 511, "%Y%m%d(%a)T%H%M%S", time_decompose
);
254 _status
.set("RTC", datebuffer
);
256 _status
.set("RTC", "N/A");
259 std::ostringstream x
;
260 auto& mo
= movb
.get_movie();
261 x
<< (information_dispatch::get_dumper_count() ? "D" : "-");
262 x
<< (last_hires
? "H" : "-");
263 x
<< (last_interlace
? "I" : "-");
266 else if(!mo
.readonly_mode())
268 else if(mo
.get_frame_count() >= mo
.get_current_frame())
272 _status
.set("Flags", x
.str());
274 if(save_jukebox
.size() > 0)
275 _status
.set("Saveslot", translate_name_mprefix(save_jukebox
[save_jukebox_pointer
]));
277 _status
.erase("Saveslot");
279 std::ostringstream x
;
280 x
<< get_framerate();
281 _status
.set("SPD%", x
.str());
286 if(movb
.get_movie().readonly_mode())
287 c
= movb
.get_movie().get_controls();
289 c
= controls
.get_committed();
290 for(unsigned i
= 0; i
< 8; i
++) {
291 unsigned pindex
= controls
.lcid_to_pcid(i
);
292 devicetype_t dtype
= controls
.pcid_to_type(pindex
);
295 char buffer
[MAX_DISPLAY_LENGTH
];
296 c
.display(pindex
, buffer
);
297 char y
[3] = {'P', 0, 0};
299 _status
.set(y
, buffer
);
303 uint64_t audio_irq_time
;
304 uint64_t controller_irq_time
;
305 uint64_t frame_irq_time
;
308 class my_interface
: public SNES::Interface
310 string
path(SNES::Cartridge::Slot slot
, const string
&hint
)
312 const char* _hint
= hint
;
313 std::string _hint2
= _hint
;
314 std::string fwp
= firmwarepath_setting
;
315 std::string finalpath
= fwp
+ "/" + _hint2
;
316 return finalpath
.c_str();
321 return our_movie
.rtc_second
;
326 return random_seed_value
;
329 void videoRefresh(const uint32_t* data
, bool hires
, bool interlace
, bool overscan
)
331 // uint64_t time_x = get_utime();
333 last_interlace
= interlace
;
335 if(stepping_into_save
)
336 messages
<< "Got video refresh in runtosave, expect desyncs!" << std::endl
;
337 video_refresh_done
= true;
338 lua_callback_do_frame_emulated();
339 bool region
= (SNES::system
.region() == SNES::System::Region::PAL
);
340 information_dispatch::do_raw_frame(data
, hires
, interlace
, overscan
, region
? VIDEO_REGION_PAL
:
342 //std::cerr << "Frame: hires flag is " << (hires ? " " : "un") << "set." << std::endl;
343 //std::cerr << "Frame: interlace flag is " << (interlace ? " " : "un") << "set." << std::endl;
344 //std::cerr << "Frame: overscan flag is " << (overscan ? " " : "un") << "set." << std::endl;
345 //std::cerr << "Frame: region flag is " << (region ? " " : "un") << "set." << std::endl;
346 lcscreen
ls(data
, hires
, interlace
, overscan
, region
);
347 location_special
= SPECIAL_FRAME_VIDEO
;
348 update_movie_state();
349 redraw_framebuffer(ls
, false, true);
350 uint32_t fps_n
, fps_d
;
353 fclocks
= interlace
? DURATION_PAL_FIELD
: DURATION_PAL_FRAME
;
355 fclocks
= interlace
? DURATION_NTSC_FIELD
: DURATION_NTSC_FRAME
;
356 fps_n
= SNES::system
.cpu_frequency();
358 uint32_t g
= gcd(fps_n
, fps_d
);
361 information_dispatch::do_frame(ls
, fps_n
, fps_d
);
362 // time_x = get_utime() - time_x;
363 // std::cerr << "IRQ TIMINGS (microseconds): "
364 // << "V: " << time_x << " "
365 // << "A: " << audio_irq_time << " "
366 // << "C: " << controller_irq_time << " "
367 // << "F: " << frame_irq_time << " "
368 // << "Total: " << (time_x + audio_irq_time + controller_irq_time + frame_irq_time) << std::endl;
369 audio_irq_time
= controller_irq_time
= 0;
372 void audioSample(int16_t l_sample
, int16_t r_sample
)
374 // uint64_t time_x = get_utime();
375 uint16_t _l
= l_sample
;
376 uint16_t _r
= r_sample
;
377 platform::audio_sample(_l
+ 32768, _r
+ 32768);
378 information_dispatch::do_sample(l_sample
, r_sample
);
379 //The SMP emits a sample every 768 ticks of its clock. Use this in order to keep track of time.
380 our_movie
.rtc_subsecond
+= 768;
381 while(our_movie
.rtc_subsecond
>= SNES::system
.apu_frequency()) {
382 our_movie
.rtc_second
++;
383 our_movie
.rtc_subsecond
-= SNES::system
.apu_frequency();
385 // audio_irq_time += get_utime() - time_x;
388 int16_t inputPoll(bool port
, SNES::Input::Device device
, unsigned index
, unsigned id
)
390 // uint64_t time_x = get_utime();
392 x
= movb
.input_poll(port
, index
, id
);
393 lua_callback_snoop_input(port
? 1 : 0, index
, id
, x
);
394 // controller_irq_time += get_utime() - time_x;
401 function_ptr_command
<> count_rerecords("count-rerecords", "Count rerecords",
402 "Syntax: count-rerecords\nCounts rerecords.\n",
403 []() throw(std::bad_alloc
, std::runtime_error
) {
404 std::vector
<char> tmp
;
405 uint64_t x
= rrdata::write(tmp
);
406 messages
<< x
<< " rerecord(s)" << std::endl
;
409 function_ptr_command
<const std::string
&> quit_emulator("quit-emulator", "Quit the emulator",
410 "Syntax: quit-emulator [/y]\nQuits emulator (/y => don't ask for confirmation).\n",
411 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
412 amode
= ADVANCE_QUIT
;
413 platform::set_paused(false);
414 platform::cancel_wait();
417 function_ptr_command
<> pause_emulator("pause-emulator", "(Un)pause the emulator",
418 "Syntax: pause-emulator\n(Un)pauses the emulator.\n",
419 []() throw(std::bad_alloc
, std::runtime_error
) {
420 if(amode
!= ADVANCE_AUTO
) {
421 amode
= ADVANCE_AUTO
;
422 platform::set_paused(false);
423 platform::cancel_wait();
424 messages
<< "Unpaused" << std::endl
;
426 platform::cancel_wait();
427 cancel_advance
= false;
428 amode
= ADVANCE_PAUSE
;
429 messages
<< "Paused" << std::endl
;
433 function_ptr_command
<> save_jukebox_prev("cycle-jukebox-backward", "Cycle save jukebox backwards",
434 "Syntax: cycle-jukebox-backward\nCycle save jukebox backwards\n",
435 []() throw(std::bad_alloc
, std::runtime_error
) {
436 if(save_jukebox_pointer
== 0)
437 save_jukebox_pointer
= save_jukebox
.size() - 1;
439 save_jukebox_pointer
--;
440 if(save_jukebox_pointer
>= save_jukebox
.size())
441 save_jukebox_pointer
= 0;
442 update_movie_state();
443 information_dispatch::do_status_update();
446 function_ptr_command
<> save_jukebox_next("cycle-jukebox-forward", "Cycle save jukebox forwards",
447 "Syntax: cycle-jukebox-forward\nCycle save jukebox forwards\n",
448 []() throw(std::bad_alloc
, std::runtime_error
) {
449 if(save_jukebox_pointer
== save_jukebox
.size() - 1)
450 save_jukebox_pointer
= 0;
452 save_jukebox_pointer
++;
453 if(save_jukebox_pointer
>= save_jukebox
.size())
454 save_jukebox_pointer
= 0;
455 update_movie_state();
456 information_dispatch::do_status_update();
459 function_ptr_command
<arg_filename
> add_jukebox("add-jukebox-save", "Add save to jukebox",
460 "Syntax: add-jukebox-save\nAdd save to jukebox\n",
461 [](arg_filename filename
) throw(std::bad_alloc
, std::runtime_error
) {
462 save_jukebox
.push_back(filename
);
463 update_movie_state();
464 information_dispatch::do_status_update();
467 function_ptr_command
<> load_jukebox("load-jukebox", "Load save from jukebox",
468 "Syntax: load-jukebox\nLoad save from jukebox\n",
469 []() throw(std::bad_alloc
, std::runtime_error
) {
470 if(!save_jukebox
.size())
471 throw std::runtime_error("No saves in jukebox");
472 mark_pending_load(save_jukebox
[save_jukebox_pointer
], LOAD_STATE_CURRENT
);
475 function_ptr_command
<> save_jukebox_c("save-jukebox", "Save save to jukebox",
476 "Syntax: save-jukebox\nSave save to jukebox\n",
477 []() throw(std::bad_alloc
, std::runtime_error
) {
478 if(!save_jukebox
.size())
479 throw std::runtime_error("No saves in jukebox");
480 mark_pending_save(save_jukebox
[save_jukebox_pointer
], SAVE_STATE
);
483 function_ptr_command
<> padvance_frame("+advance-frame", "Advance one frame",
484 "Syntax: +advance-frame\nAdvances the emulation by one frame.\n",
485 []() throw(std::bad_alloc
, std::runtime_error
) {
486 amode
= ADVANCE_FRAME
;
487 cancel_advance
= false;
488 advanced_once
= false;
489 platform::cancel_wait();
490 platform::set_paused(false);
493 function_ptr_command
<> nadvance_frame("-advance-frame", "Advance one frame",
494 "No help available\n",
495 []() throw(std::bad_alloc
, std::runtime_error
) {
496 cancel_advance
= true;
497 platform::cancel_wait();
498 platform::set_paused(false);
501 function_ptr_command
<> padvance_poll("+advance-poll", "Advance one subframe",
502 "Syntax: +advance-poll\nAdvances the emulation by one subframe.\n",
503 []() throw(std::bad_alloc
, std::runtime_error
) {
504 amode
= ADVANCE_SUBFRAME
;
505 cancel_advance
= false;
506 advanced_once
= false;
507 platform::cancel_wait();
508 platform::set_paused(false);
511 function_ptr_command
<> nadvance_poll("-advance-poll", "Advance one subframe",
512 "No help available\n",
513 []() throw(std::bad_alloc
, std::runtime_error
) {
514 cancel_advance
= true;
515 platform::cancel_wait();
516 platform::set_paused(false);
519 function_ptr_command
<> advance_skiplag("advance-skiplag", "Skip to next poll",
520 "Syntax: advance-skiplag\nAdvances the emulation to the next poll.\n",
521 []() throw(std::bad_alloc
, std::runtime_error
) {
522 amode
= ADVANCE_SKIPLAG
;
523 platform::cancel_wait();
524 platform::set_paused(false);
527 function_ptr_command
<> reset_c("reset", "Reset the SNES",
528 "Syntax: reset\nResets the SNES in beginning of the next frame.\n",
529 []() throw(std::bad_alloc
, std::runtime_error
) {
530 pending_reset_cycles
= 0;
533 function_ptr_command
<arg_filename
> load_c("load", "Load savestate (current mode)",
534 "Syntax: load <file>\nLoads SNES state from <file> in current mode\n",
535 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
536 mark_pending_load(args
, LOAD_STATE_CURRENT
);
539 function_ptr_command
<arg_filename
> load_state_c("load-state", "Load savestate (R/W)",
540 "Syntax: load-state <file>\nLoads SNES state from <file> in Read/Write mode\n",
541 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
542 mark_pending_load(args
, LOAD_STATE_RW
);
545 function_ptr_command
<arg_filename
> load_readonly("load-readonly", "Load savestate (RO)",
546 "Syntax: load-readonly <file>\nLoads SNES state from <file> in read-only mode\n",
547 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
548 mark_pending_load(args
, LOAD_STATE_RO
);
551 function_ptr_command
<arg_filename
> load_preserve("load-preserve", "Load savestate (preserve input)",
552 "Syntax: load-preserve <file>\nLoads SNES state from <file> preserving input\n",
553 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
554 mark_pending_load(args
, LOAD_STATE_PRESERVE
);
557 function_ptr_command
<arg_filename
> load_movie_c("load-movie", "Load movie",
558 "Syntax: load-movie <file>\nLoads SNES movie from <file>\n",
559 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
560 mark_pending_load(args
, LOAD_STATE_MOVIE
);
564 function_ptr_command
<arg_filename
> save_state("save-state", "Save state",
565 "Syntax: save-state <file>\nSaves SNES state to <file>\n",
566 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
567 mark_pending_save(args
, SAVE_STATE
);
570 function_ptr_command
<arg_filename
> save_movie("save-movie", "Save movie",
571 "Syntax: save-movie <file>\nSaves SNES movie to <file>\n",
572 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
573 mark_pending_save(args
, SAVE_MOVIE
);
576 function_ptr_command
<> set_rwmode("set-rwmode", "Switch to read/write mode",
577 "Syntax: set-rwmode\nSwitches to read/write mode\n",
578 []() throw(std::bad_alloc
, std::runtime_error
) {
579 movb
.get_movie().readonly_mode(false);
580 information_dispatch::do_mode_change(false);
581 lua_callback_do_readwrite();
582 update_movie_state();
583 information_dispatch::do_status_update();
586 function_ptr_command
<> set_romode("set-romode", "Switch to read-only mode",
587 "Syntax: set-romode\nSwitches to read-only mode\n",
588 []() throw(std::bad_alloc
, std::runtime_error
) {
589 movb
.get_movie().readonly_mode(true);
590 information_dispatch::do_mode_change(true);
591 update_movie_state();
592 information_dispatch::do_status_update();
595 function_ptr_command
<> toggle_rwmode("toggle-rwmode", "Toggle read/write mode",
596 "Syntax: toggle-rwmode\nToggles read/write mode\n",
597 []() throw(std::bad_alloc
, std::runtime_error
) {
598 bool c
= movb
.get_movie().readonly_mode();
599 movb
.get_movie().readonly_mode(!c
);
600 information_dispatch::do_mode_change(!c
);
602 lua_callback_do_readwrite();
603 update_movie_state();
604 information_dispatch::do_status_update();
607 function_ptr_command
<> repaint("repaint", "Redraw the screen",
608 "Syntax: repaint\nRedraws the screen\n",
609 []() throw(std::bad_alloc
, std::runtime_error
) {
610 redraw_framebuffer();
613 function_ptr_command
<> tpon("toggle-pause-on-end", "Toggle pause on end", "Toggle pause on end\n",
614 []() throw(std::bad_alloc
, std::runtime_error
) {
615 bool newstate
= !static_cast<bool>(pause_on_end
);
616 pause_on_end
.set(newstate
? "1" : "0");
617 messages
<< "Pause-on-end is now " << (newstate
? "ON" : "OFF") << std::endl
;
620 function_ptr_command
<> rewind_movie("rewind-movie", "Rewind movie to the beginning",
621 "Syntax: rewind-movie\nRewind movie to the beginning\n",
622 []() throw(std::bad_alloc
, std::runtime_error
) {
623 mark_pending_load("SOME NONBLANK NAME", LOAD_STATE_BEGINNING
);
626 function_ptr_command
<> cancel_save("cancel-saves", "Cancel all pending saves", "Syntax: cancel-save\n"
627 "Cancel pending saves\n",
628 []() throw(std::bad_alloc
, std::runtime_error
) {
629 queued_saves
.clear();
630 messages
<< "Pending saves canceled." << std::endl
;
633 function_ptr_command
<> test1("test-1", "no description available", "No help available\n",
634 []() throw(std::bad_alloc
, std::runtime_error
) {
635 redraw_framebuffer(screen_nosignal
);
638 function_ptr_command
<> test2("test-2", "no description available", "No help available\n",
639 []() throw(std::bad_alloc
, std::runtime_error
) {
640 redraw_framebuffer(screen_corrupt
);
643 function_ptr_command
<> test3("test-3", "no description available", "No help available\n",
644 []() throw(std::bad_alloc
, std::runtime_error
) {
649 inverse_key
ipause_emulator("pause-emulator", "(Un)pause");
650 inverse_key
ijback("cycle-jukebox-backward", "Cycle slot backwards");
651 inverse_key
ijforward("cycle-jukebox-forward", "Cycle slot forwards");
652 inverse_key
iloadj("load-jukebox", "load selected slot");
653 inverse_key
isavej("save-jukebox", "Save selected slot");
654 inverse_key
iadvframe("+advance-frame", "Advance frame");
655 inverse_key
iadvsubframe("+advance-poll", "Advance subframe");
656 inverse_key
iskiplag("advance-skiplag", "Advance to next poll");
657 inverse_key
ireset("reset", "System reset");
658 inverse_key
iset_rwmode("set-rwmode", "Switch to read/write");
659 inverse_key
itoggle_romode("set-romode", "Switch to read-only");
660 inverse_key
itoggle_rwmode("toggle-rwmode", "Toggle read-only");
661 inverse_key
irepaint("repaint", "Repaint screen");
662 inverse_key
itogglepause("toggle-pause-on-end", "Toggle pause-on-end");
663 inverse_key
irewind_movie("rewind-movie", "Rewind movie");
664 inverse_key
icancel_saves("cancel-saves", "Cancel pending saves");
665 inverse_key
iload1("load ${project}1.lsmv", "Load slot 1");
666 inverse_key
iload2("load ${project}2.lsmv", "Load slot 2");
667 inverse_key
iload3("load ${project}3.lsmv", "Load slot 3");
668 inverse_key
iload4("load ${project}4.lsmv", "Load slot 4");
669 inverse_key
iload5("load ${project}5.lsmv", "Load slot 5");
670 inverse_key
iload6("load ${project}6.lsmv", "Load slot 6");
671 inverse_key
iload7("load ${project}7.lsmv", "Load slot 7");
672 inverse_key
iload8("load ${project}8.lsmv", "Load slot 8");
673 inverse_key
iload9("load ${project}9.lsmv", "Load slot 9");
674 inverse_key
iload10("load ${project}10.lsmv", "Load slot 10");
675 inverse_key
iload11("load ${project}11.lsmv", "Load slot 11");
676 inverse_key
iload12("load ${project}12.lsmv", "Load slot 12");
677 inverse_key
iload13("load ${project}13.lsmv", "Load slot 13");
678 inverse_key
iload14("load ${project}14.lsmv", "Load slot 14");
679 inverse_key
iload15("load ${project}15.lsmv", "Load slot 15");
680 inverse_key
iload16("load ${project}16.lsmv", "Load slot 16");
681 inverse_key
iload17("load ${project}17.lsmv", "Load slot 17");
682 inverse_key
iload18("load ${project}18.lsmv", "Load slot 18");
683 inverse_key
iload19("load ${project}19.lsmv", "Load slot 19");
684 inverse_key
iload20("load ${project}20.lsmv", "Load slot 20");
685 inverse_key
iload21("load ${project}21.lsmv", "Load slot 21");
686 inverse_key
iload22("load ${project}22.lsmv", "Load slot 22");
687 inverse_key
iload23("load ${project}23.lsmv", "Load slot 23");
688 inverse_key
iload24("load ${project}24.lsmv", "Load slot 24");
689 inverse_key
iload25("load ${project}25.lsmv", "Load slot 25");
690 inverse_key
iload26("load ${project}26.lsmv", "Load slot 26");
691 inverse_key
iload27("load ${project}27.lsmv", "Load slot 27");
692 inverse_key
iload28("load ${project}28.lsmv", "Load slot 28");
693 inverse_key
iload29("load ${project}29.lsmv", "Load slot 29");
694 inverse_key
iload30("load ${project}30.lsmv", "Load slot 30");
695 inverse_key
iload31("load ${project}31.lsmv", "Load slot 31");
696 inverse_key
iload32("load ${project}32.lsmv", "Load slot 32");
697 inverse_key
isave1("save-state ${project}1.lsmv", "Save slot 1");
698 inverse_key
isave2("save-state ${project}2.lsmv", "Save slot 2");
699 inverse_key
isave3("save-state ${project}3.lsmv", "Save slot 3");
700 inverse_key
isave4("save-state ${project}4.lsmv", "Save slot 4");
701 inverse_key
isave5("save-state ${project}5.lsmv", "Save slot 5");
702 inverse_key
isave6("save-state ${project}6.lsmv", "Save slot 6");
703 inverse_key
isave7("save-state ${project}7.lsmv", "Save slot 7");
704 inverse_key
isave8("save-state ${project}8.lsmv", "Save slot 8");
705 inverse_key
isave9("save-state ${project}9.lsmv", "Save slot 9");
706 inverse_key
isave10("save-state ${project}10.lsmv", "Save slot 10");
707 inverse_key
isave11("save-state ${project}11.lsmv", "Save slot 11");
708 inverse_key
isave12("save-state ${project}12.lsmv", "Save slot 12");
709 inverse_key
isave13("save-state ${project}13.lsmv", "Save slot 13");
710 inverse_key
isave14("save-state ${project}14.lsmv", "Save slot 14");
711 inverse_key
isave15("save-state ${project}15.lsmv", "Save slot 15");
712 inverse_key
isave16("save-state ${project}16.lsmv", "Save slot 16");
713 inverse_key
isave17("save-state ${project}17.lsmv", "Save slot 17");
714 inverse_key
isave18("save-state ${project}18.lsmv", "Save slot 18");
715 inverse_key
isave19("save-state ${project}19.lsmv", "Save slot 19");
716 inverse_key
isave20("save-state ${project}20.lsmv", "Save slot 20");
717 inverse_key
isave21("save-state ${project}21.lsmv", "Save slot 21");
718 inverse_key
isave22("save-state ${project}22.lsmv", "Save slot 22");
719 inverse_key
isave23("save-state ${project}23.lsmv", "Save slot 23");
720 inverse_key
isave24("save-state ${project}24.lsmv", "Save slot 24");
721 inverse_key
isave25("save-state ${project}25.lsmv", "Save slot 25");
722 inverse_key
isave26("save-state ${project}26.lsmv", "Save slot 26");
723 inverse_key
isave27("save-state ${project}27.lsmv", "Save slot 27");
724 inverse_key
isave28("save-state ${project}28.lsmv", "Save slot 28");
725 inverse_key
isave29("save-state ${project}29.lsmv", "Save slot 29");
726 inverse_key
isave30("save-state ${project}30.lsmv", "Save slot 30");
727 inverse_key
isave31("save-state ${project}31.lsmv", "Save slot 31");
728 inverse_key
isave32("save-state ${project}32.lsmv", "Save slot 32");
730 bool on_quit_prompt
= false;
731 class mywindowcallbacks
: public information_dispatch
734 mywindowcallbacks() : information_dispatch("mainloop-window-callbacks") {}
735 void on_new_dumper(const std::string
& n
)
737 update_movie_state();
739 void on_destroy_dumper(const std::string
& n
)
741 update_movie_state();
743 void on_close() throw()
746 amode
= ADVANCE_QUIT
;
747 platform::set_paused(false);
748 platform::cancel_wait();
751 on_quit_prompt
= true;
753 amode
= ADVANCE_QUIT
;
754 platform::set_paused(false);
755 platform::cancel_wait();
758 on_quit_prompt
= false;
762 //If there is a pending load, perform it. Return 1 on successful load, 0 if nothing to load, -1 on load
766 if(pending_load
!= "") {
767 system_corrupt
= false;
768 if(loadmode
!= LOAD_STATE_BEGINNING
&& !do_load_state(pending_load
, loadmode
)) {
772 if(loadmode
== LOAD_STATE_BEGINNING
)
775 pending_reset_cycles
= -1;
776 amode
= ADVANCE_AUTO
;
777 platform::cancel_wait();
778 platform::set_paused(false);
779 if(!system_corrupt
) {
780 location_special
= SPECIAL_SAVEPOINT
;
781 update_movie_state();
782 information_dispatch::do_status_update();
783 platform::flush_command_queue();
790 //If there are pending saves, perform them.
793 if(!queued_saves
.empty()) {
794 stepping_into_save
= true;
795 SNES::system
.runtosave();
796 stepping_into_save
= false;
797 for(auto i
: queued_saves
)
800 queued_saves
.clear();
803 //Do (delayed) reset. Return true if proper, false if forced at frame boundary.
804 bool handle_reset(long cycles
)
808 video_refresh_done
= false;
810 messages
<< "SNES reset" << std::endl
;
811 else if(cycles
> 0) {
812 messages
<< "SNES delayed reset not implemented (doing immediate reset)" << std::endl
;
813 /* ... This code is just too buggy.
814 long cycles_executed = 0;
815 messages << "Executing delayed reset... This can take some time!" << std::endl;
816 while(cycles_executed < cycles && !video_refresh_done) {
817 //Poll inputs once in a while to prevent activating watchdog.
818 if(cycles_executed % 100 == 0)
819 platform::flush_command_queue();
823 if(!video_refresh_done)
824 messages << "SNES reset (delayed " << cycles_executed << ")" << std::endl;
826 messages << "SNES reset (forced at " << cycles_executed << ")" << std::endl;
829 SNES::system
.reset();
830 lua_callback_do_reset();
831 redraw_framebuffer(screen_nosignal
);
832 if(video_refresh_done
) {
833 to_wait_frame(get_utime());
839 bool handle_corrupt()
843 while(system_corrupt
) {
844 platform::cancel_wait();
845 platform::set_paused(true);
846 platform::flush_command_queue();
848 if(amode
== ADVANCE_QUIT
)
855 std::vector
<std::string
> get_jukebox_names()
860 void set_jukebox_names(const std::vector
<std::string
>& newj
)
863 if(save_jukebox_pointer
>= save_jukebox
.size())
864 save_jukebox_pointer
= 0;
865 update_movie_state();
868 void main_loop(struct loaded_rom
& rom
, struct moviefile
& initial
, bool load_has_to_succeed
) throw(std::bad_alloc
,
871 //Basic initialization.
872 init_special_screens();
875 auto old_inteface
= SNES::interface
;
876 SNES::interface
= &intrf
;
879 //Load our given movie.
880 bool first_round
= false;
881 bool just_did_loadstate
= false;
883 do_load_state(initial
, LOAD_STATE_DEFAULT
);
884 location_special
= SPECIAL_SAVEPOINT
;
885 update_movie_state();
886 first_round
= our_movie
.is_savestate
;
887 just_did_loadstate
= first_round
;
888 } catch(std::bad_alloc
& e
) {
890 } catch(std::exception
& e
) {
891 messages
<< "ERROR: Can't load initial state: " << e
.what() << std::endl
;
892 if(load_has_to_succeed
) {
893 messages
<< "FATAL: Can't load movie" << std::endl
;
894 platform::fatal_error();
896 system_corrupt
= true;
897 update_movie_state();
898 redraw_framebuffer(screen_corrupt
);
901 lua_callback_startup();
903 platform::set_paused(false);
904 amode
= ADVANCE_AUTO
;
905 uint64_t time_x
= get_utime();
906 while(amode
!= ADVANCE_QUIT
|| !queued_saves
.empty()) {
907 if(handle_corrupt()) {
908 first_round
= our_movie
.is_savestate
;
909 just_did_loadstate
= first_round
;
912 long resetcycles
= -1;
913 ack_frame_tick(get_utime());
914 if(amode
== ADVANCE_SKIPLAG_PENDING
)
915 amode
= ADVANCE_SKIPLAG
;
918 resetcycles
= movb
.new_frame_starting(amode
== ADVANCE_SKIPLAG
);
919 if(amode
== ADVANCE_QUIT
&& queued_saves
.empty())
921 bool delayed_reset
= (resetcycles
> 0);
922 pending_reset_cycles
= -1;
923 if(!handle_reset(resetcycles
)) {
930 if(queued_saves
.empty())
932 if(r
> 0 || system_corrupt
) {
933 first_round
= our_movie
.is_savestate
;
935 amode
= ADVANCE_PAUSE
;
938 just_did_loadstate
= first_round
;
941 //Not exactly desriable, but this at least won't desync.
942 amode
= ADVANCE_PAUSE
;
945 if(just_did_loadstate
) {
946 //If we just loadstated, we are up to date.
947 if(amode
== ADVANCE_QUIT
)
949 platform::cancel_wait();
950 platform::set_paused(amode
== ADVANCE_PAUSE
);
951 platform::flush_command_queue();
952 //We already have done the reset this frame if we are going to do one at all.
953 movb
.get_movie().set_controls(controls
.get(movb
.get_movie().get_current_frame()));
954 just_did_loadstate
= false;
956 frame_irq_time
= get_utime() - time_x
;
958 time_x
= get_utime();
959 if(amode
== ADVANCE_AUTO
)
960 platform::wait(to_wait_frame(get_utime()));
962 lua_callback_do_frame();
964 information_dispatch::do_dump_end();
965 SNES::interface
= old_inteface
;