Class: D64::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/d64/image.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImage

Returns a new instance of Image.



38
39
40
41
# File 'lib/d64/image.rb', line 38

def initialize
  @num_tracks = 35
  @name = ''
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/d64/image.rb', line 9

def filename
  @filename
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/d64/image.rb', line 10

def name
  @name
end

#num_tracksObject (readonly)

Returns the value of attribute num_tracks.



9
10
11
# File 'lib/d64/image.rb', line 9

def num_tracks
  @num_tracks
end

Class Method Details

.build_offset_tableObject



22
23
24
25
26
27
28
29
30
31
# 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

.offset(block) ⇒ Object



18
19
20
# File 'lib/d64/image.rb', line 18

def self.offset(block)
  (@offsets ||= build_offset_table)[block.track - 1][block.sector]
end

.read(filename) ⇒ Object



12
13
14
15
16
# File 'lib/d64/image.rb', line 12

def self.read(filename)
  di = new
  di.read(filename)
  di
end

.sectors_per_track(track) ⇒ Object



33
34
35
36
# 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

Instance Method Details

#allocate_sector(opts = {}) ⇒ Object



70
71
72
73
74
# 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

#bamObject



61
62
63
64
# File 'lib/d64/image.rb', line 61

def bam
  block = Block.new(18, 0)
  @bam ||= BlockMap.new(self, block, sector_data(block))
end

#commit_bamObject



80
81
82
# 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) ⇒ Object



76
77
78
# File 'lib/d64/image.rb', line 76

def commit_sector(sector)
  @image[Image.offset(sector.block), 256] = sector.bytes
end

#directory_chainObject



66
67
68
# File 'lib/d64/image.rb', line 66

def directory_chain
  sector([18, 0]).chain
end

#read(file) ⇒ Object



47
48
49
50
# File 'lib/d64/image.rb', line 47

def read(file)
  @filename = file
  @image    = File.binread(filename).bytes
end

#sector(block) ⇒ Object



56
57
58
59
# 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_sObject



43
44
45
# File 'lib/d64/image.rb', line 43

def to_s
  "<D64::Image:#{object_id.to_s(16)} @filename=#{filename}>"
end

#write(file = filename) ⇒ Object



52
53
54
# File 'lib/d64/image.rb', line 52

def write(file = filename)
  File.open(file, 'wb') { |f| f.write @image.pack('C*') }
end