1 |
/* |
2 |
* Copyright (C) 2005-2007 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: cpu_ppc_instr_loadstore.c,v 1.10 2006/12/30 13:30:55 debug Exp $ |
29 |
* |
30 |
* POWER/PowerPC load/store instructions. |
31 |
* |
32 |
* |
33 |
* Load/store instructions have the following arguments: |
34 |
* |
35 |
* arg[0] = pointer to the register to load to or store from |
36 |
* arg[1] = pointer to the base register |
37 |
* |
38 |
* arg[2] = offset (as an int32_t) |
39 |
* (or, for Indexed load/stores: pointer to index register) |
40 |
*/ |
41 |
|
42 |
|
43 |
#ifndef LS_IGNOREOFS |
44 |
void LS_GENERIC_N(struct cpu *cpu, struct ppc_instr_call *ic) |
45 |
{ |
46 |
#ifdef MODE32 |
47 |
uint32_t addr = |
48 |
#else |
49 |
uint64_t addr = |
50 |
#endif |
51 |
reg(ic->arg[1]) + |
52 |
#ifdef LS_INDEXED |
53 |
reg(ic->arg[2]); |
54 |
#else |
55 |
(int32_t)ic->arg[2]; |
56 |
#endif |
57 |
unsigned char data[LS_SIZE]; |
58 |
|
59 |
/* Synchronize the PC: */ |
60 |
int low_pc = ((size_t)ic - (size_t)cpu->cd.ppc.cur_ic_page) |
61 |
/ sizeof(struct ppc_instr_call); |
62 |
cpu->pc &= ~((PPC_IC_ENTRIES_PER_PAGE-1) << PPC_INSTR_ALIGNMENT_SHIFT); |
63 |
cpu->pc += (low_pc << PPC_INSTR_ALIGNMENT_SHIFT); |
64 |
|
65 |
#ifndef LS_B |
66 |
if ((addr & 0xfff) + LS_SIZE-1 > 0xfff) { |
67 |
fatal("PPC LOAD/STORE misalignment across page boundary: TODO" |
68 |
" (addr=0x%08x, LS_SIZE=%i)\n", (int)addr, LS_SIZE); |
69 |
exit(1); |
70 |
} |
71 |
#endif |
72 |
|
73 |
#ifdef LS_LOAD |
74 |
if (!cpu->memory_rw(cpu, cpu->mem, addr, data, sizeof(data), |
75 |
MEM_READ, CACHE_DATA)) { |
76 |
/* Exception. */ |
77 |
return; |
78 |
} |
79 |
#ifdef LS_B |
80 |
reg(ic->arg[0]) = |
81 |
#ifndef LS_ZERO |
82 |
(int8_t) |
83 |
#endif |
84 |
data[0]; |
85 |
#endif |
86 |
#ifdef LS_H |
87 |
reg(ic->arg[0]) = |
88 |
#ifdef LS_BYTEREVERSE |
89 |
((data[1] << 8) + data[0]); |
90 |
#else |
91 |
#ifndef LS_ZERO |
92 |
(int16_t) |
93 |
#endif |
94 |
((data[0] << 8) + data[1]); |
95 |
#endif /* !BYTEREVERSE */ |
96 |
#endif |
97 |
#ifdef LS_W |
98 |
reg(ic->arg[0]) = |
99 |
#ifdef LS_BYTEREVERSE |
100 |
((data[3] << 24) + (data[2] << 16) + |
101 |
(data[1] << 8) + data[0]); |
102 |
#else /* !LS_BYTEREVERSE */ |
103 |
#ifndef LS_ZERO |
104 |
(int32_t) |
105 |
#else |
106 |
(uint32_t) |
107 |
#endif |
108 |
((data[0] << 24) + (data[1] << 16) + |
109 |
(data[2] << 8) + data[3]); |
110 |
#endif /* !LS_BYTEREVERSE */ |
111 |
#endif |
112 |
#ifdef LS_D |
113 |
(*(uint64_t *)(ic->arg[0])) = |
114 |
((uint64_t)data[0] << 56) + ((uint64_t)data[1] << 48) + |
115 |
((uint64_t)data[2] << 40) + ((uint64_t)data[3] << 32) + |
116 |
((uint64_t)data[4] << 24) + (data[5] << 16) + |
117 |
(data[6] << 8) + data[7]; |
118 |
#endif |
119 |
|
120 |
#else /* store: */ |
121 |
|
122 |
#ifdef LS_B |
123 |
data[0] = reg(ic->arg[0]); |
124 |
#endif |
125 |
#ifdef LS_H |
126 |
#ifdef LS_BYTEREVERSE |
127 |
data[0] = reg(ic->arg[0]); |
128 |
data[1] = reg(ic->arg[0]) >> 8; |
129 |
#else |
130 |
data[0] = reg(ic->arg[0]) >> 8; |
131 |
data[1] = reg(ic->arg[0]); |
132 |
#endif |
133 |
#endif |
134 |
#ifdef LS_W |
135 |
#ifdef LS_BYTEREVERSE |
136 |
data[0] = reg(ic->arg[0]); |
137 |
data[1] = reg(ic->arg[0]) >> 8; |
138 |
data[2] = reg(ic->arg[0]) >> 16; |
139 |
data[3] = reg(ic->arg[0]) >> 24; |
140 |
#else |
141 |
data[0] = reg(ic->arg[0]) >> 24; |
142 |
data[1] = reg(ic->arg[0]) >> 16; |
143 |
data[2] = reg(ic->arg[0]) >> 8; |
144 |
data[3] = reg(ic->arg[0]); |
145 |
#endif /* !LS_BYTEREVERSE */ |
146 |
#endif |
147 |
#ifdef LS_D |
148 |
{ uint64_t x = *(uint64_t *)(ic->arg[0]); |
149 |
data[0] = x >> 56; |
150 |
data[1] = x >> 48; |
151 |
data[2] = x >> 40; |
152 |
data[3] = x >> 32; |
153 |
data[4] = x >> 24; |
154 |
data[5] = x >> 16; |
155 |
data[6] = x >> 8; |
156 |
data[7] = x; } |
157 |
#endif |
158 |
if (!cpu->memory_rw(cpu, cpu->mem, addr, data, sizeof(data), |
159 |
MEM_WRITE, CACHE_DATA)) { |
160 |
/* Exception. */ |
161 |
return; |
162 |
} |
163 |
#endif |
164 |
|
165 |
#ifdef LS_UPDATE |
166 |
reg(ic->arg[1]) = addr; |
167 |
#endif |
168 |
} |
169 |
#endif |
170 |
|
171 |
|
172 |
void LS_N(struct cpu *cpu, struct ppc_instr_call *ic) |
173 |
{ |
174 |
#ifdef MODE32 |
175 |
uint32_t addr = |
176 |
#else |
177 |
uint64_t addr = |
178 |
#endif |
179 |
reg(ic->arg[1]) |
180 |
#ifdef LS_INDEXED |
181 |
+ reg(ic->arg[2]) |
182 |
#else |
183 |
#ifndef LS_IGNOREOFS |
184 |
+ (int32_t)ic->arg[2] |
185 |
#endif |
186 |
#endif |
187 |
; |
188 |
|
189 |
unsigned char *page = cpu->cd.ppc. |
190 |
#ifdef LS_LOAD |
191 |
host_load |
192 |
#else |
193 |
host_store |
194 |
#endif |
195 |
[addr >> 12]; |
196 |
#ifdef LS_UPDATE |
197 |
uint32_t new_addr = addr; |
198 |
#endif |
199 |
|
200 |
#ifndef LS_B |
201 |
if (addr & (LS_SIZE-1)) { |
202 |
LS_GENERIC_N(cpu, ic); |
203 |
return; |
204 |
} |
205 |
#endif |
206 |
|
207 |
|
208 |
#ifndef MODE32 |
209 |
/*******************************************/ |
210 |
if (!cpu->is_32bit) { |
211 |
LS_GENERIC_N(cpu, ic); |
212 |
return; |
213 |
} |
214 |
/*******************************************/ |
215 |
#endif |
216 |
|
217 |
|
218 |
if (page == NULL) { |
219 |
LS_GENERIC_N(cpu, ic); |
220 |
return; |
221 |
} else { |
222 |
addr &= 4095; |
223 |
#ifdef LS_LOAD |
224 |
/* Load: */ |
225 |
#ifdef LS_B |
226 |
reg(ic->arg[0]) = |
227 |
#ifndef LS_ZERO |
228 |
(int8_t) |
229 |
#endif |
230 |
page[addr]; |
231 |
#endif /* LS_B */ |
232 |
#ifdef LS_H |
233 |
reg(ic->arg[0]) = |
234 |
#ifdef LS_BYTEREVERSE |
235 |
((page[addr+1] << 8) + page[addr]); |
236 |
#else |
237 |
#ifndef LS_ZERO |
238 |
(int16_t) |
239 |
#endif |
240 |
((page[addr] << 8) + page[addr+1]); |
241 |
#endif /* !BYTEREVERSE */ |
242 |
#endif /* LS_H */ |
243 |
#ifdef LS_W |
244 |
reg(ic->arg[0]) = |
245 |
#ifdef LS_BYTEREVERSE |
246 |
((page[addr+3] << 24) + (page[addr+2] << 16) + |
247 |
(page[addr+1] << 8) + page[addr]); |
248 |
#else /* !LS_BYTEREVERSE */ |
249 |
#ifndef LS_ZERO |
250 |
(int32_t) |
251 |
#else |
252 |
(uint32_t) |
253 |
#endif |
254 |
((page[addr] << 24) + (page[addr+1] << 16) + |
255 |
(page[addr+2] << 8) + page[addr+3]); |
256 |
#endif /* !LS_BYTEREVERSE */ |
257 |
#endif /* LS_W */ |
258 |
#ifdef LS_D |
259 |
(*(uint64_t *)(ic->arg[0])) = |
260 |
((uint64_t)page[addr+0] << 56) + |
261 |
((uint64_t)page[addr+1] << 48) + |
262 |
((uint64_t)page[addr+2] << 40) + |
263 |
((uint64_t)page[addr+3] << 32) + |
264 |
((uint64_t)page[addr+4] << 24) + (page[addr+5] << 16) + |
265 |
(page[addr+6] << 8) + page[addr+7]; |
266 |
#endif /* LS_D */ |
267 |
|
268 |
#else /* !LS_LOAD */ |
269 |
|
270 |
/* Store: */ |
271 |
#ifdef LS_B |
272 |
page[addr] = reg(ic->arg[0]); |
273 |
#endif |
274 |
#ifdef LS_H |
275 |
#ifdef LS_BYTEREVERSE |
276 |
page[addr] = reg(ic->arg[0]); |
277 |
page[addr+1] = reg(ic->arg[0]) >> 8; |
278 |
#else |
279 |
page[addr] = reg(ic->arg[0]) >> 8; |
280 |
page[addr+1] = reg(ic->arg[0]); |
281 |
#endif /* !BYTEREVERSE */ |
282 |
#endif |
283 |
#ifdef LS_W |
284 |
#ifdef LS_BYTEREVERSE |
285 |
page[addr] = reg(ic->arg[0]); |
286 |
page[addr+1] = reg(ic->arg[0]) >> 8; |
287 |
page[addr+2] = reg(ic->arg[0]) >> 16; |
288 |
page[addr+3] = reg(ic->arg[0]) >> 24; |
289 |
#else |
290 |
page[addr] = reg(ic->arg[0]) >> 24; |
291 |
page[addr+1] = reg(ic->arg[0]) >> 16; |
292 |
page[addr+2] = reg(ic->arg[0]) >> 8; |
293 |
page[addr+3] = reg(ic->arg[0]); |
294 |
#endif /* !LS_BYTEREVERSE */ |
295 |
#endif |
296 |
#ifdef LS_D |
297 |
{ uint64_t x = *(uint64_t *)(ic->arg[0]); |
298 |
page[addr] = x >> 56; |
299 |
page[addr+1] = x >> 48; |
300 |
page[addr+2] = x >> 40; |
301 |
page[addr+3] = x >> 32; |
302 |
page[addr+4] = x >> 24; |
303 |
page[addr+5] = x >> 16; |
304 |
page[addr+6] = x >> 8; |
305 |
page[addr+7] = x; } |
306 |
#endif |
307 |
#endif /* !LS_LOAD */ |
308 |
} |
309 |
|
310 |
#ifdef LS_UPDATE |
311 |
reg(ic->arg[1]) = new_addr; |
312 |
#endif |
313 |
} |
314 |
|