1 /* reg.c --- register set model for M32C simulator.
3 Copyright (C) 2005-2021 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 int enable_counting = 0;
34 int addr_mask = 0xffff;
35 int membus_mask = 0xfffff;
38 unsigned int heapbottom = 0;
39 unsigned int heaptop = 0;
52 "intb", "intbl", "intbh",
53 "sp", "usp", "isp", "pc", "flags"
72 unsigned int b2mask[] = { 0, 0xff, 0xffff, 0xffffff, 0xffffffff };
73 unsigned int b2signbit[] = { 0, (1 << 7), (1 << 15), (1 << 24), (1 << 31) };
74 int b2maxsigned[] = { 0, 0x7f, 0x7fff, 0x7fffff, 0x7fffffff };
75 int b2minsigned[] = { 0, -128, -32768, -8388608, -2147483647 - 1 };
77 static regs_type oldregs;
84 memset (®s, 0, sizeof (regs));
85 memset (&oldregs, 0, sizeof (oldregs));
89 set_pointer_width (int bytes)
94 membus_mask = 0x000fffff;
95 reg_bytes[a0] = reg_bytes[a1] = reg_bytes[sb] = reg_bytes[fb] =
96 reg_bytes[sp] = reg_bytes[usp] = reg_bytes[isp] = 2;
100 addr_mask = 0xffffff;
101 membus_mask = 0x00ffffff;
102 reg_bytes[a0] = reg_bytes[a1] = reg_bytes[sb] = reg_bytes[fb] =
103 reg_bytes[sp] = reg_bytes[usp] = reg_bytes[isp] = 3;
108 m32c_set_cpu (int cpu)
114 set_pointer_width (2);
115 decode_opcode = decode_r8c;
119 set_pointer_width (3);
120 decode_opcode = decode_m32c;
129 get_reg_i (reg_id id)
131 reg_bank_type *b = regs.r + (FLAG_B ? 1 : 0);
140 return b->r_r0 & 0xff;
146 return b->r_r1 & 0xff;
150 return b->r_r2 * 65536 + b->r_r0;
154 return b->r_r3 * 65536 + b->r_r1;
157 return b->r_a0 & addr_mask;
159 return b->r_a1 & addr_mask;
161 return (b->r_a1 & 0xffff) * 65536 | (b->r_a0 & 0xffff);
164 return b->r_sb & addr_mask;
166 return b->r_fb & addr_mask;
169 return regs.r_intbh * 65536 + regs.r_intbl;
176 return ((regs.r_flags & FLAGBIT_U) ? regs.r_usp : regs.
179 return regs.r_usp & addr_mask;
181 return regs.r_isp & addr_mask;
184 return regs.r_pc & membus_mask;
195 unsigned int rv = get_reg_i (id);
196 if (trace > ((id != pc && id != fb && id != sp) ? 0 : 1))
197 printf ("get_reg (%s) = %0*x\n", reg_names[id], reg_bytes[id] * 2, rv);
202 get_reg_ll (reg_id id)
204 reg_bank_type *b = regs.r + (FLAG_B ? 1 : 0);
209 return ((DI) b->r_r3 << 48
210 | (DI) b->r_r1 << 32 | (DI) b->r_r2 << 16 | (DI) b->r_r0);
212 return ((DI) b->r_r3 << 48
213 | (DI) b->r_r2 << 32 | (DI) b->r_r1 << 16 | (DI) b->r_r0);
219 static int highest_sp = 0, lowest_sp = 0xffffff;
222 stack_heap_stats (void)
224 printf ("heap: %08x - %08x (%d bytes)\n", heapbottom, heaptop,
225 heaptop - heapbottom);
226 printf ("stack: %08x - %08x (%d bytes)\n", lowest_sp, highest_sp,
227 highest_sp - lowest_sp);
231 put_reg (reg_id id, unsigned int v)
233 reg_bank_type *b = regs.r + (FLAG_B ? 1 : 0);
235 if (trace > ((id != pc) ? 0 : 1))
236 printf ("put_reg (%s) = %0*x\n", reg_names[id], reg_bytes[id] * 2, v);
244 b->r_r0 = (b->r_r0 & 0xff) | (v << 8);
247 b->r_r0 = (b->r_r0 & 0xff00) | (v & 0xff);
253 b->r_r1 = (b->r_r1 & 0xff) | (v << 8);
256 b->r_r1 = (b->r_r1 & 0xff00) | (v & 0xff);
262 b->r_r0 = v & 0xffff;
269 b->r_r1 = v & 0xffff;
274 b->r_a0 = v & addr_mask;
277 b->r_a1 = v & addr_mask;
280 b->r_a0 = v & 0xffff;
285 b->r_sb = v & addr_mask;
288 b->r_fb = v & addr_mask;
292 regs.r_intbl = v & 0xffff;
293 regs.r_intbh = v >> 16;
296 regs.r_intbl = v & 0xffff;
299 regs.r_intbh = v & 0xff;
305 if (regs.r_flags & FLAGBIT_U)
309 *spp = v & addr_mask;
312 printf ("collision: pc %08lx heap %08x stack %08lx\n", regs.r_pc,
316 if (*spp < lowest_sp)
318 if (*spp > highest_sp)
323 regs.r_usp = v & addr_mask;
326 regs.r_isp = v & addr_mask;
330 regs.r_pc = v & membus_mask;
341 condition_true (int cond_id)
346 static const char *cond_name[] = {
347 "C", "C&!Z", "Z", "S",
348 "!C", "!(C&!Z)", "!Z", "!S",
349 "(S^O)|Z", "O", "!(S^O)", "unk",
350 "!((S^O)|Z)", "!O", "S^O", "unk"
352 switch (cond_id & 15)
358 f = FLAG_C & !FLAG_Z;
370 f = !(FLAG_C & !FLAG_Z);
380 f = (FLAG_S ^ FLAG_O) | FLAG_Z;
386 f = !(FLAG_S ^ FLAG_O);
389 f = !((FLAG_S ^ FLAG_O) | FLAG_Z);
403 printf ("cond[%d] %s = %s\n", cond_id, cond_name[cond_id & 15],
404 f ? "true" : "false");
408 static const char *cond_name[] = {
409 "!C", "LEU", "!Z", "PZ",
410 "!O", "GT", "GE", "?",
411 "C", "GTU", "Z", "N",
412 "O", "LE", "LT", "!?"
414 switch (cond_id & 15)
420 f = !(FLAG_C & !FLAG_Z);
433 f = !((FLAG_S ^ FLAG_O) | FLAG_Z);
436 f = !(FLAG_S ^ FLAG_O);
443 f = FLAG_C & !FLAG_Z;
456 f = (FLAG_S ^ FLAG_O) | FLAG_Z;
467 printf ("cond[%d] %s = %s\n", cond_id, cond_name[cond_id & 15],
468 f ? "true" : "false");
474 set_flags (int mask, int newbits)
477 regs.r_flags &= ~mask;
478 regs.r_flags |= newbits & mask;
481 printf ("flags now \033[32m %d", (regs.r_flags >> (A16 ? 8 : 12)) & 7);
482 for (i = 7; i >= 0; i--)
483 if (regs.r_flags & (1 << i))
484 putchar ("CDZSBOIU"[i]);
487 printf ("\033[0m\n");
492 set_oszc (int value, int b, int c)
494 int mask = b2mask[b];
499 if ((value & mask) == 0)
501 if (value & b2signbit[b])
503 if ((value > b2maxsigned[b]) || (value < b2minsigned[b]))
505 set_flags (FLAGBIT_Z | FLAGBIT_S | FLAGBIT_O | FLAGBIT_C, f);
509 set_szc (int value, int b, int c)
511 int mask = b2mask[b];
516 if ((value & mask) == 0)
518 if (value & b2signbit[b])
520 set_flags (FLAGBIT_Z | FLAGBIT_S | FLAGBIT_C, f);
524 set_osz (int value, int b)
526 int mask = b2mask[b];
529 if ((value & mask) == 0)
531 if (value & b2signbit[b])
533 if (value & ~mask && (value & ~mask) != ~mask)
535 set_flags (FLAGBIT_Z | FLAGBIT_S | FLAGBIT_O, f);
539 set_sz (int value, int b)
541 int mask = b2mask[b];
544 if ((value & mask) == 0)
546 if (value & b2signbit[b])
548 set_flags (FLAGBIT_Z | FLAGBIT_S, f);
552 set_zc (int z, int c)
554 set_flags (FLAGBIT_C | FLAGBIT_Z,
555 (c ? FLAGBIT_C : 0) | (z ? FLAGBIT_Z : 0));
561 set_flags (FLAGBIT_C, c ? FLAGBIT_C : 0);
565 put_reg_ll (reg_id id, DI v)
567 reg_bank_type *b = regs.r + (FLAG_B ? 1 : 0);
592 static char fn[] = "CDZSBOIU";
593 printf ("%d.", (f >> 12) & 7);
594 for (i = 7; i >= 0; i--)
599 #define TRC(f,n, id) \
600 if (oldregs.f != regs.f) \
602 printf(" %s %0*x:%0*x", n, \
603 reg_bytes[id]*2, (unsigned int)oldregs.f, \
604 reg_bytes[id]*2, (unsigned int)regs.f); \
605 oldregs.f = regs.f; \
609 trace_register_changes (void)
613 printf ("\033[36mREGS:");
614 TRC (r[0].r_r0, "r0", r0);
615 TRC (r[0].r_r1, "r1", r1);
616 TRC (r[0].r_r2, "r2", r2);
617 TRC (r[0].r_r3, "r3", r3);
618 TRC (r[0].r_a0, "a0", a0);
619 TRC (r[0].r_a1, "a1", a1);
620 TRC (r[0].r_sb, "sb", sb);
621 TRC (r[0].r_fb, "fb", fb);
622 TRC (r[1].r_r0, "r0'", r0);
623 TRC (r[1].r_r1, "r1'", r1);
624 TRC (r[1].r_r2, "r2'", r2);
625 TRC (r[1].r_r3, "r3'", r3);
626 TRC (r[1].r_a0, "a0'", a0);
627 TRC (r[1].r_a1, "a1'", a1);
628 TRC (r[1].r_sb, "sb'", sb);
629 TRC (r[1].r_fb, "fb'", fb);
630 TRC (r_intbh, "intbh", intbh);
631 TRC (r_intbl, "intbl", intbl);
632 TRC (r_usp, "usp", usp);
633 TRC (r_isp, "isp", isp);
634 TRC (r_pc, "pc", pc);
635 if (oldregs.r_flags != regs.r_flags)
638 print_flags (oldregs.r_flags);
640 print_flags (regs.r_flags);
642 printf ("\033[0m\n");
645 #define DRC(f, n, id) \
646 printf(" %-3s %0*x", n, \
647 reg_bytes[id]*2, (unsigned int)regs.f); \
650 m32c_dump_all_registers (void)
652 printf ("\033[36mREGS:");
653 DRC (r[0].r_r0, "r0", r0);
654 DRC (r[0].r_r1, "r1", r1);
655 DRC (r[0].r_r2, "r2", r2);
656 DRC (r[0].r_r3, "r3", r3);
657 DRC (r[0].r_a0, "a0", a0);
658 DRC (r[0].r_a1, "a1", a1);
659 DRC (r[0].r_sb, "sb", sb);
660 DRC (r[0].r_fb, "fb", fb);
662 DRC (r[1].r_r0, "r0'", r0);
663 DRC (r[1].r_r1, "r1'", r1);
664 DRC (r[1].r_r2, "r2'", r2);
665 DRC (r[1].r_r3, "r3'", r3);
666 DRC (r[1].r_a0, "a0'", a0);
667 DRC (r[1].r_a1, "a1'", a1);
668 DRC (r[1].r_sb, "sb'", sb);
669 DRC (r[1].r_fb, "fb'", fb);
671 DRC (r_intbh, "intbh", intbh);
672 DRC (r_intbl, "intbl", intbl);
673 DRC (r_usp, "usp", usp);
674 DRC (r_isp, "isp", isp);
675 DRC (r_pc, "pc", pc);
677 print_flags (regs.r_flags);
678 printf ("\033[0m\n");
679 /*sim_disasm_one (); */