Mif.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.Api;
  8. import common.Instruction;
  9. import API.Utils;
  10. import java.io.File;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. /**
  15. *
  16. * @author Eugenio
  17. */
  18. public class Mif {
  19. protected ArrayList<Instruction> instrucoes;
  20. protected int size = 1024;
  21. protected StringBuilder sb;
  22. protected int line = 0;
  23. protected int wordSize = 32;
  24. protected boolean showHeader = false;
  25. protected boolean hex = false;
  26. protected String ext = "mif";
  27. public Mif() {
  28. instrucoes = new ArrayList<>();
  29. sb = new StringBuilder();
  30. }
  31. public void setExt(String ext) {
  32. this.ext = ext;
  33. }
  34. public Mif(int size) {
  35. instrucoes = new ArrayList<>();
  36. sb = new StringBuilder();
  37. this.size = size;
  38. }
  39. public void save(String path) throws IOException {
  40. try (FileWriter fw = new FileWriter(new File(path + "." + ext))) {
  41. fw.write(getCodeStream());
  42. }
  43. }
  44. /**
  45. * Adiciona uma inStrução ao mif no proximo endereco diSponivel
  46. *
  47. * @param instrucao
  48. * @throws Exception
  49. */
  50. public void Add(Instruction instrucao) throws Exception {
  51. if (instrucoes.size() == size) {
  52. throw new Exception("Mif Completo. Não existe espaço para um nova instrução.");
  53. }
  54. instrucoes.add(instrucao);
  55. }
  56. /**
  57. * adiciona uma inStrução ao mif em um endereço conhecido
  58. *
  59. * @param endereco
  60. * @param instrucao
  61. * @throws Exception
  62. */
  63. void Add(String endereco, Instruction instrucao) throws Exception {
  64. if (instrucoes.size() == size) {
  65. throw new Exception("Mif Completo. Não existe espaço para um nova instrução.");
  66. }
  67. // int end = Integer.parseInt(endereco);
  68. // int dif = end - instrucoes.size();
  69. // if (dif >= 0) {
  70. // for (int i = 0; i < dif; i++) {
  71. // instrucoes.Add(nullLine());
  72. // }
  73. // }
  74. System.out.println("SizeFinal[" + instrucoes.size() + ":" + endereco + "]");
  75. instrucoes.add(Integer.parseInt(endereco), instrucao);
  76. // } catch (Exception e) {
  77. // Pattern p = Pattern.compile("[0-9]+");
  78. // Matcher m = p.matcher(e.getMessage());
  79. // while (m.find()) {
  80. // System.out.println(m.group(0));
  81. // }
  82. // System.out.println("Ex:" + e.getMessage());
  83. //
  84. // instrucoes.Add(, instrucao);
  85. // }
  86. }
  87. protected Instruction nullLine() {
  88. Instruction r = new Instruction();
  89. r.Set("text", Api.num2bin(0, 32) + ";");
  90. return r;
  91. }
  92. @Override
  93. public String toString() {
  94. return getCodeStream();
  95. }
  96. public int InstructionCount() {
  97. return instrucoes.size();
  98. }
  99. protected String seHex(int valor, int pad) {
  100. if (hex) {
  101. String s = Utils.int2hex(valor);
  102. return Utils.Pad(pad, s);
  103. } else {
  104. return "" + valor;
  105. }
  106. }
  107. protected String seHex(String valor) {
  108. // if (hex) {
  109. // String Set = ivannosysUtils.int2hex(valor);
  110. // return ivannosysUtils.Pad(wordSize / 4, Set);
  111. // } else {
  112. // return "" + valor;
  113. // }
  114. return (hex) ? Api.bin2Hex(valor, wordSize / 4) : "" + valor;
  115. // return (hex) ? MifFactory.bin2Hex(valor) : "" + valor;
  116. }
  117. protected String seHex(String valor, int pad) {
  118. String s = seHex(valor);
  119. // System.out.println("HEX:" + Set + "Z" + Pad);
  120. return Utils.Pad(pad, s);
  121. }
  122. protected String getCodeStream() {
  123. sb = new StringBuilder();
  124. addHeader();
  125. appendLine("WIDTH=" + wordSize + ";");
  126. appendLine("DEPTH=" + size + ";");
  127. appendLine("ADDRESS_RADIX=UNS;");
  128. appendLine("DATA_RADIX=BIN;");
  129. appendLine("CONTENT BEGIN");
  130. String inst;
  131. String linha;
  132. int sizeInstruction;
  133. // System.out.println("Gerar:" + hex);
  134. int ln;
  135. int i = 0;
  136. for (Instruction instrucao : instrucoes) {
  137. // ln = getLineNumber();
  138. linha = seHex(i++, 0);
  139. // System.out.println("LN:" + ln + ">LINHA:" + linha);
  140. inst = seHex(format(instrucao));
  141. sizeInstruction = inst.replace(" ", "").length();
  142. appendLine("\t" + linha + "\t:\t" + inst
  143. // + " [" + sizeInstruction + "]"
  144. + instrucao.Get("comment"));
  145. }
  146. addFooter();
  147. return sb.toString();
  148. }
  149. protected int getLineNumber() {
  150. return line++;
  151. }
  152. protected void appendLine(String line) {
  153. sb.append(line + "\n");
  154. }
  155. protected String format(Instruction instrucao) {
  156. String formato = "";
  157. switch (instrucao.Get("type")) {
  158. case "R":
  159. // formato = "{codop} {rs} {rt} {rd} {sa} {funct}";
  160. formato = "{codop}{rs}{rt}{rd}{sa}{func}";
  161. break;
  162. case "I":
  163. // formato = "{codop} {rs} {rt} {constant}";
  164. formato = "{codop}{rs}{rt}{constant}";
  165. break;
  166. case "J":
  167. // formato = "{codop} {target}";
  168. formato = "{codop}{target}";
  169. break;
  170. default:
  171. formato = "{text}";
  172. break;
  173. }
  174. return instrucao.format(formato);
  175. }
  176. protected void addHeader() {
  177. if (!showHeader) {
  178. return;
  179. }
  180. appendLine("-- Copyright (C) 1991-2014 Altera Corporation. All rights reserved.");
  181. appendLine("-- Your use of Altera Corporation's design tools, logic functions");
  182. appendLine("-- and other software and tools, and its AMPP partner logic");
  183. appendLine("-- functions, and any output files from any of the foregoing");
  184. appendLine("-- (including device programming or simulation files), and any");
  185. appendLine("-- associated documentation or information are expressly subject");
  186. appendLine("-- to the terms and conditions of the Altera Program License");
  187. appendLine("-- Subscription Agreement, the Altera Quartus II License Agreement,");
  188. appendLine("-- the Altera MegaCore Function License Agreement, or other");
  189. appendLine("-- (including device programming or simulation files), and any");
  190. appendLine("-- applicable license agreement, including, without limitation,");
  191. appendLine("-- that your use is for the sole purpose of programming logic ");
  192. appendLine("-- devices manufactured by Altera and sold by Altera or its ");
  193. appendLine("-- authorized distributors. Please refer to the applicable ");
  194. appendLine("-- agreement for further details.");
  195. appendLine("-- Quartus II generated Memory Initialization File (.mif)");
  196. }
  197. protected void addFooter() {
  198. String inst = Utils.Pad(wordSize, "");
  199. appendLine("\t[" + seHex(InstructionCount(), 0)
  200. + ".." + seHex(size - 1, 0) + "]\t:\t" + seHex(inst, 8) + ";");
  201. appendLine("END;");
  202. }
  203. public void setHexMode(boolean hex) {
  204. this.hex = hex;
  205. }
  206. }