# -------------------------------------------------------------------
# Target configuration and (session) initialization.
# Requires global definitions of CORENAME and NUMCORES.
# -------------------------------------------------------------------

# set CORENAME if not defined
if {![info exists CORENAME]} {
  set CORENAME neorv32
}

# set NUMCORES if not defined
if {![info exists NUMCORES]} {
  set NUMCORES 1
}

# configures JTAG tap
jtag newtap $CORENAME cpu -irlen 5

# attach core(s)
if { $NUMCORES == 1 } {
  echo "Target: $CORENAME single-core"
  set TARGETNAME $CORENAME.cpu
  target create $TARGETNAME riscv -chain-position $TARGETNAME
} elseif { $NUMCORES == 2 } {
  echo "Target: $CORENAME SMP dual-core"
  set TARGETNAME_0 $CORENAME.cpu0
  set TARGETNAME_1 $CORENAME.cpu1
  target create $TARGETNAME_0 riscv -chain-position $CORENAME.cpu -rtos hwthread
  target create $TARGETNAME_1 riscv -chain-position $CORENAME.cpu -coreid 1
  target smp $TARGETNAME_0 $TARGETNAME_1
} else {
  echo "ERROR: Invalid NUMCORES value ($NUMCORES)."
  exit
}

# GDB server configuration
##gdb report_data_abort enable
##gdb report_register_access_error enable

# expose NEORV32-specific CSRs
riscv expose_csrs 4032=mxisa
riscv expose_csrs 4033=mxisah

# initialize target
init
