]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/msp430/msp430-sim.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / sim / msp430 / msp430-sim.c
1 /* Simulator for TI MSP430 and MSP430X
2
3 Copyright (C) 2013-2024 Free Software Foundation, Inc.
4 Contributed by Red Hat.
5 Based on sim/bfin/bfin-sim.c which was contributed by Analog Devices, Inc.
6
7 This file is part of simulators.
8
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.
13
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.
18
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/>. */
21
22 /* This must come before any other includes. */
23 #include "defs.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include "opcode/msp430-decode.h"
32 #include "sim-main.h"
33 #include "sim-options.h"
34 #include "sim-signal.h"
35 #include "sim-syscall.h"
36 #include "msp430-sim.h"
37
38 static sim_cia
39 msp430_pc_fetch (SIM_CPU *cpu)
40 {
41 struct msp430_cpu_state *msp430_cpu = MSP430_SIM_CPU (cpu);
42
43 return msp430_cpu->regs[0];
44 }
45
46 static void
47 msp430_pc_store (SIM_CPU *cpu, sim_cia newpc)
48 {
49 struct msp430_cpu_state *msp430_cpu = MSP430_SIM_CPU (cpu);
50
51 msp430_cpu->regs[0] = newpc;
52 }
53
54 static int
55 msp430_reg_fetch (SIM_CPU *cpu, int regno, void *buf, int len)
56 {
57 struct msp430_cpu_state *msp430_cpu = MSP430_SIM_CPU (cpu);
58 unsigned char *memory = buf;
59
60 if (0 <= regno && regno < 16)
61 {
62 if (len == 2)
63 {
64 int val = msp430_cpu->regs[regno];
65 memory[0] = val & 0xff;
66 memory[1] = (val >> 8) & 0xff;
67 return 0;
68 }
69 else if (len == 4)
70 {
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. */
75 memory[3] = 0;
76 return 0;
77 }
78 else
79 return -1;
80 }
81 else
82 return -1;
83 }
84
85 static int
86 msp430_reg_store (SIM_CPU *cpu, int regno, const void *buf, int len)
87 {
88 struct msp430_cpu_state *msp430_cpu = MSP430_SIM_CPU (cpu);
89 const unsigned char *memory = buf;
90
91 if (0 <= regno && regno < 16)
92 {
93 if (len == 2)
94 {
95 msp430_cpu->regs[regno] = (memory[1] << 8) | memory[0];
96 return len;
97 }
98
99 if (len == 4)
100 {
101 msp430_cpu->regs[regno] = ((memory[2] << 16) & 0xf0000)
102 | (memory[1] << 8) | memory[0];
103 return len;
104 }
105 }
106
107 return -1;
108 }
109
110 SIM_DESC
111 sim_open (SIM_OPEN_KIND kind,
112 struct host_callback_struct *callback,
113 struct bfd *abfd,
114 char * const *argv)
115 {
116 SIM_DESC sd = sim_state_alloc (kind, callback);
117 struct msp430_cpu_state *msp430_cpu;
118 char c;
119 int i;
120
121 /* Initialise the simulator. */
122
123 /* Set default options before parsing user options. */
124 current_target_byte_order = BFD_ENDIAN_LITTLE;
125
126 if (sim_cpu_alloc_all_extra (sd, 0, sizeof (struct msp430_cpu_state))
127 != SIM_RC_OK)
128 {
129 sim_state_free (sd);
130 return 0;
131 }
132
133 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
134 {
135 sim_state_free (sd);
136 return 0;
137 }
138
139 if (sim_parse_args (sd, argv) != SIM_RC_OK)
140 {
141 sim_state_free (sd);
142 return 0;
143 }
144
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. */
157
158 /* Check for/establish the a reference program image. */
159 if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
160 {
161 sim_state_free (sd);
162 return 0;
163 }
164
165 /* Establish any remaining configuration options. */
166 if (sim_config (sd) != SIM_RC_OK)
167 {
168 sim_state_free (sd);
169 return 0;
170 }
171
172 if (sim_post_argv_init (sd) != SIM_RC_OK)
173 {
174 sim_state_free (sd);
175 return 0;
176 }
177
178 /* CPU specific initialization. */
179 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
180 {
181 SIM_CPU *cpu = STATE_CPU (sd, i);
182
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;
187
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_");
193 }
194
195 return sd;
196 }
197
198 SIM_RC
199 sim_create_inferior (SIM_DESC sd,
200 struct bfd *abfd,
201 char * const *argv,
202 char * const *env)
203 {
204 unsigned char resetv[2];
205 int new_pc;
206
207 /* Set the PC to the default reset vector if available. */
208 sim_core_read_buffer (sd, STATE_CPU (sd, 0), read_map, resetv, 0xfffe, 2);
209 new_pc = resetv[0] + 256 * resetv[1];
210
211 /* If the reset vector isn't initialized, then use the ELF entry. */
212 if (abfd != NULL && !new_pc)
213 new_pc = bfd_get_start_address (abfd);
214
215 sim_pc_set (STATE_CPU (sd, 0), new_pc);
216 msp430_pc_store (STATE_CPU (sd, 0), new_pc);
217
218 return SIM_RC_OK;
219 }
220
221 typedef struct
222 {
223 SIM_DESC sd;
224 int gb_addr;
225 } Get_Byte_Local_Data;
226
227 static int
228 msp430_getbyte (void *vld)
229 {
230 Get_Byte_Local_Data *ld = (Get_Byte_Local_Data *)vld;
231 char buf[1];
232 SIM_DESC sd = ld->sd;
233
234 sim_core_read_buffer (sd, STATE_CPU (sd, 0), read_map, buf, ld->gb_addr, 1);
235 ld->gb_addr ++;
236 return buf[0];
237 }
238
239 #define REG(N) MSP430_SIM_CPU (STATE_CPU (sd, 0))->regs[N]
240 #define PC REG(MSR_PC)
241 #define SP REG(MSR_SP)
242 #define SR REG(MSR_SR)
243
244 static const char *
245 register_names[] =
246 {
247 "PC", "SP", "SR", "CG", "R4", "R5", "R6", "R7", "R8",
248 "R9", "R10", "R11", "R12", "R13", "R14", "R15"
249 };
250
251 static void
252 trace_reg_put (SIM_DESC sd, int n, unsigned int v)
253 {
254 TRACE_REGISTER (STATE_CPU (sd, 0), "PUT: %#x -> %s", v, register_names[n]);
255 REG (n) = v;
256 }
257
258 static unsigned int
259 trace_reg_get (SIM_DESC sd, int n)
260 {
261 TRACE_REGISTER (STATE_CPU (sd, 0), "GET: %s -> %#x", register_names[n], REG (n));
262 return REG (n);
263 }
264
265 #define REG_PUT(N,V) trace_reg_put (sd, N, V)
266 #define REG_GET(N) trace_reg_get (sd, N)
267
268 /* Hardware multiply (and accumulate) support. */
269
270 static unsigned int
271 zero_ext (unsigned int v, unsigned int bits)
272 {
273 v &= ((1 << bits) - 1);
274 return v;
275 }
276
277 static signed long long
278 sign_ext (signed long long v, unsigned int bits)
279 {
280 signed long long sb = 1LL << (bits-1); /* Sign bit. */
281 signed long long mb = (1LL << (bits-1)) - 1LL; /* Mantissa bits. */
282
283 if (v & sb)
284 v = v | ~mb;
285 else
286 v = v & mb;
287 return v;
288 }
289
290 static int
291 get_op (SIM_DESC sd, MSP430_Opcode_Decoded *opc, int n)
292 {
293 MSP430_Opcode_Operand *op = opc->op + n;
294 int rv = 0;
295 int addr;
296 unsigned char buf[4];
297 int incval = 0;
298
299 switch (op->type)
300 {
301 case MSP430_Operand_Immediate:
302 rv = op->addend;
303 break;
304 case MSP430_Operand_Register:
305 rv = REG_GET (op->reg);
306 break;
307 case MSP430_Operand_Indirect:
308 case MSP430_Operand_Indirect_Postinc:
309 addr = op->addend;
310 if (op->reg != MSR_None)
311 {
312 int reg = REG_GET (op->reg);
313 int sign = opc->ofs_430x ? 20 : 16;
314
315 /* Index values are signed. */
316 if (addr & (1 << (sign - 1)))
317 addr |= -(1 << sign);
318
319 addr += reg;
320
321 /* For MSP430 instructions the sum is limited to 16 bits if the
322 address in the index register is less than 64k even if we are
323 running on an MSP430X CPU. This is for MSP430 compatibility. */
324 if (reg < 0x10000 && ! opc->ofs_430x)
325 {
326 if (addr >= 0x10000)
327 fprintf (stderr, " XXX WRAPPING ADDRESS %x on read\n", addr);
328
329 addr &= 0xffff;
330 }
331 }
332 addr &= 0xfffff;
333 switch (opc->size)
334 {
335 case 8:
336 sim_core_read_buffer (sd, STATE_CPU (sd, 0), read_map, buf, addr, 1);
337 rv = buf[0];
338 break;
339 case 16:
340 sim_core_read_buffer (sd, STATE_CPU (sd, 0), read_map, buf, addr, 2);
341 rv = buf[0] | (buf[1] << 8);
342 break;
343 case 20:
344 case 32:
345 sim_core_read_buffer (sd, STATE_CPU (sd, 0), read_map, buf, addr, 4);
346 rv = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
347 break;
348 default:
349 assert (! opc->size);
350 break;
351 }
352 #if 0
353 /* Hack - MSP430X5438 serial port status register. */
354 if (addr == 0x5dd)
355 rv = 2;
356 #endif
357 if ((addr >= 0x130 && addr <= 0x15B)
358 || (addr >= 0x4C0 && addr <= 0x4EB))
359 {
360 switch (addr)
361 {
362 case 0x4CA:
363 case 0x13A:
364 switch (HWMULT (sd, hwmult_type))
365 {
366 case UNSIGN_MAC_32:
367 case UNSIGN_32:
368 rv = zero_ext (HWMULT (sd, hwmult_result), 16);
369 break;
370 case SIGN_MAC_32:
371 case SIGN_32:
372 rv = sign_ext (HWMULT (sd, hwmult_signed_result), 16);
373 break;
374 }
375 break;
376
377 case 0x4CC:
378 case 0x13C:
379 switch (HWMULT (sd, hwmult_type))
380 {
381 case UNSIGN_MAC_32:
382 case UNSIGN_32:
383 rv = zero_ext (HWMULT (sd, hwmult_result) >> 16, 16);
384 break;
385
386 case SIGN_MAC_32:
387 case SIGN_32:
388 rv = sign_ext (HWMULT (sd, hwmult_signed_result) >> 16, 16);
389 break;
390 }
391 break;
392
393 case 0x4CE:
394 case 0x13E:
395 switch (HWMULT (sd, hwmult_type))
396 {
397 case UNSIGN_32:
398 rv = 0;
399 break;
400 case SIGN_32:
401 rv = HWMULT (sd, hwmult_signed_result) < 0 ? -1 : 0;
402 break;
403 case UNSIGN_MAC_32:
404 rv = 0; /* FIXME: Should be carry of last accumulate. */
405 break;
406 case SIGN_MAC_32:
407 rv = HWMULT (sd, hwmult_signed_accumulator) < 0 ? -1 : 0;
408 break;
409 }
410 break;
411
412 case 0x4E4:
413 case 0x154:
414 rv = zero_ext (HWMULT (sd, hw32mult_result), 16);
415 break;
416
417 case 0x4E6:
418 case 0x156:
419 rv = zero_ext (HWMULT (sd, hw32mult_result) >> 16, 16);
420 break;
421
422 case 0x4E8:
423 case 0x158:
424 rv = zero_ext (HWMULT (sd, hw32mult_result) >> 32, 16);
425 break;
426
427 case 0x4EA:
428 case 0x15A:
429 switch (HWMULT (sd, hw32mult_type))
430 {
431 case UNSIGN_64: rv = zero_ext (HWMULT (sd, hw32mult_result) >> 48, 16); break;
432 case SIGN_64: rv = sign_ext (HWMULT (sd, hw32mult_result) >> 48, 16); break;
433 }
434 break;
435
436 default:
437 fprintf (stderr, "unimplemented HW MULT read from %x!\n", addr);
438 break;
439 }
440 }
441
442 TRACE_MEMORY (STATE_CPU (sd, 0), "GET: [%#x].%d -> %#x", addr, opc->size,
443 rv);
444 break;
445
446 default:
447 fprintf (stderr, "invalid operand %d type %d\n", n, op->type);
448 abort ();
449 }
450
451 switch (opc->size)
452 {
453 case 8:
454 rv &= 0xff;
455 incval = 1;
456 break;
457 case 16:
458 rv &= 0xffff;
459 incval = 2;
460 break;
461 case 20:
462 rv &= 0xfffff;
463 incval = 4;
464 break;
465 case 32:
466 rv &= 0xffffffff;
467 incval = 4;
468 break;
469 }
470
471 if (op->type == MSP430_Operand_Indirect_Postinc)
472 REG_PUT (op->reg, REG_GET (op->reg) + incval);
473
474 return rv;
475 }
476
477 static int
478 put_op (SIM_DESC sd, MSP430_Opcode_Decoded *opc, int n, int val)
479 {
480 MSP430_Opcode_Operand *op = opc->op + n;
481 int rv = 0;
482 int addr;
483 unsigned char buf[4];
484 int incval = 0;
485
486 switch (opc->size)
487 {
488 case 8:
489 val &= 0xff;
490 break;
491 case 16:
492 val &= 0xffff;
493 break;
494 case 20:
495 val &= 0xfffff;
496 break;
497 case 32:
498 val &= 0xffffffff;
499 break;
500 }
501
502 switch (op->type)
503 {
504 case MSP430_Operand_Register:
505 REG (op->reg) = val;
506 REG_PUT (op->reg, val);
507 break;
508 case MSP430_Operand_Indirect:
509 case MSP430_Operand_Indirect_Postinc:
510 addr = op->addend;
511 if (op->reg != MSR_None)
512 {
513 int reg = REG_GET (op->reg);
514 int sign = opc->ofs_430x ? 20 : 16;
515
516 /* Index values are signed. */
517 if (addr & (1 << (sign - 1)))
518 addr |= -(1 << sign);
519
520 addr += reg;
521
522 /* For MSP430 instructions the sum is limited to 16 bits if the
523 address in the index register is less than 64k even if we are
524 running on an MSP430X CPU. This is for MSP430 compatibility. */
525 if (reg < 0x10000 && ! opc->ofs_430x)
526 {
527 if (addr >= 0x10000)
528 fprintf (stderr, " XXX WRAPPING ADDRESS %x on write\n", addr);
529
530 addr &= 0xffff;
531 }
532 }
533 addr &= 0xfffff;
534
535 TRACE_MEMORY (STATE_CPU (sd, 0), "PUT: [%#x].%d <- %#x", addr, opc->size,
536 val);
537 #if 0
538 /* Hack - MSP430X5438 serial port transmit register. */
539 if (addr == 0x5ce)
540 putchar (val);
541 #endif
542 if ((addr >= 0x130 && addr <= 0x15B)
543 || (addr >= 0x4C0 && addr <= 0x4EB))
544 {
545 signed int a,b;
546
547 /* Hardware Multiply emulation. */
548 assert (opc->size == 16);
549
550 switch (addr)
551 {
552 case 0x4C0:
553 case 0x130:
554 HWMULT (sd, hwmult_op1) = val;
555 HWMULT (sd, hwmult_type) = UNSIGN_32;
556 break;
557
558 case 0x4C2:
559 case 0x132:
560 HWMULT (sd, hwmult_op1) = val;
561 HWMULT (sd, hwmult_type) = SIGN_32;
562 break;
563
564 case 0x4C4:
565 case 0x134:
566 HWMULT (sd, hwmult_op1) = val;
567 HWMULT (sd, hwmult_type) = UNSIGN_MAC_32;
568 break;
569
570 case 0x4C6:
571 case 0x136:
572 HWMULT (sd, hwmult_op1) = val;
573 HWMULT (sd, hwmult_type) = SIGN_MAC_32;
574 break;
575
576 case 0x4C8:
577 case 0x138:
578 HWMULT (sd, hwmult_op2) = val;
579 switch (HWMULT (sd, hwmult_type))
580 {
581 case UNSIGN_32:
582 a = HWMULT (sd, hwmult_op1);
583 b = HWMULT (sd, hwmult_op2);
584 /* For unsigned 32-bit multiplication of 16-bit operands, an
585 explicit cast is required to prevent any implicit
586 sign-extension. */
587 HWMULT (sd, hwmult_result) = (uint32_t) a * (uint32_t) b;
588 HWMULT (sd, hwmult_signed_result) = a * b;
589 HWMULT (sd, hwmult_accumulator) = HWMULT (sd, hwmult_signed_accumulator) = 0;
590 break;
591
592 case SIGN_32:
593 a = sign_ext (HWMULT (sd, hwmult_op1), 16);
594 b = sign_ext (HWMULT (sd, hwmult_op2), 16);
595 HWMULT (sd, hwmult_signed_result) = a * b;
596 HWMULT (sd, hwmult_result) = (uint32_t) a * (uint32_t) b;
597 HWMULT (sd, hwmult_accumulator) = HWMULT (sd, hwmult_signed_accumulator) = 0;
598 break;
599
600 case UNSIGN_MAC_32:
601 a = HWMULT (sd, hwmult_op1);
602 b = HWMULT (sd, hwmult_op2);
603 HWMULT (sd, hwmult_accumulator)
604 += (uint32_t) a * (uint32_t) b;
605 HWMULT (sd, hwmult_signed_accumulator) += a * b;
606 HWMULT (sd, hwmult_result) = HWMULT (sd, hwmult_accumulator);
607 HWMULT (sd, hwmult_signed_result) = HWMULT (sd, hwmult_signed_accumulator);
608 break;
609
610 case SIGN_MAC_32:
611 a = sign_ext (HWMULT (sd, hwmult_op1), 16);
612 b = sign_ext (HWMULT (sd, hwmult_op2), 16);
613 HWMULT (sd, hwmult_accumulator)
614 += (uint32_t) a * (uint32_t) b;
615 HWMULT (sd, hwmult_signed_accumulator) += a * b;
616 HWMULT (sd, hwmult_result) = HWMULT (sd, hwmult_accumulator);
617 HWMULT (sd, hwmult_signed_result) = HWMULT (sd, hwmult_signed_accumulator);
618 break;
619 }
620 break;
621
622 case 0x4CA:
623 case 0x13A:
624 /* Copy into LOW result... */
625 switch (HWMULT (sd, hwmult_type))
626 {
627 case UNSIGN_MAC_32:
628 case UNSIGN_32:
629 HWMULT (sd, hwmult_accumulator) = HWMULT (sd, hwmult_result) = zero_ext (val, 16);
630 HWMULT (sd, hwmult_signed_accumulator) = sign_ext (val, 16);
631 break;
632 case SIGN_MAC_32:
633 case SIGN_32:
634 HWMULT (sd, hwmult_signed_accumulator) = HWMULT (sd, hwmult_result) = sign_ext (val, 16);
635 HWMULT (sd, hwmult_accumulator) = zero_ext (val, 16);
636 break;
637 }
638 break;
639
640 case 0x4D0:
641 case 0x140:
642 HWMULT (sd, hw32mult_op1) = val;
643 HWMULT (sd, hw32mult_type) = UNSIGN_64;
644 break;
645
646 case 0x4D2:
647 case 0x142:
648 HWMULT (sd, hw32mult_op1) = (HWMULT (sd, hw32mult_op1) & 0xFFFF) | (val << 16);
649 break;
650
651 case 0x4D4:
652 case 0x144:
653 HWMULT (sd, hw32mult_op1) = val;
654 HWMULT (sd, hw32mult_type) = SIGN_64;
655 break;
656
657 case 0x4D6:
658 case 0x146:
659 HWMULT (sd, hw32mult_op1) = (HWMULT (sd, hw32mult_op1) & 0xFFFF) | (val << 16);
660 break;
661
662 case 0x4E0:
663 case 0x150:
664 HWMULT (sd, hw32mult_op2) = val;
665 break;
666
667 case 0x4E2:
668 case 0x152:
669 HWMULT (sd, hw32mult_op2) = (HWMULT (sd, hw32mult_op2) & 0xFFFF) | (val << 16);
670 switch (HWMULT (sd, hw32mult_type))
671 {
672 case UNSIGN_64:
673 HWMULT (sd, hw32mult_result)
674 = (uint64_t) HWMULT (sd, hw32mult_op1)
675 * (uint64_t) HWMULT (sd, hw32mult_op2);
676 break;
677 case SIGN_64:
678 HWMULT (sd, hw32mult_result)
679 = sign_ext (HWMULT (sd, hw32mult_op1), 32)
680 * sign_ext (HWMULT (sd, hw32mult_op2), 32);
681 break;
682 }
683 break;
684
685 default:
686 fprintf (stderr, "unimplemented HW MULT write to %x!\n", addr);
687 break;
688 }
689 }
690
691 switch (opc->size)
692 {
693 case 8:
694 buf[0] = val;
695 sim_core_write_buffer (sd, STATE_CPU (sd, 0), write_map, buf, addr, 1);
696 break;
697 case 16:
698 buf[0] = val;
699 buf[1] = val >> 8;
700 sim_core_write_buffer (sd, STATE_CPU (sd, 0), write_map, buf, addr, 2);
701 break;
702 case 20:
703 case 32:
704 buf[0] = val;
705 buf[1] = val >> 8;
706 buf[2] = val >> 16;
707 buf[3] = val >> 24;
708 sim_core_write_buffer (sd, STATE_CPU (sd, 0), write_map, buf, addr, 4);
709 break;
710 default:
711 assert (! opc->size);
712 break;
713 }
714 break;
715 default:
716 fprintf (stderr, "invalid operand %d type %d\n", n, op->type);
717 abort ();
718 }
719
720 switch (opc->size)
721 {
722 case 8:
723 rv &= 0xff;
724 incval = 1;
725 break;
726 case 16:
727 rv &= 0xffff;
728 incval = 2;
729 break;
730 case 20:
731 rv &= 0xfffff;
732 incval = 4;
733 break;
734 case 32:
735 rv &= 0xffffffff;
736 incval = 4;
737 break;
738 }
739
740 if (op->type == MSP430_Operand_Indirect_Postinc)
741 {
742 int new_val = REG_GET (op->reg) + incval;
743 /* SP is always word-aligned. */
744 if (op->reg == MSR_SP && (new_val & 1))
745 new_val ++;
746 REG_PUT (op->reg, new_val);
747 }
748
749 return rv;
750 }
751
752 static void
753 mem_put_val (SIM_DESC sd, int addr, int val, int bits)
754 {
755 MSP430_Opcode_Decoded opc;
756
757 opc.size = bits;
758 opc.op[0].type = MSP430_Operand_Indirect;
759 opc.op[0].addend = addr;
760 opc.op[0].reg = MSR_None;
761 put_op (sd, &opc, 0, val);
762 }
763
764 static int
765 mem_get_val (SIM_DESC sd, int addr, int bits)
766 {
767 MSP430_Opcode_Decoded opc;
768
769 opc.size = bits;
770 opc.op[0].type = MSP430_Operand_Indirect;
771 opc.op[0].addend = addr;
772 opc.op[0].reg = MSR_None;
773 return get_op (sd, &opc, 0);
774 }
775
776 #define CIO_OPEN (0xF0)
777 #define CIO_CLOSE (0xF1)
778 #define CIO_READ (0xF2)
779 #define CIO_WRITE (0xF3)
780 #define CIO_LSEEK (0xF4)
781 #define CIO_UNLINK (0xF5)
782 #define CIO_GETENV (0xF6)
783 #define CIO_RENAME (0xF7)
784 #define CIO_GETTIME (0xF8)
785 #define CIO_GETCLK (0xF9)
786 #define CIO_SYNC (0xFF)
787
788 #define CIO_I(n) (parms[(n)] + parms[(n)+1] * 256)
789 #define CIO_L(n) (parms[(n)] + parms[(n)+1] * 256 \
790 + parms[(n)+2] * 65536 + parms[(n)+3] * 16777216)
791
792 static void
793 msp430_cio (SIM_DESC sd)
794 {
795 /* A block of data at __CIOBUF__ describes the I/O operation to
796 perform. */
797 sim_cpu *cpu = STATE_CPU (sd, 0);
798 struct msp430_cpu_state *msp430_cpu = MSP430_SIM_CPU (cpu);
799 unsigned char parms[8];
800 long length;
801 int command;
802 unsigned char buffer[512];
803 long ret_buflen = 0;
804 long fd, len, rv;
805
806 sim_core_read_buffer (sd, cpu, 0, parms, msp430_cpu->cio_buffer, 5);
807 length = CIO_I (0);
808 command = parms[2];
809
810 sim_core_read_buffer (sd, cpu, 0, parms, msp430_cpu->cio_buffer + 3, 8);
811 sim_core_read_buffer (sd, cpu, 0, buffer, msp430_cpu->cio_buffer + 11, length);
812
813 switch (command)
814 {
815 case CIO_WRITE:
816 fd = CIO_I (0);
817 len = CIO_I (2);
818
819 rv = write (fd, buffer, len);
820 parms[0] = rv & 0xff;
821 parms[1] = rv >> 8;
822
823 break;
824 }
825
826 sim_core_write_buffer (sd, cpu, 0, parms, msp430_cpu->cio_buffer + 4, 8);
827 if (ret_buflen)
828 sim_core_write_buffer (sd, cpu, 0, buffer, msp430_cpu->cio_buffer + 12,
829 ret_buflen);
830 }
831
832 #define SRC get_op (sd, opcode, 1)
833 #define DSRC get_op (sd, opcode, 0)
834 #define DEST(V) put_op (sd, opcode, 0, (V))
835
836 #define DO_ALU(OP,SOP,MORE) \
837 { \
838 int s1 = DSRC; \
839 int s2 = SRC; \
840 int result = s1 OP s2 MORE; \
841 TRACE_ALU (STATE_CPU (sd, 0), "ALU: %#x %s %#x %s = %#x", s1, SOP, \
842 s2, #MORE, result); \
843 DEST (result); \
844 }
845
846 #define SIGN (1 << (opcode->size - 1))
847 #define POS(x) (((x) & SIGN) ? 0 : 1)
848 #define NEG(x) (((x) & SIGN) ? 1 : 0)
849
850 #define SX(v) sign_ext (v, opcode->size)
851 #define ZX(v) zero_ext (v, opcode->size)
852
853 static char *
854 flags2string (int f)
855 {
856 static char buf[2][6];
857 static int bi = 0;
858 char *bp = buf[bi];
859
860 bi = (bi + 1) % 2;
861
862 bp[0] = f & MSP430_FLAG_V ? 'V' : '-';
863 bp[1] = f & MSP430_FLAG_N ? 'N' : '-';
864 bp[2] = f & MSP430_FLAG_Z ? 'Z' : '-';
865 bp[3] = f & MSP430_FLAG_C ? 'C' : '-';
866 bp[4] = 0;
867 return bp;
868 }
869
870 /* Random number that won't show up in our usual logic. */
871 #define MAGIC_OVERFLOW 0x55000F
872
873 static void
874 do_flags (SIM_DESC sd,
875 MSP430_Opcode_Decoded *opcode,
876 int vnz_val, /* Signed result. */
877 int carry,
878 int overflow)
879 {
880 int f = SR;
881 int new_f = 0;
882 int signbit = 1 << (opcode->size - 1);
883
884 f &= ~opcode->flags_0;
885 f &= ~opcode->flags_set;
886 f |= opcode->flags_1;
887
888 if (vnz_val & signbit)
889 new_f |= MSP430_FLAG_N;
890 if (! (vnz_val & ((signbit << 1) - 1)))
891 new_f |= MSP430_FLAG_Z;
892 if (overflow == MAGIC_OVERFLOW)
893 {
894 if (vnz_val != SX (vnz_val))
895 new_f |= MSP430_FLAG_V;
896 }
897 else
898 if (overflow)
899 new_f |= MSP430_FLAG_V;
900 if (carry)
901 new_f |= MSP430_FLAG_C;
902
903 new_f = f | (new_f & opcode->flags_set);
904 if (SR != new_f)
905 TRACE_ALU (STATE_CPU (sd, 0), "FLAGS: %s -> %s", flags2string (SR),
906 flags2string (new_f));
907 else
908 TRACE_ALU (STATE_CPU (sd, 0), "FLAGS: %s", flags2string (new_f));
909 SR = new_f;
910 }
911
912 #define FLAGS(vnz,c) do_flags (sd, opcode, vnz, c, MAGIC_OVERFLOW)
913 #define FLAGSV(vnz,c,v) do_flags (sd, opcode, vnz, c, v)
914
915 /* These two assume unsigned 16-bit (four digit) words.
916 Mask off unwanted bits for byte operations. */
917
918 static int
919 bcd_to_binary (int v)
920 {
921 int r = ( ((v >> 0) & 0xf) * 1
922 + ((v >> 4) & 0xf) * 10
923 + ((v >> 8) & 0xf) * 100
924 + ((v >> 12) & 0xf) * 1000);
925 return r;
926 }
927
928 static int
929 binary_to_bcd (int v)
930 {
931 int r = ( ((v / 1) % 10) << 0
932 | ((v / 10) % 10) << 4
933 | ((v / 100) % 10) << 8
934 | ((v / 1000) % 10) << 12);
935 return r;
936 }
937
938 static const char *
939 cond_string (int cond)
940 {
941 switch (cond)
942 {
943 case MSC_nz:
944 return "NZ";
945 case MSC_z:
946 return "Z";
947 case MSC_nc:
948 return "NC";
949 case MSC_c:
950 return "C";
951 case MSC_n:
952 return "N";
953 case MSC_ge:
954 return "GE";
955 case MSC_l:
956 return "L";
957 case MSC_true:
958 return "MP";
959 default:
960 return "??";
961 }
962 }
963
964 /* Checks a CALL to address CALL_ADDR. If this is a special
965 syscall address then the call is simulated and non-zero is
966 returned. Otherwise 0 is returned. */
967
968 static int
969 maybe_perform_syscall (SIM_DESC sd, int call_addr)
970 {
971 sim_cpu *cpu = STATE_CPU (sd, 0);
972 struct msp430_cpu_state *msp430_cpu = MSP430_SIM_CPU (cpu);
973
974 if (call_addr == 0x00160)
975 {
976 int i;
977
978 for (i = 0; i < 16; i++)
979 {
980 if (i % 4 == 0)
981 fprintf (stderr, "\t");
982 fprintf (stderr, "R%-2d %05x ", i, msp430_cpu->regs[i]);
983 if (i % 4 == 3)
984 {
985 int sp = SP + (3 - (i / 4)) * 2;
986 unsigned char buf[2];
987
988 sim_core_read_buffer (sd, cpu, read_map, buf, sp, 2);
989
990 fprintf (stderr, "\tSP%+d: %04x", sp - SP,
991 buf[0] + buf[1] * 256);
992
993 if (i / 4 == 0)
994 {
995 int flags = SR;
996
997 fprintf (stderr, flags & 0x100 ? " V" : " -");
998 fprintf (stderr, flags & 0x004 ? "N" : "-");
999 fprintf (stderr, flags & 0x002 ? "Z" : "-");
1000 fprintf (stderr, flags & 0x001 ? "C" : "-");
1001 }
1002
1003 fprintf (stderr, "\n");
1004 }
1005 }
1006 return 1;
1007 }
1008
1009 if ((call_addr & ~0x3f) == 0x00180)
1010 {
1011 /* Syscall! */
1012 int arg1, arg2, arg3, arg4;
1013 int syscall_num = call_addr & 0x3f;
1014
1015 /* syscall_num == 2 is used for the variadic function "open".
1016 The arguments are set up differently for variadic functions.
1017 See slaa534.pdf distributed by TI. */
1018 if (syscall_num == 2)
1019 {
1020 arg1 = msp430_cpu->regs[12];
1021 arg2 = mem_get_val (sd, SP, 16);
1022 arg3 = mem_get_val (sd, SP + 2, 16);
1023 arg4 = mem_get_val (sd, SP + 4, 16);
1024 }
1025 else
1026 {
1027 arg1 = msp430_cpu->regs[12];
1028 arg2 = msp430_cpu->regs[13];
1029 arg3 = msp430_cpu->regs[14];
1030 arg4 = msp430_cpu->regs[15];
1031 }
1032
1033 msp430_cpu->regs[12] = sim_syscall (cpu, syscall_num, arg1, arg2, arg3,
1034 arg4);
1035 return 1;
1036 }
1037
1038 return 0;
1039 }
1040
1041 static void
1042 msp430_step_once (SIM_DESC sd)
1043 {
1044 sim_cpu *cpu = STATE_CPU (sd, 0);
1045 struct msp430_cpu_state *msp430_cpu = MSP430_SIM_CPU (cpu);
1046 Get_Byte_Local_Data ld;
1047 int i;
1048 int opsize;
1049 unsigned int opcode_pc;
1050 MSP430_Opcode_Decoded opcode_buf;
1051 MSP430_Opcode_Decoded *opcode = &opcode_buf;
1052 int s1, s2, result;
1053 int u1 = 0, u2, uresult;
1054 int c = 0;
1055 int carry_to_use;
1056 int n_repeats;
1057 int rept;
1058 int op_bytes = 0, op_bits;
1059
1060 PC &= 0xfffff;
1061 opcode_pc = PC;
1062
1063 if (opcode_pc < 0x10)
1064 {
1065 fprintf (stderr, "Fault: PC(%#x) is less than 0x10\n", opcode_pc);
1066 sim_engine_halt (sd, cpu, NULL, msp430_cpu->regs[0], sim_exited, -1);
1067 return;
1068 }
1069
1070 if (PC == msp430_cpu->cio_breakpoint && STATE_OPEN_KIND (sd) != SIM_OPEN_DEBUG)
1071 msp430_cio (sd);
1072
1073 ld.sd = sd;
1074 ld.gb_addr = PC;
1075 opsize = msp430_decode_opcode (msp430_cpu->regs[0], opcode, msp430_getbyte,
1076 &ld);
1077 PC += opsize;
1078 if (opsize <= 0)
1079 {
1080 fprintf (stderr, "Fault: undecodable opcode at %#x\n", opcode_pc);
1081 sim_engine_halt (sd, cpu, NULL, msp430_cpu->regs[0], sim_exited, -1);
1082 return;
1083 }
1084
1085 if (opcode->repeat_reg)
1086 n_repeats = (msp430_cpu->regs[opcode->repeats] & 0x000f) + 1;
1087 else
1088 n_repeats = opcode->repeats + 1;
1089
1090 op_bits = opcode->size;
1091 switch (op_bits)
1092 {
1093 case 8:
1094 op_bytes = 1;
1095 break;
1096 case 16:
1097 op_bytes = 2;
1098 break;
1099 case 20:
1100 case 32:
1101 op_bytes = 4;
1102 break;
1103 }
1104
1105 if (TRACE_ANY_P (cpu))
1106 trace_prefix (sd, cpu, NULL_CIA, opcode_pc, TRACE_LINENUM_P (cpu), NULL,
1107 0, " ");
1108
1109 TRACE_DISASM (cpu, opcode_pc);
1110
1111 carry_to_use = 0;
1112 switch (opcode->id)
1113 {
1114 case MSO_unknown:
1115 break;
1116
1117 /* Double-operand instructions. */
1118 case MSO_mov:
1119 if (opcode->n_bytes == 2
1120 && opcode->op[0].type == MSP430_Operand_Register
1121 && opcode->op[0].reg == MSR_CG
1122 && opcode->op[1].type == MSP430_Operand_Immediate
1123 && opcode->op[1].addend == 0
1124 /* A 16-bit write of #0 is a NOP; an 8-bit write is a BRK. */
1125 && opcode->size == 8)
1126 {
1127 /* This is the designated software breakpoint instruction. */
1128 PC -= opsize;
1129 sim_engine_halt (sd, cpu, NULL, msp430_cpu->regs[0], sim_stopped,
1130 SIM_SIGTRAP);
1131 }
1132 else
1133 {
1134 /* Otherwise, do the move. */
1135 for (rept = 0; rept < n_repeats; rept ++)
1136 {
1137 DEST (SRC);
1138 }
1139 }
1140 break;
1141
1142 case MSO_addc:
1143 for (rept = 0; rept < n_repeats; rept ++)
1144 {
1145 carry_to_use = (SR & MSP430_FLAG_C) ? 1 : 0;
1146 u1 = DSRC;
1147 u2 = SRC;
1148 s1 = SX (u1);
1149 s2 = SX (u2);
1150 uresult = u1 + u2 + carry_to_use;
1151 result = s1 + s2 + carry_to_use;
1152 TRACE_ALU (cpu, "ADDC: %#x + %#x + %d = %#x",
1153 u1, u2, carry_to_use, uresult);
1154 DEST (result);
1155 FLAGS (result, uresult != ZX (uresult));
1156 }
1157 break;
1158
1159 case MSO_add:
1160 for (rept = 0; rept < n_repeats; rept ++)
1161 {
1162 u1 = DSRC;
1163 u2 = SRC;
1164 s1 = SX (u1);
1165 s2 = SX (u2);
1166 uresult = u1 + u2;
1167 result = s1 + s2;
1168 TRACE_ALU (cpu, "ADD: %#x + %#x = %#x", u1, u2, uresult);
1169 DEST (result);
1170 FLAGS (result, uresult != ZX (uresult));
1171 }
1172 break;
1173
1174 case MSO_subc:
1175 for (rept = 0; rept < n_repeats; rept ++)
1176 {
1177 carry_to_use = (SR & MSP430_FLAG_C) ? 1 : 0;
1178 u1 = DSRC;
1179 u2 = SRC;
1180 s1 = SX (u1);
1181 s2 = SX (u2);
1182 uresult = ZX (~u2) + u1 + carry_to_use;
1183 result = s1 - s2 + (carry_to_use - 1);
1184 TRACE_ALU (cpu, "SUBC: %#x - %#x + %d = %#x",
1185 u1, u2, carry_to_use, uresult);
1186 DEST (result);
1187 FLAGS (result, uresult != ZX (uresult));
1188 }
1189 break;
1190
1191 case MSO_sub:
1192 for (rept = 0; rept < n_repeats; rept ++)
1193 {
1194 u1 = DSRC;
1195 u2 = SRC;
1196 s1 = SX (u1);
1197 s2 = SX (u2);
1198 uresult = ZX (~u2) + u1 + 1;
1199 result = SX (uresult);
1200 TRACE_ALU (cpu, "SUB: %#x - %#x = %#x",
1201 u1, u2, uresult);
1202 DEST (result);
1203 FLAGS (result, uresult != ZX (uresult));
1204 }
1205 break;
1206
1207 case MSO_cmp:
1208 for (rept = 0; rept < n_repeats; rept ++)
1209 {
1210 u1 = DSRC;
1211 u2 = SRC;
1212 s1 = SX (u1);
1213 s2 = SX (u2);
1214 uresult = ZX (~u2) + u1 + 1;
1215 result = s1 - s2;
1216 TRACE_ALU (cpu, "CMP: %#x - %#x = %x",
1217 u1, u2, uresult);
1218 FLAGS (result, uresult != ZX (uresult));
1219 }
1220 break;
1221
1222 case MSO_dadd:
1223 for (rept = 0; rept < n_repeats; rept ++)
1224 {
1225 carry_to_use = (SR & MSP430_FLAG_C) ? 1 : 0;
1226 u1 = DSRC;
1227 u2 = SRC;
1228 uresult = bcd_to_binary (u1) + bcd_to_binary (u2) + carry_to_use;
1229 result = binary_to_bcd (uresult);
1230 TRACE_ALU (cpu, "DADD: %#x + %#x + %d = %#x",
1231 u1, u2, carry_to_use, result);
1232 DEST (result);
1233 FLAGS (result, uresult > ((opcode->size == 8) ? 99 : 9999));
1234 }
1235 break;
1236
1237 case MSO_and:
1238 for (rept = 0; rept < n_repeats; rept ++)
1239 {
1240 u1 = DSRC;
1241 u2 = SRC;
1242 uresult = u1 & u2;
1243 TRACE_ALU (cpu, "AND: %#x & %#x = %#x",
1244 u1, u2, uresult);
1245 DEST (uresult);
1246 FLAGS (uresult, uresult != 0);
1247 }
1248 break;
1249
1250 case MSO_bit:
1251 for (rept = 0; rept < n_repeats; rept ++)
1252 {
1253 u1 = DSRC;
1254 u2 = SRC;
1255 uresult = u1 & u2;
1256 TRACE_ALU (cpu, "BIT: %#x & %#x -> %#x",
1257 u1, u2, uresult);
1258 FLAGS (uresult, uresult != 0);
1259 }
1260 break;
1261
1262 case MSO_bic:
1263 for (rept = 0; rept < n_repeats; rept ++)
1264 {
1265 u1 = DSRC;
1266 u2 = SRC;
1267 uresult = u1 & ~ u2;
1268 TRACE_ALU (cpu, "BIC: %#x & ~ %#x = %#x",
1269 u1, u2, uresult);
1270 DEST (uresult);
1271 }
1272 break;
1273
1274 case MSO_bis:
1275 for (rept = 0; rept < n_repeats; rept ++)
1276 {
1277 u1 = DSRC;
1278 u2 = SRC;
1279 uresult = u1 | u2;
1280 TRACE_ALU (cpu, "BIS: %#x | %#x = %#x",
1281 u1, u2, uresult);
1282 DEST (uresult);
1283 }
1284 break;
1285
1286 case MSO_xor:
1287 for (rept = 0; rept < n_repeats; rept ++)
1288 {
1289 s1 = 1 << (opcode->size - 1);
1290 u1 = DSRC;
1291 u2 = SRC;
1292 uresult = u1 ^ u2;
1293 TRACE_ALU (cpu, "XOR: %#x & %#x = %#x",
1294 u1, u2, uresult);
1295 DEST (uresult);
1296 FLAGSV (uresult, uresult != 0, (u1 & s1) && (u2 & s1));
1297 }
1298 break;
1299
1300 /* Single-operand instructions. Note: the decoder puts the same
1301 operand in SRC as in DEST, for our convenience. */
1302
1303 case MSO_rrc:
1304 for (rept = 0; rept < n_repeats; rept ++)
1305 {
1306 u1 = SRC;
1307 carry_to_use = u1 & 1;
1308 uresult = u1 >> 1;
1309 /* If the ZC bit of the opcode is set, it means we are synthesizing
1310 RRUX, so the carry bit must be ignored. */
1311 if (opcode->zc == 0 && (SR & MSP430_FLAG_C))
1312 uresult |= (1 << (opcode->size - 1));
1313 TRACE_ALU (cpu, "RRC: %#x >>= %#x",
1314 u1, uresult);
1315 DEST (uresult);
1316 FLAGS (uresult, carry_to_use);
1317 }
1318 break;
1319
1320 case MSO_swpb:
1321 for (rept = 0; rept < n_repeats; rept ++)
1322 {
1323 u1 = SRC;
1324 uresult = ((u1 >> 8) & 0x00ff) | ((u1 << 8) & 0xff00);
1325 TRACE_ALU (cpu, "SWPB: %#x -> %#x",
1326 u1, uresult);
1327 DEST (uresult);
1328 }
1329 break;
1330
1331 case MSO_rra:
1332 for (rept = 0; rept < n_repeats; rept ++)
1333 {
1334 u1 = SRC;
1335 c = u1 & 1;
1336 s1 = 1 << (opcode->size - 1);
1337 uresult = (u1 >> 1) | (u1 & s1);
1338 TRACE_ALU (cpu, "RRA: %#x >>= %#x",
1339 u1, uresult);
1340 DEST (uresult);
1341 FLAGS (uresult, c);
1342 }
1343 break;
1344
1345 case MSO_rru:
1346 for (rept = 0; rept < n_repeats; rept ++)
1347 {
1348 u1 = SRC;
1349 c = u1 & 1;
1350 uresult = (u1 >> 1);
1351 TRACE_ALU (cpu, "RRU: %#x >>= %#x",
1352 u1, uresult);
1353 DEST (uresult);
1354 FLAGS (uresult, c);
1355 }
1356 break;
1357
1358 case MSO_sxt:
1359 for (rept = 0; rept < n_repeats; rept ++)
1360 {
1361 u1 = SRC;
1362 if (u1 & 0x80)
1363 uresult = u1 | 0xfff00;
1364 else
1365 uresult = u1 & 0x000ff;
1366 TRACE_ALU (cpu, "SXT: %#x -> %#x", u1, uresult);
1367 DEST (uresult);
1368 FLAGS (uresult, c);
1369 }
1370 break;
1371
1372 case MSO_push:
1373 for (rept = 0; rept < n_repeats; rept ++)
1374 {
1375 int new_sp;
1376
1377 new_sp = REG_GET (MSR_SP) - op_bytes;
1378 /* SP is always word-aligned. */
1379 if (new_sp & 1)
1380 new_sp --;
1381 REG_PUT (MSR_SP, new_sp);
1382 u1 = SRC;
1383 mem_put_val (sd, SP, u1, op_bits);
1384 if (opcode->op[1].type == MSP430_Operand_Register)
1385 opcode->op[1].reg --;
1386 }
1387 break;
1388
1389 case MSO_pop:
1390 for (rept = 0; rept < n_repeats; rept ++)
1391 {
1392 int new_sp;
1393
1394 u1 = mem_get_val (sd, SP, op_bits);
1395 DEST (u1);
1396 if (opcode->op[0].type == MSP430_Operand_Register)
1397 opcode->op[0].reg ++;
1398 new_sp = REG_GET (MSR_SP) + op_bytes;
1399 /* SP is always word-aligned. */
1400 if (new_sp & 1)
1401 new_sp ++;
1402 REG_PUT (MSR_SP, new_sp);
1403 }
1404 break;
1405
1406 case MSO_call:
1407 u1 = SRC;
1408
1409 if (maybe_perform_syscall (sd, u1))
1410 break;
1411
1412 REG_PUT (MSR_SP, REG_GET (MSR_SP) - op_bytes);
1413 mem_put_val (sd, SP, PC, op_bits);
1414 TRACE_ALU (cpu, "CALL: func %#x ret %#x, sp %#x",
1415 u1, PC, SP);
1416 REG_PUT (MSR_PC, u1);
1417 break;
1418
1419 case MSO_reti:
1420 u1 = mem_get_val (sd, SP, 16);
1421 SR = u1 & 0xFF;
1422 SP += 2;
1423 PC = mem_get_val (sd, SP, 16);
1424 SP += 2;
1425 /* Emulate the RETI action of the 20-bit CPUX architecure.
1426 This is safe for 16-bit CPU architectures as well, since the top
1427 8-bits of SR will have been written to the stack here, and will
1428 have been read as 0. */
1429 PC |= (u1 & 0xF000) << 4;
1430 TRACE_ALU (cpu, "RETI: pc %#x sr %#x", PC, SR);
1431 break;
1432
1433 /* Jumps. */
1434
1435 case MSO_jmp:
1436 i = SRC;
1437 switch (opcode->cond)
1438 {
1439 case MSC_nz:
1440 u1 = (SR & MSP430_FLAG_Z) ? 0 : 1;
1441 break;
1442 case MSC_z:
1443 u1 = (SR & MSP430_FLAG_Z) ? 1 : 0;
1444 break;
1445 case MSC_nc:
1446 u1 = (SR & MSP430_FLAG_C) ? 0 : 1;
1447 break;
1448 case MSC_c:
1449 u1 = (SR & MSP430_FLAG_C) ? 1 : 0;
1450 break;
1451 case MSC_n:
1452 u1 = (SR & MSP430_FLAG_N) ? 1 : 0;
1453 break;
1454 case MSC_ge:
1455 u1 = (!!(SR & MSP430_FLAG_N) == !!(SR & MSP430_FLAG_V)) ? 1 : 0;
1456 break;
1457 case MSC_l:
1458 u1 = (!!(SR & MSP430_FLAG_N) == !!(SR & MSP430_FLAG_V)) ? 0 : 1;
1459 break;
1460 case MSC_true:
1461 u1 = 1;
1462 break;
1463 }
1464
1465 if (u1)
1466 {
1467 TRACE_BRANCH (cpu, "J%s: pc %#x -> %#x sr %#x, taken",
1468 cond_string (opcode->cond), PC, i, SR);
1469 PC = i;
1470 if (PC == opcode_pc)
1471 exit (0);
1472 }
1473 else
1474 TRACE_BRANCH (cpu, "J%s: pc %#x to %#x sr %#x, not taken",
1475 cond_string (opcode->cond), PC, i, SR);
1476 break;
1477
1478 default:
1479 fprintf (stderr, "error: unexpected opcode id %d\n", opcode->id);
1480 exit (1);
1481 }
1482 }
1483
1484 void
1485 sim_engine_run (SIM_DESC sd,
1486 int next_cpu_nr,
1487 int nr_cpus,
1488 int siggnal)
1489 {
1490 while (1)
1491 {
1492 msp430_step_once (sd);
1493 if (sim_events_tick (sd))
1494 sim_events_process (sd);
1495 }
1496 }