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

Class Method 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
47
48
49
50
51
52
53
54
55
56
57
# 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
  @hash_links = {}; @del_links = []
  @rmda = """
;--- #{Date.today} --- https://rmda.su
;    _______  _/| __ ______    ____
;   /  __   //  |/  \\\\   _  \\ /    \\
;  /   _/ _//        \\\\  \\\\  \\\\  \\  \\
;  \\___\\   \\\\___\\/___//______//__/\\__\\
;       \\__/
;--- size: #{@file_size}b #{"--- filename: #{@file_name}" if @file_name}

  """
end

Instance Attribute Details

#file_sizeObject (readonly)

Returns the value of attribute file_size.



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

def file_size
  @file_size
end

#orgObject (readonly)

Returns the value of attribute org.



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

def org
  @org
end

#rmdaObject (readonly)

Returns the value of attribute rmda.



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

def rmda
  @rmda
end

Class Method Details

.compile_text(file_name) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/z80_disassembler.rb', line 113

def self.compile_text(file_name)
  name = file_name.split('/').last
  [
    'length equ end - begin',
    'CODE      = #AF',
    'USR       = #C0',
    'LOAD      = #EF',
    'CLEAR     = #FD',
    'RANDOMIZE = #F9',
    '        org #5C00',
    'baszac  db 0,1',    # Line number
    '        dw linlen', # Line length
    'linzac  db CLEAR, "8", #0E, 0, 0',
    '        dw begin - 1',
    '        db 0, ":"',
    '        db LOAD, "\""',
    'codnam  ds 10, 32',
    '        org codnam',
    '        db "disasm"',
    '        org codnam + 10',
    '        db "\"", CODE, ":"',
    '        db RANDOMIZE, USR, "8", #0E, 0, 0',
    '        dw begin',  # call address
    '        db 0, #0D',
    'linlen = $ - linzac',
    'baslen = $ - baszac',
    "        emptytap '#{file_name}.tap'",
    "        savetap '#{file_name}.tap', BASIC, '#{name}', baszac, baslen, 1",
    "        savetap '#{file_name}.tap', CODE,  '#{name}', begin,  length, begin",
    "        savesna '#{file_name}.sna', begin",
    "        savebin '#{file_name}.bin', begin, length",
    "        savehob '#{file_name}.$C', '#{file_name}.bin', begin, length",
    ''
  ].join("\n")
end

Instance Method Details

#startObject



59
60
61
62
63
64
65
66
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/z80_disassembler.rb', line 59

def start
  result = []
  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

  link_num = 0
  int_addrs = @org..(@org + @file_size)
  result.select { |z| z[2] =~ /#[0-F]{4}/ && int_addrs.include?(z[2].split('#').last[0..3].hex) }.each do |x|
    z = "##{x[2].split('#').last[0..3]}"
    @hash_links[z] = "link_#{link_num += 1}" unless @hash_links[z]
  end
  result.select { |z| z[2] =~ /\$/ }.each do |x|
    z = "##{ (x[0] + x[2].split('$').last.to_i).to_s(16).upcase }"
    @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] ? (@hash_links[addr16] + ':') : '').ljust(16, ' ')
    substr, adr = if str.include?('$')
                    ["$#{str.split('$').last}", "##{(addr_ + str.split('$').last.to_i).to_s(16).upcase}"]
                  else
                    adr = "##{str.split('#').last[0..3]}"
                    [adr, adr]
                  end
    string = @hash_links.keys.include?(adr) ? str.sub(substr, @hash_links[adr]) : str

    defb = bytes_.split(" ").map { |x| "##{x}"}.join(",")
    "#{link} #{string.ljust(16, ' ')}; #{addr16.ljust(5, ' ')} / #{addr_.to_s.ljust(5, ' ')} ; #{ascii_.ljust(4, ' ')} ; #{defb}"
  end.join("\n")
  [
      @rmda,
      '                 device zxspectrum128',
      '                 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:',
      ''
  ].join("\n")
end