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