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. */
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 /***************************************************************************\
52 * Get a Word from Virtual Memory, maybe allocating the page *
53 \***************************************************************************/
56 GetWord (ARMul_State * state, ARMword address)
63 page = address >> PAGEBITS;
64 offset = (address & OFFSETBITS) >> 2;
65 pagetable = (ARMword **) state->MemDataPtr;
66 pageptr = *(pagetable + page);
70 pageptr = (ARMword *) malloc (PAGESIZE);
74 perror ("ARMulator can't allocate VM page");
78 *(pagetable + page) = pageptr;
81 return *(pageptr + offset);
84 /***************************************************************************\
85 * Put a Word into Virtual Memory, maybe allocating the page *
86 \***************************************************************************/
89 PutWord (ARMul_State * state, ARMword address, ARMword data)
96 page = address >> PAGEBITS;
97 offset = (address & OFFSETBITS) >> 2;
98 pagetable = (ARMword **)state->MemDataPtr;
99 pageptr = *(pagetable + page);
103 pageptr = (ARMword *) malloc (PAGESIZE);
106 perror ("ARMulator can't allocate VM page");
110 *(pagetable + page) = pageptr;
113 *(pageptr + offset) = data;
116 /***************************************************************************\
117 * Initialise the memory interface *
118 \***************************************************************************/
121 ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
123 ARMword ** pagetable;
127 state->MemSize = initmemsize;
129 pagetable = (ARMword **) malloc (sizeof (ARMword) * NUMPAGES);
131 if (pagetable == NULL)
134 for (page = 0 ; page < NUMPAGES ; page++)
135 *(pagetable + page) = NULL;
137 state->MemDataPtr = (unsigned char *)pagetable;
139 ARMul_ConsolePrint (state, ", 4 Gb memory");
144 /***************************************************************************\
145 * Remove the memory interface *
146 \***************************************************************************/
149 ARMul_MemoryExit (ARMul_State * state)
152 ARMword ** pagetable;
155 pagetable = (ARMword **)state->MemDataPtr;
156 for (page = 0 ; page < NUMPAGES ; page++)
158 pageptr = *(pagetable + page);
160 free ((char *)pageptr);
162 free ((char *)pagetable);
166 /***************************************************************************\
167 * ReLoad Instruction *
168 \***************************************************************************/
171 ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
174 if (address >= LOWABORT && address < HIGHABORT)
176 ARMul_PREFETCHABORT (address);
177 return ARMul_ABORTWORD;
185 if ((isize == 2) && (address & 0x2))
187 /* We return the next two halfwords: */
188 ARMword lo = GetWord (state, address);
189 ARMword hi = GetWord (state, address + 4);
191 if (state->bigendSig == HIGH)
192 return (lo << 16) | (hi >> 16);
194 return ((hi & 0xFFFF) << 16) | (lo >> 16);
197 return GetWord (state, address);
200 /***************************************************************************\
201 * Load Instruction, Sequential Cycle *
202 \***************************************************************************/
205 ARMul_LoadInstrS (ARMul_State * state, ARMword address, ARMword isize)
207 state->NumScycles ++;
210 if (( state->NumScycles & HOURGLASS_RATE ) == 0)
216 return ARMul_ReLoadInstr (state, address, isize);
219 /***************************************************************************\
220 * Load Instruction, Non Sequential Cycle *
221 \***************************************************************************/
224 ARMul_LoadInstrN (ARMul_State * state, ARMword address, ARMword isize)
226 state->NumNcycles ++;
228 return ARMul_ReLoadInstr (state, address, isize);
231 /***************************************************************************\
232 * Read Word (but don't tell anyone!) *
233 \***************************************************************************/
236 ARMul_ReadWord (ARMul_State * state, ARMword address)
239 if (address >= LOWABORT && address < HIGHABORT)
241 ARMul_DATAABORT (address);
242 return ARMul_ABORTWORD;
250 return GetWord (state, address);
253 /***************************************************************************\
254 * Load Word, Sequential Cycle *
255 \***************************************************************************/
258 ARMul_LoadWordS (ARMul_State * state, ARMword address)
260 state->NumScycles ++;
262 return ARMul_ReadWord (state, address);
265 /***************************************************************************\
266 * Load Word, Non Sequential Cycle *
267 \***************************************************************************/
270 ARMul_LoadWordN (ARMul_State * state, ARMword address)
272 state->NumNcycles ++;
274 return ARMul_ReadWord (state, address);
277 /***************************************************************************\
278 * Load Halfword, (Non Sequential Cycle) *
279 \***************************************************************************/
282 ARMul_LoadHalfWord (ARMul_State * state, ARMword address)
284 ARMword temp, offset;
286 state->NumNcycles ++;
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 \***************************************************************************/
299 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 \***************************************************************************/
314 ARMul_LoadByte (ARMul_State * state, ARMword address)
316 state->NumNcycles ++;
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);
343 /***************************************************************************\
344 * Store Word, Sequential Cycle *
345 \***************************************************************************/
348 ARMul_StoreWordS (ARMul_State * state, ARMword address, ARMword data)
350 state->NumScycles ++;
352 ARMul_WriteWord (state, address, data);
355 /***************************************************************************\
356 * Store Word, Non Sequential Cycle *
357 \***************************************************************************/
360 ARMul_StoreWordN (ARMul_State * state, ARMword address, ARMword data)
362 state->NumNcycles ++;
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;
376 state->NumNcycles ++;
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, (temp & ~(0xffffL << offset)) | ((data & 0xffffL) << offset));
395 /***************************************************************************\
396 * Write Byte (but don't tell anyone!) *
397 \***************************************************************************/
400 ARMul_WriteByte (ARMul_State * state, ARMword address, ARMword data)
402 ARMword temp, offset;
404 temp = ARMul_ReadWord (state, address);
405 offset = (((ARMword)state->bigendSig * 3) ^ (address & 3)) << 3; /* bit offset into the word */
407 PutWord (state, address, (temp & ~(0xffL << offset)) | ((data & 0xffL) << offset));
410 /***************************************************************************\
411 * Store Byte, (Non Sequential Cycle) *
412 \***************************************************************************/
415 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
417 state->NumNcycles ++;
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 \***************************************************************************/
438 ARMul_SwapWord (ARMul_State * state, ARMword address, ARMword data)
442 state->NumNcycles ++;
444 temp = ARMul_ReadWord (state, address);
446 state->NumNcycles ++;
448 PutWord (state, address, data);
453 /***************************************************************************\
454 * Swap Byte, (Two Non Sequential Cycles) *
455 \***************************************************************************/
458 ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
462 temp = ARMul_LoadByte (state, address);
463 ARMul_StoreByte (state, address, data);
468 /***************************************************************************\
470 \***************************************************************************/
473 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address)
475 state->NumIcycles += number;
479 /***************************************************************************\
481 \***************************************************************************/
484 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address)
486 state->NumCcycles += number;