fpu_calcea.c revision 1.22.2.1 1 1.22.2.1 jruoho /* $NetBSD: fpu_calcea.c,v 1.22.2.1 2011/06/06 09:05:56 jruoho Exp $ */
2 1.1 briggs
3 1.1 briggs /*
4 1.1 briggs * Copyright (c) 1995 Gordon W. Ross
5 1.1 briggs * portion Copyright (c) 1995 Ken Nakata
6 1.1 briggs * All rights reserved.
7 1.1 briggs *
8 1.1 briggs * Redistribution and use in source and binary forms, with or without
9 1.1 briggs * modification, are permitted provided that the following conditions
10 1.1 briggs * are met:
11 1.1 briggs * 1. Redistributions of source code must retain the above copyright
12 1.1 briggs * notice, this list of conditions and the following disclaimer.
13 1.1 briggs * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 briggs * notice, this list of conditions and the following disclaimer in the
15 1.1 briggs * documentation and/or other materials provided with the distribution.
16 1.1 briggs * 3. The name of the author may not be used to endorse or promote products
17 1.1 briggs * derived from this software without specific prior written permission.
18 1.1 briggs * 4. All advertising materials mentioning features or use of this software
19 1.1 briggs * must display the following acknowledgement:
20 1.1 briggs * This product includes software developed by Gordon Ross
21 1.1 briggs *
22 1.1 briggs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 briggs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 briggs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 briggs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 briggs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 briggs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 briggs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 briggs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 briggs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 briggs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 briggs */
33 1.15 lukem
34 1.22 mrg #include "opt_m68k_arch.h"
35 1.22 mrg
36 1.15 lukem #include <sys/cdefs.h>
37 1.22.2.1 jruoho __KERNEL_RCSID(0, "$NetBSD: fpu_calcea.c,v 1.22.2.1 2011/06/06 09:05:56 jruoho Exp $");
38 1.1 briggs
39 1.3 briggs #include <sys/param.h>
40 1.1 briggs #include <sys/signal.h>
41 1.4 briggs #include <sys/systm.h>
42 1.1 briggs #include <machine/frame.h>
43 1.10 is #include <m68k/m68k.h>
44 1.1 briggs
45 1.1 briggs #include "fpu_emulate.h"
46 1.1 briggs
47 1.22.2.1 jruoho #ifdef DEBUG_FPE
48 1.22.2.1 jruoho #define DPRINTF(x) printf x
49 1.22.2.1 jruoho #else
50 1.22.2.1 jruoho #define DPRINTF(x) do {} while (/* CONSTCOND */ 0)
51 1.22.2.1 jruoho #endif
52 1.22.2.1 jruoho
53 1.1 briggs /*
54 1.1 briggs * Prototypes of static functions
55 1.1 briggs */
56 1.22.2.1 jruoho static int decode_ea6(struct frame *, struct instruction *,
57 1.22.2.1 jruoho struct insn_ea *, int);
58 1.22.2.1 jruoho static int fetch_immed(struct frame *, struct instruction *, int *);
59 1.22.2.1 jruoho static int fetch_disp(struct frame *, struct instruction *, int, int *);
60 1.22.2.1 jruoho static int calc_ea(struct insn_ea *, char *, char **);
61 1.4 briggs
62 1.1 briggs /*
63 1.1 briggs * Helper routines for dealing with "effective address" values.
64 1.1 briggs */
65 1.1 briggs
66 1.1 briggs /*
67 1.1 briggs * Decode an effective address into internal form.
68 1.1 briggs * Returns zero on success, else signal number.
69 1.1 briggs */
70 1.1 briggs int
71 1.22.2.1 jruoho fpu_decode_ea(struct frame *frame, struct instruction *insn,
72 1.22.2.1 jruoho struct insn_ea *ea, int modreg)
73 1.1 briggs {
74 1.22.2.1 jruoho int sig;
75 1.1 briggs
76 1.22.2.1 jruoho #ifdef DIAGNOSTIC
77 1.22.2.1 jruoho if (insn->is_datasize < 0)
78 1.22.2.1 jruoho panic("%s: called with uninitialized datasize", __func__);
79 1.1 briggs #endif
80 1.1 briggs
81 1.22.2.1 jruoho sig = 0;
82 1.1 briggs
83 1.22.2.1 jruoho /* Set the most common value here. */
84 1.22.2.1 jruoho ea->ea_regnum = 8 + (modreg & 7);
85 1.1 briggs
86 1.22.2.1 jruoho if ((modreg & 060) == 0) {
87 1.22.2.1 jruoho /* register direct */
88 1.22.2.1 jruoho ea->ea_regnum = modreg & 0xf;
89 1.22.2.1 jruoho ea->ea_flags = EA_DIRECT;
90 1.22.2.1 jruoho DPRINTF(("%s: register direct reg=%d\n",
91 1.22.2.1 jruoho __func__, ea->ea_regnum));
92 1.22.2.1 jruoho } else if ((modreg & 077) == 074) {
93 1.22.2.1 jruoho /* immediate */
94 1.22.2.1 jruoho ea->ea_flags = EA_IMMED;
95 1.22.2.1 jruoho sig = fetch_immed(frame, insn, &ea->ea_immed[0]);
96 1.22.2.1 jruoho DPRINTF(("%s: immediate size=%d\n",
97 1.22.2.1 jruoho __func__, insn->is_datasize));
98 1.22.2.1 jruoho }
99 1.22.2.1 jruoho /*
100 1.22.2.1 jruoho * rest of the address modes need to be separately
101 1.22.2.1 jruoho * handled for the LC040 and the others.
102 1.22.2.1 jruoho */
103 1.10 is #if 0 /* XXX */
104 1.22.2.1 jruoho else if (frame->f_format == 4 && frame->f_fmt4.f_fa) {
105 1.22.2.1 jruoho /* LC040 */
106 1.22.2.1 jruoho ea->ea_flags = EA_FRAME_EA;
107 1.22.2.1 jruoho ea->ea_fea = frame->f_fmt4.f_fa;
108 1.22.2.1 jruoho DPRINTF(("%s: 68LC040 - in-frame EA (%p) size %d\n",
109 1.22.2.1 jruoho __func__, (void *)ea->ea_fea, insn->is_datasize));
110 1.22.2.1 jruoho if ((modreg & 070) == 030) {
111 1.22.2.1 jruoho /* postincrement mode */
112 1.22.2.1 jruoho ea->ea_flags |= EA_POSTINCR;
113 1.22.2.1 jruoho } else if ((modreg & 070) == 040) {
114 1.22.2.1 jruoho /* predecrement mode */
115 1.22.2.1 jruoho ea->ea_flags |= EA_PREDECR;
116 1.10 is #ifdef M68060
117 1.10 is #if defined(M68020) || defined(M68030) || defined(M68040)
118 1.22.2.1 jruoho if (cputype == CPU_68060)
119 1.10 is #endif
120 1.22.2.1 jruoho if (insn->is_datasize == 12)
121 1.22.2.1 jruoho ea->ea_fea -= 8;
122 1.10 is #endif
123 1.22.2.1 jruoho }
124 1.1 briggs }
125 1.10 is #endif /* XXX */
126 1.22.2.1 jruoho else {
127 1.22.2.1 jruoho /* 020/030 */
128 1.22.2.1 jruoho switch (modreg & 070) {
129 1.22.2.1 jruoho
130 1.22.2.1 jruoho case 020: /* (An) */
131 1.22.2.1 jruoho ea->ea_flags = 0;
132 1.22.2.1 jruoho DPRINTF(("%s: register indirect reg=%d\n",
133 1.22.2.1 jruoho __func__, ea->ea_regnum));
134 1.22.2.1 jruoho break;
135 1.22.2.1 jruoho
136 1.22.2.1 jruoho case 030: /* (An)+ */
137 1.22.2.1 jruoho ea->ea_flags = EA_POSTINCR;
138 1.22.2.1 jruoho DPRINTF(("%s: reg indirect postincrement reg=%d\n",
139 1.22.2.1 jruoho __func__, ea->ea_regnum));
140 1.22.2.1 jruoho break;
141 1.22.2.1 jruoho
142 1.22.2.1 jruoho case 040: /* -(An) */
143 1.22.2.1 jruoho ea->ea_flags = EA_PREDECR;
144 1.22.2.1 jruoho DPRINTF(("%s: reg indirect predecrement reg=%d\n",
145 1.22.2.1 jruoho __func__, ea->ea_regnum));
146 1.22.2.1 jruoho break;
147 1.22.2.1 jruoho
148 1.22.2.1 jruoho case 050: /* (d16,An) */
149 1.22.2.1 jruoho ea->ea_flags = EA_OFFSET;
150 1.22.2.1 jruoho sig = fetch_disp(frame, insn, 1, &ea->ea_offset);
151 1.22.2.1 jruoho DPRINTF(("%s: reg indirect with displacement reg=%d\n",
152 1.22.2.1 jruoho __func__, ea->ea_regnum));
153 1.8 briggs break;
154 1.8 briggs
155 1.22.2.1 jruoho case 060: /* (d8,An,Xn) */
156 1.22.2.1 jruoho ea->ea_flags = EA_INDEXED;
157 1.22.2.1 jruoho sig = decode_ea6(frame, insn, ea, modreg);
158 1.22.2.1 jruoho break;
159 1.22.2.1 jruoho
160 1.22.2.1 jruoho case 070: /* misc. */
161 1.22.2.1 jruoho ea->ea_regnum = (modreg & 7);
162 1.22.2.1 jruoho switch (modreg & 7) {
163 1.22.2.1 jruoho
164 1.22.2.1 jruoho case 0: /* (xxxx).W */
165 1.22.2.1 jruoho ea->ea_flags = EA_ABS;
166 1.22.2.1 jruoho sig = fetch_disp(frame, insn, 1,
167 1.22.2.1 jruoho &ea->ea_absaddr);
168 1.22.2.1 jruoho DPRINTF(("%s: absolute address (word)\n",
169 1.22.2.1 jruoho __func__));
170 1.22.2.1 jruoho break;
171 1.22.2.1 jruoho
172 1.22.2.1 jruoho case 1: /* (xxxxxxxx).L */
173 1.22.2.1 jruoho ea->ea_flags = EA_ABS;
174 1.22.2.1 jruoho sig = fetch_disp(frame, insn, 2,
175 1.22.2.1 jruoho &ea->ea_absaddr);
176 1.22.2.1 jruoho DPRINTF(("%s: absolute address (long)\n",
177 1.22.2.1 jruoho __func__));
178 1.22.2.1 jruoho break;
179 1.22.2.1 jruoho
180 1.22.2.1 jruoho case 2: /* (d16,PC) */
181 1.22.2.1 jruoho ea->ea_flags = EA_PC_REL | EA_OFFSET;
182 1.22.2.1 jruoho sig = fetch_disp(frame, insn, 1,
183 1.22.2.1 jruoho &ea->ea_absaddr);
184 1.22.2.1 jruoho DPRINTF(("%s: pc relative word displacement\n",
185 1.22.2.1 jruoho __func__));
186 1.22.2.1 jruoho break;
187 1.22.2.1 jruoho
188 1.22.2.1 jruoho case 3: /* (d8,PC,Xn) */
189 1.22.2.1 jruoho ea->ea_flags = EA_PC_REL | EA_INDEXED;
190 1.22.2.1 jruoho sig = decode_ea6(frame, insn, ea, modreg);
191 1.22.2.1 jruoho break;
192 1.22.2.1 jruoho
193 1.22.2.1 jruoho case 4: /* #data */
194 1.22.2.1 jruoho /* it should have been taken care of earlier */
195 1.22.2.1 jruoho default:
196 1.22.2.1 jruoho DPRINTF(("%s: invalid addr mode (7,%d)\n",
197 1.22.2.1 jruoho __func__, modreg & 7));
198 1.22.2.1 jruoho return SIGILL;
199 1.22.2.1 jruoho }
200 1.22.2.1 jruoho break;
201 1.22.2.1 jruoho }
202 1.22.2.1 jruoho }
203 1.22.2.1 jruoho ea->ea_moffs = 0;
204 1.1 briggs
205 1.22.2.1 jruoho return sig;
206 1.1 briggs }
207 1.1 briggs
208 1.1 briggs /*
209 1.1 briggs * Decode Mode=6 address modes
210 1.1 briggs */
211 1.1 briggs static int
212 1.22.2.1 jruoho decode_ea6(struct frame *frame, struct instruction *insn, struct insn_ea *ea,
213 1.22.2.1 jruoho int modreg)
214 1.1 briggs {
215 1.22.2.1 jruoho int extword, idx;
216 1.22.2.1 jruoho int basedisp, outerdisp;
217 1.22.2.1 jruoho int bd_size, od_size;
218 1.22.2.1 jruoho int sig;
219 1.22.2.1 jruoho
220 1.22.2.1 jruoho extword = fusword((void *)(insn->is_pc + insn->is_advance));
221 1.22.2.1 jruoho if (extword < 0) {
222 1.22.2.1 jruoho return SIGSEGV;
223 1.1 briggs }
224 1.22.2.1 jruoho insn->is_advance += 2;
225 1.22.2.1 jruoho
226 1.22.2.1 jruoho /* get register index */
227 1.22.2.1 jruoho ea->ea_idxreg = (extword >> 12) & 0xf;
228 1.22.2.1 jruoho idx = frame->f_regs[ea->ea_idxreg];
229 1.22.2.1 jruoho if ((extword & 0x0800) == 0) {
230 1.22.2.1 jruoho /* if word sized index, sign-extend */
231 1.22.2.1 jruoho idx &= 0xffff;
232 1.22.2.1 jruoho if (idx & 0x8000) {
233 1.22.2.1 jruoho idx |= 0xffff0000;
234 1.22.2.1 jruoho }
235 1.22.2.1 jruoho }
236 1.22.2.1 jruoho /* scale register index */
237 1.22.2.1 jruoho idx <<= ((extword >> 9) & 3);
238 1.22.2.1 jruoho
239 1.22.2.1 jruoho if ((extword & 0x100) == 0) {
240 1.22.2.1 jruoho /* brief extension word - sign-extend the displacement */
241 1.22.2.1 jruoho basedisp = (extword & 0xff);
242 1.22.2.1 jruoho if (basedisp & 0x80) {
243 1.22.2.1 jruoho basedisp |= 0xffffff00;
244 1.22.2.1 jruoho }
245 1.22.2.1 jruoho
246 1.22.2.1 jruoho ea->ea_basedisp = idx + basedisp;
247 1.22.2.1 jruoho ea->ea_outerdisp = 0;
248 1.22.2.1 jruoho DPRINTF(("%s: brief ext word idxreg=%d, basedisp=%08x\n",
249 1.22.2.1 jruoho __func__, ea->ea_idxreg, ea->ea_basedisp));
250 1.22.2.1 jruoho } else {
251 1.22.2.1 jruoho /* full extension word */
252 1.22.2.1 jruoho if (extword & 0x80) {
253 1.22.2.1 jruoho ea->ea_flags |= EA_BASE_SUPPRSS;
254 1.22.2.1 jruoho }
255 1.22.2.1 jruoho bd_size = ((extword >> 4) & 3) - 1;
256 1.22.2.1 jruoho od_size = (extword & 3) - 1;
257 1.22.2.1 jruoho sig = fetch_disp(frame, insn, bd_size, &basedisp);
258 1.22.2.1 jruoho if (sig)
259 1.22.2.1 jruoho return sig;
260 1.22.2.1 jruoho if (od_size >= 0)
261 1.22.2.1 jruoho ea->ea_flags |= EA_MEM_INDIR;
262 1.22.2.1 jruoho sig = fetch_disp(frame, insn, od_size, &outerdisp);
263 1.22.2.1 jruoho if (sig)
264 1.22.2.1 jruoho return sig;
265 1.22.2.1 jruoho
266 1.22.2.1 jruoho switch (extword & 0x44) {
267 1.22.2.1 jruoho case 0: /* preindexed */
268 1.22.2.1 jruoho ea->ea_basedisp = basedisp + idx;
269 1.22.2.1 jruoho ea->ea_outerdisp = outerdisp;
270 1.22.2.1 jruoho break;
271 1.22.2.1 jruoho case 4: /* postindexed */
272 1.22.2.1 jruoho ea->ea_basedisp = basedisp;
273 1.22.2.1 jruoho ea->ea_outerdisp = outerdisp + idx;
274 1.22.2.1 jruoho break;
275 1.22.2.1 jruoho case 0x40: /* no index */
276 1.22.2.1 jruoho ea->ea_basedisp = basedisp;
277 1.22.2.1 jruoho ea->ea_outerdisp = outerdisp;
278 1.22.2.1 jruoho break;
279 1.22.2.1 jruoho default:
280 1.22.2.1 jruoho DPRINTF(("%s: invalid indirect mode: ext word %04x\n",
281 1.22.2.1 jruoho __func__, extword));
282 1.22.2.1 jruoho return SIGILL;
283 1.22.2.1 jruoho break;
284 1.22.2.1 jruoho }
285 1.22.2.1 jruoho DPRINTF(("%s: full ext idxreg=%d, basedisp=%x, outerdisp=%x\n",
286 1.22.2.1 jruoho __func__,
287 1.22.2.1 jruoho ea->ea_idxreg, ea->ea_basedisp, ea->ea_outerdisp));
288 1.22.2.1 jruoho }
289 1.22.2.1 jruoho DPRINTF(("%s: regnum=%d, flags=%x\n",
290 1.22.2.1 jruoho __func__, ea->ea_regnum, ea->ea_flags));
291 1.22.2.1 jruoho return 0;
292 1.1 briggs }
293 1.1 briggs
294 1.1 briggs /*
295 1.1 briggs * Load a value from an effective address.
296 1.1 briggs * Returns zero on success, else signal number.
297 1.1 briggs */
298 1.1 briggs int
299 1.22.2.1 jruoho fpu_load_ea(struct frame *frame, struct instruction *insn, struct insn_ea *ea,
300 1.22.2.1 jruoho char *dst)
301 1.1 briggs {
302 1.22.2.1 jruoho int *reg;
303 1.22.2.1 jruoho char *src;
304 1.22.2.1 jruoho int len, step;
305 1.22.2.1 jruoho int sig;
306 1.1 briggs
307 1.8 briggs #ifdef DIAGNOSTIC
308 1.22.2.1 jruoho if (ea->ea_regnum & ~0xF)
309 1.22.2.1 jruoho panic("%s: bad regnum", __func__);
310 1.1 briggs #endif
311 1.1 briggs
312 1.22.2.1 jruoho DPRINTF(("%s: frame at %p\n", __func__, frame));
313 1.22.2.1 jruoho /* dst is always int or larger. */
314 1.22.2.1 jruoho len = insn->is_datasize;
315 1.22.2.1 jruoho if (len < 4)
316 1.22.2.1 jruoho dst += (4 - len);
317 1.22.2.1 jruoho step = (len == 1 && ea->ea_regnum == 15 /* sp */) ? 2 : len;
318 1.1 briggs
319 1.11 is #if 0
320 1.22.2.1 jruoho if (ea->ea_flags & EA_FRAME_EA) {
321 1.22.2.1 jruoho /* Using LC040 frame EA */
322 1.8 briggs #ifdef DEBUG_FPE
323 1.22.2.1 jruoho if (ea->ea_flags & (EA_PREDECR|EA_POSTINCR)) {
324 1.22.2.1 jruoho printf("%s: frame ea %08x w/r%d\n",
325 1.22.2.1 jruoho __func__, ea->ea_fea, ea->ea_regnum);
326 1.22.2.1 jruoho } else {
327 1.22.2.1 jruoho printf("%s: frame ea %08x\n", __func__, ea->ea_fea);
328 1.22.2.1 jruoho }
329 1.22.2.1 jruoho #endif
330 1.22.2.1 jruoho src = (char *)ea->ea_fea;
331 1.22.2.1 jruoho copyin(src + ea->ea_moffs, dst, len);
332 1.22.2.1 jruoho if (ea->ea_flags & EA_PREDECR) {
333 1.22.2.1 jruoho frame->f_regs[ea->ea_regnum] = ea->ea_fea;
334 1.22.2.1 jruoho ea->ea_fea -= step;
335 1.22.2.1 jruoho ea->ea_moffs = 0;
336 1.22.2.1 jruoho } else if (ea->ea_flags & EA_POSTINCR) {
337 1.22.2.1 jruoho ea->ea_fea += step;
338 1.22.2.1 jruoho frame->f_regs[ea->ea_regnum] = ea->ea_fea;
339 1.22.2.1 jruoho ea->ea_moffs = 0;
340 1.22.2.1 jruoho } else {
341 1.22.2.1 jruoho ea->ea_moffs += step;
342 1.22.2.1 jruoho }
343 1.22.2.1 jruoho /* That's it, folks */
344 1.22.2.1 jruoho } else
345 1.22.2.1 jruoho #endif
346 1.22.2.1 jruoho if (ea->ea_flags & EA_DIRECT) {
347 1.22.2.1 jruoho if (len > 4) {
348 1.22.2.1 jruoho DPRINTF(("%s: operand doesn't fit CPU reg\n",
349 1.22.2.1 jruoho __func__));
350 1.22.2.1 jruoho return SIGILL;
351 1.22.2.1 jruoho }
352 1.22.2.1 jruoho if (ea->ea_moffs > 0) {
353 1.22.2.1 jruoho DPRINTF(("%s: more than one move from CPU reg\n",
354 1.22.2.1 jruoho __func__));
355 1.22.2.1 jruoho return SIGILL;
356 1.22.2.1 jruoho }
357 1.22.2.1 jruoho src = (char *)&frame->f_regs[ea->ea_regnum];
358 1.22.2.1 jruoho /* The source is an int. */
359 1.22.2.1 jruoho if (len < 4) {
360 1.22.2.1 jruoho src += (4 - len);
361 1.22.2.1 jruoho DPRINTF(("%s: short/byte opr - addr adjusted\n",
362 1.22.2.1 jruoho __func__));
363 1.22.2.1 jruoho }
364 1.22.2.1 jruoho DPRINTF(("%s: src %p\n", __func__, src));
365 1.22.2.1 jruoho memcpy(dst, src, len);
366 1.22.2.1 jruoho } else if (ea->ea_flags & EA_IMMED) {
367 1.22.2.1 jruoho DPRINTF(("%s: immed %08x%08x%08x size %d\n", __func__,
368 1.22.2.1 jruoho ea->ea_immed[0], ea->ea_immed[1], ea->ea_immed[2], len));
369 1.22.2.1 jruoho src = (char *)&ea->ea_immed[0];
370 1.22.2.1 jruoho if (len < 4) {
371 1.22.2.1 jruoho src += (4 - len);
372 1.22.2.1 jruoho DPRINTF(("%s: short/byte immed opr - "
373 1.22.2.1 jruoho "addr adjusted\n", __func__));
374 1.22.2.1 jruoho }
375 1.22.2.1 jruoho memcpy(dst, src, len);
376 1.22.2.1 jruoho } else if (ea->ea_flags & EA_ABS) {
377 1.22.2.1 jruoho DPRINTF(("%s: abs addr %08x\n", __func__, ea->ea_absaddr));
378 1.22.2.1 jruoho src = (char *)ea->ea_absaddr;
379 1.22.2.1 jruoho copyin(src, dst, len);
380 1.22.2.1 jruoho } else /* register indirect */ {
381 1.22.2.1 jruoho if (ea->ea_flags & EA_PC_REL) {
382 1.22.2.1 jruoho DPRINTF(("%s: using PC\n", __func__));
383 1.22.2.1 jruoho reg = NULL;
384 1.22.2.1 jruoho /*
385 1.22.2.1 jruoho * Grab the register contents. 4 is offset to the first
386 1.22.2.1 jruoho * extension word from the opcode
387 1.22.2.1 jruoho */
388 1.22.2.1 jruoho src = (char *)insn->is_pc + 4;
389 1.22.2.1 jruoho DPRINTF(("%s: pc relative pc+4 = %p\n", __func__, src));
390 1.22.2.1 jruoho } else /* not PC relative */ {
391 1.22.2.1 jruoho DPRINTF(("%s: using register %c%d\n",
392 1.22.2.1 jruoho __func__,
393 1.22.2.1 jruoho (ea->ea_regnum >= 8) ? 'a' : 'd',
394 1.22.2.1 jruoho ea->ea_regnum & 7));
395 1.22.2.1 jruoho /* point to the register */
396 1.22.2.1 jruoho reg = &frame->f_regs[ea->ea_regnum];
397 1.22.2.1 jruoho
398 1.22.2.1 jruoho if (ea->ea_flags & EA_PREDECR) {
399 1.22.2.1 jruoho DPRINTF(("%s: predecr mode - "
400 1.22.2.1 jruoho "reg decremented\n", __func__));
401 1.22.2.1 jruoho *reg -= step;
402 1.22.2.1 jruoho ea->ea_moffs = 0;
403 1.22.2.1 jruoho }
404 1.22.2.1 jruoho
405 1.22.2.1 jruoho /* Grab the register contents. */
406 1.22.2.1 jruoho src = (char *)*reg;
407 1.22.2.1 jruoho DPRINTF(("%s: reg indirect reg = %p\n", __func__, src));
408 1.22.2.1 jruoho }
409 1.22.2.1 jruoho
410 1.22.2.1 jruoho sig = calc_ea(ea, src, &src);
411 1.22.2.1 jruoho if (sig)
412 1.22.2.1 jruoho return sig;
413 1.22.2.1 jruoho
414 1.22.2.1 jruoho copyin(src + ea->ea_moffs, dst, len);
415 1.22.2.1 jruoho
416 1.22.2.1 jruoho /* do post-increment */
417 1.22.2.1 jruoho if (ea->ea_flags & EA_POSTINCR) {
418 1.22.2.1 jruoho if (ea->ea_flags & EA_PC_REL) {
419 1.22.2.1 jruoho DPRINTF(("%s: tried to postincrement PC\n",
420 1.22.2.1 jruoho __func__));
421 1.22.2.1 jruoho return SIGILL;
422 1.22.2.1 jruoho }
423 1.22.2.1 jruoho *reg += step;
424 1.22.2.1 jruoho ea->ea_moffs = 0;
425 1.22.2.1 jruoho DPRINTF(("%s: postinc mode - reg incremented\n",
426 1.22.2.1 jruoho __func__));
427 1.22.2.1 jruoho } else {
428 1.22.2.1 jruoho ea->ea_moffs += len;
429 1.22.2.1 jruoho }
430 1.1 briggs }
431 1.1 briggs
432 1.22.2.1 jruoho return 0;
433 1.1 briggs }
434 1.1 briggs
435 1.1 briggs /*
436 1.1 briggs * Store a value at the effective address.
437 1.1 briggs * Returns zero on success, else signal number.
438 1.1 briggs */
439 1.1 briggs int
440 1.22.2.1 jruoho fpu_store_ea(struct frame *frame, struct instruction *insn, struct insn_ea *ea,
441 1.22.2.1 jruoho char *src)
442 1.1 briggs {
443 1.22.2.1 jruoho int *reg;
444 1.22.2.1 jruoho char *dst;
445 1.22.2.1 jruoho int len, step;
446 1.22.2.1 jruoho int sig;
447 1.1 briggs
448 1.22.2.1 jruoho #ifdef DIAGNOSTIC
449 1.22.2.1 jruoho if (ea->ea_regnum & ~0xf)
450 1.22.2.1 jruoho panic("%s: bad regnum", __func__);
451 1.8 briggs #endif
452 1.1 briggs
453 1.22.2.1 jruoho if (ea->ea_flags & (EA_IMMED|EA_PC_REL)) {
454 1.22.2.1 jruoho /* not alterable address mode */
455 1.22.2.1 jruoho DPRINTF(("%s: not alterable address mode\n", __func__));
456 1.22.2.1 jruoho return SIGILL;
457 1.1 briggs }
458 1.1 briggs
459 1.22.2.1 jruoho /* src is always int or larger. */
460 1.22.2.1 jruoho len = insn->is_datasize;
461 1.22.2.1 jruoho if (len < 4)
462 1.22.2.1 jruoho src += (4 - len);
463 1.22.2.1 jruoho step = (len == 1 && ea->ea_regnum == 15 /* sp */) ? 2 : len;
464 1.22.2.1 jruoho
465 1.22.2.1 jruoho if (ea->ea_flags & EA_FRAME_EA) {
466 1.22.2.1 jruoho /* Using LC040 frame EA */
467 1.22.2.1 jruoho #ifdef DEBUG_FPE
468 1.22.2.1 jruoho if (ea->ea_flags & (EA_PREDECR|EA_POSTINCR)) {
469 1.22.2.1 jruoho printf("%s: frame ea %08x w/r%d\n",
470 1.22.2.1 jruoho __func__, ea->ea_fea, ea->ea_regnum);
471 1.22.2.1 jruoho } else {
472 1.22.2.1 jruoho printf("%s: frame ea %08x\n", __func__, ea->ea_fea);
473 1.22.2.1 jruoho }
474 1.22.2.1 jruoho #endif
475 1.22.2.1 jruoho dst = (char *)ea->ea_fea;
476 1.22.2.1 jruoho copyout(src, dst + ea->ea_moffs, len);
477 1.22.2.1 jruoho if (ea->ea_flags & EA_PREDECR) {
478 1.22.2.1 jruoho frame->f_regs[ea->ea_regnum] = ea->ea_fea;
479 1.22.2.1 jruoho ea->ea_fea -= step;
480 1.22.2.1 jruoho ea->ea_moffs = 0;
481 1.22.2.1 jruoho } else if (ea->ea_flags & EA_POSTINCR) {
482 1.22.2.1 jruoho ea->ea_fea += step;
483 1.22.2.1 jruoho frame->f_regs[ea->ea_regnum] = ea->ea_fea;
484 1.22.2.1 jruoho ea->ea_moffs = 0;
485 1.22.2.1 jruoho } else {
486 1.22.2.1 jruoho ea->ea_moffs += step;
487 1.22.2.1 jruoho }
488 1.22.2.1 jruoho /* That's it, folks */
489 1.22.2.1 jruoho } else if (ea->ea_flags & EA_ABS) {
490 1.22.2.1 jruoho DPRINTF(("%s: abs addr %08x\n", __func__, ea->ea_absaddr));
491 1.22.2.1 jruoho dst = (char *)ea->ea_absaddr;
492 1.22.2.1 jruoho copyout(src, dst + ea->ea_moffs, len);
493 1.22.2.1 jruoho ea->ea_moffs += len;
494 1.22.2.1 jruoho } else if (ea->ea_flags & EA_DIRECT) {
495 1.22.2.1 jruoho if (len > 4) {
496 1.22.2.1 jruoho DPRINTF(("%s: operand doesn't fit CPU reg\n",
497 1.22.2.1 jruoho __func__));
498 1.22.2.1 jruoho return SIGILL;
499 1.22.2.1 jruoho }
500 1.22.2.1 jruoho if (ea->ea_moffs > 0) {
501 1.22.2.1 jruoho DPRINTF(("%s: more than one move to CPU reg\n",
502 1.22.2.1 jruoho __func__));
503 1.22.2.1 jruoho return SIGILL;
504 1.22.2.1 jruoho }
505 1.22.2.1 jruoho dst = (char *)&frame->f_regs[ea->ea_regnum];
506 1.22.2.1 jruoho /* The destination is an int. */
507 1.22.2.1 jruoho if (len < 4) {
508 1.22.2.1 jruoho dst += (4 - len);
509 1.22.2.1 jruoho DPRINTF(("%s: short/byte opr - dst addr adjusted\n",
510 1.22.2.1 jruoho __func__));
511 1.22.2.1 jruoho }
512 1.22.2.1 jruoho DPRINTF(("%s: dst %p\n", __func__, dst));
513 1.22.2.1 jruoho memcpy(dst, src, len);
514 1.22.2.1 jruoho } else /* One of MANY indirect forms... */ {
515 1.22.2.1 jruoho DPRINTF(("%s: using register %c%d\n", __func__,
516 1.22.2.1 jruoho (ea->ea_regnum >= 8) ? 'a' : 'd', ea->ea_regnum & 7));
517 1.22.2.1 jruoho /* point to the register */
518 1.22.2.1 jruoho reg = &(frame->f_regs[ea->ea_regnum]);
519 1.22.2.1 jruoho
520 1.22.2.1 jruoho /* do pre-decrement */
521 1.22.2.1 jruoho if (ea->ea_flags & EA_PREDECR) {
522 1.22.2.1 jruoho DPRINTF(("%s: predecr mode - reg decremented\n",
523 1.22.2.1 jruoho __func__));
524 1.22.2.1 jruoho *reg -= step;
525 1.22.2.1 jruoho ea->ea_moffs = 0;
526 1.22.2.1 jruoho }
527 1.22.2.1 jruoho
528 1.22.2.1 jruoho /* calculate the effective address */
529 1.22.2.1 jruoho sig = calc_ea(ea, (char *)*reg, &dst);
530 1.22.2.1 jruoho if (sig)
531 1.22.2.1 jruoho return sig;
532 1.22.2.1 jruoho
533 1.22.2.1 jruoho DPRINTF(("%s: dst addr=%p+%d\n", __func__, dst, ea->ea_moffs));
534 1.22.2.1 jruoho copyout(src, dst + ea->ea_moffs, len);
535 1.22.2.1 jruoho
536 1.22.2.1 jruoho /* do post-increment */
537 1.22.2.1 jruoho if (ea->ea_flags & EA_POSTINCR) {
538 1.22.2.1 jruoho *reg += step;
539 1.22.2.1 jruoho ea->ea_moffs = 0;
540 1.22.2.1 jruoho DPRINTF(("%s: postinc mode - reg incremented\n",
541 1.22.2.1 jruoho __func__));
542 1.22.2.1 jruoho } else {
543 1.22.2.1 jruoho ea->ea_moffs += len;
544 1.22.2.1 jruoho }
545 1.1 briggs }
546 1.1 briggs
547 1.22.2.1 jruoho return 0;
548 1.1 briggs }
549 1.1 briggs
550 1.1 briggs /*
551 1.1 briggs * fetch_immed: fetch immediate operand
552 1.1 briggs */
553 1.1 briggs static int
554 1.20 dsl fetch_immed(struct frame *frame, struct instruction *insn, int *dst)
555 1.1 briggs {
556 1.22.2.1 jruoho int data, ext_bytes;
557 1.1 briggs
558 1.22.2.1 jruoho ext_bytes = insn->is_datasize;
559 1.1 briggs
560 1.22.2.1 jruoho if (0 < ext_bytes) {
561 1.22.2.1 jruoho data = fusword((void *)(insn->is_pc + insn->is_advance));
562 1.22.2.1 jruoho if (data < 0)
563 1.22.2.1 jruoho return SIGSEGV;
564 1.22.2.1 jruoho if (ext_bytes == 1) {
565 1.22.2.1 jruoho /* sign-extend byte to long */
566 1.22.2.1 jruoho data &= 0xff;
567 1.22.2.1 jruoho if (data & 0x80)
568 1.22.2.1 jruoho data |= 0xffffff00;
569 1.22.2.1 jruoho } else if (ext_bytes == 2) {
570 1.22.2.1 jruoho /* sign-extend word to long */
571 1.22.2.1 jruoho data &= 0xffff;
572 1.22.2.1 jruoho if (data & 0x8000)
573 1.22.2.1 jruoho data |= 0xffff0000;
574 1.22.2.1 jruoho }
575 1.22.2.1 jruoho insn->is_advance += 2;
576 1.22.2.1 jruoho dst[0] = data;
577 1.22.2.1 jruoho }
578 1.22.2.1 jruoho if (2 < ext_bytes) {
579 1.22.2.1 jruoho data = fusword((void *)(insn->is_pc + insn->is_advance));
580 1.22.2.1 jruoho if (data < 0)
581 1.22.2.1 jruoho return SIGSEGV;
582 1.22.2.1 jruoho insn->is_advance += 2;
583 1.22.2.1 jruoho dst[0] <<= 16;
584 1.22.2.1 jruoho dst[0] |= data;
585 1.22.2.1 jruoho }
586 1.22.2.1 jruoho if (4 < ext_bytes) {
587 1.22.2.1 jruoho data = fusword((void *)(insn->is_pc + insn->is_advance));
588 1.22.2.1 jruoho if (data < 0)
589 1.22.2.1 jruoho return SIGSEGV;
590 1.22.2.1 jruoho dst[1] = data << 16;
591 1.22.2.1 jruoho data = fusword((void *)(insn->is_pc + insn->is_advance + 2));
592 1.22.2.1 jruoho if (data < 0)
593 1.22.2.1 jruoho return SIGSEGV;
594 1.22.2.1 jruoho insn->is_advance += 4;
595 1.22.2.1 jruoho dst[1] |= data;
596 1.22.2.1 jruoho }
597 1.22.2.1 jruoho if (8 < ext_bytes) {
598 1.22.2.1 jruoho data = fusword((void *)(insn->is_pc + insn->is_advance));
599 1.22.2.1 jruoho if (data < 0)
600 1.22.2.1 jruoho return SIGSEGV;
601 1.22.2.1 jruoho dst[2] = data << 16;
602 1.22.2.1 jruoho data = fusword((void *)(insn->is_pc + insn->is_advance + 2));
603 1.22.2.1 jruoho if (data < 0)
604 1.22.2.1 jruoho return SIGSEGV;
605 1.22.2.1 jruoho insn->is_advance += 4;
606 1.22.2.1 jruoho dst[2] |= data;
607 1.1 briggs }
608 1.1 briggs
609 1.22.2.1 jruoho return 0;
610 1.1 briggs }
611 1.1 briggs
612 1.1 briggs /*
613 1.12 toshii * fetch_disp: fetch displacement in full extension words
614 1.1 briggs */
615 1.1 briggs static int
616 1.21 dsl fetch_disp(struct frame *frame, struct instruction *insn, int size, int *res)
617 1.1 briggs {
618 1.22.2.1 jruoho int disp, word;
619 1.1 briggs
620 1.22.2.1 jruoho if (size == 1) {
621 1.22.2.1 jruoho word = fusword((void *)(insn->is_pc + insn->is_advance));
622 1.22.2.1 jruoho if (word < 0)
623 1.22.2.1 jruoho return SIGSEGV;
624 1.22.2.1 jruoho disp = word & 0xffff;
625 1.22.2.1 jruoho if (disp & 0x8000) {
626 1.22.2.1 jruoho /* sign-extend */
627 1.22.2.1 jruoho disp |= 0xffff0000;
628 1.22.2.1 jruoho }
629 1.22.2.1 jruoho insn->is_advance += 2;
630 1.22.2.1 jruoho } else if (size == 2) {
631 1.22.2.1 jruoho word = fusword((void *)(insn->is_pc + insn->is_advance));
632 1.22.2.1 jruoho if (word < 0)
633 1.22.2.1 jruoho return SIGSEGV;
634 1.22.2.1 jruoho disp = word << 16;
635 1.22.2.1 jruoho word = fusword((void *)(insn->is_pc + insn->is_advance + 2));
636 1.22.2.1 jruoho if (word < 0)
637 1.22.2.1 jruoho return SIGSEGV;
638 1.22.2.1 jruoho disp |= (word & 0xffff);
639 1.22.2.1 jruoho insn->is_advance += 4;
640 1.22.2.1 jruoho } else {
641 1.22.2.1 jruoho disp = 0;
642 1.1 briggs }
643 1.22.2.1 jruoho *res = disp;
644 1.22.2.1 jruoho return 0;
645 1.1 briggs }
646 1.1 briggs
647 1.1 briggs /*
648 1.1 briggs * Calculates an effective address for all address modes except for
649 1.1 briggs * register direct, absolute, and immediate modes. However, it does
650 1.1 briggs * not take care of predecrement/postincrement of register content.
651 1.1 briggs * Returns a signal value (0 == no error).
652 1.1 briggs */
653 1.1 briggs static int
654 1.21 dsl calc_ea(struct insn_ea *ea, char *ptr, char **eaddr)
655 1.21 dsl /* ptr: base address (usually a register content) */
656 1.21 dsl /* eaddr: pointer to result pointer */
657 1.1 briggs {
658 1.22.2.1 jruoho int data, word;
659 1.1 briggs
660 1.22.2.1 jruoho DPRINTF(("%s: reg indirect (reg) = %p\n", __func__, ptr));
661 1.1 briggs
662 1.22.2.1 jruoho if (ea->ea_flags & EA_OFFSET) {
663 1.22.2.1 jruoho /* apply the signed offset */
664 1.22.2.1 jruoho DPRINTF(("%s: offset %d\n", __func__, ea->ea_offset));
665 1.22.2.1 jruoho ptr += ea->ea_offset;
666 1.22.2.1 jruoho } else if (ea->ea_flags & EA_INDEXED) {
667 1.22.2.1 jruoho DPRINTF(("%s: indexed mode\n", __func__));
668 1.22.2.1 jruoho
669 1.22.2.1 jruoho if (ea->ea_flags & EA_BASE_SUPPRSS) {
670 1.22.2.1 jruoho /* base register is suppressed */
671 1.22.2.1 jruoho ptr = (char *)ea->ea_basedisp;
672 1.22.2.1 jruoho } else {
673 1.22.2.1 jruoho ptr += ea->ea_basedisp;
674 1.22.2.1 jruoho }
675 1.22.2.1 jruoho
676 1.22.2.1 jruoho if (ea->ea_flags & EA_MEM_INDIR) {
677 1.22.2.1 jruoho DPRINTF(("%s: mem indir mode: basedisp=%08x, "
678 1.22.2.1 jruoho "outerdisp=%08x\n",
679 1.22.2.1 jruoho __func__, ea->ea_basedisp, ea->ea_outerdisp));
680 1.22.2.1 jruoho DPRINTF(("%s: addr fetched from %p\n", __func__, ptr));
681 1.22.2.1 jruoho /* memory indirect modes */
682 1.22.2.1 jruoho word = fusword(ptr);
683 1.22.2.1 jruoho if (word < 0)
684 1.22.2.1 jruoho return SIGSEGV;
685 1.22.2.1 jruoho word <<= 16;
686 1.22.2.1 jruoho data = fusword(ptr + 2);
687 1.22.2.1 jruoho if (data < 0)
688 1.22.2.1 jruoho return SIGSEGV;
689 1.22.2.1 jruoho word |= data;
690 1.22.2.1 jruoho DPRINTF(("%s: fetched ptr 0x%08x\n", __func__, word));
691 1.22.2.1 jruoho ptr = (char *)word + ea->ea_outerdisp;
692 1.22.2.1 jruoho }
693 1.1 briggs }
694 1.1 briggs
695 1.22.2.1 jruoho *eaddr = ptr;
696 1.1 briggs
697 1.22.2.1 jruoho return 0;
698 1.1 briggs }
699