📄 run.c
字号:
/* SPIM S20 MIPS simulator. Execute SPIM instructions. Copyright (C) 1990-2004 by James Larus (larus@cs.wisc.edu). ALL RIGHTS RESERVED. Changes for DOS and Windows versions by David A. Carley (dac@cs.wisc.edu) SPIM is distributed under the following conditions: You may make copies of SPIM for your own use and modify those copies. All copies of SPIM must retain my name and copyright notice. You may not sell SPIM or distributed SPIM in conjunction with a commerical product or service without the expressed written consent of James Larus. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *//* $Header: /Software/SPIM/src/run.c 55 4/24/04 9:55a Larus $*/#ifdef mips#define _IEEE 1#include <nan.h>#else#define NaN(X) ((X) != (X))#endif#include <math.h>#include <stdio.h>#ifdef WIN32#define _WIN32_WINDOWS 0x0500#define VC_EXTRALEAN#include <Windows.h>#else#include <errno.h>#include <signal.h>#include <sys/time.h>#endif#include "spim.h"#include "string-stream.h"#include "spim-utils.h"#include "inst.h"#include "reg.h"#include "mem.h"#include "sym-tbl.h"#include "y.tab.h"#include "syscall.h"#include "run.h"int force_break = 0; /* For the execution env. to force an execution break */#ifndef _MSC_VERextern int errno;long atol (const char *);#endif/* Local functions: */static void bump_CP0_timer ();static void set_fpu_cc (int cond, int cc, int less, int equal, int unordered);static void signed_multiply (reg_word v1, reg_word v2);static void start_CP0_timer ();#ifdef WIN32void CALLBACK timer_completion_routine(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue);#endifstatic void unsigned_multiply (reg_word v1, reg_word v2);#define SIGN_BIT(X) ((X) & 0x80000000)#define ARITH_OVFL(RESULT, OP1, OP2) (SIGN_BIT (OP1) == SIGN_BIT (OP2) \ && SIGN_BIT (OP1) != SIGN_BIT (RESULT))/* True when delayed_branches is true and instruction is executing in delayslot of another instruction. */static int running_in_delay_slot = 0;/* Executed delayed branch and jump instructions by running the instruction from the delay slot before transfering control. Note, in branches that don't jump, the instruction in the delay slot is executed by falling through normally. We take advantage of the MIPS architecture, which leaves undefined the result of executing a delayed instruction in a delay slot. Here we execute the second branch. */#define BRANCH_INST(TEST, TARGET, NULLIFY) \ { \ if (TEST) \ { \ mem_addr target = (TARGET); \ if (delayed_branches) \ { \ /* +4 since jump in delay slot */ \ target += BYTES_PER_WORD; \ } \ JUMP_INST(target) \ } \ else if (NULLIFY) \ { \ /* If test fails and nullify bit set, skip\ instruction in delay slot. */ \ PC += BYTES_PER_WORD; \ } \ }#define JUMP_INST(TARGET) \ { \ if (delayed_branches) \ { \ running_in_delay_slot = 1; \ run_spim (PC + BYTES_PER_WORD, 1, display);\ running_in_delay_slot = 0; \ } \ /* -4 since PC is bumped after this inst */ \ PC = (TARGET) - BYTES_PER_WORD; \ }/* If the delayed_load flag is false, the result from a load is available immediate. If the delayed_load flag is true, the result from a load is not available until the subsequent instruction has executed (as in the real machine). We need a two element shift register for the value and its destination, as the instruction following the load can itself be a load instruction. */#define LOAD_INST(DEST_A, LD, MASK) \ { \ LOAD_INST_BASE (DEST_A, (LD & (MASK))) \ }#define LOAD_INST_BASE(DEST_A, VALUE) \ { \ if (delayed_loads) \ { \ delayed_load_addr1 = (DEST_A); \ delayed_load_value1 = (VALUE); \ } \ else \ { \ *(DEST_A) = (VALUE); \ } \ }#define DO_DELAYED_UPDATE() \ if (delayed_loads) \ { \ /* Check for delayed updates */ \ if (delayed_load_addr2 != NULL) \ { \ *delayed_load_addr2 = delayed_load_value2; \ } \ delayed_load_addr2 = delayed_load_addr1; \ delayed_load_value2 = delayed_load_value1; \ delayed_load_addr1 = NULL; \ }/* Run the program stored in memory, starting at address PC for STEPS_TO_RUN instruction executions. If flag DISPLAY is non-zero, print each instruction before it executes. Return non-zero if program's execution can continue. */intrun_spim (mem_addr initial_PC, int steps_to_run, int display){ instruction *inst; static reg_word *delayed_load_addr1 = NULL, delayed_load_value1; static reg_word *delayed_load_addr2 = NULL, delayed_load_value2; int step, step_size, next_step; PC = initial_PC; if (!bare_machine && mapped_io) next_step = IO_INTERVAL; else next_step = steps_to_run; /* Run to completion */ /* Start a timer running */ start_CP0_timer(); for (step_size = MIN (next_step, steps_to_run); steps_to_run > 0; steps_to_run -= step_size, step_size = MIN (next_step, steps_to_run)) { if (!bare_machine && mapped_io) /* Every IO_INTERVAL steps, check if memory-mapped IO registers have changed. */ check_memory_mapped_IO (); /* else run inner loop for all steps */ if ((CP0_Status & CP0_Status_IE) && !(CP0_Status & CP0_Status_EXL) && ((CP0_Cause & CP0_Cause_IP) & (CP0_Status & CP0_Status_IM))) { /* There is an interrupt to process if IE bit set, EXL bit not set, and non-masked IP bit set */ raise_exception (ExcCode_Int); /* Handle interrupt now, before instruction executes, so that EPC points to unexecuted instructions, which is the one to return to. */ handle_exception (); } for (step = 0; step < step_size; step += 1) { if (force_break) { force_break = 0; return (1); } R[0] = 0; /* Maintain invariant value */#ifdef WIN32 SleepEx(0, TRUE); /* Put thread in awaitable state for WaitableTimer */#else { /* Poll for timer expiration */ struct itimerval time; if (-1 == getitimer (ITIMER_REAL, &time)) { perror ("getitmer failed"); } if (time.it_value.tv_usec == 0 && time.it_value.tv_sec == 0) { /* Timer expired.*/ bump_CP0_timer (); /* Restart timer for next interval. */ time.it_interval.tv_sec = 0; time.it_interval.tv_usec = 0; time.it_value.tv_sec = 0; time.it_value.tv_usec = TIMER_TICK_MS * 1000; if (-1 == setitimer (ITIMER_REAL, &time, NULL)) { perror ("setitmer failed"); } } }#endif exception_occurred = 0; inst = read_mem_inst (PC); if (exception_occurred) /* In reading instruction */ { exception_occurred = 0; handle_exception (); continue; } else if (inst == NULL) { run_error ("Attempt to execute non-instruction at 0x%08x\n", PC); return (0); } else if (EXPR (inst) != NULL && EXPR (inst)->symbol != NULL && EXPR (inst)->symbol->addr == 0) { error ("Instruction references undefined symbol at 0x%08x\n", PC); print_inst (PC); run_error (""); return (0); } if (display) print_inst (PC);#ifdef TEST_ASM test_assembly (inst);#endif DO_DELAYED_UPDATE (); switch (OPCODE (inst)) { case Y_ADD_OP: { reg_word vs = R[RS (inst)], vt = R[RT (inst)]; reg_word sum = vs + vt; if (ARITH_OVFL (sum, vs, vt)) RAISE_EXCEPTION (ExcCode_Ov, break); R[RD (inst)] = sum; break; } case Y_ADDI_OP: { reg_word vs = R[RS (inst)], imm = (short) IMM (inst); reg_word sum = vs + imm; if (ARITH_OVFL (sum, vs, imm)) RAISE_EXCEPTION (ExcCode_Ov, break); R[RT (inst)] = sum; break; } case Y_ADDIU_OP: R[RT (inst)] = R[RS (inst)] + (short) IMM (inst); break; case Y_ADDU_OP: R[RD (inst)] = R[RS (inst)] + R[RT (inst)]; break; case Y_AND_OP: R[RD (inst)] = R[RS (inst)] & R[RT (inst)]; break; case Y_ANDI_OP: R[RT (inst)] = R[RS (inst)] & (0xffff & IMM (inst)); break; case Y_BC2F_OP: case Y_BC2FL_OP: case Y_BC2T_OP: case Y_BC2TL_OP: RAISE_EXCEPTION (ExcCode_CpU, {}); /* No Coprocessor 2 */ break; case Y_BEQ_OP: BRANCH_INST (R[RS (inst)] == R[RT (inst)], PC + IDISP (inst), 0); break; case Y_BEQL_OP: BRANCH_INST (R[RS (inst)] == R[RT (inst)], PC + IDISP (inst), 1); break; case Y_BGEZ_OP: BRANCH_INST (SIGN_BIT (R[RS (inst)]) == 0, PC + IDISP (inst), 0); break; case Y_BGEZL_OP: BRANCH_INST (SIGN_BIT (R[RS (inst)]) == 0, PC + IDISP (inst), 1); break; case Y_BGEZAL_OP: R[31] = PC + (delayed_branches ? 2 * BYTES_PER_WORD : BYTES_PER_WORD); BRANCH_INST (SIGN_BIT (R[RS (inst)]) == 0, PC + IDISP (inst), 0); break; case Y_BGEZALL_OP: R[31] = PC + (delayed_branches ? 2 * BYTES_PER_WORD : BYTES_PER_WORD); BRANCH_INST (SIGN_BIT (R[RS (inst)]) == 0, PC + IDISP (inst), 1); break; case Y_BGTZ_OP: BRANCH_INST (R[RS (inst)] != 0 && SIGN_BIT (R[RS (inst)]) == 0, PC + IDISP (inst), 0); break; case Y_BGTZL_OP: BRANCH_INST (R[RS (inst)] != 0 && SIGN_BIT (R[RS (inst)]) == 0, PC + IDISP (inst), 1); break; case Y_BLEZ_OP: BRANCH_INST (R[RS (inst)] == 0 || SIGN_BIT (R[RS (inst)]) != 0, PC + IDISP (inst), 0); break; case Y_BLEZL_OP: BRANCH_INST (R[RS (inst)] == 0 || SIGN_BIT (R[RS (inst)]) != 0, PC + IDISP (inst), 1); break; case Y_BLTZ_OP: BRANCH_INST (SIGN_BIT (R[RS (inst)]) != 0, PC + IDISP (inst), 0); break; case Y_BLTZL_OP: BRANCH_INST (SIGN_BIT (R[RS (inst)]) != 0, PC + IDISP (inst), 1); break; case Y_BLTZAL_OP: R[31] = PC + (delayed_branches ? 2 * BYTES_PER_WORD : BYTES_PER_WORD); BRANCH_INST (SIGN_BIT (R[RS (inst)]) != 0, PC + IDISP (inst), 0); break; case Y_BLTZALL_OP: R[31] = PC + (delayed_branches ? 2 * BYTES_PER_WORD : BYTES_PER_WORD); BRANCH_INST (SIGN_BIT (R[RS (inst)]) != 0, PC + IDISP (inst), 1); break; case Y_BNE_OP: BRANCH_INST (R[RS (inst)] != R[RT (inst)], PC + IDISP (inst), 0); break; case Y_BNEL_OP: BRANCH_INST (R[RS (inst)] != R[RT (inst)], PC + IDISP (inst), 1); break; case Y_BREAK_OP: if (RD (inst) == 1) /* Debugger breakpoint */ RAISE_EXCEPTION (ExcCode_Bp, return (1)) else RAISE_EXCEPTION (ExcCode_Bp, break); case Y_CACHE_OP: break; /* Memory details not implemented */ case Y_CFC0_OP: R[RT (inst)] = CCR[0][RD (inst)]; break; case Y_CFC2_OP: RAISE_EXCEPTION (ExcCode_CpU, {}); /* No Coprocessor 2 */ break; case Y_CLO_OP: { reg_word val = R[RS (inst)]; int i; for (i = 31; 0 <= i; i -= 1) if (((val >> i) & 0x1) == 0) break; R[RD (inst) ] = 31 - i; break; } case Y_CLZ_OP: { reg_word val = R[RS (inst)]; int i; for (i = 31; 0 <= i; i -= 1) if (((val >> i) & 0x1) == 1) break; R[RD (inst) ] = 31 - i; break; } case Y_COP2_OP: RAISE_EXCEPTION (ExcCode_CpU, {}); /* No Coprocessor 2 */ break; case Y_CTC0_OP: CCR[0][RD (inst)] = R[RT (inst)]; break; case Y_CTC2_OP: RAISE_EXCEPTION (ExcCode_CpU, {}); /* No Coprocessor 2 */ break; case Y_DIV_OP: /* The behavior of this instruction is undefined on divide by zero or overflow. */ if (R[RT (inst)] != 0 && !(R[RS (inst)] == 0x80000000 && R[RT (inst)] == 0xffffffff)) { LO = (reg_word) R[RS (inst)] / (reg_word) R[RT (inst)]; HI = (reg_word) R[RS (inst)] % (reg_word) R[RT (inst)]; } break; case Y_DIVU_OP: /* The behavior of this instruction is undefined on divide by zero or overflow. */ if (R[RT (inst)] != 0 && !(R[RS (inst)] == 0x80000000 && R[RT (inst)] == 0xffffffff)) { LO = (u_reg_word) R[RS (inst)] / (u_reg_word) R[RT (inst)]; HI = (u_reg_word) R[RS (inst)] % (u_reg_word) R[RT (inst)]; } break; case Y_ERET_OP: { CP0_Status &= ~CP0_Status_EXL; /* Clear EXL bit */ JUMP_INST (CP0_EPC); /* Jump to EPC */ } break; case Y_J_OP: JUMP_INST (((PC & 0xf0000000) | TARGET (inst) << 2)); break; case Y_JAL_OP: if (delayed_branches) R[31] = PC + 2 * BYTES_PER_WORD; else R[31] = PC + BYTES_PER_WORD; JUMP_INST (((PC & 0xf0000000) | (TARGET (inst) << 2))); break; case Y_JALR_OP: { mem_addr tmp = R[RS (inst)]; if (delayed_branches) R[RD (inst)] = PC + 2 * BYTES_PER_WORD; else R[RD (inst)] = PC + BYTES_PER_WORD; JUMP_INST (tmp); } break; case Y_JR_OP: { mem_addr tmp = R[RS (inst)]; JUMP_INST (tmp); } break; case Y_LB_OP: LOAD_INST (&R[RT (inst)], read_mem_byte (R[BASE (inst)] + IOFFSET (inst)), 0xffffffff); break; case Y_LBU_OP: LOAD_INST (&R[RT (inst)], read_mem_byte (R[BASE (inst)] + IOFFSET (inst)), 0xff); break; case Y_LH_OP: LOAD_INST (&R[RT (inst)], read_mem_half (R[BASE (inst)] + IOFFSET (inst)), 0xffffffff); break; case Y_LHU_OP: LOAD_INST (&R[RT (inst)], read_mem_half (R[BASE (inst)] + IOFFSET (inst)), 0xffff); break; case Y_LL_OP: /* Uniprocess, so this instruction is just a load */ LOAD_INST (&R[RT (inst)], read_mem_word (R[BASE (inst)] + IOFFSET (inst)), 0xffffffff); break; case Y_LUI_OP: R[RT (inst)] = (IMM (inst) << 16) & 0xffff0000; break; case Y_LW_OP: LOAD_INST (&R[RT (inst)], read_mem_word (R[BASE (inst)] + IOFFSET (inst)), 0xffffffff); break; case Y_LDC2_OP: RAISE_EXCEPTION (ExcCode_CpU, {}); /* No Coprocessor 2 */ break; case Y_LWC2_OP: RAISE_EXCEPTION (ExcCode_CpU, {}); /* No Coprocessor 2 */ break; case Y_LWL_OP: { mem_addr addr = R[BASE (inst)] + IOFFSET (inst); reg_word word; /* Can't be register */ int byte = addr & 0x3; reg_word reg_val = R[RT (inst)]; word = read_mem_word (addr & 0xfffffffc); if (!exception_occurred)#ifdef BIGENDIAN switch (byte) { case 0: word = word; break; case 1: word = ((word & 0xffffff) << 8) | (reg_val & 0xff); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -