Class: D64::BlockMap

Inherits:
Sector show all
Defined in:
lib/d64/block_map.rb

Instance Attribute Summary

Attributes inherited from Sector

#block, #bytes, #image

Instance Method Summary collapse

Methods inherited from Sector

#chain, #copy_content_from, #dump, #initialize, #link_to, #name_pos, #next, #pos, #to_s

Constructor Details

This class inherits a constructor from D64::Sector

Instance Method Details

#allocate(opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/d64/block_map.rb', line 24

def allocate(opts = {})
  if opts[:block]
    tn = opts[:block].track
    sn = opts[:block].sector
    free_on_track(tn)[sn] or
      fail "Can't allocate used sector [%s %02d:%02d]." % [@image.name, tn, sn]
  else
    tracks = (1..35).to_a
    if tn = opts[:trackpref]
      tracks.delete  opts[:trackpref]
      tracks.unshift opts[:trackpref]
    end
    sn = nil
    tracks.each do |tn|
      sn = free_on_track(tn).index(true)
      break if sn
    end
    return nil unless sn
  end
  mark_as_used tn, sn
  free_on_track(tn)[sn] and
    fail "Failed to mark as used!"
  puts 'Allocated sector [%s %02d:%02d]' % [@image.name, tn, sn] if ENV['DEBUG']
  Block.new(tn, sn)
end

#commitObject



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

def commit
  image.commit_bam
end

#dump_mapObject



7
8
9
10
11
# File 'lib/d64/block_map.rb', line 7

def dump_map
  puts (1..35).map { |tn|
    ("%2s " % tn) << free_on_track(tn).map { |v| v ? '[ ]' : '[x]' }.join
  }.join "\n";
end

#free_blocksObject



20
21
22
# File 'lib/d64/block_map.rb', line 20

def free_blocks
  35.times.map { |i| bytes[4 * i + 4] }.reduce :+
end

#free_on_track(tn) ⇒ Object



13
14
15
16
17
18
# File 'lib/d64/block_map.rb', line 13

def free_on_track(tn)
  tv = track_value(tn)
  Image.sectors_per_track(tn).times.map do |sn|
    tv & (2 ** sn) != 0
  end
end

#mark_as_unused(tn, sn) ⇒ Object



56
57
58
59
60
# File 'lib/d64/block_map.rb', line 56

def mark_as_unused(tn, sn)
  byte, bit = sn.divmod(8)
  @bytes[4 * tn + 1 + byte] |= (2 ** bit)
  @bytes[4 * tn] += 1
end

#mark_as_used(tn, sn) ⇒ Object



50
51
52
53
54
# File 'lib/d64/block_map.rb', line 50

def mark_as_used(tn, sn)
  byte, bit = sn.divmod(8)
  @bytes[4 * tn + 1 + byte] &= (255 - 2 ** bit)
  @bytes[4 * tn] -= 1
end