1 /* Simulator for TI MSP430 and MSP430X
3 Copyright (C) 2013-2023 Free Software Foundation, Inc.
4 Contributed by Red Hat.
5 Based on sim/bfin/bfin-sim.c which was contributed by Analog Devices, Inc.
7 This file is part of simulators.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* This must come before any other includes. */
31 #include "opcode/msp430-decode.h"
33 #include "sim-options.h"
34 #include "sim-signal.h"
35 #include "sim-syscall.h"
36 #include "msp430-sim.h"
39 msp430_pc_fetch (SIM_CPU
*cpu
)
41 struct msp430_cpu_state
*msp430_cpu
= MSP430_SIM_CPU (cpu
);
43 return msp430_cpu
->regs
[0];
47 msp430_pc_store (SIM_CPU
*cpu
, sim_cia newpc
)
49 struct msp430_cpu_state
*msp430_cpu
= MSP430_SIM_CPU (cpu
);
51 msp430_cpu
->regs
[0] = newpc
;
55 msp430_reg_fetch (SIM_CPU
*cpu
, int regno
, void *buf
, int len
)
57 struct msp430_cpu_state
*msp430_cpu
= MSP430_SIM_CPU (cpu
);
58 unsigned char *memory
= buf
;
60 if (0 <= regno
&& regno
< 16)
64 int val
= msp430_cpu
->regs
[regno
];
65 memory
[0] = val
& 0xff;
66 memory
[1] = (val
>> 8) & 0xff;
71 int val
= msp430_cpu
->regs
[regno
];
72 memory
[0] = val
& 0xff;
73 memory
[1] = (val
>> 8) & 0xff;
74 memory
[2] = (val
>> 16) & 0x0f; /* Registers are only 20 bits wide. */
86 msp430_reg_store (SIM_CPU
*cpu
, int regno
, const void *buf
, int len
)
88 struct msp430_cpu_state
*msp430_cpu
= MSP430_SIM_CPU (cpu
);
89 const unsigned char *memory
= buf
;
91 if (0 <= regno
&& regno
< 16)
95 msp430_cpu
->regs
[regno
] = (memory
[1] << 8) | memory
[0];
101 msp430_cpu
->regs
[regno
] = ((memory
[2] << 16) & 0xf0000)
102 | (memory
[1] << 8) | memory
[0];
111 sim_open (SIM_OPEN_KIND kind
,
112 struct host_callback_struct
*callback
,
116 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
117 struct msp430_cpu_state
*msp430_cpu
;
121 /* Initialise the simulator. */
123 /* Set default options before parsing user options. */
124 current_target_byte_order
= BFD_ENDIAN_LITTLE
;
126 if (sim_cpu_alloc_all_extra (sd
, 0, sizeof (struct msp430_cpu_state
))
133 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
139 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
145 /* Allocate memory if none specified by user.
146 Note - these values match the memory regions in the libgloss/msp430/msp430[xl]-sim.ld scripts. */
147 if (sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, &c
, 0x2, 1) == 0)
148 sim_do_commandf (sd
, "memory-region 0,0x20"); /* Needed by the GDB testsuite. */
149 if (sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, &c
, 0x500, 1) == 0)
150 sim_do_commandf (sd
, "memory-region 0x500,0xfac0"); /* RAM and/or ROM */
151 if (sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, &c
, 0xfffe, 1) == 0)
152 sim_do_commandf (sd
, "memory-region 0xffc0,0x40"); /* VECTORS. */
153 if (sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, &c
, 0x10000, 1) == 0)
154 sim_do_commandf (sd
, "memory-region 0x10000,0x80000"); /* HIGH FLASH RAM. */
155 if (sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, &c
, 0x90000, 1) == 0)
156 sim_do_commandf (sd
, "memory-region 0x90000,0x70000"); /* HIGH ROM. */
158 /* Check for/establish the a reference program image. */
159 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
165 /* Establish any remaining configuration options. */
166 if (sim_config (sd
) != SIM_RC_OK
)
172 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
178 /* CPU specific initialization. */
179 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
181 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
183 CPU_PC_FETCH (cpu
) = msp430_pc_fetch
;
184 CPU_PC_STORE (cpu
) = msp430_pc_store
;
185 CPU_REG_FETCH (cpu
) = msp430_reg_fetch
;
186 CPU_REG_STORE (cpu
) = msp430_reg_store
;
188 msp430_cpu
= MSP430_SIM_CPU (cpu
);
189 msp430_cpu
->cio_breakpoint
= trace_sym_value (sd
, "C$$IO$$");
190 msp430_cpu
->cio_buffer
= trace_sym_value (sd
, "__CIOBUF__");
191 if (msp430_cpu
->cio_buffer
== -1)
192 msp430_cpu
->cio_buffer
= trace_sym_value (sd
, "_CIOBUF_");
199 sim_create_inferior (SIM_DESC sd
,
204 unsigned char resetv
[2];
208 /* Set the PC to the default reset vector if available. */
209 c
= sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, resetv
, 0xfffe, 2);
210 new_pc
= resetv
[0] + 256 * resetv
[1];
212 /* If the reset vector isn't initialized, then use the ELF entry. */
213 if (abfd
!= NULL
&& !new_pc
)
214 new_pc
= bfd_get_start_address (abfd
);
216 sim_pc_set (STATE_CPU (sd
, 0), new_pc
);
217 msp430_pc_store (STATE_CPU (sd
, 0), new_pc
);
226 } Get_Byte_Local_Data
;
229 msp430_getbyte (void *vld
)
231 Get_Byte_Local_Data
*ld
= (Get_Byte_Local_Data
*)vld
;
233 SIM_DESC sd
= ld
->sd
;
235 sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, buf
, ld
->gb_addr
, 1);
240 #define REG(N) MSP430_SIM_CPU (STATE_CPU (sd, 0))->regs[N]
241 #define PC REG(MSR_PC)
242 #define SP REG(MSR_SP)
243 #define SR REG(MSR_SR)
248 "PC", "SP", "SR", "CG", "R4", "R5", "R6", "R7", "R8",
249 "R9", "R10", "R11", "R12", "R13", "R14", "R15"
253 trace_reg_put (SIM_DESC sd
, int n
, unsigned int v
)
255 TRACE_REGISTER (STATE_CPU (sd
, 0), "PUT: %#x -> %s", v
, register_names
[n
]);
260 trace_reg_get (SIM_DESC sd
, int n
)
262 TRACE_REGISTER (STATE_CPU (sd
, 0), "GET: %s -> %#x", register_names
[n
], REG (n
));
266 #define REG_PUT(N,V) trace_reg_put (sd, N, V)
267 #define REG_GET(N) trace_reg_get (sd, N)
269 /* Hardware multiply (and accumulate) support. */
272 zero_ext (unsigned int v
, unsigned int bits
)
274 v
&= ((1 << bits
) - 1);
278 static signed long long
279 sign_ext (signed long long v
, unsigned int bits
)
281 signed long long sb
= 1LL << (bits
-1); /* Sign bit. */
282 signed long long mb
= (1LL << (bits
-1)) - 1LL; /* Mantissa bits. */
292 get_op (SIM_DESC sd
, MSP430_Opcode_Decoded
*opc
, int n
)
294 MSP430_Opcode_Operand
*op
= opc
->op
+ n
;
297 unsigned char buf
[4];
302 case MSP430_Operand_Immediate
:
305 case MSP430_Operand_Register
:
306 rv
= REG_GET (op
->reg
);
308 case MSP430_Operand_Indirect
:
309 case MSP430_Operand_Indirect_Postinc
:
311 if (op
->reg
!= MSR_None
)
313 int reg
= REG_GET (op
->reg
);
314 int sign
= opc
->ofs_430x
? 20 : 16;
316 /* Index values are signed. */
317 if (addr
& (1 << (sign
- 1)))
318 addr
|= -(1 << sign
);
322 /* For MSP430 instructions the sum is limited to 16 bits if the
323 address in the index register is less than 64k even if we are
324 running on an MSP430X CPU. This is for MSP430 compatibility. */
325 if (reg
< 0x10000 && ! opc
->ofs_430x
)
328 fprintf (stderr
, " XXX WRAPPING ADDRESS %x on read\n", addr
);
337 sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, buf
, addr
, 1);
341 sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, buf
, addr
, 2);
342 rv
= buf
[0] | (buf
[1] << 8);
346 sim_core_read_buffer (sd
, STATE_CPU (sd
, 0), read_map
, buf
, addr
, 4);
347 rv
= buf
[0] | (buf
[1] << 8) | (buf
[2] << 16) | (buf
[3] << 24);
350 assert (! opc
->size
);
354 /* Hack - MSP430X5438 serial port status register. */
358 if ((addr
>= 0x130 && addr
<= 0x15B)
359 || (addr
>= 0x4C0 && addr
<= 0x4EB))
365 switch (HWMULT (sd
, hwmult_type
))
369 rv
= zero_ext (HWMULT (sd
, hwmult_result
), 16);
373 rv
= sign_ext (HWMULT (sd
, hwmult_signed_result
), 16);
380 switch (HWMULT (sd
, hwmult_type
))
384 rv
= zero_ext (HWMULT (sd
, hwmult_result
) >> 16, 16);
389 rv
= sign_ext (HWMULT (sd
, hwmult_signed_result
) >> 16, 16);
396 switch (HWMULT (sd
, hwmult_type
))
402 rv
= HWMULT (sd
, hwmult_signed_result
) < 0 ? -1 : 0;
405 rv
= 0; /* FIXME: Should be carry of last accumulate. */
408 rv
= HWMULT (sd
, hwmult_signed_accumulator
) < 0 ? -1 : 0;
415 rv
= zero_ext (HWMULT (sd
, hw32mult_result
), 16);
420 rv
= zero_ext (HWMULT (sd
, hw32mult_result
) >> 16, 16);
425 rv
= zero_ext (HWMULT (sd
, hw32mult_result
) >> 32, 16);
430 switch (HWMULT (sd
, hw32mult_type
))
432 case UNSIGN_64
: rv
= zero_ext (HWMULT (sd
, hw32mult_result
) >> 48, 16); break;
433 case SIGN_64
: rv
= sign_ext (HWMULT (sd
, hw32mult_result
) >> 48, 16); break;
438 fprintf (stderr
, "unimplemented HW MULT read from %x!\n", addr
);
443 TRACE_MEMORY (STATE_CPU (sd
, 0), "GET: [%#x].%d -> %#x", addr
, opc
->size
,
448 fprintf (stderr
, "invalid operand %d type %d\n", n
, op
->type
);
472 if (op
->type
== MSP430_Operand_Indirect_Postinc
)
473 REG_PUT (op
->reg
, REG_GET (op
->reg
) + incval
);
479 put_op (SIM_DESC sd
, MSP430_Opcode_Decoded
*opc
, int n
, int val
)
481 MSP430_Opcode_Operand
*op
= opc
->op
+ n
;
484 unsigned char buf
[4];
505 case MSP430_Operand_Register
:
507 REG_PUT (op
->reg
, val
);
509 case MSP430_Operand_Indirect
:
510 case MSP430_Operand_Indirect_Postinc
:
512 if (op
->reg
!= MSR_None
)
514 int reg
= REG_GET (op
->reg
);
515 int sign
= opc
->ofs_430x
? 20 : 16;
517 /* Index values are signed. */
518 if (addr
& (1 << (sign
- 1)))
519 addr
|= -(1 << sign
);
523 /* For MSP430 instructions the sum is limited to 16 bits if the
524 address in the index register is less than 64k even if we are
525 running on an MSP430X CPU. This is for MSP430 compatibility. */
526 if (reg
< 0x10000 && ! opc
->ofs_430x
)
529 fprintf (stderr
, " XXX WRAPPING ADDRESS %x on write\n", addr
);
536 TRACE_MEMORY (STATE_CPU (sd
, 0), "PUT: [%#x].%d <- %#x", addr
, opc
->size
,
539 /* Hack - MSP430X5438 serial port transmit register. */
543 if ((addr
>= 0x130 && addr
<= 0x15B)
544 || (addr
>= 0x4C0 && addr
<= 0x4EB))
548 /* Hardware Multiply emulation. */
549 assert (opc
->size
== 16);
555 HWMULT (sd
, hwmult_op1
) = val
;
556 HWMULT (sd
, hwmult_type
) = UNSIGN_32
;
561 HWMULT (sd
, hwmult_op1
) = val
;
562 HWMULT (sd
, hwmult_type
) = SIGN_32
;
567 HWMULT (sd
, hwmult_op1
) = val
;
568 HWMULT (sd
, hwmult_type
) = UNSIGN_MAC_32
;
573 HWMULT (sd
, hwmult_op1
) = val
;
574 HWMULT (sd
, hwmult_type
) = SIGN_MAC_32
;
579 HWMULT (sd
, hwmult_op2
) = val
;
580 switch (HWMULT (sd
, hwmult_type
))
583 a
= HWMULT (sd
, hwmult_op1
);
584 b
= HWMULT (sd
, hwmult_op2
);
585 /* For unsigned 32-bit multiplication of 16-bit operands, an
586 explicit cast is required to prevent any implicit
588 HWMULT (sd
, hwmult_result
) = (uint32_t) a
* (uint32_t) b
;
589 HWMULT (sd
, hwmult_signed_result
) = a
* b
;
590 HWMULT (sd
, hwmult_accumulator
) = HWMULT (sd
, hwmult_signed_accumulator
) = 0;
594 a
= sign_ext (HWMULT (sd
, hwmult_op1
), 16);
595 b
= sign_ext (HWMULT (sd
, hwmult_op2
), 16);
596 HWMULT (sd
, hwmult_signed_result
) = a
* b
;
597 HWMULT (sd
, hwmult_result
) = (uint32_t) a
* (uint32_t) b
;
598 HWMULT (sd
, hwmult_accumulator
) = HWMULT (sd
, hwmult_signed_accumulator
) = 0;
602 a
= HWMULT (sd
, hwmult_op1
);
603 b
= HWMULT (sd
, hwmult_op2
);
604 HWMULT (sd
, hwmult_accumulator
)
605 += (uint32_t) a
* (uint32_t) b
;
606 HWMULT (sd
, hwmult_signed_accumulator
) += a
* b
;
607 HWMULT (sd
, hwmult_result
) = HWMULT (sd
, hwmult_accumulator
);
608 HWMULT (sd
, hwmult_signed_result
) = HWMULT (sd
, hwmult_signed_accumulator
);
612 a
= sign_ext (HWMULT (sd
, hwmult_op1
), 16);
613 b
= sign_ext (HWMULT (sd
, hwmult_op2
), 16);
614 HWMULT (sd
, hwmult_accumulator
)
615 += (uint32_t) a
* (uint32_t) b
;
616 HWMULT (sd
, hwmult_signed_accumulator
) += a
* b
;
617 HWMULT (sd
, hwmult_result
) = HWMULT (sd
, hwmult_accumulator
);
618 HWMULT (sd
, hwmult_signed_result
) = HWMULT (sd
, hwmult_signed_accumulator
);
625 /* Copy into LOW result... */
626 switch (HWMULT (sd
, hwmult_type
))
630 HWMULT (sd
, hwmult_accumulator
) = HWMULT (sd
, hwmult_result
) = zero_ext (val
, 16);
631 HWMULT (sd
, hwmult_signed_accumulator
) = sign_ext (val
, 16);
635 HWMULT (sd
, hwmult_signed_accumulator
) = HWMULT (sd
, hwmult_result
) = sign_ext (val
, 16);
636 HWMULT (sd
, hwmult_accumulator
) = zero_ext (val
, 16);
643 HWMULT (sd
, hw32mult_op1
) = val
;
644 HWMULT (sd
, hw32mult_type
) = UNSIGN_64
;
649 HWMULT (sd
, hw32mult_op1
) = (HWMULT (sd
, hw32mult_op1
) & 0xFFFF) | (val
<< 16);
654 HWMULT (sd
, hw32mult_op1
) = val
;
655 HWMULT (sd
, hw32mult_type
) = SIGN_64
;
660 HWMULT (sd
, hw32mult_op1
) = (HWMULT (sd
, hw32mult_op1
) & 0xFFFF) | (val
<< 16);
665 HWMULT (sd
, hw32mult_op2
) = val
;
670 HWMULT (sd
, hw32mult_op2
) = (HWMULT (sd
, hw32mult_op2
) & 0xFFFF) | (val
<< 16);
671 switch (HWMULT (sd
, hw32mult_type
))
674 HWMULT (sd
, hw32mult_result
)
675 = (uint64_t) HWMULT (sd
, hw32mult_op1
)
676 * (uint64_t) HWMULT (sd
, hw32mult_op2
);
679 HWMULT (sd
, hw32mult_result
)
680 = sign_ext (HWMULT (sd
, hw32mult_op1
), 32)
681 * sign_ext (HWMULT (sd
, hw32mult_op2
), 32);
687 fprintf (stderr
, "unimplemented HW MULT write to %x!\n", addr
);
696 sim_core_write_buffer (sd
, STATE_CPU (sd
, 0), write_map
, buf
, addr
, 1);
701 sim_core_write_buffer (sd
, STATE_CPU (sd
, 0), write_map
, buf
, addr
, 2);
709 sim_core_write_buffer (sd
, STATE_CPU (sd
, 0), write_map
, buf
, addr
, 4);
712 assert (! opc
->size
);
717 fprintf (stderr
, "invalid operand %d type %d\n", n
, op
->type
);
741 if (op
->type
== MSP430_Operand_Indirect_Postinc
)
743 int new_val
= REG_GET (op
->reg
) + incval
;
744 /* SP is always word-aligned. */
745 if (op
->reg
== MSR_SP
&& (new_val
& 1))
747 REG_PUT (op
->reg
, new_val
);
754 mem_put_val (SIM_DESC sd
, int addr
, int val
, int bits
)
756 MSP430_Opcode_Decoded opc
;
759 opc
.op
[0].type
= MSP430_Operand_Indirect
;
760 opc
.op
[0].addend
= addr
;
761 opc
.op
[0].reg
= MSR_None
;
762 put_op (sd
, &opc
, 0, val
);
766 mem_get_val (SIM_DESC sd
, int addr
, int bits
)
768 MSP430_Opcode_Decoded opc
;
771 opc
.op
[0].type
= MSP430_Operand_Indirect
;
772 opc
.op
[0].addend
= addr
;
773 opc
.op
[0].reg
= MSR_None
;
774 return get_op (sd
, &opc
, 0);
777 #define CIO_OPEN (0xF0)
778 #define CIO_CLOSE (0xF1)
779 #define CIO_READ (0xF2)
780 #define CIO_WRITE (0xF3)
781 #define CIO_LSEEK (0xF4)
782 #define CIO_UNLINK (0xF5)
783 #define CIO_GETENV (0xF6)
784 #define CIO_RENAME (0xF7)
785 #define CIO_GETTIME (0xF8)
786 #define CIO_GETCLK (0xF9)
787 #define CIO_SYNC (0xFF)
789 #define CIO_I(n) (parms[(n)] + parms[(n)+1] * 256)
790 #define CIO_L(n) (parms[(n)] + parms[(n)+1] * 256 \
791 + parms[(n)+2] * 65536 + parms[(n)+3] * 16777216)
794 msp430_cio (SIM_DESC sd
)
796 /* A block of data at __CIOBUF__ describes the I/O operation to
798 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
799 struct msp430_cpu_state
*msp430_cpu
= MSP430_SIM_CPU (cpu
);
800 unsigned char raw_parms
[13];
801 unsigned char parms
[8];
804 unsigned char buffer
[512];
806 long fd
, addr
, len
, rv
;
808 sim_core_read_buffer (sd
, cpu
, 0, parms
, msp430_cpu
->cio_buffer
, 5);
812 sim_core_read_buffer (sd
, cpu
, 0, parms
, msp430_cpu
->cio_buffer
+ 3, 8);
813 sim_core_read_buffer (sd
, cpu
, 0, buffer
, msp430_cpu
->cio_buffer
+ 11, length
);
821 rv
= write (fd
, buffer
, len
);
822 parms
[0] = rv
& 0xff;
828 sim_core_write_buffer (sd
, cpu
, 0, parms
, msp430_cpu
->cio_buffer
+ 4, 8);
830 sim_core_write_buffer (sd
, cpu
, 0, buffer
, msp430_cpu
->cio_buffer
+ 12,
834 #define SRC get_op (sd, opcode, 1)
835 #define DSRC get_op (sd, opcode, 0)
836 #define DEST(V) put_op (sd, opcode, 0, (V))
838 #define DO_ALU(OP,SOP,MORE) \
842 int result = s1 OP s2 MORE; \
843 TRACE_ALU (STATE_CPU (sd, 0), "ALU: %#x %s %#x %s = %#x", s1, SOP, \
844 s2, #MORE, result); \
848 #define SIGN (1 << (opcode->size - 1))
849 #define POS(x) (((x) & SIGN) ? 0 : 1)
850 #define NEG(x) (((x) & SIGN) ? 1 : 0)
852 #define SX(v) sign_ext (v, opcode->size)
853 #define ZX(v) zero_ext (v, opcode->size)
858 static char buf
[2][6];
864 bp
[0] = f
& MSP430_FLAG_V
? 'V' : '-';
865 bp
[1] = f
& MSP430_FLAG_N
? 'N' : '-';
866 bp
[2] = f
& MSP430_FLAG_Z
? 'Z' : '-';
867 bp
[3] = f
& MSP430_FLAG_C
? 'C' : '-';
872 /* Random number that won't show up in our usual logic. */
873 #define MAGIC_OVERFLOW 0x55000F
876 do_flags (SIM_DESC sd
,
877 MSP430_Opcode_Decoded
*opcode
,
878 int vnz_val
, /* Signed result. */
884 int signbit
= 1 << (opcode
->size
- 1);
886 f
&= ~opcode
->flags_0
;
887 f
&= ~opcode
->flags_set
;
888 f
|= opcode
->flags_1
;
890 if (vnz_val
& signbit
)
891 new_f
|= MSP430_FLAG_N
;
892 if (! (vnz_val
& ((signbit
<< 1) - 1)))
893 new_f
|= MSP430_FLAG_Z
;
894 if (overflow
== MAGIC_OVERFLOW
)
896 if (vnz_val
!= SX (vnz_val
))
897 new_f
|= MSP430_FLAG_V
;
901 new_f
|= MSP430_FLAG_V
;
903 new_f
|= MSP430_FLAG_C
;
905 new_f
= f
| (new_f
& opcode
->flags_set
);
907 TRACE_ALU (STATE_CPU (sd
, 0), "FLAGS: %s -> %s", flags2string (SR
),
908 flags2string (new_f
));
910 TRACE_ALU (STATE_CPU (sd
, 0), "FLAGS: %s", flags2string (new_f
));
914 #define FLAGS(vnz,c) do_flags (sd, opcode, vnz, c, MAGIC_OVERFLOW)
915 #define FLAGSV(vnz,c,v) do_flags (sd, opcode, vnz, c, v)
917 /* These two assume unsigned 16-bit (four digit) words.
918 Mask off unwanted bits for byte operations. */
921 bcd_to_binary (int v
)
923 int r
= ( ((v
>> 0) & 0xf) * 1
924 + ((v
>> 4) & 0xf) * 10
925 + ((v
>> 8) & 0xf) * 100
926 + ((v
>> 12) & 0xf) * 1000);
931 binary_to_bcd (int v
)
933 int r
= ( ((v
/ 1) % 10) << 0
934 | ((v
/ 10) % 10) << 4
935 | ((v
/ 100) % 10) << 8
936 | ((v
/ 1000) % 10) << 12);
941 cond_string (int cond
)
966 /* Checks a CALL to address CALL_ADDR. If this is a special
967 syscall address then the call is simulated and non-zero is
968 returned. Otherwise 0 is returned. */
971 maybe_perform_syscall (SIM_DESC sd
, int call_addr
)
973 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
974 struct msp430_cpu_state
*msp430_cpu
= MSP430_SIM_CPU (cpu
);
976 if (call_addr
== 0x00160)
980 for (i
= 0; i
< 16; i
++)
983 fprintf (stderr
, "\t");
984 fprintf (stderr
, "R%-2d %05x ", i
, msp430_cpu
->regs
[i
]);
987 int sp
= SP
+ (3 - (i
/ 4)) * 2;
988 unsigned char buf
[2];
990 sim_core_read_buffer (sd
, cpu
, read_map
, buf
, sp
, 2);
992 fprintf (stderr
, "\tSP%+d: %04x", sp
- SP
,
993 buf
[0] + buf
[1] * 256);
999 fprintf (stderr
, flags
& 0x100 ? " V" : " -");
1000 fprintf (stderr
, flags
& 0x004 ? "N" : "-");
1001 fprintf (stderr
, flags
& 0x002 ? "Z" : "-");
1002 fprintf (stderr
, flags
& 0x001 ? "C" : "-");
1005 fprintf (stderr
, "\n");
1011 if ((call_addr
& ~0x3f) == 0x00180)
1014 int arg1
, arg2
, arg3
, arg4
;
1015 int syscall_num
= call_addr
& 0x3f;
1017 /* syscall_num == 2 is used for the variadic function "open".
1018 The arguments are set up differently for variadic functions.
1019 See slaa534.pdf distributed by TI. */
1020 if (syscall_num
== 2)
1022 arg1
= msp430_cpu
->regs
[12];
1023 arg2
= mem_get_val (sd
, SP
, 16);
1024 arg3
= mem_get_val (sd
, SP
+ 2, 16);
1025 arg4
= mem_get_val (sd
, SP
+ 4, 16);
1029 arg1
= msp430_cpu
->regs
[12];
1030 arg2
= msp430_cpu
->regs
[13];
1031 arg3
= msp430_cpu
->regs
[14];
1032 arg4
= msp430_cpu
->regs
[15];
1035 msp430_cpu
->regs
[12] = sim_syscall (cpu
, syscall_num
, arg1
, arg2
, arg3
,
1044 msp430_step_once (SIM_DESC sd
)
1046 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
1047 struct msp430_cpu_state
*msp430_cpu
= MSP430_SIM_CPU (cpu
);
1048 Get_Byte_Local_Data ld
;
1049 unsigned char buf
[100];
1052 unsigned int opcode_pc
;
1053 MSP430_Opcode_Decoded opcode_buf
;
1054 MSP430_Opcode_Decoded
*opcode
= &opcode_buf
;
1056 int u1
= 0, u2
, uresult
;
1062 int op_bytes
= 0, op_bits
;
1067 if (opcode_pc
< 0x10)
1069 fprintf (stderr
, "Fault: PC(%#x) is less than 0x10\n", opcode_pc
);
1070 sim_engine_halt (sd
, cpu
, NULL
, msp430_cpu
->regs
[0], sim_exited
, -1);
1074 if (PC
== msp430_cpu
->cio_breakpoint
&& STATE_OPEN_KIND (sd
) != SIM_OPEN_DEBUG
)
1079 opsize
= msp430_decode_opcode (msp430_cpu
->regs
[0], opcode
, msp430_getbyte
,
1084 fprintf (stderr
, "Fault: undecodable opcode at %#x\n", opcode_pc
);
1085 sim_engine_halt (sd
, cpu
, NULL
, msp430_cpu
->regs
[0], sim_exited
, -1);
1089 if (opcode
->repeat_reg
)
1090 n_repeats
= (msp430_cpu
->regs
[opcode
->repeats
] & 0x000f) + 1;
1092 n_repeats
= opcode
->repeats
+ 1;
1094 op_bits
= opcode
->size
;
1109 if (TRACE_ANY_P (cpu
))
1110 trace_prefix (sd
, cpu
, NULL_CIA
, opcode_pc
, TRACE_LINENUM_P (cpu
), NULL
,
1113 TRACE_DISASM (cpu
, opcode_pc
);
1121 /* Double-operand instructions. */
1123 if (opcode
->n_bytes
== 2
1124 && opcode
->op
[0].type
== MSP430_Operand_Register
1125 && opcode
->op
[0].reg
== MSR_CG
1126 && opcode
->op
[1].type
== MSP430_Operand_Immediate
1127 && opcode
->op
[1].addend
== 0
1128 /* A 16-bit write of #0 is a NOP; an 8-bit write is a BRK. */
1129 && opcode
->size
== 8)
1131 /* This is the designated software breakpoint instruction. */
1133 sim_engine_halt (sd
, cpu
, NULL
, msp430_cpu
->regs
[0], sim_stopped
,
1138 /* Otherwise, do the move. */
1139 for (rept
= 0; rept
< n_repeats
; rept
++)
1147 for (rept
= 0; rept
< n_repeats
; rept
++)
1149 carry_to_use
= (SR
& MSP430_FLAG_C
) ? 1 : 0;
1154 uresult
= u1
+ u2
+ carry_to_use
;
1155 result
= s1
+ s2
+ carry_to_use
;
1156 TRACE_ALU (cpu
, "ADDC: %#x + %#x + %d = %#x",
1157 u1
, u2
, carry_to_use
, uresult
);
1159 FLAGS (result
, uresult
!= ZX (uresult
));
1164 for (rept
= 0; rept
< n_repeats
; rept
++)
1172 TRACE_ALU (cpu
, "ADD: %#x + %#x = %#x", u1
, u2
, uresult
);
1174 FLAGS (result
, uresult
!= ZX (uresult
));
1179 for (rept
= 0; rept
< n_repeats
; rept
++)
1181 carry_to_use
= (SR
& MSP430_FLAG_C
) ? 1 : 0;
1186 uresult
= ZX (~u2
) + u1
+ carry_to_use
;
1187 result
= s1
- s2
+ (carry_to_use
- 1);
1188 TRACE_ALU (cpu
, "SUBC: %#x - %#x + %d = %#x",
1189 u1
, u2
, carry_to_use
, uresult
);
1191 FLAGS (result
, uresult
!= ZX (uresult
));
1196 for (rept
= 0; rept
< n_repeats
; rept
++)
1202 uresult
= ZX (~u2
) + u1
+ 1;
1203 result
= SX (uresult
);
1204 TRACE_ALU (cpu
, "SUB: %#x - %#x = %#x",
1207 FLAGS (result
, uresult
!= ZX (uresult
));
1212 for (rept
= 0; rept
< n_repeats
; rept
++)
1218 uresult
= ZX (~u2
) + u1
+ 1;
1220 TRACE_ALU (cpu
, "CMP: %#x - %#x = %x",
1222 FLAGS (result
, uresult
!= ZX (uresult
));
1227 for (rept
= 0; rept
< n_repeats
; rept
++)
1229 carry_to_use
= (SR
& MSP430_FLAG_C
) ? 1 : 0;
1232 uresult
= bcd_to_binary (u1
) + bcd_to_binary (u2
) + carry_to_use
;
1233 result
= binary_to_bcd (uresult
);
1234 TRACE_ALU (cpu
, "DADD: %#x + %#x + %d = %#x",
1235 u1
, u2
, carry_to_use
, result
);
1237 FLAGS (result
, uresult
> ((opcode
->size
== 8) ? 99 : 9999));
1242 for (rept
= 0; rept
< n_repeats
; rept
++)
1247 TRACE_ALU (cpu
, "AND: %#x & %#x = %#x",
1250 FLAGS (uresult
, uresult
!= 0);
1255 for (rept
= 0; rept
< n_repeats
; rept
++)
1260 TRACE_ALU (cpu
, "BIT: %#x & %#x -> %#x",
1262 FLAGS (uresult
, uresult
!= 0);
1267 for (rept
= 0; rept
< n_repeats
; rept
++)
1271 uresult
= u1
& ~ u2
;
1272 TRACE_ALU (cpu
, "BIC: %#x & ~ %#x = %#x",
1279 for (rept
= 0; rept
< n_repeats
; rept
++)
1284 TRACE_ALU (cpu
, "BIS: %#x | %#x = %#x",
1291 for (rept
= 0; rept
< n_repeats
; rept
++)
1293 s1
= 1 << (opcode
->size
- 1);
1297 TRACE_ALU (cpu
, "XOR: %#x & %#x = %#x",
1300 FLAGSV (uresult
, uresult
!= 0, (u1
& s1
) && (u2
& s1
));
1304 /* Single-operand instructions. Note: the decoder puts the same
1305 operand in SRC as in DEST, for our convenience. */
1308 for (rept
= 0; rept
< n_repeats
; rept
++)
1311 carry_to_use
= u1
& 1;
1313 /* If the ZC bit of the opcode is set, it means we are synthesizing
1314 RRUX, so the carry bit must be ignored. */
1315 if (opcode
->zc
== 0 && (SR
& MSP430_FLAG_C
))
1316 uresult
|= (1 << (opcode
->size
- 1));
1317 TRACE_ALU (cpu
, "RRC: %#x >>= %#x",
1320 FLAGS (uresult
, carry_to_use
);
1325 for (rept
= 0; rept
< n_repeats
; rept
++)
1328 uresult
= ((u1
>> 8) & 0x00ff) | ((u1
<< 8) & 0xff00);
1329 TRACE_ALU (cpu
, "SWPB: %#x -> %#x",
1336 for (rept
= 0; rept
< n_repeats
; rept
++)
1340 s1
= 1 << (opcode
->size
- 1);
1341 uresult
= (u1
>> 1) | (u1
& s1
);
1342 TRACE_ALU (cpu
, "RRA: %#x >>= %#x",
1350 for (rept
= 0; rept
< n_repeats
; rept
++)
1354 uresult
= (u1
>> 1);
1355 TRACE_ALU (cpu
, "RRU: %#x >>= %#x",
1363 for (rept
= 0; rept
< n_repeats
; rept
++)
1367 uresult
= u1
| 0xfff00;
1369 uresult
= u1
& 0x000ff;
1370 TRACE_ALU (cpu
, "SXT: %#x -> %#x", u1
, uresult
);
1377 for (rept
= 0; rept
< n_repeats
; rept
++)
1381 new_sp
= REG_GET (MSR_SP
) - op_bytes
;
1382 /* SP is always word-aligned. */
1385 REG_PUT (MSR_SP
, new_sp
);
1387 mem_put_val (sd
, SP
, u1
, op_bits
);
1388 if (opcode
->op
[1].type
== MSP430_Operand_Register
)
1389 opcode
->op
[1].reg
--;
1394 for (rept
= 0; rept
< n_repeats
; rept
++)
1398 u1
= mem_get_val (sd
, SP
, op_bits
);
1400 if (opcode
->op
[0].type
== MSP430_Operand_Register
)
1401 opcode
->op
[0].reg
++;
1402 new_sp
= REG_GET (MSR_SP
) + op_bytes
;
1403 /* SP is always word-aligned. */
1406 REG_PUT (MSR_SP
, new_sp
);
1413 if (maybe_perform_syscall (sd
, u1
))
1416 REG_PUT (MSR_SP
, REG_GET (MSR_SP
) - op_bytes
);
1417 mem_put_val (sd
, SP
, PC
, op_bits
);
1418 TRACE_ALU (cpu
, "CALL: func %#x ret %#x, sp %#x",
1420 REG_PUT (MSR_PC
, u1
);
1424 u1
= mem_get_val (sd
, SP
, 16);
1427 PC
= mem_get_val (sd
, SP
, 16);
1429 /* Emulate the RETI action of the 20-bit CPUX architecure.
1430 This is safe for 16-bit CPU architectures as well, since the top
1431 8-bits of SR will have been written to the stack here, and will
1432 have been read as 0. */
1433 PC
|= (u1
& 0xF000) << 4;
1434 TRACE_ALU (cpu
, "RETI: pc %#x sr %#x", PC
, SR
);
1441 switch (opcode
->cond
)
1444 u1
= (SR
& MSP430_FLAG_Z
) ? 0 : 1;
1447 u1
= (SR
& MSP430_FLAG_Z
) ? 1 : 0;
1450 u1
= (SR
& MSP430_FLAG_C
) ? 0 : 1;
1453 u1
= (SR
& MSP430_FLAG_C
) ? 1 : 0;
1456 u1
= (SR
& MSP430_FLAG_N
) ? 1 : 0;
1459 u1
= (!!(SR
& MSP430_FLAG_N
) == !!(SR
& MSP430_FLAG_V
)) ? 1 : 0;
1462 u1
= (!!(SR
& MSP430_FLAG_N
) == !!(SR
& MSP430_FLAG_V
)) ? 0 : 1;
1471 TRACE_BRANCH (cpu
, "J%s: pc %#x -> %#x sr %#x, taken",
1472 cond_string (opcode
->cond
), PC
, i
, SR
);
1474 if (PC
== opcode_pc
)
1478 TRACE_BRANCH (cpu
, "J%s: pc %#x to %#x sr %#x, not taken",
1479 cond_string (opcode
->cond
), PC
, i
, SR
);
1483 fprintf (stderr
, "error: unexpected opcode id %d\n", opcode
->id
);
1489 sim_engine_run (SIM_DESC sd
,
1496 msp430_step_once (sd
);
1497 if (sim_events_tick (sd
))
1498 sim_events_process (sd
);