This file is indexed.

/usr/share/openmsx/scripts/_cashandler.tcl is in openmsx-data 0.12.0-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
namespace eval cashandler {

set help_text_cas \
{-----------------------------------------------------------
  CAS-file tools 1.0 for openMSX Made By: NYYRIKKI & wouter_
------------------------------------------------------------
Usage:
 casload <file>             | Open CAS-file for load
 cassave <file>             | Open CAS-file for save
 caslist                    | Show loaded CAS-file content
 casrun [<file>] [<number>] | Automatically run program
 caspos <number>            | Select file to load from CAS
 caseject                   | Deactivate CAS-file support
}
set_help_text casload  $help_text_cas
set_help_text cassave  $help_text_cas
set_help_text caslist  $help_text_cas
set_help_text casrun   $help_text_cas
set_help_text caspos   $help_text_cas
set_help_text caseject $help_text_cas
set_tabcompletion_proc casload utils::file_completion
set_tabcompletion_proc cassave utils::file_completion
set_tabcompletion_proc casrun  utils::file_completion

variable fidr ""  ;# file id of opened read  file, "" if not active
variable fidw ""  ;# file id of opened write file, "" if not active
variable bptapion ;# tapion
variable bptapin  ;# tapin
variable bptapoon ;# tapoon
variable bptapout ;# tapout
variable bptapoof ;# tapoof
variable bphread  ;# h.read

variable casheader [binary format H* "1FA6DEBACC137D74"]
variable casbios [dict create r [list 0x00E2 tapion 0x00E5 tapin  0x00E8 tapiof] \
                              w [list 0x00EB tapoon 0x00EE tapout 0x00F1 tapoof]]

proc casload {filename} {
	casopen $filename "r"
	return "Cassette inserted, overriding normal openMSX cassette load routines."
}
proc cassave {filename} {
	casopen $filename "w"
	return "Cassette inserted, overriding normal openMSX cassette save routines."
}
proc caseject {} {
	casclose "r"
	casclose "w"
	return "Cassette ejected, normal openMSX cassette routines in use."
}

proc casopen {filename rw} {
	# Possibly close previous file.
	casclose $rw

	# Open file.
	variable fid${rw}
	set fid${rw} [open $filename $rw]
	fconfigure [set fid${rw}] -translation binary -encoding binary

	# Install BIOS handlers.
	variable casbios
	foreach {addr func} [dict get $casbios $rw] {
		variable bp${func}
		set bp${func} [debug set_bp [peek16 $addr "slotted memory"] {[pc_in_slot 0 0]} [namespace code $func]]
	}
}

proc casclose {rw} {
	# Was active?
	variable fid${rw}
	if {[set fid${rw}] eq ""} return

	# Uninstall BIOS handlers.
	variable casbios
	foreach {addr func} [dict get $casbios $rw] {
		variable bp${func}
		debug remove_bp [set bp${func}]
	}

	if {$rw eq "r"} {
		# In case of read (possibly) also remove bphread.
		variable bphread
		catch {debug remove_bp $bphread} ;# often not set, so catch error
	} else {
	        # In case of write align end of file in order to make combine with other CAS-files easy.
		set align [expr {[tell $fidw] & 7}]
		if {$align} {puts -nonewline $fidw [string repeat \x0 [expr {8 - $align}]]}
	}

	# Close file and deactivate.
	close [set fid${rw}]
	set fid${rw} ""
}

proc tapion {} {
	# TAPION
	# Address  : #00E1
	# Function : Reads the header block after turning the cassette motor on
	# Output   : C-flag set if failed
	# Registers: All
	reg F [expr {([seekheader] == -1) ? 0x01 : 0x40}] ;# set carry flag if header not found
	ret
}

proc tapin {} {
	# TAPIN
	# Address  : #00E4
	# Function : Read data from the tape
	# Output   : A  - read value
	#           C-flag set if failed
	# Registers: All
	variable fidr
	if {[binary scan [read $fidr 1] cu val]} {
		reg A $val
		reg F 0x40 ;# ok, clear carry flag
	} else {
		reg F 1 ;# end-of-file reached, set carry flag
	}
	ret
}

proc tapiof {} {
        #TAPIOF
        #Address  : #00E7
        #Function : Stops reading from tape
        #Registers: None
	ret
}

proc tapoon {} {
        #TAPOON
        #Address  : #00EA
        #Function : Turns on the cassette motor and writes the header
        #Input    : A  - #00 short header
        #            not #00 long header
        #Output   : C-flag set if failed
        #Registers: All
	if {[catch {
		variable fidw
		variable casheader
		set align [expr {[tell $fidw] & 7}]
		if {$align} {puts -nonewline $fidw [string repeat \x0 [expr {8 - $align}]]}
		puts -nonewline $fidw $casheader
		reg F 0x40 ;# ok, clear carry flag
	}]} {
		reg F 1 ;# write error, set carry flag
	}
	ret
}

proc tapout {} {
        #TAPOUT
        #Address  : #00ED
        #Function : Writes data on the tape
        #Input    : A  - data to write
        #Output   : C-flag set if failed
        #Registers: All
	if {[catch {
		variable fidw
		puts -nonewline $fidw [binary format c* [reg A]]
		reg F 0x40 ;# all went fine, clear carry flag
	}]} {
		reg F 1 ;# write error, set carry flag
	}
	ret
}

proc tapoof {} {
        #TAPOOF
        #Address  : #00F0
        #Function : Stops writing on the tape
        #Registers: None
	ret
}

proc ret {} {
	reg PC [peek16 [reg SP]]
	reg SP [expr {[reg SP] + 2}]
}

proc seekheader {} {
	# Skip till 8-bytes aligned.
	variable fidr
	set align [expr {[tell $fidr] & 7}]
	if {$align} {read $fidr [expr {8 - $align}]}

	# Search header.
	variable casheader
	while {![eof $fidr] && [read $fidr 8] ne $casheader} {}

	# Return position of header in cas file, or -1 if not found.
	expr {[eof $fidr] ? -1 : ([tell $fidr] - 8)}
}

proc readheader {} {
	# Read (first) type-id byte.
	variable fidr
	set byte [read $fidr 1]
	if {![binary scan $byte cu val]} {return -1}

	# This must be one of 0xEA 0xD0 0xD3.
	set type [lsearch -exact -integer [list 0xEA 0xD0 0xD3] $val]
	if {$type == -1} {return -1}

	# And it must repeat 9 more times.
	for {set i 0} {$i < 9} {incr i} {
		if {[read $fidr 1] ne $byte} {return -1}
	}
	return $type
}

proc checkactive {} {
	variable fidr
	if {$fidr eq ""} {error "No cas file loaded, use 'casload <filename>'."}
}

proc caslist {} {
	checkactive

	set    result "Position: Type: Name:    Offset:\n"
	append result "--------------------------------\n"

	variable fidr
	set oldpos [tell $fidr]
	seek $fidr 0
	set i 0
	while {![eof $fidr]} {
		set headerpos [seekheader]
		set type [readheader]
		if {$type == -1} continue
		append result [expr {($headerpos < $oldpos) ? "| " : "  "}]
		append result [format %5d [incr i]] " : "
		append result [lindex "TXT BIN BAS" $type] " : "
		append result [read $fidr 6] " : "
		append result $headerpos "\n"
	}
	seek $fidr $oldpos
	return $result
}

proc caspos {{position 1}} {
	checkactive
	lassign [seekpos $position] headerpos type
	return "Cassette header put to offset: $headerpos"
}

# Seek to the start of the n-th header and return both the
# file-offset and the type of this header.
proc seekpos {position} {
	if {![string is integer $position] || ($position <= 0)} {
		error "Expected a strict positive integer, but got $position."
	}

	variable fidr
	seek $fidr 0
	set i 0
	while {$i != $position} {
		set headerpos [seekheader]
		set type [readheader]
		if {$type != -1} {incr i}
		if {[eof $fidr]} {error "No entry $position in this cas file."}
	}
	seek $fidr $headerpos
	list $headerpos $type
}

proc casrun {{filename 1} {position 1}} {
	variable fidr
	variable bphread
	if {[string is integer $filename] && ($fidr ne "")} {
		# First argument is actually a position instead of a filename,
		# only works when there already is a cas file loaded.
		set position $filename
		catch {debug remove_bp $bphread} ;# often not set, so catch error
	} else {
		# Interpret 1st argument as a filename and load it.
		casload $filename
	}
	lassign [seekpos $position] headerpos type

	catch {carta eject}
	catch {cartb eject} ;# there are machines without slot-B
	reset
	set ::power on
	keymatrixdown 6 1 ;# press SHIFT
	set bphread [debug set_bp 0xff07 {} [namespace code "typeload $type"]]
	return ""
}

proc typeload {type} {
	variable bphread
	debug remove_bp $bphread
	keymatrixup 6 1 ;# release SHIFT

	set cmd [lindex [list "RUN\"CAS:\r" "BLOAD\"CAS:\",R\r" "CLOAD\rRUN\r"] $type]
	set keybuf 0xfbf0
	debug write_block memory $keybuf $cmd
	poke16 0xf3fa $keybuf
	poke16 0xf3f8 [expr {$keybuf + [string length $cmd]}]
}

######################################################
proc tapedeck {args} {
	if {[string toupper [file extension [lindex $args end]]]==".CAS"} {

		switch [lindex $args 0] {
			eject         {caseject}
			rewind        {caspos 1}
			motorcontrol  {}
			play          {}
			record        {}
			new           {cassave [lindex $args 1]}
			insert        {casload [lindex $args 1]}
			getpos        {}
			getlength     {}
			""            {}
			default       {casload $args}
		}
	} else {
		caseject
	}
	return [uplevel 1 [list interp invokehidden {} -global cassetteplayer] $args]
}

######################################################

namespace export casload cassave caslist casrun caspos caseject

} ;# namespace cashandler

namespace import cashandler::*