d64tools 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0124058c11111a068dd31c4535edc1b6f4f168a9
4
+ data.tar.gz: fa1a24b4e2aad4cd2dea0b7cd683dce907fa2064
5
+ SHA512:
6
+ metadata.gz: 711722c77f90011c4ba737a9764b816c83f4c2665d9bb5a99415633bdc0769baf9408b1678e47713151f331bfa07419f2f29c2f346be614d80a1ad6b401230c1
7
+ data.tar.gz: 8fb079f24abff8ce7b268ecfd351614bef9eabf662acdde60b7498fadc5fd949922b61eb66b77c365f59a50b1fc5d3b03b188a64b31fb7347b8d60beff35927e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in d64tools.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Lars Haugseth
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # D64tools
2
+
3
+ A Ruby gem with tools for working with D64 images (dumps of Commodore 1541 floppy disks.)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'd64tools', github: 'lhz/d64tools'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install d64tools
20
+
21
+ ## Usage
22
+
23
+ There are several tools installed with this gem:
24
+
25
+ ```
26
+ d64reserve <d64-image>
27
+ ```
28
+ This tool will reserve track 35 in the BAM of the given d64 image, to avoid other tools adding files to the image using that track.
29
+
30
+ ```
31
+ d64unreserve <d64-image>
32
+ ```
33
+ This tool will free up track 35 in the BAM of the given d64 image, so that other tools may utilize that track again.
34
+
35
+ ```
36
+ d64hide <d64-source-image> <d64-dirart-image> <d64-target-image>
37
+ ```
38
+ This tool will move the directory from track 18 on the source image over to track 35, copy the directory from track 18 on the dirart image on to track 18 and store the result in the target image.
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/d64tools.
49
+
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
54
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "d64tools"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/d64tools.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'd64/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "d64tools"
8
+ spec.version = D64::VERSION
9
+ spec.authors = ["Lars Haugseth"]
10
+ spec.email = ["github@larshaugseth.com"]
11
+
12
+ spec.summary = %q{D64 floppy image tools}
13
+ spec.description = %q{Tools to perform various operations on D64 floppy images.}
14
+ spec.homepage = "http://github.com/lhz/d64tools"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
data/exe/d64hide ADDED
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'd64tools'
4
+
5
+ source_file = ARGV[0]
6
+ dirart_file = ARGV[1]
7
+ target_file = ARGV[2]
8
+
9
+ source_file && dirart_file && target_file or
10
+ fail "Usage: #{$0} <d64-source> <d64-dirart> <d64-target>"
11
+
12
+ source_image = D64::Image.read(source_file)
13
+ source_image.name = 'source'
14
+
15
+ dirart_image = D64::Image.read(dirart_file)
16
+ dirart_image.name = 'dirart'
17
+
18
+ source_dir = source_image.directory_chain
19
+ dirart_dir = dirart_image.directory_chain
20
+
21
+ art_size = dirart_dir.size
22
+ src_free = source_image.bam.free_blocks
23
+
24
+ src_free >= art_size or
25
+ fail "Only #{src_free} of #{art_size} blocks free for dirart!"
26
+
27
+ puts "Dir art needs #{art_size} out of #{src_free} free blocks." if ENV['DEBUG']
28
+
29
+ prv_sector = nil
30
+ dir_sector = nil
31
+
32
+ boot_ptr = source_image.sector([18, 1]).bytes[3, 2]
33
+ puts "Boot file at [%02d:%02d]" % boot_ptr if ENV['DEBUG']
34
+
35
+ target_sectors = [
36
+ [35, 0], [35, 1], [35, 4], [35, 7], [35, 10],
37
+ [35, 13], [35, 16], [35, 2], [35, 5], [35, 8],
38
+ [35, 11], [35, 14], [35, 3], [35, 6], [35, 9],
39
+ [35, 12], [35, 15]
40
+ ]
41
+
42
+ [dirart_dir.size, source_dir.size].max.times do |i|
43
+ src_sector = source_dir[i]
44
+ art_sector = dirart_dir[i]
45
+
46
+ if src_sector
47
+ # Move source sector to track 35 and replace with dirart sector
48
+ wanted = target_sectors[i]
49
+ new_sector = source_image.allocate_sector(block: D64::Block.new(*wanted))
50
+ new_sector.copy_content_from(src_sector)
51
+
52
+ if art_sector
53
+ src_sector.copy_content_from(art_sector, include_link: i == art_size - 1)
54
+ end
55
+ else
56
+ # Just copy the dirart sector since we ran out of files on source
57
+ new_sector = source_image.allocate_sector(block: art_sector.block)
58
+ new_sector.copy_content_from(art_sector)
59
+ end
60
+
61
+ if prv_sector
62
+ prv_sector.link_to new_sector
63
+ else
64
+ dir_sector = new_sector
65
+ end
66
+ prv_sector = new_sector
67
+
68
+ source_image.commit_bam
69
+ end
70
+
71
+ # if source_dir.size > art_size
72
+ # prv_sector.link_to(source_dir[art_size])
73
+ # end
74
+
75
+ # Make first dirart file point to first real file
76
+ sector = source_image.sector([18, 1])
77
+ fnum = (0..7).find { |i| sector.bytes[32 * i + 2] == 0x82 }
78
+ sector.bytes[32 * fnum + 3, 2] = boot_ptr
79
+ sector.commit
80
+
81
+ source_image.write(target_file)
82
+
83
+ puts "Finished, hidden directory now at #{dir_sector.pos}." if ENV['DEBUG']
data/exe/d64reserve ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Reserve all sectors on track 35 for hidden directory
4
+
5
+ require 'd64tools'
6
+
7
+ file = ARGV[0]
8
+
9
+ file or
10
+ fail "Usage: #{$0} <d64-file>"
11
+
12
+ image = D64::Image.read(file)
13
+
14
+ 17.times do |sn|
15
+ image.bam.mark_as_used 35, sn
16
+ end
17
+ image.bam.commit
18
+
19
+ image.write file
data/exe/d64unreserve ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Unreserve all sectors on track 35 to make room for hidden directory
4
+
5
+ require 'd64tools'
6
+
7
+ file = ARGV[0]
8
+
9
+ file or
10
+ fail "Usage: #{$0} <d64-file>"
11
+
12
+ image = D64::Image.read(file)
13
+
14
+ 17.times do |sn|
15
+ image.bam.mark_as_unused 35, sn
16
+ end
17
+ image.bam.commit
18
+
19
+ image.write file
data/lib/d64.rb ADDED
@@ -0,0 +1,2 @@
1
+ module D64
2
+ end
data/lib/d64/block.rb ADDED
@@ -0,0 +1,3 @@
1
+ module D64
2
+ Block = Struct.new(:track, :sector)
3
+ end
@@ -0,0 +1,73 @@
1
+ require "d64/block"
2
+ require "d64/sector"
3
+
4
+ module D64
5
+ class BlockMap < Sector
6
+
7
+ def dump_map
8
+ puts (1..35).map { |tn|
9
+ ("%2s " % tn) << free_on_track(tn).map { |v| v ? '[ ]' : '[x]' }.join
10
+ }.join "\n";
11
+ end
12
+
13
+ def free_on_track(tn)
14
+ tv = track_value(tn)
15
+ Image.sectors_per_track(tn).times.map do |sn|
16
+ tv & (2 ** sn) != 0
17
+ end
18
+ end
19
+
20
+ def free_blocks
21
+ 35.times.map { |i| bytes[4 * i + 4] }.reduce :+
22
+ end
23
+
24
+ def allocate(opts = {})
25
+ if opts[:block]
26
+ tn = opts[:block].track
27
+ sn = opts[:block].sector
28
+ free_on_track(tn)[sn] or
29
+ fail "Can't allocate used sector [%s %02d:%02d]." % [@image.name, tn, sn]
30
+ else
31
+ tracks = (1..35).to_a
32
+ if tn = opts[:trackpref]
33
+ tracks.delete opts[:trackpref]
34
+ tracks.unshift opts[:trackpref]
35
+ end
36
+ sn = nil
37
+ tracks.each do |tn|
38
+ sn = free_on_track(tn).index(true)
39
+ break if sn
40
+ end
41
+ return nil unless sn
42
+ end
43
+ mark_as_used tn, sn
44
+ free_on_track(tn)[sn] and
45
+ fail "Failed to mark as used!"
46
+ puts 'Allocated sector [%s %02d:%02d]' % [@image.name, tn, sn] if ENV['DEBUG']
47
+ Block.new(tn, sn)
48
+ end
49
+
50
+ def mark_as_used(tn, sn)
51
+ byte, bit = sn.divmod(8)
52
+ @bytes[4 * tn + 1 + byte] &= (255 - 2 ** bit)
53
+ @bytes[4 * tn] -= 1
54
+ end
55
+
56
+ def mark_as_unused(tn, sn)
57
+ byte, bit = sn.divmod(8)
58
+ @bytes[4 * tn + 1 + byte] |= (2 ** bit)
59
+ @bytes[4 * tn] += 1
60
+ end
61
+
62
+ def commit
63
+ image.commit_bam
64
+ end
65
+
66
+ private
67
+
68
+ def track_value(tn)
69
+ a, b, c = bytes[4 * tn + 1, 3]
70
+ (c << 16) + (b << 8) + a
71
+ end
72
+ end
73
+ end
data/lib/d64/image.rb ADDED
@@ -0,0 +1,90 @@
1
+ require "d64/block"
2
+ require "d64/sector"
3
+ require "d64/block_map"
4
+
5
+ module D64
6
+ # http://vice-emu.sourceforge.net/vice_15.html#SEC278
7
+
8
+ class Image
9
+ attr_reader :num_tracks, :filename
10
+ attr_accessor :name
11
+
12
+ def self.read(filename)
13
+ di = new
14
+ di.read(filename)
15
+ di
16
+ end
17
+
18
+ def self.offset(block)
19
+ (@offsets ||= build_offset_table)[block.track - 1][block.sector]
20
+ end
21
+
22
+ def self.build_offset_table
23
+ ss = -1
24
+ 40.times.map do |i|
25
+ tn = i + 1
26
+ sectors_per_track(tn).times.map do
27
+ ss += 1
28
+ ss * 256
29
+ end
30
+ end
31
+ end
32
+
33
+ def self.sectors_per_track(track)
34
+ @spt ||= [0] + [21] * 17 + [19] * 7 + [18] * 6 + [17] * 10
35
+ @spt[track]
36
+ end
37
+
38
+ def initialize
39
+ @num_tracks = 35
40
+ @name = ''
41
+ end
42
+
43
+ def to_s
44
+ "<D64::Image:#{object_id.to_s(16)} @filename=#{filename}>"
45
+ end
46
+
47
+ def read(file)
48
+ @filename = file
49
+ @image = File.binread(filename).bytes
50
+ end
51
+
52
+ def write(file = filename)
53
+ File.open(file, 'wb') { |f| f.write @image.pack('C*') }
54
+ end
55
+
56
+ def sector(block)
57
+ block = Block.new(block[0], block[1]) if block.is_a?(Array)
58
+ Sector.new(self, block, sector_data(block))
59
+ end
60
+
61
+ def bam
62
+ block = Block.new(18, 0)
63
+ @bam ||= BlockMap.new(self, block, sector_data(block))
64
+ end
65
+
66
+ def directory_chain
67
+ sector([18, 0]).chain
68
+ end
69
+
70
+ def allocate_sector(opts = {})
71
+ block = bam.allocate(opts) or
72
+ fail "Failed to allocate sector!"
73
+ Sector.new(self, block, [0] * 256)
74
+ end
75
+
76
+ def commit_sector(sector)
77
+ @image[Image.offset(sector.block), 256] = sector.bytes
78
+ end
79
+
80
+ def commit_bam
81
+ @image[Image.offset(@bam.block) + 4, 140] = @bam.bytes[4, 140]
82
+ end
83
+
84
+ private
85
+
86
+ def sector_data(block)
87
+ @image[Image.offset(block), 256]
88
+ end
89
+ end
90
+ end
data/lib/d64/sector.rb ADDED
@@ -0,0 +1,78 @@
1
+ module D64
2
+ class Sector
3
+ attr_reader :image, :block, :bytes
4
+
5
+ def initialize(image, block, bytes)
6
+ @image = image
7
+ @block = block
8
+ @bytes = bytes
9
+ end
10
+
11
+ def to_s
12
+ "<D64::Sector:#{object_id.to_s(16)} t=#{@block.track} s=#{@block.sector}>"
13
+ end
14
+
15
+ def pos
16
+ '[%02d:%02d]' % [block.track, block.sector]
17
+ end
18
+
19
+ def name_pos
20
+ '[%s %02d:%02d]' % [image.name, block.track, block.sector]
21
+ end
22
+
23
+ def next
24
+ tn, sn = @bytes[0, 2]
25
+ image.sector([tn, sn]) if tn > 0
26
+ end
27
+
28
+ def chain
29
+ list = [self]
30
+ while list.last.next
31
+ list << list.last.next
32
+ end
33
+ list
34
+ end
35
+
36
+ def copy_content_from(other, opts = {})
37
+ puts "Copying content from #{other.name_pos} to #{name_pos}." if ENV['DEBUG']
38
+ first = (opts[:include_link] ? 0 : 2)
39
+ if block.track == 18 && block.sector == 0 && !opts[:include_bam]
40
+ @bytes[first..3] = other.bytes[first..3]
41
+ @bytes[0x90..-1] = other.bytes[0x90..-1]
42
+ else
43
+ @bytes[first..-1] = other.bytes[first..-1]
44
+ end
45
+ commit
46
+ end
47
+
48
+ def link_to(other, opts = {})
49
+ puts "Linking #{name_pos} to #{other.name_pos}." if ENV['DEBUG']
50
+ @bytes[0, 2] = [other.block.track, other.block.sector]
51
+ commit
52
+ end
53
+
54
+ def commit
55
+ image.commit_sector(self)
56
+ end
57
+
58
+ def dump
59
+ puts 8.times.map { |i|
60
+ ('%05X %s ' % [Image.offset(block) + 32 * i, pos]) <<
61
+ (' %02X' * 32 % @bytes[32 * i, 32]) <<
62
+ (' %32s' % bytes_string(@bytes[32 * i, 32]))
63
+ }.join("\n")
64
+ end
65
+
66
+ private
67
+
68
+ def bytes_string(bytes)
69
+ bytes.map { |b|
70
+ case b
71
+ when 0x20..0x7F then b.chr
72
+ else
73
+ '.'
74
+ end
75
+ }.join
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module D64
2
+ VERSION = "0.1.0"
3
+ end
data/lib/d64tools.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "d64"
2
+ require "d64/version"
3
+ require "d64/image"
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: d64tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lars Haugseth
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Tools to perform various operations on D64 floppy images.
42
+ email:
43
+ - github@larshaugseth.com
44
+ executables:
45
+ - d64hide
46
+ - d64reserve
47
+ - d64unreserve
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".gitignore"
52
+ - Gemfile
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - d64tools.gemspec
59
+ - exe/d64hide
60
+ - exe/d64reserve
61
+ - exe/d64unreserve
62
+ - lib/d64.rb
63
+ - lib/d64/block.rb
64
+ - lib/d64/block_map.rb
65
+ - lib/d64/image.rb
66
+ - lib/d64/sector.rb
67
+ - lib/d64/version.rb
68
+ - lib/d64tools.rb
69
+ homepage: http://github.com/lhz/d64tools
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.4.5
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: D64 floppy image tools
93
+ test_files: []
94
+ has_rdoc: