Memory.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. @Override
  47. public String toString() {
  48. String out = "";
  49. for (Map.Entry<Long, Long> o : data.entrySet()) {
  50. out = out.concat(o.getKey() + "\t:\t" + o.getValue() + "\n");
  51. }
  52. return out;
  53. }
  54. protected void ReadFile() throws FileNotFoundException, IOException {
  55. String current = Paths.get(".").toAbsolutePath().normalize().toString();
  56. // String content = new String(Files.readAllBytes(Paths.get(current + file)));
  57. // System.out.println("current + this.file:" + current + this.file);
  58. long addr = -4;
  59. Scanner sc = new Scanner(new File(current + this.file));
  60. String value;
  61. String[] parts;
  62. while (sc.hasNextLine()) {
  63. // content += sc.nextLine() + "\n";
  64. value = sc.nextLine();
  65. parts = value.replaceAll("\\s+", "").split(":");
  66. if (parts.length == 1) {
  67. addr += 4;
  68. } else {
  69. addr = Long.parseLong(parts[0], 16);
  70. value = parts[1];
  71. }
  72. data.put((long) addr, Long.parseLong(value, 10));
  73. }
  74. // System.out.println("ReadFiel:" + content);
  75. }
  76. public long R(long address) throws Exception {
  77. valid(address);
  78. return data.get((address + size) % size);
  79. }
  80. public void W(long address, long value) throws Exception {
  81. valid(address);
  82. data.put((address + size) % size, value);
  83. }
  84. protected void valid(long address) throws Exception {
  85. // System.out.println("VALID:" + address + ":" + size);
  86. if (Math.abs(address) > size) {
  87. throw new Exception("Address '" + address + "' out of range limit " + size + "!");
  88. }
  89. }
  90. public Memory Save() {
  91. return Save(file);
  92. }
  93. public Memory Save(String filename) {
  94. String out = "";
  95. for (Map.Entry<Long, Long> entry : data.entrySet()) {
  96. out = out.concat(Utils.pad(4, "" + entry.getKey()) + ":\t" + entry.getValue() + "\n");
  97. }
  98. Utils.WriteFile(filename, out);
  99. return this;
  100. }
  101. }