MemoryJUnitTest.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. import API.Utils;
  7. import org.junit.After;
  8. import org.junit.AfterClass;
  9. import org.junit.Assert;
  10. import org.junit.Before;
  11. import org.junit.BeforeClass;
  12. import org.junit.Test;
  13. import tools.mips.Memory;
  14. /**
  15. *
  16. * @author EUGENIO CARVALHO
  17. */
  18. public class MemoryJUnitTest {
  19. private Memory memory;
  20. public MemoryJUnitTest() {
  21. }
  22. @BeforeClass
  23. public static void setUpClass() {
  24. }
  25. @AfterClass
  26. public static void tearDownClass() {
  27. }
  28. @Before
  29. public void setUp() {
  30. memory = new Memory(16 * 1024);
  31. }
  32. @After
  33. public void tearDown() {
  34. }
  35. @Test
  36. public void WriteByte() throws Exception {
  37. String bd = "00000010";
  38. memory.WB(bd);
  39. Assert.assertEquals(bd, memory.RB(0));
  40. }
  41. @Test
  42. public void WriteByteAddressed() throws Exception {
  43. String bd = "00000010";
  44. Long address = 16L;
  45. memory.WB(address, bd);
  46. Assert.assertEquals(bd, memory.RB(address));
  47. }
  48. //
  49. // @Test
  50. // public void WriteMultByteAddressed() throws Exception {
  51. // Long value = 14030L;
  52. //
  53. // Long address = 16L;
  54. // // Escreve 4 bytes a partir do endereco 16
  55. // memory.W(address, Long.toBinaryString(value), 4);
  56. //
  57. // // Lê 4 bytes a partir do endereco 16 -> converte binario para long
  58. // Assert.assertEquals(value, memory.ReadLong(address, 4));
  59. // }
  60. @Test
  61. public void WriteNegativeValueAddressed() throws Exception {
  62. String bin = Long.toBinaryString(3L);
  63. printOneAndTwosComplement(Utils.Pad(32, bin));
  64. bin = Long.toBinaryString(-3L);
  65. printOneAndTwosComplement(Utils.Pad(32, bin));
  66. // System.out.println("2c:" + findTwoscomplement(new StringBuffer(Long.toBinaryString(value))));
  67. // convertStringToDecimal(
  68. // findTwoscomplement(new StringBuffer(Long.toBinaryString(value)))
  69. // );
  70. // Escreve 4 bytes a partir do endereco 16
  71. // memory.W(address, Long.toBinaryString(value), 4);
  72. // Lê 4 bytes a partir do endereco 16 -> converte binario para long
  73. // Assert.assertEquals(value, memory.ReadLong(address, 4));
  74. }
  75. @Test
  76. public void WriteInvalidAddress() throws Exception {
  77. boolean result = false;
  78. try {
  79. String bd = "00000010";
  80. Long address = 17000L;
  81. memory.WB(address, bd);
  82. } catch (Exception e) {
  83. result = true;
  84. }
  85. Assert.assertTrue(result);
  86. }
  87. // Print 1's and 2's complement of binary number
  88. // represented by "bin"
  89. public static void printOneAndTwosComplement(String bin) {
  90. int n = bin.length();
  91. int i;
  92. String ones = "";
  93. StringBuilder twos;
  94. // for ones complement flip every bit
  95. for (i = 0; i < n; i++) {
  96. ones += flip(bin.charAt(i));
  97. }
  98. System.out.println("Bin b:" + bin);
  99. System.out.println("One b:" + ones);
  100. twos = new StringBuilder(Long.toBinaryString(Long.parseLong(ones, 2) + 1L));
  101. System.out.println("Two b:" + twos);
  102. // for (i = n - 1; i >= 0; i--) {
  103. for (i = 0; i < n; i++) {
  104. if (ones.charAt(i) == '1') {
  105. twos.setCharAt(i, '0');
  106. } else {
  107. twos.setCharAt(i, '1');
  108. break;
  109. }
  110. }
  111. System.out.println("Two a:" + twos);
  112. // If No break : all are 1 as in 111 or 11111;
  113. // in such case, add extra 1 at beginning
  114. // if (i == -1) {
  115. // twos = '1' + twos;
  116. // }
  117. // System.out.println("1's complement: " + ones);
  118. // System.out.println("2's complement: " +);
  119. }
  120. // Returns '0' for '1' and '1' for '0'
  121. public static char flip(char c) {
  122. return (c == '0') ? '1' : '0';
  123. }
  124. //
  125. // static String findTwoscomplement(StringBuffer str) {
  126. // int n = str.length();
  127. //
  128. // // Traverse the string to get first '1' from
  129. // // the last of string
  130. // int i;
  131. // for (i = n - 1; i >= 0; i--) {
  132. // if (str.charAt(i) == '1') {
  133. // break;
  134. // }
  135. // }
  136. //
  137. // // If there exists no '1' concat 1 at the
  138. // // starting of string
  139. // if (i == -1) {
  140. // return "1" + str;
  141. // }
  142. //
  143. // // Continue traversal after the position of
  144. // // first '1'
  145. // for (int k = i - 1; k >= 0; k--) {
  146. // //Just flip the values
  147. // if (str.charAt(k) == '1') {
  148. // str.replace(k, k + 1, "0");
  149. // } else {
  150. // str.replace(k, k + 1, "1");
  151. // }
  152. // }
  153. //
  154. // // return the modified string
  155. // return str.toString();
  156. // }
  157. //
  158. // public static void convertStringToDecimal(String binary) {
  159. // int decimal = 0;
  160. //
  161. // int power = 0;
  162. //
  163. // if (binary.charAt(0) == '1' && binary.length() == 32) {
  164. //
  165. // StringBuilder builder = new StringBuilder();
  166. //
  167. // for (int i = 0; i < binary.length(); i++) {
  168. //
  169. // builder.append((binary.charAt(i) == '1' ? '0' : '1'));
  170. //
  171. // }
  172. //
  173. // while (binary.length() > 0) {
  174. //
  175. // int temp = Integer
  176. // .parseInt(builder.charAt((binary.length()) - 1) + "");
  177. // decimal += temp * Math.pow(2, power++);
  178. // binary = binary.substring(0, binary.length() - 1);
  179. //
  180. // }
  181. //
  182. // System.out.println((decimal + 1) * (-1));
  183. //
  184. // } else {
  185. //
  186. // while (binary.length() > 0) {
  187. // int temp = Integer
  188. // .parseInt(binary.charAt((binary.length()) - 1) + "");
  189. // decimal += temp * Math.pow(2, power++);
  190. // binary = binary.substring(0, binary.length() - 1);
  191. // }
  192. //
  193. // System.out.println(decimal);
  194. //
  195. // }
  196. // }
  197. // public static String TwosCompliment(String bin) {
  198. // String twos = "", ones = "";
  199. // System.out.println("bin:" + bin);
  200. //
  201. // for (int i = 0; i < bin.length(); i++) {
  202. // ones += flip(bin.charAt(i));
  203. // }
  204. //
  205. // int number0 = (int) Long.parseLong(ones, 2);
  206. //
  207. // System.out.println("number0:" + number0);
  208. //
  209. // StringBuilder builder = new StringBuilder(ones);
  210. // boolean b = false;
  211. //
  212. // for (int i = ones.length() - 1; i > 0; i--) {
  213. // if (ones.charAt(i) == '1') {
  214. // builder.setCharAt(i, '0');
  215. // } else {
  216. // builder.setCharAt(i, '1');
  217. // b = true;
  218. // break;
  219. // }
  220. // }
  221. // if (!b) {
  222. // builder.append("1", 0, 7);
  223. // }
  224. //
  225. // twos = builder.toString();
  226. //
  227. // return twos;
  228. // }
  229. }