cleaned intruction initialization and fixed a test accidentally (was schoddy code)

This commit is contained in:
Torsten Ruger 2014-04-25 15:07:47 +03:00
parent eca9e66f73
commit 75873ea18e
7 changed files with 34 additions and 50 deletions

View File

@ -23,8 +23,7 @@ module Asm
end end
attr_reader :codes , :position attr_reader :codes , :position
def instruction(clazz,name, *args) def instruction(clazz, opcode , condition_code , update_status , *args)
opcode = name.to_s
arg_nodes = [] arg_nodes = []
args.each do |arg| args.each do |arg|
if (arg.is_a?(Asm::Register)) if (arg.is_a?(Asm::Register))
@ -39,24 +38,23 @@ module Asm
raise "Invalid argument #{arg.inspect} for instruction" raise "Invalid argument #{arg.inspect} for instruction"
end end
end end
add_code clazz.new(opcode , arg_nodes) add_code clazz.new(opcode , condition_code , update_status , arg_nodes)
end end
def self.define_instruction(inst , clazz ) def self.define_instruction(inst , clazz )
define_method(inst) do |*args| define_method(inst) do |*args|
instruction clazz , inst , *args instruction clazz , inst , :al , 0 , *args
end end
define_method(inst.to_s+'s') do |*args| define_method("#{inst}s") do |*args|
instruction clazz , inst.to_s+'s' , *args instruction clazz , inst , :al , 1 , *args
end end
ArmMachine::COND_CODES.keys.each do |cond_suffix| ArmMachine::COND_CODES.keys.each do |suffix|
suffix = cond_suffix.to_s define_method("#{inst}#{suffix}") do |*args|
define_method(inst.to_s + suffix) do |*args| instruction clazz , inst , suffix , 0 , *args
instruction clazz , inst + suffix , *args
end end
define_method(inst.to_s + 's'+ suffix) do |*args| define_method("#{inst}s#{suffix}") do |*args|
instruction clazz , inst.to_s + 's' + suffix, *args instruction clazz , inst , suffix , 1 , *args
end end
end end
end end

View File

@ -39,9 +39,9 @@ module Asm
:ge => 0b1010, :gt => 0b1100, :ge => 0b1010, :gt => 0b1100,
:vs => 0b0110 :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 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 end
REGISTERS = { 'r0' => 0, 'r1' => 1, 'r2' => 2, 'r3' => 3, 'r4' => 4, 'r5' => 5, REGISTERS = { 'r0' => 0, 'r1' => 1, 'r2' => 2, 'r3' => 3, 'r4' => 4, 'r5' => 5,

View File

@ -13,10 +13,6 @@ module Asm
class CallInstruction < Instruction class CallInstruction < Instruction
def initialize(opcode , args)
super(opcode,args)
end
def assemble(io) def assemble(io)
case opcode case opcode
when :b, :bl when :b, :bl
@ -34,13 +30,13 @@ module Asm
else else
raise "else not coded #{arg.inspect}" raise "else not coded #{arg.inspect}"
end end
io.write_uint8 OPCODES[opcode] | (COND_CODES[@cond] << 4) io.write_uint8 OPCODES[opcode] | (COND_CODES[@condition_code] << 4)
when :swi when :swi
arg = args[0] arg = args[0]
if (arg.is_a?(Asm::NumLiteral)) if (arg.is_a?(Asm::NumLiteral))
packed = [arg.value].pack('L')[0,3] packed = [arg.value].pack('L')[0,3]
io << packed io << packed
io.write_uint8 0b1111 | (COND_CODES[@cond] << 4) io.write_uint8 0b1111 | (COND_CODES[@condition_code] << 4)
else else
raise Asm::AssemblyError.new("invalid operand argument expected literal not #{arg}") raise Asm::AssemblyError.new("invalid operand argument expected literal not #{arg}")
end end

View File

@ -17,20 +17,10 @@ module Asm
COND_POSTFIXES = Regexp.union( COND_CODES.keys.collect{|k|k.to_s} ).source COND_POSTFIXES = Regexp.union( COND_CODES.keys.collect{|k|k.to_s} ).source
def initialize(opcode , args) def initialize(opcode , condition_code , update_status , args)
opcode = opcode.to_s.downcase @update_status_flag = update_status
@cond = :al @condition_code = condition_code.to_sym
if (opcode =~ /(#{COND_POSTFIXES})$/) @opcode = opcode
@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
@args = args @args = args
@operand = 0 @operand = 0
end end
@ -38,7 +28,7 @@ module Asm
attr_reader :opcode, :args attr_reader :opcode, :args
# Many arm instructions may be conditional, where the default condition is always (al) # Many arm instructions may be conditional, where the default condition is always (al)
# ArmMachine::COND_CODES names them, and this attribute reflects it # ArmMachine::COND_CODES names them, and this attribute reflects it
attr_reader :cond attr_reader :condition_code
attr_reader :operand attr_reader :operand
# Logic instructions may be executed with or without affecting the status register # Logic instructions may be executed with or without affecting the status register

View File

@ -4,8 +4,8 @@ module Asm
class LogicInstruction < Instruction class LogicInstruction < Instruction
def initialize( opcode , args) def initialize(opcode , condition_code , update_status , args)
super(opcode , args) super(opcode , condition_code , update_status , args)
@rn = nil @rn = nil
@i = 0 @i = 0
@rd = args[0] @rd = args[0]
@ -83,8 +83,8 @@ module Asm
end end
end end
class CompareInstruction < LogicInstruction class CompareInstruction < LogicInstruction
def initialize( opcode , args) def initialize(opcode , condition_code , update_status , args)
super(opcode , args) super(opcode , condition_code , update_status , args)
@update_status_flag = 1 @update_status_flag = 1
@rn = args[0] @rn = args[0]
@rd = reg "r0" @rd = reg "r0"
@ -94,8 +94,8 @@ module Asm
end end
end end
class MoveInstruction < LogicInstruction class MoveInstruction < LogicInstruction
def initialize( opcode , args) def initialize(opcode , condition_code , update_status , args)
super(opcode , args) super(opcode , condition_code , update_status , args)
@rn = reg "r0" # register zero = zero bit pattern @rn = reg "r0" # register zero = zero bit pattern
end end
def build def build

View File

@ -5,8 +5,8 @@ module Asm
# Implemented: immediate offset with offset=0 # Implemented: immediate offset with offset=0
class MemoryInstruction < Instruction class MemoryInstruction < Instruction
def initialize(opcode , args) def initialize(opcode , condition_code , update_status , args)
super( opcode , args ) super(opcode , condition_code , update_status , args)
@i = 0 #I flag (third bit) @i = 0 #I flag (third bit)
@pre_post_index = 0 #P flag @pre_post_index = 0 #P flag
@add_offset = 0 #U flag @add_offset = 0 #U flag

View File

@ -4,8 +4,8 @@ module Asm
# ADDRESSING MODE 4 # ADDRESSING MODE 4
class StackInstruction < Instruction class StackInstruction < Instruction
def initialize(opcode , args) def initialize(opcode , condition_code , update_status , args)
super(opcode,args) super(opcode , condition_code , update_status , args)
@update_status_flag= 0 @update_status_flag= 0
@rn = reg "r0" # register zero = zero bit pattern @rn = reg "r0" # register zero = zero bit pattern
# downward growing, decrement before memory access # downward growing, decrement before memory access
@ -27,7 +27,7 @@ module Asm
def assemble(io) def assemble(io)
build build
instuction_class = 0b10 # OPC_STACK 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 rn = reg "sp" # sp register
#assemble of old #assemble of old
val = operand val = operand