1 /* Simulator for TI MSP430 and MSP430X
3 Copyright (C) 2013-2014 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/>. */
29 #include "opcode/msp430-decode.h"
32 #include "targ-vals.h"
35 loader_write_mem (SIM_DESC sd
,
37 const unsigned char *buf
,
40 SIM_CPU
*cpu
= MSP430_CPU (sd
);
41 return sim_core_write_buffer (sd
, cpu
, write_map
, buf
, taddr
, bytes
);
45 msp430_pc_fetch (SIM_CPU
*cpu
)
47 return cpu
->state
.regs
[0];
51 msp430_pc_store (SIM_CPU
*cpu
, sim_cia newpc
)
53 cpu
->state
.regs
[0] = newpc
;
57 lookup_symbol (SIM_DESC sd
, const char *name
)
59 struct bfd
*abfd
= STATE_PROG_BFD (sd
);
60 asymbol
**symbol_table
= STATE_SYMBOL_TABLE (sd
);
61 long number_of_symbols
= STATE_NUM_SYMBOLS (sd
);
64 if (symbol_table
== NULL
)
68 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
69 if (storage_needed
<= 0)
72 STATE_SYMBOL_TABLE (sd
) = symbol_table
= xmalloc (storage_needed
);
73 STATE_NUM_SYMBOLS (sd
) = number_of_symbols
=
74 bfd_canonicalize_symtab (abfd
, symbol_table
);
77 for (i
= 0; i
< number_of_symbols
; i
++)
78 if (strcmp (symbol_table
[i
]->name
, name
) == 0)
80 long val
= symbol_table
[i
]->section
->vma
+ symbol_table
[i
]->value
;
87 msp430_reg_fetch (SIM_CPU
*cpu
, int regno
, unsigned char *buf
, int len
)
89 if (0 <= regno
&& regno
< 16)
93 int val
= cpu
->state
.regs
[regno
];
95 buf
[1] = (val
>> 8) & 0xff;
100 int val
= cpu
->state
.regs
[regno
];
102 buf
[1] = (val
>> 8) & 0xff;
103 buf
[2] = (val
>> 16) & 0x0f; /* Registers are only 20 bits wide. */
115 msp430_reg_store (SIM_CPU
*cpu
, int regno
, unsigned char *buf
, int len
)
117 if (0 <= regno
&& regno
< 16)
121 cpu
->state
.regs
[regno
] = (buf
[1] << 8) | buf
[0];
127 cpu
->state
.regs
[regno
] = ((buf
[2] << 16) & 0xf0000)
128 | (buf
[1] << 8) | buf
[0];
137 msp430_initialize_cpu (SIM_DESC sd
, SIM_CPU
*cpu
)
139 memset (&cpu
->state
, 0, sizeof (cpu
->state
));
143 sim_open (SIM_OPEN_KIND kind
,
144 struct host_callback_struct
*callback
,
148 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
150 struct bfd
*prog_bfd
;
152 /* Initialise the simulator. */
154 if (sim_cpu_alloc_all (sd
, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK
)
160 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
166 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
172 CPU_PC_FETCH (MSP430_CPU (sd
)) = msp430_pc_fetch
;
173 CPU_PC_STORE (MSP430_CPU (sd
)) = msp430_pc_store
;
174 CPU_REG_FETCH (MSP430_CPU (sd
)) = msp430_reg_fetch
;
175 CPU_REG_STORE (MSP430_CPU (sd
)) = msp430_reg_store
;
177 /* Allocate memory if none specified by user. */
178 if (sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, &c
, 0x130, 1) == 0)
179 sim_do_commandf (sd
, "memory-region 0,0x20");
180 if (sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, &c
, 0x200, 1) == 0)
181 sim_do_commandf (sd
, "memory-region 0x200,0xffe00");
182 if (sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, &c
, 0xfffe, 1) == 0)
183 sim_do_commandf (sd
, "memory-region 0xfffe,2");
184 if (sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, &c
, 0x10000, 1) == 0)
185 sim_do_commandf (sd
, "memory-region 0x10000,0x100000");
187 /* Check for/establish the a reference program image. */
188 if (sim_analyze_program (sd
,
189 (STATE_PROG_ARGV (sd
) != NULL
190 ? *STATE_PROG_ARGV (sd
)
191 : NULL
), abfd
) != SIM_RC_OK
)
197 prog_bfd
= sim_load_file (sd
, argv
[0], callback
,
201 1 /* use LMA instead of VMA */,
203 if (prog_bfd
== NULL
)
209 /* Establish any remaining configuration options. */
210 if (sim_config (sd
) != SIM_RC_OK
)
216 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
222 /* CPU specific initialization. */
223 assert (MAX_NR_PROCESSORS
== 1);
224 msp430_initialize_cpu (sd
, MSP430_CPU (sd
));
226 msp430_trace_init (STATE_PROG_BFD (sd
));
228 MSP430_CPU (sd
)->state
.cio_breakpoint
= lookup_symbol (sd
, "C$$IO$$");
229 MSP430_CPU (sd
)->state
.cio_buffer
= lookup_symbol (sd
, "__CIOBUF__");
230 if (MSP430_CPU (sd
)->state
.cio_buffer
== -1)
231 MSP430_CPU (sd
)->state
.cio_buffer
= lookup_symbol (sd
, "_CIOBUF_");
237 sim_close (SIM_DESC sd
,
240 free (STATE_SYMBOL_TABLE (sd
));
245 sim_create_inferior (SIM_DESC sd
,
250 unsigned char resetv
[2];
254 /* Set the PC to the default reset vector if available. */
255 c
= sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, resetv
, 0xfffe, 2);
256 new_pc
= resetv
[0] + 256 * resetv
[1];
258 /* If the reset vector isn't initialized, then use the ELF entry. */
259 if (abfd
!= NULL
&& !new_pc
)
260 new_pc
= bfd_get_start_address (abfd
);
262 sim_pc_set (MSP430_CPU (sd
), new_pc
);
263 msp430_pc_store (MSP430_CPU (sd
), new_pc
);
272 } Get_Byte_Local_Data
;
275 msp430_getbyte (void *vld
)
277 Get_Byte_Local_Data
*ld
= (Get_Byte_Local_Data
*)vld
;
279 SIM_DESC sd
= ld
->sd
;
281 sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, buf
, ld
->gb_addr
, 1);
286 #define REG(N) MSP430_CPU (sd)->state.regs[(N)]
287 #define PC REG(MSR_PC)
288 #define SP REG(MSR_SP)
289 #define SR REG(MSR_SR)
294 "PC", "SP", "SR", "CG", "R4", "R5", "R6", "R7", "R8",
295 "R9", "R10", "R11", "R12", "R13", "R14", "R15"
299 trace_reg_put (SIM_DESC sd
, int n
, unsigned int v
)
301 if (TRACE_VPU_P (MSP430_CPU (sd
)))
302 trace_generic (sd
, MSP430_CPU (sd
), TRACE_VPU_IDX
,
303 "PUT: %#x -> %s", v
, register_names
[n
]);
308 trace_reg_get (SIM_DESC sd
, int n
)
310 if (TRACE_VPU_P (MSP430_CPU (sd
)))
311 trace_generic (sd
, MSP430_CPU (sd
), TRACE_VPU_IDX
,
312 "GET: %s -> %#x", register_names
[n
], REG (n
));
316 #define REG_PUT(N,V) trace_reg_put (sd, N, V)
317 #define REG_GET(N) trace_reg_get (sd, N)
319 /* Hardware multiply (and accumulate) support. */
320 static enum { UNSIGN_32
, SIGN_32
, UNSIGN_MAC_32
, SIGN_MAC_32
} hwmult_type
;
321 static unsigned32 hwmult_op1
;
322 static unsigned32 hwmult_op2
;
323 static unsigned32 hwmult_result
;
324 static signed32 hwmult_signed_result
;
325 static unsigned32 hwmult_accumulator
;
326 static signed32 hwmult_signed_accumulator
;
328 static enum { UNSIGN_64
, SIGN_64
} hw32mult_type
;
329 static unsigned64 hw32mult_op1
;
330 static unsigned64 hw32mult_op2
;
331 static unsigned64 hw32mult_result
;
334 zero_ext (unsigned int v
, unsigned int bits
)
336 v
&= ((1 << bits
) - 1);
340 static signed long long
341 sign_ext (signed long long v
, unsigned int bits
)
343 signed long long sb
= 1LL << (bits
-1); /* Sign bit. */
344 signed long long mb
= (1LL << (bits
-1)) - 1LL; /* Mantissa bits. */
354 get_op (SIM_DESC sd
, MSP430_Opcode_Decoded
*opc
, int n
)
356 MSP430_Opcode_Operand
*op
= opc
->op
+ n
;
359 unsigned char buf
[4];
364 case MSP430_Operand_Immediate
:
367 case MSP430_Operand_Register
:
368 rv
= REG_GET (op
->reg
);
370 case MSP430_Operand_Indirect
:
371 case MSP430_Operand_Indirect_Postinc
:
373 if (op
->reg
!= MSR_None
)
376 /* Index values are signed, but the sum is limited to 16
377 bits if the register < 64k, for MSP430 compatibility in
381 reg
= REG_GET (op
->reg
);
383 if (reg
< 0x10000 && ! opc
->ofs_430x
)
390 sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, buf
, addr
, 1);
394 sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, buf
, addr
, 2);
395 rv
= buf
[0] | (buf
[1] << 8);
399 sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, buf
, addr
, 4);
400 rv
= buf
[0] | (buf
[1] << 8) | (buf
[2] << 16) | (buf
[3] << 24);
403 assert (! opc
->size
);
407 /* Hack - MSP430X5438 serial port status register. */
411 if (addr
>= 0x130 && addr
<= 0x15B)
419 case UNSIGN_32
: rv
= zero_ext (hwmult_result
, 16); break;
421 case SIGN_32
: rv
= sign_ext (hwmult_signed_result
, 16); break;
430 rv
= zero_ext (hwmult_result
>> 16, 16);
435 rv
= sign_ext (hwmult_signed_result
>> 16, 16);
447 rv
= hwmult_signed_result
< 0 ? -1 : 0;
450 rv
= 0; /* FIXME: Should be carry of last accumulate. */
453 rv
= hwmult_signed_accumulator
< 0 ? -1 : 0;
459 rv
= zero_ext (hw32mult_result
, 16);
463 rv
= zero_ext (hw32mult_result
>> 16, 16);
467 rv
= zero_ext (hw32mult_result
>> 32, 16);
471 switch (hw32mult_type
)
473 case UNSIGN_64
: rv
= zero_ext (hw32mult_result
>> 48, 16); break;
474 case SIGN_64
: rv
= sign_ext (hw32mult_result
>> 48, 16); break;
479 fprintf (stderr
, "unimplemented HW MULT read!\n");
484 if (TRACE_MEMORY_P (MSP430_CPU (sd
)))
485 trace_generic (sd
, MSP430_CPU (sd
), TRACE_MEMORY_IDX
,
486 "GET: [%#x].%d -> %#x", addr
, opc
->size
, rv
);
489 fprintf (stderr
, "invalid operand %d type %d\n", n
, op
->type
);
513 if (op
->type
== MSP430_Operand_Indirect_Postinc
)
514 REG_PUT (op
->reg
, REG_GET (op
->reg
) + incval
);
520 put_op (SIM_DESC sd
, MSP430_Opcode_Decoded
*opc
, int n
, int val
)
522 MSP430_Opcode_Operand
*op
= opc
->op
+ n
;
525 unsigned char buf
[4];
546 case MSP430_Operand_Register
:
548 REG_PUT (op
->reg
, val
);
550 case MSP430_Operand_Indirect
:
551 case MSP430_Operand_Indirect_Postinc
:
553 if (op
->reg
!= MSR_None
)
556 /* Index values are signed, but the sum is limited to 16
557 bits if the register < 64k, for MSP430 compatibility in
561 reg
= REG_GET (op
->reg
);
568 if (TRACE_MEMORY_P (MSP430_CPU (sd
)))
569 trace_generic (sd
, MSP430_CPU (sd
), TRACE_MEMORY_IDX
,
570 "PUT: [%#x].%d <- %#x", addr
, opc
->size
, val
);
572 /* Hack - MSP430X5438 serial port transmit register. */
576 if (addr
>= 0x130 && addr
<= 0x15B)
580 /* Hardware Multiply emulation. */
581 assert (opc
->size
== 16);
585 case 0x130: hwmult_op1
= val
; hwmult_type
= UNSIGN_32
; break;
586 case 0x132: hwmult_op1
= val
; hwmult_type
= SIGN_32
; break;
587 case 0x134: hwmult_op1
= val
; hwmult_type
= UNSIGN_MAC_32
; break;
588 case 0x136: hwmult_op1
= val
; hwmult_type
= SIGN_MAC_32
; break;
590 case 0x138: hwmult_op2
= val
;
594 hwmult_result
= hwmult_op1
* hwmult_op2
;
595 hwmult_signed_result
= (signed) hwmult_result
;
596 hwmult_accumulator
= hwmult_signed_accumulator
= 0;
600 a
= sign_ext (hwmult_op1
, 16);
601 b
= sign_ext (hwmult_op2
, 16);
602 hwmult_signed_result
= a
* b
;
603 hwmult_result
= (unsigned) hwmult_signed_result
;
604 hwmult_accumulator
= hwmult_signed_accumulator
= 0;
608 hwmult_accumulator
+= hwmult_op1
* hwmult_op2
;
609 hwmult_signed_accumulator
+= hwmult_op1
* hwmult_op2
;
610 hwmult_result
= hwmult_accumulator
;
611 hwmult_signed_result
= hwmult_signed_accumulator
;
615 a
= sign_ext (hwmult_op1
, 16);
616 b
= sign_ext (hwmult_op2
, 16);
617 hwmult_accumulator
+= a
* b
;
618 hwmult_signed_accumulator
+= a
* b
;
619 hwmult_result
= hwmult_accumulator
;
620 hwmult_signed_result
= hwmult_signed_accumulator
;
626 /* Copy into LOW result... */
631 hwmult_accumulator
= hwmult_result
= zero_ext (val
, 16);
632 hwmult_signed_accumulator
= sign_ext (val
, 16);
636 hwmult_signed_accumulator
= hwmult_result
= sign_ext (val
, 16);
637 hwmult_accumulator
= zero_ext (val
, 16);
642 case 0x140: hw32mult_op1
= val
; hw32mult_type
= UNSIGN_64
; break;
643 case 0x142: hw32mult_op1
= (hw32mult_op1
& 0xFFFF) | (val
<< 16); break;
644 case 0x144: hw32mult_op1
= val
; hw32mult_type
= SIGN_64
; break;
645 case 0x146: hw32mult_op1
= (hw32mult_op1
& 0xFFFF) | (val
<< 16); break;
646 case 0x150: hw32mult_op2
= val
; break;
648 case 0x152: hw32mult_op2
= (hw32mult_op2
& 0xFFFF) | (val
<< 16);
649 switch (hw32mult_type
)
652 hw32mult_result
= hw32mult_op1
* hw32mult_op2
;
655 hw32mult_result
= sign_ext (hw32mult_op1
, 32) * sign_ext (hw32mult_op2
, 32);
661 fprintf (stderr
, "unimplemented HW MULT write to %x!\n", addr
);
670 sim_core_write_buffer (sd
, MSP430_CPU (sd
), write_map
, buf
, addr
, 1);
675 sim_core_write_buffer (sd
, MSP430_CPU (sd
), write_map
, buf
, addr
, 2);
683 sim_core_write_buffer (sd
, MSP430_CPU (sd
), write_map
, buf
, addr
, 4);
686 assert (! opc
->size
);
691 fprintf (stderr
, "invalid operand %d type %d\n", n
, op
->type
);
715 if (op
->type
== MSP430_Operand_Indirect_Postinc
)
717 int new_val
= REG_GET (op
->reg
) + incval
;
718 /* SP is always word-aligned. */
719 if (op
->reg
== MSR_SP
&& (new_val
& 1))
721 REG_PUT (op
->reg
, new_val
);
728 mem_put_val (SIM_DESC sd
, int addr
, int val
, int bits
)
730 MSP430_Opcode_Decoded opc
;
733 opc
.op
[0].type
= MSP430_Operand_Indirect
;
734 opc
.op
[0].addend
= addr
;
735 opc
.op
[0].reg
= MSR_None
;
736 put_op (sd
, &opc
, 0, val
);
740 mem_get_val (SIM_DESC sd
, int addr
, int bits
)
742 MSP430_Opcode_Decoded opc
;
745 opc
.op
[0].type
= MSP430_Operand_Indirect
;
746 opc
.op
[0].addend
= addr
;
747 opc
.op
[0].reg
= MSR_None
;
748 return get_op (sd
, &opc
, 0);
751 #define CIO_OPEN (0xF0)
752 #define CIO_CLOSE (0xF1)
753 #define CIO_READ (0xF2)
754 #define CIO_WRITE (0xF3)
755 #define CIO_LSEEK (0xF4)
756 #define CIO_UNLINK (0xF5)
757 #define CIO_GETENV (0xF6)
758 #define CIO_RENAME (0xF7)
759 #define CIO_GETTIME (0xF8)
760 #define CIO_GETCLK (0xF9)
761 #define CIO_SYNC (0xFF)
763 #define CIO_I(n) (parms[(n)] + parms[(n)+1] * 256)
764 #define CIO_L(n) (parms[(n)] + parms[(n)+1] * 256 \
765 + parms[(n)+2] * 65536 + parms[(n)+3] * 16777216)
768 msp430_cio (SIM_DESC sd
)
770 /* A block of data at __CIOBUF__ describes the I/O operation to
773 unsigned char raw_parms
[13];
774 unsigned char parms
[8];
777 unsigned char buffer
[512];
779 long fd
, addr
, len
, rv
;
781 sim_core_read_buffer (sd
, MSP430_CPU (sd
), 0, parms
,
782 MSP430_CPU (sd
)->state
.cio_buffer
, 5);
786 sim_core_read_buffer (sd
, MSP430_CPU (sd
), 0, parms
,
787 MSP430_CPU (sd
)->state
.cio_buffer
+ 3, 8);
789 sim_core_read_buffer (sd
, MSP430_CPU (sd
), 0, buffer
,
790 MSP430_CPU (sd
)->state
.cio_buffer
+ 11, length
);
798 rv
= write (fd
, buffer
, len
);
799 parms
[0] = rv
& 0xff;
805 sim_core_write_buffer (sd
, MSP430_CPU (sd
), 0, parms
,
806 MSP430_CPU (sd
)->state
.cio_buffer
+ 4, 8);
808 sim_core_write_buffer (sd
, MSP430_CPU (sd
), 0, buffer
,
809 MSP430_CPU (sd
)->state
.cio_buffer
+ 12, ret_buflen
);
812 #define SRC get_op (sd, opcode, 1)
813 #define DSRC get_op (sd, opcode, 0)
814 #define DEST(V) put_op (sd, opcode, 0, (V))
817 msp430_dis_read (bfd_vma memaddr
,
820 struct disassemble_info
*dinfo
)
822 SIM_DESC sd
= dinfo
->private_data
;
823 sim_core_read_buffer (sd
, MSP430_CPU (sd
), 0, myaddr
, memaddr
, length
);
827 #define DO_ALU(OP,SOP,MORE) \
831 int result = s1 OP s2 MORE; \
832 if (TRACE_ALU_P (MSP430_CPU (sd))) \
833 trace_generic (sd, MSP430_CPU (sd), TRACE_ALU_IDX, \
834 "ALU: %#x %s %#x %s = %#x", s1, SOP, s2, #MORE, result); \
838 #define SIGN (1 << (opcode->size - 1))
839 #define POS(x) (((x) & SIGN) ? 0 : 1)
840 #define NEG(x) (((x) & SIGN) ? 1 : 0)
842 #define SX(v) sign_ext (v, opcode->size)
843 #define ZX(v) zero_ext (v, opcode->size)
848 static char buf
[2][6];
854 bp
[0] = f
& MSP430_FLAG_V
? 'V' : '-';
855 bp
[1] = f
& MSP430_FLAG_N
? 'N' : '-';
856 bp
[2] = f
& MSP430_FLAG_Z
? 'Z' : '-';
857 bp
[3] = f
& MSP430_FLAG_C
? 'C' : '-';
862 /* Random number that won't show up in our usual logic. */
863 #define MAGIC_OVERFLOW 0x55000F
866 do_flags (SIM_DESC sd
,
867 MSP430_Opcode_Decoded
*opcode
,
868 int vnz_val
, /* Signed result. */
874 int signbit
= 1 << (opcode
->size
- 1);
876 f
&= ~opcode
->flags_0
;
877 f
&= ~opcode
->flags_set
;
878 f
|= opcode
->flags_1
;
880 if (vnz_val
& signbit
)
881 new_f
|= MSP430_FLAG_N
;
882 if (! (vnz_val
& ((signbit
<< 1) - 1)))
883 new_f
|= MSP430_FLAG_Z
;
884 if (overflow
== MAGIC_OVERFLOW
)
886 if (vnz_val
!= SX (vnz_val
))
887 new_f
|= MSP430_FLAG_V
;
891 new_f
|= MSP430_FLAG_V
;
893 new_f
|= MSP430_FLAG_C
;
895 new_f
= f
| (new_f
& opcode
->flags_set
);
896 if (TRACE_ALU_P (MSP430_CPU (sd
)))
899 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
900 "FLAGS: %s -> %s", flags2string (SR
),
901 flags2string (new_f
));
903 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
904 "FLAGS: %s", flags2string (new_f
));
909 #define FLAGS(vnz,c) do_flags (sd, opcode, vnz, c, MAGIC_OVERFLOW)
910 #define FLAGSV(vnz,c,v) do_flags (sd, opcode, vnz, c, v)
912 /* These two assume unsigned 16-bit (four digit) words.
913 Mask off unwanted bits for byte operations. */
916 bcd_to_binary (int v
)
918 int r
= ( ((v
>> 0) & 0xf) * 1
919 + ((v
>> 4) & 0xf) * 10
920 + ((v
>> 8) & 0xf) * 100
921 + ((v
>> 12) & 0xf) * 1000);
926 binary_to_bcd (int v
)
928 int r
= ( ((v
/ 1) % 10) << 0
929 | ((v
/ 10) % 10) << 4
930 | ((v
/ 100) % 10) << 8
931 | ((v
/ 1000) % 10) << 12);
936 syscall_read_mem (host_callback
*cb
, struct cb_syscall
*sc
,
937 unsigned long taddr
, char *buf
, int bytes
)
939 SIM_DESC sd
= (SIM_DESC
) sc
->p1
;
940 SIM_CPU
*cpu
= (SIM_CPU
*) sc
->p2
;
942 return sim_core_read_buffer (sd
, cpu
, read_map
, buf
, taddr
, bytes
);
946 syscall_write_mem (host_callback
*cb
, struct cb_syscall
*sc
,
947 unsigned long taddr
, const char *buf
, int bytes
)
949 SIM_DESC sd
= (SIM_DESC
) sc
->p1
;
950 SIM_CPU
*cpu
= (SIM_CPU
*) sc
->p2
;
952 return sim_core_write_buffer (sd
, cpu
, write_map
, buf
, taddr
, bytes
);
956 cond_string (int cond
)
981 /* Checks a CALL to address CALL_ADDR. If this is a special
982 syscall address then the call is simulated and non-zero is
983 returned. Otherwise 0 is returned. */
986 maybe_perform_syscall (SIM_DESC sd
, int call_addr
)
988 if (call_addr
== 0x00160)
992 for (i
= 0; i
< 16; i
++)
995 fprintf (stderr
, "\t");
996 fprintf (stderr
, "R%-2d %05x ", i
, MSP430_CPU (sd
)->state
.regs
[i
]);
999 int sp
= SP
+ (3 - (i
/ 4)) * 2;
1000 unsigned char buf
[2];
1002 sim_core_read_buffer (sd
, MSP430_CPU (sd
), read_map
, buf
, sp
, 2);
1004 fprintf (stderr
, "\tSP%+d: %04x", sp
- SP
,
1005 buf
[0] + buf
[1] * 256);
1011 fprintf (stderr
, flags
& 0x100 ? " V" : " -");
1012 fprintf (stderr
, flags
& 0x004 ? "N" : "-");
1013 fprintf (stderr
, flags
& 0x002 ? "Z" : "-");
1014 fprintf (stderr
, flags
& 0x001 ? "C" : "-");
1017 fprintf (stderr
, "\n");
1023 if ((call_addr
& ~0x3f) == 0x00180)
1026 int syscall_num
= call_addr
& 0x3f;
1027 host_callback
*cb
= STATE_CALLBACK (sd
);
1030 CB_SYSCALL_INIT (&sc
);
1032 sc
.func
= syscall_num
;
1033 sc
.arg1
= MSP430_CPU (sd
)->state
.regs
[12];
1034 sc
.arg2
= MSP430_CPU (sd
)->state
.regs
[13];
1035 sc
.arg3
= MSP430_CPU (sd
)->state
.regs
[14];
1036 sc
.arg4
= MSP430_CPU (sd
)->state
.regs
[15];
1038 if (TRACE_SYSCALL_P (MSP430_CPU (sd
)))
1040 const char *syscall_name
= "*unknown*";
1042 switch (syscall_num
)
1044 case TARGET_SYS_exit
:
1045 syscall_name
= "exit(%d)";
1047 case TARGET_SYS_open
:
1048 syscall_name
= "open(%#x,%#x)";
1050 case TARGET_SYS_close
:
1051 syscall_name
= "close(%d)";
1053 case TARGET_SYS_read
:
1054 syscall_name
= "read(%d,%#x,%d)";
1056 case TARGET_SYS_write
:
1057 syscall_name
= "write(%d,%#x,%d)";
1060 trace_generic (sd
, MSP430_CPU (sd
), TRACE_SYSCALL_IDX
,
1061 syscall_name
, sc
.arg1
, sc
.arg2
, sc
.arg3
, sc
.arg4
);
1064 /* Handle SYS_exit here. */
1065 if (syscall_num
== 1)
1067 sim_engine_halt (sd
, MSP430_CPU (sd
), NULL
,
1068 MSP430_CPU (sd
)->state
.regs
[0],
1069 sim_exited
, sc
.arg1
);
1074 sc
.p2
= MSP430_CPU (sd
);
1075 sc
.read_mem
= syscall_read_mem
;
1076 sc
.write_mem
= syscall_write_mem
;
1078 cb_syscall (cb
, &sc
);
1080 if (TRACE_SYSCALL_P (MSP430_CPU (sd
)))
1081 trace_generic (sd
, MSP430_CPU (sd
), TRACE_SYSCALL_IDX
,
1082 "returns %ld", sc
.result
);
1084 MSP430_CPU (sd
)->state
.regs
[12] = sc
.result
;
1092 msp430_step_once (SIM_DESC sd
)
1094 Get_Byte_Local_Data ld
;
1095 unsigned char buf
[100];
1098 unsigned int opcode_pc
;
1099 MSP430_Opcode_Decoded opcode_buf
;
1100 MSP430_Opcode_Decoded
*opcode
= &opcode_buf
;
1102 int u1
, u2
, uresult
;
1108 int op_bytes
, op_bits
;
1113 if (opcode_pc
< 0x10)
1115 fprintf (stderr
, "Fault: PC(%#x) is less than 0x10\n", opcode_pc
);
1116 sim_engine_halt (sd
, MSP430_CPU (sd
), NULL
,
1117 MSP430_CPU (sd
)->state
.regs
[0],
1122 if (PC
== MSP430_CPU (sd
)->state
.cio_breakpoint
1123 && STATE_OPEN_KIND (sd
) != SIM_OPEN_DEBUG
)
1128 opsize
= msp430_decode_opcode (MSP430_CPU (sd
)->state
.regs
[0],
1129 opcode
, msp430_getbyte
, &ld
);
1133 fprintf (stderr
, "Fault: undecodable opcode at %#x\n", opcode_pc
);
1134 sim_engine_halt (sd
, MSP430_CPU (sd
), NULL
,
1135 MSP430_CPU (sd
)->state
.regs
[0],
1140 if (opcode
->repeat_reg
)
1141 n_repeats
= (MSP430_CPU (sd
)->state
.regs
[opcode
->repeats
] & 0x000f) + 1;
1143 n_repeats
= opcode
->repeats
+ 1;
1145 op_bits
= opcode
->size
;
1160 if (TRACE_INSN_P (MSP430_CPU (sd
)))
1162 disassemble_info info
;
1163 unsigned char b
[10];
1165 msp430_trace_one (opcode_pc
);
1167 sim_core_read_buffer (sd
, MSP430_CPU (sd
), 0, b
, opcode_pc
, opsize
);
1169 init_disassemble_info (&info
, stderr
, (fprintf_ftype
) fprintf
);
1170 info
.private_data
= sd
;
1171 info
.read_memory_func
= msp430_dis_read
;
1172 fprintf (stderr
, "%#8x ", opcode_pc
);
1173 for (i
= 0; i
< opsize
; i
+= 2)
1174 fprintf (stderr
, " %02x%02x", b
[i
+1], b
[i
]);
1175 for (; i
< 6; i
+= 2)
1176 fprintf (stderr
, " ");
1177 fprintf (stderr
, " ");
1178 print_insn_msp430 (opcode_pc
, &info
);
1179 fprintf (stderr
, "\n");
1183 if (TRACE_ANY_P (MSP430_CPU (sd
)))
1184 trace_prefix (sd
, MSP430_CPU (sd
), NULL_CIA
, opcode_pc
,
1185 TRACE_LINENUM_P (MSP430_CPU (sd
)), NULL
, 0, "");
1193 /* Double-operand instructions. */
1195 if (opcode
->n_bytes
== 2
1196 && opcode
->op
[0].type
== MSP430_Operand_Register
1197 && opcode
->op
[0].reg
== MSR_CG
1198 && opcode
->op
[1].type
== MSP430_Operand_Immediate
1199 && opcode
->op
[1].addend
== 0
1200 /* A 16-bit write of #0 is a NOP; an 8-bit write is a BRK. */
1201 && opcode
->size
== 8)
1203 /* This is the designated software breakpoint instruction. */
1205 sim_engine_halt (sd
, MSP430_CPU (sd
), NULL
,
1206 MSP430_CPU (sd
)->state
.regs
[0],
1207 sim_stopped
, SIM_SIGTRAP
);
1212 /* Otherwise, do the move. */
1213 for (rept
= 0; rept
< n_repeats
; rept
++)
1221 for (rept
= 0; rept
< n_repeats
; rept
++)
1223 carry_to_use
= (SR
& MSP430_FLAG_C
) ? 1 : 0;
1228 uresult
= u1
+ u2
+ carry_to_use
;
1229 result
= s1
+ s2
+ carry_to_use
;
1230 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1231 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1232 "ADDC: %#x + %#x + %d = %#x",
1233 u1
, u2
, carry_to_use
, uresult
);
1235 FLAGS (result
, uresult
!= ZX (uresult
));
1240 for (rept
= 0; rept
< n_repeats
; rept
++)
1248 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1249 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1250 "ADD: %#x + %#x = %#x",
1253 FLAGS (result
, uresult
!= ZX (uresult
));
1258 for (rept
= 0; rept
< n_repeats
; rept
++)
1260 carry_to_use
= (SR
& MSP430_FLAG_C
) ? 1 : 0;
1265 uresult
= ZX (~u2
) + u1
+ carry_to_use
;
1266 result
= s1
- s2
+ (carry_to_use
- 1);
1267 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1268 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1269 "SUBC: %#x - %#x + %d = %#x",
1270 u1
, u2
, carry_to_use
, uresult
);
1272 FLAGS (result
, uresult
!= ZX (uresult
));
1277 for (rept
= 0; rept
< n_repeats
; rept
++)
1283 uresult
= ZX (~u2
) + u1
+ 1;
1284 result
= SX (uresult
);
1285 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1286 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1287 "SUB: %#x - %#x = %#x",
1290 FLAGS (result
, uresult
!= ZX (uresult
));
1295 for (rept
= 0; rept
< n_repeats
; rept
++)
1301 uresult
= ZX (~u2
) + u1
+ 1;
1303 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1304 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1305 "CMP: %#x - %#x = %x",
1307 FLAGS (result
, uresult
!= ZX (uresult
));
1312 for (rept
= 0; rept
< n_repeats
; rept
++)
1314 carry_to_use
= (SR
& MSP430_FLAG_C
) ? 1 : 0;
1317 uresult
= bcd_to_binary (u1
) + bcd_to_binary (u2
) + carry_to_use
;
1318 result
= binary_to_bcd (uresult
);
1319 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1320 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1321 "DADD: %#x + %#x + %d = %#x",
1322 u1
, u2
, carry_to_use
, result
);
1324 FLAGS (result
, uresult
> ((opcode
->size
== 8) ? 99 : 9999));
1329 for (rept
= 0; rept
< n_repeats
; rept
++)
1334 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1335 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1336 "AND: %#x & %#x = %#x",
1339 FLAGS (uresult
, uresult
!= 0);
1344 for (rept
= 0; rept
< n_repeats
; rept
++)
1349 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1350 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1351 "BIT: %#x & %#x -> %#x",
1353 FLAGS (uresult
, uresult
!= 0);
1358 for (rept
= 0; rept
< n_repeats
; rept
++)
1362 uresult
= u1
& ~ u2
;
1363 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1364 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1365 "BIC: %#x & ~ %#x = %#x",
1372 for (rept
= 0; rept
< n_repeats
; rept
++)
1377 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1378 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1379 "BIS: %#x | %#x = %#x",
1386 for (rept
= 0; rept
< n_repeats
; rept
++)
1388 s1
= 1 << (opcode
->size
- 1);
1392 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1393 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1394 "XOR: %#x & %#x = %#x",
1397 FLAGSV (uresult
, uresult
!= 0, (u1
& s1
) && (u2
& s1
));
1401 /* Single-operand instructions. Note: the decoder puts the same
1402 operand in SRC as in DEST, for our convenience. */
1405 for (rept
= 0; rept
< n_repeats
; rept
++)
1408 carry_to_use
= u1
& 1;
1410 if (SR
& MSP430_FLAG_C
)
1411 uresult
|= (1 << (opcode
->size
- 1));
1412 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1413 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1417 FLAGS (uresult
, carry_to_use
);
1422 for (rept
= 0; rept
< n_repeats
; rept
++)
1425 uresult
= ((u1
>> 8) & 0x00ff) | ((u1
<< 8) & 0xff00);
1426 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1427 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1435 for (rept
= 0; rept
< n_repeats
; rept
++)
1439 s1
= 1 << (opcode
->size
- 1);
1440 uresult
= (u1
>> 1) | (u1
& s1
);
1441 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1442 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1451 for (rept
= 0; rept
< n_repeats
; rept
++)
1455 uresult
= (u1
>> 1);
1456 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1457 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1466 for (rept
= 0; rept
< n_repeats
; rept
++)
1470 uresult
= u1
| 0xfff00;
1472 uresult
= u1
& 0x000ff;
1473 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1474 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1483 for (rept
= 0; rept
< n_repeats
; rept
++)
1487 new_sp
= REG_GET (MSR_SP
) - op_bytes
;
1488 /* SP is always word-aligned. */
1491 REG_PUT (MSR_SP
, new_sp
);
1493 mem_put_val (sd
, SP
, u1
, op_bits
);
1494 if (opcode
->op
[1].type
== MSP430_Operand_Register
)
1495 opcode
->op
[1].reg
--;
1500 for (rept
= 0; rept
< n_repeats
; rept
++)
1504 u1
= mem_get_val (sd
, SP
, op_bits
);
1506 if (opcode
->op
[0].type
== MSP430_Operand_Register
)
1507 opcode
->op
[0].reg
++;
1508 new_sp
= REG_GET (MSR_SP
) + op_bytes
;
1509 /* SP is always word-aligned. */
1512 REG_PUT (MSR_SP
, new_sp
);
1519 if (maybe_perform_syscall (sd
, u1
))
1522 REG_PUT (MSR_SP
, REG_GET (MSR_SP
) - op_bytes
);
1523 mem_put_val (sd
, SP
, PC
, op_bits
);
1524 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1525 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1526 "CALL: func %#x ret %#x, sp %#x",
1528 REG_PUT (MSR_PC
, u1
);
1532 u1
= mem_get_val (sd
, SP
, 16);
1535 PC
= mem_get_val (sd
, SP
, 16);
1537 /* Emulate the RETI action of the 20-bit CPUX architecure.
1538 This is safe for 16-bit CPU architectures as well, since the top
1539 8-bits of SR will have been written to the stack here, and will
1540 have been read as 0. */
1541 PC
|= (u1
& 0xF000) << 4;
1542 if (TRACE_ALU_P (MSP430_CPU (sd
)))
1543 trace_generic (sd
, MSP430_CPU (sd
), TRACE_ALU_IDX
,
1544 "RETI: pc %#x sr %#x",
1552 switch (opcode
->cond
)
1555 u1
= (SR
& MSP430_FLAG_Z
) ? 0 : 1;
1558 u1
= (SR
& MSP430_FLAG_Z
) ? 1 : 0;
1561 u1
= (SR
& MSP430_FLAG_C
) ? 0 : 1;
1564 u1
= (SR
& MSP430_FLAG_C
) ? 1 : 0;
1567 u1
= (SR
& MSP430_FLAG_N
) ? 1 : 0;
1570 u1
= (!!(SR
& MSP430_FLAG_N
) == !!(SR
& MSP430_FLAG_V
)) ? 1 : 0;
1573 u1
= (!!(SR
& MSP430_FLAG_N
) == !!(SR
& MSP430_FLAG_V
)) ? 0 : 1;
1582 if (TRACE_BRANCH_P (MSP430_CPU (sd
)))
1583 trace_generic (sd
, MSP430_CPU (sd
), TRACE_BRANCH_IDX
,
1584 "J%s: pc %#x -> %#x sr %#x, taken",
1585 cond_string (opcode
->cond
), PC
, i
, SR
);
1587 if (PC
== opcode_pc
)
1591 if (TRACE_BRANCH_P (MSP430_CPU (sd
)))
1592 trace_generic (sd
, MSP430_CPU (sd
), TRACE_BRANCH_IDX
,
1593 "J%s: pc %#x to %#x sr %#x, not taken",
1594 cond_string (opcode
->cond
), PC
, i
, SR
);
1598 fprintf (stderr
, "error: unexpected opcode id %d\n", opcode
->id
);
1604 sim_engine_run (SIM_DESC sd
,
1611 msp430_step_once (sd
);
1612 if (sim_events_tick (sd
))
1613 sim_events_process (sd
);