; --------------------------------------------------------------------------------
; @Title: Demo script for Z20K148M on EVB-ARDO-ZC20K148-QFP64-V03 Board (FLASH)
; @Description:
;   Programs the sieve demo application into the processor internal flash and
;   sets up a demo debug scenario.
;   This script can be used as a template for flashing an application.
; @Chip: Z20K148M

; @copyright : Copyright (c) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved.

; --------------------------------------------------------------------------------
LOCAL &param_cpu
&param_cpu = "Z20K148M"

; WinCLEAR

; --------------------------------------------------------------------------------
; initialize and start the debugger
RESet
SYStem.RESet
IF "&param_cpu"=="Z20K114M"||"&param_cpu"=="Z20K116M"||"&param_cpu"=="Z20K118M"
  SYStem.CPU CortexM0+
ELSE IF "&param_cpu"=="Z20K144M"||"&param_cpu"=="Z20K146M"||"&param_cpu"=="Z20K148M"
  SYStem.CPU CortexM4
ELSE
(
  PRINT %ERROR "Device is not supported!"
  ENDDO
)

IF VERSION.BUILD.BASE()<83714.
(
  SYStem.CONFIG ETM Base E:0xE0041000
)
SYStem.CONFIG.DEBUGPORTTYPE SWD
IF COMBIPROBE()||UTRACE()
(
  SYStem.CONFIG.CONNECTOR MIPI20T
)
SYStem.Option DUALPORT ON
SYStem.MemAccess DAP
SYStem.JtagClock CTCK 10MHz
Trace.DISable
SYStem.Up

; initialize sram for flash algorithm
GOSUB InitSram

; disable Watchdog
GOSUB DisableWatchdog

; --------------------------------------------------------------------------------
; Flash programming

; prepare flash programming (declarations)
DO ~~~~/Z20K CPU=&param_cpu

; ReProgram Flash
FLASH.ReProgram ALL
Data.LOAD.auto ~~~~/../Build/Bin/X01Hq008_App_default/X01Hq008_App_default_ALL_H.hex /noclear
Data.LOAD.Elf ~~~~/../Build/Bin/X01Hq008_App_default/X01Hq008_App_default.elf
FLASH.ReProgram OFF

; --------------------------------------------------------------------------------
; Reset the target again
SYStem.Up

GOSUB DisableWatchdog

; --------------------------------------------------------------------------------
; start program execution
Go.direct main
WAIT !STATE.RUN()

; --------------------------------------------------------------------------------
; open some windows
; WinCLEAR
; Mode.Hll
; WinPOS 0. 0. 116. 26.
; List.auto
; WinPOS 120. 0. 100. 8.
; Frame.view
; WinPOS 120. 14.
; Var.Watch
;V ar.AddWatch %SpotLight ast flags
; WinPOS 120. 25.
; Register.view /SpotLight
; WinPOS 0. 32.
; Var.DRAW %DEFault sinewave
; WinPOS 120. 25.

; Per.view ~~~~/&param_cpu/&param_cpu.per

ENDDO

; --------------------------------------------------------------------------------
InitSram:
(
  IF "&param_cpu"=="Z20K114M"
    Data.Set 0x20000000--0x20001FFF %long 0x0   ; init SRAM with 0
  ELSE IF "&param_cpu"=="Z20K116M"
    Data.Set 0x20000000--0x20003FFF %long 0x0   ; init SRAM with 0
  ELSE IF "&param_cpu"=="Z20K118M"
    Data.Set 0x1FFFC000--0x20003FFF %long 0x0   ; init SRAM with 0
  ELSE IF "&param_cpu"=="Z20K144M"
    Data.Set 0x20000000--0x2000FFFF %long 0x0   ; init SRAM with 0
  ELSE IF "&param_cpu"=="Z20K146M"
    Data.Set 0x20000000--0x2001FFFF %long 0x0   ; init SRAM with 0
  ELSE IF "&param_cpu"=="Z20K148M"
    Data.Set 0x1FFE0000--0x2001FFFF %long 0x0   ; init SRAM with 0
  ELSE
  (
    PRINT %ERROR "Device is not supported!"
    ENDDO
  )

  RETURN
)

DisableWatchdog:
(
  LOCAL &tmp1 &tmp2
  &tmp1=Data.Long(ST:0x20000000)
  &tmp2=Data.Long(ST:0x20000004)
  Register.SWAP
  Data.Assemble ST:0x20000000  str  r1,[r0]
  Data.Assemble ,              str  r3,[r2]
  Data.Assemble ,              str  r5,[r4]
  Data.Assemble ,              bkpt #0
  Register.Set PC 0x20000000
  Register.Set SP 0x20001000
  Register.Set R0 0x40052014
  Register.Set R1 0xB0D9A1C4
  Register.Set R2 0x40052014
  Register.Set R3 0x1A1E3B0F
  Register.Set R4 0x40052008
  Register.Set R5 0x01040040
  Go.direct
  WAIT !STATE.RUN()

  Data.Set ST:0x20000000 %Long &tmp1
  Data.Set ST:0x20000004 %Long &tmp2
  Register.SWAP

  RETURN
)

; Set source code file dir
PATH D:\zhangxun\0_GIT_File\ilcp_branch_275_c100intergratior