$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) $:.unshift(File.dirname(__FILE__+"\\..")) unless $:.include?(File.dirname(__FILE__+"\\..")) || $:.include?(File.expand_path(File.dirname(__FILE__+"\\.."))) require 'ImageFormat' class D81< ImageFormat # # For manipulating D81 files, as used by many C64 emulators. # # References: # http://www.unusedino.de/ec64/technical/formats/d81.html # D81_BYTES_PER_SECTOR=0x100 D81_SECTORS_PER_TRACK=40 def self.possible_extensions ['.d81'] end def self.host_system C64 end def sectors_in_track(track_no) D81_SECTORS_PER_TRACK end #for a given byte stream, how many tracks could be interpreted by this image format? def track_count file_bytes.length/(D81_SECTORS_PER_TRACK*256) end def end_track return track_count end #what track does the directory start on? def dir_track_no 40 end #what sector does the directory start on? def dir_sector_no 3 end def get_sector(track_no,sector_no) return nil unless track_no.between?(1,end_track) return nil unless sector_no.between?(0,D81_SECTORS_PER_TRACK-1) track_start=D81_BYTES_PER_SECTOR*(D81_SECTORS_PER_TRACK*(track_no-1)) sector_start=track_start+(D81_BYTES_PER_SECTOR*sector_no) # puts "T %0d, S %0d, TRACK OFFSET=$%x, SECTOR OFFSET=$%x" % [track_no,sector_no,track_start,sector_start] file_bytes[sector_start,D81_BYTES_PER_SECTOR] end def set_sector(track_no,sector_no,sector_contents) raise "d81 currently read only" raise "invalid track number #{track_no}" unless track_no.between?(1,end_track) raise "invalid sector number #{sector_no}" unless sector_no.between?(0,D81_SECTORS_PER_TRACK-1) padded_sector_contents=(sector_contents+("\000"*D81_BYTES_PER_SECTOR))[0,D81_BYTES_PER_SECTOR] track_start=D81_BYTES_PER_SECTOR*(D81_SECTORS_PER_TRACK*(track_no-1)) sector_start=track_start+(D81_BYTES_PER_SECTOR*sector_no) file_bytes[sector_start,D81_BYTES_PER_SECTOR]=padded_sector_contents end end # == Author # Jonno Downes (jonno@jamtronix.com) # # == Copyright # Copyright (c) 2008,2009 Jonno Downes (jonno@jamtronix.com) # #Permission is hereby granted, free of charge, to any person obtaining #a copy of this software and associated documentation files (the #"Software"), to deal in the Software without restriction, including #without limitation the rights to use, copy, modify, merge, publish, #distribute, sublicense, and/or sell copies of the Software, and to #permit persons to whom the Software is furnished to do so, subject to #the following conditions: # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.