123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tools.mips;
- import API.Utils;
- import Export.MemoryInitializationFile;
- import common.Instruction;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.TreeMap;
- import targets.mips.Descriprion;
- /**
- *
- * @author EUGENIO CARVALHO
- */
- public class MipsProcessor {
- public String ID = "default";
- public Integer WORD = 4, BYTE = 1;
- public MipsSettings settings;
- public Memory DataMemory;
- public Memory InstructionMemory;
- public Long PC = 0L;
- public RegisterBank RBank;
- public HashMap<String, Instruction> instructions;
- public TreeMap<String, Boolean> breakPoint = new TreeMap<>();
- public MipsProcessor(MipsSettings settings) throws Exception {
- this.settings = new MipsSettings();
- this.settings.Set("mode", "debug").Set("step.by.step", "false");
- InstructionMemory = new Memory(
- settings.Get("memory.instruction"),
- settings.GetInt("memory.instruction.size")
- );
- InstructionMemory.SetIO(new MemoryInitializationFile());
- DataMemory = new Memory(
- settings.Get("memory.data"),
- settings.GetInt("memory.data.size")
- );
- DataMemory.SetIO(new MemoryInitializationFile());
- WORD = 4;
- // Limpa todos os dados armazenados
- // DataMemory.Reset();
- // System.out.println("Init Md:" + DataMemory.R(0L));
- RBank = new RegisterBank(32);
- InitInstructions();
- this.settings.copy(
- "debugmode,"
- + "mode,"
- + "step.by.step,"
- + "breakpoints,"
- + "memory.instruction,"
- + "memory.instruction.size,"
- + "memory.data,"
- + "memory.data.size",
- settings);
- SetBreak(this.settings.Get("breakpoints"));
- }
- public MipsProcessor SetBreak(String address) {
- for (String addrs : address.split(",")) {
- if (!addrs.equals("")) {
- // Range address
- if (addrs.contains("-")) {
- String[] parts = addrs.split("-");
- Long init = Long.parseLong(parts[0], 16), end = Long.parseLong(parts[1], 16);
- for (; init <= end; init += WORD) {
- breakPoint.put(Long.toString(init, 16), true);
- }
- } else {
- breakPoint.put(addrs, true);
- }
- }
- }
- System.out.println("Breakpoints:\n" + breakPoint);
- return this;
- }
- public MipsProcessor Persist() throws Exception {
- DataMemory.Save();
- return this;
- }
- protected void Log(String msg) {
- if (!settings.eq("mode", "debug")) {
- return;
- }
- if (breakPoint.size() > 0 && !breakPoint.containsKey(Long.toHexString(PC))) {
- return;
- }
- System.out.println(msg);
- }
- public MipsProcessor Run() throws Exception {
- Instruction instruction;
- Integer interation = 0;
- String cAddress;
- BufferedReader br = null;
- Boolean stepByStep = settings.eq("step.by.step", "true");
- if (stepByStep || !breakPoint.isEmpty()) {
- br = new BufferedReader(new InputStreamReader(System.in));
- }
- // Carrega o conteudo das memorias
- InstructionMemory.ReadFile();
- DataMemory.ReadFile();
- // Fetch
- // System.out.println("InstructionMemory:" + InstructionMemory);
- System.out.println("Start simulation ... " + ID);
- while (true) {
- instruction = Decode(InstructionMemory.R(PC, WORD));
- // Quando encontra a instrucao stop para a simulacao
- if (instruction.eq("inst", "stop")) {
- break;
- }
- cAddress = Long.toHexString(PC);
- Log("Interation(" + (interation++) + ")[" + cAddress + "]" + instruction.Get("inst"));
- // Executa a instrucao
- Execute(instruction);
- Log("Registradores(\n" + RBank + ")\n Next PC:" + Long.toHexString(PC));
- // Verifica o controle de execução do simulador
- if ((stepByStep || breakPoint.containsKey(cAddress)) && br != null) {
- br.readLine();
- }
- }
- return this;
- }
- public Instruction Decode(String bin) {
- // int val = (int) instruction;
- // String bin = Utils.Pad(32, Integer.toBinaryString(val));
- // System.out.println("Run:" + bin + ":" + bin.length());
- String codop = bin.substring(0, 6), key, func = "";
- if (codop.equals("000000")) {
- func = bin.substring(26);
- key = "@" + func;
- } else {
- key = codop;
- }
- if (!instructions.containsKey(key)) {
- System.out.println("Não encontrou a instrução " + key);
- return null;
- }
- Instruction i = instructions.get(key).copy();
- String rs = bin.substring(6, 11),
- rt = bin.substring(11, 16);
- switch (i.Get("type")) {
- case "R":
- i.Set("rs.bin", rs);
- i.Set("rt.bin", rt);
- i.Set("rd.bin", bin.substring(16, 21));
- i.Set("shamt.bin", bin.substring(21, 26));
- i.Set("funct.bin", func);
- i.Set("rs", Long.parseLong(rs, 2));
- i.Set("rt", Long.parseLong(rt, 2));
- i.Set("rd", Long.parseLong(bin.substring(16, 21), 2));
- i.Set("shamt", Long.parseLong(bin.substring(21, 26), 2));
- i.Set("funct", func);
- break;
- case "I":
- i.Set("rs.bin", rs);
- i.Set("rt.bin", rt);
- i.Set("imm.bin", bin.substring(16));
- i.Set("rs", Long.parseLong(rs, 2));
- i.Set("rt", Long.parseLong(rt, 2));
- i.Set("imm", Utils.bin32ToDec(bin.substring(16)));
- break;
- case "J":
- i.Set("addr.bin", bin.substring(6));
- i.Set("imm", Utils.bin32ToDec(bin.substring(6)));
- }
- return i;
- }
- // public Integer IntToHex(Integer n) {
- // return Integer.valueOf(String.valueOf(n), 16);
- // }
- protected void InitInstructions() {
- instructions = new HashMap<>();
- String key;
- for (Map.Entry<String, Instruction> entry : Descriprion.Codops.entrySet()) {
- // String string = entry.getKey();
- Instruction instruction = entry.getValue();
- key = instruction.Get("codop");
- if (instruction.eq("codop", "000000")) {
- key = "@" + instruction.Get("func");
- }
- instructions.put(key, instruction);
- };
- }
- private void Execute(Instruction inst) throws Exception {
- long rs = 0,
- rt = 0,
- imm = 0,
- rd = 0,
- shamt = 0;
- boolean hasImm = true;
- switch (inst.Get("type")) {
- case "R":
- rd = inst.GetInt("rd");
- shamt = inst.GetInt("shamt");
- hasImm = false;
- case "I":
- rs = inst.GetInt("rs");
- rt = inst.GetInt("rt");
- case "J":
- if (hasImm) {
- // System.out.println("HAS_IMM:" + inst.Get("imm"));
- imm = inst.getLong("imm");
- }
- }
- Log(inst.Get("inst")
- + " [rd:" + RBank.names.get((int) rd)
- + "] [rs:" + RBank.names.get((int) rs)
- + "] [rt:" + RBank.names.get((int) rt)
- + "] [shamt:" + shamt
- + "] [imm:" + imm + "]");
- switch (inst.Get("inst")) {
- // R
- case "add":
- case "addu":
- RBank.W(rd, RBank.R(rs) + RBank.R(rt));
- break;
- case "addi":
- case "addiu":
- // System.out.println("RBank.R(rs) + imm:" + RBank.R(rs) + ":" + imm);
- RBank.W(rt, RBank.R(rs) + imm);
- break;
- case "stop":
- break;
- case "and":
- case "andi":
- RBank.W(rd, RBank.R(rs) & RBank.R(rt));
- break;
- case "div":
- case "divu":
- RBank.W(RBank.REG_LO, RBank.R(rs) / RBank.R(rt));
- RBank.W(RBank.REG_HI, RBank.R(rs) % RBank.R(rt));
- break;
- case "jr":
- PC = RBank.R(rs);
- break;
- case "mfhi":
- RBank.W(rd, RBank.R(RBank.REG_HI));
- break;
- case "mflo":
- RBank.W(rd, RBank.R(RBank.REG_LO));
- break;
- case "mthi":
- RBank.W(RBank.R(RBank.REG_HI), RBank.R(rs));
- break;
- case "mtlo":
- RBank.W(RBank.R(RBank.REG_LO), RBank.R(rs));
- break;
- case "mult":
- case "multu":
- // verificar a questao do hi e lo
- // System.out.println("Mult:"
- // + rs + "(" + RBank.names.Get((int) rs) + "):"
- // + rt + "(" + RBank.names.Get((int) rt) + ")");
- RBank.W(RBank.REG_LO, RBank.R(rs) * RBank.R(rt));
- break;
- // case "nor":
- // RBank.W(rd, rs * rt);
- // break;
- case "or":
- RBank.W(rd, RBank.R(rs) | RBank.R(rt));
- break;
- case "sll":
- RBank.W(rd, RBank.R(rt) << shamt);
- break;
- case "slt":
- RBank.W(rd, (RBank.R(rs) < RBank.R(rt)) ? 1 : 0);
- break;
- case "sra":
- RBank.W(rd, RBank.R(rt) >> shamt);
- break;
- case "srl":
- RBank.W(rd, RBank.R(rt) >>> shamt);
- break;
- case "sub":
- case "subu":
- RBank.W(rd, RBank.R(rs) - RBank.R(rt));
- break;
- case "xor":
- RBank.W(rd, RBank.R(rs) ^ RBank.R(rt));
- break;
- // // I
- case "beq":
- if (RBank.R(rs) == RBank.R(rt)) {
- PC += ((imm + 1) << 2);
- return;
- }
- break;
- case "bgez":
- if (RBank.R(rs) >= 0) {
- PC += ((imm + 1) << 2);
- return;
- }
- break;
- case "bgtz":
- if (RBank.R(rs) > 0) {
- PC += ((imm + 1) << 2);
- return;
- }
- break;
- case "blez":
- if (RBank.R(rs) <= 0) {
- PC += ((imm + 1) << 2);
- return;
- }
- break;
- case "bltz":
- // System.out.println("BLTZ:" + rs);
- if (RBank.R(rs) < 0) {
- PC += ((imm + 1) << 2);
- return;
- }
- break;
- case "bne":
- if (RBank.R(rs) != RBank.R(rt)) {
- PC += ((imm + 1) << 2);
- return;
- }
- break;
- // case "lb":
- // if (RBank.R(rs) != RBank.R(rt)) {
- // PC += imm << 2;
- // return;
- // }
- // case "lbu":
- // case "lh":
- // case "lhu":
- case "lui":
- RBank.W(rt, imm);
- break;
- case "lb":
- RBank.W(
- rt,
- DataMemory.ReadLong(
- RBank.R(rs) + imm,
- BYTE // Apenas 1 byte
- ));
- Log(String.format(
- "Load endereco '%d' <- '%d'\n",
- RBank.R(rs) + imm,
- DataMemory.ReadLong(RBank.R(rs) + imm, BYTE)
- ));
- break;
- case "lw":
- // Escreve no banco de registradores no registrador enderecado por rt
- RBank.W(
- rt,
- // Lê da memoria de dados uma palavra de 4 bytes
- DataMemory.ReadLong(
- // Endereco de leitura equivale ao conteudo do registrador rs + imediato
- RBank.R(rs) + imm,
- WORD
- ));
- Log(String.format(
- "Load endereco '%d' <- '%d'\n",
- RBank.R(rs) + imm,
- DataMemory.ReadLong(RBank.R(rs) + imm, WORD)
- ));
- break;
- case "ori":
- RBank.W(rt, RBank.R(rs) | imm);
- break;
- case "slti":
- // case "sltiu":
- case "sw":
- DataMemory.W(
- RBank.R(rs) + imm,
- RBank.R(rt),
- WORD // uma palavra inteira
- );
- Log(String.format("Store '%d' no endereco '%d'\n", RBank.R(rt), RBank.R(rs) + imm));
- break;
- // case "xori":
- // J
- case "jal":
- RBank.W(31, PC);
- case "j":
- PC = imm << 2;
- return;
- default:
- throw new Exception("Instrução " + inst.Get("inst") + " não definida!");
- }
- PC += WORD;
- }
- }
- /**
- * <main>: 0: 0201326595 jal c <preenche+0x0> .0 -- jump to <preenche>
- * 4: 0000000000 sll zero,zero,0 .0 -- Nop 8: 4294967295 stop -- End of programa
- * <preenche>: c: 0666763252 addiu sp,sp,-12 -- prolog| push stack frame 10:
- * 0001962017 addu fp,zero,sp -- prolog|copy fp ← sp 14: 0605028353 addiu
- * s0,zero,1 .0 -- copy _V1 ← 1 18: 2949644288 sw s0,fp,0 .1 -- store content of
- * s0 in _V1 1c: 0605093888 addiu s1,zero,0 .2 -- copy _V2 ← 0 20: 2949709828 sw
- * s1,fp,4 .3 -- store content of s1 in _V2 24: 0134217774 j b8 <preenche+0xac>
- * .4 -- jump to preenche+_i3 28: 0000000000 sll zero,zero,0 .4 -- Nop 2c:
- * 0605159424 addiu s2,zero,0 .5 -- copy _V3 ← 0 30: 2949775368 sw s2,fp,8 .6 --
- * store content of s2 in _V3 34: 0134217764 j 90 <preenche+0x84> .7 -- jump to
- * preenche+_i7 38: 0000000000 sll zero,zero,0 .7 -- Nop 3c: 2412969988 lw
- * s3,fp,4 .8 -- load content from _V2 in s3 40: 0000000000 sll zero,zero,0 .8
- * -- Nop 44: 0001267745 addu t3,zero,s3 .9 -- copy _T4 ← _V2 48: 0000745536 sll
- * t4,t3,1 .10 -- _T5 = _T4 << 1 4c: 2413035528 lw s4,fp,8 .11 -- load content
- * from _V3 in s4 50: 0000000000 sll zero,zero,0 .11 -- Nop 54: 0001337377 addu
- * t5,zero,s4 .12 -- copy _T7 ← _V3 58: 0026042401 addu t4,t4,t5 .13 -- _T5 =
- * _T5 + _T7 5c: 0000815232 sll t6,t4,2 .14 -- _T11 = _T5 << 2 60: 2413101056 lw
- * s5,fp,0 .15 -- load content from _V1 in s5 64: 0000000000 sll zero,zero,0 .15
- * -- Nop 68: 0059666465 addu t6,gp,t6 .16 6c: 2916417536 sw s5,t6,0 .16 --
- * store content of t6 in _G13[_T11] 70: 2413101056 lw s5,fp,0 .17 -- load
- * content from _V1 in s5 74: 0000000000 sll zero,zero,0 .17 -- Nop 78:
- * 0649396225 addiu s5,s5,1 .18 -- _V1 = _V1 + 1 7c: 2949971968 sw s5,fp,0 .19
- * -- store content of s5 in _V1 80: 2413035528 lw s4,fp,8 .20 -- load content
- * from _V3 in s4 84: 0000000000 sll zero,zero,0 .20 -- Nop 88: 0647233537 addiu
- * s4,s4,1 .21 -- _V3 = _V3 + 1 8c: 2949906440 sw s4,fp,8 .22 -- store content
- * of s4 in _V3 90: 2413232136 lw s7,fp,8 .23 -- load content from _V3 in s7 94:
- * 0000000000 sll zero,zero,0 .23 -- Nop 98: 0604504066 addiu t0,zero,2 .24 9c:
- * 0048762915 subu v0,s7,t0 .24 a0: 0071368678 bltz v0,3c <preenche+0x30> .24 --
- * branch if register < 0 a4: 0000000000 sll zero,zero,0 .24 -- Nop a8:
- * 2412773380 lw s0,fp,4 .25 -- load content from _V2 in s0 ac: 0000000000 sll
- * zero,zero,0 .25 -- Nop b0: 0638582785 addiu s0,s0,1 .26 -- _V2 = _V2 + 1 b4:
- * 2949644292 sw s0,fp,4 .27 -- store content of s0 in _V2 b8: 2412838916 lw
- * s1,fp,4 .28 -- load content from _V2 in s1 bc: 0000000000 sll zero,zero,0 .28
- * -- Nop c0: 0604635138 addiu t2,zero,2 .29 c4: 0036311075 subu v0,s1,t2 .29
- * c8: 0071368664 bltz v0,2c <preenche+0x20> .29 -- branch if register < 0 cc:
- * 0000000000 sll zero,zero,0 .29 -- Nop d0: 0666697740 addiu sp,sp,12 --
- * epilog| pop stack frame d4: 0001962017 addu fp,zero,sp -- epilog| pop stack
- * frame d8: 0065011720 jr ra -- epilog| return
- */
|