set __psg_log_file -1

proc start_psg_log { {filename "log.psg"} } {
	global __psg_log_file
	set __psg_log_file [open $filename {WRONLY TRUNC CREAT}]
	set header "0x50 0x53 0x47 0x1A 0 0 0 0 0 0 0 0 0 0 0 0"
	fconfigure $__psg_log_file -translation binary
	puts -nonewline $__psg_log_file [binary format c16 $header]
	__do_psg_log
	return ""
}

proc stop_psg_log {} {
	global __psg_log_file
	
	close $__psg_log_file
	set __psg_log_file -1
	return ""
}

proc __do_psg_log {} {
	global __psg_log_file
	if {$__psg_log_file == -1} return
	for {set i 0} {$i < 14} {incr i} {
		set value [debug read "PSG regs" $i]
		puts -nonewline $__psg_log_file [binary format c2 "$i $value"]
	}
	puts -nonewline $__psg_log_file [binary format c 0xFF]
	after frame __do_psg_log
}