$:.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 G64 < D64 # # raw GCR binary representation of a 1541 diskette # # References: # http://www.unusedino.de/ec64/technical/formats/g64.html # http://www.baltissen.org/newhtm/1541c.htm # http://www.scribd.com/doc/40438/The-Commodore-1541-Disk-Drive-Users-Guide # CBM_GCR_CODES={ "01010"=>0x00, "01011"=>0x01, "10010"=>0x02, "10011"=>0x03, "01110"=>0x04, "01111"=>0x05, "10110"=>0x06, "10111"=>0x07, "01001"=>0x08, "11001"=>0x09, "11010"=>0x0A, "11011"=>0x0B, "01101"=>0x0C, "11101"=>0x0D, "11110"=>0x0E, "10101"=>0x0F } #G64 format starts with a signature is_valid_image_if lambda { file_system_image.file_bytes[0,8]=="GCR-1541" } def initialize(file_bytes) @file_bytes=file_bytes @tracks={} @end_track=0 signature,version,raw_track_count,track_length=file_bytes.unpack("A8ccv") 0.upto(raw_track_count-1) do |raw_track_no| cbm_track_no=(raw_track_no+2.0)/2 track_offset=file_bytes[0x0c+4*raw_track_no,4].unpack("V")[0] if track_offset>0 then @end_track=cbm_track_no end end end def self.possible_extensions ['.g64'] end def track_count @end_track end def end_track @end_track end def get_sector(track_no,sector_no) #puts "TR #{track_no} : #{sector_no}" parse_raw_track((track_no-1)*2) if @tracks[track_no].nil? @tracks[track_no][sector_no] end private def parse_raw_track(raw_track_no) # cbm_track_no=(raw_track_no+2.0)/2 track_offset=file_bytes[0x0c+4*raw_track_no,4].unpack("V")[0] if track_offset>0 then track_length=file_bytes[track_offset,2].unpack("v")[0] track_bytes=file_bytes[track_offset+2,track_length] # puts "RAW #{raw_track_no} CBM #{cbm_track_no} OFFSET #{track_offset} #{track_bytes.length}" s="" track_bytes.each_byte do |byte| s+="%08b" % byte end raw_blocks=s.split(/1{10,200}/) decoded_blocks=[] raw_blocks.each do |raw_block| this_byte=0 high_nibble=true decoded_block_bytes="" 0.upto([(raw_block.length/5-1),518].min) do |i| gcr_bits=raw_block[i*5,5] this_nibble=CBM_GCR_CODES[gcr_bits] if this_nibble.nil? then # puts "invalid GCR bits #{gcr_bits} in #{raw_block} offset #{i*5}" else if (high_nibble) then this_byte=this_nibble*0x10 high_nibble=false else this_byte+=this_nibble high_nibble=true decoded_block_bytes<>>>" #puts raw_block #puts "ID BYTE %02X %02X " % [decoded_block_bytes[0],decoded_block_bytes.length] #decoded_block_bytes.each {|x| print "%02x " % x} #puts "<<<<" #puts end 0.upto(decoded_blocks.length-2) do |i| if (decoded_blocks[i][0]==0x08) && (decoded_blocks[i+1][0]==0x07) then header_block=decoded_blocks[i] data_block=decoded_blocks[i+1] sector_no=header_block[2] track_no=header_block[3] format_id=header_block[4]+header_block[5] @tracks[track_no]={} if @tracks[track_no].nil? # puts "TRACK %02X SECTOR %02X ID #{format_id} LENGTH %02x" % [track_no,sector_no,data_block.length] sector_data=data_block[1,data_block.length-1] @tracks[track_no][sector_no]=sector_data #puts hex_dump(data_block[1,256]) end end end end end # == Author # Jonno Downes (jonno@jamtronix.com) # # == Copyright # Copyright (c) 2008 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.