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 #undef PAGESIZE /* Cleanup system headers. */
50 #define NUMPAGES 64 * 1024
51 #define PAGESIZE 64 * 1024
53 #define OFFSETBITS 0xffff
55 int SWI_vector_installed = FALSE;
57 /***************************************************************************\
58 * Get a Word from Virtual Memory, maybe allocating the page *
59 \***************************************************************************/
62 GetWord (ARMul_State * state, ARMword address, int check)
69 if (check && state->is_XScale)
70 XScale_check_memacc (state, &address, 0);
72 page = address >> PAGEBITS;
73 offset = (address & OFFSETBITS) >> 2;
74 pagetable = (ARMword **) state->MemDataPtr;
75 pageptr = *(pagetable + page);
79 pageptr = (ARMword *) malloc (PAGESIZE);
83 perror ("ARMulator can't allocate VM page");
87 *(pagetable + page) = pageptr;
90 return *(pageptr + offset);
93 /***************************************************************************\
94 * Put a Word into Virtual Memory, maybe allocating the page *
95 \***************************************************************************/
98 PutWord (ARMul_State * state, ARMword address, ARMword data, int check)
105 if (check && state->is_XScale)
106 XScale_check_memacc (state, &address, 1);
108 page = address >> PAGEBITS;
109 offset = (address & OFFSETBITS) >> 2;
110 pagetable = (ARMword **) state->MemDataPtr;
111 pageptr = *(pagetable + page);
115 pageptr = (ARMword *) malloc (PAGESIZE);
118 perror ("ARMulator can't allocate VM page");
122 *(pagetable + page) = pageptr;
126 SWI_vector_installed = TRUE;
128 *(pageptr + offset) = data;
131 /***************************************************************************\
132 * Initialise the memory interface *
133 \***************************************************************************/
136 ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
142 state->MemSize = initmemsize;
144 pagetable = (ARMword **) malloc (sizeof (ARMword *) * NUMPAGES);
146 if (pagetable == NULL)
149 for (page = 0; page < NUMPAGES; page++)
150 *(pagetable + page) = NULL;
152 state->MemDataPtr = (unsigned char *) pagetable;
154 ARMul_ConsolePrint (state, ", 4 Gb memory");
159 /***************************************************************************\
160 * Remove the memory interface *
161 \***************************************************************************/
164 ARMul_MemoryExit (ARMul_State * state)
170 pagetable = (ARMword **) state->MemDataPtr;
171 for (page = 0; page < NUMPAGES; page++)
173 pageptr = *(pagetable + page);
175 free ((char *) pageptr);
177 free ((char *) pagetable);
181 /***************************************************************************\
182 * ReLoad Instruction *
183 \***************************************************************************/
186 ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
189 if (address >= LOWABORT && address < HIGHABORT)
191 ARMul_PREFETCHABORT (address);
192 return ARMul_ABORTWORD;
200 if ((isize == 2) && (address & 0x2))
202 /* We return the next two halfwords: */
203 ARMword lo = GetWord (state, address, FALSE);
204 ARMword hi = GetWord (state, address + 4, FALSE);
206 if (state->bigendSig == HIGH)
207 return (lo << 16) | (hi >> 16);
209 return ((hi & 0xFFFF) << 16) | (lo >> 16);
212 return GetWord (state, address, TRUE);
215 /***************************************************************************\
216 * Load Instruction, Sequential Cycle *
217 \***************************************************************************/
219 ARMword ARMul_LoadInstrS (ARMul_State * state, ARMword address, ARMword isize)
223 return ARMul_ReLoadInstr (state, address, isize);
226 /***************************************************************************\
227 * Load Instruction, Non Sequential Cycle *
228 \***************************************************************************/
230 ARMword ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
234 return ARMul_ReLoadInstr (state, address, isize);
237 /***************************************************************************\
238 * Read Word (but don't tell anyone!) *
239 \***************************************************************************/
241 ARMword ARMul_ReadWord (ARMul_State * state, ARMword address)
244 if (address >= LOWABORT && address < HIGHABORT)
246 ARMul_DATAABORT (address);
247 return ARMul_ABORTWORD;
255 return GetWord (state, address, TRUE);
258 /***************************************************************************\
259 * Load Word, Sequential Cycle *
260 \***************************************************************************/
262 ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
266 return ARMul_ReadWord (state, address);
269 /***************************************************************************\
270 * Load Word, Non Sequential Cycle *
271 \***************************************************************************/
273 ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
277 return ARMul_ReadWord (state, address);
280 /***************************************************************************\
281 * Load Halfword, (Non Sequential Cycle) *
282 \***************************************************************************/
284 ARMword ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
286 ARMword temp, offset;
290 temp = ARMul_ReadWord (state, address);
291 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
293 return (temp >> offset) & 0xffff;
296 /***************************************************************************\
297 * Read Byte (but don't tell anyone!) *
298 \***************************************************************************/
300 ARMword ARMul_ReadByte (ARMul_State * state, ARMword address)
302 ARMword temp, offset;
304 temp = ARMul_ReadWord (state, address);
305 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
307 return (temp >> offset & 0xffL);
310 /***************************************************************************\
311 * Load Byte, (Non Sequential Cycle) *
312 \***************************************************************************/
314 ARMword ARMul_LoadByte (ARMul_State * state, ARMword address)
318 return ARMul_ReadByte (state, address);
321 /***************************************************************************\
322 * Write Word (but don't tell anyone!) *
323 \***************************************************************************/
326 ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
329 if (address >= LOWABORT && address < HIGHABORT)
331 ARMul_DATAABORT (address);
340 PutWord (state, address, data, TRUE);
343 /***************************************************************************\
344 * Store Word, Sequential Cycle *
345 \***************************************************************************/
348 ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
352 ARMul_WriteWord (state, address, data);
355 /***************************************************************************\
356 * Store Word, Non Sequential Cycle *
357 \***************************************************************************/
360 ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
364 ARMul_WriteWord (state, address, data);
367 /***************************************************************************\
368 * Store HalfWord, (Non Sequential Cycle) *
369 \***************************************************************************/
372 ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
374 ARMword temp, offset;
382 state->Emulate = FALSE;
384 (void) putc ((char) data, stderr); /* Write Char */
389 temp = ARMul_ReadWord (state, address);
390 offset = (((ARMword) state->bigendSig * 2) ^ (address & 2)) << 3; /* bit offset into the word */
392 PutWord (state, address,
393 (temp & ~(0xffffL << offset)) | ((data & 0xffffL) << offset),
397 /***************************************************************************\
398 * Write Byte (but don't tell anyone!) *
399 \***************************************************************************/
402 ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
404 ARMword temp, offset;
406 temp = ARMul_ReadWord (state, address);
407 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
409 PutWord (state, address,
410 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),
414 /***************************************************************************\
415 * Store Byte, (Non Sequential Cycle) *
416 \***************************************************************************/
419 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
427 state->Emulate = FALSE;
429 (void) putc ((char) data, stderr); /* Write Char */
434 ARMul_WriteByte (state, address, data);
437 /***************************************************************************\
438 * Swap Word, (Two Non Sequential Cycles) *
439 \***************************************************************************/
441 ARMword ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
447 temp = ARMul_ReadWord (state, address);
451 PutWord (state, address, data, TRUE);
456 /***************************************************************************\
457 * Swap Byte, (Two Non Sequential Cycles) *
458 \***************************************************************************/
460 ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
464 temp = ARMul_LoadByte (state, address);
465 ARMul_StoreByte (state, address, data);
470 /***************************************************************************\
472 \***************************************************************************/
475 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
477 state->NumIcycles += number;
481 /***************************************************************************\
483 \***************************************************************************/
486 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
488 state->NumCcycles += number;
493 /* Read a byte. Do not check for alignment or access errors. */
496 ARMul_SafeReadByte (ARMul_State * state, ARMword address)
498 ARMword temp, offset;
500 temp = GetWord (state, address, FALSE);
501 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
503 return (temp >> offset & 0xffL);
507 ARMul_SafeWriteByte (ARMul_State * state, ARMword address, ARMword data)
509 ARMword temp, offset;
511 temp = GetWord (state, address, FALSE);
512 offset = (((ARMword) state->bigendSig * 3) ^ (address & 3)) << 3;
514 PutWord (state, address,
515 (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset),