MipsProcessor.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 Export.MemoryInitializationFile;
  9. import common.Instruction;
  10. import java.io.BufferedReader;
  11. import java.io.InputStreamReader;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import java.util.TreeMap;
  15. import targets.mips.Descriprion;
  16. /**
  17. *
  18. * @author EUGENIO CARVALHO
  19. */
  20. public class MipsProcessor {
  21. public String ID = "default";
  22. public Integer WORD = 4, BYTE = 1;
  23. public MipsSettings settings;
  24. public Memory DataMemory;
  25. public Memory InstructionMemory;
  26. public Long PC = 0L;
  27. public RegisterBank RBank;
  28. public HashMap<String, Instruction> instructions;
  29. public TreeMap<String, Boolean> breakPoint = new TreeMap<>();
  30. public MipsProcessor(MipsSettings settings) throws Exception {
  31. this.settings = new MipsSettings();
  32. this.settings.Set("mode", "debug").Set("step.by.step", "false");
  33. InstructionMemory = new Memory(
  34. settings.Get("memory.instruction"),
  35. settings.GetInt("memory.instruction.size")
  36. );
  37. InstructionMemory.SetIO(new MemoryInitializationFile());
  38. DataMemory = new Memory(
  39. settings.Get("memory.data"),
  40. settings.GetInt("memory.data.size")
  41. );
  42. DataMemory.SetIO(new MemoryInitializationFile());
  43. WORD = 4;
  44. // Limpa todos os dados armazenados
  45. // DataMemory.Reset();
  46. // System.out.println("Init Md:" + DataMemory.R(0L));
  47. RBank = new RegisterBank(32);
  48. InitInstructions();
  49. this.settings.copy(
  50. "debugmode,"
  51. + "mode,"
  52. + "step.by.step,"
  53. + "breakpoints,"
  54. + "memory.instruction,"
  55. + "memory.instruction.size,"
  56. + "memory.data,"
  57. + "memory.data.size",
  58. settings);
  59. SetBreak(this.settings.Get("breakpoints"));
  60. }
  61. public MipsProcessor SetBreak(String address) {
  62. for (String addrs : address.split(",")) {
  63. if (!addrs.equals("")) {
  64. // Range address
  65. if (addrs.contains("-")) {
  66. String[] parts = addrs.split("-");
  67. Long init = Long.parseLong(parts[0], 16), end = Long.parseLong(parts[1], 16);
  68. for (; init <= end; init += WORD) {
  69. breakPoint.put(Long.toString(init, 16), true);
  70. }
  71. } else {
  72. breakPoint.put(addrs, true);
  73. }
  74. }
  75. }
  76. System.out.println("Breakpoints:\n" + breakPoint);
  77. return this;
  78. }
  79. public MipsProcessor Persist() throws Exception {
  80. DataMemory.Save();
  81. return this;
  82. }
  83. protected void Log(String msg) {
  84. if (!settings.eq("mode", "debug")) {
  85. return;
  86. }
  87. if (breakPoint.size() > 0 && !breakPoint.containsKey(Long.toHexString(PC))) {
  88. return;
  89. }
  90. System.out.println(msg);
  91. }
  92. public MipsProcessor Run() throws Exception {
  93. Instruction instruction;
  94. Integer interation = 0;
  95. String cAddress;
  96. BufferedReader br = null;
  97. Boolean stepByStep = settings.eq("step.by.step", "true");
  98. if (stepByStep || !breakPoint.isEmpty()) {
  99. br = new BufferedReader(new InputStreamReader(System.in));
  100. }
  101. // Carrega o conteudo das memorias
  102. InstructionMemory.ReadFile();
  103. DataMemory.ReadFile();
  104. // Fetch
  105. // System.out.println("InstructionMemory:" + InstructionMemory);
  106. System.out.println("Start simulation ... " + ID);
  107. while (true) {
  108. instruction = Decode(InstructionMemory.R(PC, WORD));
  109. // Quando encontra a instrucao stop para a simulacao
  110. if (instruction.eq("inst", "stop")) {
  111. break;
  112. }
  113. cAddress = Long.toHexString(PC);
  114. Log("Interation(" + (interation++) + ")[" + cAddress + "]" + instruction.Get("inst"));
  115. // Executa a instrucao
  116. Execute(instruction);
  117. Log("Registradores(\n" + RBank + ")\n Next PC:" + Long.toHexString(PC));
  118. // Verifica o controle de execução do simulador
  119. if ((stepByStep || breakPoint.containsKey(cAddress)) && br != null) {
  120. br.readLine();
  121. }
  122. }
  123. return this;
  124. }
  125. public Instruction Decode(String bin) {
  126. // int val = (int) instruction;
  127. // String bin = Utils.Pad(32, Integer.toBinaryString(val));
  128. // System.out.println("Run:" + bin + ":" + bin.length());
  129. String codop = bin.substring(0, 6), key, func = "";
  130. if (codop.equals("000000")) {
  131. func = bin.substring(26);
  132. key = "@" + func;
  133. } else {
  134. key = codop;
  135. }
  136. if (!instructions.containsKey(key)) {
  137. System.out.println("Não encontrou a instrução " + key);
  138. return null;
  139. }
  140. Instruction i = instructions.get(key).copy();
  141. String rs = bin.substring(6, 11),
  142. rt = bin.substring(11, 16);
  143. switch (i.Get("type")) {
  144. case "R":
  145. i.Set("rs.bin", rs);
  146. i.Set("rt.bin", rt);
  147. i.Set("rd.bin", bin.substring(16, 21));
  148. i.Set("shamt.bin", bin.substring(21, 26));
  149. i.Set("funct.bin", func);
  150. i.Set("rs", Long.parseLong(rs, 2));
  151. i.Set("rt", Long.parseLong(rt, 2));
  152. i.Set("rd", Long.parseLong(bin.substring(16, 21), 2));
  153. i.Set("shamt", Long.parseLong(bin.substring(21, 26), 2));
  154. i.Set("funct", func);
  155. break;
  156. case "I":
  157. i.Set("rs.bin", rs);
  158. i.Set("rt.bin", rt);
  159. i.Set("imm.bin", bin.substring(16));
  160. i.Set("rs", Long.parseLong(rs, 2));
  161. i.Set("rt", Long.parseLong(rt, 2));
  162. i.Set("imm", Utils.bin32ToDec(bin.substring(16)));
  163. break;
  164. case "J":
  165. i.Set("addr.bin", bin.substring(6));
  166. i.Set("imm", Utils.bin32ToDec(bin.substring(6)));
  167. }
  168. return i;
  169. }
  170. // public Integer IntToHex(Integer n) {
  171. // return Integer.valueOf(String.valueOf(n), 16);
  172. // }
  173. protected void InitInstructions() {
  174. instructions = new HashMap<>();
  175. String key;
  176. for (Map.Entry<String, Instruction> entry : Descriprion.Codops.entrySet()) {
  177. // String string = entry.getKey();
  178. Instruction instruction = entry.getValue();
  179. key = instruction.Get("codop");
  180. if (instruction.eq("codop", "000000")) {
  181. key = "@" + instruction.Get("func");
  182. }
  183. instructions.put(key, instruction);
  184. };
  185. }
  186. private void Execute(Instruction inst) throws Exception {
  187. long rs = 0,
  188. rt = 0,
  189. imm = 0,
  190. rd = 0,
  191. shamt = 0;
  192. boolean hasImm = true;
  193. switch (inst.Get("type")) {
  194. case "R":
  195. rd = inst.GetInt("rd");
  196. shamt = inst.GetInt("shamt");
  197. hasImm = false;
  198. case "I":
  199. rs = inst.GetInt("rs");
  200. rt = inst.GetInt("rt");
  201. case "J":
  202. if (hasImm) {
  203. // System.out.println("HAS_IMM:" + inst.Get("imm"));
  204. imm = inst.getLong("imm");
  205. }
  206. }
  207. Log(inst.Get("inst")
  208. + " [rd:" + RBank.names.get((int) rd)
  209. + "] [rs:" + RBank.names.get((int) rs)
  210. + "] [rt:" + RBank.names.get((int) rt)
  211. + "] [shamt:" + shamt
  212. + "] [imm:" + imm + "]");
  213. switch (inst.Get("inst")) {
  214. // R
  215. case "add":
  216. case "addu":
  217. RBank.W(rd, RBank.R(rs) + RBank.R(rt));
  218. break;
  219. case "addi":
  220. case "addiu":
  221. // System.out.println("RBank.R(rs) + imm:" + RBank.R(rs) + ":" + imm);
  222. RBank.W(rt, RBank.R(rs) + imm);
  223. break;
  224. case "stop":
  225. break;
  226. case "and":
  227. case "andi":
  228. RBank.W(rd, RBank.R(rs) & RBank.R(rt));
  229. break;
  230. case "div":
  231. case "divu":
  232. RBank.W(RBank.REG_LO, RBank.R(rs) / RBank.R(rt));
  233. RBank.W(RBank.REG_HI, RBank.R(rs) % RBank.R(rt));
  234. break;
  235. case "jr":
  236. PC = RBank.R(rs);
  237. break;
  238. case "mfhi":
  239. RBank.W(rd, RBank.R(RBank.REG_HI));
  240. break;
  241. case "mflo":
  242. RBank.W(rd, RBank.R(RBank.REG_LO));
  243. break;
  244. case "mthi":
  245. RBank.W(RBank.R(RBank.REG_HI), RBank.R(rs));
  246. break;
  247. case "mtlo":
  248. RBank.W(RBank.R(RBank.REG_LO), RBank.R(rs));
  249. break;
  250. case "mult":
  251. case "multu":
  252. // verificar a questao do hi e lo
  253. // System.out.println("Mult:"
  254. // + rs + "(" + RBank.names.Get((int) rs) + "):"
  255. // + rt + "(" + RBank.names.Get((int) rt) + ")");
  256. RBank.W(RBank.REG_LO, RBank.R(rs) * RBank.R(rt));
  257. break;
  258. // case "nor":
  259. // RBank.W(rd, rs * rt);
  260. // break;
  261. case "or":
  262. RBank.W(rd, RBank.R(rs) | RBank.R(rt));
  263. break;
  264. case "sll":
  265. RBank.W(rd, RBank.R(rt) << shamt);
  266. break;
  267. case "slt":
  268. RBank.W(rd, (RBank.R(rs) < RBank.R(rt)) ? 1 : 0);
  269. break;
  270. case "sra":
  271. RBank.W(rd, RBank.R(rt) >> shamt);
  272. break;
  273. case "srl":
  274. RBank.W(rd, RBank.R(rt) >>> shamt);
  275. break;
  276. case "sub":
  277. case "subu":
  278. RBank.W(rd, RBank.R(rs) - RBank.R(rt));
  279. break;
  280. case "xor":
  281. RBank.W(rd, RBank.R(rs) ^ RBank.R(rt));
  282. break;
  283. // // I
  284. case "beq":
  285. if (RBank.R(rs) == RBank.R(rt)) {
  286. PC += ((imm + 1) << 2);
  287. return;
  288. }
  289. break;
  290. case "bgez":
  291. if (RBank.R(rs) >= 0) {
  292. PC += ((imm + 1) << 2);
  293. return;
  294. }
  295. break;
  296. case "bgtz":
  297. if (RBank.R(rs) > 0) {
  298. PC += ((imm + 1) << 2);
  299. return;
  300. }
  301. break;
  302. case "blez":
  303. if (RBank.R(rs) <= 0) {
  304. PC += ((imm + 1) << 2);
  305. return;
  306. }
  307. break;
  308. case "bltz":
  309. // System.out.println("BLTZ:" + rs);
  310. if (RBank.R(rs) < 0) {
  311. PC += ((imm + 1) << 2);
  312. return;
  313. }
  314. break;
  315. case "bne":
  316. if (RBank.R(rs) != RBank.R(rt)) {
  317. PC += ((imm + 1) << 2);
  318. return;
  319. }
  320. break;
  321. // case "lb":
  322. // if (RBank.R(rs) != RBank.R(rt)) {
  323. // PC += imm << 2;
  324. // return;
  325. // }
  326. // case "lbu":
  327. // case "lh":
  328. // case "lhu":
  329. case "lui":
  330. RBank.W(rt, imm);
  331. break;
  332. case "lb":
  333. RBank.W(
  334. rt,
  335. DataMemory.ReadLong(
  336. RBank.R(rs) + imm,
  337. BYTE // Apenas 1 byte
  338. ));
  339. Log(String.format(
  340. "Load endereco '%d' <- '%d'\n",
  341. RBank.R(rs) + imm,
  342. DataMemory.ReadLong(RBank.R(rs) + imm, BYTE)
  343. ));
  344. break;
  345. case "lw":
  346. // Escreve no banco de registradores no registrador enderecado por rt
  347. RBank.W(
  348. rt,
  349. // Lê da memoria de dados uma palavra de 4 bytes
  350. DataMemory.ReadLong(
  351. // Endereco de leitura equivale ao conteudo do registrador rs + imediato
  352. RBank.R(rs) + imm,
  353. WORD
  354. ));
  355. Log(String.format(
  356. "Load endereco '%d' <- '%d'\n",
  357. RBank.R(rs) + imm,
  358. DataMemory.ReadLong(RBank.R(rs) + imm, WORD)
  359. ));
  360. break;
  361. case "ori":
  362. RBank.W(rt, RBank.R(rs) | imm);
  363. break;
  364. case "slti":
  365. // case "sltiu":
  366. case "sw":
  367. DataMemory.W(
  368. RBank.R(rs) + imm,
  369. RBank.R(rt),
  370. WORD // uma palavra inteira
  371. );
  372. Log(String.format("Store '%d' no endereco '%d'\n", RBank.R(rt), RBank.R(rs) + imm));
  373. break;
  374. // case "xori":
  375. // J
  376. case "jal":
  377. RBank.W(31, PC);
  378. case "j":
  379. PC = imm << 2;
  380. return;
  381. default:
  382. throw new Exception("Instrução " + inst.Get("inst") + " não definida!");
  383. }
  384. PC += WORD;
  385. }
  386. }
  387. /**
  388. * <main>: 0: 0201326595 jal c <preenche+0x0> .0 -- jump to <preenche>
  389. * 4: 0000000000 sll zero,zero,0 .0 -- Nop 8: 4294967295 stop -- End of programa
  390. * <preenche>: c: 0666763252 addiu sp,sp,-12 -- prolog| push stack frame 10:
  391. * 0001962017 addu fp,zero,sp -- prolog|copy fp ← sp 14: 0605028353 addiu
  392. * s0,zero,1 .0 -- copy _V1 ← 1 18: 2949644288 sw s0,fp,0 .1 -- store content of
  393. * s0 in _V1 1c: 0605093888 addiu s1,zero,0 .2 -- copy _V2 ← 0 20: 2949709828 sw
  394. * s1,fp,4 .3 -- store content of s1 in _V2 24: 0134217774 j b8 <preenche+0xac>
  395. * .4 -- jump to preenche+_i3 28: 0000000000 sll zero,zero,0 .4 -- Nop 2c:
  396. * 0605159424 addiu s2,zero,0 .5 -- copy _V3 ← 0 30: 2949775368 sw s2,fp,8 .6 --
  397. * store content of s2 in _V3 34: 0134217764 j 90 <preenche+0x84> .7 -- jump to
  398. * preenche+_i7 38: 0000000000 sll zero,zero,0 .7 -- Nop 3c: 2412969988 lw
  399. * s3,fp,4 .8 -- load content from _V2 in s3 40: 0000000000 sll zero,zero,0 .8
  400. * -- Nop 44: 0001267745 addu t3,zero,s3 .9 -- copy _T4 ← _V2 48: 0000745536 sll
  401. * t4,t3,1 .10 -- _T5 = _T4 << 1 4c: 2413035528 lw s4,fp,8 .11 -- load content
  402. * from _V3 in s4 50: 0000000000 sll zero,zero,0 .11 -- Nop 54: 0001337377 addu
  403. * t5,zero,s4 .12 -- copy _T7 ← _V3 58: 0026042401 addu t4,t4,t5 .13 -- _T5 =
  404. * _T5 + _T7 5c: 0000815232 sll t6,t4,2 .14 -- _T11 = _T5 << 2 60: 2413101056 lw
  405. * s5,fp,0 .15 -- load content from _V1 in s5 64: 0000000000 sll zero,zero,0 .15
  406. * -- Nop 68: 0059666465 addu t6,gp,t6 .16 6c: 2916417536 sw s5,t6,0 .16 --
  407. * store content of t6 in _G13[_T11] 70: 2413101056 lw s5,fp,0 .17 -- load
  408. * content from _V1 in s5 74: 0000000000 sll zero,zero,0 .17 -- Nop 78:
  409. * 0649396225 addiu s5,s5,1 .18 -- _V1 = _V1 + 1 7c: 2949971968 sw s5,fp,0 .19
  410. * -- store content of s5 in _V1 80: 2413035528 lw s4,fp,8 .20 -- load content
  411. * from _V3 in s4 84: 0000000000 sll zero,zero,0 .20 -- Nop 88: 0647233537 addiu
  412. * s4,s4,1 .21 -- _V3 = _V3 + 1 8c: 2949906440 sw s4,fp,8 .22 -- store content
  413. * of s4 in _V3 90: 2413232136 lw s7,fp,8 .23 -- load content from _V3 in s7 94:
  414. * 0000000000 sll zero,zero,0 .23 -- Nop 98: 0604504066 addiu t0,zero,2 .24 9c:
  415. * 0048762915 subu v0,s7,t0 .24 a0: 0071368678 bltz v0,3c <preenche+0x30> .24 --
  416. * branch if register < 0 a4: 0000000000 sll zero,zero,0 .24 -- Nop a8:
  417. * 2412773380 lw s0,fp,4 .25 -- load content from _V2 in s0 ac: 0000000000 sll
  418. * zero,zero,0 .25 -- Nop b0: 0638582785 addiu s0,s0,1 .26 -- _V2 = _V2 + 1 b4:
  419. * 2949644292 sw s0,fp,4 .27 -- store content of s0 in _V2 b8: 2412838916 lw
  420. * s1,fp,4 .28 -- load content from _V2 in s1 bc: 0000000000 sll zero,zero,0 .28
  421. * -- Nop c0: 0604635138 addiu t2,zero,2 .29 c4: 0036311075 subu v0,s1,t2 .29
  422. * c8: 0071368664 bltz v0,2c <preenche+0x20> .29 -- branch if register < 0 cc:
  423. * 0000000000 sll zero,zero,0 .29 -- Nop d0: 0666697740 addiu sp,sp,12 --
  424. * epilog| pop stack frame d4: 0001962017 addu fp,zero,sp -- epilog| pop stack
  425. * frame d8: 0065011720 jr ra -- epilog| return
  426. */