summaryrefslogtreecommitdiffstats
path: root/Bachelor/Mikroprozessorsysteme2/ARM202U/SOURCE/WIN32/ARMUL/ARMFAST.C
blob: 4f31123029323f0fa1943211394a6b0f83611c27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/***************************************************************************\
* armfast.c                                                                 *
* Version 1.20                                                              *
* ARMulator II Simple Memory Interface.                                     *
* Copyright (C) 1991 Advanced RISC Machines Limited. All rights reserved.   *
* Written by Dave Jaggar.                                                   *
* Project started on 1st July 1991.                                         *
\***************************************************************************/

/*
 * RCS $Revision: 1.14 $
 * Checkin $Date: 1995/01/18 12:26:52 $
 * Revising $Author: hmeekings $
 */

/* This file contains the fastest of example ARMulator memory systems.
It implements the ARM's memory as a fixed size block of host memory, and
does endian switching for byte accesses only. Memory accesses outside
the memory block are truncated to the size of the memory block. Each
routine is almost a cut-and-paste of some other, it's all quite simple */
/* Halfwords added 940909 by Andy Chapman */

#include "armdefs.h"

#define DEFMEMSIZE (512 * 1024) /* default memory size, must be a power of 2 */
#define WORDWRAP (DEFMEMSIZE - 4)
#define HWORDWRAP (DEFMEMSIZE - 2)
#define BYTEWRAP (DEFMEMSIZE - 1)

#define WORDMASK(addr) (addr & WORDWRAP)
#define HWORDMASK(addr) (addr & HWORDWRAP)
#define BYTEMASK(addr) (addr & BYTEWRAP)
#define ENDSWAP(addr) (addr ^ 3)
#define ENDSWAPH(addr) (addr ^ 2)
#define ENDSWAPB(addr) (addr ^ 1)
static unsigned HostEndian ;

#ifdef DOWATCH
#include "dbg_rdi.h"
#endif

/***************************************************************************\
*                      Initialise the memory interface                      *
\***************************************************************************/

unsigned ARMul_MemoryInit(ARMul_State *state, unsigned long initmemsize)
{unsigned char *Memory ;

 if (initmemsize == 0 || initmemsize > DEFMEMSIZE)
    initmemsize = DEFMEMSIZE ;
 state->MemSize = initmemsize ;
 Memory = (unsigned char *)malloc(DEFMEMSIZE) ;
 if (Memory == NULL)
    return(FALSE) ;
 state->MemInPtr = Memory ;
 *(ARMword *)Memory = 1 ;
 HostEndian = (*Memory != 1) ; /* 1 for big endian, 0 for little */
#ifdef BIGEND
 state->bigendSig = HIGH ;
#endif
#ifdef LITTLEEND
 state->bigendSig = LOW ;
#endif
 ARMul_ConsolePrint(state, ", %d Kb RAM",state->MemSize/1024) ;
#ifdef DOWATCH
 state->CanWatch = TRUE ;
#endif
 return(TRUE) ;
}

/***************************************************************************\
*                         Remove the memory interface                       *
\***************************************************************************/

void ARMul_MemoryExit(ARMul_State *state)
{free((char *)state->MemInPtr) ;
 return ;
 }

/***************************************************************************\
*                   Load Instruction, Sequential Cycle                      *
\***************************************************************************/

ARMword ARMul_LoadInstrS(ARMul_State *state,ARMword address)
{state->NumScycles++ ;

#ifdef DOWATCH
 if (state->MemReadDebug) ARMul_CheckWatch(state, address, RDIWatch_WordRead);
#endif

#ifdef HOURGLASS_RATE
 if( (state->NumScycles & HOURGLASS_RATE) == 0 ) {
    armsd_hourglass();
    }
#endif

 return( *(ARMword *)(state->MemInPtr + WORDMASK(address)) ) ;
}

/***************************************************************************\
*                 Load Instruction, Non Sequential Cycle                    *
\***************************************************************************/

ARMword ARMul_LoadInstrN(ARMul_State *state,ARMword address)
{state->NumNcycles++ ;
#ifdef DOWATCH
 if (state->MemReadDebug) ARMul_CheckWatch(state, address, RDIWatch_WordRead);
#endif
 return( *(ARMword *)(state->MemInPtr + WORDMASK(address)) ) ;
}

/***************************************************************************\
*                   Load 16 Bit Instruction, Sequential Cycle               *
\***************************************************************************/

ARMword ARMul_LoadInstr16S(ARMul_State *state,ARMword address)
{state->NumScycles++ ;

#ifdef HOURGLASS_RATE
 if( (state->NumScycles & HOURGLASS_RATE) == 0 ) {
    armsd_hourglass();
    }
#endif

 return( (*(ARMword *)(state->MemInPtr + WORDMASK(address))) >>
        ((((ARMword)state->bigendSig * 2) ^ (address & 2)) << 3) &
        0xffffL ) ;
}

/***************************************************************************\
*                 Load 16 Bit Instruction, Non Sequential Cycle             *
\***************************************************************************/

ARMword ARMul_LoadInstr16N(ARMul_State *state,ARMword address)
{state->NumNcycles++ ;
 return( (*(ARMword *)(state->MemInPtr + WORDMASK(address))) >>
        ((((ARMword)state->bigendSig * 2) ^ (address & 2)) << 3) &
        0xffffL ) ;
}

/***************************************************************************\
*                        Load Word, Sequential Cycle                        *
\***************************************************************************/

ARMword ARMul_LoadWordS(ARMul_State *state,ARMword address)
{state->NumScycles++ ;
#ifdef DOWATCH
 if (state->MemReadDebug) ARMul_CheckWatch(state, address, RDIWatch_WordRead);
#endif
 return( *(ARMword *)(state->MemInPtr + WORDMASK(address)) ) ;
}

/***************************************************************************\
*                      Load Word, Non Sequential Cycle                      *
\***************************************************************************/

ARMword ARMul_LoadWordN(ARMul_State *state,ARMword address)
{state->NumNcycles++ ;
#ifdef DOWATCH
 if (state->MemReadDebug) ARMul_CheckWatch(state, address, RDIWatch_WordRead);
#endif
 return( *(ARMword *)(state->MemInPtr + WORDMASK(address)) ) ;
}

/***************************************************************************\
*                     Load Halfword, (Non Sequential Cycle)                *
\***************************************************************************/

ARMword ARMul_LoadHalfWord(ARMul_State *state,ARMword address)
{state->NumNcycles++ ;
 address = BYTEMASK(address);
 if (HostEndian == state->bigendSig) {
   return( (ARMword)((*(state->MemInPtr + address) << 8) +
                     *(state->MemInPtr + ENDSWAPB(address)))) ;
 } else {
   return( (ARMword)((*(state->MemInPtr + ENDSWAPH(address)) << 8)+
                     *(state->MemInPtr + ENDSWAPB(ENDSWAPH(address))))) ;
 }
}

/***************************************************************************\
*                     Load Byte, (Non Sequential Cycle)                     *
\***************************************************************************/

ARMword ARMul_LoadByte(ARMul_State *state,ARMword address)
{state->NumNcycles++ ;
#ifdef DOWATCH
 if (state->MemReadDebug) ARMul_CheckWatch(state, address, RDIWatch_ByteRead);
#endif
 if (HostEndian == state->bigendSig)
    return( (ARMword)*(state->MemInPtr + BYTEMASK(address)) ) ;
 else
    return( (ARMword)*(state->MemInPtr + ENDSWAP(BYTEMASK(address))) ) ;
}

/***************************************************************************\
*                       Store Word, Sequential Cycle                        *
\***************************************************************************/

void ARMul_StoreWordS(ARMul_State *state,ARMword address, ARMword data)
{state->NumScycles++ ;
#ifdef DOWATCH
 if (state->MemWriteDebug) ARMul_CheckWatch(state, address, RDIWatch_WordWrite);
#endif
 *(ARMword *)(state->MemInPtr + WORDMASK(address)) = data ;
}
/***************************************************************************\
*                       Store Word, Sequential Cycle                        *
\***************************************************************************/

void ARMul_StoreWordN(ARMul_State *state,ARMword address, ARMword data)
{state->NumNcycles++ ;
#ifdef DOWATCH
 if (state->MemWriteDebug) ARMul_CheckWatch(state, address, RDIWatch_WordWrite);
#endif
 *(ARMword *)(state->MemInPtr + WORDMASK(address) ) = data ;
}

/***************************************************************************\
*                    Store HalfWord, (Non Sequential Cycle)                *
\***************************************************************************/

void ARMul_StoreHalfWord(ARMul_State *state,ARMword address, ARMword data)
{state->NumNcycles++ ;

 address = HWORDMASK(address);
 if (HostEndian == state->bigendSig) {
   *(state->MemInPtr + address) = (unsigned char)((data & 0xff00) >> 8);
   *(state->MemInPtr + address + 1) = (unsigned char)data ;
 } else {
   *(state->MemInPtr + ENDSWAPH(address)) = (unsigned char)((data & 0xff00) >> 8);
   *(state->MemInPtr + ENDSWAPH(address) + 1) = (unsigned char)data ;
 }
}

/***************************************************************************\
*                    Store Byte, (Non Sequential Cycle)                     *
\***************************************************************************/

void ARMul_StoreByte(ARMul_State *state,ARMword address, ARMword data)
{state->NumNcycles++ ;
#ifdef DOWATCH
 if (state->MemWriteDebug) ARMul_CheckWatch(state, address, RDIWatch_ByteWrite);
#endif
 if (HostEndian == state->bigendSig)
    *(state->MemInPtr + BYTEMASK(address)) = (unsigned char)data ;
 else
    *(state->MemInPtr + ENDSWAP(BYTEMASK(address))) = (unsigned char)data ;
}

/***************************************************************************\
*                   Swap Word, (Two Non Sequential Cycles)                  *
\***************************************************************************/

ARMword ARMul_SwapWord(ARMul_State *state,ARMword address, ARMword data)
{ARMword temp ;

 temp = ARMul_LoadWordN(state,address) ;
 ARMul_StoreWordN(state,address,data) ;
 return(temp) ;
}

/***************************************************************************\
*                   Swap Byte, (Two Non Sequential Cycles)                  *
\***************************************************************************/

ARMword ARMul_SwapByte(ARMul_State *state,ARMword address, ARMword data)
{ARMword temp ;

 temp = ARMul_LoadByte(state,address) ;
 ARMul_StoreByte(state,address,data) ;
 return(temp) ;
}

/***************************************************************************\
*                             Count I Cycles                                *
\***************************************************************************/

void ARMul_Icycles(ARMul_State *state,unsigned number, ARMword address)
{state->NumIcycles += number ;
}

/***************************************************************************\
*                             Count C Cycles                                *
\***************************************************************************/

void ARMul_Ccycles(ARMul_State *state,unsigned number, ARMword address)
{state->NumCcycles += number ;
}

/***************************************************************************\
*                      Read Word (but don't tell anyone!)                   *
\***************************************************************************/

ARMword ARMul_ReadWord(ARMul_State *state,ARMword address)
{
 return( *(ARMword *)(state->MemInPtr + WORDMASK(address)) ) ;
}

/***************************************************************************\
*                      Read Halfword (but don't tell anyone!)              *
\***************************************************************************/

ARMword ARMul_ReadHalfWord(ARMul_State *state,ARMword address)
{
 address = BYTEMASK(address);
 if (HostEndian == state->bigendSig) {
   return( (ARMword)((*(state->MemInPtr + address) << 8) +
                     *(state->MemInPtr + ENDSWAPB(address)))) ;
 } else {
   return( (ARMword)((*(state->MemInPtr + ENDSWAPH(address)) << 8)+
                     *(state->MemInPtr + ENDSWAPB(ENDSWAPH(address))))) ;
 }
}

/***************************************************************************\
*                      Read Byte (but don't tell anyone!)                   *
\***************************************************************************/

ARMword ARMul_ReadByte(ARMul_State *state,ARMword address)
{
 if (HostEndian == state->bigendSig)
    return( (ARMword)*(state->MemInPtr + BYTEMASK(address)) ) ;
 else
    return( (ARMword)*(state->MemInPtr + ENDSWAP(BYTEMASK(address))) ) ;
}

/***************************************************************************\
*                     Write Word (but don't tell anyone!)                   *
\***************************************************************************/

void ARMul_WriteWord(ARMul_State *state,ARMword address, ARMword data)
{
 *(ARMword *)(state->MemInPtr + WORDMASK(address)) = data ;
}

/***************************************************************************\
*                     Write Halfword (but don't tell anyone!)              *
\***************************************************************************/

void ARMul_WriteHalfWord(ARMul_State *state,ARMword address, ARMword data)
{
 address = HWORDMASK(address);
 if (HostEndian == state->bigendSig) {
   *(state->MemInPtr + address) = (unsigned char)((data & 0xff00) >> 8);
   *(state->MemInPtr + address + 1) = (unsigned char)data ;
 } else {
   *(state->MemInPtr + ENDSWAPH(address)) = (unsigned char)((data & 0xff00) >> 8);
   *(state->MemInPtr + ENDSWAPH(address) + 1) = (unsigned char)data ;
 }
}

/***************************************************************************\
*                     Write Byte (but don't tell anyone!)                   *
\***************************************************************************/

void ARMul_WriteByte(ARMul_State *state,ARMword address, ARMword data)
{
 if (HostEndian == state->bigendSig)
    *(state->MemInPtr + BYTEMASK(address)) = (unsigned char)data ;
 else
    *(state->MemInPtr + ENDSWAP(BYTEMASK(address))) = (unsigned char)data ;
}