Simulation.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package Export;
  7. import API.ExportInterface;
  8. import API.Utils;
  9. import common.Block;
  10. import common.Code;
  11. import common.Instruction;
  12. import java.util.Map;
  13. import tools.mips.Memory;
  14. import tools.mips.MipsSettings;
  15. /**
  16. *
  17. * @author EUGENIO CARVALHO
  18. */
  19. public class Simulation implements ExportInterface {
  20. protected MipsSettings settings;
  21. public Simulation(tools.mips.MipsSettings settings) {
  22. this.settings = settings;
  23. }
  24. @Override
  25. public void Exec(Code IR, Code Target) throws Exception {
  26. // Integer index;
  27. // System.out.println(Target.GlobalContext());
  28. String bin;
  29. Memory memory = new Memory(
  30. settings.Get("memory.instruction"),
  31. settings.GetInt("memory.instruction.size")
  32. );
  33. memory.SetIO(new MemoryInitializationFile());
  34. for (Map.Entry<String, Block> x : Target.stmts.entrySet()) {
  35. for (Instruction instr : x.getValue().Instructions()) {
  36. bin = instr.Get("inst.bin");
  37. if (bin.equals("")) {
  38. continue;
  39. }
  40. // Divide os bytes da instrução binaria e escreve na memoria
  41. for (String sbyte : Utils.SplitEach(bin, 8)) {
  42. memory.WB(sbyte);
  43. }
  44. }
  45. }
  46. // Grava resutlado no arquivo de memoria do simulador
  47. memory.Save();
  48. // Instancia um simulador, executa e salva a memoria
  49. new tools.mips.MipsProcessor(settings)
  50. .Run()
  51. .Persist();
  52. }
  53. }