From 500851d246bdb5fa330e1c9c513f3e70f8bb32a5 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 27 Mar 2018 20:47:41 +0300 Subject: [PATCH] start on new binary creation process now writing into BinaryCode instead of stream also in the risc layer, not arm, for reusability --- lib/risc/binary_writer.rb | 34 ++++++++++++++++++++++++++++++++++ lib/risc/machine.rb | 10 ++++++++++ test/risc/test_assembler.rb | 3 ++- test/risc/test_machine.rb | 6 ++++-- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 lib/risc/binary_writer.rb 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