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. */
25 /* This must come before any other includes. */
32 #ifdef VALIDATE /* for running the validate suite */
33 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
39 #ifdef ABORTS /* the memory system will abort */
40 /* For the old test suite Abort between 32 Kbytes and 32 Mbytes
41 For the new test suite Abort between 8 Mbytes and 26 Mbytes */
42 /* #define LOWABORT 32 * 1024
43 #define HIGHABORT 32 * 1024 * 1024 */
44 #define LOWABORT 8 * 1024 * 1024
45 #define HIGHABORT 26 * 1024 * 1024
49 #define NUMPAGES 64 * 1024
50 #define PAGESIZE 64 * 1024
52 #define OFFSETBITS 0xffff
54 int SWI_vector_installed
= FALSE
;
56 /***************************************************************************\
57 * Get a Word from Virtual Memory, maybe allocating the page *
58 \***************************************************************************/
61 GetWord (ARMul_State
* state
, ARMword address
, int check
)
68 if (check
&& state
->is_XScale
)
69 XScale_check_memacc (state
, &address
, 0);
71 page
= address
>> PAGEBITS
;
72 offset
= (address
& OFFSETBITS
) >> 2;
73 pagetable
= (ARMword
**) state
->MemDataPtr
;
74 pageptr
= *(pagetable
+ page
);
78 pageptr
= (ARMword
*) malloc (PAGESIZE
);
82 perror ("ARMulator can't allocate VM page");
86 *(pagetable
+ page
) = pageptr
;
89 return *(pageptr
+ offset
);
92 /***************************************************************************\
93 * Put a Word into Virtual Memory, maybe allocating the page *
94 \***************************************************************************/
97 PutWord (ARMul_State
* state
, ARMword address
, ARMword data
, int check
)
104 if (check
&& state
->is_XScale
)
105 XScale_check_memacc (state
, &address
, 1);
107 page
= address
>> PAGEBITS
;
108 offset
= (address
& OFFSETBITS
) >> 2;
109 pagetable
= (ARMword
**) state
->MemDataPtr
;
110 pageptr
= *(pagetable
+ page
);
114 pageptr
= (ARMword
*) malloc (PAGESIZE
);
117 perror ("ARMulator can't allocate VM page");
121 *(pagetable
+ page
) = pageptr
;
125 SWI_vector_installed
= TRUE
;
127 *(pageptr
+ offset
) = data
;
130 /***************************************************************************\
131 * Initialise the memory interface *
132 \***************************************************************************/
135 ARMul_MemoryInit (ARMul_State
* state
, unsigned long initmemsize
)
141 state
->MemSize
= initmemsize
;
143 pagetable
= (ARMword
**) malloc (sizeof (ARMword
*) * NUMPAGES
);
145 if (pagetable
== NULL
)
148 for (page
= 0; page
< NUMPAGES
; page
++)
149 *(pagetable
+ page
) = NULL
;
151 state
->MemDataPtr
= (unsigned char *) pagetable
;
153 ARMul_ConsolePrint (state
, ", 4 Gb memory");
158 /***************************************************************************\
159 * Remove the memory interface *
160 \***************************************************************************/
163 ARMul_MemoryExit (ARMul_State
* state
)
169 pagetable
= (ARMword
**) state
->MemDataPtr
;
170 for (page
= 0; page
< NUMPAGES
; page
++)
172 pageptr
= *(pagetable
+ page
);
174 free ((char *) pageptr
);
176 free ((char *) pagetable
);
180 /***************************************************************************\
181 * ReLoad Instruction *
182 \***************************************************************************/
185 ARMul_ReLoadInstr (ARMul_State
* state
, ARMword address
, ARMword isize
)
188 if (address
>= LOWABORT
&& address
< HIGHABORT
)
190 ARMul_PREFETCHABORT (address
);
191 return ARMul_ABORTWORD
;
199 if ((isize
== 2) && (address
& 0x2))
201 /* We return the next two halfwords: */
202 ARMword lo
= GetWord (state
, address
, FALSE
);
203 ARMword hi
= GetWord (state
, address
+ 4, FALSE
);
205 if (state
->bigendSig
== HIGH
)
206 return (lo
<< 16) | (hi
>> 16);
208 return ((hi
& 0xFFFF) << 16) | (lo
>> 16);
211 return GetWord (state
, address
, TRUE
);
214 /***************************************************************************\
215 * Load Instruction, Sequential Cycle *
216 \***************************************************************************/
218 ARMword
ARMul_LoadInstrS (ARMul_State
* state
, ARMword address
, ARMword isize
)
222 return ARMul_ReLoadInstr (state
, address
, isize
);
225 /***************************************************************************\
226 * Load Instruction, Non Sequential Cycle *
227 \***************************************************************************/
229 ARMword
ARMul_LoadInstrN (ARMul_State
* state
, ARMword address
, ARMword isize
)
233 return ARMul_ReLoadInstr (state
, address
, isize
);
236 /***************************************************************************\
237 * Read Word (but don't tell anyone!) *
238 \***************************************************************************/
240 ARMword
ARMul_ReadWord (ARMul_State
* state
, ARMword address
)
243 if (address
>= LOWABORT
&& address
< HIGHABORT
)
245 ARMul_DATAABORT (address
);
246 return ARMul_ABORTWORD
;
254 return GetWord (state
, address
, TRUE
);
257 /***************************************************************************\
258 * Load Word, Sequential Cycle *
259 \***************************************************************************/
261 ARMword
ARMul_LoadWordS (ARMul_State
* state
, ARMword address
)
265 return ARMul_ReadWord (state
, address
);
268 /***************************************************************************\
269 * Load Word, Non Sequential Cycle *
270 \***************************************************************************/
272 ARMword
ARMul_LoadWordN (ARMul_State
* state
, ARMword address
)
276 return ARMul_ReadWord (state
, address
);
279 /***************************************************************************\
280 * Load Halfword, (Non Sequential Cycle) *
281 \***************************************************************************/
283 ARMword
ARMul_LoadHalfWord (ARMul_State
* state
, ARMword address
)
285 ARMword temp
, offset
;
289 temp
= ARMul_ReadWord (state
, address
);
290 offset
= (((ARMword
) state
->bigendSig
* 2) ^ (address
& 2)) << 3; /* bit offset into the word */
292 return (temp
>> offset
) & 0xffff;
295 /***************************************************************************\
296 * Read Byte (but don't tell anyone!) *
297 \***************************************************************************/
299 ARMword
ARMul_ReadByte (ARMul_State
* state
, ARMword address
)
301 ARMword temp
, offset
;
303 temp
= ARMul_ReadWord (state
, address
);
304 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3; /* bit offset into the word */
306 return (temp
>> offset
& 0xffL
);
309 /***************************************************************************\
310 * Load Byte, (Non Sequential Cycle) *
311 \***************************************************************************/
313 ARMword
ARMul_LoadByte (ARMul_State
* state
, ARMword address
)
317 return ARMul_ReadByte (state
, address
);
320 /***************************************************************************\
321 * Write Word (but don't tell anyone!) *
322 \***************************************************************************/
325 ARMul_WriteWord (ARMul_State
* state
, ARMword address
, ARMword data
)
328 if (address
>= LOWABORT
&& address
< HIGHABORT
)
330 ARMul_DATAABORT (address
);
339 PutWord (state
, address
, data
, TRUE
);
342 /***************************************************************************\
343 * Store Word, Sequential Cycle *
344 \***************************************************************************/
347 ARMul_StoreWordS (ARMul_State
* state
, ARMword address
, ARMword data
)
351 ARMul_WriteWord (state
, address
, data
);
354 /***************************************************************************\
355 * Store Word, Non Sequential Cycle *
356 \***************************************************************************/
359 ARMul_StoreWordN (ARMul_State
* state
, ARMword address
, ARMword data
)
363 ARMul_WriteWord (state
, address
, data
);
366 /***************************************************************************\
367 * Store HalfWord, (Non Sequential Cycle) *
368 \***************************************************************************/
371 ARMul_StoreHalfWord (ARMul_State
* state
, ARMword address
, ARMword data
)
373 ARMword temp
, offset
;
381 state
->Emulate
= FALSE
;
383 (void) putc ((char) data
, stderr
); /* Write Char */
388 temp
= ARMul_ReadWord (state
, address
);
389 offset
= (((ARMword
) state
->bigendSig
* 2) ^ (address
& 2)) << 3; /* bit offset into the word */
391 PutWord (state
, address
,
392 (temp
& ~(0xffffL
<< offset
)) | ((data
& 0xffffL
) << offset
),
396 /***************************************************************************\
397 * Write Byte (but don't tell anyone!) *
398 \***************************************************************************/
401 ARMul_WriteByte (ARMul_State
* state
, ARMword address
, ARMword data
)
403 ARMword temp
, offset
;
405 temp
= ARMul_ReadWord (state
, address
);
406 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3; /* bit offset into the word */
408 PutWord (state
, address
,
409 (temp
& ~(0xffL
<< offset
)) | ((data
& 0xffL
) << offset
),
413 /***************************************************************************\
414 * Store Byte, (Non Sequential Cycle) *
415 \***************************************************************************/
418 ARMul_StoreByte (ARMul_State
* state
, ARMword address
, ARMword data
)
426 state
->Emulate
= FALSE
;
428 (void) putc ((char) data
, stderr
); /* Write Char */
433 ARMul_WriteByte (state
, address
, data
);
436 /***************************************************************************\
437 * Swap Word, (Two Non Sequential Cycles) *
438 \***************************************************************************/
440 ARMword
ARMul_SwapWord (ARMul_State
* state
, ARMword address
, ARMword data
)
446 temp
= ARMul_ReadWord (state
, address
);
450 PutWord (state
, address
, data
, TRUE
);
455 /***************************************************************************\
456 * Swap Byte, (Two Non Sequential Cycles) *
457 \***************************************************************************/
459 ARMword
ARMul_SwapByte (ARMul_State
* state
, ARMword address
, ARMword data
)
463 temp
= ARMul_LoadByte (state
, address
);
464 ARMul_StoreByte (state
, address
, data
);
469 /***************************************************************************\
471 \***************************************************************************/
474 ARMul_Icycles (ARMul_State
* state
, unsigned number
, ARMword address ATTRIBUTE_UNUSED
)
476 state
->NumIcycles
+= number
;
480 /***************************************************************************\
482 \***************************************************************************/
485 ARMul_Ccycles (ARMul_State
* state
, unsigned number
, ARMword address ATTRIBUTE_UNUSED
)
487 state
->NumCcycles
+= number
;
492 /* Read a byte. Do not check for alignment or access errors. */
495 ARMul_SafeReadByte (ARMul_State
* state
, ARMword address
)
497 ARMword temp
, offset
;
499 temp
= GetWord (state
, address
, FALSE
);
500 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3;
502 return (temp
>> offset
& 0xffL
);
506 ARMul_SafeWriteByte (ARMul_State
* state
, ARMword address
, ARMword data
)
508 ARMword temp
, offset
;
510 temp
= GetWord (state
, address
, FALSE
);
511 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3;
513 PutWord (state
, address
,
514 (temp
& ~(0xffL
<< offset
)) | ((data
& 0xffL
) << offset
),