1a5a4af3bSchristos /* m32r2 simulator support code 2*8b657b07Schristos Copyright (C) 1997-2023 Free Software Foundation, Inc. 3a5a4af3bSchristos Contributed by Cygnus Support. 4a5a4af3bSchristos 5a5a4af3bSchristos This file is part of GDB, the GNU debugger. 6a5a4af3bSchristos 7a5a4af3bSchristos This program is free software; you can redistribute it and/or modify 8a5a4af3bSchristos it under the terms of the GNU General Public License as published by 9a5a4af3bSchristos the Free Software Foundation; either version 3 of the License, or 10a5a4af3bSchristos (at your option) any later version. 11a5a4af3bSchristos 12a5a4af3bSchristos This program is distributed in the hope that it will be useful, 13a5a4af3bSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 14a5a4af3bSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15a5a4af3bSchristos GNU General Public License for more details. 16a5a4af3bSchristos 17a5a4af3bSchristos You should have received a copy of the GNU General Public License 18a5a4af3bSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19a5a4af3bSchristos 20*8b657b07Schristos /* This must come before any other includes. */ 21*8b657b07Schristos #include "defs.h" 22*8b657b07Schristos 23a5a4af3bSchristos #define WANT_CPU m32r2f 24a5a4af3bSchristos #define WANT_CPU_M32R2F 25a5a4af3bSchristos 26a5a4af3bSchristos #include "sim-main.h" 27a5a4af3bSchristos #include "cgen-mem.h" 28a5a4af3bSchristos #include "cgen-ops.h" 29a5a4af3bSchristos 30a5a4af3bSchristos /* The contents of BUF are in target byte order. */ 31a5a4af3bSchristos 32a5a4af3bSchristos int 33*8b657b07Schristos m32r2f_fetch_register (SIM_CPU *current_cpu, int rn, void *buf, int len) 34a5a4af3bSchristos { 35a5a4af3bSchristos return m32rbf_fetch_register (current_cpu, rn, buf, len); 36a5a4af3bSchristos } 37a5a4af3bSchristos 38a5a4af3bSchristos /* The contents of BUF are in target byte order. */ 39a5a4af3bSchristos 40a5a4af3bSchristos int 41*8b657b07Schristos m32r2f_store_register (SIM_CPU *current_cpu, int rn, const void *buf, int len) 42a5a4af3bSchristos { 43a5a4af3bSchristos return m32rbf_store_register (current_cpu, rn, buf, len); 44a5a4af3bSchristos } 45a5a4af3bSchristos 46a5a4af3bSchristos /* Cover fns to get/set the control registers. 47a5a4af3bSchristos FIXME: Duplicated from m32r.c. The issue is structure offsets. */ 48a5a4af3bSchristos 49a5a4af3bSchristos USI 50a5a4af3bSchristos m32r2f_h_cr_get_handler (SIM_CPU *current_cpu, UINT cr) 51a5a4af3bSchristos { 52a5a4af3bSchristos switch (cr) 53a5a4af3bSchristos { 54a5a4af3bSchristos case H_CR_PSW : /* PSW. */ 55a5a4af3bSchristos return (((CPU (h_bpsw) & 0xc1) << 8) 56a5a4af3bSchristos | ((CPU (h_psw) & 0xc0) << 0) 57a5a4af3bSchristos | GET_H_COND ()); 58a5a4af3bSchristos case H_CR_BBPSW : /* Backup backup psw. */ 59a5a4af3bSchristos return CPU (h_bbpsw) & 0xc1; 60a5a4af3bSchristos case H_CR_CBR : /* Condition bit. */ 61a5a4af3bSchristos return GET_H_COND (); 62a5a4af3bSchristos case H_CR_SPI : /* Interrupt stack pointer. */ 63a5a4af3bSchristos if (! GET_H_SM ()) 64a5a4af3bSchristos return CPU (h_gr[H_GR_SP]); 65a5a4af3bSchristos else 66a5a4af3bSchristos return CPU (h_cr[H_CR_SPI]); 67a5a4af3bSchristos case H_CR_SPU : /* User stack pointer. */ 68a5a4af3bSchristos if (GET_H_SM ()) 69a5a4af3bSchristos return CPU (h_gr[H_GR_SP]); 70a5a4af3bSchristos else 71a5a4af3bSchristos return CPU (h_cr[H_CR_SPU]); 72a5a4af3bSchristos case H_CR_BPC : /* Backup pc. */ 73a5a4af3bSchristos return CPU (h_cr[H_CR_BPC]) & 0xfffffffe; 74a5a4af3bSchristos case H_CR_BBPC : /* Backup backup pc. */ 75a5a4af3bSchristos return CPU (h_cr[H_CR_BBPC]) & 0xfffffffe; 76a5a4af3bSchristos case 4 : /* ??? unspecified, but apparently available */ 77a5a4af3bSchristos case 5 : /* ??? unspecified, but apparently available */ 78a5a4af3bSchristos return CPU (h_cr[cr]); 79a5a4af3bSchristos default : 80a5a4af3bSchristos return 0; 81a5a4af3bSchristos } 82a5a4af3bSchristos } 83a5a4af3bSchristos 84a5a4af3bSchristos void 85a5a4af3bSchristos m32r2f_h_cr_set_handler (SIM_CPU *current_cpu, UINT cr, USI newval) 86a5a4af3bSchristos { 87a5a4af3bSchristos switch (cr) 88a5a4af3bSchristos { 89a5a4af3bSchristos case H_CR_PSW : /* psw */ 90a5a4af3bSchristos { 91a5a4af3bSchristos int old_sm = (CPU (h_psw) & 0x80) != 0; 92a5a4af3bSchristos int new_sm = (newval & 0x80) != 0; 93a5a4af3bSchristos CPU (h_bpsw) = (newval >> 8) & 0xff; 94a5a4af3bSchristos CPU (h_psw) = newval & 0xff; 95a5a4af3bSchristos SET_H_COND (newval & 1); 96a5a4af3bSchristos /* When switching stack modes, update the registers. */ 97a5a4af3bSchristos if (old_sm != new_sm) 98a5a4af3bSchristos { 99a5a4af3bSchristos if (old_sm) 100a5a4af3bSchristos { 101a5a4af3bSchristos /* Switching user -> system. */ 102a5a4af3bSchristos CPU (h_cr[H_CR_SPU]) = CPU (h_gr[H_GR_SP]); 103a5a4af3bSchristos CPU (h_gr[H_GR_SP]) = CPU (h_cr[H_CR_SPI]); 104a5a4af3bSchristos } 105a5a4af3bSchristos else 106a5a4af3bSchristos { 107a5a4af3bSchristos /* Switching system -> user. */ 108a5a4af3bSchristos CPU (h_cr[H_CR_SPI]) = CPU (h_gr[H_GR_SP]); 109a5a4af3bSchristos CPU (h_gr[H_GR_SP]) = CPU (h_cr[H_CR_SPU]); 110a5a4af3bSchristos } 111a5a4af3bSchristos } 112a5a4af3bSchristos break; 113a5a4af3bSchristos } 114a5a4af3bSchristos case H_CR_BBPSW : /* backup backup psw */ 115a5a4af3bSchristos CPU (h_bbpsw) = newval & 0xff; 116a5a4af3bSchristos break; 117a5a4af3bSchristos case H_CR_CBR : /* condition bit */ 118a5a4af3bSchristos SET_H_COND (newval & 1); 119a5a4af3bSchristos break; 120a5a4af3bSchristos case H_CR_SPI : /* interrupt stack pointer */ 121a5a4af3bSchristos if (! GET_H_SM ()) 122a5a4af3bSchristos CPU (h_gr[H_GR_SP]) = newval; 123a5a4af3bSchristos else 124a5a4af3bSchristos CPU (h_cr[H_CR_SPI]) = newval; 125a5a4af3bSchristos break; 126a5a4af3bSchristos case H_CR_SPU : /* user stack pointer */ 127a5a4af3bSchristos if (GET_H_SM ()) 128a5a4af3bSchristos CPU (h_gr[H_GR_SP]) = newval; 129a5a4af3bSchristos else 130a5a4af3bSchristos CPU (h_cr[H_CR_SPU]) = newval; 131a5a4af3bSchristos break; 132a5a4af3bSchristos case H_CR_BPC : /* backup pc */ 133a5a4af3bSchristos CPU (h_cr[H_CR_BPC]) = newval; 134a5a4af3bSchristos break; 135a5a4af3bSchristos case H_CR_BBPC : /* backup backup pc */ 136a5a4af3bSchristos CPU (h_cr[H_CR_BBPC]) = newval; 137a5a4af3bSchristos break; 138a5a4af3bSchristos case 4 : /* ??? unspecified, but apparently available */ 139a5a4af3bSchristos case 5 : /* ??? unspecified, but apparently available */ 140a5a4af3bSchristos CPU (h_cr[cr]) = newval; 141a5a4af3bSchristos break; 142a5a4af3bSchristos default : 143a5a4af3bSchristos /* ignore */ 144a5a4af3bSchristos break; 145a5a4af3bSchristos } 146a5a4af3bSchristos } 147a5a4af3bSchristos 148a5a4af3bSchristos /* Cover fns to access h-psw. */ 149a5a4af3bSchristos 150a5a4af3bSchristos UQI 151a5a4af3bSchristos m32r2f_h_psw_get_handler (SIM_CPU *current_cpu) 152a5a4af3bSchristos { 153a5a4af3bSchristos return (CPU (h_psw) & 0xfe) | (CPU (h_cond) & 1); 154a5a4af3bSchristos } 155a5a4af3bSchristos 156a5a4af3bSchristos void 157a5a4af3bSchristos m32r2f_h_psw_set_handler (SIM_CPU *current_cpu, UQI newval) 158a5a4af3bSchristos { 159a5a4af3bSchristos CPU (h_psw) = newval; 160a5a4af3bSchristos CPU (h_cond) = newval & 1; 161a5a4af3bSchristos } 162a5a4af3bSchristos 163a5a4af3bSchristos /* Cover fns to access h-accum. */ 164a5a4af3bSchristos 165a5a4af3bSchristos DI 166a5a4af3bSchristos m32r2f_h_accum_get_handler (SIM_CPU *current_cpu) 167a5a4af3bSchristos { 168a5a4af3bSchristos /* Sign extend the top 8 bits. */ 169a5a4af3bSchristos DI r; 170a5a4af3bSchristos r = ANDDI (CPU (h_accum), MAKEDI (0xffffff, 0xffffffff)); 171a5a4af3bSchristos r = XORDI (r, MAKEDI (0x800000, 0)); 172a5a4af3bSchristos r = SUBDI (r, MAKEDI (0x800000, 0)); 173a5a4af3bSchristos return r; 174a5a4af3bSchristos } 175a5a4af3bSchristos 176a5a4af3bSchristos void 177a5a4af3bSchristos m32r2f_h_accum_set_handler (SIM_CPU *current_cpu, DI newval) 178a5a4af3bSchristos { 179a5a4af3bSchristos CPU (h_accum) = newval; 180a5a4af3bSchristos } 181a5a4af3bSchristos 182a5a4af3bSchristos /* Cover fns to access h-accums. */ 183a5a4af3bSchristos 184a5a4af3bSchristos DI 185a5a4af3bSchristos m32r2f_h_accums_get_handler (SIM_CPU *current_cpu, UINT regno) 186a5a4af3bSchristos { 187a5a4af3bSchristos /* FIXME: Yes, this is just a quick hack. */ 188a5a4af3bSchristos DI r; 189a5a4af3bSchristos if (regno == 0) 190a5a4af3bSchristos r = CPU (h_accum); 191a5a4af3bSchristos else 192a5a4af3bSchristos r = CPU (h_accums[1]); 193a5a4af3bSchristos /* Sign extend the top 8 bits. */ 194a5a4af3bSchristos r = ANDDI (r, MAKEDI (0xffffff, 0xffffffff)); 195a5a4af3bSchristos r = XORDI (r, MAKEDI (0x800000, 0)); 196a5a4af3bSchristos r = SUBDI (r, MAKEDI (0x800000, 0)); 197a5a4af3bSchristos return r; 198a5a4af3bSchristos } 199a5a4af3bSchristos 200a5a4af3bSchristos void 201a5a4af3bSchristos m32r2f_h_accums_set_handler (SIM_CPU *current_cpu, UINT regno, DI newval) 202a5a4af3bSchristos { 203a5a4af3bSchristos /* FIXME: Yes, this is just a quick hack. */ 204a5a4af3bSchristos if (regno == 0) 205a5a4af3bSchristos CPU (h_accum) = newval; 206a5a4af3bSchristos else 207a5a4af3bSchristos CPU (h_accums[1]) = newval; 208a5a4af3bSchristos } 209a5a4af3bSchristos 210a5a4af3bSchristos #if WITH_PROFILE_MODEL_P 211a5a4af3bSchristos 212a5a4af3bSchristos /* Initialize cycle counting for an insn. 213a5a4af3bSchristos FIRST_P is non-zero if this is the first insn in a set of parallel 214a5a4af3bSchristos insns. */ 215a5a4af3bSchristos 216a5a4af3bSchristos void 217a5a4af3bSchristos m32r2f_model_insn_before (SIM_CPU *cpu, int first_p) 218a5a4af3bSchristos { 219a5a4af3bSchristos m32rbf_model_insn_before (cpu, first_p); 220a5a4af3bSchristos } 221a5a4af3bSchristos 222a5a4af3bSchristos /* Record the cycles computed for an insn. 223a5a4af3bSchristos LAST_P is non-zero if this is the last insn in a set of parallel insns, 224a5a4af3bSchristos and we update the total cycle count. 225a5a4af3bSchristos CYCLES is the cycle count of the insn. */ 226a5a4af3bSchristos 227a5a4af3bSchristos void 228a5a4af3bSchristos m32r2f_model_insn_after (SIM_CPU *cpu, int last_p, int cycles) 229a5a4af3bSchristos { 230a5a4af3bSchristos m32rbf_model_insn_after (cpu, last_p, cycles); 231a5a4af3bSchristos } 232a5a4af3bSchristos 233a5a4af3bSchristos static INLINE void 234a5a4af3bSchristos check_load_stall (SIM_CPU *cpu, int regno) 235a5a4af3bSchristos { 236a5a4af3bSchristos UINT h_gr = CPU_M32R_MISC_PROFILE (cpu)->load_regs; 237a5a4af3bSchristos 238a5a4af3bSchristos if (regno != -1 239a5a4af3bSchristos && (h_gr & (1 << regno)) != 0) 240a5a4af3bSchristos { 241a5a4af3bSchristos CPU_M32R_MISC_PROFILE (cpu)->load_stall += 2; 242a5a4af3bSchristos if (TRACE_INSN_P (cpu)) 243a5a4af3bSchristos cgen_trace_printf (cpu, " ; Load stall of 2 cycles."); 244a5a4af3bSchristos } 245a5a4af3bSchristos } 246a5a4af3bSchristos 247a5a4af3bSchristos int 248a5a4af3bSchristos m32r2f_model_m32r2_u_exec (SIM_CPU *cpu, const IDESC *idesc, 249a5a4af3bSchristos int unit_num, int referenced, 250a5a4af3bSchristos INT sr, INT sr2, INT dr) 251a5a4af3bSchristos { 252a5a4af3bSchristos check_load_stall (cpu, sr); 253a5a4af3bSchristos check_load_stall (cpu, sr2); 254a5a4af3bSchristos return idesc->timing->units[unit_num].done; 255a5a4af3bSchristos } 256a5a4af3bSchristos 257a5a4af3bSchristos int 258a5a4af3bSchristos m32r2f_model_m32r2_u_cmp (SIM_CPU *cpu, const IDESC *idesc, 259a5a4af3bSchristos int unit_num, int referenced, 260a5a4af3bSchristos INT src1, INT src2) 261a5a4af3bSchristos { 262a5a4af3bSchristos check_load_stall (cpu, src1); 263a5a4af3bSchristos check_load_stall (cpu, src2); 264a5a4af3bSchristos return idesc->timing->units[unit_num].done; 265a5a4af3bSchristos } 266a5a4af3bSchristos 267a5a4af3bSchristos int 268a5a4af3bSchristos m32r2f_model_m32r2_u_mac (SIM_CPU *cpu, const IDESC *idesc, 269a5a4af3bSchristos int unit_num, int referenced, 270a5a4af3bSchristos INT src1, INT src2) 271a5a4af3bSchristos { 272a5a4af3bSchristos check_load_stall (cpu, src1); 273a5a4af3bSchristos check_load_stall (cpu, src2); 274a5a4af3bSchristos return idesc->timing->units[unit_num].done; 275a5a4af3bSchristos } 276a5a4af3bSchristos 277a5a4af3bSchristos int 278a5a4af3bSchristos m32r2f_model_m32r2_u_cti (SIM_CPU *cpu, const IDESC *idesc, 279a5a4af3bSchristos int unit_num, int referenced, 280a5a4af3bSchristos INT sr) 281a5a4af3bSchristos { 282a5a4af3bSchristos PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu); 283a5a4af3bSchristos int taken_p = (referenced & (1 << 1)) != 0; 284a5a4af3bSchristos 285a5a4af3bSchristos check_load_stall (cpu, sr); 286a5a4af3bSchristos if (taken_p) 287a5a4af3bSchristos { 288a5a4af3bSchristos CPU_M32R_MISC_PROFILE (cpu)->cti_stall += 2; 289a5a4af3bSchristos PROFILE_MODEL_TAKEN_COUNT (profile) += 1; 290a5a4af3bSchristos } 291a5a4af3bSchristos else 292a5a4af3bSchristos PROFILE_MODEL_UNTAKEN_COUNT (profile) += 1; 293a5a4af3bSchristos return idesc->timing->units[unit_num].done; 294a5a4af3bSchristos } 295a5a4af3bSchristos 296a5a4af3bSchristos int 297a5a4af3bSchristos m32r2f_model_m32r2_u_load (SIM_CPU *cpu, const IDESC *idesc, 298a5a4af3bSchristos int unit_num, int referenced, 299a5a4af3bSchristos INT sr, INT dr) 300a5a4af3bSchristos { 301a5a4af3bSchristos CPU_M32R_MISC_PROFILE (cpu)->load_regs_pending |= (1 << dr); 302a5a4af3bSchristos return idesc->timing->units[unit_num].done; 303a5a4af3bSchristos } 304a5a4af3bSchristos 305a5a4af3bSchristos int 306a5a4af3bSchristos m32r2f_model_m32r2_u_store (SIM_CPU *cpu, const IDESC *idesc, 307a5a4af3bSchristos int unit_num, int referenced, 308a5a4af3bSchristos INT src1, INT src2) 309a5a4af3bSchristos { 310a5a4af3bSchristos return idesc->timing->units[unit_num].done; 311a5a4af3bSchristos } 312a5a4af3bSchristos 313a5a4af3bSchristos #endif /* WITH_PROFILE_MODEL_P */ 314