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