Automatic date update in version.in
[binutils-gdb.git] / sim / arm / armvirt.c
blobc162ba79e9b7c4ec045f39e30d54c06a4066a859
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. */
26 #include "defs.h"
28 #include "armos.h"
29 #include "armdefs.h"
30 #include "ansidecl.h"
32 #ifdef VALIDATE /* for running the validate suite */
33 #define TUBE 48 * 1024 * 1024 /* write a char on the screen */
34 #define ABORTS 1
35 #endif
37 /* #define ABORTS */
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
47 #endif
49 #undef PAGESIZE /* Cleanup system headers. */
50 #define NUMPAGES 64 * 1024
51 #define PAGESIZE 64 * 1024
52 #define PAGEBITS 16
53 #define OFFSETBITS 0xffff
55 int SWI_vector_installed = FALSE;
57 /***************************************************************************\
58 * Get a Word from Virtual Memory, maybe allocating the page *
59 \***************************************************************************/
61 static ARMword
62 GetWord (ARMul_State * state, ARMword address, int check)
64 ARMword page;
65 ARMword offset;
66 ARMword **pagetable;
67 ARMword *pageptr;
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);
77 if (pageptr == NULL)
79 pageptr = (ARMword *) malloc (PAGESIZE);
81 if (pageptr == NULL)
83 perror ("ARMulator can't allocate VM page");
84 exit (12);
87 *(pagetable + page) = pageptr;
90 return *(pageptr + offset);
93 /***************************************************************************\
94 * Put a Word into Virtual Memory, maybe allocating the page *
95 \***************************************************************************/
97 static void
98 PutWord (ARMul_State * state, ARMword address, ARMword data, int check)
100 ARMword page;
101 ARMword offset;
102 ARMword **pagetable;
103 ARMword *pageptr;
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);
113 if (pageptr == NULL)
115 pageptr = (ARMword *) malloc (PAGESIZE);
116 if (pageptr == NULL)
118 perror ("ARMulator can't allocate VM page");
119 exit (13);
122 *(pagetable + page) = pageptr;
125 if (address == 0x8)
126 SWI_vector_installed = TRUE;
128 *(pageptr + offset) = data;
131 /***************************************************************************\
132 * Initialise the memory interface *
133 \***************************************************************************/
135 unsigned
136 ARMul_MemoryInit (ARMul_State * state, unsigned long initmemsize)
138 ARMword **pagetable;
139 unsigned page;
141 if (initmemsize)
142 state->MemSize = initmemsize;
144 pagetable = (ARMword **) malloc (sizeof (ARMword *) * NUMPAGES);
146 if (pagetable == NULL)
147 return FALSE;
149 for (page = 0; page < NUMPAGES; page++)
150 *(pagetable + page) = NULL;
152 state->MemDataPtr = (unsigned char *) pagetable;
154 ARMul_ConsolePrint (state, ", 4 Gb memory");
156 return TRUE;
159 /***************************************************************************\
160 * Remove the memory interface *
161 \***************************************************************************/
163 void
164 ARMul_MemoryExit (ARMul_State * state)
166 ARMword page;
167 ARMword **pagetable;
168 ARMword *pageptr;
170 pagetable = (ARMword **) state->MemDataPtr;
171 for (page = 0; page < NUMPAGES; page++)
173 pageptr = *(pagetable + page);
174 if (pageptr != NULL)
175 free ((char *) pageptr);
177 free ((char *) pagetable);
178 return;
181 /***************************************************************************\
182 * ReLoad Instruction *
183 \***************************************************************************/
185 ARMword
186 ARMul_ReLoadInstr (ARMul_State * state, ARMword address, ARMword isize)
188 #ifdef ABORTS
189 if (address >= LOWABORT && address < HIGHABORT)
191 ARMul_PREFETCHABORT (address);
192 return ARMul_ABORTWORD;
194 else
196 ARMul_CLEARABORT;
198 #endif
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);
208 else
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)
221 state->NumScycles++;
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)
232 state->NumNcycles++;
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)
243 #ifdef ABORTS
244 if (address >= LOWABORT && address < HIGHABORT)
246 ARMul_DATAABORT (address);
247 return ARMul_ABORTWORD;
249 else
251 ARMul_CLEARABORT;
253 #endif
255 return GetWord (state, address, TRUE);
258 /***************************************************************************\
259 * Load Word, Sequential Cycle *
260 \***************************************************************************/
262 ARMword ARMul_LoadWordS (ARMul_State * state, ARMword address)
264 state->NumScycles++;
266 return ARMul_ReadWord (state, address);
269 /***************************************************************************\
270 * Load Word, Non Sequential Cycle *
271 \***************************************************************************/
273 ARMword ARMul_LoadWordN (ARMul_State * state, ARMword address)
275 state->NumNcycles++;
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;
288 state->NumNcycles++;
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)
316 state->NumNcycles++;
318 return ARMul_ReadByte (state, address);
321 /***************************************************************************\
322 * Write Word (but don't tell anyone!) *
323 \***************************************************************************/
325 void
326 ARMul_WriteWord (ARMul_State * state, ARMword address, ARMword data)
328 #ifdef ABORTS
329 if (address >= LOWABORT && address < HIGHABORT)
331 ARMul_DATAABORT (address);
332 return;
334 else
336 ARMul_CLEARABORT;
338 #endif
340 PutWord (state, address, data, TRUE);
343 /***************************************************************************\
344 * Store Word, Sequential Cycle *
345 \***************************************************************************/
347 void
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 \***************************************************************************/
359 void
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 \***************************************************************************/
371 void
372 ARMul_StoreHalfWord (ARMul_State * state, ARMword address, ARMword data)
374 ARMword temp, offset;
376 state->NumNcycles++;
378 #ifdef VALIDATE
379 if (address == TUBE)
381 if (data == 4)
382 state->Emulate = FALSE;
383 else
384 (void) putc ((char) data, stderr); /* Write Char */
385 return;
387 #endif
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),
394 TRUE);
397 /***************************************************************************\
398 * Write Byte (but don't tell anyone!) *
399 \***************************************************************************/
401 void
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),
411 TRUE);
414 /***************************************************************************\
415 * Store Byte, (Non Sequential Cycle) *
416 \***************************************************************************/
418 void
419 ARMul_StoreByte (ARMul_State * state, ARMword address, ARMword data)
421 state->NumNcycles++;
423 #ifdef VALIDATE
424 if (address == TUBE)
426 if (data == 4)
427 state->Emulate = FALSE;
428 else
429 (void) putc ((char) data, stderr); /* Write Char */
430 return;
432 #endif
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)
443 ARMword temp;
445 state->NumNcycles++;
447 temp = ARMul_ReadWord (state, address);
449 state->NumNcycles++;
451 PutWord (state, address, data, TRUE);
453 return temp;
456 /***************************************************************************\
457 * Swap Byte, (Two Non Sequential Cycles) *
458 \***************************************************************************/
460 ARMword ARMul_SwapByte (ARMul_State * state, ARMword address, ARMword data)
462 ARMword temp;
464 temp = ARMul_LoadByte (state, address);
465 ARMul_StoreByte (state, address, data);
467 return temp;
470 /***************************************************************************\
471 * Count I Cycles *
472 \***************************************************************************/
474 void
475 ARMul_Icycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
477 state->NumIcycles += number;
478 ARMul_CLEARABORT;
481 /***************************************************************************\
482 * Count C Cycles *
483 \***************************************************************************/
485 void
486 ARMul_Ccycles (ARMul_State * state, unsigned number, ARMword address ATTRIBUTE_UNUSED)
488 state->NumCcycles += number;
489 ARMul_CLEARABORT;
493 /* Read a byte. Do not check for alignment or access errors. */
495 ARMword
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);
506 void
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),
516 FALSE);