;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Allow run time access for J784S4 TCMA/TCMB
; 14 Jun 22 - Richard Woodruff - Adapt J721S2 for J784S4.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Script parameters: LOCK or SPLIT

PRIVATE &parameters
ENTRY %LINE &parameters
&parameters=STRing.UPpeR("&parameters")
LOCAL &param_lock &smult &badd &bmult &size &apbase &bpbase

&param_lock=(STRing.SCAN("&parameters","LOCK",0)!=-1)
IF &param_lock
        &smult=2   ; run time address only visible by dmsc so use param
ELSE
        &smult=1

IF !CPUIS("*CR5-*")
(
	PRINT %ERROR "Please select R5 CPU for J784S4 Exiting ..."
	ENDDO "FALSE()"
)
IF !SYStem.Up()
(
	PRINT %ERROR "SYStem.Mode must be Up! Exiting..."
	ENDDO "FALSE()"
)

; Each R5 has ATCM=32K + BTCM=32K in split mode
; In lock step mode the single safety core uses the total memory so ATCM=64K + BTCM=64K

IF CORE.ISASSIGNED(1.)&&CORE.ISASSIGNED(2.)
(
        PRINT %WARNING "TCM DAPREMAP is not supported in SMP debug mode"
        ENDDO "FALSE()"
)
IF CORE.ISASSIGNED(2.)&&(&param_lock)
(
        PRINT %WARNING "LOCK step is not possible for core.assign 2, use core.assign 1"
        ENDDO "FALSE()"
)

IF CORE.ISASSIGNED(1.)
        &badd=0
ELSE
        &badd=0x100000

IF CPUIS("*CR5-MCU*")
(
        &apbase=0x41000000
        &bpbase=0x41010000
        &size=(1024.*32.)*&smult-1
        &bmult=4
)
IF CPUIS("*CR5-MAIN0*")
(
        &apbase=0x05C00000
        &bpbase=0x05C10000
        &size=(1024.*32.)*&smult-1
        &bmult=1
)
IF CPUIS("*CR5-MAIN1*")
(
        &apbase=0x05E00000
        &bpbase=0x05E10000
        &size=(1024.*32.)*&smult-1
        &bmult=1
)
IF CPUIS("*CR5-MAIN2*")
(
        &apbase=0x05900000
        &bpbase=0x05910000
        &size=(1024.*32.)*&smult-1
        &bmult=1
)

; Remap TCMA and TCMB, can remap up to 16 ranges
SYStem.MemAccess.DAP
SYStem.Option DAPREMAP 0x0++&size (&apbase+(&badd*&bmult)) 0x41010000++&size (&bpbase+(&badd*&bmult))

ENDDO "TRUE()"
