ZX Spectrum tape file: Ruby parsing library

TAP files are used by emulators of ZX Spectrum computer (released in 1982 by Sinclair Research). TAP file stores blocks of data as if they are written to magnetic tape, which was used as primary media for ZX Spectrum. Contents of this file can be viewed as a very simple linear filesystem, storing named files with some basic metainformation prepended as a header.

File extension

tap

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of ZX Spectrum tape file using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for Ruby generated by Kaitai Struct depends on the Ruby runtime library. You have to install it before you can parse data.

The Ruby runtime library can be installed from RubyGems:

gem install kaitai-struct

Code

Parse a local file and get structure in memory:

data = ZxSpectrumTap.from_file("path/to/local/file.tap")

Or parse structure from a string of bytes:

bytes = "\x00\x01\x02..."
data = ZxSpectrumTap.new(Kaitai::Struct::Stream.new(bytes))

After that, one can get various attributes from the structure by invoking getter methods like:

data.blocks # => get blocks

Ruby source code to parse ZX Spectrum tape file

zx_spectrum_tap.rb

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

require 'kaitai/struct/struct'

unless Gem::Version.new(Kaitai::Struct::VERSION) >= Gem::Version.new('0.9')
  raise "Incompatible Kaitai Struct Ruby API: 0.9 or later is required, but you have #{Kaitai::Struct::VERSION}"
end


##
# TAP files are used by emulators of ZX Spectrum computer (released in
# 1982 by Sinclair Research). TAP file stores blocks of data as if
# they are written to magnetic tape, which was used as primary media
# for ZX Spectrum. Contents of this file can be viewed as a very
# simple linear filesystem, storing named files with some basic
# metainformation prepended as a header.
# @see https://sinclair.wiki.zxnet.co.uk/wiki/TAP_format Source
class ZxSpectrumTap < Kaitai::Struct::Struct

  FLAG_ENUM = {
    0 => :flag_enum_header,
    255 => :flag_enum_data,
  }
  I__FLAG_ENUM = FLAG_ENUM.invert

  HEADER_TYPE_ENUM = {
    0 => :header_type_enum_program,
    1 => :header_type_enum_num_array,
    2 => :header_type_enum_char_array,
    3 => :header_type_enum_bytes,
  }
  I__HEADER_TYPE_ENUM = HEADER_TYPE_ENUM.invert
  def initialize(_io, _parent = nil, _root = self)
    super(_io, _parent, _root)
    _read
  end

  def _read
    @blocks = []
    i = 0
    while not @_io.eof?
      @blocks << Block.new(@_io, self, @_root)
      i += 1
    end
    self
  end
  class Block < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @len_block = @_io.read_u2le
      @flag = Kaitai::Struct::Stream::resolve_enum(ZxSpectrumTap::FLAG_ENUM, @_io.read_u1)
      if  ((len_block == 19) && (flag == :flag_enum_header)) 
        @header = Header.new(@_io, self, @_root)
      end
      if len_block == 19
        @data = @_io.read_bytes((header.len_data + 4))
      end
      if flag == :flag_enum_data
        @headerless_data = @_io.read_bytes((len_block - 1))
      end
      self
    end
    attr_reader :len_block
    attr_reader :flag
    attr_reader :header
    attr_reader :data
    attr_reader :headerless_data
  end
  class ProgramParams < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @autostart_line = @_io.read_u2le
      @len_program = @_io.read_u2le
      self
    end
    attr_reader :autostart_line
    attr_reader :len_program
  end
  class BytesParams < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @start_address = @_io.read_u2le
      @reserved = @_io.read_bytes(2)
      self
    end
    attr_reader :start_address
    attr_reader :reserved
  end
  class Header < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @header_type = Kaitai::Struct::Stream::resolve_enum(ZxSpectrumTap::HEADER_TYPE_ENUM, @_io.read_u1)
      @filename = Kaitai::Struct::Stream::bytes_strip_right(@_io.read_bytes(10), 32)
      @len_data = @_io.read_u2le
      case header_type
      when :header_type_enum_program
        @params = ProgramParams.new(@_io, self, @_root)
      when :header_type_enum_num_array
        @params = ArrayParams.new(@_io, self, @_root)
      when :header_type_enum_char_array
        @params = ArrayParams.new(@_io, self, @_root)
      when :header_type_enum_bytes
        @params = BytesParams.new(@_io, self, @_root)
      end
      @checksum = @_io.read_u1
      self
    end
    attr_reader :header_type
    attr_reader :filename
    attr_reader :len_data
    attr_reader :params

    ##
    # Bitwise XOR of all bytes including the flag byte
    attr_reader :checksum
  end
  class ArrayParams < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @reserved = @_io.read_u1
      @var_name = @_io.read_u1
      @reserved1 = @_io.read_bytes(2)
      raise Kaitai::Struct::ValidationNotEqualError.new([0, 128].pack('C*'), reserved1, _io, "/types/array_params/seq/2") if not reserved1 == [0, 128].pack('C*')
      self
    end
    attr_reader :reserved

    ##
    # Variable name (1..26 meaning A$..Z$ +192)
    attr_reader :var_name
    attr_reader :reserved1
  end
  attr_reader :blocks
end