diff --git a/lib/risc/binary_writer.rb b/lib/risc/binary_writer.rb new file mode 100644 index 00000000..07f78d2e --- /dev/null +++ b/lib/risc/binary_writer.rb @@ -0,0 +1,34 @@ +module Risc + # Little glue class to get the assembled binary from the Arm Instructions into + # the BinaryCode instances. Arm code originally uses io, but slightly modified + # really only uses write_unsigned_int_32 , which converts + # to an set_internal_word on the BinaryCode + # + # Machine instantiates and the writer reads from the arm Instructions + # and writes to the BinaryCode + class BinaryWriter + def initialize( code ) + @code = code + end + + # write into the given BinaryCode instance + def assemble( instruction ) + @index = 1 + while(instruction) + begin + instruction.assemble(self) + rescue LinkException + instruction.assemble(self) + end + instruction = instruction.next + puts "Next #{instruction.to_s}" + end + end + + def write_unsigned_int_32( bin ) + @code.set_word( @index , bin ) + @index += 1 + end + end + +end diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index 460efb8d..e6582ff6 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -1,4 +1,5 @@ require_relative "collector" +require_relative "binary_writer" module Risc # The Risc Machine is an abstraction of the register level. This is seperate from the @@ -93,6 +94,15 @@ module Risc at end + def create_binary + objects.each do |id , method| + next unless method.is_a? Parfait::TypedMethod + puts "CODE1 #{method.name}:#{}" + writer = BinaryWriter.new(method.binary) + writer.assemble(method.cpu_instructions) + end + end + def boot initialize @objects = nil diff --git a/test/risc/test_assembler.rb b/test/risc/test_assembler.rb index b7f27b7d..07c9b225 100644 --- a/test/risc/test_assembler.rb +++ b/test/risc/test_assembler.rb @@ -28,8 +28,9 @@ module Risc assert @assembler.assemble end def test_write_space - @assembler = Assembler.new(@machine , Collector.collect_space) assert @machine.translate_arm + assert @machine.position_all + @assembler = Assembler.new(@machine , Collector.collect_space) #assert @assembler.write_as_string end end diff --git a/test/risc/test_machine.rb b/test/risc/test_machine.rb index 404765bd..64f159f8 100644 --- a/test/risc/test_machine.rb +++ b/test/risc/test_machine.rb @@ -6,13 +6,11 @@ module Risc def setup @machine = Risc.machine.boot end - def test_objects objects = @machine.objects assert_equal Hash , objects.class assert 350 < objects.length end - def test_position_length @machine.position_all objects = @machine.objects @@ -25,5 +23,9 @@ module Risc assert Positioned.position(obj) end end + def test_binary + @machine.position_all + @machine.create_binary + end end end