Class: Z80Disassembler::Disassembler

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

Constant Summary collapse

T_R =

0 1 2 3 4 5 6 7

[    'B',    'C',    'D',    'E',    'H',    'L', '(HL)',    'A'].freeze
T_CC =
[   'NZ',    'Z',   'NC',    'C',   'PO',   'PE',    'P',    'M'].freeze
T_ALU =
[  'ADD',  'ADC',  'SUB',  'SBC',  'AND',  'XOR',   'OR',   'CP'].freeze
T_ROT =
[  'RLC',  'RRC',   'RL',   'RR',  'SLA',  'SRA',  'SLL',  'SRL'].freeze
T_IM =
[    '0',  '0/1',    '1',    '2',    '0',  '0/1',    '1',    '2'].freeze
T_RP =
[   'BC',   'DE',   'HL',   'SP'].freeze
T_RP2 =
[   'BC',   'DE',   'HL',   'AF'].freeze
ASCII =
[
  ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
  '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\',']', '^', '_',
  '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, org = 32_768) ⇒ Disassembler

Returns a new instance of Disassembler.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/z80_disassembler.rb', line 28

def initialize(file, org = 32_768)
  @file = file; @org = org.to_i
  if file.original_filename[-3..-1] == '.$C'
    File.open(@file) do |f|
      z = f.read(17)
      @file_name   = "#{z[0..7]}.#{z[8]}"
      @org         = bytes_to_int(z[ 9..10])
      @file_size   = bytes_to_int(z[11..12])
      @sectors     = bytes_to_int(z[13..14])
      checksum1    = bytes_to_int(z[15..16])
      checksum2    = (z[0..14].sum * 257 + 105).to_s(16)[-4..-1].hex
      @code        = f.read(@file_size).bytes if checksum1 == checksum2
    end
  end
  @code ||= File.open(@file).read.bytes
  @file_size ||= @file.size
  @x = 0; @y = 0; @z = 0; @p = 0; @q = 0; @xx = nil
  @lambda = nil; @prefix = nil; @prev = nil; @result = []
end

Instance Attribute Details

#orgObject (readonly)

Returns the value of attribute org.



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

def org
  @org
end

Instance Method Details

#startObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/z80_disassembler.rb', line 48

def start
  addr = @org
  bytes = []; ascii = []
  @code.each do |byte|
    load_vars(byte)
    str = command_from_byte(byte)
    @prev = byte.to_s(16)
    ascii << ((32..126).include?(byte) ? ASCII[byte - 32] : ' ')
    bytes << @prev.rjust(2, '0').upcase
    next unless str

    @result << [addr, "##{addr.to_s(16)}".upcase, str, bytes.join(' '), ascii.join]
    addr += bytes.size
    bytes = []
    ascii = []
  end
  @result
end

#textObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/z80_disassembler.rb', line 67

def text
  hash_links = {}
  del_links = []
  link_num = 0
  int_addrs = @org..(@org + @file_size)
  with_links = @result.select { |z| z[2] =~ /#[0-F]{4}/ && int_addrs.include?(z[2].split('#').last[0..3].hex) }
  with_links.each do |x|
    z = "##{x[2].split('#').last[0..3]}"
    hash_links[z] = "link_#{link_num += 1}" unless hash_links[z]
  end

  code = @result.map do |addr, addr16, str, bytes, ascii|
    del_links << hash_links[addr16] if hash_links[addr16]
    link = (hash_links[addr16] || '').ljust(16, ' ')
    adr = '#' + str.split('#').last[0..3]
    string = hash_links.keys.include?(adr) ? str.sub(adr, hash_links[adr]) : str
    "#{link} #{string.ljust(16, ' ')}; #{addr16.ljust(5, ' ')} / #{addr.to_s.ljust(5, ' ')} ; #{bytes.ljust(14, ' ')} ; #{ascii.ljust(4, ' ')} ;"
  end.join("\n")

  [
    '                 device zxspectrum48',
    '                 ORG #' + @org.to_s(16),
    hash_links.map { |key, val| "#{val.ljust(16, ' ')} equ #{key}" unless del_links.include?(val) }.compact.join("\n"),
    'begin:',
    code,
    'end:',
    '        savesna "disasm.sna", begin',
    '        savebin "disasm.C", begin, end - begin',
    ''
  ].join("\n")
end