_guess_title.tcl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. namespace eval guess_title {
  2. set_help_text guess_title \
  3. {Guess the title of the currently running software. Remember... it's only a guess! It will be wrong some times. (But it will be right in many cases.)
  4. }
  5. # Here are some cases worth to consider and test.
  6. # * FM-PAC as extension and a ROM game slot 2. You don't want to get the FM-PAC
  7. # returned when it's used as FM module, but you do when you did _FMPAC and use
  8. # the internal software of it.
  9. # * Rollerball runs in page 2 (when using the proper ROM type)
  10. # * Philips Music Module, start up with ESC. You don't want to return the ROM
  11. # as 'running software', but when you did not press ESC, you do.
  12. # * Koei games like Teitoku no Ketsudan. In combination with an FM-PAC in slot
  13. # 1. This games seems to run from RAM mostly.
  14. # * Tape converted to cart. Runs in RAM.
  15. # * SCC extension in combination with a disk game/demo. Runs with an empty ROM
  16. # which you don't want to return as title.
  17. # * Sony HB-75P with internal software (Personal Data Bank). Runs in page 2.
  18. # * MSX-DOS
  19. # * MSX-DOS 2 (all pages are RAM)
  20. # this one checks on the checkpage for external or internal software
  21. proc guess_rom_device_z80space {internal checkpage} {
  22. lassign [get_selected_slot $checkpage] ps ss
  23. if {$ss eq "X"} {set ss 0}
  24. set incorrectslottype [machine_info isexternalslot $ps $ss]
  25. if {$internal} {
  26. set incorrectslottype [expr {!$incorrectslottype}]
  27. }
  28. if {$incorrectslottype} {
  29. foreach device [machine_info slot $ps $ss $checkpage] {
  30. set type [dict get [machine_info device $device] "type"]
  31. # try to ignore RAM devices
  32. if {$type ne "RAM" && $type ne "MemoryMapper" && $type ne "PanasonicRAM"} {
  33. return $device
  34. }
  35. }
  36. }
  37. return ""
  38. }
  39. proc guess_rom_device_nonextension {} {
  40. set system_rom_paths [filepool::get_paths_for_type system_rom]
  41. # Loop over all external slots which contain a ROM, return the first
  42. # which is not located in one of the systemrom filepools.
  43. for {set ps 0} {$ps < 4} {incr ps} {
  44. for {set ss 0} {$ss < 4} {incr ss} {
  45. if {![machine_info isexternalslot $ps $ss]} continue
  46. foreach device [machine_info slot $ps $ss 1] {
  47. set path ""
  48. catch {set path [dict get [machine_info device $device] "filename"]}
  49. if {$path eq ""} continue
  50. set ok 1
  51. foreach syspath $system_rom_paths {
  52. if {[string first $syspath $path] == 0} {
  53. set ok 0; break
  54. }
  55. }
  56. if {$ok} {return $device}
  57. }
  58. }
  59. }
  60. return ""
  61. }
  62. proc guess_rom_device_naive {} {
  63. for {set ps 0} {$ps < 4} {incr ps} {
  64. for {set ss 0} {$ss < 4} {incr ss} {
  65. if {[machine_info isexternalslot $ps $ss]} {
  66. set device_list [list]
  67. foreach device [machine_info slot $ps $ss 1] {
  68. # try to ignore RAM devices
  69. set type [dict get [machine_info device $device] "type"]
  70. if {$type ne "RAM" && $type ne "MemoryMapper" && $type ne "PanasonicRAM"} {
  71. lappend device_list $device
  72. }
  73. }
  74. if {[llength $device_list] != 0} {
  75. return $device_list
  76. }
  77. }
  78. }
  79. }
  80. return ""
  81. }
  82. proc guess_disk_title {drive_name} {
  83. # check name of the diskimage (remove directory part and extension)
  84. set disk ""
  85. catch {set disk [lindex [$drive_name] 1]}
  86. return [file rootname [file tail $disk]]
  87. }
  88. proc guess_cassette_title {} {
  89. # check name of the cassette image (remove directory part and extension)
  90. set cassette ""
  91. catch {set cassette [lindex [cassetteplayer] 1]}
  92. return [file rootname [file tail $cassette]]
  93. }
  94. proc guess_title {{fallback ""}} {
  95. # first try to see what is actually mapped in Z80 space
  96. # that is often correct, if it gives a result...
  97. # but it doesn't give a result for ROMs that copy themselves to RAM
  98. # (e.g. Koei games, tape games converted to ROM, etc.).
  99. set result [guess_rom_device_z80space false 1]
  100. if {$result ne ""} {return [rom_device_to_title $result]}
  101. # then try disks
  102. # games typically run from drive A, almost never from another drive
  103. set title [guess_disk_title "diska"]
  104. if {$title ne ""} {return $title}
  105. # then try cassette
  106. set title [guess_cassette_title]
  107. if {$title ne ""} {return $title}
  108. # if that doesn't give a result, try non extension devices
  109. set result [guess_rom_device_nonextension]
  110. if {$result ne ""} {return [rom_device_to_title $result]}
  111. # if that doesn't give a result, just return the first thing we find in
  112. # an external slot
  113. # ... this doesn't add much to the nonextension version
  114. # set result [guess_rom_device_naive]
  115. # perhaps we should simply return internal software if nothing found yet
  116. # Do page 1 last, because BASIC is in there
  117. set result [guess_rom_device_z80space true 3]
  118. if {$result ne ""} {return [rom_device_to_title $result]}
  119. set result [guess_rom_device_z80space true 2]
  120. if {$result ne ""} {return [rom_device_to_title $result]}
  121. set result [guess_rom_device_z80space true 1]
  122. if {$result ne ""} {return [rom_device_to_title $result]}
  123. # guess failed, return fallback
  124. return $fallback
  125. }
  126. proc rom_device_to_title {device} {
  127. set result $device
  128. if {[string tolower [file extension $device]] in [list .rom .ri .mx1 .mx2]} {
  129. set result [string totitle [file rootname $device]]
  130. }
  131. return $result
  132. }
  133. # use this proc if you only want to guess ROM titles
  134. proc guess_rom_title {} {
  135. return [rom_device_to_title [guess_rom_device]]
  136. }
  137. proc guess_rom_device {} {
  138. set result [guess_rom_device_z80space false 1]
  139. if {$result ne ""} {return $result}
  140. # if that doesn't give a result, try non extension devices
  141. set result [guess_rom_device_nonextension]
  142. if {$result ne ""} {return $result}
  143. # if that doesn't give a result, just return the first thing we find in
  144. # an external slot (but not RAM)
  145. set result [guess_rom_device_naive]
  146. return $result
  147. }
  148. namespace export guess_title
  149. namespace export guess_rom_title
  150. namespace export guess_rom_device
  151. } ;# namespace guess_title
  152. namespace import guess_title::*