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 int SWI_vector_installed
= FALSE
;
54 /***************************************************************************\
55 * Get a Word from Virtual Memory, maybe allocating the page *
56 \***************************************************************************/
59 GetWord (ARMul_State
* state
, ARMword address
)
66 page
= address
>> PAGEBITS
;
67 offset
= (address
& OFFSETBITS
) >> 2;
68 pagetable
= (ARMword
**) state
->MemDataPtr
;
69 pageptr
= *(pagetable
+ page
);
73 pageptr
= (ARMword
*) malloc (PAGESIZE
);
77 perror ("ARMulator can't allocate VM page");
81 *(pagetable
+ page
) = pageptr
;
84 return *(pageptr
+ offset
);
87 /***************************************************************************\
88 * Put a Word into Virtual Memory, maybe allocating the page *
89 \***************************************************************************/
92 PutWord (ARMul_State
* state
, ARMword address
, ARMword data
)
99 page
= address
>> PAGEBITS
;
100 offset
= (address
& OFFSETBITS
) >> 2;
101 pagetable
= (ARMword
**) state
->MemDataPtr
;
102 pageptr
= *(pagetable
+ page
);
106 pageptr
= (ARMword
*) malloc (PAGESIZE
);
109 perror ("ARMulator can't allocate VM page");
113 *(pagetable
+ page
) = pageptr
;
117 SWI_vector_installed
= TRUE
;
119 *(pageptr
+ offset
) = data
;
122 /***************************************************************************\
123 * Initialise the memory interface *
124 \***************************************************************************/
127 ARMul_MemoryInit (ARMul_State
* state
, unsigned long initmemsize
)
133 state
->MemSize
= initmemsize
;
135 pagetable
= (ARMword
**) malloc (sizeof (ARMword
) * NUMPAGES
);
137 if (pagetable
== NULL
)
140 for (page
= 0; page
< NUMPAGES
; page
++)
141 *(pagetable
+ page
) = NULL
;
143 state
->MemDataPtr
= (unsigned char *) pagetable
;
145 ARMul_ConsolePrint (state
, ", 4 Gb memory");
150 /***************************************************************************\
151 * Remove the memory interface *
152 \***************************************************************************/
155 ARMul_MemoryExit (ARMul_State
* state
)
161 pagetable
= (ARMword
**) state
->MemDataPtr
;
162 for (page
= 0; page
< NUMPAGES
; page
++)
164 pageptr
= *(pagetable
+ page
);
166 free ((char *) pageptr
);
168 free ((char *) pagetable
);
172 /***************************************************************************\
173 * ReLoad Instruction *
174 \***************************************************************************/
177 ARMul_ReLoadInstr (ARMul_State
* state
, ARMword address
, ARMword isize
)
180 if (address
>= LOWABORT
&& address
< HIGHABORT
)
182 ARMul_PREFETCHABORT (address
);
183 return ARMul_ABORTWORD
;
191 if ((isize
== 2) && (address
& 0x2))
193 /* We return the next two halfwords: */
194 ARMword lo
= GetWord (state
, address
);
195 ARMword hi
= GetWord (state
, address
+ 4);
197 if (state
->bigendSig
== HIGH
)
198 return (lo
<< 16) | (hi
>> 16);
200 return ((hi
& 0xFFFF) << 16) | (lo
>> 16);
203 return GetWord (state
, address
);
206 /***************************************************************************\
207 * Load Instruction, Sequential Cycle *
208 \***************************************************************************/
210 ARMword
ARMul_LoadInstrS (ARMul_State
* state
, ARMword address
, ARMword isize
)
215 if ((state
->NumScycles
& HOURGLASS_RATE
) == 0)
221 return ARMul_ReLoadInstr (state
, address
, isize
);
224 /***************************************************************************\
225 * Load Instruction, Non Sequential Cycle *
226 \***************************************************************************/
228 ARMword
ARMul_LoadInstrN (ARMul_State
* state
, ARMword address
, ARMword isize
)
232 return ARMul_ReLoadInstr (state
, address
, isize
);
235 /***************************************************************************\
236 * Read Word (but don't tell anyone!) *
237 \***************************************************************************/
239 ARMword
ARMul_ReadWord (ARMul_State
* state
, ARMword address
)
242 if (address
>= LOWABORT
&& address
< HIGHABORT
)
244 ARMul_DATAABORT (address
);
245 return ARMul_ABORTWORD
;
253 return GetWord (state
, address
);
256 /***************************************************************************\
257 * Load Word, Sequential Cycle *
258 \***************************************************************************/
260 ARMword
ARMul_LoadWordS (ARMul_State
* state
, ARMword address
)
264 return ARMul_ReadWord (state
, address
);
267 /***************************************************************************\
268 * Load Word, Non Sequential Cycle *
269 \***************************************************************************/
271 ARMword
ARMul_LoadWordN (ARMul_State
* state
, ARMword address
)
275 return ARMul_ReadWord (state
, address
);
278 /***************************************************************************\
279 * Load Halfword, (Non Sequential Cycle) *
280 \***************************************************************************/
282 ARMword
ARMul_LoadHalfWord (ARMul_State
* state
, ARMword address
)
284 ARMword temp
, offset
;
288 temp
= ARMul_ReadWord (state
, address
);
289 offset
= (((ARMword
) state
->bigendSig
* 2) ^ (address
& 2)) << 3; /* bit offset into the word */
291 return (temp
>> offset
) & 0xffff;
294 /***************************************************************************\
295 * Read Byte (but don't tell anyone!) *
296 \***************************************************************************/
298 ARMword
ARMul_ReadByte (ARMul_State
* state
, ARMword address
)
300 ARMword temp
, offset
;
302 temp
= ARMul_ReadWord (state
, address
);
303 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3; /* bit offset into the word */
305 return (temp
>> offset
& 0xffL
);
308 /***************************************************************************\
309 * Load Byte, (Non Sequential Cycle) *
310 \***************************************************************************/
312 ARMword
ARMul_LoadByte (ARMul_State
* state
, ARMword address
)
316 return ARMul_ReadByte (state
, address
);
319 /***************************************************************************\
320 * Write Word (but don't tell anyone!) *
321 \***************************************************************************/
324 ARMul_WriteWord (ARMul_State
* state
, ARMword address
, ARMword data
)
327 if (address
>= LOWABORT
&& address
< HIGHABORT
)
329 ARMul_DATAABORT (address
);
338 PutWord (state
, address
, data
);
341 /***************************************************************************\
342 * Store Word, Sequential Cycle *
343 \***************************************************************************/
346 ARMul_StoreWordS (ARMul_State
* state
, ARMword address
, ARMword data
)
350 ARMul_WriteWord (state
, address
, data
);
353 /***************************************************************************\
354 * Store Word, Non Sequential Cycle *
355 \***************************************************************************/
358 ARMul_StoreWordN (ARMul_State
* state
, ARMword address
, ARMword data
)
362 ARMul_WriteWord (state
, address
, data
);
365 /***************************************************************************\
366 * Store HalfWord, (Non Sequential Cycle) *
367 \***************************************************************************/
370 ARMul_StoreHalfWord (ARMul_State
* state
, ARMword address
, ARMword data
)
372 ARMword temp
, offset
;
380 state
->Emulate
= FALSE
;
382 (void) putc ((char) data
, stderr
); /* Write Char */
387 temp
= ARMul_ReadWord (state
, address
);
388 offset
= (((ARMword
) state
->bigendSig
* 2) ^ (address
& 2)) << 3; /* bit offset into the word */
390 PutWord (state
, address
,
391 (temp
& ~(0xffffL
<< offset
)) | ((data
& 0xffffL
) << offset
));
394 /***************************************************************************\
395 * Write Byte (but don't tell anyone!) *
396 \***************************************************************************/
399 ARMul_WriteByte (ARMul_State
* state
, ARMword address
, ARMword data
)
401 ARMword temp
, offset
;
403 temp
= ARMul_ReadWord (state
, address
);
404 offset
= (((ARMword
) state
->bigendSig
* 3) ^ (address
& 3)) << 3; /* bit offset into the word */
406 PutWord (state
, address
,
407 (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
);
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
;