; --------------------------------------------------------------------------------
; @Title: Tracing Vector Timing Hooks via SRI Trace
; @Description: This script sets up MCDS to trace the events
;   generated by the TRACE32 specific instrumentation of the
;   Vector Timing Hooks (VTH). In particular, tracing of the
;   variables trace32_runnable/arti_rte_trace and
;   trace32_taskevent/arti_os_trace is set.
;
; @Keywords: AURIX, AURIX2G, Infineon, multi-core, TriCore
; @Author: RAI
; @Chip: TC2*, TC3*
; @Copyright: (C) 1989-2020 Lauterbach GmbH, licensed for use with TRACE32(R) only
; @Props: NoWelcome, NoIndex
; --------------------------------------------------------------------------------
; $Id: vth_sri_trace.cmm 14610 2019-07-10 09:37:00Z meick $

LOCAL &Addr_runnable &AddrRange_runnable
LOCAL &Addr_taskevent &AddrRange_taskevent
LOCAL &runnableexists

&runnableexists=FALSE()

;Check TRACE32 Software version
IF CPUFAMILY()!="TRICORE"
(
  PRINT %ERROR "Only for TriCore devices!"
  ENDDO
)

;Check TRACE32 Software version
IF VERSION.BUILD()<116064.
(
  PRINT %ERROR "Please update your TRACE32 software version!"
  ENDDO
)

; Check if variables exist
IF !Var.EXIST(trace32_taskevent)
(
  IF !Var.EXIST(arti_os_trace)
  (
    PRINT %ERROR "Variable trace32_taskevent or arti_os_trace does not exist!"
    ENDDO
  )
)

IF Var.EXIST(trace32_runnable)||Var.EXIST(arti_rte_trace)
(
  &runnableexists=TRUE()
)

IF Var.EXIST(trace32_taskevent)
(
  &Addr_taskevent=Var.ADDRESS(trace32_taskevent)
  &Addr_taskevent=ADDRESS.OFFSET(&Addr_taskevent)
  &AddrRange_taskevent=Var.RANGE(trace32_taskevent)
)
ELSE
(
  &Addr_taskevent=Var.ADDRESS(arti_os_trace)
  &Addr_taskevent=ADDRESS.OFFSET(&Addr_taskevent)
  &AddrRange_taskevent=Var.RANGE(arti_os_trace)
)

IF Var.EXIST(trace32_runnable)
(
  &Addr_runnable=Var.ADDRESS(trace32_runnable)
  &Addr_runnable=ADDRESS.OFFSET(&Addr_runnable)
  &AddrRange_runnable=Var.RANGE(trace32_runnable)
)
ELSE IF Var.EXIST(arti_rte_trace)
(
  &Addr_runnable=Var.ADDRESS(arti_rte_trace)
  &Addr_runnable=ADDRESS.OFFSET(&Addr_runnable)
  &AddrRange_runnable=Var.RANGE(arti_rte_trace)
)


; Check if MCDS module is available
IF MCDS.Module.NAME()=="<none>"
(
  PRINT %ERROR "No Trace (MCDS) available!"
  ENDDO
)

; Clear all Trace Breakpoints
Break.Delete /TraceEnable 
Break.Delete /TraceData 
Break.Delete /TraceON 
Break.Delete /TraceOFF 
Break.Delete /WATCH 
Break.Delete /TraceTrigger 

; Reset MCDS and clear all sources
MCDS.RESet
MCDS.SOURCE.NONE

IF CPUIS(TC3*)
(
  IF MCDS.Module.NAME()=="MCDS"
  (
    ; OLDA starts at 0x8FE00000 (cached) and 0xAFE00000 (uncached)
    ; LMU SRAM starts at 0x90000000 (cached) and 0xB0000000 (uncached)
    IF (&Addr_taskevent&0xDFF00000)==0x8FE00000
      MCDS.SOURCE.Set SRI.1.SLAVE OLDA
    ELSE IF (&Addr_taskevent&0xDF000000)==0x90000000
      MCDS.SOURCE.Set SRI.1.SLAVE LMU0
    ELSE
      PRINT %ERROR "Wrong memory used"

    MCDS.Set.SRI EAddr0_1 &AddrRange_taskevent
    MCDS.Set.SRI ACCess0_1 /Write
    MCDS.Set.SRI EVT0 EAddr0_1
    MCDS.Set.SRI EVT0 ACCess0_1
    MCDS.Set.SRI ACT DTU_WDAT1 aisAUTO EVT0
    MCDS.Set.SRI ACT DTU_WADR1 aisAUTO EVT0

    ; TimeStamp Setting
    MCDS.Set.MCX ACT TSUR_EN aisAUTO SYNC_RQ /LeVel /Always
    MCDS.Set.SRI ACT TSUR_TSR aisAUTO EVT0 /Normal /LeVel

    if &runnableexists==TRUE()
    (
      MCDS.Set.SRI EAddr1_1 &AddrRange_runnable
      MCDS.Set.SRI ACCess1_1 /Write
      MCDS.Set.SRI EVT1 EAddr1_1
      MCDS.Set.SRI EVT1 ACCess1_1
      MCDS.Set.SRI ACT DTU_WDAT1 aisAUTO EVT1
      MCDS.Set.SRI ACT DTU_WADR1 aisAUTO EVT1

      ; TimeStamp Setting
      MCDS.Set.SRI ACT TSUR_TSR aisAUTO EVT1 /Normal /LeVel
    )
  )
  ELSE IF MCDS.Module.NAME()=="MCDSlight"
  (
    ; OLDA starts at 0x8FE00000 (cached) and 0xAFE00000 (uncached)
    ; LMU SRAM starts at 0x90000000 (cached) and 0xB0000000 (uncached)
    IF (&Addr_taskevent&0xDFF00000)==0x8FE00000
      MCDS.SOURCE.Set CpuMux0.Core OLDA
    ELSE IF (&Addr_taskevent&0xDF000000)==0x90000000
      MCDS.SOURCE.Set CpuMux0.Core LMU0
    ELSE
      PRINT %ERROR "Wrong memory used"

    MCDS.Set.CpuMux0 EAddr0 &AddrRange_taskevent
    MCDS.Set.CpuMux0 ACCess0 /Write
    MCDS.Set.CpuMux0 EVT0 EAddr0
    MCDS.Set.CpuMux0 EVT0 ACCess0
    MCDS.Set.CpuMux0 ACT DTU_WDAT aisAUTO EVT0
    MCDS.Set.CpuMux0 ACT DTU_WADR aisAUTO EVT0

    ; TimeStamp Setting
    MCDS.Set.MCX ACT TSUR_EN aisAUTO SYNC_RQ /LeVel /Always
    MCDS.Set.CpuMux0 ACT TSUR_TSR aisAUTO EVT0 /Normal /LeVel

    if &runnableexists==TRUE()
    (
      MCDS.Set.CpuMux0 EAddr1 &AddrRange_runnable
      MCDS.Set.CpuMux0 ACCess1 /Write
      MCDS.Set.CpuMux0 EVT12 EAddr1
      MCDS.Set.CpuMux0 EVT12 ACCess1
      MCDS.Set.CpuMux0 ACT DTU_WDAT aisAUTO EVT12
      MCDS.Set.CpuMux0 ACT DTU_WADR aisAUTO EVT12

      ; TimeStamp Setting
      MCDS.Set.CpuMux0 ACT TSUR_TSR aisAUTO EVT12 /Normal /LeVel
    )
  )
  ELSE IF MCDS.Module.NAME()=="miniMCDS"
  (
    ; OLDA starts at 0x8FE00000 (cached) and 0xAFE00000 (uncached)
    ; LMU SRAM starts at 0x90000000 (cached) and 0xB0000000 (uncached)
    IF (&Addr_taskevent&0xDFF00000)==0x8FE00000
      MCDS.SOURCE.Set CpuMux0.Core OLDA
    ELSE IF (&Addr_taskevent&0xDF000000)==0x90000000
      MCDS.SOURCE.Set CpuMux0.Core LMU0
    ELSE
      PRINT %ERROR "Wrong memory used"

    MCDS.Set.CpuMux0 EAddr0 &AddrRange_taskevent
    MCDS.Set.CpuMux0 ACCess0 /Write
    MCDS.Set.MCX EVT8 EAddr0
    MCDS.Set.MCX EVT8 ACCess0
    MCDS.Set.MCX ACT DTU_WDAT aisAUTO EVT8
    MCDS.Set.MCX ACT DTU_WADR aisAUTO EVT8

    ; TimeStamp Setting
    MCDS.Set.MCX ACT TSUR_EN aisAUTO EVT8 /LeVel /Normal

    if &runnableexists==TRUE()
    (
      MCDS.Set.CpuMux0 EAddr1 &AddrRange_runnable
      MCDS.Set.CpuMux0 ACCess1 /Write
      MCDS.Set.MCX EVT10 EAddr1
      MCDS.Set.MCX EVT10 ACCess1
      MCDS.Set.MCX ACT DTU_WDAT aisAUTO EVT10
      MCDS.Set.MCX ACT DTU_WADR aisAUTO EVT10

      ; TimeStamp Setting
      MCDS.Set.MCX ACT TSUR_EN aisAUTO EVT10 /LeVel /Normal
    )
  )
)
ELSE IF CPUIS(TC2*)
(
  IF MCDS.Module.NAME()=="MCDS"
  (
    ; OLDA starts at 0x8FE70000 (cached) and 0xAFE70000 (uncached)
    ; LMU SRAM starts at 0x90000000 (cached) and 0xB0000000 (uncached)
    IF ((&Addr_taskevent&0xDFFF0000)!=0x8FE70000)&&((&Addr_taskevent&0xDFFF0000)!=0x90000000)
      PRINT %ERROR "Wrong memory used"

    MCDS.SOURCE.Set SRI.2.SLAVE LMU

    MCDS.Set.SRI EAddr0_2 &AddrRange_taskevent
    MCDS.Set.SRI ACCess0_2 /Write
    MCDS.Set.SRI EVT4 EAddr0_2
    MCDS.Set.SRI EVT4 ACCess0_2
    MCDS.Set.SRI ACT DTU_WDAT2 aisAUTO EVT4
    MCDS.Set.SRI ACT DTU_WADR2 aisAUTO EVT4

    ; TimeStamp Setting
    MCDS.Set.MCX ACT TSUR_EN aisAUTO SYNC_RQ /LeVel /Always
    MCDS.Set.SRI ACT TSUR_TSR aisAUTO EVT4 /Normal /LeVel

    if &runnableexists==TRUE()
    (
      MCDS.Set.SRI EAddr1_2 &AddrRange_runnable
      MCDS.Set.SRI ACCess1_2 /Write
      MCDS.Set.SRI EVT5  EAddr1_2
      MCDS.Set.SRI EVT5 ACCess1_2
      MCDS.Set.SRI ACT DTU_WDAT2 aisAUTO EVT5
      MCDS.Set.SRI ACT DTU_WADR2 aisAUTO EVT5

      ; TimeStamp Setting
      MCDS.Set.SRI ACT TSUR_TSR aisAUTO EVT5 /Normal /LeVel
    )
  )
  ELSE
  (
    PRINT %ERROR "MiniMCDS is not suitable for slave trace"
  )
)
ELSE
(
  PRINT %ERROR "Script only for AURIX or AURIX2g"
)

; global TimeStamp Setting
MCDS.CLOCK.TimeStamp Relative
CLOCK.ON

ENDDO
