Memory.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.nio.file.Paths;
  12. import java.util.LinkedHashMap;
  13. import java.util.Map;
  14. import java.util.Scanner;
  15. /**
  16. *
  17. * @author EUGENIO CARVALHO
  18. */
  19. public class Memory {
  20. protected boolean writable;
  21. protected String file;
  22. protected Integer size;
  23. protected LinkedHashMap<Long, Long> data = new LinkedHashMap<>();
  24. public boolean AllAddress = false;
  25. public Memory ReadOnly() {
  26. this.writable = false;
  27. return this;
  28. }
  29. public Memory Writable() {
  30. this.writable = true;
  31. return this;
  32. }
  33. public Memory(String filename, int size, boolean all) throws IOException {
  34. this.file = filename;
  35. this.size = size;
  36. this.AllAddress = all;
  37. if (all) {
  38. for (long i = 0; i < size; i += 4) {
  39. data.put(i, (long) 0);
  40. }
  41. }
  42. if (!filename.equals("")) {
  43. ReadFile();
  44. }
  45. }
  46. public void Reset() {
  47. for (long i = 0; i < size; i += 4) {
  48. data.put(i, (long) 0);
  49. }
  50. }
  51. @Override
  52. public String toString() {
  53. String out = "";
  54. for (Map.Entry<Long, Long> o : data.entrySet()) {
  55. out = out.concat(o.getKey() + "\t:\t" + o.getValue() + "\n");
  56. }
  57. return out;
  58. }
  59. protected void ReadFile() throws FileNotFoundException, IOException {
  60. String current = Paths.get(".").toAbsolutePath().normalize().toString();
  61. // String content = new String(Files.readAllBytes(Paths.get(current + file)));
  62. // System.out.println("current + this.file:" + current + this.file);
  63. long addr = -4;
  64. Scanner sc = new Scanner(new File(current + this.file));
  65. String value;
  66. String[] parts;
  67. while (sc.hasNextLine()) {
  68. // content += sc.nextLine() + "\n";
  69. value = sc.nextLine();
  70. if (value.equals("")) {
  71. continue;
  72. }
  73. parts = value.replaceAll("\\s+", "").split(":");
  74. // System.out.println("parts:" + new ArrayList<String>(Arrays.asList(parts)));
  75. if (parts.length == 1) {
  76. addr += 4;
  77. } else {
  78. addr = Long.parseLong(parts[0], 10);
  79. value = parts[1];
  80. }
  81. // System.out.println("Read:" + (long) addr + ":" + Long.parseLong(value, 10));
  82. data.put((long) addr, Long.parseLong(value, 10));
  83. }
  84. // System.out.println("ReadFiel:" + content);
  85. }
  86. public long R(long address) throws Exception {
  87. valid(address);
  88. return data.get((address + size) % size);
  89. }
  90. public void W(long address, long value) throws Exception {
  91. valid(address);
  92. data.put((address + size) % size, value);
  93. }
  94. protected void valid(long address) throws Exception {
  95. // System.out.println("VALID:" + address + ":" + size);
  96. if (Math.abs(address) > size) {
  97. throw new Exception("Address '" + address + "' out of range limit " + size + "!");
  98. }
  99. }
  100. public Memory Save() {
  101. return Save(file);
  102. }
  103. public Memory Save(String filename) {
  104. String out = "";
  105. for (Map.Entry<Long, Long> entry : data.entrySet()) {
  106. out = out.concat(Utils.pad(4, "" + entry.getKey()) + ":\t" + entry.getValue() + "\n");
  107. }
  108. Utils.WriteFile(filename, out);
  109. return this;
  110. }
  111. }