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 3 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, see <http://www.gnu.org/licenses/>. */
17 /* This file contains a complete ARMulator memory model, modelling a
18 "virtual memory" system. A much simpler model can be found in armfast.c,
19 and that model goes faster too, but has a fixed amount of memory. This
20 model's memory has 64K pages, allocated on demand from a 64K entry page
21 table. The routines PutWord and GetWord implement this. Pages are never
22 freed as they might be needed again. A single area of memory may be
23 defined to generate aborts. */
29 #ifdef VALIDATE /* for running the validate suite */
30 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
36 #ifdef ABORTS /* the memory system will abort */
37 /* For the old test suite Abort between 32 Kbytes and 32 Mbytes
38 For the new test suite Abort between 8 Mbytes and 26 Mbytes */
39 /* #define LOWABORT 32 * 1024
40 #define HIGHABORT 32 * 1024 * 1024 */
41 #define LOWABORT 8 * 1024 * 1024
42 #define HIGHABORT 26 * 1024 * 1024
46 #define NUMPAGES 64 * 1024
47 #define PAGESIZE 64 * 1024
49 #define OFFSETBITS 0xffff
51 int SWI_vector_installed
= FALSE
;
53 /***************************************************************************\
54 * Get a Word from Virtual Memory, maybe allocating the page *
55 \***************************************************************************/
58 GetWord (ARMul_State
* state
, ARMword address
, int check
)
65 if (check
&& state
->is_XScale
)
66 XScale_check_memacc (state
, &address
, 0);
68 page
= address
>> PAGEBITS
;
69 offset
= (address
& OFFSETBITS
) >> 2;
70 pagetable
= (ARMword
**) state
->MemDataPtr
;
71 pageptr
= *(pagetable
+ page
);
75 pageptr
= (ARMword
*) malloc (PAGESIZE
);
79 perror ("ARMulator can't allocate VM page");
83 *(pagetable
+ page
) = pageptr
;
86 return *(pageptr
+ offset
);
89 /***************************************************************************\
90 * Put a Word into Virtual Memory, maybe allocating the page *
91 \***************************************************************************/
94 PutWord (ARMul_State
* state
, ARMword address
, ARMword data
, int check
)
101 if (check
&& state
->is_XScale
)
102 XScale_check_memacc (state
, &address
, 1);
104 page
= address
>> PAGEBITS
;
105 offset
= (address
& OFFSETBITS
) >> 2;
106 pagetable
= (ARMword
**) state
->MemDataPtr
;
107 pageptr
= *(pagetable
+ page
);
111 pageptr
= (ARMword
*) malloc (PAGESIZE
);
114 perror ("ARMulator can't allocate VM page");
118 *(pagetable
+ page
) = pageptr
;
122 SWI_vector_installed
= TRUE
;
124 *(pageptr
+ offset
) = data
;
127 /***************************************************************************\
128 * Initialise the memory interface *
129 \***************************************************************************/
132 ARMul_MemoryInit (ARMul_State
* state
, unsigned long initmemsize
)
138 state
->MemSize
= initmemsize
;
140 pagetable
= (ARMword
**) malloc (sizeof (ARMword
*) * NUMPAGES
);
142 if (pagetable
== NULL
)
145 for (page
= 0; page
< NUMPAGES
; page
++)
146 *(pagetable
+ page
) = NULL
;
148 state
->MemDataPtr
= (unsigned char *) pagetable
;
150 ARMul_ConsolePrint (state
, ", 4 Gb memory");
155 /***************************************************************************\
156 * Remove the memory interface *
157 \***************************************************************************/
160 ARMul_MemoryExit (ARMul_State
* state
)
166 pagetable
= (ARMword
**) state
->MemDataPtr
;
167 for (page
= 0; page
< NUMPAGES
; page
++)
169 pageptr
= *(pagetable
+ page
);
171 free ((char *) pageptr
);
173 free ((char *) pagetable
);
177 /***************************************************************************\
178 * ReLoad Instruction *
179 \***************************************************************************/
182 ARMul_ReLoadInstr (ARMul_State
* state
, ARMword address
, ARMword isize
)
185 if (address
>= LOWABORT
&& address
< HIGHABORT
)
187 ARMul_PREFETCHABORT (address
);
188 return ARMul_ABORTWORD
;
196 if ((isize
== 2) && (address
& 0x2))
198 /* We return the next two halfwords: */
199 ARMword lo
= GetWord (state
, address
, FALSE
);
200 ARMword hi
= GetWord (state
, address
+ 4, FALSE
);
202 if (state
->bigendSig
== HIGH
)
203 return (lo
<< 16) | (hi
>> 16);
205 return ((hi
& 0xFFFF) << 16) | (lo
>> 16);
208 return GetWord (state
, address
, TRUE
);
211 /***************************************************************************\
212 * Load Instruction, Sequential Cycle *
213 \***************************************************************************/
215 ARMword
ARMul_LoadInstrS (ARMul_State
* state
, ARMword address
, ARMword isize
)
219 return ARMul_ReLoadInstr (state
, address
, isize
);
222 /***************************************************************************\
223 * Load Instruction, Non Sequential Cycle *
224 \***************************************************************************/
226 ARMword
ARMul_LoadInstrN (ARMul_State
* state
, ARMword address
, ARMword isize
)
230 return ARMul_ReLoadInstr (state
, address
, isize
);
233 /***************************************************************************\
234 * Read Word (but don't tell anyone!) *
235 \***************************************************************************/
237 ARMword
ARMul_ReadWord (ARMul_State
* state
, ARMword address
)
240 if (address
>= LOWABORT
&& address
< HIGHABORT
)
242 ARMul_DATAABORT (address
);
243 return ARMul_ABORTWORD
;
251 return GetWord (state
, address
, TRUE
);
254 /***************************************************************************\
255 * Load Word, Sequential Cycle *
256 \***************************************************************************/
258 ARMword
ARMul_LoadWordS (ARMul_State
* state
, ARMword address
)
262 return ARMul_ReadWord (state
, address
);
265 /***************************************************************************\
266 * Load Word, Non Sequential Cycle *
267 \***************************************************************************/
269 ARMword
ARMul_LoadWordN (ARMul_State
* state
, ARMword address
)
273 return ARMul_ReadWord (state
, address
);
276 /***************************************************************************\
277 * Load Halfword, (Non Sequential Cycle) *
278 \***************************************************************************/
280 ARMword
ARMul_LoadHalfWord (ARMul_State
* state
, ARMword address
)
282 ARMword temp
, offset
;
286 temp
= ARMul_ReadWord (state
, address
);
287 offset
= (((ARMword
) state
->bigendSig
* 2) ^ (address
& 2)) << 3; /* bit offset into the word */
289 return (temp
>> offset
) & 0xffff;
292 /***************************************************************************\
293 * Read Byte (but don't tell anyone!) *
294 \***************************************************************************/
296 ARMword
ARMul_ReadByte (ARMul_State
* state
, ARMword address
)
298 ARMword temp
, offset
;
300 temp
= ARMul_ReadWord (state
, address
);
301 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3; /* bit offset into the word */
303 return (temp
>> offset
& 0xffL
);
306 /***************************************************************************\
307 * Load Byte, (Non Sequential Cycle) *
308 \***************************************************************************/
310 ARMword
ARMul_LoadByte (ARMul_State
* state
, ARMword address
)
314 return ARMul_ReadByte (state
, address
);
317 /***************************************************************************\
318 * Write Word (but don't tell anyone!) *
319 \***************************************************************************/
322 ARMul_WriteWord (ARMul_State
* state
, ARMword address
, ARMword data
)
325 if (address
>= LOWABORT
&& address
< HIGHABORT
)
327 ARMul_DATAABORT (address
);
336 PutWord (state
, address
, data
, TRUE
);
339 /***************************************************************************\
340 * Store Word, Sequential Cycle *
341 \***************************************************************************/
344 ARMul_StoreWordS (ARMul_State
* state
, ARMword address
, ARMword data
)
348 ARMul_WriteWord (state
, address
, data
);
351 /***************************************************************************\
352 * Store Word, Non Sequential Cycle *
353 \***************************************************************************/
356 ARMul_StoreWordN (ARMul_State
* state
, ARMword address
, ARMword data
)
360 ARMul_WriteWord (state
, address
, data
);
363 /***************************************************************************\
364 * Store HalfWord, (Non Sequential Cycle) *
365 \***************************************************************************/
368 ARMul_StoreHalfWord (ARMul_State
* state
, ARMword address
, ARMword data
)
370 ARMword temp
, offset
;
378 state
->Emulate
= FALSE
;
380 (void) putc ((char) data
, stderr
); /* Write Char */
385 temp
= ARMul_ReadWord (state
, address
);
386 offset
= (((ARMword
) state
->bigendSig
* 2) ^ (address
& 2)) << 3; /* bit offset into the word */
388 PutWord (state
, address
,
389 (temp
& ~(0xffffL
<< offset
)) | ((data
& 0xffffL
) << offset
),
393 /***************************************************************************\
394 * Write Byte (but don't tell anyone!) *
395 \***************************************************************************/
398 ARMul_WriteByte (ARMul_State
* state
, ARMword address
, ARMword data
)
400 ARMword temp
, offset
;
402 temp
= ARMul_ReadWord (state
, address
);
403 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3; /* bit offset into the word */
405 PutWord (state
, address
,
406 (temp
& ~(0xffL
<< offset
)) | ((data
& 0xffL
) << offset
),
410 /***************************************************************************\
411 * Store Byte, (Non Sequential Cycle) *
412 \***************************************************************************/
415 ARMul_StoreByte (ARMul_State
* state
, ARMword address
, ARMword data
)
423 state
->Emulate
= FALSE
;
425 (void) putc ((char) data
, stderr
); /* Write Char */
430 ARMul_WriteByte (state
, address
, data
);
433 /***************************************************************************\
434 * Swap Word, (Two Non Sequential Cycles) *
435 \***************************************************************************/
437 ARMword
ARMul_SwapWord (ARMul_State
* state
, ARMword address
, ARMword data
)
443 temp
= ARMul_ReadWord (state
, address
);
447 PutWord (state
, address
, data
, TRUE
);
452 /***************************************************************************\
453 * Swap Byte, (Two Non Sequential Cycles) *
454 \***************************************************************************/
456 ARMword
ARMul_SwapByte (ARMul_State
* state
, ARMword address
, ARMword data
)
460 temp
= ARMul_LoadByte (state
, address
);
461 ARMul_StoreByte (state
, address
, data
);
466 /***************************************************************************\
468 \***************************************************************************/
471 ARMul_Icycles (ARMul_State
* state
, unsigned number
, ARMword address ATTRIBUTE_UNUSED
)
473 state
->NumIcycles
+= number
;
477 /***************************************************************************\
479 \***************************************************************************/
482 ARMul_Ccycles (ARMul_State
* state
, unsigned number
, ARMword address ATTRIBUTE_UNUSED
)
484 state
->NumCcycles
+= number
;
489 /* Read a byte. Do not check for alignment or access errors. */
492 ARMul_SafeReadByte (ARMul_State
* state
, ARMword address
)
494 ARMword temp
, offset
;
496 temp
= GetWord (state
, address
, FALSE
);
497 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3;
499 return (temp
>> offset
& 0xffL
);
503 ARMul_SafeWriteByte (ARMul_State
* state
, ARMword address
, ARMword data
)
505 ARMword temp
, offset
;
507 temp
= GetWord (state
, address
, FALSE
);
508 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3;
510 PutWord (state
, address
,
511 (temp
& ~(0xffL
<< offset
)) | ((data
& 0xffL
) << offset
),