; --------------------------------------------------------------------------------
; @Title: Windriver register definition (.reg) to PRACTICE converter
; @Description:
;   This script parses a windriver register definition files and converts
;   all lines starting with "SCT" or "SC" to PER.Set commands. All other lines
;   are ignored.
; @Keywords: converter, script, windriver
; @Author: DIE
; @Copyright: (C) 1989-2016 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: sct2practice.cmm 10561 2017-03-15 18:26:17Z rweiss $

;Configure parameters
LOCAL &cf_tar &cpufamily
&cf_tar=""
&cpufamily=""

; ask for file to convert
LOCAL &input_file
LOCAL &ouput_file
DIALOG.FILE "*.reg"
ENTRY %LINE &input_file
IF "&input_file"==""
  ENDDO
&ouput_file="&input_file.cmm"

; open / create files
OPEN #1 "&input_file" /Read
OPEN #2 "&ouput_file" /Create /Write

; write header into output file
WRITE #2 "; Generated "+CLOCK.DATE()+", "+CLOCK.TIME()
WRITE #2 "; source file: &input_file"
WRITE #2 ""

LOCAL &linecount &errorcount
&linecount=0
&errorcount=0

; parse input file
RePeaT
(
  LOCAL &command &readLine &result
  READ #1 &command %LINE &readLine
  &linecount=&linecount+1
  &command=CONVert.TOUPPER("&command")
  &readLine=CONVert.TOUPPER(STRing.TRIM("&readLine"))
  &result=""
  
  ON ERROR GOSUB
  (
    LOCAL &lstr
    &lstr=FORMAT.DECIMAL(1.,&linecount)
    WRITE #2 "STOP ""SYNTAX ERROR IN LINE &lstr"""
    CLOSE #1
    CLOSE #2
    DIALOG.OK "Syntax error found in line &lstr"
    ENDDO
  )
  
  IF (("&command"=="SCT")||("&command"=="SC"))
  (
    GOSUB Decode_SCT &readLine
    ENTRY %LINE &result
  )
  ELSE IF "&command"=="CF"
  (
    GOSUB Decode_CF &readLine
    ENTRY %LINE &result
  )
  ELSE
  (
    ; insert as comment
    &result="; &command &readLine"
  )
  IF "&result"!=""
    WRITE #2 "&result"
)
WHILE !FILE.EOF(1)

CLOSE #1

WRITE #2 "ENDDO"
CLOSE #2

if &errorcount!=0
  DIALOG.OK "Script conversion completed with errors."
else
  PRINT "Script converted successfully"

PEDIT "&ouput_file"
ENDDO

; --------------------------------------------------------------------------------
; subroutines
; --------------------------------------------------------------------------------

; --------------------------------------------------------------------------------
; CF
; --------------------------------------------------------------------------------
Decode_CF:
  LOCAL &param &value1 &rest &cmd
  ENTRY &param &value1 %LINE &rest
  IF "&param"=="TAR"
  (
    &cf_tar="&value1"
    &cmd="; SYStem.CPU &value1"

    IF CPUFAMILY()=="POWERPC"
    (
      IF (STRing.MID("&cf_tar",0,1)=="8")
      (
        IF !((STRing.MID("&cf_tar",3,1)>="0")&&(STRing.MID("&cf_tar",3,1)<="9"))
          &cpufamily="MPC8XX"
        ELSE IF STRing.MID("&cf_tar",0,2)=="82"
          &cpufamily="MPC82XX"
        ELSE IF STRing.MID("&cf_tar",0,2)=="83"
          &cpufamily="MPC83XX"
        ELSE IF STRing.MID("&cf_tar",0,2)=="85"
          &cpufamily="MPC85XX"
        ELSE
          &cpufamily="MPC86XX"
      )
      IF (STRing.MID("&cf_tar",0,1)=="7")
      (
        IF !((STRing.MID("&cf_tar",3,1)>="0")&&(STRing.MID("&cf_tar",3,1)<="9"))
          &cpufamily="MPC7XX"
        ELSE
          &cpufamily="MPC74XX"
      )
    )
  )
  RETURN &cmd
  
; --------------------------------------------------------------------------------
; SCT/SC
; --------------------------------------------------------------------------------
Decode_SCT:
  LOCAL &grp &rname &addr &value &options
  ENTRY &grp &addr &value %LINE &options
  &rname=""
 
  ; additional handling for MPC8xx
  IF "&cpufamily"=="MPC8XX"
  (
    ; check for IMMR
    if "&grp"=="IMMR"
    (
      &cmd="PER.Set SPR:0x27E %Long 0x&value              ; &grp"
      RETURN &cmd
    )
  )

  ;access width
  LOCAL &awidth &spacer
  IF STRing.LENgth("&value")==2.
  (
    &awidth="%Byte"
    &spacer="              "
  )
  ELSE IF STRing.LENgth("&value")==4.
  (
    &awidth="%Word"
    &spacer="            "
  )
  ELSE IF STRing.LENgth("&value")==8.
  (
    &awidth="%Long"
    &spacer="        "
  )
  ELSE IF STRing.LENgth("&value")==16.
  (
    &awidth="%Quad"
    &spacer=""
  )
  ELSE
  (
    LOCAL &message  &lstr
    &lstr=FORMAT.DECIMAL(1.,&linecount)
    &message="STOP ""PARSER ERROR - unsupported data width in line &lstr"""
    PRINT %ERROR "&message"
    &errorcount=&errorcount+1
    RETURN &message
  )

  ; simple register
  LOCAL &cmd
  &cmd="PER.Set ANC:0x&addr &awidth 0x&value &spacer; &grp[&rname]"
  RETURN &cmd
