sim-if.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Main simulator entry points specific to the IQ2000.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions.
  4. This file is part of the GNU simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include <stdlib.h>
  19. #include "sim/callback.h"
  20. #include "sim-options.h"
  21. #include "libiberty.h"
  22. #include "bfd.h"
  23. static void free_state (SIM_DESC);
  24. /* Cover function for sim_cgen_disassemble_insn. */
  25. static void
  26. iq2000bf_disassemble_insn (SIM_CPU *cpu, const CGEN_INSN *insn,
  27. const ARGBUF *abuf, IADDR pc, char *buf)
  28. {
  29. sim_cgen_disassemble_insn(cpu, insn, abuf, pc, buf);
  30. }
  31. /* Cover function of sim_state_free to free the cpu buffers as well. */
  32. static void
  33. free_state (SIM_DESC sd)
  34. {
  35. if (STATE_MODULES (sd) != NULL)
  36. sim_module_uninstall (sd);
  37. sim_cpu_free_all (sd);
  38. sim_state_free (sd);
  39. }
  40. extern const SIM_MACH * const iq2000_sim_machs[];
  41. /* Create an instance of the simulator. */
  42. SIM_DESC
  43. sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
  44. char * const *argv)
  45. {
  46. char c;
  47. int i;
  48. SIM_DESC sd = sim_state_alloc (kind, callback);
  49. /* Set default options before parsing user options. */
  50. STATE_MACHS (sd) = iq2000_sim_machs;
  51. STATE_MODEL_NAME (sd) = "iq2000";
  52. current_alignment = STRICT_ALIGNMENT;
  53. current_target_byte_order = BFD_ENDIAN_BIG;
  54. /* The cpu data is kept in a separately allocated chunk of memory. */
  55. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
  56. {
  57. free_state (sd);
  58. return 0;
  59. }
  60. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  61. {
  62. free_state (sd);
  63. return 0;
  64. }
  65. /* The parser will print an error message for us, so we silently return. */
  66. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  67. {
  68. free_state (sd);
  69. return 0;
  70. }
  71. /* Allocate core managed memory. */
  72. sim_do_commandf (sd, "memory region 0x%x,0x%x", IQ2000_INSN_VALUE, IQ2000_INSN_MEM_SIZE);
  73. sim_do_commandf (sd, "memory region 0x%x,0x%x", IQ2000_DATA_VALUE, IQ2000_DATA_MEM_SIZE);
  74. /* check for/establish the reference program image */
  75. if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
  76. {
  77. free_state (sd);
  78. return 0;
  79. }
  80. /* Establish any remaining configuration options. */
  81. if (sim_config (sd) != SIM_RC_OK)
  82. {
  83. free_state (sd);
  84. return 0;
  85. }
  86. if (sim_post_argv_init (sd) != SIM_RC_OK)
  87. {
  88. free_state (sd);
  89. return 0;
  90. }
  91. /* Open a copy of the cpu descriptor table. */
  92. {
  93. CGEN_CPU_DESC cd = iq2000_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
  94. CGEN_ENDIAN_BIG);
  95. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  96. {
  97. SIM_CPU *cpu = STATE_CPU (sd, i);
  98. CPU_CPU_DESC (cpu) = cd;
  99. CPU_DISASSEMBLER (cpu) = iq2000bf_disassemble_insn;
  100. }
  101. iq2000_cgen_init_dis (cd);
  102. }
  103. return sd;
  104. }
  105. SIM_RC
  106. sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv,
  107. char * const *env)
  108. {
  109. SIM_CPU *current_cpu = STATE_CPU (sd, 0);
  110. host_callback *cb = STATE_CALLBACK (sd);
  111. SIM_ADDR addr;
  112. if (abfd != NULL)
  113. addr = bfd_get_start_address (abfd);
  114. else
  115. addr = CPU2INSN(0);
  116. sim_pc_set (current_cpu, addr);
  117. /* Standalone mode (i.e. `run`) will take care of the argv for us in
  118. sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
  119. with `gdb`), we need to handle it because the user can change the
  120. argv on the fly via gdb's 'run'. */
  121. if (STATE_PROG_ARGV (sd) != argv)
  122. {
  123. freeargv (STATE_PROG_ARGV (sd));
  124. STATE_PROG_ARGV (sd) = dupargv (argv);
  125. }
  126. if (STATE_PROG_ENVP (sd) != env)
  127. {
  128. freeargv (STATE_PROG_ENVP (sd));
  129. STATE_PROG_ENVP (sd) = dupargv (env);
  130. }
  131. cb->argv = STATE_PROG_ARGV (sd);
  132. cb->envp = STATE_PROG_ENVP (sd);
  133. return SIM_RC_OK;
  134. }