From 75873ea18ec08a4bfcd13caa12cbb92f7afe0fd6 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 25 Apr 2014 15:07:47 +0300 Subject: [PATCH] cleaned intruction initialization and fixed a test accidentally (was schoddy code) --- lib/asm/arm_assembler.rb | 22 ++++++++++------------ lib/asm/arm_machine.rb | 4 ++-- lib/asm/call_instruction.rb | 8 ++------ lib/asm/instruction.rb | 20 +++++--------------- lib/asm/logic_instruction.rb | 20 ++++++++++---------- lib/asm/memory_instruction.rb | 4 ++-- lib/asm/stack_instruction.rb | 6 +++--- 7 files changed, 34 insertions(+), 50 deletions(-) diff --git a/lib/asm/arm_assembler.rb b/lib/asm/arm_assembler.rb index 6bde6312..0b495140 100644 --- a/lib/asm/arm_assembler.rb +++ b/lib/asm/arm_assembler.rb @@ -23,8 +23,7 @@ module Asm end attr_reader :codes , :position - def instruction(clazz,name, *args) - opcode = name.to_s + def instruction(clazz, opcode , condition_code , update_status , *args) arg_nodes = [] args.each do |arg| if (arg.is_a?(Asm::Register)) @@ -39,24 +38,23 @@ module Asm raise "Invalid argument #{arg.inspect} for instruction" end end - add_code clazz.new(opcode , arg_nodes) + add_code clazz.new(opcode , condition_code , update_status , arg_nodes) end def self.define_instruction(inst , clazz ) define_method(inst) do |*args| - instruction clazz , inst , *args + instruction clazz , inst , :al , 0 , *args end - define_method(inst.to_s+'s') do |*args| - instruction clazz , inst.to_s+'s' , *args + define_method("#{inst}s") do |*args| + instruction clazz , inst , :al , 1 , *args end - ArmMachine::COND_CODES.keys.each do |cond_suffix| - suffix = cond_suffix.to_s - define_method(inst.to_s + suffix) do |*args| - instruction clazz , inst + suffix , *args + ArmMachine::COND_CODES.keys.each do |suffix| + define_method("#{inst}#{suffix}") do |*args| + instruction clazz , inst , suffix , 0 , *args end - define_method(inst.to_s + 's'+ suffix) do |*args| - instruction clazz , inst.to_s + 's' + suffix, *args + define_method("#{inst}s#{suffix}") do |*args| + instruction clazz , inst , suffix , 1 , *args end end end diff --git a/lib/asm/arm_machine.rb b/lib/asm/arm_machine.rb index 1298c88f..34ebfffe 100644 --- a/lib/asm/arm_machine.rb +++ b/lib/asm/arm_machine.rb @@ -39,9 +39,9 @@ module Asm :ge => 0b1010, :gt => 0b1100, :vs => 0b0110 } - #return the bit pattern for the @cond variable, which signals the conditional code + #return the bit pattern for the @condition_code variable, which signals the conditional code def cond_bit_code - COND_CODES[@cond] or throw "no code found for #{@cond}" + COND_CODES[@condition_code] or throw "no code found for #{@condition_code}" end REGISTERS = { 'r0' => 0, 'r1' => 1, 'r2' => 2, 'r3' => 3, 'r4' => 4, 'r5' => 5, diff --git a/lib/asm/call_instruction.rb b/lib/asm/call_instruction.rb index 5c2e1de8..7547a69a 100644 --- a/lib/asm/call_instruction.rb +++ b/lib/asm/call_instruction.rb @@ -12,10 +12,6 @@ module Asm # Registers 0-6 hold the call values as for a normal c call class CallInstruction < Instruction - - def initialize(opcode , args) - super(opcode,args) - end def assemble(io) case opcode @@ -34,13 +30,13 @@ module Asm else raise "else not coded #{arg.inspect}" end - io.write_uint8 OPCODES[opcode] | (COND_CODES[@cond] << 4) + io.write_uint8 OPCODES[opcode] | (COND_CODES[@condition_code] << 4) when :swi arg = args[0] if (arg.is_a?(Asm::NumLiteral)) packed = [arg.value].pack('L')[0,3] io << packed - io.write_uint8 0b1111 | (COND_CODES[@cond] << 4) + io.write_uint8 0b1111 | (COND_CODES[@condition_code] << 4) else raise Asm::AssemblyError.new("invalid operand argument expected literal not #{arg}") end diff --git a/lib/asm/instruction.rb b/lib/asm/instruction.rb index cab56ff4..07fd2dd3 100644 --- a/lib/asm/instruction.rb +++ b/lib/asm/instruction.rb @@ -17,20 +17,10 @@ module Asm COND_POSTFIXES = Regexp.union( COND_CODES.keys.collect{|k|k.to_s} ).source - def initialize(opcode , args) - opcode = opcode.to_s.downcase - @cond = :al - if (opcode =~ /(#{COND_POSTFIXES})$/) - @cond = $1.to_sym - opcode = opcode[0..-3] - end unless opcode == 'teq' - if (opcode =~ /s$/) - @update_status_flag= 1 - opcode = opcode[0..-2] - else - @update_status_flag= 0 - end - @opcode = opcode.downcase.to_sym + def initialize(opcode , condition_code , update_status , args) + @update_status_flag = update_status + @condition_code = condition_code.to_sym + @opcode = opcode @args = args @operand = 0 end @@ -38,7 +28,7 @@ module Asm attr_reader :opcode, :args # Many arm instructions may be conditional, where the default condition is always (al) # ArmMachine::COND_CODES names them, and this attribute reflects it - attr_reader :cond + attr_reader :condition_code attr_reader :operand # Logic instructions may be executed with or without affecting the status register diff --git a/lib/asm/logic_instruction.rb b/lib/asm/logic_instruction.rb index 790a8d06..2a89828b 100644 --- a/lib/asm/logic_instruction.rb +++ b/lib/asm/logic_instruction.rb @@ -4,10 +4,10 @@ module Asm class LogicInstruction < Instruction - def initialize( opcode , args) - super(opcode , args) + def initialize(opcode , condition_code , update_status , args) + super(opcode , condition_code , update_status , args) @rn = nil - @i = 0 + @i = 0 @rd = args[0] end attr_accessor :i, :rn, :rd @@ -19,7 +19,7 @@ module Asm end #(stays in subclases, while build is overriden to provide different arguments) - def do_build(arg) + def do_build(arg) if arg.is_a?(Asm::StringLiteral) # do pc relative addressing with the difference to the instuction # 8 is for the funny pipeline adjustment (ie oc pointing to fetch and not execute) @@ -72,8 +72,8 @@ module Asm build instuction_class = 0b00 # OPC_DATA_PROCESSING val = operand.is_a?(Register) ? operand.bits : operand - val |= (rd.bits << 12) - val |= (rn.bits << 12+4) + val |= (rd.bits << 12) + val |= (rn.bits << 12+4) val |= (update_status_flag << 12+4+4)#20 val |= (op_bit_code << 12+4+4 +1) val |= (i << 12+4+4 +1+4) @@ -83,8 +83,8 @@ module Asm end end class CompareInstruction < LogicInstruction - def initialize( opcode , args) - super(opcode , args) + def initialize(opcode , condition_code , update_status , args) + super(opcode , condition_code , update_status , args) @update_status_flag = 1 @rn = args[0] @rd = reg "r0" @@ -94,8 +94,8 @@ module Asm end end class MoveInstruction < LogicInstruction - def initialize( opcode , args) - super(opcode , args) + def initialize(opcode , condition_code , update_status , args) + super(opcode , condition_code , update_status , args) @rn = reg "r0" # register zero = zero bit pattern end def build diff --git a/lib/asm/memory_instruction.rb b/lib/asm/memory_instruction.rb index 884b065e..d606ab45 100644 --- a/lib/asm/memory_instruction.rb +++ b/lib/asm/memory_instruction.rb @@ -5,8 +5,8 @@ module Asm # Implemented: immediate offset with offset=0 class MemoryInstruction < Instruction - def initialize(opcode , args) - super( opcode , args ) + def initialize(opcode , condition_code , update_status , args) + super(opcode , condition_code , update_status , args) @i = 0 #I flag (third bit) @pre_post_index = 0 #P flag @add_offset = 0 #U flag diff --git a/lib/asm/stack_instruction.rb b/lib/asm/stack_instruction.rb index 80a6c477..daf938d8 100644 --- a/lib/asm/stack_instruction.rb +++ b/lib/asm/stack_instruction.rb @@ -4,8 +4,8 @@ module Asm # ADDRESSING MODE 4 class StackInstruction < Instruction - def initialize(opcode , args) - super(opcode,args) + def initialize(opcode , condition_code , update_status , args) + super(opcode , condition_code , update_status , args) @update_status_flag= 0 @rn = reg "r0" # register zero = zero bit pattern # downward growing, decrement before memory access @@ -27,7 +27,7 @@ module Asm def assemble(io) build instuction_class = 0b10 # OPC_STACK - cond = @cond.is_a?(Symbol) ? COND_CODES[@cond] : @cond + cond = @condition_code.is_a?(Symbol) ? COND_CODES[@condition_code] : @condition_code rn = reg "sp" # sp register #assemble of old val = operand