1 /* srcdest.c --- decoding M32C addressing modes.
3 Copyright (C) 2005-2018 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "libiberty.h"
29 static int src_indirect
= 0;
30 static int dest_indirect
= 0;
31 static int src_addend
= 0;
32 static int dest_addend
= 0;
42 rv
= mem_get_qi (get_reg (pc
));
56 rv
= mem_get_hi (get_reg (pc
));
70 rv
= mem_get_psi (get_reg (pc
));
79 return disp24 () & 0x000fffff;
90 *--bp
= (v
& 1) ? '1' : '0';
97 static const char *the_bits
= 0;
100 decode_indirect (int si
, int di
)
104 if (trace
&& (si
|| di
))
105 printf ("indirect: s:%d d:%d\n", si
, di
);
109 decode_index (int sa
, int da
)
113 if (trace
&& (sa
|| da
))
114 printf ("index: s:%d d:%d\n", sa
, da
);
118 decode_srcdest4 (int destcode
, int bw
)
121 static const char *dc_wnames
[16] = { "r0", "r1", "r2", "r3",
122 "a0", "a1", "[a0]", "[a1]",
123 "disp8[a0]", "disp8[a1]", "disp8[sb]", "disp8[fb]",
124 "disp16[a0]", "disp16[a1]", "disp16[sb]", "disp16"
126 static const char *dc_bnames
[4] = { "r0l", "r0h", "r1l", "r1h" };;
128 sd
.bytes
= bw
? 2 : 1;
129 sd
.mem
= (destcode
>= 6) ? 1 : 0;
133 const char *n
= dc_wnames
[destcode
];
134 if (bw
== 0 && destcode
<= 3)
135 n
= dc_bnames
[destcode
];
137 the_bits
= bits (destcode
, 4);
138 printf ("decode: %s (%d) : %s\n", the_bits
, destcode
, n
);
145 sd
.u
.reg
= bw
? r0
: r0l
;
148 sd
.u
.reg
= bw
? r1
: r0h
;
151 sd
.u
.reg
= bw
? r2
: r1l
;
154 sd
.u
.reg
= bw
? r3
: r1h
;
163 sd
.u
.addr
= get_reg (a0
);
166 sd
.u
.addr
= get_reg (a1
);
169 sd
.u
.addr
= get_reg (a0
) + disp8 ();
172 sd
.u
.addr
= get_reg (a1
) + disp8 ();
175 sd
.u
.addr
= get_reg (sb
) + disp8 ();
178 sd
.u
.addr
= get_reg (fb
) + sign_ext (disp8 (), 8);
181 sd
.u
.addr
= get_reg (a0
) + disp16 ();
184 sd
.u
.addr
= get_reg (a1
) + disp16 ();
187 sd
.u
.addr
= get_reg (sb
) + disp16 ();
190 sd
.u
.addr
= disp16 ();
196 sd
.u
.addr
&= addr_mask
;
201 decode_jumpdest (int destcode
, int w
)
204 static const char *dc_wnames
[16] = { "r0", "r1", "r2", "r3",
205 "a0", "a1", "[a0]", "[a1]",
206 "disp8[a0]", "disp8[a1]", "disp8[sb]", "disp8[fb]",
207 "disp20[a0]", "disp20[a1]", "disp16[sb]", "abs16"
209 static const char *dc_anames
[4] = { "r0l", "r0h", "r1l", "r1h" };
211 sd
.bytes
= w
? 2 : 3;
212 sd
.mem
= (destcode
>= 6) ? 1 : 0;
216 const char *n
= dc_wnames
[destcode
];
217 if (w
== 0 && destcode
<= 3)
218 n
= dc_anames
[destcode
];
220 the_bits
= bits (destcode
, 4);
221 printf ("decode: %s : %s\n", the_bits
, n
);
228 sd
.u
.reg
= w
? r0
: r2r0
;
231 sd
.u
.reg
= w
? r1
: r2r0
;
234 sd
.u
.reg
= w
? r2
: r3r1
;
237 sd
.u
.reg
= w
? r3
: r3r1
;
240 sd
.u
.reg
= w
? a0
: a1a0
;
243 sd
.u
.reg
= w
? a1
: a1a0
;
246 sd
.u
.addr
= get_reg (a0
);
249 sd
.u
.addr
= get_reg (a1
);
252 sd
.u
.addr
= get_reg (a0
) + disp8 ();
255 sd
.u
.addr
= get_reg (a1
) + disp8 ();
258 sd
.u
.addr
= get_reg (sb
) + disp8 ();
261 sd
.u
.addr
= get_reg (fb
) + sign_ext (disp8 (), 8);
264 sd
.u
.addr
= get_reg (a0
) + disp20 ();
267 sd
.u
.addr
= get_reg (a1
) + disp20 ();
270 sd
.u
.addr
= get_reg (sb
) + disp16 ();
273 sd
.u
.addr
= disp16 ();
279 sd
.u
.addr
&= addr_mask
;
284 decode_dest3 (int destcode
, int bw
)
286 static char map
[8] = { -1, -1, -1, 1, 0, 10, 11, 15 };
288 the_bits
= bits (destcode
, 3);
289 return decode_srcdest4 (map
[destcode
], bw
);
293 decode_src2 (int srccode
, int bw
, int d
)
295 static char map
[4] = { 0, 10, 11, 15 };
297 the_bits
= bits (srccode
, 2);
298 return decode_srcdest4 (srccode
? map
[srccode
] : 1 - d
, bw
);
311 a0
, a0
, 1, 0, "[A0]"}, /* 0 0 0 0 0 */
313 a1
, a1
, 1, 0, "[A1]"}, /* 0 0 0 0 1 */
315 a0
, a0
, 0, 0, "A0"}, /* 0 0 0 1 0 */
317 a1
, a1
, 0, 0, "A1"}, /* 0 0 0 1 1 */
319 a0
, a0
, 1, 1, "dsp:8[A0]"}, /* 0 0 1 0 0 */
321 a1
, a1
, 1, 1, "dsp:8[A1]"}, /* 0 0 1 0 1 */
323 sb
, sb
, 1, 1, "dsp:8[SB]"}, /* 0 0 1 1 0 */
325 fb
, fb
, 1, -1, "dsp:8[FB]"}, /* 0 0 1 1 1 */
327 a0
, a0
, 1, 2, "dsp:16[A0]"}, /* 0 1 0 0 0 */
329 a1
, a1
, 1, 2, "dsp:16[A1]"}, /* 0 1 0 0 1 */
331 sb
, sb
, 1, 2, "dsp:16[SB]"}, /* 0 1 0 1 0 */
333 fb
, fb
, 1, -2, "dsp:16[FB]"}, /* 0 1 0 1 1 */
335 a0
, a0
, 1, 3, "dsp:24[A0]"}, /* 0 1 1 0 0 */
337 a1
, a1
, 1, 3, "dsp:24[A1]"}, /* 0 1 1 0 1 */
339 mem
, mem
, 1, 3, "abs24"}, /* 0 1 1 1 0 */
341 mem
, mem
, 1, 2, "abs16"}, /* 0 1 1 1 1 */
343 r0h
, r2
, 0, 0, "R0H/R2"}, /* 1 0 0 0 0 */
345 r1h
, r3
, 0, 0, "R1H/R3"}, /* 1 0 0 0 1 */
347 r0l
, r0
, 0, 0, "R0L/R0"}, /* 1 0 0 1 0 */
349 r1l
, r1
, 0, 0, "R1L/R1"}, /* 1 0 0 1 1 */
353 decode_sd23 (int bbb
, int bb
, int bytes
, int ind
, int add
)
356 int code
= (bbb
<< 2) | bb
;
358 if (code
>= ARRAY_SIZE (modes23
))
372 sprintf (ad
, "%+d", add
);
376 the_bits
= bits (code
, 4);
377 printf ("decode: %s (%d) : %s%s%s%s\n", the_bits
, code
, b1
,
378 modes23
[code
].name
, ad
, b2
);
383 sd
.mem
= modes23
[code
].is_memory
;
386 if (modes23
[code
].w_regno
== mem
)
389 sd
.u
.addr
= get_reg (modes23
[code
].w_regno
);
390 switch (modes23
[code
].disp_bytes
)
393 sd
.u
.addr
+= disp8 ();
396 sd
.u
.addr
+= disp16 ();
399 sd
.u
.addr
+= sign_ext (disp8 (), 8);
402 sd
.u
.addr
+= sign_ext (disp16 (), 16);
405 sd
.u
.addr
+= disp24 ();
413 sd
.u
.addr
= mem_get_si (sd
.u
.addr
& membus_mask
);
414 sd
.u
.addr
&= membus_mask
;
418 sd
.u
.reg
= (bytes
> 1) ? modes23
[code
].w_regno
: modes23
[code
].b_regno
;
419 if (bytes
== 3 || bytes
== 4)
442 decode_dest23 (int ddd
, int dd
, int bytes
)
444 return decode_sd23 (ddd
, dd
, bytes
, dest_indirect
, dest_addend
);
448 decode_src23 (int sss
, int ss
, int bytes
)
450 return decode_sd23 (sss
, ss
, bytes
, src_indirect
, src_addend
);
454 decode_dest2 (int dd
, int bytes
)
456 /* r0l/r0, abs16, dsp:8[SB], dsp:8[FB] */
457 static char map
[4] = { 0x12, 0x0f, 0x06, 0x07 };
459 the_bits
= bits (dd
, 2);
460 return decode_sd23 (map
[dd
] >> 2, map
[dd
] & 3, bytes
, dest_indirect
,
465 decode_src3 (int sss
, int bytes
)
467 /* r0, r1, a0, a1, r2, r3, N/A, N/A */
468 static char map
[8] = { 0x12, 0x13, 0x02, 0x03, 0x10, 0x11, 0, 0 };
470 the_bits
= bits (sss
, 3);
471 return decode_sd23 (map
[sss
] >> 2, map
[sss
] & 3, bytes
, src_indirect
,
476 decode_dest1 (int destcode
, int bw
)
478 the_bits
= bits (destcode
, 1);
479 return decode_srcdest4 (destcode
, bw
);
483 decode_cr (int crcode
)
485 static int regcode
[] = { 0, intbl
, intbh
, flags
, isp
, sp
, sb
, fb
};
489 sd
.u
.reg
= regcode
[crcode
& 7];
494 decode_cr_b (int crcode
, int bank
)
496 /* FIXME: intbl, intbh, isp */
497 static int regcode
[3][8] = {
498 {0, 0, flags
, 0, 0, 0, 0, 0},
499 {intb
, sp
, sb
, fb
, 0, 0, 0, isp
},
500 {0, 0, 0, 0, 0, 0, 0, 0}
504 sd
.bytes
= bank
? 3 : 2;
505 sd
.u
.reg
= regcode
[bank
][crcode
& 7];
510 widen_sd (srcdest sd
)
542 rv
.bytes
= reg_bytes
[reg
];
557 v
= mem_get_qi (sd
.u
.addr
);
560 v
= mem_get_hi (sd
.u
.addr
);
563 v
= mem_get_psi (sd
.u
.addr
);
566 v
= mem_get_si (sd
.u
.addr
);
574 v
= get_reg (sd
.u
.reg
);
592 put_dest (srcdest sd
, int v
)
599 mem_put_qi (sd
.u
.addr
, v
);
602 mem_put_hi (sd
.u
.addr
, v
);
605 mem_put_psi (sd
.u
.addr
, v
);
608 mem_put_si (sd
.u
.addr
, v
);
626 put_reg (sd
.u
.reg
, v
);
631 decode_bit (int destcode
)
635 static const char *dc_names
[] = { "r0", "r1", "r2", "r3",
636 "a0", "a1", "[a0]", "[a1]",
637 "disp8[a0]", "disp8[a1]", "disp8[sb]", "disp8[fb]",
638 "disp16[a0]", "disp16[a1]", "disp16[sb]", "abs16"
643 const char *the_bits
= bits (destcode
, 4);
644 printf ("decode: %s : %s\n", the_bits
, dc_names
[destcode
]);
674 addr
= get_reg (a0
) + disp8 ();
677 addr
= get_reg (a1
) + disp8 ();
680 addr
= get_reg (sb
) * 8 + disp8 ();
683 addr
= get_reg (fb
) * 8 + sign_ext (disp8 (), 8);
686 addr
= get_reg (a0
) + disp16 ();
689 addr
= get_reg (a1
) + disp16 ();
692 addr
= get_reg (sb
) + disp16 ();
703 sd
.mask
= 1 << (d
& 0x0f);
709 sd
.mask
= 1 << (addr
& 7);
710 sd
.u
.addr
= addr
>> 3;
716 decode_bit11 (int op0
)
719 sd
.mask
= 1 << (op0
& 7);
721 sd
.u
.addr
= get_reg (sb
) + disp8 ();
730 b
= mem_get_qi (sd
.u
.addr
) & sd
.mask
;
732 b
= get_reg (sd
.u
.reg
) & sd
.mask
;
737 put_bit (srcdest sd
, int val
)
741 b
= mem_get_qi (sd
.u
.addr
);
743 b
= get_reg (sd
.u
.reg
);
749 mem_put_qi (sd
.u
.addr
, b
);
751 put_reg (sd
.u
.reg
, b
);
755 get_bit2 (srcdest sd
, int bit
)
759 b
= mem_get_qi (sd
.u
.addr
+ (bit
>> 3)) & (1 << (bit
& 7));
761 b
= get_reg (sd
.u
.reg
) & (1 << bit
);
766 put_bit2 (srcdest sd
, int bit
, int val
)
770 b
= mem_get_qi (sd
.u
.addr
+ (bit
>> 3));
772 b
= get_reg (sd
.u
.reg
);
774 b
|= (1 << (bit
& 7));
776 b
&= ~(1 << (bit
& 7));
778 mem_put_qi (sd
.u
.addr
+ (bit
>> 3), b
);
780 put_reg (sd
.u
.reg
, b
);