class D64::Image
Attributes
filename[R]
name[RW]
num_tracks[R]
Public Class Methods
build_offset_table()
click to toggle source
# File lib/d64/image.rb, line 22 def self.build_offset_table ss = -1 40.times.map do |i| tn = i + 1 sectors_per_track(tn).times.map do ss += 1 ss * 256 end end end
new()
click to toggle source
# File lib/d64/image.rb, line 38 def initialize @num_tracks = 35 @name = '' end
offset(block)
click to toggle source
# File lib/d64/image.rb, line 18 def self.offset(block) (@offsets ||= build_offset_table)[block.track - 1][block.sector] end
read(filename)
click to toggle source
# File lib/d64/image.rb, line 12 def self.read(filename) di = new di.read(filename) di end
sectors_per_track(track)
click to toggle source
# File lib/d64/image.rb, line 33 def self.sectors_per_track(track) @spt ||= [0] + [21] * 17 + [19] * 7 + [18] * 6 + [17] * 10 @spt[track] end
Public Instance Methods
allocate_sector(opts = {})
click to toggle source
# File lib/d64/image.rb, line 70 def allocate_sector(opts = {}) block = bam.allocate(opts) or fail "Failed to allocate sector!" Sector.new(self, block, [0] * 256) end
bam()
click to toggle source
# File lib/d64/image.rb, line 61 def bam block = Block.new(18, 0) @bam ||= BlockMap.new(self, block, sector_data(block)) end
commit_bam()
click to toggle source
# File lib/d64/image.rb, line 80 def commit_bam @image[Image.offset(@bam.block) + 4, 140] = @bam.bytes[4, 140] end
commit_sector(sector)
click to toggle source
# File lib/d64/image.rb, line 76 def commit_sector(sector) @image[Image.offset(sector.block), 256] = sector.bytes end
directory_chain()
click to toggle source
# File lib/d64/image.rb, line 66 def directory_chain sector([18, 0]).chain end
read(file)
click to toggle source
# File lib/d64/image.rb, line 47 def read(file) @filename = file @image = File.binread(filename).bytes end
sector(block)
click to toggle source
# File lib/d64/image.rb, line 56 def sector(block) block = Block.new(block[0], block[1]) if block.is_a?(Array) Sector.new(self, block, sector_data(block)) end
to_s()
click to toggle source
# File lib/d64/image.rb, line 43 def to_s "<D64::Image:#{object_id.to_s(16)} @filename=#{filename}>" end
write(file = filename)
click to toggle source
# File lib/d64/image.rb, line 52 def write(file = filename) File.open(file, 'wb') { |f| f.write @image.pack('C*') } end
Private Instance Methods
sector_data(block)
click to toggle source
# File lib/d64/image.rb, line 86 def sector_data(block) @image[Image.offset(block), 256] end