Commit | Line | Data |
---|---|---|
18c64df6 AC |
1 | /* MIPS Simulator definition. |
2 | Copyright (C) 1997 Free Software Foundation, Inc. | |
3 | Contributed by Cygnus Support. | |
4 | ||
5 | This file is part of GDB, the GNU debugger. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License along | |
18 | with this program; if not, write to the Free Software Foundation, Inc., | |
19 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #ifndef SIM_MAIN_H | |
22 | #define SIM_MAIN_H | |
23 | ||
24 | /* This simulator doesn't cache the Current Instruction Address */ | |
25 | #define SIM_ENGINE_HALT_HOOK(SD, LAST_CPU, CIA) | |
26 | #define SIM_ENGINE_RESUME_HOOK(SD, LAST_CPU, CIA) | |
27 | ||
28 | #define SIM_HAVE_BIENDIAN | |
29 | #define SIM_HAVE_FLATMEM | |
30 | ||
31 | ||
32 | /* hobble some common features for moment */ | |
18c64df6 AC |
33 | #define WITH_WATCHPOINTS 1 |
34 | ||
35 | #include "sim-basics.h" | |
36 | ||
dad6f1f3 | 37 | typedef address_word sim_cia; |
18c64df6 AC |
38 | |
39 | #include "sim-base.h" | |
40 | ||
41 | ||
42 | /* Depreciated macros and types for manipulating 64bit values. Use | |
43 | ../common/sim-bits.h and ../common/sim-endian.h macros instead. */ | |
44 | ||
45 | typedef signed64 word64; | |
46 | typedef unsigned64 uword64; | |
47 | ||
48 | #define WORD64LO(t) (unsigned int)((t)&0xFFFFFFFF) | |
49 | #define WORD64HI(t) (unsigned int)(((uword64)(t))>>32) | |
50 | #define SET64LO(t) (((uword64)(t))&0xFFFFFFFF) | |
51 | #define SET64HI(t) (((uword64)(t))<<32) | |
52 | #define WORD64(h,l) ((word64)((SET64HI(h)|SET64LO(l)))) | |
53 | #define UWORD64(h,l) (SET64HI(h)|SET64LO(l)) | |
54 | ||
55 | /* Sign-extend the given value (e) as a value (b) bits long. We cannot | |
56 | assume the HI32bits of the operand are zero, so we must perform a | |
57 | mask to ensure we can use the simple subtraction to sign-extend. */ | |
58 | #define SIGNEXTEND(e,b) \ | |
fb5a2a3e | 59 | ((unsigned_word) \ |
18c64df6 AC |
60 | (((e) & ((uword64) 1 << ((b) - 1))) \ |
61 | ? (((e) & (((uword64) 1 << (b)) - 1)) - ((uword64)1 << (b))) \ | |
fb5a2a3e | 62 | : ((e) & (((((uword64) 1 << ((b) - 1)) - 1) << 1) | 1)))) |
18c64df6 AC |
63 | |
64 | /* Check if a value will fit within a halfword: */ | |
65 | #define NOTHALFWORDVALUE(v) ((((((uword64)(v)>>16) == 0) && !((v) & ((unsigned)1 << 15))) || (((((uword64)(v)>>32) == 0xFFFFFFFF) && ((((uword64)(v)>>16) & 0xFFFF) == 0xFFFF)) && ((v) & ((unsigned)1 << 15)))) ? (1 == 0) : (1 == 1)) | |
66 | ||
67 | /* windows always looses */ | |
68 | #include <signal.h> | |
69 | #ifndef SIGBUS | |
70 | #define SIGBUS SIGSEGV | |
71 | #endif | |
72 | #ifdef _WIN32 | |
73 | #define SIGTRAP 5 | |
74 | #define SIGQUIT 3 | |
75 | #endif | |
76 | ||
77 | ||
ea985d24 | 78 | |
0c2c5f61 AC |
79 | /* Floating-point operations: */ |
80 | ||
81 | /* FPU registers must be one of the following types. All other values | |
82 | are reserved (and undefined). */ | |
83 | typedef enum { | |
84 | fmt_single = 0, | |
85 | fmt_double = 1, | |
86 | fmt_word = 4, | |
87 | fmt_long = 5, | |
88 | /* The following are well outside the normal acceptable format | |
89 | range, and are used in the register status vector. */ | |
90 | fmt_unknown = 0x10000000, | |
91 | fmt_uninterpreted = 0x20000000, | |
92 | } FP_formats; | |
93 | ||
94 | unsigned64 value_fpr PARAMS ((SIM_DESC sd, int fpr, FP_formats)); | |
95 | #define ValueFPR(FPR,FMT) value_fpr (sd, (FPR), (FMT)) | |
96 | ||
97 | void store_fpr PARAMS ((SIM_DESC sd, int fpr, FP_formats fmt, unsigned64 value)); | |
98 | #define StoreFPR(FPR,FMT,VALUE) store_fpr (sd, (FPR), (FMT), (VALUE)) | |
99 | ||
100 | int NaN PARAMS ((unsigned64 op, FP_formats fmt)); | |
101 | int Infinity PARAMS ((unsigned64 op, FP_formats fmt)); | |
102 | int Less PARAMS ((unsigned64 op1, unsigned64 op2, FP_formats fmt)); | |
103 | int Equal PARAMS ((unsigned64 op1, unsigned64 op2, FP_formats fmt)); | |
104 | unsigned64 AbsoluteValue PARAMS ((unsigned64 op, FP_formats fmt)); | |
105 | unsigned64 Negate PARAMS ((unsigned64 op, FP_formats fmt)); | |
106 | unsigned64 Add PARAMS ((unsigned64 op1, unsigned64 op2, FP_formats fmt)); | |
107 | unsigned64 Sub PARAMS ((unsigned64 op1, unsigned64 op2, FP_formats fmt)); | |
108 | unsigned64 Multiply PARAMS ((unsigned64 op1, unsigned64 op2, FP_formats fmt)); | |
109 | unsigned64 Divide PARAMS ((unsigned64 op1, unsigned64 op2, FP_formats fmt)); | |
110 | unsigned64 Recip PARAMS ((unsigned64 op, FP_formats fmt)); | |
111 | unsigned64 SquareRoot PARAMS ((unsigned64 op, FP_formats fmt)); | |
112 | unsigned64 convert PARAMS ((SIM_DESC sd, int rm, unsigned64 op, FP_formats from, FP_formats to)); | |
113 | #define Convert(rm,op,from,to) convert(sd,rm,op,from,to) | |
114 | ||
ea985d24 AC |
115 | /* Macro to update FPSR condition-code field. This is complicated by |
116 | the fact that there is a hole in the index range of the bits within | |
117 | the FCSR register. Also, the number of bits visible depends on the | |
118 | MIPS ISA version being supported. */ | |
119 | ||
120 | #define SETFCC(cc,v) {\ | |
121 | int bit = ((cc == 0) ? 23 : (24 + (cc)));\ | |
122 | FCSR = ((FCSR & ~(1 << bit)) | ((v) << bit));\ | |
123 | } | |
124 | #define GETFCC(cc) (((((cc) == 0) ? (FCSR & (1 << 23)) : (FCSR & (1 << (24 + (cc))))) != 0) ? 1 : 0) | |
125 | ||
126 | /* This should be the COC1 value at the start of the preceding | |
127 | instruction: */ | |
128 | #define PREVCOC1() ((STATE & simPCOC1) ? 1 : 0) | |
129 | ||
130 | #if 1 | |
131 | #define SizeFGR() (WITH_TARGET_WORD_BITSIZE) | |
132 | #else | |
133 | /* They depend on the CPU being simulated */ | |
134 | #define SizeFGR() ((WITH_TARGET_WORD_BITSIZE == 64 && ((SR & status_FR) == 1)) ? 64 : 32) | |
135 | #endif | |
136 | ||
137 | /* Standard FCRS bits: */ | |
138 | #define IR (0) /* Inexact Result */ | |
139 | #define UF (1) /* UnderFlow */ | |
140 | #define OF (2) /* OverFlow */ | |
141 | #define DZ (3) /* Division by Zero */ | |
142 | #define IO (4) /* Invalid Operation */ | |
143 | #define UO (5) /* Unimplemented Operation */ | |
144 | ||
145 | /* Get masks for individual flags: */ | |
146 | #if 1 /* SAFE version */ | |
147 | #define FP_FLAGS(b) (((unsigned)(b) < 5) ? (1 << ((b) + 2)) : 0) | |
148 | #define FP_ENABLE(b) (((unsigned)(b) < 5) ? (1 << ((b) + 7)) : 0) | |
149 | #define FP_CAUSE(b) (((unsigned)(b) < 6) ? (1 << ((b) + 12)) : 0) | |
150 | #else | |
151 | #define FP_FLAGS(b) (1 << ((b) + 2)) | |
152 | #define FP_ENABLE(b) (1 << ((b) + 7)) | |
153 | #define FP_CAUSE(b) (1 << ((b) + 12)) | |
154 | #endif | |
155 | ||
156 | #define FP_FS (1 << 24) /* MIPS III onwards : Flush to Zero */ | |
157 | ||
158 | #define FP_MASK_RM (0x3) | |
159 | #define FP_SH_RM (0) | |
160 | #define FP_RM_NEAREST (0) /* Round to nearest (Round) */ | |
161 | #define FP_RM_TOZERO (1) /* Round to zero (Trunc) */ | |
162 | #define FP_RM_TOPINF (2) /* Round to Plus infinity (Ceil) */ | |
163 | #define FP_RM_TOMINF (3) /* Round to Minus infinity (Floor) */ | |
164 | #define GETRM() (int)((FCSR >> FP_SH_RM) & FP_MASK_RM) | |
165 | ||
166 | ||
167 | ||
168 | /* Integer ALU operations: */ | |
169 | ||
170 | #include "sim-alu.h" | |
171 | ||
172 | #define ALU32_END(ANS) \ | |
173 | if (ALU32_HAD_OVERFLOW) \ | |
174 | SignalExceptionIntegerOverflow (); \ | |
92ad193b | 175 | (ANS) = ALU32_OVERFLOW_RESULT |
ea985d24 AC |
176 | |
177 | ||
178 | #define ALU64_END(ANS) \ | |
179 | if (ALU64_HAD_OVERFLOW) \ | |
180 | SignalExceptionIntegerOverflow (); \ | |
92ad193b | 181 | (ANS) = ALU64_OVERFLOW_RESULT; |
ea985d24 AC |
182 | |
183 | /* start-sanitize-r5900 */ | |
184 | ||
185 | #define BYTES_IN_MMI_REGS (sizeof(signed_word) + sizeof(signed_word)) | |
186 | #define HALFWORDS_IN_MMI_REGS (BYTES_IN_MMI_REGS/2) | |
187 | #define WORDS_IN_MMI_REGS (BYTES_IN_MMI_REGS/4) | |
188 | #define DOUBLEWORDS_IN_MMI_REGS (BYTES_IN_MMI_REGS/8) | |
189 | ||
190 | #define BYTES_IN_MIPS_REGS (sizeof(signed_word)) | |
191 | #define HALFWORDS_IN_MIPS_REGS (BYTES_IN_MIPS_REGS/2) | |
192 | #define WORDS_IN_MIPS_REGS (BYTES_IN_MIPS_REGS/4) | |
193 | #define DOUBLEWORDS_IN_MIPS_REGS (BYTES_IN_MIPS_REGS/8) | |
194 | ||
195 | /* SUB_REG_FETCH - return as lvalue some sub-part of a "register" | |
196 | T - type of the sub part | |
197 | TC - # of T's in the mips part of the "register" | |
198 | I - index (from 0) of desired sub part | |
199 | A - low part of "register" | |
200 | A1 - high part of register | |
201 | */ | |
202 | #define SUB_REG_FETCH(T,TC,A,A1,I) \ | |
203 | (*(((I) < (TC) ? (T*)(A) : (T*)(A1)) \ | |
204 | + (CURRENT_HOST_BYTE_ORDER == BIG_ENDIAN \ | |
205 | ? ((TC) - 1 - (I) % (TC)) \ | |
206 | : ((I) % (TC)) \ | |
207 | ) \ | |
208 | ) \ | |
209 | ) | |
210 | ||
211 | /* | |
212 | GPR_<type>(R,I) - return, as lvalue, the I'th <type> of general register R | |
213 | where <type> has two letters: | |
214 | 1 is S=signed or U=unsigned | |
215 | 2 is B=byte H=halfword W=word D=doubleword | |
216 | */ | |
217 | ||
218 | #define SUB_REG_SB(A,A1,I) SUB_REG_FETCH(signed8, BYTES_IN_MIPS_REGS, A, A1, I) | |
219 | #define SUB_REG_SH(A,A1,I) SUB_REG_FETCH(signed16, HALFWORDS_IN_MIPS_REGS, A, A1, I) | |
220 | #define SUB_REG_SW(A,A1,I) SUB_REG_FETCH(signed32, WORDS_IN_MIPS_REGS, A, A1, I) | |
221 | #define SUB_REG_SD(A,A1,I) SUB_REG_FETCH(signed64, DOUBLEWORDS_IN_MIPS_REGS, A, A1, I) | |
222 | ||
223 | #define SUB_REG_UB(A,A1,I) SUB_REG_FETCH(unsigned8, BYTES_IN_MIPS_REGS, A, A1, I) | |
224 | #define SUB_REG_UH(A,A1,I) SUB_REG_FETCH(unsigned16, HALFWORDS_IN_MIPS_REGS, A, A1, I) | |
225 | #define SUB_REG_UW(A,A1,I) SUB_REG_FETCH(unsigned32, WORDS_IN_MIPS_REGS, A, A1, I) | |
226 | #define SUB_REG_UD(A,A1,I) SUB_REG_FETCH(unsigned64, DOUBLEWORDS_IN_MIPS_REGS, A, A1, I) | |
227 | ||
228 | #define GPR_SB(R,I) SUB_REG_SB(®ISTERS[R], ®ISTERS1[R], I) | |
229 | #define GPR_SH(R,I) SUB_REG_SH(®ISTERS[R], ®ISTERS1[R], I) | |
230 | #define GPR_SW(R,I) SUB_REG_SW(®ISTERS[R], ®ISTERS1[R], I) | |
231 | #define GPR_SD(R,I) SUB_REG_SD(®ISTERS[R], ®ISTERS1[R], I) | |
232 | ||
233 | #define GPR_UB(R,I) SUB_REG_UB(®ISTERS[R], ®ISTERS1[R], I) | |
234 | #define GPR_UH(R,I) SUB_REG_UH(®ISTERS[R], ®ISTERS1[R], I) | |
235 | #define GPR_UW(R,I) SUB_REG_UW(®ISTERS[R], ®ISTERS1[R], I) | |
236 | #define GPR_UD(R,I) SUB_REG_UD(®ISTERS[R], ®ISTERS1[R], I) | |
237 | ||
238 | ||
239 | #define RS_SB(I) SUB_REG_SB(&rs_reg, &rs_reg1, I) | |
240 | #define RS_SH(I) SUB_REG_SH(&rs_reg, &rs_reg1, I) | |
241 | #define RS_SW(I) SUB_REG_SW(&rs_reg, &rs_reg1, I) | |
242 | #define RS_SD(I) SUB_REG_SD(&rs_reg, &rs_reg1, I) | |
243 | ||
244 | #define RS_UB(I) SUB_REG_UB(&rs_reg, &rs_reg1, I) | |
245 | #define RS_UH(I) SUB_REG_UH(&rs_reg, &rs_reg1, I) | |
246 | #define RS_UW(I) SUB_REG_UW(&rs_reg, &rs_reg1, I) | |
247 | #define RS_UD(I) SUB_REG_UD(&rs_reg, &rs_reg1, I) | |
248 | ||
249 | #define RT_SB(I) SUB_REG_SB(&rt_reg, &rt_reg1, I) | |
250 | #define RT_SH(I) SUB_REG_SH(&rt_reg, &rt_reg1, I) | |
251 | #define RT_SW(I) SUB_REG_SW(&rt_reg, &rt_reg1, I) | |
252 | #define RT_SD(I) SUB_REG_SD(&rt_reg, &rt_reg1, I) | |
253 | ||
254 | #define RT_UB(I) SUB_REG_UB(&rt_reg, &rt_reg1, I) | |
255 | #define RT_UH(I) SUB_REG_UH(&rt_reg, &rt_reg1, I) | |
256 | #define RT_UW(I) SUB_REG_UW(&rt_reg, &rt_reg1, I) | |
257 | #define RT_UD(I) SUB_REG_UD(&rt_reg, &rt_reg1, I) | |
258 | ||
259 | ||
260 | ||
261 | #define LO_SB(I) SUB_REG_SB(&LO, &LO1, I) | |
262 | #define LO_SH(I) SUB_REG_SH(&LO, &LO1, I) | |
263 | #define LO_SW(I) SUB_REG_SW(&LO, &LO1, I) | |
264 | #define LO_SD(I) SUB_REG_SD(&LO, &LO1, I) | |
265 | ||
266 | #define LO_UB(I) SUB_REG_UB(&LO, &LO1, I) | |
267 | #define LO_UH(I) SUB_REG_UH(&LO, &LO1, I) | |
268 | #define LO_UW(I) SUB_REG_UW(&LO, &LO1, I) | |
269 | #define LO_UD(I) SUB_REG_UD(&LO, &LO1, I) | |
270 | ||
271 | #define HI_SB(I) SUB_REG_SB(&HI, &HI1, I) | |
272 | #define HI_SH(I) SUB_REG_SH(&HI, &HI1, I) | |
273 | #define HI_SW(I) SUB_REG_SW(&HI, &HI1, I) | |
274 | #define HI_SD(I) SUB_REG_SD(&HI, &HI1, I) | |
275 | ||
276 | #define HI_UB(I) SUB_REG_UB(&HI, &HI1, I) | |
277 | #define HI_UH(I) SUB_REG_UH(&HI, &HI1, I) | |
278 | #define HI_UW(I) SUB_REG_UW(&HI, &HI1, I) | |
279 | #define HI_UD(I) SUB_REG_UD(&HI, &HI1, I) | |
280 | ||
281 | /* end-sanitize-r5900 */ | |
282 | ||
0c2c5f61 AC |
283 | |
284 | ||
285 | ||
18c64df6 | 286 | struct _sim_cpu { |
0c2c5f61 AC |
287 | |
288 | ||
289 | /* The following are internal simulator state variables: */ | |
ea985d24 | 290 | sim_cia cia; |
dad6f1f3 | 291 | #define CPU_CIA(CPU) (PC) |
0c2c5f61 AC |
292 | address_word ipc; /* internal Instruction PC */ |
293 | address_word dspc; /* delay-slot PC */ | |
294 | #define IPC ((STATE_CPU (sd,0))->ipc) | |
295 | #define DSPC ((STATE_CPU (sd,0))->dspc) | |
296 | ||
dad6f1f3 AC |
297 | /* Issue a delay slot instruction immediatly by re-calling |
298 | idecode_issue */ | |
299 | #define DELAY_SLOT(TARGET) \ | |
300 | do { \ | |
301 | address_word target = (TARGET); \ | |
302 | instruction_word delay_insn; \ | |
303 | sim_events_slip (sd, 1); \ | |
304 | PC = CIA + 4; \ | |
305 | STATE |= simDELAYSLOT; \ | |
306 | delay_insn = IMEM (PC); \ | |
307 | idecode_issue (sd, delay_insn, (PC)); \ | |
308 | STATE &= !simDELAYSLOT; \ | |
309 | PC = target; \ | |
310 | } while (0) | |
311 | #define NULLIFY_NEXT_INSTRUCTION() \ | |
312 | do { \ | |
313 | sim_events_slip (sd, 1); \ | |
314 | NIA = CIA + 4; \ | |
315 | } while (0) | |
ea985d24 AC |
316 | |
317 | ||
0c2c5f61 AC |
318 | |
319 | /* State of the simulator */ | |
320 | unsigned int state; | |
321 | unsigned int dsstate; | |
322 | #define STATE ((STATE_CPU (sd,0))->state) | |
323 | #define DSSTATE ((STATE_CPU (sd,0))->dsstate) | |
324 | ||
ea985d24 AC |
325 | /* Flags in the "state" variable: */ |
326 | #define simHALTEX (1 << 2) /* 0 = run; 1 = halt on exception */ | |
327 | #define simHALTIN (1 << 3) /* 0 = run; 1 = halt on interrupt */ | |
328 | #define simTRACE (1 << 8) /* 0 = do nothing; 1 = trace address activity */ | |
ea985d24 AC |
329 | #define simPCOC0 (1 << 17) /* COC[1] from current */ |
330 | #define simPCOC1 (1 << 18) /* COC[1] from previous */ | |
331 | #define simDELAYSLOT (1 << 24) /* 0 = do nothing; 1 = delay slot entry exists */ | |
332 | #define simSKIPNEXT (1 << 25) /* 0 = do nothing; 1 = skip instruction */ | |
333 | #define simSIGINT (1 << 28) /* 0 = do nothing; 1 = SIGINT has occured */ | |
334 | #define simJALDELAYSLOT (1 << 29) /* 1 = in jal delay slot */ | |
335 | ||
336 | ||
337 | ||
0c2c5f61 AC |
338 | |
339 | /* This is nasty, since we have to rely on matching the register | |
340 | numbers used by GDB. Unfortunately, depending on the MIPS target | |
341 | GDB uses different register numbers. We cannot just include the | |
342 | relevant "gdb/tm.h" link, since GDB may not be configured before | |
343 | the sim world, and also the GDB header file requires too much other | |
344 | state. */ | |
345 | ||
346 | #ifndef TM_MIPS_H | |
347 | #define LAST_EMBED_REGNUM (89) | |
348 | #define NUM_REGS (LAST_EMBED_REGNUM + 1) | |
349 | /* start-sanitize-r5900 */ | |
350 | #undef NUM_REGS | |
351 | #define NUM_REGS (128) | |
352 | /* end-sanitize-r5900 */ | |
353 | #endif | |
354 | ||
355 | /* To keep this default simulator simple, and fast, we use a direct | |
356 | vector of registers. The internal simulator engine then uses | |
357 | manifests to access the correct slot. */ | |
358 | ||
fb5a2a3e | 359 | unsigned_word registers[LAST_EMBED_REGNUM + 1]; |
0c2c5f61 AC |
360 | int register_widths[NUM_REGS]; |
361 | #define REGISTERS ((STATE_CPU (sd,0))->registers) | |
362 | ||
363 | #define GPR (®ISTERS[0]) | |
364 | #define FGRIDX (38) | |
365 | #define FGR (®ISTERS[FGRIDX]) | |
366 | #define LO (REGISTERS[33]) | |
367 | #define HI (REGISTERS[34]) | |
368 | #define PC (REGISTERS[37]) | |
369 | #define CAUSE (REGISTERS[36]) | |
370 | #define SRIDX (32) | |
371 | #define SR (REGISTERS[SRIDX]) /* CPU status register */ | |
372 | #define FCR0IDX (71) | |
373 | #define FCR0 (REGISTERS[FCR0IDX]) /* really a 32bit register */ | |
374 | #define FCR31IDX (70) | |
375 | #define FCR31 (REGISTERS[FCR31IDX]) /* really a 32bit register */ | |
376 | #define FCSR (FCR31) | |
377 | #define Debug (REGISTERS[86]) | |
378 | #define DEPC (REGISTERS[87]) | |
379 | #define EPC (REGISTERS[88]) | |
380 | #define COCIDX (LAST_EMBED_REGNUM + 2) /* special case : outside the normal range */ | |
381 | ||
382 | /* The following are pseudonyms for standard registers */ | |
383 | #define ZERO (REGISTERS[0]) | |
384 | #define V0 (REGISTERS[2]) | |
385 | #define A0 (REGISTERS[4]) | |
386 | #define A1 (REGISTERS[5]) | |
387 | #define A2 (REGISTERS[6]) | |
388 | #define A3 (REGISTERS[7]) | |
389 | #define SP (REGISTERS[29]) | |
390 | #define RA (REGISTERS[31]) | |
391 | ||
392 | /* Keep the current format state for each register: */ | |
393 | FP_formats fpr_state[32]; | |
394 | #define FPR_STATE ((STATE_CPU (sd, 0))->fpr_state) | |
395 | ||
396 | ||
397 | /* Slots for delayed register updates. For the moment we just have a | |
398 | fixed number of slots (rather than a more generic, dynamic | |
399 | system). This keeps the simulator fast. However, we only allow | |
400 | for the register update to be delayed for a single instruction | |
401 | cycle. */ | |
402 | #define PSLOTS (5) /* Maximum number of instruction cycles */ | |
403 | int pending_in; | |
404 | int pending_out; | |
405 | int pending_total; | |
406 | int pending_slot_count[PSLOTS]; | |
407 | int pending_slot_reg[PSLOTS]; | |
408 | unsigned_word pending_slot_value[PSLOTS]; | |
409 | #define PENDING_IN ((STATE_CPU (sd, 0))->pending_in) | |
410 | #define PENDING_OUT ((STATE_CPU (sd, 0))->pending_out) | |
411 | #define PENDING_TOTAL ((STATE_CPU (sd, 0))->pending_total) | |
412 | #define PENDING_SLOT_COUNT ((STATE_CPU (sd, 0))->pending_slot_count) | |
413 | #define PENDING_SLOT_REG ((STATE_CPU (sd, 0))->pending_slot_reg) | |
414 | #define PENDING_SLOT_VALUE ((STATE_CPU (sd, 0))->pending_slot_value) | |
415 | ||
ea985d24 AC |
416 | /* The following are not used for MIPS IV onwards: */ |
417 | #define PENDING_FILL(r,v) {\ | |
418 | /* printf("DBG: FILL BEFORE pending_in = %d, pending_out = %d, pending_total = %d\n",PENDING_IN,PENDING_OUT,PENDING_TOTAL); */\ | |
419 | if (PENDING_SLOT_REG[PENDING_IN] != (LAST_EMBED_REGNUM + 1))\ | |
420 | sim_io_eprintf(sd,"Attempt to over-write pending value\n");\ | |
421 | PENDING_SLOT_COUNT[PENDING_IN] = 2;\ | |
422 | PENDING_SLOT_REG[PENDING_IN] = (r);\ | |
423 | PENDING_SLOT_VALUE[PENDING_IN] = (uword64)(v);\ | |
424 | /*printf("DBG: FILL reg %d value = 0x%s\n",(r),pr_addr(v));*/\ | |
425 | PENDING_TOTAL++;\ | |
426 | PENDING_IN++;\ | |
427 | if (PENDING_IN == PSLOTS)\ | |
428 | PENDING_IN = 0;\ | |
429 | /*printf("DBG: FILL AFTER pending_in = %d, pending_out = %d, pending_total = %d\n",PENDING_IN,PENDING_OUT,PENDING_TOTAL);*/\ | |
430 | } | |
431 | ||
0c2c5f61 AC |
432 | |
433 | /* LLBIT = Load-Linked bit. A bit of "virtual" state used by atomic | |
434 | read-write instructions. It is set when a linked load occurs. It | |
435 | is tested and cleared by the conditional store. It is cleared | |
436 | (during other CPU operations) when a store to the location would | |
437 | no longer be atomic. In particular, it is cleared by exception | |
438 | return instructions. */ | |
439 | int llbit; | |
440 | #define LLBIT ((STATE_CPU (sd, 0))->llbit) | |
441 | ||
442 | ||
443 | /* The HIACCESS and LOACCESS counts are used to ensure that | |
444 | corruptions caused by using the HI or LO register to close to a | |
445 | following operation are spotted. */ | |
446 | ||
447 | int hiaccess; | |
448 | int loaccess; | |
449 | #define HIACCESS ((STATE_CPU (sd, 0))->hiaccess) | |
450 | #define LOACCESS ((STATE_CPU (sd, 0))->loaccess) | |
451 | /* start-sanitize-r5900 */ | |
452 | int hi1access; | |
453 | int lo1access; | |
454 | #define HI1ACCESS ((STATE_CPU (sd, 0))->hi1access) | |
455 | #define LO1ACCESS ((STATE_CPU (sd, 0))->lo1access) | |
456 | /* end-sanitize-r5900 */ | |
457 | #if 1 | |
458 | /* The 4300 and a few other processors have interlocks on hi/lo | |
459 | register reads, and hence do not have this problem. To avoid | |
460 | spurious warnings, we just disable this always. */ | |
461 | #define CHECKHILO(s) | |
462 | #else | |
463 | unsigned_word HLPC; | |
464 | /* If either of the preceding two instructions have accessed the HI | |
465 | or LO registers, then the values they see should be | |
466 | undefined. However, to keep the simulator world simple, we just | |
467 | let them use the value read and raise a warning to notify the | |
468 | user: */ | |
469 | #define CHECKHILO(s) {\ | |
470 | if ((HIACCESS != 0) || (LOACCESS != 0)) \ | |
471 | sim_io_eprintf(sd,"%s over-writing HI and LO registers values (PC = 0x%s HLPC = 0x%s)\n",(s),pr_addr(PC),pr_addr(HLPC));\ | |
472 | } | |
0425cfb3 | 473 | /* start-sanitize-r5900 */ |
0c2c5f61 AC |
474 | #undef CHECKHILO |
475 | #define CHECKHILO(s) {\ | |
476 | if ((HIACCESS != 0) || (LOACCESS != 0) || (HI1ACCESS != 0) || (LO1ACCESS != 0))\ | |
477 | sim_io_eprintf(sd,"%s over-writing HI and LO registers values (PC = 0x%s HLPC = 0x%s)\n",(s),pr_addr(PC),pr_addr(HLPC));\ | |
478 | } | |
479 | /* end-sanitize-r5900 */ | |
480 | #endif | |
481 | ||
482 | ||
483 | /* start-sanitize-r5900 */ | |
484 | /* The R5900 has 128 bit registers, but the hi 64 bits are only | |
485 | touched by multimedia (MMI) instructions. The normal mips | |
486 | instructions just use the lower 64 bits. To avoid changing the | |
487 | older parts of the simulator to handle this weirdness, the high | |
488 | 64 bits of each register are kept in a separate array | |
489 | (registers1). The high 64 bits of any register are by convention | |
490 | refered by adding a '1' to the end of the normal register's name. | |
491 | So LO still refers to the low 64 bits of the LO register, LO1 | |
492 | refers to the high 64 bits of that same register. */ | |
493 | ||
494 | signed_word registers1[LAST_EMBED_REGNUM + 1]; | |
495 | #define REGISTERS1 ((STATE_CPU (sd, 0))->registers1) | |
496 | #define GPR1 (®ISTERS1[0]) | |
497 | #define LO1 (REGISTERS1[32]) | |
498 | #define HI1 (REGISTERS1[33]) | |
499 | #define REGISTER_SA (124) | |
500 | ||
501 | unsigned_word sa; /* the shift amount register */ | |
502 | #define SA ((STATE_CPU (sd, 0))->sa) | |
503 | ||
504 | /* end-sanitize-r5900 */ | |
01b9cd49 AC |
505 | /* start-sanitize-vr5400 */ |
506 | ||
507 | /* end-sanitize-vr5400 */ | |
0c2c5f61 AC |
508 | |
509 | ||
510 | ||
18c64df6 AC |
511 | sim_cpu_base base; |
512 | }; | |
513 | ||
514 | ||
515 | /* MIPS specific simulator watch config */ | |
516 | ||
517 | void watch_options_install PARAMS ((SIM_DESC sd)); | |
518 | ||
519 | struct swatch { | |
520 | sim_event *pc; | |
521 | sim_event *clock; | |
522 | sim_event *cycles; | |
523 | }; | |
524 | ||
525 | ||
526 | /* FIXME: At present much of the simulator is still static */ | |
527 | struct sim_state { | |
528 | ||
529 | struct swatch watch; | |
530 | ||
531 | sim_cpu cpu[1]; | |
532 | #if (WITH_SMP) | |
533 | #define STATE_CPU(sd,n) (&(sd)->cpu[n]) | |
534 | #else | |
535 | #define STATE_CPU(sd,n) (&(sd)->cpu[0]) | |
536 | #endif | |
537 | ||
538 | sim_state_base base; | |
539 | }; | |
540 | ||
541 | ||
ea985d24 AC |
542 | |
543 | /* Status information: */ | |
544 | ||
545 | /* TODO : these should be the bitmasks for these bits within the | |
546 | status register. At the moment the following are VR4300 | |
547 | bit-positions: */ | |
548 | #define status_KSU_mask (0x3) /* mask for KSU bits */ | |
549 | #define status_KSU_shift (3) /* shift for field */ | |
550 | #define ksu_kernel (0x0) | |
551 | #define ksu_supervisor (0x1) | |
552 | #define ksu_user (0x2) | |
553 | #define ksu_unknown (0x3) | |
554 | ||
555 | #define status_IE (1 << 0) /* Interrupt enable */ | |
556 | #define status_EXL (1 << 1) /* Exception level */ | |
557 | #define status_RE (1 << 25) /* Reverse Endian in user mode */ | |
558 | #define status_FR (1 << 26) /* enables MIPS III additional FP registers */ | |
559 | #define status_SR (1 << 20) /* soft reset or NMI */ | |
560 | #define status_BEV (1 << 22) /* Location of general exception vectors */ | |
561 | #define status_TS (1 << 21) /* TLB shutdown has occurred */ | |
562 | #define status_ERL (1 << 2) /* Error level */ | |
563 | #define status_RP (1 << 27) /* Reduced Power mode */ | |
564 | ||
565 | #define cause_BD ((unsigned)1 << 31) /* Exception in branch delay slot */ | |
566 | ||
567 | /* NOTE: We keep the following status flags as bit values (1 for true, | |
568 | 0 for false). This allows them to be used in binary boolean | |
569 | operations without worrying about what exactly the non-zero true | |
570 | value is. */ | |
571 | ||
572 | /* UserMode */ | |
573 | #define UserMode ((((SR & status_KSU_mask) >> status_KSU_shift) == ksu_user) ? 1 : 0) | |
574 | ||
575 | /* BigEndianMem */ | |
576 | /* Hardware configuration. Affects endianness of LoadMemory and | |
577 | StoreMemory and the endianness of Kernel and Supervisor mode | |
578 | execution. The value is 0 for little-endian; 1 for big-endian. */ | |
579 | #define BigEndianMem (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN) | |
580 | /*(state & simBE) ? 1 : 0)*/ | |
581 | ||
582 | /* ByteSwapMem */ | |
583 | /* This is true if the host and target have different endianness. */ | |
584 | #define ByteSwapMem (CURRENT_TARGET_BYTE_ORDER != CURRENT_HOST_BYTE_ORDER) | |
585 | ||
586 | /* ReverseEndian */ | |
587 | /* This mode is selected if in User mode with the RE bit being set in | |
588 | SR (Status Register). It reverses the endianness of load and store | |
589 | instructions. */ | |
590 | #define ReverseEndian (((SR & status_RE) && UserMode) ? 1 : 0) | |
591 | ||
592 | /* BigEndianCPU */ | |
593 | /* The endianness for load and store instructions (0=little;1=big). In | |
594 | User mode this endianness may be switched by setting the state_RE | |
595 | bit in the SR register. Thus, BigEndianCPU may be computed as | |
596 | (BigEndianMem EOR ReverseEndian). */ | |
597 | #define BigEndianCPU (BigEndianMem ^ ReverseEndian) /* Already bits */ | |
598 | ||
599 | ||
600 | ||
18c64df6 AC |
601 | /* Exceptions: */ |
602 | ||
603 | /* NOTE: These numbers depend on the processor architecture being | |
604 | simulated: */ | |
605 | #define Interrupt (0) | |
606 | #define TLBModification (1) | |
607 | #define TLBLoad (2) | |
608 | #define TLBStore (3) | |
609 | #define AddressLoad (4) | |
610 | #define AddressStore (5) | |
611 | #define InstructionFetch (6) | |
612 | #define DataReference (7) | |
613 | #define SystemCall (8) | |
614 | #define BreakPoint (9) | |
615 | #define ReservedInstruction (10) | |
616 | #define CoProcessorUnusable (11) | |
617 | #define IntegerOverflow (12) /* Arithmetic overflow (IDT monitor raises SIGFPE) */ | |
618 | #define Trap (13) | |
619 | #define FPE (15) | |
620 | #define DebugBreakPoint (16) | |
621 | #define Watch (23) | |
622 | ||
623 | /* The following exception code is actually private to the simulator | |
624 | world. It is *NOT* a processor feature, and is used to signal | |
625 | run-time errors in the simulator. */ | |
626 | #define SimulatorFault (0xFFFFFFFF) | |
627 | ||
628 | void signal_exception (SIM_DESC sd, int exception, ...); | |
629 | #define SignalException(exc,instruction) signal_exception (sd, (exc), (instruction)) | |
630 | #define SignalExceptionInterrupt() signal_exception (sd, Interrupt) | |
631 | #define SignalExceptionInstructionFetch() signal_exception (sd, InstructionFetch) | |
632 | #define SignalExceptionAddressStore() signal_exception (sd, AddressStore) | |
633 | #define SignalExceptionAddressLoad() signal_exception (sd, AddressLoad) | |
634 | #define SignalExceptionSimulatorFault(buf) signal_exception (sd, SimulatorFault, buf) | |
635 | #define SignalExceptionFPE() signal_exception (sd, FPE) | |
636 | #define SignalExceptionIntegerOverflow() signal_exception (sd, IntegerOverflow) | |
637 | #define SignalExceptionCoProcessorUnusable() signal_exception (sd, CoProcessorUnusable) | |
638 | ||
639 | ||
18c64df6 AC |
640 | /* Co-processor accesses */ |
641 | ||
642 | void cop_lw PARAMS ((SIM_DESC sd, int coproc_num, int coproc_reg, unsigned int memword)); | |
643 | void cop_ld PARAMS ((SIM_DESC sd, int coproc_num, int coproc_reg, uword64 memword)); | |
644 | unsigned int cop_sw PARAMS ((SIM_DESC sd, int coproc_num, int coproc_reg)); | |
645 | uword64 cop_sd PARAMS ((SIM_DESC sd, int coproc_num, int coproc_reg)); | |
646 | ||
647 | #define COP_LW(coproc_num,coproc_reg,memword) cop_lw(sd,coproc_num,coproc_reg,memword) | |
648 | #define COP_LD(coproc_num,coproc_reg,memword) cop_ld(sd,coproc_num,coproc_reg,memword) | |
649 | #define COP_SW(coproc_num,coproc_reg) cop_sw(sd,coproc_num,coproc_reg) | |
650 | #define COP_SD(coproc_num,coproc_reg) cop_sd(sd,coproc_num,coproc_reg) | |
651 | ||
ea985d24 AC |
652 | void decode_coproc PARAMS ((SIM_DESC sd,unsigned int instruction)); |
653 | #define DecodeCoproc(instruction) decode_coproc(sd, (instruction)) | |
654 | ||
18c64df6 AC |
655 | |
656 | ||
657 | /* Memory accesses */ | |
658 | ||
ea985d24 AC |
659 | /* The following are generic to all versions of the MIPS architecture |
660 | to date: */ | |
661 | ||
662 | /* Memory Access Types (for CCA): */ | |
663 | #define Uncached (0) | |
664 | #define CachedNoncoherent (1) | |
665 | #define CachedCoherent (2) | |
666 | #define Cached (3) | |
667 | ||
668 | #define isINSTRUCTION (1 == 0) /* FALSE */ | |
669 | #define isDATA (1 == 1) /* TRUE */ | |
670 | #define isLOAD (1 == 0) /* FALSE */ | |
671 | #define isSTORE (1 == 1) /* TRUE */ | |
672 | #define isREAL (1 == 0) /* FALSE */ | |
673 | #define isRAW (1 == 1) /* TRUE */ | |
525d929e | 674 | /* The parameter HOST (isTARGET / isHOST) is ignored */ |
ea985d24 | 675 | #define isTARGET (1 == 0) /* FALSE */ |
525d929e | 676 | /* #define isHOST (1 == 1) TRUE */ |
ea985d24 AC |
677 | |
678 | /* The "AccessLength" specifications for Loads and Stores. NOTE: This | |
679 | is the number of bytes minus 1. */ | |
680 | #define AccessLength_BYTE (0) | |
681 | #define AccessLength_HALFWORD (1) | |
682 | #define AccessLength_TRIPLEBYTE (2) | |
683 | #define AccessLength_WORD (3) | |
684 | #define AccessLength_QUINTIBYTE (4) | |
685 | #define AccessLength_SEXTIBYTE (5) | |
686 | #define AccessLength_SEPTIBYTE (6) | |
687 | #define AccessLength_DOUBLEWORD (7) | |
688 | #define AccessLength_QUADWORD (15) | |
689 | ||
525d929e | 690 | int address_translation PARAMS ((SIM_DESC sd, address_word vAddr, int IorD, int LorS, address_word *pAddr, int *CCA, int raw)); |
18c64df6 | 691 | #define AddressTranslation(vAddr,IorD,LorS,pAddr,CCA,host,raw) \ |
525d929e | 692 | address_translation(sd, vAddr,IorD,LorS,pAddr,CCA,raw) |
18c64df6 | 693 | |
dad6f1f3 | 694 | void load_memory PARAMS ((SIM_DESC sd, uword64* memvalp, uword64* memval1p, int CCA, int AccessLength, address_word pAddr, address_word vAddr, int IorD, int raw)); |
18c64df6 AC |
695 | #define LoadMemory(memvalp,memval1p,CCA,AccessLength,pAddr,vAddr,IorD,raw) \ |
696 | load_memory(sd,memvalp,memval1p,CCA,AccessLength,pAddr,vAddr,IorD,raw) | |
697 | ||
dad6f1f3 | 698 | void store_memory PARAMS ((SIM_DESC sd, int CCA, int AccessLength, uword64 MemElem, uword64 MemElem1, address_word pAddr, address_word vAddr, int raw)); |
18c64df6 AC |
699 | #define StoreMemory(CCA,AccessLength,MemElem,MemElem1,pAddr,vAddr,raw) \ |
700 | store_memory(sd,CCA,AccessLength,MemElem,MemElem1,pAddr,vAddr,raw) | |
701 | ||
dad6f1f3 | 702 | void cache_op PARAMS ((SIM_DESC sd, int op, address_word pAddr, address_word vAddr, unsigned int instruction)); |
18c64df6 AC |
703 | #define CacheOp(op,pAddr,vAddr,instruction) cache_op(sd,op,pAddr,vAddr,instruction) |
704 | ||
ea985d24 AC |
705 | void sync_operation PARAMS ((SIM_DESC sd, int stype)); |
706 | #define SyncOperation(stype) sync_operation (sd, (stype)) | |
707 | ||
dad6f1f3 | 708 | void prefetch PARAMS ((SIM_DESC sd, int CCA, address_word pAddr, address_word vAddr, int DATA, int hint)); |
ea985d24 AC |
709 | #define Prefetch(CCA,pAddr,vAddr,DATA,hint) prefetch(sd,CCA,pAddr,vAddr,DATA,hint) |
710 | ||
dad6f1f3 AC |
711 | unsigned32 ifetch32 PARAMS ((SIM_DESC sd, address_word cia)); |
712 | #define IMEM(CIA) ifetch32 (SD, (CIA)) | |
ea985d24 AC |
713 | |
714 | ||
18c64df6 | 715 | #endif |