URL
https://opencores.org/ocsvn/or1k/or1k/trunk
Subversion Repositories or1k
[/] [or1k/] [tags/] [tn_m001/] [or1ksim/] [debug/] [debug_unit.c] - Rev 479
Go to most recent revision | Compare with Previous | Blame | View Log
/* debug_unit.c -- Simulation of Or1k debug unit Copyright (C) 2001 Chris Ziomkowski, chris@asics.ws This file is part of OpenRISC 1000 Architectural Simulator. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* This is an architectural level simulation of the Or1k debug unit as described in OpenRISC 1000 System Architecture Manual, v. 0.1 on 22 April, 2001. This unit is described in Section 13. Every attempt has been made to be as accurate as possible with respect to the registers and the behavior. There are no known limitations at this time. */ #define DEBUG_JTAG 1 #include <stdlib.h> #include <stdio.h> #include <string.h> #include <inttypes.h> #include "debug_unit.h" #include "sim-config.h" #include "except.h" #include "arch.h" #include "abstract.h" #include "parse.h" #include "trace.h" #include "sprs.h" #include "../gdb.h" #include "../cpu/or1k/except.h" #include "opcode/or32.h" DevelopmentInterface development; /* External STALL signal to debug interface */ int in_reset = 0; /* Risc stall state */ int cpu_stalled = 0; /* Current watchpoint state */ unsigned long watchpoints = 0; static int calculate_watchpoints (); static int watchpoint[10]; void set_stall_state(int state) { development.riscop &= ~RISCOP_STALL; development.riscop |= state ? RISCOP_STALL : 0; if(testsprbits(SPR_DMR1, SPR_DMR1_DXFW)) /* If debugger disabled */ state = 0; cpu_stalled = state; } void du_reset() { development.riscop = 0; set_stall_state (0); } inline int CheckDebugUnit(DebugUnitAction action, unsigned long udata) { /* Do not stop, if we have debug module disabled or during reset */ if(!config.debug.enabled || in_reset) return 0; /* If we're single stepping, always stop */ if(action == DebugInstructionFetch && testsprbits (SPR_DMR1, SPR_DMR1_ST)) return 1; return 0; /* TODO: Enable matchpoints switch(action) { case DebugInstructionFetch: condition = DCR_CT_InsnAddress; break; case DebugLoadAddress: condition = DCR_CT_LoadAddress; break; case DebugStoreAddress: condition = DCR_CT_StoreAddress; break; case DebugLoadData: condition = DCR_CT_LoadData; break; case DebugStoreData: condition = DCR_CT_StoreData; break; } return calculate_watchpoints(); */ } /* Checks whether we should stall the RISC or cause an exception */ static int calculate_watchpoints() { int breakpoint = 0; int i, bit; /* Hopefully this loop would be unrolled run at max. speed */ for(i = 0, bit = 1; i < 11; i++, bit <<= 1) { int chain1, chain2; int match = 0; int DCR_hit = 0; /* Calculate first 8 matchpoints, result is put into DCR_hit */ if (i < 8) { unsigned long dcr = mfspr (SPR_DCR(i)); /* Whether this matchpoint is enabled, calculate conditions */ if ((dcr & SPR_DCR_DP) && (dcr & SPR_DCR_CT != SPR_DCR_CT_DISABLED)) { /* Get one operand */ unsigned long op1; unsigned long op2 = mfspr (SPR_DVR(i)); switch (dcr & SPR_DCR_CT) { case SPR_DCR_CT_IFEA: op1 = runtime.cpu.ifea; break; case SPR_DCR_CT_LEA: op1 = runtime.cpu.lea; break; case SPR_DCR_CT_SEA: op1 = runtime.cpu.sea; break; case SPR_DCR_CT_LD: op1 = runtime.cpu.ld; break; case SPR_DCR_CT_SD: op1 = runtime.cpu.sd; break; case SPR_DCR_CT_LSEA: op1 = runtime.cpu.lsea; break; } /* Perform signed comparison? */ if (dcr & SPR_DCR_SC) { long sop1 = op1, sop2 = op2; /* Convert to signed */ switch(dcr & SPR_DCR_CC) { case SPR_DCR_CC_MASKED: DCR_hit = sop1 & sop2; break; case SPR_DCR_CC_EQUAL: DCR_hit = sop1 == sop2; break; case SPR_DCR_CC_NEQUAL: DCR_hit = sop1 != sop2; break; case SPR_DCR_CC_LESS: DCR_hit = sop1 < sop2; break; case SPR_DCR_CC_LESSE: DCR_hit = sop1 <= sop2; break; case SPR_DCR_CC_GREAT: DCR_hit = sop1 > sop2; break; case SPR_DCR_CC_GREATE: DCR_hit = sop1 >= sop2; break; } } else { switch(dcr & SPR_DCR_CC) { case SPR_DCR_CC_MASKED: DCR_hit = op1 & op2; break; case SPR_DCR_CC_EQUAL: DCR_hit = op1 == op2; break; case SPR_DCR_CC_NEQUAL: DCR_hit = op1 != op2; break; case SPR_DCR_CC_LESS: DCR_hit = op1 < op2; break; case SPR_DCR_CC_LESSE: DCR_hit = op1 <= op2; break; case SPR_DCR_CC_GREAT: DCR_hit = op1 > op2; break; case SPR_DCR_CC_GREATE: DCR_hit = op1 >= op2; break; } } } } /* Chain matchpoints */ switch(i) { case 0: chain1 = chain2 = DCR_hit; break; case 8: chain1 = getsprbits (SPR_DWCR0, SPR_DWCR_COUNT) == getsprbits (SPR_DWCR0, SPR_DWCR_MATCH); chain2 = watchpoints & (1 << 7); break; case 9: chain1 = getsprbits (SPR_DWCR1, SPR_DWCR_COUNT) == getsprbits (SPR_DWCR1, SPR_DWCR_MATCH); chain2 = watchpoints & (1 << 8); break; case 10: /* TODO: External watchpoint - not yet handled! */ #if 0 chain1 = external_watchpoint; chain2 = watchpoints & (1 << 9); #else chain1 = chain2 = 0; #endif break; default: chain1 = DCR_hit; chain2 = watchpoints & (bit >> 1); break; } switch(getsprbits (SPR_DMR1, SPR_DMR1_CW0 << i)) { case 0: match = chain1; break; case 1: match = chain1 && chain2; break; case 2: match = chain1 || chain2; break; default: break; } if(match & !(watchpoints & bit)) { int counter = (getsprbits (SPR_DMR2, SPR_DMR2_AWTC) & bit) ? 1 : 0; int enabled = counter ? getsprbits (SPR_DMR2, SPR_DMR2_WCE1) : getsprbits (SPR_DMR2, SPR_DMR2_WCE0); if(enabled) setsprbits (SPR_DWCR0 + counter, SPR_DWCR_COUNT, getsprbits (SPR_DWCR0 + counter, SPR_DWCR_COUNT) + 1); if(getsprbits (SPR_DMR2, SPR_DMR2_WGB) & bit) breakpoint = 1; } watchpoints &= ~bit; watchpoints |= bit; } return breakpoint; } static DebugScanChainIDs current_scan_chain = JTAG_CHAIN_GLOBAL; int DebugGetRegister(unsigned int address, unsigned long* data) { int err; #ifdef DEBUG_JTAG printf("Debug get register %x\n",address); fflush(stdout); #endif switch(current_scan_chain) { case JTAG_CHAIN_DEBUG_UNIT: *data = mfspr (address); debug (2, "READ (%08x) = %08x\n", address, *data); break; case JTAG_CHAIN_TRACE: *data = 0; /* Scan chain not yet implemented */ err = 0; break; case JTAG_CHAIN_DEVELOPMENT: err = get_devint_reg(address,data); break; case JTAG_CHAIN_WISHBONE: err = GetWishboneMemory(address,data); break; } #ifdef DEBUG_JTAG printf("!get reg %x\n", *data); fflush(stdout); #endif return err; } int DebugSetRegister(unsigned int address,unsigned long data) { int err; #ifdef DEBUG_JTAG printf("Debug set register %x <- %x\n", address, data); fflush(stdout); #endif switch(current_scan_chain) { case JTAG_CHAIN_DEBUG_UNIT: debug (2, "WRITE (%08x) = %08x\n", address, data); mtspr(address, data); break; case JTAG_CHAIN_TRACE: err = JTAG_PROXY_ACCESS_EXCEPTION; break; case JTAG_CHAIN_DEVELOPMENT: err = set_devint_reg (address, data); break; case JTAG_CHAIN_WISHBONE: err = debug_set_mem (address, data); break; } #ifdef DEBUG_JTAG printf("!set reg\n"); fflush(stdout); #endif return err; } int DebugSetChain(int chain) { #ifdef DEBUG_JTAG printf("Debug set chain %x\n",chain); fflush(stdout); #endif switch(chain) { case JTAG_CHAIN_DEBUG_UNIT: case JTAG_CHAIN_TRACE: case JTAG_CHAIN_DEVELOPMENT: case JTAG_CHAIN_WISHBONE: current_scan_chain = chain; break; default: /* All other chains not implemented */ return JTAG_PROXY_INVALID_CHAIN; } #ifdef DEBUG_JTAG printf("!set chain\n"); fflush(stdout); #endif return 0; } void sim_reset (); /* Sets development interface register */ int set_devint_reg(unsigned int address, unsigned long data) { int err = 0; unsigned long value = data; int old_value; switch(address) { case DEVELOPINT_MODER: development.moder = value; break; case DEVELOPINT_TSEL: development.tsel = value; break; case DEVELOPINT_QSEL: development.qsel = value; break; case DEVELOPINT_SSEL: development.ssel = value; break; case DEVELOPINT_RISCOP: old_value = (development.riscop & RISCOP_RESET) != 0; development.riscop = value; in_reset = (development.riscop & RISCOP_RESET) != 0; /* Reset the cpu on the negative edge of RESET */ if(old_value && !in_reset) sim_reset(); /* Reset all units */ set_stall_state((development.riscop & RISCOP_STALL) != 0); break; case DEVELOPINT_RECWP0: case DEVELOPINT_RECWP1: case DEVELOPINT_RECWP2: case DEVELOPINT_RECWP3: case DEVELOPINT_RECWP4: case DEVELOPINT_RECWP5: case DEVELOPINT_RECWP6: case DEVELOPINT_RECWP7: case DEVELOPINT_RECWP8: case DEVELOPINT_RECWP9: case DEVELOPINT_RECWP10: development.recwp[address - DEVELOPINT_RECWP0] = value; break; case DEVELOPINT_RECBP0: development.recbp = value; break; default: err = JTAG_PROXY_INVALID_ADDRESS; break; } #ifdef DEBUG_JTAG printf("set_devint_reg %08x = %08x\n", address, data); fflush(stdout); #endif return err; } int get_devint_reg(unsigned int address,unsigned long *data) { int err = 0; unsigned long value = 0; switch(address) { case DEVELOPINT_MODER: value = development.moder; break; case DEVELOPINT_TSEL: value = development.tsel; break; case DEVELOPINT_QSEL: value = development.qsel; break; case DEVELOPINT_SSEL: value = development.ssel; break; case DEVELOPINT_RISCOP: value = development.riscop; break; case DEVELOPINT_RECWP0: case DEVELOPINT_RECWP1: case DEVELOPINT_RECWP2: case DEVELOPINT_RECWP3: case DEVELOPINT_RECWP4: case DEVELOPINT_RECWP5: case DEVELOPINT_RECWP6: case DEVELOPINT_RECWP7: case DEVELOPINT_RECWP8: case DEVELOPINT_RECWP9: case DEVELOPINT_RECWP10: value = development.recwp[address - DEVELOPINT_RECWP0]; break; case DEVELOPINT_RECBP0: value = development.recbp; break; default: err = JTAG_PROXY_INVALID_ADDRESS; break; } #ifdef DEBUG_JTAG printf("get_devint_reg %08x = %08x\n", address, value); fflush(stdout); #endif *data = value; return err; } /* Writes to bus address */ int debug_set_mem (unsigned int address,unsigned long data) { int err = 0; debug (2, "MEMWRITE (%08x) = %08x\n", address, data); if(!verify_memoryarea(address)) err = JTAG_PROXY_INVALID_ADDRESS; else { unsigned char t_data[4]; int *tmp = (int*)t_data; extern char null_str[1]; /* From cpu/common/parse.c */ int i; *tmp = htonl(data); /* We have already converted to host order */ setsim_mem8(address++, t_data[0]); /* Back to network byte order */ setsim_mem8(address++, t_data[1]); setsim_mem8(address++, t_data[2]); setsim_mem8(address++, t_data[3]); } return err; } int GetWishboneMemory(unsigned int address,unsigned long *data) { int err = 0; if(!verify_memoryarea(address)) err = JTAG_PROXY_INVALID_ADDRESS; else { unsigned char t_data[4]; int *tmp = (int*)t_data; int bp; t_data[0] = evalsim_mem8(address++); t_data[1] = evalsim_mem8(address++); t_data[2] = evalsim_mem8(address++); t_data[3] = evalsim_mem8(address++); /* Already in network byte order */ *data = ntohl(*tmp); /* But we assume it is in host order later */ } debug (2, "MEMREAD (%08x) = %08x\n", address, *data); return err; } /* debug_ignore_exception returns 1 if the exception should be ignored. */ int debug_ignore_exception (unsigned long except) { int result = 0; unsigned long dsr = mfspr (SPR_DSR); unsigned long drr = mfspr (SPR_DRR); &debug_ignore_exception; printf ("0x%08x 0x%08x \n", dsr, drr); switch(except) { case EXCEPT_RESET: drr |= result = dsr & SPR_DSR_RSTE; break; case EXCEPT_BUSERR: drr |= result = dsr & SPR_DSR_BUSEE; break; case EXCEPT_DPF: drr |= result = dsr & SPR_DSR_DPFE; break; case EXCEPT_IPF: drr |= result = dsr & SPR_DSR_IPFE; break; case EXCEPT_LPINT: drr |= result = dsr & SPR_DSR_LPINTE; break; case EXCEPT_ALIGN: drr |= result = dsr & SPR_DSR_AE; break; case EXCEPT_ILLEGAL: drr |= result = dsr & SPR_DSR_IIE; break; case EXCEPT_HPINT: drr |= result = dsr & SPR_DSR_LPINTE; break; case EXCEPT_DTLBMISS: drr |= result = dsr & SPR_DSR_DME; break; case EXCEPT_ITLBMISS: drr |= result = dsr & SPR_DSR_IME; break; case EXCEPT_RANGE: drr |= result = dsr & SPR_DSR_RE; break; case EXCEPT_SYSCALL: drr |= result = dsr & SPR_DSR_SCE; break; case EXCEPT_TRAP: drr |= result = dsr & SPR_DSR_TE; break; default: break; } printf ("0x%08x 0x%08x %i\n", dsr, drr, result); mtspr (SPR_DRR, drr); set_stall_state (result != 0); return (result != 0); }
Go to most recent revision | Compare with Previous | Blame | View Log