2961
|
1 /* Copyright 2005-2006 Guillaume Duhamel
|
|
2 Copyright 2005 Theo Berkau
|
|
3
|
|
4 This file is part of Yabause.
|
|
5
|
|
6 Yabause is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2 of the License, or
|
|
9 (at your option) any later version.
|
|
10
|
|
11 Yabause is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with Yabause; if not, write to the Free Software
|
|
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21 #ifndef MEM_H
|
|
22 #define MEM_H
|
|
23
|
|
24 #include <stdlib.h>
|
|
25 #include "types.h"
|
|
26
|
|
27 /* Type 1 Memory, faster for byte (8 bits) accesses */
|
|
28
|
|
29 static INLINE u8 T1ReadByte(u8 * mem, u32 addr)
|
|
30 {
|
|
31 return mem[addr];
|
|
32 }
|
|
33
|
|
34 static INLINE u16 T1ReadWord(u8 * mem, u32 addr)
|
|
35 {
|
|
36 #ifdef WORDS_BIGENDIAN
|
|
37 return (mem[addr + 1] << 8) | mem[addr];
|
|
38 #else
|
|
39 return *((u16 *) (mem + addr));
|
|
40 #endif
|
|
41 }
|
|
42
|
|
43 static INLINE u32 T1ReadLong(u8 * mem, u32 addr)
|
|
44 {
|
|
45 #ifdef WORDS_BIGENDIAN
|
|
46 return (mem[addr + 3] << 24 | mem[addr + 2] << 16 |
|
|
47 mem[addr + 1] << 8 | mem[addr]);
|
|
48 #else
|
|
49 return *((u32 *)mem + (addr>>2));
|
|
50 #endif
|
|
51 }
|
|
52
|
|
53 static INLINE u64 T1ReadQuad(u8 * mem, u32 addr)
|
|
54 {
|
|
55 #ifdef WORDS_BIGENDIAN
|
|
56 return (mem[addr + 7] << 56 | mem[addr + 6] << 48 |
|
|
57 mem[addr + 5] << 40 | mem[addr + 4] << 32 |
|
|
58 mem[addr + 3] << 24 | mem[addr + 2] << 16 |
|
|
59 mem[addr + 1] << 8 | mem[addr]);
|
|
60 #else
|
|
61 return *((u64 *) (mem + addr));
|
|
62 #endif
|
|
63 }
|
|
64
|
|
65 static INLINE void T1WriteByte(u8 * mem, u32 addr, u8 val)
|
|
66 {
|
|
67 mem[addr] = val;
|
|
68 }
|
|
69
|
|
70 static INLINE void T1WriteWord(u8 * mem, u32 addr, u16 val)
|
|
71 {
|
|
72 #ifdef WORDS_BIGENDIAN
|
|
73 mem[addr + 1] = val >> 8;
|
|
74 mem[addr] = val & 0xFF;
|
|
75 #else
|
|
76 *((u16 *) (mem + addr)) = val;
|
|
77 #endif
|
|
78 }
|
|
79
|
|
80 static INLINE void T1WriteLong(u8 * mem, u32 addr, u32 val)
|
|
81 {
|
|
82 #ifdef WORDS_BIGENDIAN
|
|
83 mem[addr + 3] = val >> 24;
|
|
84 mem[addr + 2] = (val >> 16) & 0xFF;
|
|
85 mem[addr + 1] = (val >> 8) & 0xFF;
|
|
86 mem[addr] = val & 0xFF;
|
|
87 #else
|
|
88 *((u32 *) (mem + addr)) = val;
|
|
89 #endif
|
|
90 }
|
|
91
|
|
92 /* Type 2 Memory, faster for word (16 bits) accesses */
|
|
93
|
|
94 static INLINE u8 T2ReadByte(u8 * mem, u32 addr)
|
|
95 {
|
|
96 #ifdef WORDS_BIGENDIAN
|
|
97 return mem[addr ^ 1];
|
|
98 #else
|
|
99 return mem[addr];
|
|
100 #endif
|
|
101 }
|
|
102
|
|
103 static INLINE u16 T2ReadWord(u8 * mem, u32 addr)
|
|
104 {
|
|
105 return *((u16 *) (mem + addr));
|
|
106 }
|
|
107
|
|
108 static INLINE u32 T2ReadLong(u8 * mem, u32 addr)
|
|
109 {
|
|
110 #ifdef WORDS_BIGENDIAN
|
|
111 return *((u16 *) (mem + addr + 2)) << 16 | *((u16 *) (mem + addr));
|
|
112 #else
|
|
113 return *((u32 *) (mem + addr));
|
|
114 #endif
|
|
115 }
|
|
116
|
|
117 static INLINE void T2WriteByte(u8 * mem, u32 addr, u8 val)
|
|
118 {
|
|
119 #ifdef WORDS_BIGENDIAN
|
|
120 mem[addr ^ 1] = val;
|
|
121 #else
|
|
122 mem[addr] = val;
|
|
123 #endif
|
|
124 }
|
|
125
|
|
126 static INLINE void T2WriteWord(u8 * mem, u32 addr, u16 val)
|
|
127 {
|
|
128 *((u16 *) (mem + addr)) = val;
|
|
129 }
|
|
130
|
|
131 static INLINE void T2WriteLong(u8 * mem, u32 addr, u32 val)
|
|
132 {
|
|
133 #ifdef WORDS_BIGENDIAN
|
|
134 *((u16 *) (mem + addr + 2)) = val >> 16;
|
|
135 *((u16 *) (mem + addr)) = val & 0xFFFF;
|
|
136 #else
|
|
137 *((u32 *) (mem + addr)) = val;
|
|
138 #endif
|
|
139 }
|
|
140
|
|
141 #endif
|