MipsProcessor.java 17 KB

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