1 /* armvirt.c -- ARMulator virtual memory interace: ARM6 Instruction Emulator.
2 Copyright (C) 1994 Advanced RISC Machines Ltd.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* This file contains a complete ARMulator memory model, modelling a
19 "virtual memory" system. A much simpler model can be found in armfast.c,
20 and that model goes faster too, but has a fixed amount of memory. This
21 model's memory has 64K pages, allocated on demand from a 64K entry page
22 table. The routines PutWord and GetWord implement this. Pages are never
23 freed as they might be needed again. A single area of memory may be
24 defined to generate aborts. */
30 #ifdef VALIDATE /* for running the validate suite */
31 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
37 #ifdef ABORTS /* the memory system will abort */
38 /* For the old test suite Abort between 32 Kbytes and 32 Mbytes
39 For the new test suite Abort between 8 Mbytes and 26 Mbytes */
40 /* #define LOWABORT 32 * 1024
41 #define HIGHABORT 32 * 1024 * 1024 */
42 #define LOWABORT 8 * 1024 * 1024
43 #define HIGHABORT 26 * 1024 * 1024
47 #define NUMPAGES 64 * 1024
48 #define PAGESIZE 64 * 1024
50 #define OFFSETBITS 0xffff
52 /***************************************************************************\
53 * Get a Word from Virtual Memory, maybe allocating the page *
54 \***************************************************************************/
57 GetWord (ARMul_State
* state
, ARMword address
)
64 page
= address
>> PAGEBITS
;
65 offset
= (address
& OFFSETBITS
) >> 2;
66 pagetable
= (ARMword
**) state
->MemDataPtr
;
67 pageptr
= *(pagetable
+ page
);
71 pageptr
= (ARMword
*) malloc (PAGESIZE
);
75 perror ("ARMulator can't allocate VM page");
79 *(pagetable
+ page
) = pageptr
;
82 return *(pageptr
+ offset
);
85 /***************************************************************************\
86 * Put a Word into Virtual Memory, maybe allocating the page *
87 \***************************************************************************/
90 PutWord (ARMul_State
* state
, ARMword address
, ARMword data
)
97 page
= address
>> PAGEBITS
;
98 offset
= (address
& OFFSETBITS
) >> 2;
99 pagetable
= (ARMword
**) state
->MemDataPtr
;
100 pageptr
= *(pagetable
+ page
);
104 pageptr
= (ARMword
*) malloc (PAGESIZE
);
107 perror ("ARMulator can't allocate VM page");
111 *(pagetable
+ page
) = pageptr
;
114 *(pageptr
+ offset
) = data
;
117 /***************************************************************************\
118 * Initialise the memory interface *
119 \***************************************************************************/
122 ARMul_MemoryInit (ARMul_State
* state
, unsigned long initmemsize
)
128 state
->MemSize
= initmemsize
;
130 pagetable
= (ARMword
**) malloc (sizeof (ARMword
) * NUMPAGES
);
132 if (pagetable
== NULL
)
135 for (page
= 0; page
< NUMPAGES
; page
++)
136 *(pagetable
+ page
) = NULL
;
138 state
->MemDataPtr
= (unsigned char *) pagetable
;
140 ARMul_ConsolePrint (state
, ", 4 Gb memory");
145 /***************************************************************************\
146 * Remove the memory interface *
147 \***************************************************************************/
150 ARMul_MemoryExit (ARMul_State
* state
)
156 pagetable
= (ARMword
**) state
->MemDataPtr
;
157 for (page
= 0; page
< NUMPAGES
; page
++)
159 pageptr
= *(pagetable
+ page
);
161 free ((char *) pageptr
);
163 free ((char *) pagetable
);
167 /***************************************************************************\
168 * ReLoad Instruction *
169 \***************************************************************************/
172 ARMul_ReLoadInstr (ARMul_State
* state
, ARMword address
, ARMword isize
)
175 if (address
>= LOWABORT
&& address
< HIGHABORT
)
177 ARMul_PREFETCHABORT (address
);
178 return ARMul_ABORTWORD
;
186 if ((isize
== 2) && (address
& 0x2))
188 /* We return the next two halfwords: */
189 ARMword lo
= GetWord (state
, address
);
190 ARMword hi
= GetWord (state
, address
+ 4);
192 if (state
->bigendSig
== HIGH
)
193 return (lo
<< 16) | (hi
>> 16);
195 return ((hi
& 0xFFFF) << 16) | (lo
>> 16);
198 return GetWord (state
, address
);
201 /***************************************************************************\
202 * Load Instruction, Sequential Cycle *
203 \***************************************************************************/
205 ARMword
ARMul_LoadInstrS (ARMul_State
* state
, ARMword address
, ARMword isize
)
210 if ((state
->NumScycles
& HOURGLASS_RATE
) == 0)
216 return ARMul_ReLoadInstr (state
, address
, isize
);
219 /***************************************************************************\
220 * Load Instruction, Non Sequential Cycle *
221 \***************************************************************************/
223 ARMword
ARMul_LoadInstrN (ARMul_State
* state
, ARMword address
, ARMword isize
)
227 return ARMul_ReLoadInstr (state
, address
, isize
);
230 /***************************************************************************\
231 * Read Word (but don't tell anyone!) *
232 \***************************************************************************/
234 ARMword
ARMul_ReadWord (ARMul_State
* state
, ARMword address
)
237 if (address
>= LOWABORT
&& address
< HIGHABORT
)
239 ARMul_DATAABORT (address
);
240 return ARMul_ABORTWORD
;
248 return GetWord (state
, address
);
251 /***************************************************************************\
252 * Load Word, Sequential Cycle *
253 \***************************************************************************/
255 ARMword
ARMul_LoadWordS (ARMul_State
* state
, ARMword address
)
259 return ARMul_ReadWord (state
, address
);
262 /***************************************************************************\
263 * Load Word, Non Sequential Cycle *
264 \***************************************************************************/
266 ARMword
ARMul_LoadWordN (ARMul_State
* state
, ARMword address
)
270 return ARMul_ReadWord (state
, address
);
273 /***************************************************************************\
274 * Load Halfword, (Non Sequential Cycle) *
275 \***************************************************************************/
277 ARMword
ARMul_LoadHalfWord (ARMul_State
* state
, ARMword address
)
279 ARMword temp
, offset
;
283 temp
= ARMul_ReadWord (state
, address
);
284 offset
= (((ARMword
) state
->bigendSig
* 2) ^ (address
& 2)) << 3; /* bit offset into the word */
286 return (temp
>> offset
) & 0xffff;
289 /***************************************************************************\
290 * Read Byte (but don't tell anyone!) *
291 \***************************************************************************/
293 ARMword
ARMul_ReadByte (ARMul_State
* state
, ARMword address
)
295 ARMword temp
, offset
;
297 temp
= ARMul_ReadWord (state
, address
);
298 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3; /* bit offset into the word */
300 return (temp
>> offset
& 0xffL
);
303 /***************************************************************************\
304 * Load Byte, (Non Sequential Cycle) *
305 \***************************************************************************/
307 ARMword
ARMul_LoadByte (ARMul_State
* state
, ARMword address
)
311 return ARMul_ReadByte (state
, address
);
314 /***************************************************************************\
315 * Write Word (but don't tell anyone!) *
316 \***************************************************************************/
319 ARMul_WriteWord (ARMul_State
* state
, ARMword address
, ARMword data
)
322 if (address
>= LOWABORT
&& address
< HIGHABORT
)
324 ARMul_DATAABORT (address
);
333 PutWord (state
, address
, data
);
336 /***************************************************************************\
337 * Store Word, Sequential Cycle *
338 \***************************************************************************/
341 ARMul_StoreWordS (ARMul_State
* state
, ARMword address
, ARMword data
)
345 ARMul_WriteWord (state
, address
, data
);
348 /***************************************************************************\
349 * Store Word, Non Sequential Cycle *
350 \***************************************************************************/
353 ARMul_StoreWordN (ARMul_State
* state
, ARMword address
, ARMword data
)
357 ARMul_WriteWord (state
, address
, data
);
360 /***************************************************************************\
361 * Store HalfWord, (Non Sequential Cycle) *
362 \***************************************************************************/
365 ARMul_StoreHalfWord (ARMul_State
* state
, ARMword address
, ARMword data
)
367 ARMword temp
, offset
;
375 state
->Emulate
= FALSE
;
377 (void) putc ((char) data
, stderr
); /* Write Char */
382 temp
= ARMul_ReadWord (state
, address
);
383 offset
= (((ARMword
) state
->bigendSig
* 2) ^ (address
& 2)) << 3; /* bit offset into the word */
385 PutWord (state
, address
,
386 (temp
& ~(0xffffL
<< offset
)) | ((data
& 0xffffL
) << offset
));
389 /***************************************************************************\
390 * Write Byte (but don't tell anyone!) *
391 \***************************************************************************/
394 ARMul_WriteByte (ARMul_State
* state
, ARMword address
, ARMword data
)
396 ARMword temp
, offset
;
398 temp
= ARMul_ReadWord (state
, address
);
399 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3; /* bit offset into the word */
401 PutWord (state
, address
,
402 (temp
& ~(0xffL
<< offset
)) | ((data
& 0xffL
) << offset
));
405 /***************************************************************************\
406 * Store Byte, (Non Sequential Cycle) *
407 \***************************************************************************/
410 ARMul_StoreByte (ARMul_State
* state
, ARMword address
, ARMword data
)
418 state
->Emulate
= FALSE
;
420 (void) putc ((char) data
, stderr
); /* Write Char */
425 ARMul_WriteByte (state
, address
, data
);
428 /***************************************************************************\
429 * Swap Word, (Two Non Sequential Cycles) *
430 \***************************************************************************/
432 ARMword
ARMul_SwapWord (ARMul_State
* state
, ARMword address
, ARMword data
)
438 temp
= ARMul_ReadWord (state
, address
);
442 PutWord (state
, address
, data
);
447 /***************************************************************************\
448 * Swap Byte, (Two Non Sequential Cycles) *
449 \***************************************************************************/
451 ARMword
ARMul_SwapByte (ARMul_State
* state
, ARMword address
, ARMword data
)
455 temp
= ARMul_LoadByte (state
, address
);
456 ARMul_StoreByte (state
, address
, data
);
461 /***************************************************************************\
463 \***************************************************************************/
466 ARMul_Icycles (ARMul_State
* state
, unsigned number
, ARMword address ATTRIBUTE_UNUSED
)
468 state
->NumIcycles
+= number
;
472 /***************************************************************************\
474 \***************************************************************************/
477 ARMul_Ccycles (ARMul_State
* state
, unsigned number
, ARMword address ATTRIBUTE_UNUSED
)
479 state
->NumCcycles
+= number
;