# The MIT License (MIT) # # Copyright (c) 2016 ITO SOFT DESIGN Inc. # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. module LadderDrive class Asm attr_reader :codes attr_reader :endian LITTLE_ENDIAN = 0 BIG_ENDIAN = 1 def initialize source, endian = nil @endian = endian || BIG_ENDIAN @lines = [] line_no = 1 address = 0 source.each_line do |line| begin @lines << AsmLine.new(line, address, @endian) address = @lines.last.next_address line_no += 1 rescue SyntaxError => e puts "#{e.class}: line:#{line_no}; #{line.chomp}; #{e.to_s} " throw end end end def dump @codes.map do |c| c.to_s(16).rjust(2, "0") end end def dump_line @lines.map do |line| "#{line.address.to_s(16).rjust(4, "0")} #{line.dump_line}" end .join("\n") << "\n" end def codes @lines.map do |line| line.codes end.flatten end private def parse line @lines << AsmLine.new(line) end end class AsmLine attr_reader :line attr_reader :codes attr_reader :address attr_reader :endian def initialize line, address = 0, endian = nil @endian = endian || Asm::LITTLE_ENDIAN @line = line.upcase.chomp @codes = [] @address = address parse end def dump_line "#{dump}\t#{line}" end def dump @codes.map do |c| c.to_s(16).rjust(2, "0") end .join(" ") .ljust(12) end def next_address address + codes.size end private OPERAND_TYPE_NONE = 0 OPERAND_TYPE_TYPE_AND_NUMBER = 1 OPERAND_TYPE_TYPE_AND_NUMBER_NUMBER = 2 def parse /([^#]*)/ =~ line a = $1.split(/\s+/) mnemonic, operand1, operand2 = a if mnemonic @codes << encode_mnemonic(mnemonic) case operand_type(mnemonic) when OPERAND_TYPE_TYPE_AND_NUMBER @codes += parse_type_and_number(mnemonic, operand1, operand2) end end end def operand_type mnemonic case mnemonic when /LD/, /AND/, /ANI/, /OR[^B]?$/, /OUT/, "SET", "RST", "PLS", "PLF", "FF", /SF(L|R)/ OPERAND_TYPE_TYPE_AND_NUMBER else OPERAND_TYPE_NONE end end MNEMONIC_DEF = <> 2 when 0, 1 type_code |= 0x80 else type_code |= 0xa0 end if number < 256 codes = [type_code, number] else case endian when Asm::LITTLE_ENDIAN codes = [type_code | 0x10, number & 0xff, (number & 0xff00) >> 8] when Asm::BIG_ENDIAN codes = [type_code | 0x10, (number & 0xff00) >> 8, number & 0xff] end end # If mnemonic is out/outi and device type is timer or counter, append time or count. case mnemonic when /OUT/ case suffix when 'T' codes += time_value(value) when 'C' codes += counter_value(value) end end codes end def time_value value raise SyntaxError.new "It must be required time value." unless /^\d*\.?\d*$/ =~ value t = value.to_f g = 0 codes = [] while t < 16384 && g <= 3 t *= 10.0 g += 1 end t /= 10.0 g -= 1 v = (g << 14) | t.to_i case endian when Asm::LITTLE_ENDIAN codes = [v & 0xff, (v & 0xff00) >> 8] when Asm::BIG_ENDIAN codes = [(v & 0xff00) >> 8, v & 0xff] end codes #rdlsf@de end def counter_value value raise SyntaxError.new "It must be required count value." unless /^\d+$/ =~ value v = value.to_i case endian when Asm::LITTLE_ENDIAN codes = [v & 0xff, (v & 0xff00) >> 8] when Asm::BIG_ENDIAN codes = [(v & 0xff00) >> 8, v & 0xff] end end end end if $0 == __FILE__ asm = LadderDrive::Asm.new ARGF puts asm.dump_line end