]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/erc32/sis.c
sim: switch config.h usage to defs.h
[thirdparty/binutils-gdb.git] / sim / erc32 / sis.c
CommitLineData
296730a5
MF
1/* This file is part of SIS (SPARC instruction simulator)
2
3666a048 3 Copyright (C) 1995-2021 Free Software Foundation, Inc.
296730a5 4 Contributed by Jiri Gaisler, European Space Agency
17d88f73
JB
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 3 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, see <http://www.gnu.org/licenses/>. */
c906108c 18
6df01ab8
MF
19/* This must come before any other includes. */
20#include "defs.h"
21
c906108c
SS
22#include <signal.h>
23#include <string.h>
c906108c 24#include <stdlib.h>
c906108c 25#include <stdio.h>
c906108c
SS
26#include <sys/fcntl.h>
27#include "sis.h"
28#include <dis-asm.h>
29#include "sim-config.h"
638fcdad 30#include <inttypes.h>
c906108c 31
c906108c
SS
32#define VAL(x) strtol(x,(char **)NULL,0)
33
34/* Structures and functions from readline library */
35
5634e839
JG
36#include "readline/readline.h"
37#include "readline/history.h"
c906108c
SS
38
39/* Command history buffer length - MUST be binary */
40#define HIST_LEN 64
41
42extern struct disassemble_info dinfo;
43extern struct pstate sregs;
44extern struct estate ebase;
45
46extern int ctrl_c;
47extern int nfp;
48extern int ift;
49extern int wrp;
50extern int rom8;
51extern int uben;
52extern int sis_verbose;
53extern char *sis_version;
54extern struct estate ebase;
55extern struct evcell evbuf[];
56extern struct irqcell irqarr[];
57extern int irqpend, ext_irl;
58extern int termsave;
59extern int sparclite;
60extern int dumbio;
61extern char uart_dev1[];
62extern char uart_dev2[];
63extern uint32 last_load_addr;
64
65#ifdef ERA
66extern int era;
67#endif
68
69int
81e6e8ae 70run_sim(struct pstate *sregs, uint64 icount, int dis)
c906108c 71{
102b920e 72 int irq, mexc, deb;
c906108c 73
96d67095 74 sregs->starttime = get_time();
c906108c
SS
75 init_stdio();
76 if (sregs->err_mode) icount = 0;
77 deb = dis || sregs->histlen || sregs->bptnum;
78 irq = 0;
79 while (icount > 0) {
80
102b920e 81 mexc = memory_iread (sregs->pc, &sregs->inst, &sregs->hold);
c906108c
SS
82 sregs->icnt = 1;
83 if (sregs->annul) {
84 sregs->annul = 0;
85 sregs->pc = sregs->npc;
86 sregs->npc = sregs->npc + 4;
87 } else {
88 sregs->fhold = 0;
89 if (ext_irl) irq = check_interrupts(sregs);
90 if (!irq) {
91 if (mexc) {
92 sregs->trap = I_ACC_EXC;
93 } else {
94 if (deb) {
95 if ((sregs->bphit = check_bpt(sregs)) != 0) {
96 restore_stdio();
5831e29b 97 return BPT_HIT;
c906108c
SS
98 }
99 if (sregs->histlen) {
100 sregs->histbuf[sregs->histind].addr = sregs->pc;
101 sregs->histbuf[sregs->histind].time = ebase.simtime;
102 sregs->histind++;
103 if (sregs->histind >= sregs->histlen)
104 sregs->histind = 0;
105 }
106 if (dis) {
638fcdad 107 printf(" %8" PRIu64 " ", ebase.simtime);
c906108c
SS
108 dis_mem(sregs->pc, 1, &dinfo);
109 }
110 }
111 dispatch_instruction(sregs);
112 icount--;
113 }
114 }
115 if (sregs->trap) {
116 irq = 0;
117 sregs->err_mode = execute_trap(sregs);
118 if (sregs->err_mode) {
119 error_mode(sregs->pc);
120 icount = 0;
121 }
122 }
123 }
124 advance_time(sregs);
125 if (ctrl_c || (sregs->tlimit <= ebase.simtime)) {
126 icount = 0;
127 if (sregs->tlimit <= ebase.simtime) sregs->tlimit = -1;
128 }
129 }
96d67095 130 sregs->tottime += get_time() - sregs->starttime;
c906108c
SS
131 restore_stdio();
132 if (sregs->err_mode)
5831e29b 133 return ERROR;
c906108c
SS
134 if (ctrl_c) {
135 ctrl_c = 0;
5831e29b 136 return CTRL_C;
c906108c 137 }
5831e29b 138 return TIME_OUT;
c906108c
SS
139}
140
141int
81e6e8ae 142main(int argc, char **argv)
c906108c
SS
143{
144
145 int cont = 1;
146 int stat = 1;
147 int freq = 14;
148 int copt = 0;
149
150 char *cfile, *bacmd;
151 char *cmdq[HIST_LEN];
152 int cmdi = 0;
153 int i;
e26473a1 154 int lfile = 0;
c906108c
SS
155
156 cfile = 0;
157 for (i = 0; i < 64; i++)
158 cmdq[i] = 0;
14e1f957 159 printf("\n SIS - SPARC instruction simulator %s, copyright Jiri Gaisler 1995\n", sis_version);
c906108c
SS
160 printf(" Bug-reports to jgais@wd.estec.esa.nl\n\n");
161 while (stat < argc) {
162 if (argv[stat][0] == '-') {
163 if (strcmp(argv[stat], "-v") == 0) {
0a13382c 164 sis_verbose += 1;
c906108c
SS
165 } else if (strcmp(argv[stat], "-c") == 0) {
166 if ((stat + 1) < argc) {
167 copt = 1;
168 cfile = argv[++stat];
169 }
170 } else if (strcmp(argv[stat], "-nfp") == 0)
171 nfp = 1;
172 else if (strcmp(argv[stat], "-ift") == 0)
173 ift = 1;
174 else if (strcmp(argv[stat], "-wrp") == 0)
175 wrp = 1;
176 else if (strcmp(argv[stat], "-rom8") == 0)
177 rom8 = 1;
178 else if (strcmp(argv[stat], "-uben") == 0)
179 uben = 1;
180 else if (strcmp(argv[stat], "-uart1") == 0) {
181 if ((stat + 1) < argc)
182 strcpy(uart_dev1, argv[++stat]);
183 } else if (strcmp(argv[stat], "-uart2") == 0) {
184 if ((stat + 1) < argc)
185 strcpy(uart_dev2, argv[++stat]);
186 } else if (strcmp(argv[stat], "-freq") == 0) {
187 if ((stat + 1) < argc)
188 freq = VAL(argv[++stat]);
189 } else if (strcmp(argv[stat], "-sparclite") == 0) {
190 sparclite = 1;
191#ifdef ERA
192 } else if (strcmp(argv[stat], "-era") == 0) {
193 era = 1;
194#endif
195 } else if (strcmp(argv[stat], "-dumbio") == 0) {
196 dumbio = 1;
197 } else {
198 printf("unknown option %s\n", argv[stat]);
199 usage();
200 exit(1);
201 }
202 } else {
e26473a1 203 lfile = stat;
c906108c
SS
204 }
205 stat++;
206 }
207 if (nfp)
208 printf("FPU disabled\n");
209#ifdef ERA
210 if (era)
211 printf("ERA ECC emulation enabled\n");
212#endif
213 sregs.freq = freq;
214
215 INIT_DISASSEMBLE_INFO(dinfo, stdout, (fprintf_ftype) fprintf);
d3e9b40a
JG
216#ifdef HOST_LITTLE_ENDIAN
217 dinfo.endian = BFD_ENDIAN_LITTLE;
218#else
c906108c 219 dinfo.endian = BFD_ENDIAN_BIG;
d3e9b40a 220#endif
c906108c
SS
221
222 termsave = fcntl(0, F_GETFL, 0);
223 using_history();
224 init_signals();
225 ebase.simtime = 0;
226 reset_all();
227 init_bpt(&sregs);
228 init_sim();
e26473a1
JG
229 if (lfile)
230 last_load_addr = bfd_load(argv[lfile]);
c906108c
SS
231#ifdef STAT
232 reset_stat(&sregs);
233#endif
234
235 if (copt) {
236 bacmd = (char *) malloc(256);
237 strcpy(bacmd, "batch ");
238 strcat(bacmd, cfile);
239 exec_cmd(&sregs, bacmd);
240 }
241 while (cont) {
242
243 if (cmdq[cmdi] != 0) {
244#if 0
245 remove_history(cmdq[cmdi]);
246#else
247 remove_history(cmdi);
248#endif
249 free(cmdq[cmdi]);
250 cmdq[cmdi] = 0;
251 }
252 cmdq[cmdi] = readline("sis> ");
253 if (cmdq[cmdi] && *cmdq[cmdi])
254 add_history(cmdq[cmdi]);
255 if (cmdq[cmdi])
256 stat = exec_cmd(&sregs, cmdq[cmdi]);
257 else {
258 puts("\n");
259 exit(0);
260 }
261 switch (stat) {
262 case OK:
263 break;
264 case CTRL_C:
265 printf("\b\bInterrupt!\n");
266 case TIME_OUT:
638fcdad 267 printf(" Stopped at time %" PRIu64 " (%.3f ms)\n", ebase.simtime,
c906108c
SS
268 ((double) ebase.simtime / (double) sregs.freq) / 1000.0);
269 break;
270 case BPT_HIT:
271 printf("breakpoint at 0x%08x reached\n", sregs.pc);
272 sregs.bphit = 1;
273 break;
274 case ERROR:
275 printf("IU in error mode (%d)\n", sregs.trap);
276 stat = 0;
638fcdad 277 printf(" %8" PRIu64 " ", ebase.simtime);
c906108c
SS
278 dis_mem(sregs.pc, 1, &dinfo);
279 break;
280 default:
281 break;
282 }
283 ctrl_c = 0;
284 stat = OK;
285
286 cmdi = (cmdi + 1) & (HIST_LEN - 1);
287
288 }
289 return 0;
290}
291