Debian Codegraph @

mednafen/masmem.h

raw symbols SCIP
1 #ifndef __MDFN_PSX_MASMEM_H
2 #define __MDFN_PSX_MASMEM_H
3
4 // TODO, WIP (big-endian stores and loads not fully supported yet)
5
6 #ifdef MSB_FIRST
7 #define MAS_NATIVE_IS_BIGENDIAN 1
8 #else
9 #define MAS_NATIVE_IS_BIGENDIAN 0
10 #endif
11
12 static INLINE uint16 LoadU16_RBO(const uint16 *a)
13 {
14 #ifdef ARCH_POWERPC
15 uint16 tmp;
16
17 __asm__ ("lhbrx %0, %y1" : "=r"(tmp) : "Z"(*a));
18
19 return(tmp);
20
21 #else
22 uint16 tmp = *a;
23 return((tmp << 8) | (tmp >> 8));
24 #endif
25 }
26
27 static INLINE uint32 LoadU32_RBO(const uint32 *a)
28 {
29 #ifdef ARCH_POWERPC
30 uint32 tmp;
31
32 __asm__ ("lwbrx %0, %y1" : "=r"(tmp) : "Z"(*a));
33
34 return(tmp);
35 #else
36 uint32 tmp = *a;
37 return((tmp << 24) | ((tmp & 0xFF00) << 8) | ((tmp >> 8) & 0xFF00) | (tmp >> 24));
38 #endif
39 }
40
41 static INLINE void StoreU16_RBO(uint16 *a, const uint16 v)
42 {
43 #ifdef ARCH_POWERPC
44 __asm__ ("sthbrx %0, %y1" : : "r"(v), "Z"(*a));
45 #else
46 uint16 tmp = (v << 8) | (v >> 8);
47 *a = tmp;
48 #endif
49 }
50
51 static INLINE void StoreU32_RBO(uint32 *a, const uint32 v)
52 {
53 #ifdef ARCH_POWERPC
54 __asm__ ("stwbrx %0, %y1" : : "r"(v), "Z"(*a));
55 #else
56 uint32 tmp = (v << 24) | ((v & 0xFF00) << 8) | ((v >> 8) & 0xFF00) | (v >> 24);
57 *a = tmp;
58 #endif
59 }
60
61 static INLINE uint16 LoadU16_LE(const uint16 *a)
62 {
63 #ifdef MSB_FIRST
64 return LoadU16_RBO(a);
65 #else
66 return *a;
67 #endif
68 }
69
70 static INLINE uint32 LoadU32_LE(const uint32 *a)
71 {
72 #ifdef MSB_FIRST
73 return LoadU32_RBO(a);
74 #else
75 return *a;
76 #endif
77 }
78
79 static INLINE void StoreU16_LE(uint16 *a, const uint16 v)
80 {
81 #ifdef MSB_FIRST
82 StoreU16_RBO(a, v);
83 #else
84 *a = v;
85 #endif
86 }
87
88 static INLINE void StoreU32_LE(uint32 *a, const uint32 v)
89 {
90 #ifdef MSB_FIRST
91 StoreU32_RBO(a, v);
92 #else
93 *a = v;
94 #endif
95 }
96
97
98 // address must not be >= size specified by template parameter, and address must be a multiple of the byte-size of the
99 // unit(1,2,4) being read(except for Read/WriteU24, which only needs to be byte-aligned).
100 //
101 // max_unit_type should be uint16 or uint32
102 //
103 // pre_padding and post_padding are specified in units of sizeof(max_unit_type).
104 //
105 template<unsigned size, typename max_unit_type, bool big_endian> //, unsigned pre_padding_count, unsigned post_padding_count>
106 struct MultiAccessSizeMem
107 {
108 //max_unit_type pre_padding[pre_padding_count ? pre_padding_count : 1];
109
110 union
111 {
112 uint8 data8[size];
113 uint16 data16[size / sizeof(uint16)];
114 uint32 data32[size / sizeof(uint32)];
115 };
116
117 //max_unit_type post_padding[post_padding_count ? post_padding_count : 1];
118
119 INLINE uint8 ReadU8(uint32 address)
120 {
121 return data8[address];
122 }
123
124 INLINE uint16 ReadU16(uint32 address)
125 {
126 if(MAS_NATIVE_IS_BIGENDIAN == big_endian)
127 return *(uint16*)(((uint8*)data16) + address);
128 else
129 return LoadU16_RBO((uint16*)(((uint8*)data16) + address));
130 }
131
132 INLINE uint32 ReadU32(uint32 address)
133 {
134 if(MAS_NATIVE_IS_BIGENDIAN == big_endian)
135 return *(uint32*)(((uint8*)data32) + address);
136 else
137 return LoadU32_RBO((uint32*)(((uint8*)data32) + address));
138 }
139
140 INLINE uint32 ReadU24(uint32 address)
141 {
142 uint32 ret;
143
144 if(!big_endian)
145 {
146 ret = ReadU8(address) | (ReadU8(address + 1) << 8) | (ReadU8(address + 2) << 16);
147 }
148
149 return(ret);
150 }
151
152
153 INLINE void WriteU8(uint32 address, uint8 value)
154 {
155 data8[address] = value;
156 }
157
158 INLINE void WriteU16(uint32 address, uint16 value)
159 {
160 if(MAS_NATIVE_IS_BIGENDIAN == big_endian)
161 *(uint16*)(((uint8*)data16) + address) = value;
162 else
163 StoreU16_RBO((uint16*)(((uint8*)data16) + address), value);
164 }
165
166 INLINE void WriteU32(uint32 address, uint32 value)
167 {
168 if(MAS_NATIVE_IS_BIGENDIAN == big_endian)
169 *(uint32*)(((uint8*)data32) + address) = value;
170 else
171 StoreU32_RBO((uint32*)(((uint8*)data32) + address), value);
172 }
173
174 INLINE void WriteU24(uint32 address, uint32 value)
175 {
176 if(!big_endian)
177 {
178 WriteU8(address + 0, value >> 0);
179 WriteU8(address + 1, value >> 8);
180 WriteU8(address + 2, value >> 16);
181 }
182 }
183
184 template<typename T>
185 INLINE T Read(uint32 address)
186 {
187 if(sizeof(T) == 4)
188 return(ReadU32(address));
189 else if(sizeof(T) == 2)
190 return(ReadU16(address));
191 else
192 return(ReadU8(address));
193 }
194
195 template<typename T>
196 INLINE void Write(uint32 address, T value)
197 {
198 if(sizeof(T) == 4)
199 WriteU32(address, value);
200 else if(sizeof(T) == 2)
201 WriteU16(address, value);
202 else
203 WriteU8(address, value);
204 }
205 };
206
207 #undef MAS_NATIVE_IS_BIGENDIAN
208
209 #endif