Home | History | Annotate | Line # | Download | only in mips
micromipsrun.c revision 1.1.1.5
      1      1.1  christos /*  Run function for the micromips simulator
      2      1.1  christos 
      3  1.1.1.5  christos     Copyright (C) 2005-2023 Free Software Foundation, Inc.
      4      1.1  christos     Contributed by Imagination Technologies, Ltd.
      5      1.1  christos     Written by Andrew Bennett <andrew.bennett (at) imgtec.com>.
      6      1.1  christos 
      7      1.1  christos     This file is part of the MIPS sim.
      8      1.1  christos 
      9      1.1  christos     This program is free software; you can redistribute it and/or modify
     10      1.1  christos     it under the terms of the GNU General Public License as published by
     11      1.1  christos     the Free Software Foundation; either version 3 of the License, or
     12      1.1  christos     (at your option) any later version.
     13      1.1  christos 
     14      1.1  christos     This program is distributed in the hope that it will be useful,
     15      1.1  christos     but WITHOUT ANY WARRANTY; without even the implied warranty of
     16      1.1  christos     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17      1.1  christos     GNU General Public License for more details.
     18      1.1  christos 
     19      1.1  christos     You should have received a copy of the GNU General Public License
     20      1.1  christos     along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     21      1.1  christos 
     22  1.1.1.5  christos /* This must come before any other includes.  */
     23  1.1.1.5  christos #include "defs.h"
     24  1.1.1.5  christos 
     25      1.1  christos #include "sim-main.h"
     26      1.1  christos #include "micromips16_idecode.h"
     27      1.1  christos #include "micromips32_idecode.h"
     28      1.1  christos #include "micromips_m32_idecode.h"
     29      1.1  christos #include "bfd.h"
     30      1.1  christos #include "sim-engine.h"
     31      1.1  christos 
     32      1.1  christos /* These definitions come from the *_support.h files generated by igen and are
     33      1.1  christos    required because they are used in some of the macros in the code below.
     34      1.1  christos    Unfortunately we can not just blindly include the *_support.h files to get
     35      1.1  christos    these definitions because some of the defines in these files are specific
     36      1.1  christos    for a particular configuration of the simulator for example instruction word
     37      1.1  christos    size is 16 bits for micromips16 and 32 bits for micromips32.  This means we
     38      1.1  christos    could break future code changes by doing this, so a safer approach is to just
     39      1.1  christos    extract the defines that we need to get this file to compile.  */
     40      1.1  christos #define SD sd
     41      1.1  christos #define CPU cpu
     42      1.1  christos 
     43      1.1  christos address_word
     44      1.1  christos micromips_instruction_decode (SIM_DESC sd, sim_cpu * cpu,
     45      1.1  christos 			      address_word cia,
     46      1.1  christos 			      int instruction_size)
     47      1.1  christos {
     48      1.1  christos   if (instruction_size == MICROMIPS_DELAYSLOT_SIZE_ANY)
     49      1.1  christos     {
     50      1.1  christos       micromips16_instruction_word instruction_0 = IMEM16_MICROMIPS (cia);
     51      1.1  christos       if (MICROMIPS_MINOR_OPCODE (instruction_0) > 0
     52      1.1  christos 	  && MICROMIPS_MINOR_OPCODE (instruction_0) < 4)
     53      1.1  christos 	return micromips16_idecode_issue (sd, instruction_0, cia);
     54      1.1  christos       else
     55      1.1  christos 	{
     56      1.1  christos 	  micromips32_instruction_word instruction_0 = IMEM32_MICROMIPS (cia);
     57      1.1  christos 	  return micromips32_idecode_issue (sd, instruction_0, cia);
     58      1.1  christos 	}
     59      1.1  christos     }
     60      1.1  christos   else if (instruction_size == MICROMIPS_DELAYSLOT_SIZE_16)
     61      1.1  christos     {
     62      1.1  christos       micromips16_instruction_word instruction_0 = IMEM16_MICROMIPS (cia);
     63      1.1  christos       if (MICROMIPS_MINOR_OPCODE (instruction_0) > 0
     64      1.1  christos 	  && MICROMIPS_MINOR_OPCODE (instruction_0) < 4)
     65      1.1  christos 	return micromips16_idecode_issue (sd, instruction_0, cia);
     66      1.1  christos       else
     67      1.1  christos 	sim_engine_abort (sd, cpu, cia,
     68      1.1  christos 			  "Invalid 16 bit micromips instruction");
     69      1.1  christos     }
     70      1.1  christos   else if (instruction_size == MICROMIPS_DELAYSLOT_SIZE_32)
     71      1.1  christos     {
     72      1.1  christos       micromips32_instruction_word instruction_0 = IMEM32_MICROMIPS (cia);
     73      1.1  christos       return micromips32_idecode_issue (sd, instruction_0, cia);
     74      1.1  christos     }
     75      1.1  christos   else
     76      1.1  christos     return NULL_CIA;
     77      1.1  christos }
     78      1.1  christos 
     79      1.1  christos void
     80      1.1  christos sim_engine_run (SIM_DESC sd, int next_cpu_nr, int nr_cpus,
     81      1.1  christos 		int signal)
     82      1.1  christos {
     83  1.1.1.5  christos   struct mips_sim_state *state = MIPS_SIM_STATE (sd);
     84      1.1  christos   micromips_m32_instruction_word instruction_0;
     85      1.1  christos   sim_cpu *cpu = STATE_CPU (sd, next_cpu_nr);
     86      1.1  christos   micromips32_instruction_address cia = CPU_PC_GET (cpu);
     87  1.1.1.5  christos   state->isa_mode = ISA_MODE_MIPS32;
     88      1.1  christos 
     89      1.1  christos   while (1)
     90      1.1  christos     {
     91      1.1  christos       micromips32_instruction_address nia;
     92      1.1  christos 
     93      1.1  christos       /* Allow us to switch back from MIPS32 to microMIPS
     94      1.1  christos 	 This covers two cases:
     95      1.1  christos 	 1. Setting the correct isa mode based on the start address
     96      1.1  christos 	 from the elf header.
     97      1.1  christos 	 2. Setting the correct isa mode after a MIPS32 jump or branch
     98      1.1  christos 	 instruction.  */
     99  1.1.1.5  christos       if ((state->isa_mode == ISA_MODE_MIPS32)
    100      1.1  christos 	  && ((cia & 0x1) == ISA_MODE_MICROMIPS))
    101      1.1  christos 	{
    102  1.1.1.5  christos 	  state->isa_mode = ISA_MODE_MICROMIPS;
    103      1.1  christos 	  cia = cia & ~0x1;
    104      1.1  christos 	}
    105      1.1  christos 
    106      1.1  christos #if defined (ENGINE_ISSUE_PREFIX_HOOK)
    107      1.1  christos       ENGINE_ISSUE_PREFIX_HOOK ();
    108      1.1  christos #endif
    109  1.1.1.5  christos       switch (state->isa_mode)
    110      1.1  christos 	{
    111      1.1  christos 	case ISA_MODE_MICROMIPS:
    112      1.1  christos 	  nia =
    113      1.1  christos 	    micromips_instruction_decode (sd, cpu, cia,
    114      1.1  christos 					  MICROMIPS_DELAYSLOT_SIZE_ANY);
    115      1.1  christos 	  break;
    116      1.1  christos 	case ISA_MODE_MIPS32:
    117      1.1  christos 	  instruction_0 = IMEM32 (cia);
    118      1.1  christos 	  nia = micromips_m32_idecode_issue (sd, instruction_0, cia);
    119      1.1  christos 	  break;
    120      1.1  christos 	default:
    121      1.1  christos 	  nia = NULL_CIA;
    122      1.1  christos 	}
    123      1.1  christos 
    124      1.1  christos #if defined (ENGINE_ISSUE_POSTFIX_HOOK)
    125      1.1  christos       ENGINE_ISSUE_POSTFIX_HOOK ();
    126      1.1  christos #endif
    127      1.1  christos 
    128      1.1  christos       /* Update the instruction address */
    129      1.1  christos       cia = nia;
    130      1.1  christos 
    131      1.1  christos       /* process any events */
    132      1.1  christos       if (sim_events_tick (sd))
    133      1.1  christos 	{
    134      1.1  christos 	  CPU_PC_SET (cpu, cia);
    135      1.1  christos 	  sim_events_process (sd);
    136      1.1  christos 	  cia = CPU_PC_GET (cpu);
    137      1.1  christos 	}
    138      1.1  christos     }
    139      1.1  christos }
    140