_tas_tools.tcl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. namespace eval tas {
  2. ### periodic refresh stuff
  3. variable callback_list ""
  4. variable callback_frame_id
  5. variable callback_realtime_id
  6. # TODO move to utils?
  7. # Register the given callback to be executed
  8. # - (Once) when it is registered
  9. # - Every MSX frame
  10. # - Or at least 10 times per second (e.g. when MSX emulation is paused)
  11. proc enable_periodic {callback} {
  12. variable callback_list
  13. variable callback_frame_id
  14. variable callback_realtime_id
  15. # on 1st callback activate the after-stuff
  16. if {[llength $callback_list] == 0} {
  17. set callback_frame_id [after frame [namespace code periodic_frame]]
  18. set callback_realtime_id [after realtime 0.1 [namespace code periodic_realtime]]
  19. }
  20. # add to list of registered callbacks
  21. lappend callback_list $callback
  22. # execute callback for the first time
  23. $callback
  24. }
  25. # Remove a previously registered callback. After this proc returns, the
  26. # callback is guaranteed to not be called anymore. So it's (no longer)
  27. # required to add a check in the callback proc.
  28. proc disable_periodic {callback} {
  29. variable callback_list
  30. variable callback_frame_id
  31. variable callback_realtime_id
  32. # remove from registered callbacks
  33. set idx [lsearch -exact $callback_list $callback]
  34. if {$idx == -1} return
  35. set callback_list [lreplace $callback_list $idx $idx]
  36. # if the last callback was removed then deactivate after stuff
  37. if {[llength $callback_list] == 0} {
  38. after cancel $callback_frame_id
  39. after cancel $callback_realtime_id
  40. }
  41. }
  42. proc periodic_execute {} {
  43. variable callback_list
  44. foreach callback $callback_list {
  45. $callback
  46. }
  47. }
  48. proc periodic_frame {} {
  49. variable callback_frame_id
  50. variable callback_realtime_id
  51. periodic_execute
  52. # postpone 'after realtime' / reschedule 'after frame'
  53. after cancel $callback_realtime_id
  54. set callback_frame_id [after frame [namespace code periodic_frame]]
  55. set callback_realtime_id [after realtime 0.1 [namespace code periodic_realtime]]
  56. }
  57. proc periodic_realtime {} {
  58. variable callback_realtime_id
  59. periodic_execute
  60. # reschedule 'after realtime', no need to handle 'after frame'
  61. set callback_realtime_id [after realtime 0.1 [namespace code periodic_realtime]]
  62. }
  63. ### frame counter ###
  64. set_help_text toggle_frame_counter\
  65. {Toggles display of a frame counter in the lower right corner.}
  66. proc toggle_frame_counter {} {
  67. if {[osd exists framecount]} {
  68. disable_periodic framecount_update
  69. osd destroy framecount
  70. return ""
  71. }
  72. osd create rectangle framecount \
  73. -x 269 -y 227 -h 7 -w 42 -scaled true \
  74. -rgba "0x0044aa80 0x2266dd80 0x0055cc80 0x44aaff80" \
  75. -borderrgba 0x00000040 -bordersize 0.5
  76. osd create text framecount.text -x 3 -y 1 -size 4 -rgba 0xffffffff
  77. enable_periodic framecount_update
  78. return ""
  79. }
  80. proc framecount_update {} {
  81. # Heuristic: A 'ex (sp),ix' Z80 instruction takes 25 cycles. If we're
  82. # not more than 25 Z80 cycles past the start of a frame, then indicate
  83. # we're at the start of the frame. (But it could as well be the 2nd or
  84. # 3rd (short) instruction in the frame.)
  85. set inside [expr {([machine_info VDP_cycle_in_frame] < (6 * 25)) ? "" : "+"}]
  86. osd configure framecount.text -text "Frame: [machine_info VDP_frame_count]$inside"
  87. }
  88. ### frame advance/reverse and helper procs for TAS mode key bindings ###
  89. proc get_frame_duration {} {
  90. expr {(1368.0 * (([vdpreg 9] & 2) ? 313 : 262)) / (6 * 3579545)}
  91. }
  92. proc get_start_of_frame_time {} {
  93. expr {[machine_info time] - [machine_info VDP_cycle_in_frame] / (6.0 * 3579545)}
  94. }
  95. set_help_text prev_frame \
  96. {Rewind to the (start of) the previous frame. Useful
  97. to bind to a key in combination with next_frame. You can also
  98. go back N frames if you add N as the optional argument.}
  99. proc prev_frame {{count 1}} {
  100. set t [expr {[get_start_of_frame_time] - $count * [get_frame_duration]}]
  101. if {$t < 0} {set t 0}
  102. reverse goto $t
  103. }
  104. set_help_text next_frame \
  105. {Emulates until the (start of) the next frame, then pause emulation.
  106. Useful to bind to a key and emulate frame by frame. You can also
  107. go over the next N frames if you add N as the optional argument.}
  108. proc next_frame {{count 1}} {
  109. set t [expr {[get_start_of_frame_time] + $count * [get_frame_duration]}]
  110. after time [expr {$t - [machine_info time]}] "set ::pause on"
  111. set ::pause off
  112. return ""
  113. }
  114. set_help_text start_of_frame \
  115. {Rewind to the start of the current frame.
  116. See also prev_frame and next_frame.}
  117. proc start_of_frame {} {
  118. prev_frame 0
  119. }
  120. set_help_text advance_frame \
  121. {Emulate forward for the duration of one frame. The relative
  122. timing position within the frame remains unchanged. You can also
  123. advance N frames if you add N as the optional argument.}
  124. proc advance_frame {{count 1}} {
  125. after time [expr {$count * [get_frame_duration]}] "set ::pause on"
  126. set ::pause off
  127. return ""
  128. }
  129. set_help_text reverse_frame \
  130. {Emulate backward for the duration of one frame. The relative
  131. timing position within the frame remains unchanged. You can also
  132. reverse N frames if you add N as the optional argument.}
  133. proc reverse_frame {{count 1}} {
  134. set t [expr {[machine_info time] - $count * [get_frame_duration]}]
  135. if {$t < 0} {set t 0}
  136. reverse goto $t
  137. }
  138. proc load_replay {name} {
  139. reverse loadreplay -goto savetime $name
  140. return ""
  141. }
  142. ### Very basic replay slot selector ###
  143. user_setting create string current_replay_slot "Name of the current replay slot." slot0
  144. proc list_slots {} {
  145. set slots [list]
  146. for {set i 0} {$i <= 9} {incr i} {
  147. lappend slots "slot$i"
  148. }
  149. return $slots
  150. }
  151. proc menu_create_slot_menu {} {
  152. set items [list_slots]
  153. set menu_def \
  154. { execute tas::set_slot
  155. font-size 8
  156. border-size 2
  157. width 100
  158. xpos 100
  159. ypos 100
  160. header { text "Select Replay Slot"
  161. font-size 10
  162. post-spacing 6 }}
  163. return [osd_menu::prepare_menu_list $items 10 $menu_def]
  164. }
  165. proc set_slot {item} {
  166. osd_menu::menu_close_all
  167. set ::current_replay_slot $item
  168. }
  169. proc open_select_slot_menu {} {
  170. osd_menu::do_menu_open [menu_create_slot_menu]
  171. osd_menu::select_menu_item $::current_replay_slot
  172. for {set i 0} {$i < [llength [tas::list_slots]]} {incr i} {
  173. bind -layer slot_menu "$i" "tas::set_slot [lindex [tas::list_slots] $i]; tas::unbind_number_keys"
  174. }
  175. activate_input_layer slot_menu
  176. }
  177. proc unbind_number_keys {} {
  178. deactivate_input_layer slot_menu
  179. }
  180. proc save_replay_slot {} {
  181. reverse savereplay $::current_replay_slot
  182. }
  183. proc load_replay_slot {} {
  184. load_replay $::current_replay_slot
  185. }
  186. ### Show Cursor Keys / 'fire buttons and others' ###
  187. variable keys
  188. proc show_keys {} {
  189. variable keys
  190. # get joysticka values
  191. set joy [debug read joystickports 0]
  192. foreach key $keys {
  193. show_key_press [dict get $key name] [eval [dict get $key check_expr]]
  194. }
  195. }
  196. #move to other TCL script?
  197. proc is_key_pressed {row bit} {
  198. expr {!([debug read keymatrix $row] & (1 << $bit))}
  199. }
  200. proc show_key_press {key state} {
  201. set keycol [expr {$state ? 0xff000080 : "0x0044aa80 0x2266dd80 0x0055cc80 0x44aaff80"}]
  202. osd configure cursors.$key -rgba $keycol
  203. }
  204. proc create_key {name x y} {
  205. osd create rectangle cursors.$name -x $x -y $y -w 16 -h 7 \
  206. -rgba "0x0044aa80 0x2266dd80 0x0055cc80 0x44aaff80" \
  207. -bordersize 0.5 -borderrgba 0x00000040
  208. osd create text cursors.$name.text -x 2 -y 1 -text $name -size 4 -rgba 0xffffffff
  209. }
  210. # you can setup your own key definitions by loading a script which defines a proc like this:
  211. #
  212. # proc define_custom_keys {} {
  213. # variable tas::keys
  214. #
  215. # set tas::keys {{
  216. # name up
  217. # x 20
  218. # y 2
  219. # check_expr {expr {([is_key_pressed 8 5] || !($joy & 1))}}
  220. # } {
  221. # name shift
  222. # x 186
  223. # y 8
  224. # check_expr {is_key_pressed 6 0}
  225. # }}
  226. #}
  227. #
  228. proc toggle_cursors {} {
  229. variable keys
  230. if {[osd exists cursors]} {
  231. disable_periodic show_keys
  232. osd destroy cursors
  233. } else {
  234. osd create rectangle cursors -x 64 -y 219 -h 26 -w 204 -scaled true -rgba 0x00000000
  235. if {[info procs ::define_custom_keys] eq "::define_custom_keys"} {
  236. define_custom_keys
  237. } else {
  238. define_default_keys
  239. }
  240. foreach key $keys {
  241. create_key [dict get $key name] [dict get $key x] [dict get $key y]
  242. }
  243. enable_periodic show_keys
  244. }
  245. }
  246. proc define_default_keys {} {
  247. variable keys
  248. set keys {{
  249. name up
  250. x 20
  251. y 2
  252. check_expr {expr {([is_key_pressed 8 5] || !($joy & 1))}}
  253. } {
  254. name down
  255. x 20
  256. y 14
  257. check_expr {expr {([is_key_pressed 8 6] || !($joy & 2))}}
  258. } {
  259. name left
  260. x 2
  261. y 8
  262. check_expr {expr {([is_key_pressed 8 4] || !($joy & 4))}}
  263. } {
  264. name right
  265. x 38
  266. y 8
  267. check_expr {expr {([is_key_pressed 8 7] || !($joy & 8))}}
  268. } {
  269. name space
  270. x 60
  271. y 8
  272. check_expr {expr {([is_key_pressed 8 0] || !($joy & 16))}}
  273. } {
  274. name m
  275. x 78
  276. y 8
  277. check_expr {expr {([is_key_pressed 4 2] || !($joy & 32))}}
  278. } {
  279. name n
  280. x 96
  281. y 8
  282. check_expr {is_key_pressed 4 3}
  283. } {
  284. name z
  285. x 114
  286. y 8
  287. check_expr {is_key_pressed 5 7}
  288. } {
  289. name x
  290. x 132
  291. y 8
  292. check_expr {is_key_pressed 5 5}
  293. } {
  294. name graph
  295. x 150
  296. y 8
  297. check_expr {is_key_pressed 6 2}
  298. } {
  299. name ctrl
  300. x 168
  301. y 8
  302. check_expr {is_key_pressed 6 1}
  303. } {
  304. name shift
  305. x 186
  306. y 8
  307. check_expr {is_key_pressed 6 0}
  308. }}
  309. }
  310. ### RAM Watch ###
  311. # TODO:
  312. # - be smarter with coordinates, by using negative ones
  313. # - maybe put description on separate line to make window narrow again?
  314. variable addr_watches [dict create] ;# dict of RAM watches
  315. # TODO move to utils?
  316. proc dict_insert_sorted {dictname key value} {
  317. upvar $dictname d
  318. set i 0
  319. dict for {k v} $d {
  320. if {$key <= $k} {
  321. if {$key == $k} {
  322. dict set d $key $value
  323. return $d
  324. }
  325. break
  326. }
  327. incr i 2
  328. }
  329. set d [linsert $d $i $key $value]
  330. }
  331. proc ram_watch_add {addr_str args} {
  332. variable addr_watches
  333. set type_dict [dict create byte {peek 2} b {peek 2} \
  334. u8 {peek 2} s8 {peek_s8 2} \
  335. word {peek16 4} w {peek16 4} \
  336. u16 {peek16 4} s16 {peek_s16 4} \
  337. u16_LE {peek16 4} s16_LE {peek_s16 4} \
  338. u16_BE {peek16_BE 4} s16_BE {peek_s16_BE 4}]
  339. set format_dict [dict create dec "%d" hex "0x%0SX"]
  340. # sanitize input
  341. set addr [format 0x%04X $addr_str]
  342. if {($addr < 0) || ($addr > 0xffff)} {
  343. error "Address must be in range 0x0..0xffff, got: $addr_str"
  344. }
  345. set addr_already_watched [dict exists $addr_watches $addr]
  346. # defaults
  347. set desc "?"
  348. set type "byte"
  349. set format "hex"
  350. if {$addr_already_watched} {
  351. # start from the previously set values
  352. set desc [dict get $addr_watches $addr -desc]
  353. set type [dict get $addr_watches $addr -type]
  354. set format [dict get $addr_watches $addr -format]
  355. }
  356. while {[llength $args] > 0} {
  357. set option [lindex $args 0]
  358. switch -- $option {
  359. "-desc" {
  360. set desc [lindex $args 1]
  361. set args [lrange $args 2 end]
  362. }
  363. "-type" {
  364. set type [lindex $args 1]
  365. if {![dict exists $type_dict $type]} {
  366. error "Unsupported type: $type. Choose from: [dict keys $type_dict]"
  367. }
  368. set args [lrange $args 2 end]
  369. }
  370. "-format" {
  371. set format [lindex $args 1]
  372. if {![dict exists $format_dict $format]} {
  373. error "Unsupported format: $format. Choose from: [dict keys $format_dict]"
  374. }
  375. set args [lrange $args 2 end]
  376. }
  377. "default" {
  378. error "Invalid option: $option."
  379. }
  380. }
  381. }
  382. lassign [dict get $type_dict $type] peek_method num_hex_digits
  383. set fmtStr1 [dict get $format_dict $format]
  384. set fmtStr2 [string map [list S $num_hex_digits] $fmtStr1]
  385. set exprStr "set v \[$peek_method $addr\]; set r \"\[expr {(\$v < 0) ? \"-\" : \"\"}\]\[format $fmtStr2 \[expr {abs(\$v)}\]\]\"; set r"
  386. # add watch to watches
  387. set old_nof_watches [dict size $addr_watches]
  388. dict_insert_sorted addr_watches $addr [dict create -desc $desc -format $format -type $type exprStr $exprStr]
  389. # if OSD doesn't exist yet create it
  390. if {$old_nof_watches == 0} {
  391. ram_watch_init_widget
  392. }
  393. # (possibly) add one extra entry
  394. if {!$addr_already_watched} {
  395. ram_watch_add_to_widget $old_nof_watches
  396. ram_watch_update_widget_size
  397. }
  398. ram_watch_update_addresses
  399. if {$old_nof_watches == 0} {
  400. enable_periodic ram_watch_update_values
  401. }
  402. return ""
  403. }
  404. proc ram_watch_init_widget {} {
  405. osd create rectangle ram_watch \
  406. -x 0 -y 0 -h 240 -w 320 -scaled true -rgba 0x00000000
  407. osd create rectangle ram_watch.addr \
  408. -x 257 -y 1 -w 62 -h 221 \
  409. -rgba "0x0044aa80 0x2266dd80 0x0055cc80 0x44aaff80" \
  410. -borderrgba 0x00000040 -bordersize 0.5
  411. osd create text ram_watch.addr.title -text "RAM Watch" -x 2 -y 1 -size 4 -rgba 0xffffffff
  412. }
  413. proc ram_watch_update_widget_size {} {
  414. variable addr_watches
  415. set nr [dict size $addr_watches]
  416. osd configure ram_watch.addr -h [expr {8 + ($nr * 6)}]
  417. }
  418. proc ram_watch_add_to_widget {nr} {
  419. osd create rectangle ram_watch.addr.mem$nr \
  420. -x 2 -y [expr {8 + ($nr * 6)}] -h 5 -w 16 -rgba 0x40404080
  421. osd create text ram_watch.addr.mem$nr.text \
  422. -size 4 -rgba 0xffffffff
  423. osd create rectangle ram_watch.addr.val$nr \
  424. -x 19 -y [expr {8 + ($nr * 6)}] -h 5 -w 17 -rgba 0x40404080
  425. osd create text ram_watch.addr.val$nr.text \
  426. -size 4 -rgba 0xffffffff
  427. osd create rectangle ram_watch.addr.desc$nr \
  428. -x 37 -y [expr {8 + ($nr * 6)}] -h 5 -w 23 -rgba 0x40404080 -clip true
  429. osd create text ram_watch.addr.desc$nr.text \
  430. -size 4 -rgba 0xffffffff
  431. }
  432. proc ram_watch_remove {addr_str} {
  433. variable addr_watches
  434. # sanitize input
  435. set addr [format 0x%04X $addr_str]
  436. # check watch exists
  437. if {![dict exists $addr_watches $addr]} {
  438. # not an error
  439. return ""
  440. }
  441. #remove address
  442. dict unset addr_watches $addr
  443. set i [dict size $addr_watches]
  444. #remove one OSD entry
  445. osd destroy ram_watch.addr.mem$i
  446. osd destroy ram_watch.addr.val$i
  447. osd destroy ram_watch.addr.desc$i
  448. #if all elements are gone don't display anything anymore.
  449. if {$i == 0} {
  450. disable_periodic ram_watch_update_values
  451. osd destroy ram_watch
  452. } else {
  453. ram_watch_update_addresses
  454. ram_watch_update_widget_size
  455. }
  456. return ""
  457. }
  458. proc ram_watch_clear {} {
  459. variable addr_watches
  460. set addr_watches [dict create]
  461. disable_periodic ram_watch_update_values
  462. osd destroy ram_watch
  463. return ""
  464. }
  465. proc ram_watch_update_addresses {} {
  466. variable addr_watches
  467. set i 0
  468. dict for {addr v} $addr_watches {
  469. osd configure ram_watch.addr.mem$i.text -text $addr
  470. osd configure ram_watch.addr.desc$i.text -text [dict get $v -desc]
  471. incr i
  472. }
  473. }
  474. proc ram_watch_update_values {} {
  475. variable addr_watches
  476. set i 0
  477. dict for {addr v} $addr_watches {
  478. set exprStr [dict get $v exprStr]
  479. osd configure ram_watch.addr.val$i.text -text [eval $exprStr]
  480. incr i
  481. }
  482. }
  483. proc ram_watch_save {name} {
  484. variable addr_watches
  485. # exprStr property doesn't need to be saved
  486. set filtered_watches [dict create]
  487. dict for {addr v} $addr_watches {
  488. dict set filtered_watches $addr [dict filter $v key -*]
  489. }
  490. set directory [file normalize $::env(OPENMSX_USER_DATA)/../ramwatches]
  491. file mkdir $directory
  492. set fullname [file join $directory ${name}.wch]
  493. if {[catch {
  494. set the_file [open $fullname {WRONLY TRUNC CREAT}]
  495. puts $the_file $filtered_watches
  496. close $the_file
  497. } errorText]} {
  498. error "Failed to save to $fullname: $errorText"
  499. }
  500. return "Successfully saved RAM watch to $fullname"
  501. }
  502. proc ram_watch_load {name} {
  503. variable addr_watches
  504. set directory [file normalize $::env(OPENMSX_USER_DATA)/../ramwatches]
  505. set fullname [file join $directory ${name}.wch]
  506. if {[catch {
  507. set the_file [open $fullname {RDONLY}]
  508. set new_addr_watches [read $the_file]
  509. close $the_file
  510. } errorText]} {
  511. error "Failed to load from $fullname: $errorText"
  512. }
  513. ram_watch_clear
  514. dict for {addr v} $new_addr_watches {
  515. ram_watch_add $addr {*}$v
  516. }
  517. return "Successfully loaded $fullname"
  518. }
  519. proc list_ram_watch_files {} {
  520. set directory [file normalize $::env(OPENMSX_USER_DATA)/../ramwatches]
  521. set results [list]
  522. foreach f [lsort [glob -tails -directory $directory -type f -nocomplain *.wch]] {
  523. lappend results [file rootname $f]
  524. }
  525. return $results
  526. }
  527. proc ram_watch {subcmd args} {
  528. switch -- $subcmd {
  529. "add" {ram_watch_add {*}$args}
  530. "remove" {ram_watch_remove {*}$args}
  531. "clear" {ram_watch_clear {*}$args}
  532. "load" {ram_watch_load {*}$args}
  533. "save" {ram_watch_save {*}$args}
  534. default {error "Invalid subcommand: $subcmd"}
  535. }
  536. }
  537. set_help_proc ram_watch [namespace code ram_watch_help]
  538. proc ram_watch_help {args} {
  539. switch -- [lindex $args 1] {
  540. "add" {return {Add an address to the list of RAM watch addresses on the right side of the screen. The list will be updated in real time, whenever a value changes.
  541. Syntax: ram_watch add <address> [<options>...]
  542. Possible options are:
  543. -desc <description> describes the address
  544. -type <type> datatype: byte, word, u8, s8, u16, s16, u16_BE, ...
  545. -format <format> formatting: dec, hex
  546. }}
  547. "remove" {return {Remove an address from the list of RAM watch addresses from the list of RAM watch addresses on the right side of the screen. When the last address is removed, the list will disappear automatically.
  548. Syntax: ram_watch remove <address>
  549. }}
  550. "clear" {return {Removes all RAM watches.
  551. Syntax: ram_watch clear
  552. }}
  553. "save" {return {Save the current list of RAM watches to a file.
  554. Syntax: ram_watch save <filename>
  555. }}
  556. "load" {return {Load a list of RAM watches from file.
  557. Syntax: ram_watch load <filename>
  558. }}
  559. default {return {Control the RAM watch functionality.
  560. Syntax: ram_watch <sub-command> [<arguments>]
  561. Where sub-command is one of:
  562. add Add (or modify) a new RAM watch address
  563. remove Remove a previously added address
  564. clear Shortcut to remove all addresses
  565. save Save current list of RAM watches to a file
  566. load Load a previously saved list of RAM watches
  567. Use 'help ram_watch <sub-command>' to get more detailed help on a specific sub-command.
  568. }}
  569. }
  570. }
  571. set_tabcompletion_proc ram_watch [namespace code ram_watch_tabcompletion]
  572. proc ram_watch_tabcompletion {args} {
  573. if {[llength $args] == 2} {
  574. return [list "add" "remove" "clear" "save" "load"]
  575. }
  576. switch -- [lindex $args 1] {
  577. "add" {return [list -desc -type -format]}
  578. "load" -
  579. "save" {return [list_ram_watch_files]}
  580. default {return [list]}
  581. }
  582. }
  583. ### Lag counter ###
  584. variable lag_counter
  585. variable lag_counter_wp
  586. variable previous_frame_count 0
  587. variable input_read_in_frame false
  588. proc space_read {} {
  589. variable input_read_in_frame
  590. set input_read_in_frame true
  591. }
  592. proc toggle_lag_counter {} {
  593. variable lag_counter
  594. variable lag_counter_wp
  595. if {[osd exists lag_counter]} {
  596. osd destroy lag_counter
  597. debug remove_watchpoint $lag_counter_wp
  598. return ""
  599. }
  600. # we set it to -1, because the updater is always called at the start
  601. # and there the incr is present, so it will always incr. We compensate
  602. # for that by setting it to -1. See also on other places where we
  603. # force a call to the updater.
  604. set lag_counter -1
  605. set lag_counter_wp [debug set_watchpoint read_io 0xa9 {[expr [debug read ioports 0xaa] & 0x0f] == 0x08} {tas::space_read}]
  606. osd create rectangle lag_counter \
  607. -x 269 -y 220 -h 7 -w 42 -scaled true \
  608. -borderrgba 0x00000040 -bordersize 0.5
  609. osd create text lag_counter.text -x 3 -y 1 -size 4 -rgba 0xffffffff
  610. update_lag_counter ;# can't use enable_periodic
  611. return ""
  612. }
  613. proc update_lag_counter {} {
  614. update_lag_counter2
  615. after frame [namespace code update_lag_counter]
  616. }
  617. proc update_lag_counter2 {} {
  618. variable lag_counter
  619. variable previous_frame_count
  620. variable input_read_in_frame
  621. if {![osd exists lag_counter]} return
  622. set new_frame_count [machine_info VDP_frame_count]
  623. if {$new_frame_count == 1} {
  624. # reset lag counter if frame counter is reset (e.g. after reset, or loading replay)
  625. set lag_counter -1
  626. }
  627. # GFX9000 also triggers frame ends, so we should check if we
  628. # actually advanced a V99x8 frame here
  629. # Also, we check whether it was reset to force updating the display
  630. if { $previous_frame_count != $new_frame_count || $lag_counter == -1} {
  631. set previous_frame_count $new_frame_count
  632. set col [expr {!$input_read_in_frame ? 0xff000080 : "0x0044aa80 0x2266dd80 0x0055cc80 0x44aaff80"}]
  633. osd configure lag_counter -rgba $col
  634. if {!$input_read_in_frame} {
  635. incr lag_counter
  636. osd configure lag_counter.text -text "Lag fr.: $lag_counter"
  637. } else {
  638. set input_read_in_frame false
  639. }
  640. }
  641. }
  642. proc reset_lag_counter {} {
  643. variable lag_counter
  644. set lag_counter -1
  645. update_lag_counter2
  646. return ""
  647. }
  648. ### movie length display ###
  649. set_help_text toggle_movie_length_display\
  650. {Toggles display of the movie length in the upper right corner.}
  651. proc toggle_movie_length_display {} {
  652. if {[osd exists movielength]} {
  653. disable_periodic movielength_update
  654. osd destroy movielength
  655. return ""
  656. }
  657. osd create rectangle movielength \
  658. -x 269 -y 234 -h 7 -w 42 -scaled true \
  659. -rgba "0x0044aa80 0x2266dd80 0x0055cc80 0x44aaff80" \
  660. -borderrgba 0x00000040 -bordersize 0.5
  661. osd create text movielength.text -x 3 -y 1 -size 4 -rgba 0xffffffff
  662. enable_periodic movielength_update
  663. return ""
  664. }
  665. proc movielength_update {} {
  666. osd configure movielength.text -text "Length: [utils::format_time_subseconds [dict get [reverse status] last_event]]"
  667. }
  668. namespace export toggle_frame_counter
  669. namespace export prev_frame
  670. namespace export next_frame
  671. namespace export start_of_frame
  672. namespace export advance_frame
  673. namespace export reverse_frame
  674. namespace export toggle_cursors
  675. namespace export ram_watch
  676. namespace export toggle_lag_counter
  677. namespace export reset_lag_counter
  678. namespace export toggle_movie_length_display
  679. }
  680. namespace import tas::*