Memory.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 tools.mips;
  7. import API.Utils;
  8. import java.util.TreeMap;
  9. /**
  10. *
  11. * @author EUGENIO CARVALHO
  12. */
  13. public class Memory {
  14. public Integer WORD_BYTES_COUNT = 4;
  15. protected String EMPTY_BYTE = "00000000";
  16. protected boolean writable;
  17. protected String file;
  18. protected Long size = 0L, localAddress = 0L;
  19. protected TreeMap<Long, String> data = new TreeMap<>();
  20. protected MemoryImportExportInterface io;
  21. public Memory ReadOnly() {
  22. this.writable = false;
  23. return this;
  24. }
  25. public int WordBitSize() {
  26. return WORD_BYTES_COUNT * 8;
  27. }
  28. public Memory Writable() {
  29. this.writable = true;
  30. return this;
  31. }
  32. public void Reset() {
  33. data.entrySet().forEach((entry) -> {
  34. entry.setValue(EMPTY_BYTE);
  35. });
  36. }
  37. public Memory(long size) {
  38. this.size = size;
  39. }
  40. public Memory(String filename, long size) throws Exception {
  41. if (filename.equals("")) {
  42. throw new Exception("Empty file not allowed.");
  43. }
  44. this.file = filename;
  45. this.size = size;
  46. }
  47. public TreeMap<Long, String> Data() {
  48. return data;
  49. }
  50. @Override
  51. public String toString() {
  52. try {
  53. return Dump();
  54. } catch (Exception ex) {
  55. return ex.getMessage();
  56. }
  57. }
  58. protected void CheckIOSet() throws Exception {
  59. if (io == null) {
  60. throw new Exception("Interface de IO da memória não definida.");
  61. }
  62. }
  63. public String Dump() throws Exception {
  64. CheckIOSet();
  65. return io.DumpFile(this);
  66. }
  67. protected void ReadFile() throws Exception {
  68. CheckIOSet();
  69. io.FromFile(this);
  70. }
  71. public Long ReadLong(long address, int rsize) throws Exception {
  72. return Long.parseLong(R(address, rsize), 2);
  73. }
  74. public String R(long address, int rsize) throws Exception {
  75. valid(address);
  76. String buffer = "";
  77. for (int i = 0; i < rsize; i++) {
  78. buffer += RB(address + i);
  79. }
  80. return Utils.Pad(buffer, "0", Utils.PAD_LEFT, 32);
  81. }
  82. public String RB(long address) throws Exception {
  83. valid(address);
  84. address = (address + size) % size;
  85. if (!data.containsKey(address)) {
  86. data.put(address, EMPTY_BYTE);
  87. }
  88. return data.get(address);
  89. }
  90. protected void valid(long address) throws Exception {
  91. if (Math.abs(address) > size) {
  92. throw new Exception("Address '" + address + "' out of range limit " + size + "!");
  93. }
  94. }
  95. public void WB(String sbyte) throws Exception {
  96. WB(localAddress++, sbyte);
  97. }
  98. public void WB(Long address, String sbyte) throws Exception {
  99. valid(address);
  100. int len = sbyte.length();
  101. if (len > 8) {
  102. sbyte = sbyte.substring(sbyte.length() - 8);
  103. } else if (len < 8) {
  104. sbyte = Utils.Pad(sbyte, "0", Utils.PAD_LEFT, 8);
  105. }
  106. data.put((address + size) % size, sbyte);
  107. }
  108. public void W(long address, long value, int wsize) throws Exception {
  109. W(address, Long.toBinaryString(value), wsize);
  110. }
  111. public void W(long address, String bin, int wsize) throws Exception {
  112. valid(address);
  113. int bpos = 4 - wsize;
  114. bin = Utils.Pad(bin, "0", Utils.PAD_LEFT, 32);
  115. String[] bytes = Utils.SplitEach(bin, 8);
  116. for (int i = 0; i < wsize; i++) {
  117. WB(address + i, bytes[bpos + i]);
  118. }
  119. }
  120. public Memory Save() throws Exception {
  121. return Save(file);
  122. }
  123. public Memory Save(String filename) throws Exception {
  124. // Grava o conteudo no arquivo
  125. Utils.WriteFile(filename, Dump());
  126. return this;
  127. }
  128. public void SetIO(MemoryImportExportInterface memoryInitializationFile) {
  129. io = memoryInitializationFile;
  130. }
  131. public String File() {
  132. return file;
  133. }
  134. public long Size() {
  135. return size;
  136. }
  137. }