2 /* Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 Contributed by Red Hat.
6 This file is part of the GNU opcodes library.
8 This library 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, or (at your option)
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 #include "opcode/msp430-decode.h"
34 MSP430_Opcode_Decoded *msp430;
35 int (*getbyte)(void *);
42 #define AU ATTRIBUTE_UNUSED
43 #define GETBYTE() getbyte_swapped (ld)
44 #define B ((unsigned long) GETBYTE ())
47 getbyte_swapped (LocalData *ld)
51 if (ld->op_ptr == ld->msp430->n_bytes)
55 b = ld->getbyte (ld->ptr);
56 ld->op [(ld->msp430->n_bytes++)^1] = b;
58 while (ld->msp430->n_bytes & 1);
60 return ld->op[ld->op_ptr++];
63 #define ID(x) msp430->id = x
65 #define OP(n, t, r, a) (msp430->op[n].type = t, \
66 msp430->op[n].reg = r, \
67 msp430->op[n].addend = a)
69 #define OPX(n, t, r1, r2, a) \
70 (msp430->op[n].type = t, \
71 msp430->op[n].reg = r1, \
72 msp430->op[n].reg2 = r2, \
73 msp430->op[n].addend = a)
75 #define SYNTAX(x) msp430->syntax = x
76 #define UNSUPPORTED() msp430->syntax = "*unknown*"
78 #define DC(c) OP (0, MSP430_Operand_Immediate, 0, c)
79 #define DR(r) OP (0, MSP430_Operand_Register, r, 0)
80 #define DM(r, a) OP (0, MSP430_Operand_Indirect, r, a)
81 #define DA(a) OP (0, MSP430_Operand_Indirect, MSR_None, a)
82 #define AD(r, ad) encode_ad (r, ad, ld, 0)
83 #define ADX(r, ad, x) encode_ad (r, ad, ld, x)
85 #define SC(c) OP (1, MSP430_Operand_Immediate, 0, c)
86 #define SR(r) OP (1, MSP430_Operand_Register, r, 0)
87 #define SM(r, a) OP (1, MSP430_Operand_Indirect, r, a)
88 #define SA(a) OP (1, MSP430_Operand_Indirect, MSR_None, a)
89 #define SI(r) OP (1, MSP430_Operand_Indirect_Postinc, r, 0)
90 #define AS(r, as) encode_as (r, as, ld, 0)
91 #define ASX(r, as, x) encode_as (r, as, ld, x)
93 #define BW(x) msp430->size = (x ? 8 : 16)
94 /* The last 20 is for SWPBX.Z and SXTX.A. */
95 #define ABW(a,x) msp430->size = (a ? ((x ? 8 : 16)) : (x ? 20 : 20))
97 #define IMMU(bytes) immediate (bytes, 0, ld)
98 #define IMMS(bytes) immediate (bytes, 1, ld)
100 /* Helper macros for known status bits settings. */
101 #define F_____ msp430->flags_1 = msp430->flags_0 = 0; msp430->flags_set = 0
102 #define F_VNZC msp430->flags_1 = msp430->flags_0 = 0; msp430->flags_set = 0x87
103 #define F_0NZC msp430->flags_1 = 0; msp430->flags_0 = 0x80; msp430->flags_set = 0x07
106 /* The chip is little-endian, but GETBYTE byte-swaps words because the
107 decoder is based on 16-bit "words" so *this* logic is big-endian. */
110 immediate (int bytes, int sign_extend, LocalData *ld)
118 if (sign_extend && (i & 0x80))
124 if (sign_extend && (i & 0x8000))
131 if (sign_extend && (i & 0x800000))
139 if (sign_extend && (i & 0x80000000ULL))
144 "Programmer error: immediate() called with invalid byte count %d\n",
155 01 X(Rn) Sym - X(abs) #1
157 11 (Rn++) #imm - #8 #-1
161 1 X(Rn) Sym - X(abs) - */
164 encode_ad (int reg, int ad, LocalData *ld, int ext)
166 MSP430_Opcode_Decoded *msp430 = ld->msp430;
170 int x = IMMU(2) | (ext << 16);
173 case 0: /* (PC) -> Symbolic. */
174 DA (x + ld->pc + ld->op_ptr - 2);
176 case 2: /* (SR) -> Absolute. */
191 encode_as (int reg, int as, LocalData *ld, int ext)
193 MSP430_Opcode_Decoded *msp430 = ld->msp430;
212 case 0: /* PC -> Symbolic. */
213 x = IMMU(2) | (ext << 16);
214 SA (x + ld->pc + ld->op_ptr - 2);
216 case 2: /* SR -> Absolute. */
217 x = IMMU(2) | (ext << 16);
224 x = IMMU(2) | (ext << 16);
251 /* This fetch *is* the *PC++ that the opcode encodes :-) */
252 x = IMMU(2) | (ext << 16);
271 encode_rep_zc (int srxt, int dsxt, LocalData *ld)
273 MSP430_Opcode_Decoded *msp430 = ld->msp430;
275 msp430->repeat_reg = srxt & 1;
276 msp430->repeats = dsxt;
277 msp430->zc = (srxt & 2) ? 1 : 0;
280 #define REPZC(s,d) encode_rep_zc (s, d, ld)
283 dopc_to_id (int dopc)
287 case 4: return MSO_mov;
288 case 5: return MSO_add;
289 case 6: return MSO_addc;
290 case 7: return MSO_subc;
291 case 8: return MSO_sub;
292 case 9: return MSO_cmp;
293 case 10: return MSO_dadd;
294 case 11: return MSO_bit;
295 case 12: return MSO_bic;
296 case 13: return MSO_bis;
297 case 14: return MSO_xor;
298 case 15: return MSO_and;
299 default: return MSO_unknown;
304 sopc_to_id (int sop, int c)
308 case 0: return MSO_rrc;
309 case 1: return MSO_swpb;
310 case 2: return MSO_rra;
311 case 3: return MSO_sxt;
312 case 4: return MSO_push;
313 case 5: return MSO_call;
314 case 6: return MSO_reti;
315 default: return MSO_unknown;
320 msp430_decode_opcode (unsigned long pc,
321 MSP430_Opcode_Decoded *msp430,
322 int (*getbyte)(void *),
325 LocalData lds, *ld = &lds;
326 unsigned char op_buf[20] = {0};
327 unsigned char *op = op_buf;
330 int srxt_bits, dsxt_bits;
333 lds.getbyte = getbyte;
339 memset (msp430, 0, sizeof (*msp430));
341 /* These are overridden by an extension word. */
349 /* 430X extention word. */
350 /** 0001 1srx t l 00 dsxt 430x */
353 srxt_bits = srx * 2 + t;
355 op = op_buf + lds.op_ptr;
356 msp430->ofs_430x = 1;
357 goto post_extension_word;
360 opcode:4 sreg:4 Ad:1 BW:1 As:2 Dreg:4
363 opcode:9 BW:1 Ad:2 DSreg:4
366 opcode:3 Cond:3 pcrel:10. */
368 /* Double-Operand "opcode" fields. */
369 /** VARY dopc 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 */
371 /** dopc sreg a b as dreg %D%b %1,%0 */
373 ID (dopc_to_id (dopc)); ASX (sreg, as, srxt_bits); ADX (dreg, a, dsxt_bits); ABW (al_bit, b);
374 if (a == 0 && as == 0)
375 REPZC (srxt_bits, dsxt_bits);
379 case MSO_mov: F_____; break;
380 case MSO_add: F_VNZC; break;
381 case MSO_addc: F_VNZC; break;
382 case MSO_subc: F_VNZC; break;
383 case MSO_sub: F_VNZC; break;
384 case MSO_cmp: F_VNZC; break;
385 case MSO_dadd: F_VNZC; break;
386 case MSO_bit: F_0NZC; break;
387 case MSO_bic: F_____; break;
388 case MSO_bis: F_____; break;
389 case MSO_xor: F_VNZC; break;
390 case MSO_and: F_0NZC; break;
394 /** 0001 00so c b ad dreg %S%b %1 */
396 ID (sopc_to_id (so,c)); ASX (dreg, ad, srxt_bits); ABW (al_bit, b);
399 REPZC (srxt_bits, dsxt_bits);
401 /* The helper functions encode for source, but it's
402 both source and dest, with a few documented exceptions. */
403 msp430->op[0] = msp430->op[1];
405 /* RETI ignores the operand. */
406 if (msp430->id == MSO_reti)
407 msp430->syntax = "%S";
411 case MSO_rrc: F_VNZC; break;
412 case MSO_swpb: F_____; break;
413 case MSO_rra: F_0NZC; break;
414 case MSO_sxt: F_0NZC; break;
415 case MSO_push: F_____; break;
416 case MSO_call: F_____; break;
417 case MSO_reti: F_VNZC; break;
421 /* 20xx 0010 0000 ---- ----
422 3cxx 0011 1100 ---- ----
423 001j mp-- ---- ----. */
424 /** 001jmp aa addrlsbs %J %1 */
426 raddr = (aa << 9) | (addrlsbs << 1);
428 raddr = raddr - 0x800;
429 /* This is a pc-relative jump, but we don't use SM because that
430 would load the target address from the memory at X(PC), not use
431 PC+X *as* the address. So we use SC to use the address, not the
432 data at that address. */
433 ID (MSO_jmp); SC (pc + raddr + msp430->n_bytes);
436 /* Extended instructions. */
438 /** 0000 srcr 0000 dstr MOVA @%1, %0 */
439 ID (MSO_mov); SM (srcr, 0); DR (dstr);
441 msp430->ofs_430x = 1;
443 /** 0000 srcr 0001 dstr MOVA @%1+, %0 */
444 ID (MSO_mov); SI (srcr); DR (dstr);
446 msp430->ofs_430x = 1;
448 /** 0000 srcr 0010 dstr MOVA &%1, %0 */
449 ID (MSO_mov); SA ((srcr << 16) + IMMU(2)); DR (dstr);
451 msp430->ofs_430x = 1;
453 /** 0000 srcr 0011 dstr MOVA %1, %0 */
454 ID (MSO_mov); SM (srcr, IMMS(2)); DR (dstr);
456 msp430->ofs_430x = 1;
458 /** 0000 srcr 0110 dstr MOVA %1, &%0 */
459 ID (MSO_mov); SR (srcr); DA ((dstr << 16) + IMMU(2));
461 msp430->ofs_430x = 1;
463 /** 0000 srcr 0111 dstr MOVA %1, &%0 */
464 ID (MSO_mov); SR (srcr); DM (dstr, IMMS(2));
466 msp430->ofs_430x = 1;
468 /** 0000 srcr 1000 dstr MOVA %1, %0 */
469 ID (MSO_mov); SC ((srcr << 16) + IMMU(2)); DR (dstr);
471 msp430->ofs_430x = 1;
473 /** 0000 srcr 1001 dstr CMPA %1, %0 */
474 ID (MSO_cmp); SC ((srcr << 16) + IMMU(2)); DR (dstr);
476 msp430->ofs_430x = 1;
479 /** 0000 srcr 1010 dstr ADDA %1, %0 */
480 ID (MSO_add); SC ((srcr << 16) + IMMU(2)); DR (dstr);
482 msp430->ofs_430x = 1;
485 /** 0000 srcr 1011 dstr SUBA %1, %0 */
486 ID (MSO_sub); SC ((srcr << 16) + IMMU(2)); DR (dstr);
488 msp430->ofs_430x = 1;
491 /** 0000 srcr 1011 dstr SUBA %1, %0 */
492 ID (MSO_sub); SC ((srcr << 16) + IMMU(2)); DR (dstr);
494 msp430->ofs_430x = 1;
497 /** 0000 srcr 1100 dstr MOVA %1, %0 */
498 ID (MSO_mov); SR (srcr); DR (dstr);
500 msp430->ofs_430x = 1;
502 /** 0000 srcr 1101 dstr CMPA %1, %0 */
503 ID (MSO_cmp); SR (srcr); DR (dstr);
505 msp430->ofs_430x = 1;
508 /** 0000 srcr 1110 dstr ADDA %1, %0 */
509 ID (MSO_add); SR (srcr); DR (dstr);
511 msp430->ofs_430x = 1;
514 /** 0000 srcr 1111 dstr SUBA %1, %0 */
515 ID (MSO_sub); SR (srcr); DR (dstr);
517 msp430->ofs_430x = 1;
520 /** 0000 bt00 010w dstr RRCM.A %c, %0 */
521 ID (MSO_rrc); DR (dstr); SR (dstr);
522 msp430->repeats = bt;
523 msp430->size = w ? 16 : 20;
524 msp430->ofs_430x = 1;
527 /** 0000 bt01 010w dstr RRAM.A %c, %0 */
528 ID (MSO_rra); DR (dstr); SR (dstr);
529 msp430->repeats = bt;
530 msp430->size = w ? 16 : 20;
531 msp430->ofs_430x = 1;
534 /** 0000 bt10 010w dstr RLAM.A %c, %0 */
535 ID (MSO_add); DR (dstr); SR (dstr);
536 msp430->repeats = bt;
537 msp430->size = w ? 16 : 20;
538 msp430->ofs_430x = 1;
541 /** 0000 bt11 010w dstr RRUM.A %c, %0 */
542 ID (MSO_rru); DR (dstr); SR (dstr);
543 msp430->repeats = bt;
544 msp430->size = w ? 16 : 20;
545 msp430->ofs_430x = 1;
548 /** 0001 0011 0000 0000 RETI */
551 msp430->ofs_430x = 1;
553 /** 0001 0011 01as dstr CALLA %0 */
554 ID (MSO_call); AS (dstr, as);
556 msp430->ofs_430x = 1;
558 /** 0001 0011 1000 extb CALLA %0 */
559 ID (MSO_call); SA (IMMU(2) | (extb << 16));
561 msp430->ofs_430x = 1;
563 /** 0001 0011 1001 extb CALLA %0 */
564 raddr = IMMU(2) | (extb << 16);
567 ID (MSO_call); SA (pc + raddr + msp430->n_bytes);
569 msp430->ofs_430x = 1;
571 /** 0001 0011 1011 extb CALLA %0 */
572 ID (MSO_call); SC (IMMU(2) | (extb << 16));
574 msp430->ofs_430x = 1;
576 /** 0001 010w bits srcr PUSHM.A %0 */
577 ID (MSO_push); SR (srcr);
578 msp430->size = w ? 16 : 20;
579 msp430->repeats = bits;
580 msp430->ofs_430x = 1;
582 /** 0001 011w bits dstr POPM.A %0 */
583 ID (MSO_pop); DR (dstr);
584 msp430->size = w ? 16 : 20;
585 msp430->repeats = bits;
586 msp430->ofs_430x = 1;
590 return msp430->n_bytes;