Switch erc32 simulator copyright headers to FSF.
[deliverable/binutils-gdb.git] / sim / erc32 / sis.c
CommitLineData
17d88f73
JB
1/* Copyright (C) 1995-2015 Free Software Foundation, Inc.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 3 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
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/>. */
c906108c
SS
15
16#include "config.h"
17#include <signal.h>
18#include <string.h>
19#ifdef HAVE_STDLIB_H
20#include <stdlib.h>
21#endif
22#include <stdio.h>
c906108c
SS
23#include <sys/fcntl.h>
24#include "sis.h"
25#include <dis-asm.h>
26#include "sim-config.h"
638fcdad 27#include <inttypes.h>
c906108c 28
c906108c
SS
29#define VAL(x) strtol(x,(char **)NULL,0)
30
31/* Structures and functions from readline library */
32
5634e839
JG
33#include "readline/readline.h"
34#include "readline/history.h"
c906108c
SS
35
36/* Command history buffer length - MUST be binary */
37#define HIST_LEN 64
38
39extern struct disassemble_info dinfo;
40extern struct pstate sregs;
41extern struct estate ebase;
42
43extern int ctrl_c;
44extern int nfp;
45extern int ift;
46extern int wrp;
47extern int rom8;
48extern int uben;
49extern int sis_verbose;
50extern char *sis_version;
51extern struct estate ebase;
52extern struct evcell evbuf[];
53extern struct irqcell irqarr[];
54extern int irqpend, ext_irl;
55extern int termsave;
56extern int sparclite;
57extern int dumbio;
58extern char uart_dev1[];
59extern char uart_dev2[];
60extern uint32 last_load_addr;
61
62#ifdef ERA
63extern int era;
64#endif
65
66int
67run_sim(sregs, icount, dis)
68 struct pstate *sregs;
94110024 69 uint64 icount;
c906108c
SS
70 int dis;
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
142main(argc, argv)
143 int argc;
144 char **argv;
145{
146
147 int cont = 1;
148 int stat = 1;
149 int freq = 14;
150 int copt = 0;
151
152 char *cfile, *bacmd;
153 char *cmdq[HIST_LEN];
154 int cmdi = 0;
155 int i;
e26473a1 156 int lfile = 0;
c906108c
SS
157
158 cfile = 0;
159 for (i = 0; i < 64; i++)
160 cmdq[i] = 0;
14e1f957 161 printf("\n SIS - SPARC instruction simulator %s, copyright Jiri Gaisler 1995\n", sis_version);
c906108c
SS
162 printf(" Bug-reports to jgais@wd.estec.esa.nl\n\n");
163 while (stat < argc) {
164 if (argv[stat][0] == '-') {
165 if (strcmp(argv[stat], "-v") == 0) {
0a13382c 166 sis_verbose += 1;
c906108c
SS
167 } else if (strcmp(argv[stat], "-c") == 0) {
168 if ((stat + 1) < argc) {
169 copt = 1;
170 cfile = argv[++stat];
171 }
172 } else if (strcmp(argv[stat], "-nfp") == 0)
173 nfp = 1;
174 else if (strcmp(argv[stat], "-ift") == 0)
175 ift = 1;
176 else if (strcmp(argv[stat], "-wrp") == 0)
177 wrp = 1;
178 else if (strcmp(argv[stat], "-rom8") == 0)
179 rom8 = 1;
180 else if (strcmp(argv[stat], "-uben") == 0)
181 uben = 1;
182 else if (strcmp(argv[stat], "-uart1") == 0) {
183 if ((stat + 1) < argc)
184 strcpy(uart_dev1, argv[++stat]);
185 } else if (strcmp(argv[stat], "-uart2") == 0) {
186 if ((stat + 1) < argc)
187 strcpy(uart_dev2, argv[++stat]);
188 } else if (strcmp(argv[stat], "-freq") == 0) {
189 if ((stat + 1) < argc)
190 freq = VAL(argv[++stat]);
191 } else if (strcmp(argv[stat], "-sparclite") == 0) {
192 sparclite = 1;
193#ifdef ERA
194 } else if (strcmp(argv[stat], "-era") == 0) {
195 era = 1;
196#endif
197 } else if (strcmp(argv[stat], "-dumbio") == 0) {
198 dumbio = 1;
199 } else {
200 printf("unknown option %s\n", argv[stat]);
201 usage();
202 exit(1);
203 }
204 } else {
e26473a1 205 lfile = stat;
c906108c
SS
206 }
207 stat++;
208 }
209 if (nfp)
210 printf("FPU disabled\n");
211#ifdef ERA
212 if (era)
213 printf("ERA ECC emulation enabled\n");
214#endif
215 sregs.freq = freq;
216
217 INIT_DISASSEMBLE_INFO(dinfo, stdout, (fprintf_ftype) fprintf);
d3e9b40a
JG
218#ifdef HOST_LITTLE_ENDIAN
219 dinfo.endian = BFD_ENDIAN_LITTLE;
220#else
c906108c 221 dinfo.endian = BFD_ENDIAN_BIG;
d3e9b40a 222#endif
c906108c
SS
223
224 termsave = fcntl(0, F_GETFL, 0);
225 using_history();
226 init_signals();
227 ebase.simtime = 0;
228 reset_all();
229 init_bpt(&sregs);
230 init_sim();
e26473a1
JG
231 if (lfile)
232 last_load_addr = bfd_load(argv[lfile]);
c906108c
SS
233#ifdef STAT
234 reset_stat(&sregs);
235#endif
236
237 if (copt) {
238 bacmd = (char *) malloc(256);
239 strcpy(bacmd, "batch ");
240 strcat(bacmd, cfile);
241 exec_cmd(&sregs, bacmd);
242 }
243 while (cont) {
244
245 if (cmdq[cmdi] != 0) {
246#if 0
247 remove_history(cmdq[cmdi]);
248#else
249 remove_history(cmdi);
250#endif
251 free(cmdq[cmdi]);
252 cmdq[cmdi] = 0;
253 }
254 cmdq[cmdi] = readline("sis> ");
255 if (cmdq[cmdi] && *cmdq[cmdi])
256 add_history(cmdq[cmdi]);
257 if (cmdq[cmdi])
258 stat = exec_cmd(&sregs, cmdq[cmdi]);
259 else {
260 puts("\n");
261 exit(0);
262 }
263 switch (stat) {
264 case OK:
265 break;
266 case CTRL_C:
267 printf("\b\bInterrupt!\n");
268 case TIME_OUT:
638fcdad 269 printf(" Stopped at time %" PRIu64 " (%.3f ms)\n", ebase.simtime,
c906108c
SS
270 ((double) ebase.simtime / (double) sregs.freq) / 1000.0);
271 break;
272 case BPT_HIT:
273 printf("breakpoint at 0x%08x reached\n", sregs.pc);
274 sregs.bphit = 1;
275 break;
276 case ERROR:
277 printf("IU in error mode (%d)\n", sregs.trap);
278 stat = 0;
638fcdad 279 printf(" %8" PRIu64 " ", ebase.simtime);
c906108c
SS
280 dis_mem(sregs.pc, 1, &dinfo);
281 break;
282 default:
283 break;
284 }
285 ctrl_c = 0;
286 stat = OK;
287
288 cmdi = (cmdi + 1) & (HIST_LEN - 1);
289
290 }
291 return 0;
292}
293
This page took 0.668349 seconds and 4 git commands to generate.