(--
    'InsMemory.balsa'
     The Insmemory model for the SAMIPS architecture  with precise exceptions 
       
     June 2004
--)


import [type]

procedure InsMemory (input  CInsAdd  : InsAdd;
	             output CIns     : CIns; 
                     output start    : bit)
is variable s : String
   variable mem : BalsaMemory
   variable f : InsAndLine
   variable MemWord: word
   variable Add_R : word
   variable Col : Col

begin
  mem := BalsaMemoryNew ();
  f.f := FileOpen ("Dhrystoneall.txt", read);
  f.lineNo := 0;
  --print "Load InsMemory";
  loop while not FileEOF (f.f) then
      s := FileReadLine (f.f);
      s := "0x" + SubString(s, 10, 8);
       --print "Line ", f.lineNo, ": ", s ;
      mem := BalsaMemoryWrite({11, 32}, mem, f.lineNo, FromString(word, s, s));
       --Add_R := BalsaMemoryRead({11, 32}, mem, f.lineNo);
       --print "A: ",f.lineNo, " I: ", NumberToString (word, Add_R, 16, 0, 1);         
      f.lineNo := (f.lineNo + 1 as InsSize)  
  end;
  start <- 1;
  loop
     CInsAdd -> then Add_R := CInsAdd.a ||
                     Col   := CInsAdd.C
     end;

     if (Add_R and 2147483648) = 2147483648 then  -- 16#80000000
        -- Add_R := (Add_R - 2147476736 as word)    -- - 16#80000000 +16#1B00        
        -- Add_R := (Add_R - 2147481088 as word)    -- - 16#80000000 + 16#A00
           Add_R := (Add_R - 2147476224 as word)      -- - 16#80000000 +16#1D00
     else Add_R := (Add_R - 4194304 as word)      end;     -- 16#400000

        MemWord :=  BalsaMemoryRead({11, 32}, mem, (#(Add_R)[12..2] as InsSize)); 
        print "A: ",NumberToString (word, Add_R, 16, 0, 1), " I: ", NumberToString (word, MemWord, 16, 0, 1) ||
        CIns <- {MemWord, Col} 
     -- CIns <- {BalsaMemoryRead({11, 32}, mem, (#(Add_R)[12..2] as InsSize)), Col}
  end
end       
