MemoryInitializationFile.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.Utils;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.nio.file.Paths;
  11. import java.util.ArrayList;
  12. import java.util.Map;
  13. import java.util.Scanner;
  14. import java.util.TreeMap;
  15. import tools.mips.Memory;
  16. import tools.mips.MemoryImportExportInterface;
  17. /**
  18. *
  19. * @author Eugenio
  20. */
  21. public class MemoryInitializationFile implements MemoryImportExportInterface {
  22. public MemoryInitializationFile() {
  23. }
  24. @Override
  25. public void FromFile(Memory memory) throws Exception {
  26. String value;
  27. String[] parts;
  28. Scanner sc;
  29. String current = Paths.get(".").toAbsolutePath().normalize().toString();
  30. // Tenta carregar o arquivo
  31. boolean read = false;
  32. try {
  33. sc = new Scanner(new File(current + memory.File()));
  34. } catch (FileNotFoundException e) {
  35. throw new Exception(String.format("O arquivo da memoria não foi encontrado. %s", memory.File()));
  36. }
  37. while (sc.hasNextLine()) {
  38. value = sc.nextLine();
  39. if (value.equals("")) {
  40. continue;
  41. // Se encontra o fim do arquivo
  42. } else if (value.equals("END;")) {
  43. break;
  44. } else if (!read) {
  45. // Se ainda não iniciou a leitura
  46. // Busca o inicio das palavras
  47. if (value.equals("BEGIN")) {
  48. read = true;
  49. }
  50. continue;
  51. //Se for o inicio de um intervalo vazio de palavras
  52. } else if (value.charAt(0) == '[') {
  53. continue;
  54. }
  55. // trata e escreve o valor na memória
  56. parts = value.replaceAll("\\s+", "").split(":");
  57. memory.W(Long.parseLong(parts[0], 10), parts[1].split(";")[0], 4);
  58. }
  59. }
  60. @Override
  61. public String DumpFile(Memory memory) throws Exception {
  62. String out = Header(memory);
  63. Long address, base = 0L;
  64. ArrayList<String> buffer = new ArrayList<>();
  65. TreeMap<Long, String> data = memory.Data();
  66. Integer pad = data.lastKey().toString().length();
  67. for (Map.Entry<Long, String> o : data.entrySet()) {
  68. address = o.getKey();
  69. if (address % memory.WORD_BYTES_COUNT == 0) {
  70. out += Append(base, buffer, pad);
  71. if ((address - base) > memory.WORD_BYTES_COUNT) {
  72. out += String.format(
  73. "[%d..%d]:\t00000000000000000000000000000000;\n",
  74. base + memory.WORD_BYTES_COUNT,
  75. address - memory.WORD_BYTES_COUNT
  76. );
  77. }
  78. base = address;
  79. buffer = new ArrayList<>();
  80. }
  81. buffer.add(o.getValue());
  82. }
  83. out += Append(base, buffer, pad);
  84. out += Footer(memory);
  85. return out;
  86. }
  87. protected static String Append(Long base, ArrayList<String> buffer, Integer pad) {
  88. String out = "", bin;
  89. if (!buffer.isEmpty()) {
  90. bin = Utils.Join(buffer, "");
  91. out = Utils.Pad(pad, "" + base)
  92. + ":\t"
  93. + bin
  94. + ";\t% dec "
  95. + Long.parseUnsignedLong(Utils.padPreserveSignal(64, bin), 2)
  96. + " %\n";
  97. }
  98. return out;
  99. }
  100. protected static String Header(Memory m) {
  101. return "-- Copyright (C) 1991-2014 Altera Corporation. All rights reserved.\n"
  102. .concat("-- Your use of Altera Corporation's design tools, logic functions\n")
  103. .concat("-- and other software and tools, and its AMPP partner logic\n")
  104. .concat("-- functions, and any output files from any of the foregoing\n")
  105. .concat("-- (including device programming or simulation files), and any\n")
  106. .concat("-- associated documentation or information are expressly subject\n")
  107. .concat("-- to the terms and conditions of the Altera Program License\n")
  108. .concat("-- Subscription Agreement, the Altera Quartus II License Agreement,\n")
  109. .concat("-- the Altera MegaCore Function License Agreement, or other\n")
  110. .concat("-- (including device programming or simulation files), and any\n")
  111. .concat("-- applicable license agreement, including, without limitation,\n")
  112. .concat("-- that your use is for the sole purpose of programming logic \n")
  113. .concat("-- devices manufactured by Altera and sold by Altera or its \n")
  114. .concat("-- authorized distributors. Please refer to the applicable \n")
  115. .concat("-- agreement for further details.\n")
  116. .concat("-- Quartus II generated Memory Initialization File (.mif)\n")
  117. .concat("\n")
  118. .concat("WIDTH = " + m.WordBitSize() + ";\n")
  119. .concat("DEPTH = " + m.Size() + ";\n\n")
  120. .concat("ADDRESS_RADIX = DEC;\n")
  121. .concat("DATA_RADIX = BIN;\n\n")
  122. .concat("CONTENT\nBEGIN\n");
  123. }
  124. protected static String Footer(Memory m) {
  125. // String inst = Utils.Pad(wordSize, "");
  126. //
  127. // appendLine("\t[" + seHex(InstructionCount(), 0)
  128. // + ".." + seHex(size - 1, 0) + "]\t:\t" + seHex(inst, 8) + ";");
  129. return "END;";
  130. }
  131. }