1
0
mirror of https://github.com/safiire/n65.git synced 2024-12-12 00:29:03 +00:00
n65/lib/directives/incbin.rb
Safiire 2c938f7312 This is s a big rewrite including: A scoped symbol table, segment and
bank management, Use of promises to resolve symbols that are used before
they are defined.  A base class for all instructions and assembler
directives.  Hopefully my scoped symbols can be used to create C like
data structures in the zero page, ie sprite.x   New code to prodce the
final ROM.  Basically everything was rewritten.
2015-03-05 12:33:56 -08:00

52 lines
937 B
Ruby

require_relative '../instruction_base'
module Assembler6502
####
## This directive instruction can include a binary file
class IncBin < InstructionBase
#### Custom Exceptions
class FileNotFound < StandardError; end
####
## Try to parse an incbin directive
def self.parse(line)
match_data = line.match(/^\.incbin "([^"]+)"$/)
return nil if match_data.nil?
filename = match_data[1]
IncBin.new(filename)
end
####
## Initialize with filename
def initialize(filename)
@filename = filename
end
####
## Execute on the assembler
def exec(assembler)
unless File.exists?(@filename)
fail(FileNotFound, ".incbin can't find #{@filename}")
end
data = File.read(@filename).unpack('C*')
assembler.write_memory(data)
end
####
## Display
def to_s
".incbin \"#{@filename}\""
end
end
end