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 Method Summary collapse

Constructor Details

#initialize(file_name, addr = 32_768) ⇒ Disassembler

Returns a new instance of Disassembler.



27
28
29
30
31
32
# File 'lib/z80_disassembler.rb', line 27

def initialize(file_name, addr = 32_768)
  @file_name = file_name; @addr = addr.to_i
  @x = 0; @y = 0; @z = 0; @p = 0; @q = 0; @xx = nil
  @lambda = nil; @prefix = nil; @prev = nil
  @bytes = []; @ascii = []; @result = {}
end

Instance Method Details

#startObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/z80_disassembler.rb', line 34

def start
  File.open(@file_name).each_byte do |byte|
    load_vars(byte)
    str = case @prefix
          when 'cb' then @prefix = nil; cb_prefix
          when 'ed' then @prefix = nil; ed_prefix
          when 'dd' then @xx = 'IX'; xx_prefix(byte)
          when 'fd' then @xx = 'IY'; xx_prefix(byte)
          when 'xx' then temp = @temp; @temp = nil; displacement(byte, temp)
          when 2    then @prefix -= 1; @temp = byte.to_s(16).rjust(2, '0').upcase; nil
          when 1
            resp = @lambda.call(@arg, byte.to_s(16).rjust(2, '0').upcase)
            @prefix = nil; temp = @temp; @temp = nil
            if temp && resp.include?(')')
              resp = @xx ? displacement(temp.hex, resp) : resp.sub(')', "#{temp})").sub('(', '(#')
            elsif temp
              resp += temp
            end
            resp = hl_to_xx(resp, @xx) unless @xx.nil?
            @xx = nil
            resp
          else command
          end
    @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