micromipsrun.c 4.4 KB

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