Class: N65::DW

Inherits:
InstructionBase show all
Defined in:
lib/n65/directives/dw.rb

Overview

This directive instruction can include a binary file

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InstructionBase

#unresolved_symbols?

Constructor Details

#initialize(value) ⇒ DW

Initialize with filename



33
34
35
# File 'lib/n65/directives/dw.rb', line 33

def initialize(value)
  @value = value
end

Class Method Details

.parse(line) ⇒ Object

Try to parse a dw directive



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/n65/directives/dw.rb', line 12

def self.parse(line)

  ##  Maybe it is a straight up bit of hex
  match_data = line.match(/^\.dw\s+\$([0-9A-F]{1,4})$/)
  unless match_data.nil?
    word = match_data[1].to_i(16)
    return DW.new(word)
  end

  ##  Or maybe it points to a symbol
  match_data = line.match(/^\.dw\s+([A-Za-z_][A-Za-z0-9_\.]+)/)
  unless match_data.nil?
    symbol = match_data[1]
    return DW.new(symbol)
  end
  nil
end

Instance Method Details

#exec(assembler) ⇒ Object

Execute on the assembler, now in this case value may

be a symbol that needs to be resolved, if so we return
a lambda which can be executed later, with the promise
that that symbol will have then be defined
This is a little complicated, I admit.


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
69
70
# File 'lib/n65/directives/dw.rb', line 44

def exec(assembler)

  promise = assembler.with_saved_state do |saved_assembler|
    value = saved_assembler.symbol_table.resolve_symbol(@value)
    bytes = [value & 0xFFFF].pack('S').bytes
    saved_assembler.write_memory(bytes)
  end


  ##  Try to execute it now, or setup the promise to return
  case @value
  when Fixnum
    bytes = [@value & 0xFFFF].pack('S').bytes
    assembler.write_memory(bytes)
  when String
    begin
      promise.call
    rescue SymbolTable::UndefinedSymbol
      ##  Must still advance PC before returning promise, so we'll write
      ##  a place holder value of 0xDEAD
      assembler.write_memory([0xDE, 0xAD])
      return promise
    end
  else
    fail("Uknown argument in .dw directive")
  end
end

#to_sObject

Display



75
76
77
78
79
80
81
82
# File 'lib/n65/directives/dw.rb', line 75

def to_s
  case @value
  when String
    ".dw #{@value}"
  when Fixnum
    ".dw $%4.X" % @value
  end
end