24cfad13af4e4c5b86258cc988a676b2a7d8b3d3
[binutils-gdb.git] / sim / tic80 / interp.c
1 /* This file is part of the GDB simulators.
2
3 Copyright (C) 1997, Free Software Foundation
4 Condtributed by Cyngnus Solutions.
5
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 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 */
21
22
23
24 #include "sim-main.h"
25
26 #include "idecode.h"
27 #include "itable.h"
28
29 #include <signal.h>
30
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #else
34 #ifdef HAVE_STRINGS_H
35 #include <strings.h>
36 #endif
37 #endif
38
39 void
40 engine_init (SIM_DESC sd)
41 {
42 memset (&STATE_CPU (sd, 0)->reg, 0, sizeof STATE_CPU (sd, 0)->reg);
43 memset (&STATE_CPU (sd, 0)->cia, 0, sizeof STATE_CPU (sd, 0)->cia);
44 CPU_STATE (STATE_CPU (sd, 0)) = sd;
45 }
46
47
48 /* Mechanisms for stopping/restarting the simulation */
49
50 void
51 engine_error (SIM_DESC sd,
52 sim_cpu *cpu,
53 instruction_address cia,
54 const char *fmt,
55 ...)
56 {
57 va_list ap;
58 va_start (ap, fmt);
59 sim_io_evprintf (sd, fmt, ap);
60 va_end (ap);
61
62 if (sd->halt_ok)
63 {
64 sim_io_eprintf (sd, "\n");
65 engine_halt (sd, cpu, cia, sim_stopped, SIGABRT);
66 }
67 else
68 sim_io_error (sd, " - aborting simulation");
69 }
70
71 void
72 engine_halt (SIM_DESC sd,
73 sim_cpu *cpu,
74 instruction_address cia,
75 enum sim_stop reason,
76 int siggnal)
77 {
78 if (!sd->halt_ok)
79 sim_io_error (sd, "engine_halt - bad longjmp");
80 sd->reason = reason;
81 sd->siggnal = siggnal;
82 sd->halt_ok = 0;
83 sd->restart_ok = 0;
84 if (cpu != NULL)
85 cpu->cia = cia;
86 longjmp (sd->path_to_halt, 1);
87 }
88
89 void
90 engine_restart (SIM_DESC sd,
91 sim_cpu *cpu,
92 instruction_address cia)
93 {
94 if (!sd->restart_ok)
95 sim_io_error (sd, "engine_restart - bad longjmp");
96 sd->restart_ok = 0;
97 cpu->cia = cia;
98 longjmp(sd->path_to_restart, 1);
99 }
100
101
102 void
103 engine_run_until_stop (SIM_DESC sd,
104 volatile int *keep_running)
105 {
106 if (!setjmp (sd->path_to_halt))
107 {
108 instruction_address cia;
109 sim_cpu *cpu = STATE_CPU (sd, 0);
110 sd->halt_ok = 1;
111 setjmp (sd->path_to_restart);
112 sd->restart_ok = 1;
113 cia = cpu->cia;
114 do
115 {
116 instruction_word insn = IMEM (cia);
117 cia = idecode_issue (sd, insn, cia);
118 }
119 while (*keep_running);
120 engine_halt (sd, cpu, cia, sim_stopped, SIGINT);
121 }
122 }
123
124
125 void
126 engine_step (SIM_DESC sd)
127 {
128 if (!setjmp (sd->path_to_halt))
129 {
130 instruction_address cia;
131 instruction_word insn;
132 sim_cpu *cpu = STATE_CPU (sd, 0);
133 sd->halt_ok = 1;
134 setjmp (sd->path_to_restart);
135 sd->restart_ok = 1;
136 cia = cpu->cia;
137 insn = IMEM (cia);
138 cia = idecode_issue (sd, insn, cia);
139 engine_halt (sd, cpu, cia, sim_stopped, SIGTRAP);
140 }
141 }