2020-08-30 18:55:55 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-31 09:40:12 +00:00
|
|
|
require_relative 'n65/version'
|
|
|
|
require_relative 'n65/symbol_table'
|
|
|
|
require_relative 'n65/memory_space'
|
|
|
|
require_relative 'n65/parser'
|
2015-02-18 03:05:37 +00:00
|
|
|
|
2015-03-29 17:19:19 +00:00
|
|
|
module N65
|
2015-02-18 11:05:18 +00:00
|
|
|
class Assembler
|
2015-03-04 17:58:50 +00:00
|
|
|
attr_reader :program_counter, :current_segment, :current_bank, :symbol_table, :virtual_memory, :promises
|
2015-02-18 03:05:37 +00:00
|
|
|
|
2015-03-04 17:58:50 +00:00
|
|
|
class AddressOutOfRange < StandardError; end
|
|
|
|
class InvalidSegment < StandardError; end
|
|
|
|
class WriteOutOfBounds < StandardError; end
|
|
|
|
class INESHeaderAlreadySet < StandardError; end
|
|
|
|
class FileNotFound < StandardError; end
|
2015-02-19 02:36:22 +00:00
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Assemble from an asm file to a nes ROM
|
2020-08-30 18:55:55 +00:00
|
|
|
# TODO: This reall needs a logger instead of all these unless quiet conditions
|
2017-09-26 17:11:12 +00:00
|
|
|
def self.from_file(infile, options)
|
2020-08-30 18:55:55 +00:00
|
|
|
raise(FileNotFound, infile) unless File.exist?(infile)
|
2015-03-04 17:58:50 +00:00
|
|
|
|
2020-08-30 18:55:55 +00:00
|
|
|
assembler = new
|
2015-03-04 17:58:50 +00:00
|
|
|
program = File.read(infile)
|
2017-09-26 17:11:12 +00:00
|
|
|
output_file = options[:output_file]
|
2015-03-04 17:58:50 +00:00
|
|
|
|
2017-09-26 17:11:12 +00:00
|
|
|
puts "Building #{infile}" unless options[:quiet]
|
2020-08-30 18:32:49 +00:00
|
|
|
# Process each line in the file
|
2015-03-31 09:40:12 +00:00
|
|
|
program.split(/\n/).each_with_index do |line, line_number|
|
|
|
|
begin
|
|
|
|
assembler.assemble_one_line(line)
|
|
|
|
rescue StandardError => e
|
2020-08-30 18:55:55 +00:00
|
|
|
warn("\n\n#{e.class}\n#{line}\n#{e}\nOn line #{line_number}")
|
2015-03-31 09:40:12 +00:00
|
|
|
exit(1)
|
|
|
|
end
|
2017-09-26 17:11:12 +00:00
|
|
|
print '.' unless options[:quiet]
|
2015-03-04 17:58:50 +00:00
|
|
|
end
|
2017-09-26 17:11:12 +00:00
|
|
|
puts unless options[:quiet]
|
2015-03-04 17:58:50 +00:00
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Second pass to resolve any missing symbols.
|
2020-08-30 18:55:55 +00:00
|
|
|
print 'Second pass, resolving symbols...' unless options[:quiet]
|
2015-03-04 17:58:50 +00:00
|
|
|
assembler.fulfill_promises
|
2020-08-30 18:55:55 +00:00
|
|
|
puts ' Done.' unless options[:quiet]
|
2015-02-18 11:05:18 +00:00
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Optionally write out a symbol map
|
2017-09-26 17:11:12 +00:00
|
|
|
if options[:write_symbol_table]
|
|
|
|
print "Writing symbol table to #{output_file}.yaml..." unless options[:quiet]
|
|
|
|
File.open("#{output_file}.yaml", 'w') do |fp|
|
|
|
|
fp.write(assembler.symbol_table.export_to_yaml)
|
|
|
|
end
|
2020-08-30 18:55:55 +00:00
|
|
|
puts 'Done.' unless options[:quiet]
|
2017-09-26 17:11:12 +00:00
|
|
|
end
|
2015-03-04 17:58:50 +00:00
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Optionally write out cycle count for subroutines
|
2017-09-27 21:58:34 +00:00
|
|
|
if options[:cycle_count]
|
|
|
|
print "Writing subroutine cycle counts to #{output_file}.cycles.yaml..." unless options[:quiet]
|
|
|
|
File.open("#{output_file}.cycles.yaml", 'w') do |fp|
|
|
|
|
fp.write(assembler.symbol_table.export_cycle_count_yaml)
|
|
|
|
end
|
2020-08-30 18:55:55 +00:00
|
|
|
puts 'Done.' unless options[:quiet]
|
2017-09-27 21:58:34 +00:00
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Emit the complete binary ROM
|
2017-09-26 17:11:12 +00:00
|
|
|
rom = assembler.emit_binary_rom
|
|
|
|
File.open(output_file, 'w') do |fp|
|
|
|
|
fp.write(rom)
|
|
|
|
end
|
|
|
|
|
2020-08-30 18:55:55 +00:00
|
|
|
return if options[:quiet]
|
|
|
|
|
|
|
|
rom_size = rom.size
|
|
|
|
rom_size_hex = format('%x', rom_size)
|
|
|
|
assembler.print_bank_usage
|
|
|
|
puts "Total size: $#{rom_size_hex}, #{rom_size} bytes"
|
2015-02-18 11:05:18 +00:00
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Initialize with a bank 1 of prog space for starters
|
2015-03-04 17:58:50 +00:00
|
|
|
def initialize
|
2015-02-18 11:05:18 +00:00
|
|
|
@ines_header = nil
|
2015-03-04 17:58:50 +00:00
|
|
|
@program_counter = 0x0
|
|
|
|
@current_segment = :prog
|
|
|
|
@current_bank = 0x0
|
|
|
|
@symbol_table = SymbolTable.new
|
|
|
|
@promises = []
|
|
|
|
@virtual_memory = {
|
2020-08-30 18:55:55 +00:00
|
|
|
prog: [MemorySpace.create_prog_rom],
|
|
|
|
char: []
|
2015-03-04 17:58:50 +00:00
|
|
|
}
|
2015-02-18 03:05:37 +00:00
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Return an object that contains the assembler's current state
|
2015-03-22 06:42:21 +00:00
|
|
|
def get_current_state
|
|
|
|
saved_program_counter, saved_segment, saved_bank = @program_counter, @current_segment, @current_bank
|
|
|
|
saved_scope = symbol_table.scope_stack.dup
|
2020-08-30 18:55:55 +00:00
|
|
|
OpenStruct.new(
|
|
|
|
program_counter: saved_program_counter,
|
|
|
|
segment: saved_segment,
|
|
|
|
bank: saved_bank,
|
|
|
|
scope: saved_scope
|
|
|
|
)
|
2015-03-22 06:42:21 +00:00
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Set the current state from an OpenStruct
|
2015-03-22 06:42:21 +00:00
|
|
|
def set_current_state(struct)
|
|
|
|
@program_counter, @current_segment, @current_bank = struct.program_counter, struct.segment, struct.bank
|
|
|
|
symbol_table.scope_stack = struct.scope.dup
|
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# This is the main assemble method, it parses one line into an object
|
|
|
|
# which when given a reference to this assembler, controls the assembler
|
|
|
|
# itself through public methods, executing assembler directives, and
|
|
|
|
# emitting bytes into our virtual memory spaces. Empty lines or lines
|
|
|
|
# with only comments parse to nil, and we just ignore them.
|
2015-03-04 17:58:50 +00:00
|
|
|
def assemble_one_line(line)
|
|
|
|
parsed_object = Parser.parse(line)
|
2020-08-30 18:55:55 +00:00
|
|
|
return if parsed_object.nil?
|
2015-03-04 17:58:50 +00:00
|
|
|
|
2020-08-30 18:55:55 +00:00
|
|
|
exec_result = parsed_object.exec(self)
|
2015-03-04 17:58:50 +00:00
|
|
|
|
2020-08-30 18:55:55 +00:00
|
|
|
# TODO: I could perhaps keep a tally of cycles used per top level scope here
|
|
|
|
if parsed_object.respond_to?(:cycles)
|
|
|
|
# puts "Line: #{line}"
|
|
|
|
# puts "Cycles #{parsed_object.cycles}"
|
|
|
|
# puts "Sym: #{@symbol_table.scope_stack}"
|
|
|
|
@symbol_table.add_cycles(parsed_object.cycles)
|
2015-02-25 00:43:50 +00:00
|
|
|
end
|
2020-08-30 18:55:55 +00:00
|
|
|
|
|
|
|
# If we have returned a promise save it for the second pass
|
|
|
|
@promises << exec_result if exec_result.is_a?(Proc)
|
2015-03-04 17:58:50 +00:00
|
|
|
end
|
2015-02-25 00:43:50 +00:00
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# This will empty out our promise queue and try to fullfil operations
|
|
|
|
# that required an undefined symbol when first encountered.
|
2015-03-04 17:58:50 +00:00
|
|
|
def fulfill_promises
|
2020-08-30 18:32:49 +00:00
|
|
|
while (promise = @promises.pop)
|
2015-03-04 17:58:50 +00:00
|
|
|
promise.call
|
2015-02-25 00:43:50 +00:00
|
|
|
end
|
2015-03-04 17:58:50 +00:00
|
|
|
end
|
2015-02-25 00:43:50 +00:00
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# This rewinds the state of the assembler, so a promise can be
|
|
|
|
# executed with a previous state, for example if we can't resolve
|
|
|
|
# a symbol right now, and want to try during the second pass
|
2015-03-22 06:42:21 +00:00
|
|
|
def with_saved_state(&block)
|
|
|
|
## Save the current state of the assembler
|
|
|
|
old_state = get_current_state
|
|
|
|
|
|
|
|
lambda do
|
2020-08-30 18:32:49 +00:00
|
|
|
# Set the assembler state back to the old state and run the block like that
|
2015-03-22 06:42:21 +00:00
|
|
|
set_current_state(old_state)
|
|
|
|
block.call(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Write to memory space. Typically, we are going to want to write
|
|
|
|
# to the location of the current PC, current segment, and current bank.
|
|
|
|
# Bounds check is inside MemorySpace#write
|
2015-03-04 17:58:50 +00:00
|
|
|
def write_memory(bytes, pc = @program_counter, segment = @current_segment, bank = @current_bank)
|
|
|
|
memory_space = get_virtual_memory_space(segment, bank)
|
|
|
|
memory_space.write(pc, bytes)
|
|
|
|
@program_counter += bytes.size
|
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Set the iNES header
|
2015-03-04 17:58:50 +00:00
|
|
|
def set_ines_header(ines_header)
|
2020-08-30 18:32:49 +00:00
|
|
|
raise(INESHeaderAlreadySet) unless @ines_header.nil?
|
|
|
|
|
2015-03-04 17:58:50 +00:00
|
|
|
@ines_header = ines_header
|
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Set the program counter
|
2015-03-04 17:58:50 +00:00
|
|
|
def program_counter=(address)
|
2020-08-30 18:32:49 +00:00
|
|
|
raise(AddressOutOfRange) unless address_within_range?(address)
|
|
|
|
|
2015-03-04 17:58:50 +00:00
|
|
|
@program_counter = address
|
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Set the current segment, prog or char.
|
2015-03-04 17:58:50 +00:00
|
|
|
def current_segment=(segment)
|
|
|
|
segment = segment.to_sym
|
2020-08-30 18:32:49 +00:00
|
|
|
raise(InvalidSegment, "#{segment} is not a valid segment. Try prog or char") unless valid_segment?(segment)
|
|
|
|
|
2015-03-04 17:58:50 +00:00
|
|
|
@current_segment = segment
|
2015-02-25 00:43:50 +00:00
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# Set the current bank, create it if it does not exist
|
2015-03-04 17:58:50 +00:00
|
|
|
def current_bank=(bank_number)
|
|
|
|
memory_space = get_virtual_memory_space(@current_segment, bank_number)
|
2020-08-30 18:55:55 +00:00
|
|
|
@virtual_memory[@current_segment][bank_number] = MemorySpace.create_bank(@current_segment) if memory_space.nil?
|
2015-03-04 17:58:50 +00:00
|
|
|
@current_bank = bank_number
|
|
|
|
end
|
|
|
|
|
|
|
|
def emit_binary_rom
|
|
|
|
progs = @virtual_memory[:prog]
|
|
|
|
chars = @virtual_memory[:char]
|
|
|
|
|
2015-04-12 04:03:13 +00:00
|
|
|
rom_size = 0x10
|
2020-08-30 19:48:37 +00:00
|
|
|
rom_size += MemorySpace::BANK_SIZES[:prog] * progs.size
|
|
|
|
rom_size += MemorySpace::BANK_SIZES[:char] * chars.size
|
2015-03-04 17:58:50 +00:00
|
|
|
|
|
|
|
rom = MemorySpace.new(rom_size, :rom)
|
|
|
|
|
|
|
|
offset = 0x0
|
|
|
|
offset += rom.write(0x0, @ines_header.emit_bytes)
|
|
|
|
|
|
|
|
progs.each do |prog|
|
2020-08-30 19:48:37 +00:00
|
|
|
offset += rom.write(offset, prog.read(0x8000, MemorySpace::BANK_SIZES[:prog]))
|
2015-02-18 03:05:37 +00:00
|
|
|
end
|
|
|
|
|
2015-03-04 17:58:50 +00:00
|
|
|
chars.each do |char|
|
2020-08-30 19:48:37 +00:00
|
|
|
offset += rom.write(offset, char.read(0x0, MemorySpace::BANK_SIZES[:char]))
|
2015-02-18 03:05:37 +00:00
|
|
|
end
|
2015-03-04 17:58:50 +00:00
|
|
|
rom.emit_bytes.pack('C*')
|
|
|
|
end
|
|
|
|
|
2020-08-30 18:32:49 +00:00
|
|
|
# TODO: Use StringIO to build output
|
2017-09-26 17:11:12 +00:00
|
|
|
def print_bank_usage
|
|
|
|
puts
|
2020-08-30 18:32:49 +00:00
|
|
|
puts 'ROM Structure {'
|
|
|
|
puts ' iNES 1.0 Header: $10 bytes'
|
2017-09-26 17:11:12 +00:00
|
|
|
|
|
|
|
@virtual_memory[:prog].each_with_index do |prog_rom, bank_number|
|
|
|
|
puts " PROG ROM bank #{bank_number}: #{prog_rom.usage_info}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@virtual_memory[:char].each_with_index do |char_rom, bank_number|
|
|
|
|
puts " CHAR ROM bank #{bank_number}: #{char_rom.usage_info}"
|
|
|
|
end
|
2020-08-30 18:32:49 +00:00
|
|
|
puts '}'
|
2017-09-26 17:11:12 +00:00
|
|
|
end
|
|
|
|
|
2015-03-04 17:58:50 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def get_virtual_memory_space(segment, bank_number)
|
|
|
|
@virtual_memory[segment][bank_number]
|
|
|
|
end
|
|
|
|
|
|
|
|
def address_within_range?(address)
|
|
|
|
address >= 0 && address < 2**16
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_segment?(segment)
|
2020-08-30 18:32:49 +00:00
|
|
|
%i[prog char].include?(segment)
|
2015-02-18 11:05:18 +00:00
|
|
|
end
|
2015-02-18 03:05:37 +00:00
|
|
|
end
|
|
|
|
end
|