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. */
31 #ifdef VALIDATE /* for running the validate suite */
32 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
38 #ifdef ABORTS /* the memory system will abort */
39 /* For the old test suite Abort between 32 Kbytes and 32 Mbytes
40 For the new test suite Abort between 8 Mbytes and 26 Mbytes */
41 /* #define LOWABORT 32 * 1024
42 #define HIGHABORT 32 * 1024 * 1024 */
43 #define LOWABORT 8 * 1024 * 1024
44 #define HIGHABORT 26 * 1024 * 1024
48 #define NUMPAGES 64 * 1024
49 #define PAGESIZE 64 * 1024
51 #define OFFSETBITS 0xffff
53 int SWI_vector_installed = FALSE;
55 /***************************************************************************\
56 * Get a Word from Virtual Memory, maybe allocating the page *
57 \***************************************************************************/
60 GetWord (ARMul_State * state, ARMword address, int check)
67 XScale_check_memacc (state, &address, 0);
69 page = address >> PAGEBITS;
70 offset = (address & OFFSETBITS) >> 2;
71 pagetable = (ARMword **) state->MemDataPtr;
72 pageptr = *(pagetable + page);
76 pageptr = (ARMword *) malloc (PAGESIZE);
80 perror ("ARMulator can't allocate VM page");
84 *(pagetable + page) = pageptr;
87 return *(pageptr + offset);
90 /***************************************************************************\
91 * Put a Word into Virtual Memory, maybe allocating the page *
92 \***************************************************************************/
95 PutWord (ARMul_State * state, ARMword address, ARMword data, int check)
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)
220 if ((state->NumScycles & HOURGLASS_RATE) == 0)
226 return ARMul_ReLoadInstr (state, address, isize);
229 /***************************************************************************\
230 * Load Instruction, Non Sequential Cycle *
231 \***************************************************************************/
233 ARMword ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
237 return ARMul_ReLoadInstr (state, address, isize);
240 /***************************************************************************\
241 * Read Word (but don't tell anyone!) *
242 \***************************************************************************/
244 ARMword ARMul_ReadWord (ARMul_State * state, ARMword address)
247 if (address >= LOWABORT && address < HIGHABORT)
249 ARMul_DATAABORT (address);
250 return ARMul_ABORTWORD;
258 return GetWord (state, address, TRUE);
261 /***************************************************************************\
262 * Load Word, Sequential Cycle *
263 \***************************************************************************/
265 ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
269 return ARMul_ReadWord (state, address);
272 /***************************************************************************\
273 * Load Word, Non Sequential Cycle *
274 \***************************************************************************/
276 ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
280 return ARMul_ReadWord (state, address);
283 /***************************************************************************\
284 * Load Halfword, (Non Sequential Cycle) *
285 \***************************************************************************/
287 ARMword ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
289 ARMword temp, offset;
293 temp = ARMul_ReadWord (state, address);
294 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
296 return (temp >> offset) & 0xffff;
299 /***************************************************************************\
300 * Read Byte (but don't tell anyone!) *
301 \***************************************************************************/
303 ARMword ARMul_ReadByte (ARMul_State * state, ARMword address)
305 ARMword temp, offset;
307 temp = ARMul_ReadWord (state, address);
308 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
310 return (temp >> offset & 0xffL);
313 /***************************************************************************\
314 * Load Byte, (Non Sequential Cycle) *
315 \***************************************************************************/
317 ARMword ARMul_LoadByte (ARMul_State * state, ARMword address)
321 return ARMul_ReadByte (state, address);
324 /***************************************************************************\
325 * Write Word (but don't tell anyone!) *
326 \***************************************************************************/
329 ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
332 if (address >= LOWABORT && address < HIGHABORT)
334 ARMul_DATAABORT (address);
343 PutWord (state, address, data, TRUE);
346 /***************************************************************************\
347 * Store Word, Sequential Cycle *
348 \***************************************************************************/
351 ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
355 ARMul_WriteWord (state, address, data);
358 /***************************************************************************\
359 * Store Word, Non Sequential Cycle *
360 \***************************************************************************/
363 ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
367 ARMul_WriteWord (state, address, data);
370 /***************************************************************************\
371 * Store HalfWord, (Non Sequential Cycle) *
372 \***************************************************************************/
375 ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
377 ARMword temp, offset;
385 state->Emulate = FALSE;
387 (void) putc ((char) data, stderr); /* Write Char */
392 temp = ARMul_ReadWord (state, address);
393 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
395 PutWord (state, address,
396 (temp & ~(0xffffL << offset)) | ((data & 0xffffL) << offset),
400 /***************************************************************************\
401 * Write Byte (but don't tell anyone!) *
402 \***************************************************************************/
405 ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
407 ARMword temp, offset;
409 temp = ARMul_ReadWord (state, address);
410 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
412 PutWord (state, address,
413 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),
417 /***************************************************************************\
418 * Store Byte, (Non Sequential Cycle) *
419 \***************************************************************************/
422 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
430 state->Emulate = FALSE;
432 (void) putc ((char) data, stderr); /* Write Char */
437 ARMul_WriteByte (state, address, data);
440 /***************************************************************************\
441 * Swap Word, (Two Non Sequential Cycles) *
442 \***************************************************************************/
444 ARMword ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
450 temp = ARMul_ReadWord (state, address);
454 PutWord (state, address, data, TRUE);
459 /***************************************************************************\
460 * Swap Byte, (Two Non Sequential Cycles) *
461 \***************************************************************************/
463 ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
467 temp = ARMul_LoadByte (state, address);
468 ARMul_StoreByte (state, address, data);
473 /***************************************************************************\
475 \***************************************************************************/
478 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
480 state->NumIcycles += number;
484 /***************************************************************************\
486 \***************************************************************************/
489 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
491 state->NumCcycles += number;
496 /* Read a byte. Do not check for alignment or access errors. */
499 ARMul_SafeReadByte (ARMul_State * state, ARMword address)
501 ARMword temp, offset;
503 temp = GetWord (state, address, FALSE);
504 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
506 return (temp >> offset & 0xffL);
510 ARMul_SafeWriteByte (ARMul_State * state, ARMword address, ARMword data)
512 ARMword temp, offset;
514 temp = GetWord (state, address, FALSE);
515 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
517 PutWord (state, address,
518 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),