; --------------------------------------------------------------------------------
; @Title: ETM Programming Dialog
; @Description: Please refer to pdf/trace_arm_etm_dialog.pdf for more info
; @Props: NoIndex, NoWelcome, NoFullText
; @Author: -
; @Copyright: (C) 1989-2014 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: etmdialog.cmm 11204 2017-08-10 12:24:05Z smeister $


; --------------------------------------------------------------------------------
;                               Dialog Levelx
; --------------------------------------------------------------------------------

  IF WINdow.EXIST(_a_dialog)
    ENDDO

  LOCAL &DBoolean &DBinary &DHex &DInteger &DAscii &DString &DRange &DAddress &DARange &DTime &DTRange &DFloat &DNumeric &DIntNum &DBitmask &DEmpty
  LOCAL &spres &ret &adir &test &stateon &subdialog &version

  &version="Version 1.9.1: 05.08.2007"
  ;Test setzten damit cmm files alle aus einem verzeichnis genommen werden egal wo systemdir ist
  &test=0
  &DBoolean=0x00000001
  &DBinary=0x00000002
  &DHex=0x00000004
  &DInteger=0x00000008
  &DFloat=0x00000010
  &DAscii=0x00000020
  &DIntNum=0x0000002E
  &DNumeric=0x0000003E
  &DString=0x00000040
  &DRange=0x00000080
  &DAddress=0x00000100
  &DARange=0x00000200
  &DTime=0x00000400
  &DTRange=0x00000800
  &DBitmask=0x00004000
  &DEmpty=0x00008000
  &subdialog=0.
  &stateon=-1. ; Sobald Goto Levelx in Level0 benutzt wird hier die Zeile von Goto Levelx eintragen

  print ""
  winpos ,,,,,, _a_dialog
  dialog
  (
        header "ETM Programming"
	icon ":atrigger"

; --------------------------------------------------------------------------------
;                                Definition
; --------------------------------------------------------------------------------
        pos 0. 0. 83.
        line "Definition"
        pos 10. 1. 7.
        text "Address"
        pos 39. 1. 7.
        text "Address"
; --------------------------------------------------------------------------------
;                                A-Range
; --------------------------------------------------------------------------------
        pos 1. 2. 8.
        text "A-Range :"
        pos 10. 2. 25.
RA1:    EDIT "" "gosub CallCheckAddr A1"
        pos 35. 2. 2.
VA1:    BUTTON "V" "gosub CallVAddr A1"
        pos 37. 2. 2.
        text " or"
        pos 39. 2. 25.
RA2:    EDIT "" "gosub CallCheckAddr A2"
        pos 64. 2. 2.
VA2:    BUTTON "V" "gosub CallVAddr A2"
; --------------------------------------------------------------------------------
;                                B-Range
; --------------------------------------------------------------------------------
        pos 1. 3. 8.
        text "B-Range :"
        pos 10. 3. 25.
RB1:    EDIT "" "gosub CallCheckAddr B1"
        pos 35. 3. 2.
VB1:    BUTTON "V" "gosub CallVAddr B1"
        pos 37. 3. 2.
        text " or"
        pos 39. 3. 25.
RB2:    EDIT "" "gosub CallCheckAddr B2"
        pos 64. 3. 2.
VB2:    BUTTON "V" "gosub CallVAddr B2"
; --------------------------------------------------------------------------------
;                                C-Range
; --------------------------------------------------------------------------------
        pos 1. 4. 8.
        text "C-Range :"
        pos 10. 4. 25.
RC1:    EDIT "" "gosub CallCheckAddr C1"
        pos 35. 4. 2.
VC1:    BUTTON "V" "gosub CallVAddr C1"
        pos 37. 4. 2.
        text " or"
        pos 39. 4. 25.
RC2:    EDIT "" "gosub CallCheckAddr C2"
        pos 64. 4. 2.
VC2:    BUTTON "V" "gosub CallVAddr C2"
; --------------------------------------------------------------------------------
;                                Data0
; --------------------------------------------------------------------------------
        pos 8. 5.5 10.
        text "Value"
        pos 1. 6.5 6.5
        text "DATA0:"
        pos 7.5 6.5 20.
RD01:   EDIT "" "gosub CallCheckData D0"
        pos 27.5 6.5 2.
VD01:   BUTTON "V" "gosub CallVData D0"
        pos 30. 6.5 8.
RD02:   PULLDOWN "BYTE,WORD,LONG" "gosub CallCheckData D0"
; --------------------------------------------------------------------------------
;                                Data1
; --------------------------------------------------------------------------------
        pos 1. 7.5 6.5
        text "DATA1:"
        pos 7.5 7.5 20.
RD11:   EDIT "" "gosub CallCheckData D1"
        pos 27.5 7.5 2.
VD11:   BUTTON "V" "gosub CallVData D1"
        pos 30. 7.5 8.
RD12:   PULLDOWN "BYTE,WORD,LONG" "gosub CallCheckData D1"
; --------------------------------------------------------------------------------
;                                Count0
; --------------------------------------------------------------------------------
        pos 48.5 5.5 10.
        text "Value"
        pos 41. 6.5 7.5
        text "COUNT0:"
        pos 48.5 6.5 17.
EC0:    EDIT "" "gosub CallCheckCount C0"
        pos 65.5 6.5 2.
VC01:   BUTTON "V" "gosub CallVCount C0"
; --------------------------------------------------------------------------------
;                                Count1
; --------------------------------------------------------------------------------
        pos 41. 7.5 7.5
        text "COUNT1:"
        pos 48.5 7.5 17.
EC1:    EDIT "" "gosub CallCheckCount C1"
        pos 65.5 7.5 2.
VC11:   BUTTON "V" "gosub CallVCount C1"
; --------------------------------------------------------------------------------
;                                Level0
; --------------------------------------------------------------------------------
        pos 0. 9. 83.
        line "Level0"
        pos 1. 10. 10.
        text "Action"
        pos 21. 10. 13.
        text "Address/Range"
        pos 37. 10. 10.
        text "RD/WR..."
        pos 49. 10. 10.
        text "Data"
        pos 61. 10. 10.
        text "Count"
        pos 73. 10. 11.
        text "External"
; --------------------------------------------------------------------------------
;                                Level0-Z1
; --------------------------------------------------------------------------------
        pos 1. 11. 18.
S0A1:   PULLDOWN ",Trace,Trace (Global),TraceON (Global),TraceOFF (Global),View Data,Goto Level1,Goto Level2,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S0A1"
        pos 19. 11. 2.
        text "IF"
        pos 21. 11. 14.
S0AR1:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S0AR1"
        pos 35. 11. 2.
        text " &"
        pos 37. 11. 10.
S0WR1:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S0WR1"
        pos 47. 11. 2.
        text " &"
        pos 49. 11. 10.
S0D1:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S0D1"
        pos 59. 11. 2.
        text " &"
        pos 61. 11. 10.
S0C1:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S0C1"
        pos 71. 11. 2.
        text " &"
        pos 73. 11. 10.
S0E1:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level0-Z2
; --------------------------------------------------------------------------------
        pos 1. 12. 18.
S0A2:   PULLDOWN ",Trace,Trace (Global),TraceON (Global),TraceOFF (Global),View Data,Goto Level1,Goto Level2,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S0A2"
        pos 19. 12. 2.
        text "IF"
        pos 21. 12. 14.
S0AR2:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S0AR2"
        pos 35. 12. 2.
        text " &"
        pos 37. 12. 10.
S0WR2:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S0WR2"
        pos 47. 12. 2.
        text " &"
        pos 49. 12. 10.
S0D2:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S0D2"
        pos 59. 12. 2.
        text " &"
        pos 61. 12. 10.
S0C2:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S0C2"
        pos 71. 12. 2.
        text " &"
        pos 73. 12. 10.
S0E2:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level0-Z3
; --------------------------------------------------------------------------------
        pos 1. 13. 18.
S0A3:   PULLDOWN ",Trace,Trace (Global),TraceON (Global),TraceOFF (Global),View Data,Goto Level1,Goto Level2,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S0A3"
        pos 19. 13. 2.
        text "IF"
        pos 21. 13. 14.
S0AR3:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S0AR3"
        pos 35. 13. 2.
        text " &"
        pos 37. 13. 10.
S0WR3:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S0WR3"
        pos 47. 13. 2.
        text " &"
        pos 49. 13. 10.
S0D3:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S0D3"
        pos 59. 13. 2.
        text " &"
        pos 61. 13. 10.
S0C3:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S0C3"
        pos 71. 13. 2.
        text " &"
        pos 73. 13. 10.
S0E3:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level0-Z4
; --------------------------------------------------------------------------------
        pos 1. 14. 18.
S0A4:   PULLDOWN ",Trace,Trace (Global),TraceON (Global),TraceOFF (Global),View Data,Goto Level1,Goto Level2,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S0A4"
        pos 19. 14. 2.
        text "IF"
        pos 21. 14. 14.
S0AR4:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S0AR4"
        pos 35. 14. 2.
        text " &"
        pos 37. 14. 10.
S0WR4:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S0WR4"
        pos 47. 14. 2.
        text " &"
        pos 49. 14. 10.
S0D4:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S0D4"
        pos 59. 14. 2.
        text " &"
        pos 61. 14. 10.
S0C4:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S0C4"
        pos 71. 14. 2.
        text " &"
        pos 73. 14. 10.
S0E4:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level1
; --------------------------------------------------------------------------------
        pos 0. 15. 83.
        line "Level1"
; --------------------------------------------------------------------------------
;                                Level1-Z1
; --------------------------------------------------------------------------------
        pos 1. 16. 18.
S1A1:   PULLDOWN ",Trace,View Data,Goto Level0,Goto Level2,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S1A1"
        pos 19. 16. 2.
        text "IF"
        pos 21. 16. 14.
S1AR1:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S1AR1"
        pos 35. 16. 2.
        text " &"
        pos 37. 16. 10.
S1WR1:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S1WR1"
        pos 47. 16. 2.
        text " &"
        pos 49. 16. 10.
S1D1:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S1D1"
        pos 59. 16. 2.
        text " &"
        pos 61. 16. 10.
S1C1:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S1C1"
        pos 71. 16. 2.
        text " &"
        pos 73. 16. 10.
S1E1:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level1-Z2
; --------------------------------------------------------------------------------
        pos 1. 17. 18.
S1A2:   PULLDOWN ",Trace,View Data,Goto Level0,Goto Level2,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S1A2"
        pos 19. 17. 2.
        text "IF"
        pos 21. 17. 14.
S1AR2:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S1AR2"
        pos 35. 17. 2.
        text " &"
        pos 37. 17. 10.
S1WR2:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S1WR2"
        pos 47. 17. 2.
        text " &"
        pos 49. 17. 10.
S1D2:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S1D2"
        pos 59. 17. 2.
        text " &"
        pos 61. 17. 10.
S1C2:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S1C2"
        pos 71. 17. 2.
        text " &"
        pos 73. 17. 10.
S1E2:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level1-Z3
; --------------------------------------------------------------------------------
        pos 1. 18. 18.
S1A3:   PULLDOWN ",Trace,View Data,Goto Level0,Goto Level2,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S1A3"
        pos 19. 18. 2.
        text "IF"
        pos 21. 18. 14.
S1AR3:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S1AR3"
        pos 35. 18. 2.
        text " &"
        pos 37. 18. 10.
S1WR3:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S1WR3"
        pos 47. 18. 2.
        text " &"
        pos 49. 18. 10.
S1D3:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S1D3"
        pos 59. 18. 2.
        text " &"
        pos 61. 18. 10.
S1C3:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S1C3"
        pos 71. 18. 2.
        text " &"
        pos 73. 18. 10.
S1E3:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level2
; --------------------------------------------------------------------------------
        pos 0. 19. 83.
        line "Level2"
; --------------------------------------------------------------------------------
;                                Level2-Z1
; --------------------------------------------------------------------------------
        pos 1. 20. 18.
S2A1:   PULLDOWN ",Trace,View Data,Goto Level0,Goto Level1,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S2A1"
        pos 19. 20. 2.
        text "IF"
        pos 21. 20. 14.
S2AR1:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S2AR1"
        pos 35. 20. 2.
        text " &"
        pos 37. 20. 10.
S2WR1:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S2WR1"
        pos 47. 20. 2.
        text " &"
        pos 49. 20. 10.
S2D1:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S2D1"
        pos 59. 20. 2.
        text " &"
        pos 61. 20. 10.
S2C1:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S2C1"
        pos 71. 20. 2.
        text " &"
        pos 73. 20. 10.
S2E1:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level2-Z2
; --------------------------------------------------------------------------------
        pos 1. 21. 18.
S2A2:   PULLDOWN ",Trace,View Data,Goto Level0,Goto Level1,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S2A2"
        pos 19. 21. 2.
        text "IF"
        pos 21. 21. 14.
S2AR2:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S2AR2"
        pos 35. 21. 2.
        text " &"
        pos 37. 21. 10.
S2WR2:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S2WR2"
        pos 47. 21. 2.
        text " &"
        pos 49. 21. 10.
S2D2:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S2D2"
        pos 59. 21. 2.
        text " &"
        pos 61. 21. 10.
S2C2:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S2C2"
        pos 71. 21. 2.
        text " &"
        pos 73. 21. 10.
S2E2:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Level2-Z3
; --------------------------------------------------------------------------------
        pos 1. 22. 18.
S2A3:   PULLDOWN ",Trace,View Data,Goto Level0,Goto Level1,Decr COUNT0,Decr COUNT1,Reload COUNT0,Reload COUNT1,Set EXT0,Set EXT1,Trigger TRACE" "gosub CallAction S2A3"
        pos 19. 22. 2.
        text "IF"
        pos 21. 22. 14.
S2AR3:  PULLDOWN ",A-Range,B-Range,C-Range,AB-Range,AC-Range,BC-Range,NOT A-Range,NOT B-Range,NOT C-Range" "gosub CallARange S2AR3"
        pos 35. 22. 2.
        text " &"
        pos 37. 22. 10.
S2WR3:  PULLDOWN ",READ,WRITE,ACCESS,FETCH,EXECUTE,EXECUTEPASS,EXECUTEFAIL" "gosub CallAccess S2WR3"
        pos 47. 22. 2.
        text " &"
        pos 49. 22. 10.
S2D3:   PULLDOWN ",DATA0,DATA1,NOT DATA0,NOT DATA1" "gosub CallData S2D3"
        pos 59. 22. 2.
        text " &"
        pos 61. 22. 10.
S2C3:   PULLDOWN ",COUNT0,NOT COUNT0,COUNT1,NOT COUNT1" "gosub CallCount S2C3"
        pos 71. 22. 2.
        text " &"
        pos 73. 22. 10.
S2E3:   PULLDOWN ",EXT0,NOT EXT0,EXT1,NOT EXT1" ""
; --------------------------------------------------------------------------------
;                                Button-Leiste
; --------------------------------------------------------------------------------
        pos 0. 23. 83.
        line ""
        pos 1. 24.5 8.
        BUTTON "ETM ALL 0" "gosub ETMReset"
        pos 9.5 24.5 6.
        BUTTON "CLEAR" "gosub InitDialogS1"
        pos 16. 24.5 6.
        BUTTON "LOAD" "gosub InitProgram"
        pos 22.5 24.5 6.
        BUTTON "VIEW" "gosub SetAnaProgram 1."
        pos 29. 24.5 7.
        BUTTON "SAVE" "gosub SetAnaProgram 2."
        pos 37. 24.5 15.
        BUTTON "Program"
        (
          LOCAL &file
          if &subdialog!=0.
          (
            LOCAL &text
            if &subdialog==1.
              &text="Please close Define Address Window first!"
            else if &subdialog==2.
              &text="Please close Define Data Window first!"
            else if &subdialog==3.
              &text="Please close Define Count Window first!"
            print %ERROR "&text"
          )
          else
          (
            &file=OS.PTD()+"/etm.cmm"
            gosub SetAnaProgram 0. &file
          )
        )
        pos 52.5 24.5 15.
        BUTTON "Program && Save" "gosub SetAnaProgram 0."
        pos 68. 24.5 15.
        BUTTON "Program && Save && Close" "jumpto okclose"
        pos 1. 26. 12.
        text "Actual used file:"
        pos 13. 26. 25.
IFILE:  EDIT ""
        (
          if &subdialog!=0.
          (
            LOCAL &text
            if &subdialog==1.
              &text="Please close Define Address Window first!"
            else if &subdialog==2.
              &text="Please close Define Data Window first!"
            else if &subdialog==3.
              &text="Please close Define Count Window first!"
            print %ERROR "&text"
            dialog.set IFILE ""
          )
        )
        pos 38. 26. 2.
FVBUT:  BUTTON "V"
        (
          if &subdialog!=0.
          (
            LOCAL &text
            if &subdialog==1.
              &text="Please close Define Address Window first!"
            else if &subdialog==2.
              &text="Please close Define Data Window first!"
            else if &subdialog==3.
              &text="Please close Define Count Window first!"
            print %ERROR "&text"
          )
          else
            dialog.setfilew IFILE "&adir"
        )
        CLOSE "jumpto winclose"
        pos 77. 26. 6.
        BUTTON "About" "gosub ShowVersion"
;        pos 50. 26.25 1.
;        text ""
  )
  &adir="*.cmm"
  gosub InitDialogS1
  stop
okclose:
  gosub SetAnaProgram 0.
winclose:
  if &subdialog!=0.
    dialog.end
  dialog.end
  ENDDO

ETMReset:
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
  )
  else
  (
    gosub InitDialogS1
  )
  RETURN

InitDialogS1:

  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
  )
  else
  (
    &stateon=-1.
    dialog.set RA1 ""
    dialog.set RA2 ""
    dialog.set RB1 ""
    dialog.set RB2 ""
    dialog.set RC1 ""
    dialog.set RC2 ""
    if etm.addrcomp()==0.
    (
      dialog.disable RA1
      dialog.disable RA2
      dialog.disable RB1
      dialog.disable RB2
      dialog.disable RC1
      dialog.disable RC2
      dialog.disable VA1
      dialog.disable VA2
      dialog.disable VB1
      dialog.disable VB2
      dialog.disable VC1
      dialog.disable VC2
    )
    else if etm.addrcomp()==1.
    (
      dialog.disable RB1
      dialog.disable RB2
      dialog.disable RC1
      dialog.disable RC2
      dialog.disable VB1
      dialog.disable VB2
      dialog.disable VC1
      dialog.disable VC2
    )
    else if etm.addrcomp()==2.
    (
      dialog.disable RC1
      dialog.disable RC2
      dialog.disable VC1
      dialog.disable VC2
    )
    dialog.set RD01 ""
    dialog.set RD11 ""
    dialog.set RD02 "BYTE"
    dialog.set RD12 "BYTE"
    if etm.datacomp()==0.
    (
      dialog.disable RD01
      dialog.disable RD11
      dialog.disable VD01
      dialog.disable VD11
      dialog.disable RD02
      dialog.disable RD12
    )
    else if etm.datacomp()==1.
    (
      dialog.disable RD11
      dialog.disable VD11
      dialog.disable RD12
    )
    dialog.set EC0  ""
    dialog.set EC1  ""
    if etm.counters()==0.
    (
      dialog.disable EC0
      dialog.disable EC1
      dialog.disable VC01
      dialog.disable VC11
    )
    else if etm.counters()==1.
    (
      dialog.disable EC1
      dialog.disable VC11
    )
    dialog.set S0A1  ""
    dialog.set S0AR1 ""
    dialog.set S0WR1 ""
    dialog.set S0D1  ""
    dialog.set S0C1  ""
    dialog.set S0E1  ""
    dialog.set S0A2  ""
    dialog.set S0AR2 ""
    dialog.set S0WR2 ""
    dialog.set S0D2  ""
    dialog.set S0C2  ""
    dialog.set S0E2  ""
    dialog.set S0A3  ""
    dialog.set S0AR3 ""
    dialog.set S0WR3 ""
    dialog.set S0D3  ""
    dialog.set S0C3  ""
    dialog.set S0E3  ""
    dialog.set S0A4  ""
    dialog.set S0AR4 ""
    dialog.set S0WR4 ""
    dialog.set S0D4  ""
    dialog.set S0C4  ""
    dialog.set S0E4  ""
    dialog.set S1A1  ""
    dialog.set S1AR1 ""
    dialog.set S1WR1 ""
    dialog.set S1D1  ""
    dialog.set S1C1  ""
    dialog.set S1E1  ""
    dialog.set S1A2  ""
    dialog.set S1AR2 ""
    dialog.set S1WR2 ""
    dialog.set S1D2  ""
    dialog.set S1C2  ""
    dialog.set S1E2  ""
    dialog.set S1A3  ""
    dialog.set S1AR3 ""
    dialog.set S1WR3 ""
    dialog.set S1D3  ""
    dialog.set S1C3  ""
    dialog.set S1E3  ""
    dialog.set S2A1  ""
    dialog.set S2AR1 ""
    dialog.set S2WR1 ""
    dialog.set S2D1  ""
    dialog.set S2C1  ""
    dialog.set S2E1  ""
    dialog.set S2A2  ""
    dialog.set S2AR2 ""
    dialog.set S2WR2 ""
    dialog.set S2D2  ""
    dialog.set S2C2  ""
    dialog.set S2E2  ""
    dialog.set S2A3  ""
    dialog.set S2AR3 ""
    dialog.set S2WR3 ""
    dialog.set S2D3  ""
    dialog.set S2C3  ""
    dialog.set S2E3  ""
    dialog.set IFILE ""

    if etm.addrcomp()==0.
    (
      dialog.disable S0AR1
      dialog.disable S0AR2
      dialog.disable S0AR3
      dialog.disable S0AR4
    )
    if etm.datacomp()==0.
    (
      dialog.disable S0D1
      dialog.disable S0D2
      dialog.disable S0D3
      dialog.disable S0D4
    )
    if etm.counters()==0.
    (
      dialog.disable S0C1
      dialog.disable S0C2
      dialog.disable S0C3
      dialog.disable S0C4
    )
    dialog.enable S0A1
    dialog.enable S0WR1
    dialog.enable S0A2
    dialog.enable S0WR2
    dialog.enable S0A3
    dialog.enable S0WR3
    dialog.enable S0A4
    dialog.enable S0WR4
    dialog.disable S1A1
    dialog.disable S1AR1
    dialog.disable S1WR1
    dialog.disable S1D1
    dialog.disable S1C1
    dialog.disable S1A2
    dialog.disable S1AR2
    dialog.disable S1WR2
    dialog.disable S1D2
    dialog.disable S1C2
    dialog.disable S1A3
    dialog.disable S1AR3
    dialog.disable S1WR3
    dialog.disable S1D3
    dialog.disable S1C3
    dialog.disable S2A1
    dialog.disable S2AR1
    dialog.disable S2WR1
    dialog.disable S2D1
    dialog.disable S2C1
    dialog.disable S2A2
    dialog.disable S2AR2
    dialog.disable S2WR2
    dialog.disable S2D2
    dialog.disable S2C2
    dialog.disable S2A3
    dialog.disable S2AR3
    dialog.disable S2WR3
    dialog.disable S2D3
    dialog.disable S2C3
  )

  RETURN ; End InitDialogS1

CallCheckAddr:
  LOCAL &aval &anr &atype &rtype &act &data
  ENTRY &anr

  &anr="R"+"&anr"
  &aval=dialog.string("&anr")
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &anr ""
  )
  else if "&aval"!=""
  (
    on error gosub
    (
      &aval=""
      return
    )
    gosub GetAddrType &aval
    ENTRY &atype

    if (&atype!=&DAddress)&&(&atype!=&DARange)&&("&aval"!="")
    (
      &atype="&aval"+" no valid address!"
      print %ERROR "&atype"
      &aval=""
    )
    else
    (
      gosub CheckAddrCompMax
      ENTRY &atype
      if &atype==1.
        &aval=""
      if "&aval"!=""
      (
        &rtype=string.mid("&anr",0.,1.)
        gosub CheckAddrUsage "&rtype"
        ENTRY &atype &data
        if "&atype"=="-1."
           &aval=""
      )
    )
    if "&aval"==""
     dialog.set &anr ""
  )
  RETURN ; End CallCheckAddr

CallVAddr:
  ; Uebergabe A1/2 B1/2 C1/2
  LOCAL &addr &anr &aval
  ENTRY &anr
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
  )
  else
  (
    &anr="R"+"&anr"
    &addr=dialog.string("&anr")
    if &test==1.
      do "~~~~/diaaddress.cmm" &addr
    else
      do "~~/demo/analyzer/trigger/diaaddress.cmm" &addr
    ENTRY &aval
    if "&aval"!="0."
      dialog.set &anr "&aval"
  )
  RETURN ;  End CallVAddr

CheckAddrCompMax:
  LOCAL &ret
  &ret=0.
  if etm.addrcomp()==0.
  (
    print %error "Sorry no address comparators available!"
    &ret=1.
  )
  else if etm.addrcomp()==1.
  (
    LOCAL &anr &aval1 &aval2 &atype1 &atype2
    &aval1=dialog.string(RA1)
    &aval2=dialog.string(RA2)
    if "&aval1"!=""&&"&aval2"!=""
    (
      gosub GetAddrType &aval1
      ENTRY &atype1
      gosub GetAddrType &aval2
      ENTRY &atype2
      &anr=&atype1+&atype2
      if &anr>0x200
      (
         print %error "Sorry only one address comparator pair available!"
         &ret=1.
      )
    )
  )
  else if etm.addrcomp()==2.
  (
    LOCAL &anr &aval1 &aval2 &atype1 &atype2 &bval1 &bval2 &btype1 &btype2
    &aval1=dialog.string(RA1)
    &aval2=dialog.string(RA2)
    &bval1=dialog.string(RB1)
    &bval2=dialog.string(RB2)
    gosub GetAddrType &aval1
    ENTRY &atype1
    gosub GetAddrType &aval2
    ENTRY &atype2
    gosub GetAddrType &bval1
    ENTRY &btype1
    gosub GetAddrType &bval2
    ENTRY &btype2
    &anr=&atype1+&atype2+&btype1+&btype2
    if (&anr>0x400)
    (
      print %error "Sorry only 2 address comparator pairs available!"
      &ret=1.
    )
  )
  else if etm.addrcomp()==3.
  (
    LOCAL &anr &aval1 &aval2 &atype1 &atype2 &bval1 &bval2 &btype1 &btype2 &cval1 &cval2 &ctype1 &ctype2
    &aval1=dialog.string(RA1)
    &aval2=dialog.string(RA2)
    &bval1=dialog.string(RB1)
    &bval2=dialog.string(RB2)
    &cval1=dialog.string(RC1)
    &cval2=dialog.string(RC2)
    gosub GetAddrType &aval1
    ENTRY &atype1
    gosub GetAddrType &aval2
    ENTRY &atype2
    gosub GetAddrType &bval1
    ENTRY &btype1
    gosub GetAddrType &bval2
    ENTRY &btype2
    gosub GetAddrType &cval1
    ENTRY &ctype1
    gosub GetAddrType &cval2
    ENTRY &ctype2
    &anr=&atype1+&atype2+&btype1+&btype2+&ctype1+&ctype2
    if (&anr>0x600)
    (
      print %error "Sorry only 3 address comparator pairs available!"
      &ret=1.
    )
  )
  else if etm.addrcomp()==4.
  (
    LOCAL &anr &aval1 &aval2 &atype1 &atype2 &bval1 &bval2 &btype1 &btype2 &cval1 &cval2 &ctype1 &ctype2
    &aval1=dialog.string(RA1)
    &aval2=dialog.string(RA2)
    &bval1=dialog.string(RB1)
    &bval2=dialog.string(RB2)
    &cval1=dialog.string(RC1)
    &cval2=dialog.string(RC2)
    gosub GetAddrType &aval1
    ENTRY &atype1
    gosub GetAddrType &aval2
    ENTRY &atype2
    gosub GetAddrType &bval1
    ENTRY &btype1
    gosub GetAddrType &bval2
    ENTRY &btype2
    gosub GetAddrType &cval1
    ENTRY &ctype1
    gosub GetAddrType &cval2
    ENTRY &ctype2
    &anr=&atype1+&atype2+&btype1+&btype2+&ctype1+&ctype2
    if (&anr>0x800)
    (
      print %error "Sorry only 4 address comparator pairs available!"
      &ret=1.
    )
  )
  else if etm.addrcomp()==5.
  (
    LOCAL &anr &aval1 &aval2 &atype1 &atype2 &bval1 &bval2 &btype1 &btype2 &cval1 &cval2 &ctype1 &ctype2
    &aval1=dialog.string(RA1)
    &aval2=dialog.string(RA2)
    &bval1=dialog.string(RB1)
    &bval2=dialog.string(RB2)
    &cval1=dialog.string(RC1)
    &cval2=dialog.string(RC2)
    gosub GetAddrType &aval1
    ENTRY &atype1
    gosub GetAddrType &aval2
    ENTRY &atype2
    gosub GetAddrType &bval1
    ENTRY &btype1
    gosub GetAddrType &bval2
    ENTRY &btype2
    gosub GetAddrType &cval1
    ENTRY &ctype1
    gosub GetAddrType &cval2
    ENTRY &ctype2
    &anr=&atype1+&atype2+&btype1+&btype2+&ctype1+&ctype2
    if (&anr>0xa00)
    (
      print %error "Sorry only 5 address comparator pairs available!"
      &ret=1.
    )
  )

  RETURN &ret ; End CheckAddrCompMax

GetAddrType:
  LOCAL &aval &atype
  ENTRY &aval
  if "&aval"!=""
  (
    EVAL &aval
    &atype=eval.type()
  )
  else
    &atype=0x0
 RETURN &atype

CallCheckCount:
  ; Uebergabe C0 or C1
  LOCAL &cval &cnr &ce &t1 &cmax &c0e &c0val &ttmp
  ENTRY &cnr
  &ce="E"+"&cnr"
  &cval=dialog.string("&ce")
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &ce ""
  )
  else if "&cval"!=""
  (
      on error gosub
      (
        &cval=""
        return
      )
      EVAL &cval
      &t1=eval.type()

      if &t1!=&DInteger&&&t1!=&DIntNum&&&t1!=&DFloat&&&t1!=&DBinary&&&t1!=&DHex&&&t1!=&DNumeric&&"&cval"!=""
      (
        &t1="&cval"+" no valid counter !"
        print %ERROR "&t1"
        &cval=""
      )
      if &cval>0xffff
      (
        &t1="&cval"+" larger than max value 0xffff!"
        print %ERROR "&t1"
        &cval=""
      )
    if "&cval"==""
      dialog.set &ce ""
    else
      dialog.set &ce "&cval"
  )
  RETURN ; End CallCheckCount

CallVCount:
  ; Uebergabe: C0 or C1
  LOCAL &cval &cnr &ce
  ENTRY &cnr
  &ce="E"+"&cnr"
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &ce ""
  )
  else
  (
    &cval=dialog.string("&ce")
    if &test==1.
      do "~~~~/diaetmcount.cmm" &cval
    else
      do "~~/demo/analyzer/trigger/diaetmcount.cmm" &cval
    ENTRY &cval
    if &cval!="0."
    (
      dialog.set &ce &cval
      gosub CallCheckCount &cnr
    )
  )
  RETURN ; End CallVCount

CallCheckData:
  LOCAL &dval &dnr &dc &dt &ndval &type
  ENTRY &dnr
  &dc="R"+"&dnr"+"1"
  &dt="R"+"&dnr"+"2"
  &dval=dialog.string("&dc")
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &dc ""
  )
  else if "&dval"!=""
  (
    &type=dialog.string("&dt")
    if "&type"!=""
    (
      gosub CheckData &dval &type
      ENTRY &ndval
      if "&ndval"==""
        dialog.set &dc ""
      if "&dval"!="&ndval"
        dialog.set &dc "&ndval"
    )
    else
    (
      LOCAL &txt
      &txt="Data type not defined. Define type first!"
      print %ERROR "&txt"
      dialog.set &dc ""
    )
  )
  RETURN ; End CallCheckData

CallVData:
  ; Uebergabe: D0, D1
  LOCAL &dval &dtype &dnr &dc &dt
  ENTRY &dnr
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
  )
  else
  (
    &dc="R"+"&dnr"+"1"
    &dt="R"+"&dnr"+"2"
    &dval=dialog.string("&dc")
    &dtype=dialog.string("&dt")
    if &test==1.
      do "~~~~/diaetmdata.cmm" &dtype &dval
    else
      do "~~/demo/analyzer/trigger/diaetmdata.cmm" &dtype &dval
    ENTRY &dval &dtype
    if &dval!="0."
      dialog.set &dc &dval
    if &dtype!=""
      dialog.set &dt &dtype
  )
  RETURN ; End CallVData

CallAction:
  ; Uebergabe: S<level>A<line>
  LOCAL &anr &action &level &line &reta
  ENTRY &anr
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &anr ""
  )
  else
  (
    &action=dialog.string("&anr")
    &level=string.mid("&anr",1.,1.)
    &level="&level"+"."
    &line=string.mid("&anr",3.,1.)
    &line="&line"+"."
    gosub InitStates "&action" &level &line
    ENTRY &reta
    if &reta>0.
      dialog.set &anr ""
  )
  RETURN ; End CallAction

CallARange:
  ; Uebergabe: S<level>AR<line>
  LOCAL &anr &range &level &line &reta
  ENTRY &anr
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &anr ""
  )
  else
  (
    &range=dialog.string("&anr")
    &level=string.mid("&anr",1.,1.)
    &level="&level"+"."
    &line=string.mid("&anr",4.,1.)
    &line="&line"+"."
    gosub CheckARange "&range" &level &line
    ENTRY &reta
    if &reta==1.
      dialog.set &anr ""
  )
  RETURN ; End CallARange

CallAccess:
  ; Uebergabe: S<level>WR<line>
  LOCAL &anr &access &level &line &reta
  ENTRY &anr
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &anr ""
  )
  else
  (
    &access=dialog.string("&anr")
    &level=string.mid("&anr",1.,1.)
    &level="&level"+"."
    &line=string.mid("&anr",4.,1.)
    &line="&line"+"."
    gosub CheckAccess "&anr" &level &line
    ENTRY &reta
    if &reta==1.
      dialog.set &anr ""
  )
  RETURN ; End CallAccess

CallData:
  ; Uebergabe: S<level>D<line>
  LOCAL &anr &data &level &line &reta
  ENTRY &anr
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &anr ""
  )
  else
  (
    &data=dialog.string("&anr")
    &level=string.mid("&anr",1.,1.)
    &level="&level"+"."
    &line=string.mid("&anr",3.,1.)
    &line="&line"+"."
    gosub CheckDataCall "&data" &level &line
    ENTRY &reta
    if &reta==1.
      dialog.set &anr ""
  )
  RETURN ; End CallData

CallCount:
  ; Uebergabe: S<level>C<line>
  LOCAL &anr &count &level &line &reta
  ENTRY &anr
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
    dialog.set &anr ""
  )
  else
  (
    &count=dialog.string("&anr")
    &level=string.mid("&anr",1.,1.)
    &level="&level"+"."
    &line=string.mid("&anr",3.,1.)
    &line="&line"+"."
    gosub CheckCount "&count" &level &line
    ENTRY &reta
    if &reta==1.
      dialog.set &anr ""
  )
  RETURN ; End CallCount

CheckARange:
   ; Aufruf bei der Auswahl von Range in Action line
   LOCAL &spres &A1R &A2R &A3R &ret &diaret &ra1 &ra2 &ra3 &level &line &txt &t1 &t2
   LOCAL &a &r &rw &d &c &e
   ENTRY &spres &level &line
   &ret=0.
   &ra1="0."
   &ra2="0."
   &ra3="0."

   gosub GetXLineY &level &line
   ENTRY &a &r &rw &d &c &e
   if &a!=""
   (
     gosub CheckActTwice 1. &a &level &line
     ENTRY &ret
     if &ret==-1.
       &ret=1.
     else if &ret>0.
       &ret=0.
   )

   if &ret==0.
   (
     gosub CheckAddrCompMax
     ENTRY &ret
   )

   if &ret==0.
   (
     if etm.addrcomp()==0.&&&spres!=""
     (
       &t1="Sorry no address comparator pairs available!"
       print %ERROR "&t1"
       &ret=1.
     )
     else if etm.addrcomp()==1.&&&spres!=""
     (
       if &spres!="A-Range"&&&spres!="NOT A-Range"
       (
         &t1="Sorry only 1 address comparator pair available!"
         print %ERROR "&t1"
         &ret=1.
       )
     )
     else if etm.addrcomp()==2.&&&spres!=""
     (
       if &spres=="C-Range"||&spres=="NOT C-Range"||&spres=="BC-Range"||&spres=="AC-Range"
       (
         &t1="Sorry only 2 address comparator pairs available!"
         print %ERROR "&t1"
         &ret=1.
       )
     )
   )
   if &ret==0.
   (
     if &spres=="A-Range"||&spres=="AB-Range"||&spres=="AC-Range"||&spres=="NOT A-Range"
     (
       &A1R=dialog.string(RA1)
       &A2R=dialog.string(RA2)
       if "&A1R"==""&&"&A2R"==""
       (
         if &test==1.
           do "~~~~/diaaddress.cmm"
         else
           do "~~/demo/analyzer/trigger/diaaddress.cmm"
         ENTRY &diaret
         if "&diaret"=="0."
           &ret=1.
         else
           &ra1="&diaret"
       )
     )
   )
   if &ret==0.
   (
     if &spres=="B-Range"||&spres=="AB-Range"||&spres=="BC-Range"||&spres=="NOT B-Range"
     (
       &A1R=dialog.string(RB1)
       &A2R=dialog.string(RB2)
       if "&A1R"==""&&"&A2R"==""
       (
         if &test==1.
           do "~~~~/diaaddress.cmm"
         else
           do "~~/demo/analyzer/trigger/diaaddress.cmm"
         ENTRY &diaret
         if "&diaret"=="0."
           &ret=1.
         else
           &ra2="&diaret"
       )
     )
     if &ret==0.
     (
       if &spres=="C-Range"||&spres=="AC-Range"||&spres=="BC-Range"||&spres=="NOT C-Range"
       (
         &A1R=dialog.string(RC1)
         &A2R=dialog.string(RC2)
         if "&A1R"==""&&"&A2R"==""
         (
           if &test==1.
             do "~~~~/diaaddress.cmm"
           else
             do "~~/demo/analyzer/trigger/diaaddress.cmm"
           ENTRY &diaret
           if "&diaret"=="0."
             &ret=1.
           else
             &ra3="&diaret"
         )
       )
     )
   )
   if &ret==0.
   (
     LOCAL &a &r &rw &d &c &e &t1 &A1R &A2R &B1R &B2R &C1R &C2R
     gosub GetXLineY &level &line
     ENTRY &a &r &rw &d &c &e
     &A1R=dialog.string(RA1)
     &A2R=dialog.string(RA2)
     &B1R=dialog.string(RB1)
     &B2R=dialog.string(RB2)
     &C1R=dialog.string(RC1)
     &C2R=dialog.string(RC2)

     if (&spres=="AC-Range")
     (
       if ("&A1R"!=""&&"&A2R"!=""&&("&C1R"!=""||"&C2R"!=""||&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon)))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
       if (("&A1R"!=""||"&A2R"!="")&&("&C1R"!=""||"&C2R"!="")&&(&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon)))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
       else if (("&A1R"!=""||"&A2R"!="")&&("&C1R"!=""&&"&C2R"!=""))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
     )
     if (&spres=="AB-Range")
     (
       if ("&A1R"!=""&&"&A2R"!=""&&("&B1R"!=""||"&B2R"!=""||&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon)))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
       if (("&A1R"!=""||"&A2R"!="")&&("&B1R"!=""||"&B2R"!="")&&(&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon)))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
       else if (("&A1R"!=""||"&A2R"!="")&&("&B1R"!=""&&"&B2R"!=""))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
     )
     if (&spres=="BC-Range")
     (
       if ("&B1R"!=""&&"&B2R"!=""&&("&C1R"!=""||"&C2R"!=""||&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon)))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
       if (("&B1R"!=""||"&B2R"!="")&&("&C1R"!=""||"&C2R"!="")&&(&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon)))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
       else if (("&B1R"!=""||"&B2R"!="")&&("&C1R"!=""&&"&C2R"!=""))
       (
         &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
     )
     if (&spres=="AB-Range"||&spres=="AC-Range")&&(&c!=""||("&A1R"!=""&&"&A2R"!="")||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon))
     (
       &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&t1"
       &ret=1.
     )
     if (&spres=="AB-Range"||&spres=="BC-Range")&&(&c!=""||("&B1R"!=""&&"&B2R"!="")||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon))
     (
       &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&t1"
       &ret=1.
     )
     if (&spres=="AC-Range"||&spres=="BC-Range")&&(&c!=""||("&C1R"!=""&&"&C2R"!="")||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon))
     (
       &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&t1"
       &ret=1.
     )
     if (&spres=="A-Range"||&spres=="NOT A-Range")&&((&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon))&&"&A1R"!=""&&"&A2R"!="")
     (
       &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&t1"
       &ret=1.
     )
     if (&spres=="B-Range"||&spres=="NOT B-Range")&&((&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon))&&"&B1R"!=""&&"&B2R"!="")
     (
       &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&t1"
       &ret=1.
     )
     if (&spres=="C-Range"||&spres=="NOT C-Range")&&((&c!=""||&level>0.||(&level==0.&&&stateon>-1.&&&line>&stateon))&&"&C1R"!=""&&"&C2R"!="")
     (
       &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&t1"
       &ret=1.
     )
     if &level>0.&&(&c!=""&&(&spres=="AB-Range"||&spres=="AC-Range"||&spres=="BC-Range"))
     (
       &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&t1"
       &ret=1.
     )
     if &level>0.&&(&c!=""&&(&spres=="A-Range"||&spres=="B-Range"||&spres=="C-Range"))
     (
       &t1="Too many conditions defined for Level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&t1"
       &ret=1.
     )
     if &spres==""&&(&d!=""||&rw!="")
     (
       LOCAL &t2 &ar
       &t1="No address defined for data/access condition in Level"+"&level"+" line "+"&line"+" ! Deleted ..."
       print "&t1"
       ;&ret=1.
       &t1=string.cut("&level",-1.)
       &t2=string.cut("&line",-1.)
       &ar="S"+"&t1"+"D"+"&t2"
       dialog.set &ar ""
       &ar="S"+"&t1"+"WR"+"&t2"
       dialog.set &ar ""
     )
   )

   if &ret==0.
   (
     LOCAL &ta &tb &tc &da &db &dc &tx
     if "&ra1"!="0."
       dialog.set RA1 "&ra1"
     if "&ra2"!="0."
       dialog.set RB1 "&ra2"
     if "&ra3"!="0."
       dialog.set RC1 "&ra3"
     if &level>0.
     (
       &t1=string.cut("&level",-1.)
       &t2=string.cut("&line",-1.)
       &ar="S"+"&t1"+"C"+"&t2"
       if &spres==""
         dialog.enable &ar
       else
         dialog.disable &ar
     )
     &ta=0.
     &tb=0.
     &tc=0.
     &da=0.
     &db=0.
     &dc=0.
     &t1=string.cut("&level",-1.)
     &t2=string.cut("&line",-1.)
     &ar="S"+"&t1"+"AR"+"&t2"
     dialog.set &ar ""
     if &spres=="A-Range"||&spres=="NOT A-Range"||&spres=="AB-Range"||&spres=="AC-Range"
     (
        gosub CheckAddrUsage "A"
        ENTRY &ta &da
        if "&ta"=="-1."
          &ret=1.
     )
     if &spres=="B-Range"||&spres=="NOT B-Range"||&spres=="BC-Range"||&spres=="AB-Range"
     (
        gosub CheckAddrUsage "B"
        ENTRY &tb &db
        if "&tb"=="-1."
          &ret=1.
     )
     if &spres=="C-Range"||&spres=="NOT C-Range"||&spres=="AC-Range"||&spres=="BC-Range"
     (
        gosub CheckAddrUsage "C"
        ENTRY &tc &dc
        if "&tc"=="-1."
          &ret=1.
     )
     dialog.set &ar &spres
     if &ret==0.
     (
       &ar="S"+"&t1"+"WR"+"&t2"
       if ("&ta"!="0."&&"&tb"!="0."&&("&ta"!="&tb"||"&da"!="&db"))
       (
         if ("&ta"!="&tb")
           &tx="Different RD/WR type defined for AB-Range "+"&ta"+" != "+"&tb"+" "+"&level"+" line "+"&line"+" !"
         else
           &tx="Different DATA defined for AB-Range "+"&da"+" != "+"&db"+" "+"&level"+" line "+"&line"+" !"
         print %ERROR "&tx"
         &ret=1.
       )
       else if ("&ta"!="0."&&"&tc"!="0."&&("&ta"!="&tc"||"&da"!="&dc"))
       (
         if "&ta"!="&tc"
           &tx="Different RD/WR type defined for AC-Range "+"&ta"+" != "+"&tc"+" "+"&level"+" line "+"&line"+" !"
         else
           &tx="Different DATA defined for AC-Range "+"&da"+" != "+"&dc"+" "+"&level"+" line "+"&line"+" !"
         print %ERROR "&tx"
         &ret=1.
       )
       else if ("&tb"!="0."&&"&tc"!="0."&&("&tb"!="&tc"||"&db"!="&dc"))
       (
         if "&tb"!="&tc"
           &tx="Different RD/WR type defined for BC-Range "+"&tb"+" != "+"&tc"+" "+"&level"+" line "+"&line"+" !"
         else
           &tx="Different DATA defined for BC-Range "+"&db"+" != "+"&dc"+" "+"&level"+" line "+"&line"+" !"
         print %ERROR "&tx"
         &ret=1.
       )
       else if ("&ta"!="0."&&"&ta"!="EXECUTE")
         dialog.set &ar "&ta"
       else if ("&ta"!="0."&&"&ta"=="EXECUTE")
         dialog.set &ar ""
       else if ("&tb"!="0."&&"&tb"!="EXECUTE")
         dialog.set &ar "&tb"
       else if ("&tb"!="0."&&"&tb"=="EXECUTE")
         dialog.set &ar ""
       else if ("&tc"!="0."&&"&tc"!="EXECUTE")
         dialog.set &ar "&tc"
       else if ("&tc"!="0."&&"&tc"=="EXECUTE")
         dialog.set &ar ""
       &ar="S"+"&t1"+"D"+"&t2"
       if ("&da"!="0.")
         dialog.set &ar "&da"
       else if ("&ta"!="0."&&"&da"=="0.")
         dialog.set &ar ""
       else if ("&db"!="0.")
         dialog.set &ar "&db"
       else if ("&tb"!="0."&&"&db"=="0.")
         dialog.set &ar ""
       else if ("&dc"!="0.")
         dialog.set &ar "&dc"
       else if ("&tc"!="0."&&"&dc"=="0.")
         dialog.set &ar ""
     )
   )
  RETURN &ret  ;End CheckARange

CheckAccess:
  LOCAL &spres &level &line &ret &a &r &rw &d &c &e &act &data
  ENTRY &spres &level &line
  gosub GetXLineY &level &line
  ENTRY &a &r &rw &d &c &e
  &ret=0.
  if &a!=""
  (
    gosub CheckActTwice 1. &a &level &line
    ENTRY &ret
    if &ret==-1.
      &ret=1.
    else if &ret>0.
      &ret=0.
  )
  if &ret==0.&&&spres!=""
  (
    if &d=="DATA0"||&d=="DATA1"||&d=="!DATA0"||&d=="!DATA1"||&a=="View Data"
    (
      if &spres=="FETCH"||&spres=="EXECUTE"||&spres=="EXECUTEPASS"||&spres=="EXECUTEFAIL"
      (
        &a="Access "+"&spres"+" not valid for data access!"
        print %error "&a"
        &ret=1.
      )
    )
    if string.find(&r,"B")
    (
      if &r=="AB"||&r=="AB&&BB"||&r=="AB&&CB"||&r=="!AB"
      (
        gosub CheckAddrUsage "A"
        ENTRY &type &data
        if "&type"=="-1."
          &ret=1.
      )
      if &r=="BB"||&r=="AB&&BB"||&r=="BB&&CB"||&r=="!BB"
      (
        gosub CheckAddrUsage "B"
        ENTRY &type &data
        if "&type"=="-1."
          &ret=1.
      )
      if &r=="CB"||&r=="AB&&CB"||&r=="BB&&CB"||&r=="!CB"
      (
        gosub CheckAddrUsage "C"
        ENTRY &type &data
        if "&type"=="-1."
          &ret=1.
      )
    )
    if &r==""
    (
       &a="Address condition not defined for access in level"+"&level"+" line "+"&line"+" !"
       print %ERROR "&a"
       &ret=1.
    )
  )
  RETURN &ret ; End CheckAccess

CheckDataCall:
   ; wird aufgerufen wenn DATAX als Condition ausgewählt wurde
   LOCAL &spres &dval &dtype &ret &level &line &txt &act &a &r &rw &d &c &e &data
   ENTRY &spres &level &line
   &ret=0.

   gosub GetXLineY &level &line
   ENTRY &a &r &rw &d &c &e
   if &a!=""
   (
     gosub CheckActTwice 1. &a &level &line
     ENTRY &ret
     if &ret==-1.
       &ret=1.
     else if &ret>0.
       &ret=0.
   )
   if &ret==0.
   (
     if &spres=="DATA0"||&spres=="NOT DATA0"
     (
       if etm.datacomp()>0.
       (
         &dval=dialog.string(RD01)
         &dtype=dialog.string(RD02)
         if "&dval"==""
         (
           if &test==1.
             do "~~~~/diaetmdata.cmm" &dtype &dval
           else
             do "~~/demo/analyzer/trigger/diaetmdata.cmm" &dtype &dval
           ENTRY &dval &dtype
           if &dval!="0."
             dialog.set RD01 &dval
           else
             &ret=1.
           if &dtype!=""
             dialog.set RD02 &dtype
         )
       )
       else
       (
         &t1="No data comparators available !"
         print %ERROR "&t1"
         &ret=1.
       )
     )
     if &spres=="DATA1"||&spres=="NOT DATA1"
     (
       if etm.datacomp()>1.
       (
         &dval=dialog.string(RD11)
         &dtype=dialog.string(RD12)
         if "&dval"==""
         (
           if &test==1.
             do "~~~~/diaetmdata.cmm" &dtype &dval
           else
             do "~~/demo/analyzer/trigger/diaetmdata.cmm" &dtype &dval
           ENTRY &dval &dtype
           if &dval!="0."
             dialog.set RD11 &dval
           else
             &ret=1.
           if &dtype!=""
             dialog.set RD12 &dtype
         )
       )
       else
       (
         &t1="No/only one data comparator available !"
         print %ERROR "&t1"
         &ret=1.
       )
     )
     if &ret!=1.&&&spres!=""
     (
       LOCAL &a &r &rw &d &c &e &t1
       gosub GetXLineY &level &line
       ENTRY &a &r &rw &d &c &e
       &t1=string.mid(&r,string.len(&r)-1,1)
       if "&t1"!="B"
       (
         &t1="Address condition not defined for data access in level"+"&level"+" line "+"&line"+" !"
         print %ERROR "&t1"
         &ret=1.
       )
       else if (&rw=="FETCH"||&rw=="EXECUTE"||&r=="FETCH"||&r=="EXECUTE"||&r=="EXECUTEPASS"||&r=="EXECUTEFAIL")
       (
         &t1="Access type ("+"&rw"+") defined for Level"+"&level"+" line "+"&line"+". No Data allowed!"
         print %ERROR "&t1"
         &ret=1.
       )
       else
       (
         gosub CheckAddrUsage "&t1"
         ENTRY &t1 &data
         if "&t1"=="-1."
            &ret=1.
       )
     )
   )
  RETURN &ret ; End CheckDataCall

InitStates:
   LOCAL &spres &level &line &ret &mod &txt &lmask &rmask &act &actfirst
   ENTRY &spres &level &line
   &ret=0.
   if (&spres!="View Data")&&(&spres!="Trace (Global)")&&(&spres!="TraceON (Global)")&&(&spres!="TraceOFF (Global)")
   (
     LOCAL &t1 &t2 &c
     ; enable Counter & External Datacomparator in this line
     &t1=string.cut("&level",-1.)
     &t2=string.cut("&line",-1.)
     &c="S"+"&t1"+"C"+"&t2"
     dialog.enable &c
     &c="S"+"&t1"+"E"+"&t2"
     dialog.enable &c
   )
   if &spres==""
   (
     if &level==0.
     (
       LOCAL &a1 &a2 &a3 &a4
       &a1=dialog.string(S0A1)
       &a2=dialog.string(S0A2)
       &a3=dialog.string(S0A3)
       &a4=dialog.string(S0A4)

       if ("&a1"!="Goto Level1")&&("&a1"!="Goto Level2")&&("&a2"!="Goto Level1")&&("&a2"!="Goto Level2")&&("&a3"!="Goto Level1")&&("&a3"!="Goto Level2")&&("&a4"!="Goto Level1")&&("&a4"!="Goto Level2")
         &stateon=-1. ; kein Goto Levelx in Level 0 definiert
       else if "&a1"=="Goto Level1"||"&a1"=="Goto Level2"
         &stateon=1.
       else if "&a2"=="Goto Level1"||"&a2"=="Goto Level2"
         &stateon=2.
       else if "&a2"=="Goto Level1"||"&a3"=="Goto Level2"
         &stateon=3.
       else if "&a4"=="Goto Level1"||"&a4"=="Goto Level2"
         &stateon=4.
     )
     else if &level==1.
     (
       LOCAL &a1 &a2 &a3 &b11 &b12 &b13 &b14 &b21 &b22 &b23 &b24 &b31 &b32 &b33 &b34 &b41 &b42 &b43 &b44
       &a1=dialog.string(S1A1)
       &b11=dialog.string(S1AR1)
       &b12=dialog.string(S1WR1)
       &b13=dialog.string(S1D1)
       &b14=dialog.string(S1C1)
       &a2=dialog.string(S1A2)
       &b21=dialog.string(S1AR2)
       &b22=dialog.string(S1WR2)
       &b23=dialog.string(S1D2)
       &b24=dialog.string(S1C2)
       &a3=dialog.string(S1A3)
       &b31=dialog.string(S1AR3)
       &b32=dialog.string(S1WR3)
       &b33=dialog.string(S1D3)
       &b34=dialog.string(S1C3)
       if "&a1"==""&&"&a2"==""&&"&a3"==""&&"&b11"==""&&"&b12"==""&&"&b13"==""&&"&b14"==""&&"&b21"==""&&"&b22"==""&&"&b23"==""&&"&b24"==""&&"&b31"==""&&"&b32"==""&&"&b33"==""&&"&b34"==""&&"&b41"==""&&"&b42"==""&&"&b43"==""&&"&b44"==""
       (
         dialog.disable S1A1
         if etm.addrcomp()!=0.
         (
           dialog.disable S1AR1
           dialog.disable S1AR2
           dialog.disable S1AR3
         )
         if etm.datacomp()!=0.
         (
           dialog.disable S1D1
           dialog.disable S1D2
           dialog.disable S1D3
         )
         if etm.counters()!=0.
         (
           dialog.disable S1C1
           dialog.disable S1C2
           dialog.disable S1C3
         )
         dialog.disable S1WR1
         dialog.disable S1A2
         dialog.disable S1WR2
         dialog.disable S1A3
         dialog.disable S1WR3
       )
     )
     else
     (
       LOCAL &a1 &a2 &a3 &b11 &b12 &b13 &b14 &b21 &b22 &b23 &b24 &b31 &b32 &b33 &b34 &b41 &b42 &b43 &b44
       &a1=dialog.string(S2A1)
       &b11=dialog.string(S2AR1)
       &b12=dialog.string(S2WR1)
       &b13=dialog.string(S2D1)
       &b14=dialog.string(S2C1)
       &a2=dialog.string(S2A2)
       &b21=dialog.string(S2AR2)
       &b22=dialog.string(S2WR2)
       &b23=dialog.string(S2D2)
       &b24=dialog.string(S2C2)
       &a3=dialog.string(S2A3)
       &b31=dialog.string(S2AR3)
       &b32=dialog.string(S2WR3)
       &b33=dialog.string(S2D3)
       &b34=dialog.string(S2C3)
       if "&a1"==""&&"&a2"==""&&"&a3"==""&&"&b11"==""&&"&b12"==""&&"&b13"==""&&"&b14"==""&&"&b21"==""&&"&b22"==""&&"&b23"==""&&"&b24"==""&&"&b31"==""&&"&b32"==""&&"&b33"==""&&"&b34"==""&&"&b41"==""&&"&b42"==""&&"&b43"==""&&"&b44"==""
       (
         if etm.addrcomp()!=0.
         (
           dialog.disable S2AR1
           dialog.disable S2AR2
           dialog.disable S2AR3
         )
         if etm.datacomp()!=0.
         (
           dialog.disable S2D1
           dialog.disable S2D2
           dialog.disable S2D3
         )
         if etm.counters()!=0.
         (
           dialog.disable S2C1
           dialog.disable S2C2
           dialog.disable S2C3
         )
         dialog.disable S2A1
         dialog.disable S2WR1
         dialog.disable S2A2
         dialog.disable S2WR2
         dialog.disable S2A3
         dialog.disable S2WR3
       )
     )
   )
   else ; any action
   (
     gosub CheckActTwice 1. &spres &level &line
     ENTRY &actfirst
     if &actfirst>0.
       &ret=0.
     else if &actfirst==-1.
       &ret=1.
   )

   if &ret==0.&&(&spres=="Reload COUNT0"||&spres=="Decr COUNT0")
   (
     gosub CheckCount "COUNT0" &level &line
     ENTRY &ret
   )
   if &ret==0.&&(&spres=="Reload COUNT1"||&spres=="Decr COUNT1")
   (
     gosub CheckCount "COUNT1" &level &line
     ENTRY &ret
   )
   if &ret==0.&&(&spres=="View Data"||&spres=="Trace (Global)"||&spres=="TraceON (Global)"||&spres=="TraceOFF (Global)")
   (
     LOCAL &t1 &t2 &c
    ; disable Counter in this line due to restrictions
     &t1=string.cut("&level",-1.)
     &t2=string.cut("&line",-1.)
     &c="S"+"&t1"+"C"+"&t2"
     dialog.disable &c
     &c="S"+"&t1"+"E"+"&t2"
     dialog.disable &c
   )
   if &ret==0.&&&spres=="Goto Level0"
   (
     ; nur in Level1 oder Level2 auswaehlbar
     if etm.addrcomp()!=0.
     (
       dialog.enable S0AR1
       dialog.enable S0AR2
       dialog.enable S0AR3
       dialog.enable S0AR4
     )
     if etm.datacomp()!=0.
     (
       dialog.enable S0D1
       dialog.enable S0D2
       dialog.enable S0D3
       dialog.enable S0D4
     )
     if etm.counters()!=0.
     (
       dialog.enable S0C1
       dialog.enable S0C2
       dialog.enable S0C3
       dialog.enable S0C4
     )
     dialog.enable S0A1
     dialog.enable S0WR1
     dialog.enable S0A2
     dialog.enable S0WR2
     dialog.enable S0A3
     dialog.enable S0WR3
     dialog.enable S0A4
     dialog.enable S0WR4
   )
   if &ret==0.&&&spres=="Goto Level1"
   (
     if etm.sequencer()==0.
     (
       print %error "Sorry no sequencer available!"
       &ret=1.
     )
     else
     (
       if etm.addrcomp()!=0.
       (
         dialog.enable S1AR1
         dialog.enable S1AR2
         dialog.enable S1AR3
       )
       if etm.datacomp()!=0.
       (
         dialog.enable S1D1
         dialog.enable S1D2
         dialog.enable S1D3
       )
       if etm.counters()!=0.
       (
         dialog.enable S1C1
         dialog.enable S1C3
         dialog.enable S1C2
       )
       dialog.enable S1A1
       dialog.enable S1WR1
       dialog.enable S1A2
       dialog.enable S1WR2
       dialog.enable S1A3
       dialog.enable S1WR3
       if &level==0.&&(&stateon==-1.||&stateon>&line)
         &stateon=&line
     )
   )
   if &ret==0.&&&spres=="Goto Level2"
   (
     if etm.sequencer()==0.
     (
       print %error "Sorry no sequencer available!"
       &ret=1.
     )
     else
     (
       if etm.addrcomp()!=0.
       (
         dialog.enable S2AR1
         dialog.enable S2AR2
         dialog.enable S2AR3
       )
       if etm.datacomp()!=0.
       (
         dialog.enable S2D1
         dialog.enable S2D2
         dialog.enable S2D3
       )
       if etm.counters()!=0.
       (
         dialog.enable S2C1
         dialog.enable S2C2
         dialog.enable S2C3
       )
       dialog.enable S2A1
       dialog.enable S2WR1
       dialog.enable S2A2
       dialog.enable S2WR2
       dialog.enable S2A3
       dialog.enable S2WR3
       if &level==0.&&(&stateon==-1.||&stateon>&line)
         &stateon=&line
     )
   )
  RETURN &ret

CheckCount:
   LOCAL &spres &cval &cunit &ret &msg &level &txt &ar &r &t1 &t2 &a
   ENTRY &spres &level &line
   &ret=0.
   &t1=string.cut("&level",-1.)
   &t2=string.cut("&line",-1.)
   &ar="S"+"&t1"+"AR"+"&t2"
   &r=dialog.string(&ar)
   &ar="S"+"&t1"+"A"+"&t2"
   &a=dialog.string(&ar)
   if "&a"==""
     &a=""""""
   else
     &a="""&a"""
   if &a!=""
   (
     gosub CheckActTwice 1. &a &level &line
     ENTRY &ret
     if &ret==-1.
       &ret=1.
     else if &ret>0.
       &ret=0.
   )
   if "&r"!=""&&&ret==0.
   (
     gosub CheckARange "&r" &level &line
     ENTRY &ret
   )

   if &ret==0.&&(&spres=="COUNT0"||&spres=="NOT COUNT0")
   (
     if etm.counters()==0.
     (
         &t1="No counters available !"
         print %ERROR "&t1"
         &ret=1.
     )
     else if dialog.string(EC0)==""
     (
       if &test==1.
         do "~~~~/diaetmcount.cmm"
       else
         do "~~/demo/analyzer/trigger/diaetmcount.cmm"
       ENTRY &cval
       if &cval!="0."
       (
         dialog.set EC0 &cval
       )
       else
         &ret=1.
       if dialog.string(EC0)==""
       (
           &t1="No counter defined !"
           print %ERROR "&t1"
           &ret=1.
       )
     )
   )
   if &ret==0.&&(&spres=="COUNT1"||&spres=="NOT COUNT1")
   (
     if etm.counters()<=1.
     (
         &t1="Only 0/1 counter available !"
         print %ERROR "&t1"
         &ret=1.
     )
     else if dialog.string(EC1)==""
     (
       if &test==1.
         do "~~~~/diaetmcount.cmm"
       else
         do "~~/demo/analyzer/trigger/diaetmcount.cmm"
       ENTRY &cval
       if &cval!="0."
       (
         dialog.set EC1 &cval
       )
       else
         &ret=1.
       if dialog.string(EC1)==""
       (
           &t1="No counter defined !"
           print %ERROR "&t1"
           &ret=1.
       )
     )
   )
   if &ret==0.
   (
     if &level>0.&&&a!="Decr COUNT0"&&&a!="Decr COUNT1"&&&a!="Reload COUNT0"&&&a!="Reload COUNT1"
     ( ; alles außer Count ausblenden
       &ar="S"+"&t1"+"AR"+"&t2"
       if &spres==""
         dialog.enable &ar
       else
         dialog.disable &ar
       &ar="S"+"&t1"+"WR"+"&t2"
       if &spres==""
         dialog.enable &ar
       else
         dialog.disable &ar
       &ar="S"+"&t1"+"D"+"&t2"
       if &spres==""
         dialog.enable &ar
       else
         dialog.disable &ar
     )
   )
  RETURN &ret

CheckActTwice:
  LOCAL &mode &act &level &line &a &r &rw &d &c &e &x &y &ret
  ENTRY &mode &act &level &line
  &y=0.
  &ret=0.
  if &act=="Goto Level0"||&act=="Goto Level1"||&act=="Goto Level2"
    return &ret

  while &y<3.&&&ret!=-1.
  (
    LOCAL &xende
    if &y==0.
      &xende=5.
    else
      &xende=4.
    &x=1.
    while &x<&xende&&&ret!=-1.
    (
      if &x!=&line||&y!=&level
      (
        gosub GetXLineY &y &x
        ENTRY &a &r &rw &d &c &e
        if &a==&act
        (
          LOCAL &a2 &r2 &rw2 &d2 &c2 &e2
          if &level==4.
          (
            &a2=&act
            &r2=""""""
            &rw2=""""""
            &d2=""""""
            &c2=""""""
          )
          else
          (
            gosub GetXLineY &level &line
            ENTRY &a2 &r2 &rw2 &d2 &c2 &e2
          )

          if &level!=&y&&&ret==0.
          (
            ; no condition at all allowed
            if &r!=""||&r2!=""||&rw!=""||&rw2!=""||&d!=""||&d2!=""||&c!=""||&c2!=""
            (
              if &mode==1.
              (
                LOCAL &msg
                &msg=&act+" used with different condition in Level"+"&y"+" line "+"&x"
                print %error "&msg"
              )
              &ret=-1.
            )
            else
              &ret=(&y+1.)*10.+&x
          )
          else if &level!=&y
          (
            if &mode==1.
            (
              LOCAL &msg
              if &ret>0.
                &msg=&act+" tripple use not allowd"
              else
                &msg=&act+" already used in Level"+"&y"+" line "+"&x"
              print %error "&msg"
            )
            &ret=-1.
          )
        )
      )
      &x=&x+1.
    )
    &y=&y+1.
  )
  RETURN &ret ; 0 not used ever 1 used twice but ok 2 used more than one not ok  ret=lz. l=Level z=Zeile

; --------------------------------------------------------------------------------
;                  Programm schreiben und Analyzer programmieren
; --------------------------------------------------------------------------------
SetAnaProgram:
  LOCAL &addr &data &count &zeilen &nocomment &file &ret &level &act &radi &addcode &maxc &ret
  ENTRY &nocomment %line &file
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Cannot program. Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Cannot program. Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Cannot program. Please close Define Count Window first!"
    print %ERROR "&text"
    RETURN
  )

  if "&file"==""
  (
    &file=dialog.string(IFILE)
    if &nocomment==0.||&nocomment==2.
    (
      while "&file"==""
      (
        &file="&adir"
        dialog.setfilew IFILE "&file"
        &file=dialog.string(IFILE)
        if "&file"==""
        (
          RETURN
        )
      )
      gosub SetDir &file
    )
    else
    (
      if "&file"==""
      (
        &file=os.ptd()
        &file="&file"+"/etm.cmm"
      )
      else
      (
        LOCAL &p &x &y &tfile
        &y=string.len("&file")-1.
        &x=&y+1.
        &p=string.mid("&file",&y,1.)
        while "&p"!="/"&&"&p"!="\"
        (
          &y=&y-1.
          &p=string.mid("&file",&y,1.)
        )
        &tfile=string.mid("&file",&y+1.,&x-&y)
        &file=os.ptd()
        &file="&file"+"/"+"&tfile"
      )
    )
  )
  OPEN #1 "&file" /CREATE
  &data=0y0
  &count=0y0
  &zeilen=0y0
  &addr=0y0
  &radi=radix()
  &msg=";"+"&radi"+" written with ETM Programming Dialog &version"
  WRITE #1 "&msg"
  &msg="ETM.CLEAR"
  WRITE #1 "&msg"
  gosub WriteADDR &nocomment
  ENTRY &addr &addcode &maxc
  if &addr>=0.
  (
    &addr=&addr*0y100
    gosub WriteDATA &nocomment &addcode &maxc
    ENTRY &data
    if &data>=0.
    (
      &addr=&addr+&data
      &addr=&addr*0y100
      gosub WriteCOUNT &nocomment
      ENTRY &count
      if &count>=0.
      (
        &addr=&addr+&count
        gosub WriteStates &addr &nocomment &addcode &maxc
        ENTRY &zeilen &level &act
      )
    )
  )
  if &addr>=0.||&data>=0.||&count>=0.
  (
    gosub CheckActTwice 0. "Trigger TRACE" 4. 4.
    ENTRY &ret
    if &ret==0.
    (
      WRITE #1 ""
      &msg="ETM.SET TRIGGER NOT TRUE"
      WRITE #1 "&msg"
    )
  )
  CLOSE #1
  if &nocomment==0.
  (
    if &addr>=0.&&&data>=0.&&&count>=0.&&&zeilen>0.
    (
      ;on error gosub
      ;  return
      do "&file"
      print "ETM programmed sucessfully!"
    )
    else
    (
      LOCAL &msg
      &msg="Could not program ET;! Stop at Level"+"&level"+" Line "+"&act"
      ;print %ERROR "&msg"
    )
  )
  else if &nocomment==2.
    print "ETM program saved sucessfully!"
  else
  (
    if &addr>=0.&&&data>=0.&&&count>=0.&&&zeilen>0.
    (
      edit.quit "&file"
      edit "&file"
    )
    else
      print %ERROR "Programm not correct! Could not edit!"
  )
RETURN

WriteADDR:
  LOCAL &a1 &a2 &a3 &a4 &x &y &addx &addrc &res &reshr &reshs &noco &add &maxcomp
  ENTRY &noco
  &a4=""
  &maxcomp=0x1
  &a3=0.
  &res=0.
  &reshr=0x0000000
  &reshs=0x0000000
  gosub CheckAddrCompMax
  ENTRY &res
  while &a3<2.&&&res>=0.
  (
    &y=0x1
    &x=0.
    while &x<3.&&&res>=0.
    (
      if &x==0.
      (
        gosub GetXRange "A"
        ENTRY &a1 &a2
      )
      else if &x==1.
      (
        if "&addx"!=""&&&res>=0.&&&a3==1.
        (
          gosub WriteADDRCom "A" &a1 &a2
          &res=0y100
        )
        gosub GetXRange "B"
        ENTRY &a1 &a2
      )
      else if &x==2.
      (
        if "&addx"!=""&&&res>=0.&&&a3==1.
        (
          gosub WriteADDRCom "B" &a1 &a2
          &res=&res+0y10
        )
        gosub GetXRange "C"
        ENTRY &a1 &a2
      )

      &add=0.
      &addx=""
      on error gosub
        return
      if "&a1"!=""
      (
        EVAL &a1
        &addrc=eval.type()
        if &addrc!=&DAddress&&&addrc!=&DARange
        (
          if &x==0.
            print %error "First Address A-Range definition wrong."
          else if &x==1.
            print %error "First Address B-Range definition wrong."
          else if &x==2.
            print %error "First Address C-Range definition wrong."
          &add=-1.
        )
        if &addrc==&DARange&&&a3==0.
        (
          gosub DefineRange "1" &a1 &x &maxcomp &reshr &reshs
          ENTRY &add &reshr &reshs &maxcomp
        )
        else if &addrc==&DAddress&&&a3==1.
        (
          gosub DefineSAddress "1" &a1 &x &maxcomp &reshr
          ENTRY &add &reshr &maxcomp
        )
        &addx=1.
      )
      else
        &add=0.
      if "&a2"!=""
      (
        EVAL &a2
        &addrc=eval.type()
        if &addrc!=&DAddress&&&addrc!=&DARange
        (
          if &x==0.
            print %error "Second Address A-Range definition wrong."
          else if &x==1.
            print %error "Second Address B-Range definition wrong."
          else if &x==2.
            print %error "Second Address C-Range definition wrong."
          &add=-1.
        )
        if &addrc==&DARange&&&a3==0.
        (
          gosub DefineRange "2" &a2 &x &maxcomp &reshr &reshs
          ENTRY &add &reshr &reshs &maxcomp
        )
        else if &addrc==&DAddress&&&a3==1.
        (
          gosub DefineSAddress "2" &a2 &x &maxcomp &reshr
          ENTRY &add &reshr &maxcomp
        )
        &addx=&addx+2.
      )
      else if &add>=0.
        &add=0.

      if &add<0.
        &res=-1.
      &x=&x+1.
    )
    if &a3==0.&&&maxcomp>0x1
      &maxcomp=(&maxcomp*0x2)-0x1
    &a3=&a3+1.
  )
  if "&addx"!=""&&&res>=0.
  (
    gosub WriteADDRCom "C" &a1 &a2
    &res=&res+0y1
  )
  if &res>=0.
  (
    gosub CodeADDRange &reshr &reshs &maxcomp
    ENTRY &reshr
  )
  RETURN &res &reshr &maxcomp

GetXRange:
  LOCAL &r1 &r2 &a1 &a2 &x &t1
  ENTRY &x
  &r1="R"+&x+"1"
  &r2="R"+&x+"2"
  &a1=dialog.string(&r1)
  &a2=dialog.string(&r2)
  RETURN &a1 &a2

WriteADDRCom:
  LOCAL &x &r1 &r2
  ENTRY &x &r1 &r2

  &addrc=";#"+&x+"B,"+"&r1"+","+"&r2"
  WRITE #1 "&addrc"
  RETURN

DefineRange:
  LOCAL &nr &sax &x &y &yr &reshr &reshs &ind &beg &end &str &type &ret &data
  ENTRY &nr &sax &x &y &reshr &reshs

  if &x==0.; ARange
  (
    if &nr=="1"
    (
      &yr=&y*0x100000
      &reshs=&reshs+0xf00000
    )
    else
    (
      &yr=&y*0x010000
      &reshs=&reshs+0x0f0000
    )
    if etm.addrcomp()==0.
    (
      print %error "Could not write A-Range definition. No address comparators available!"
      &type=-1.
    )
    else
    (
      gosub CheckAddrUsage "A"
      ENTRY &type &data
    )
  )
  else if &x==1. ;BRange
  (
    if &nr=="1"
    (
      &yr=&y*0x001000
      &reshs=&reshs+0x00f000
    )
    else
    (
      &yr=&y*0x000100
      &reshs=&reshs+0x000f00
    )
    if etm.addrcomp()==0.
    (
      print %error "Could not write B-Range definition. No address comparators available!"
      &type=-1.
    )
    else if etm.addrcomp()==1.
    (
      print %error "Could not write B-Range definition. Only 1 address comparator available!"
      &type=-1.
    )
    else
    (
      gosub CheckAddrUsage "B"
      ENTRY &type &data
    )
  )
  else ;CRange
  (
    if &nr=="1"
    (
      &yr=&y*0x000010
      &reshs=&reshs+0x0000f0
    )
    else
    (
      &yr=&y*0x000001
      &reshs=&reshs+0x00000f
    )
    if etm.addrcomp()==0.
    (
      print %error "Could not write C-Range definition. No address comparators available!"
      &type=-1.
    )
    else if etm.addrcomp()==1.||etm.addrcomp()==2.
    (
      print %error "Could not write C-Range definition. Only 1/2 address comparators available!"
      &type=-1.
    )
    else
    (
      gosub CheckAddrUsage "C"
      ENTRY &type &data
    )
  )
  if "&type"!="-1."&&"&type"!="0."
  (
    &reshr=&reshr+&yr
    &str="ETM.SET RANGE "+"&y"+" "+"&type"+" "+"&sax"
    WRITE #1 "&str"
    if "&data"=="DATA0"||"&data"=="DATA1"||"&data"=="!DATA0"||"&data"=="!DATA1"
    (
      LOCAL &ybeg &yend &inv
      ; Datencomparator auch gleich mitschreiben
      if "&data"=="DATA0"||"&data"=="!DATA0"
      (
        &sax=dialog.string(RD01)
        &type=dialog.string(RD02)
      )
      else ; DATA1
      (
        &sax=dialog.string(RD11)
        &type=dialog.string(RD12)
      )
      if "&type"=="BYTE"
        &sax=&sax|0xxxxxxx00
      else if "&type"=="WORD"
        &sax=&sax|0xxxxx0000
      &yend=&y*0x2
      &ybeg=(&y*0x2)-0x1
      &inv=""
      if "&data"=="!DATA0"||"&data"=="!DATA1"
        &inv="!= "
      &data="ETM.SET DATA "+"&ybeg"+" "+"&type"+" "+"&inv"+"&sax"
      WRITE #1 "&data"
      &data="ETM.SET DATA "+"&yend"+" "+"&type"+" "+"&inv"+"&sax"
      WRITE #1 "&data"
    )
    &y=&y+0x1
    &ret=0.
  )
  else if "&type"=="0."
  ( ; Defined but never used
    &reshr=&reshr+&yr
    &str="ETM.SET RANGE "+"&y"+" EXECUTE "+"&sax"
    WRITE #1 "&str"
    &y=&y+0x1
    &ret=0.
  )
  else
    &ret=-1.
  RETURN &ret &reshr &reshs &y ; END DefineRange
  ; RETURN &ret -> -1 error 0. ok  &reshr/rsehs -> Mask which addrranges are used  &y -> Count which addr comparators are used

DefineSAddress:
  LOCAL &nr &sax &x &y &resh &ret &yr &str &type &ret &data
  ENTRY &nr &sax &x &y &resh

  if &x==0.; ARange
  (
    if &nr=="1"
      &yr=&y*0x100000
    else
      &yr=&y*0x010000
    if etm.addrcomp()==0.
    (
      print %error "Could not write A-Range definition. No address comparators available!"
      &type=-1.
    )
    else
    (
      gosub CheckAddrUsage "A"
      ENTRY &type &data
    )
  )
  else if &x==1. ;BRange
  (
    if &nr=="1"
      &yr=&y*0x001000
    else
      &yr=&y*0x000100
    if etm.addrcomp()==0.
    (
      print %error "Could not write B-Range definition. No address comparators available!"
      &type=-1.
    )
    else if etm.addrcomp()==1.
    (
      print %error "Could not write B-Range definition. Only 1 address comparators available!"
      &type=-1.
    )
    else
    (
      gosub CheckAddrUsage "B"
      ENTRY &type &data
    )
  )
  else ;CRange
  (
    if &nr=="1"
      &yr=&y*0x000010
    else
      &yr=&y*0x000001
    if etm.addrcomp()==0.
    (
      print %error "Could not write C-Range definition. No address comparators available!"
      &type=-1.
    )
    else if etm.addrcomp()==1.||etm.addrcomp()==2.
    (
      print %error "Could not write C-Range definition. Only 1/2 address comparators available!"
      &type=-1.
    )
    else
    (
      gosub CheckAddrUsage "C"
      ENTRY &type &data
    )
  )
  if "&type"!="-1."&&"&type"!="0."
  (
    &resh=&resh+&yr
    &str="ETM.SET ADDRESS "+"&y"+" "+"&type"+" "+"&sax"
    WRITE #1 "&str"
    if "&data"=="DATA0"||"&data"=="DATA1"||"&data"=="!DATA0"||"&data"=="!DATA1"
    (
      LOCAL &inv
      ; Datencomparator auch gleich mitschreiben
      if "&data"=="DATA0"||"&data"=="!DATA0"
      (
        &sax=dialog.string(RD01)
        &type=dialog.string(RD02)
      )
      else ; DATA1
      (
        &sax=dialog.string(RD11)
        &type=dialog.string(RD12)
      )
      if "&type"=="BYTE"
        &sax=&sax|0xxxxxxx00
      else if "&type"=="WORD"
        &sax=&sax|0xxxxx0000
      &inv=""
      if "&data"=="!DATA0"||"&data"=="!DATA1"
        &inv="!= "
      &data="ETM.SET DATA "+"&y"+" "+"&type"+" "+"&inv"+"&sax"
      WRITE #1 "&data"
    )
    &y=&y+0x1
    &ret=0.
  )
  else if "&type"=="0."
  ( ; Defined but never used
    &resh=&resh+&yr
    &str="ETM.SET ADDRESS "+"&y"+" EXECUTE "+"&sax"
    WRITE #1 "&str"
    &y=&y+0x1
    &ret=0.
  )
  else
    &ret=-1.
  RETURN &ret &resh &y ; END DefineSAddress
  ; RETURN &ret -> -1 error 0. ok  rseh -> Mask which addrranges are used  &y -> Count which addr comparators are used

CheckAddrUsage:
  LOCAL &type &access &data &x &y &action
  ENTRY &type
  &access=""
  &data=""
  &action=""""""
  ; alle Condtions durchgehen
  ;   prüfen ob &type in einer Form verwendet Kombinationen nicht vergessen!
  ;   prüfen ob Acess gesetzt
  ;   wenn ja
  ;      prüfen ob Acess logisch
  ;      wenn ja
  ;        &access übergeben
  &y=0.
  while &y<3.&&"&access"!="-1."
  (
    LOCAL &xende
    &x=1.
    if &y==0.
      &xende=5.
    else
      &xende=4.
    while &x<&xende&&"&access"!="-1."
    (
      LOCAL &a &r &rw &d &c &e &found &sa3a
      gosub GetXLineY &y &x
      ENTRY &a &r &rw &d &c &e
      if &rw=="(READ||WRITE)"
        &rw="""ACCESS"""

      &found=0.
      if (&type=="A"&&(&r=="AB"||&r=="AB&&BB"||&r=="AB&&CB"||&r=="!AB"))
       &found=1.
      if (&type=="B"&&(&r=="BB"||&r=="AB&&BB"||&r=="BB&&CB"||&r=="!BB"))
       &found=1.
      if (&type=="C"&&(&r=="CB"||&r=="AB&&CB"||&r=="BB&&CB"||&r=="!CB"))
       &found=1.

      if &found==1.
      (
         &action="&a"
         if (&rw==""&&&d!="DATA0"&&&d!="DATA1"&&&d!="!DATA0"&&&d!="!DATA1")
         (;no access no data defined -> default: EXECUTE
           if "&access"==""&&&action=="View Data"
             &access="ACCESS"
           if "&access"==""
             &access="EXECUTE"
           else if "&access"!="EXECUTE"
           (
             LOCAL &txt
             &txt="Access definition conflict for "+&type+"-Range ! "+"&access"+" != EXECUTE level "+"&y"+" line "+"&x"
             print %error "&txt"
             &access=-1.
           )
         )
         else if (&d=="DATA0"||&d=="DATA1"||&d=="!DATA0"||&d=="!DATA1")
         ( ; data defined
           if "&access"==""
           (
             if &rw=="READ"||&rw=="WRITE"||&rw=="ACCESS"
               &access=&rw
             else if &rw==""
               &access="ACCESS"
             else if &rw=="FETCH"||&rw=="EXECUTE"||&rw=="EXECUTEPASS"||&rw=="EXECUTEFAIL"
             (
               LOCAL &txt
               &txt=&rw+" not allowd with DATA condition. level "+"&y"+" line "+"&x"
               print %error "&txt"
               &access=-1.
             )
           )
           else
           (
             if &rw==""
               &rw="""ACCESS"""
             if &rw!="&access"&&&rw!="ACCESS"&&"&access"!="ACCESS"
             (
               LOCAL &txt
               &txt="Access definition conflict for "+&type+"-Range ! "+"&access"+" != "+&rw+" level "+"&y"+" line "+"&x"
               print %error "&txt"
               &access=-1.
             )
           )
           if "&data"==""
             &data=&d
           else if "&data"!=&d
           (
             LOCAL &txt
             &txt="Data definition conflict for "+&type+"-Range ! "+"&data"+" != "+&d+" level "+"&y"+" line "+"&x"
             print %error "&txt"
             &access=-1.
           )
         )
         else if "&access"==""
           &access=&rw
         else if "&access"!=""&&&rw!=""&&&rw!="&access"
         (
           if &rw==""
             &rw="""EXECUTE"""
           LOCAL &txt
           &txt="Access definition conflict for "+&type+"-Range ! "+"&access"+" != "+&rw+" level "+"&y"+" line "+"&x"
           print %error "&txt"
           &access=-1.
         )
      )
      &x=&x+1.
    )
    &y=&y+1.
  )

  if "&access"==""&&&action=="View Data"
    &access="ACCESS"
  else if "&access"==""
    &access=0.
  if "&data"==""
    &data=0.
  RETURN &access &data

CodeADDRange:
  LOCAL &resr &ress &maxc &maxd &aaddr &mask
  ENTRY &resr &ress &maxc

  &mask=&ress&0xf00000
  if &mask!=0. ;A0 is range
  (
    &maxd=&maxc*0x100000
    &resr=&resr+&maxd
  )
  &mask=&ress&0x0f0000
  if &mask!=0. ;A1 is range
  (
    &maxd=&maxc*0x010000
    &resr=&resr+&maxd
  )
  &mask=&ress&0x00f000
  if &mask!=0. ;B0 is range
  (
    &maxd=&maxc*0x001000
    &resr=&resr+&maxd
  )
  &mask=&ress&0x000f00
  if &mask!=0. ;B1 is range
  (
    &maxd=&maxc*0x000100
    &resr=&resr+&maxd
  )
  &mask=&ress&0x0000f0
  if &mask!=0. ;C0 is range
  (
    &maxd=&maxc*0x000010
    &resr=&resr+&maxd
  )
  &mask=&ress&0x00000f
  if &mask!=0. ;C1 is range
  (
    &maxd=&maxc*0x000001
    &resr=&resr+&maxd
  )
  RETURN &resr

CollectAnd:
  LOCAL &sand &level &line &r &rw &d &c &e &rx &rwx &dx &cx &addr &addrc &amask &maxc &ex
  ENTRY &sand &level &line &amask &maxc &r &rw &d &c &e
  &addr=""
  &addrc=""

  gosub ConvETM &amask &maxc &level &line &r &rw &d &c &e
  ENTRY &rx &rwx &dx &cx &ex

  if &r!=""||&rx!=""
  (
    if &r!=""
       &addrc=&r
    if &rx!=""
       &addr=&rx
  )
  &addrc="&addrc"+","
  if &rw!=""||&rwx!=""
  (
    if &rw!=""
      &addrc="&addrc"+&rw
    if &rwx!=""
      &addr="&addr"+&sand+&rwx
  )
  &addrc="&addrc"+","
  if &c!=""||&cx!=""
  (
    if &c!=""
      &addrc="&addrc"+&c
    if &cx!=""
      &addr="&addr"+&sand+&cx
  )
  &addrc="&addrc"+","
  if &d!=""||&dx!=""
  (
    if &d!=""
      &addrc="&addrc"+&d
    if &dx!=""
      &addr="&addr"+&sand+&dx
  )
  &addrc="&addrc"+","
  if &e!=""||&ex!=""
  (
    if &e!=""
      &addrc="&addrc"+&e
    if &ex!=""
      &addr="&addr"+&sand+&ex
  )
  if string.mid("&addr",0.,4.)==&sand
    &addr=string.mid("&addr",4,80.)
  RETURN "&addr" &addrc

ConvETM:
  LOCAL &amask &maxc &level &line &r &rw &d &c &ct &lev &t1 &t2 &a &ar &e
  ENTRY &amask &maxc &level &line &r &rw &d &c &e

  &t1=string.cut("&level",-1.)
  &t2=string.cut("&line",-1.)
  &ar="S"+"&t1"+"A"+"&t2"
  &a=dialog.string(&ar)
  &a=string.scan("&a","Goto",0.)

  &level=&level+1.
  &lev=string.mid("&level",0.,1.)

  if &d=="DATA0"||&d=="DATA1"||&d=="!DATA0"||&d=="!DATA1"
    &d=""""""

  if &rw=="READ"||&rw=="WRITE"||&rw=="(READ||WRITE)"||&rw=="FETCH"||&rw=="EXECUTE"||&rw=="EXECUTEPASS"||&rw=="EXECUTEFAIL"
    &rw=""""""

  gosub ConvETMCount &c
  ENTRY &c

  gosub ConvETMExternal &e
  ENTRY &e

  if &r==""&&&a<0.&&(&level>1.||(&stateon!=-1.&&&line>&stateon))
  (
    &r="seq "+"&lev"
    &r="""&r"""
  )
  else ; irgendeine Range definiert
  (
     LOCAL &smask &an &a1 &a2 &bn &b1 &b2 &cn &c1 &c2
     if &r=="AB"||&r=="!AB"||&r=="AB&&BB"||&r=="AB&&CB"
     (
        &an=""
        &smask=&amask&0xF00000
        &smask=&smask/0x100000
        gosub ConvETMArange &smask &maxc
        ENTRY &a1

        &smask=&amask&0x0F0000
        &smask=&smask/0x010000
        gosub ConvETMArange &smask &maxc
        ENTRY &a2
        if &r=="!AB"
        (
         if &a1!=""&&&a2!=""
           &an="! "+&a1+" && ! "+&a2
         else if &a1!=""
           &an="! "+&a1
         else if &a2!=""
           &an="! "+&a2
         else
           &an=""
        )
        else
        (
         if &a1!=""&&&a2!=""
           &an=&a1+" || "+&a2
         else if &a1!=""
           &an=&a1
         else if &a2!=""
           &an=&a2
         else
           &an=""
        )
     )
     if &r=="BB"||&r=="!BB"||&r=="AB&&BB"||&r=="BB&CB"
     (
        &bn=""
        &smask=&amask&0x00F000
        &smask=&smask/0x001000
        gosub ConvETMArange &smask &maxc
        ENTRY &b1

        &smask=&amask&0x000F00
        &smask=&smask/0x000100
        gosub ConvETMArange &smask &maxc
        ENTRY &b2
        if &r=="!BB"
        (
         if &b1!=""&&&b2!=""
           &bn="! "+&b1+" && ! "+&b2
         else if &b1!=""
           &bn="! "+&b1
         else if &b2!=""
           &bn="! "+&b2
         else
           &bn=""
        )
        else
        (
         if &b1!=""&&&b2!=""
           &bn=&b1+" || "+&b2
         else if &b1!=""
           &bn=&b1
         else if &b2!=""
           &bn=&b2
         else
           &bn=""
        )
     )
     else if &r=="CB"||&r=="!CB"||&r=="AB&&CB"||&r=="BB&&CB"
     (
        &cn=""
        &smask=&amask&0x0000F0
        &smask=&smask/0x000010
        gosub ConvETMArange &smask &maxc
        ENTRY &c1

        &smask=&amask&0x00000F
        &smask=&smask/0x000001
        gosub ConvETMArange &smask &maxc
        ENTRY &c2
        if &r=="!CB"
        (
         if &c1!=""&&&c2!=""
           &cn="! "+&c1+" && ! "+&c2
         else if &c1!=""
           &cn="! "+&c1
         else if &c2!=""
           &cn="! "+&c2
         else
           &cn=""
        )
        else
        (
         if &c1!=""&&&c2!=""
           &cn=&c1+" || "+&c2
         else if &c1!=""
           &cn=&c1
         else if &c2!=""
           &cn=&c2
         else
           &cn=""
        )
     )

     if "&an"!=""
     (
       if "&bn"!=""
         &r="&an"+" && "+"&bn"
       else if "&cn"!=""
         &r="&an"+" && "+"&cn"
       else
         &r="&an"
       &r="""&r"""
     )
     else if "&bn"!=""
     (
       if "&cn"!=""
         &r="&bn"+" && "+"&cn"
       else
         &r="&bn"
       &r="""&r"""
     )
     else  if "&cn"!=""
       &r="""&cn"""
  )

  if &rw==""&&&a<0.&&(&level>1.||(&stateon!=-1.&&&line>&stateon))&&!string.find("&r","seq")
  (
    &rw="seq "+"&lev"
    &rw="""&rw"""
  )

  if &d==""&&&a<0.&&(&level>1.||(&stateon!=-1.&&&line>&stateon))&&!string.find("&rw","seq")&&!string.find("&r","seq")
  (
    &d="seq "+"&lev"
    &d="""&d"""
  )
  RETURN &r &rw &d &c &e

ConvETMCount:
  LOCAL &c &ct
  ENTRY &c
    if &c=="COUNT0"
      &ct="COUNT 1"
    else if &c=="COUNT1"
      &ct="COUNT 2"
    else if &c=="!COUNT0"
      &ct="! COUNT 1"
    else if &c=="!COUNT1"
      &ct="! COUNT 2"
    else
      &ct=""
    &ct="""&ct"""
  RETURN &ct

ConvETMExternal:
  LOCAL &e &et
  ENTRY &e
    if &e=="EXT0"
      &et="EXTERNAL 1"
    else if &e=="EXT1"
      &et="EXTERNAL 2"
    else if &e=="!EXT0"
      &et="! EXTERNAL 1"
    else if &e=="!EXT1"
      &et="! EXTERNAL 2"
    else
      &et=""
    &et="""&et"""
  RETURN &et

ConvETMArange:
  LOCAL &mask &ret
  ENTRY &mask &maxc
  &ret=""
  if &mask>0.
  (
    if &mask>&maxc
    (
      &mask=&mask-&maxc
      &ret="RANGE "+"&mask"
    )
    else
      &ret="ADDRESS "+"&mask"
  )
  RETURN "&ret"

WriteDATA:
  LOCAL &sa1 &sa1t &datac &data &res &noco &acode &acompv &acomp1 &acomp2 &acomp3 &acomp4 &maxc &x &y
  ENTRY &noco &acode &maxc

  &res=0.
  &y=0.
  while &y<2.&&&res>=0.
  (
    if &y==0.
    (
      &sa1=dialog.string(RD01)
      &sa1t=dialog.string(RD02)
    )
    else
    (
      &sa1=dialog.string(RD11)
      &sa1t=dialog.string(RD12)
    )

    if "&sa1"!=""
    (
      if etm.datacomp()==0.
      (
        print %ERROR "Could not write data definition DATA0. No data comparators available!"
        &sa1=""
        &res=-1.
      )
      else if etm.datacomp()==1.&&&y==1.
      (
        print %ERROR "Could not write data definition DATA1. Only 1 data comparator available!"
        &sa1=""
        &res=-1.
      )
      else
      (
        gosub CheckData &sa1 &sa1t
        ENTRY &sa1
      )
      if "&sa1"!=""
      (
        LOCAL &datax
        if &y==0.
        (
          &datac=";#D0,"+"&sa1t"+","+"&sa1"
          WRITE #1 "&datac"
          &datax="""DATA0"""
        )
        else
        (
          &datac=";#D1,"+"&sa1t"+","+"&sa1"
          WRITE #1 "&datac"
          &datax="""DATA1"""
        )
        if &y==0.&&&res>=0.
          &res=0y10
        else if &res>=0.
          &res=&res+0y1
      )
      else
        &res=-1.
    )
    &y=&y+1.
  )
  RETURN &res

GetNumberAddrComp:
; zum Datenevent den zugehörigen Addresscomparator herausfinden
  LOCAL &data &ndata &amask &comp1 &comp2 &comp3 &comp4 &x &y &a &r &rw &d &c &e &found &smask &ret
  ENTRY &data &amask &maxc
  &comp1=-1.
  &comp2=-1.
  &comp3=-1.
  &comp4=-1.
  &ndata="NOT "+&data
  &y=0.
  while &y<3.
  (
    &x=1.
    if &y==0.
      &xende=5.
    else
      &xende=4.
    while &x<&xende
    (
      gosub GetXLineY &y &x
      ENTRY &a &r &rw &d &c &e

      if &d==&data||&d=="&ndata"
      (
        if (&r=="AB"||&r=="AB&&BB"||&r=="AB&&CB"||&r=="!AB")
        (
          LOCAL &x
          &smask=&amask&0xF00000
          &smask=&smask/0x100000
          &x=0.
          while &x<2.
          (
            if &smask>&maxc
            (
              LOCAL &ra &re
              ; es ist eine Range
              &smask=&smask-&maxc
              &re=&smask*0x2
              &ra=&re-0x1
              if (&comp1==-1.&&&comp2==-1.)||(&comp1==&ra&&&comp2==&re)
              (
                &comp1=&ra
                &comp2=&re
              )
              else if (&comp3==-1.&&&comp4==-1.)||(&comp3==&ra&&&comp4==&re)
              (
                &comp3=&ra
                &comp4=&re
              )
              else
              (
                &ret="Too many data comparators used for action "+&a+" event "+&r+" "+&rw+" "+&d
                print %ERROR "&ret"
                &sa1=-1.
              )
            )
            else if &smask!=0x0
            (
            ; es ist eine normale Addresse
              if &comp1==-1.
                &comp1=&smask
              else if &comp2==-1.
                &comp2=&smask
              else if &comp3==-1.
                &comp3=&smask
              else if &comp4==-1.
                &comp4=&smask
              else
              (
                &ret="Too many data comparators used for action "+&a+" event "+&r+" "+&rw+" "+&d
                print %ERROR "&ret"
                &sa1=-1.
              )
            )
            &smask=&amask&0x0F0000
            &smask=&smask/0x010000
            &x=&x+1.
          )
        )
        if (&r=="BB"||&r=="AB&&BB"||&r=="BB&&CB"||&r=="!BB")
        (
          LOCAL &x
          &smask=&amask&0x00F000
          &smask=&smask/0x001000
          &x=0.
          while &x<2.
          (
            if &smask>&maxc
            (
              LOCAL &ra &re
              ; es ist eine Range
              &smask=&smask-&maxc
              &re=&smask*0x2
              &ra=&re-0x1
              if (&comp1==-1.&&&comp2==-1.)||(&comp1==&ra&&&comp2==&re)
              (
                &comp1=&ra
                &comp2=&re
              )
              else if (&comp3==-1.&&&comp4==-1.)||(&comp3==&ra&&&comp4==&re)
              (
                &comp3=&ra
                &comp4=&re
              )
              else
              (
                &ret="Too many data comparators used for action "+&a+" event "+&r+" "+&rw+" "+&d
                print %ERROR "&ret"
                &sa1=-1.
              )
            )
            else if &smask!=0x0
            (
            ; es ist eine normale Addresse
              if &comp1==-1.
                &comp1=&smask
              else if &comp2==-1.
                &comp2=&smask
              else if &comp3==-1.
                &comp3=&smask
              else if &comp4==-1.
                &comp4=&smask
              else
              (
                &ret="Too many data comparators used for action "+&a+" event "+&r+" "+&rw+" "+&d
                print %ERROR "&ret"
                &sa1=-1.
              )
            )
            &smask=&amask&0x000F00
            &smask=&smask/0x000100
            &x=&x+1.
          )
        )
        if (&r=="CB"||&r=="AB&&CB"||&r=="BB&&CB"||&r=="!CB")
        (
          LOCAL &x
          &smask=&amask&0x0000F0
          &smask=&smask/0x000010
          &x=0.
          while &x<2.
          (
            if &smask>&maxc
            (
              LOCAL &ra &re
              ; es ist eine Range
              &smask=&smask-&maxc
              &re=&smask*0x2
              &ra=&re-0x1
              if (&comp1==-1.&&&comp2==-1.)||(&comp1==&ra&&&comp2==&re)
              (
                &comp1=&ra
                &comp2=&re
              )
              else if (&comp3==-1.&&&comp4==-1.)||(&comp3==&ra&&&comp4==&re)
              (
                &comp3=&ra
                &comp4=&re
              )
              else
              (
                &ret="Too many data comparators used for action "+&a+" event "+&r+" "+&rw+" "+&d
                print %ERROR "&ret"
                &sa1=-1.
              )
            )
            else if &smask!=0x0
            (
            ; es ist eine normale Addresse
              if &comp1==-1.
                &comp1=&smask
              else if &comp2==-1.
                &comp2=&smask
              else if &comp3==-1.
                &comp3=&smask
              else if &comp4==-1.
                &comp4=&smask
              else
              (
                &ret="Too many data comparators used for action "+&a+" event "+&r+" "+&rw+" "+&d
                print %ERROR "&ret"
                &sa1=-1.
              )
            )
            &smask=&amask&0x00000F
            &smask=&smask/0x000001
            &x=&x+1.
          )
        )
      )
      &x=&x+1.
    )
    &y=&y+1.
  )
  if &comp1==-1.
    &comp1=0.
  if &comp2==-1.
    &comp2=0.
  if &comp3==-1.
    &comp3=0.
  if &comp4==-1.
    &comp4=0.
  RETURN &comp1 &comp2 &comp3 &comp4 ; END GetNumberAddrComp

DataToMask: ; NOT USED ANYMORE !!!!
; alle X durch 1 ersetzten in der Maske
  LOCAL &value &mask &x &xbeg &xend &bin
  ENTRY &value
  &mask=""
  &xbeg=0.
  &bin=0.
  ; erstes x/y ist von 0x... oder 0y...
  &x=string.scan("&value","y",&xbeg)
  if &x==-1.
  (
    &x=string.scan("&value","Y",&xbeg)
    if &x==-1.
    (
      &x=string.scan("&value","x",&xbeg)
      if &x==-1.
      (
        &x=string.scan("&value","X",&xbeg)
        if &x==-1.
        (
          return &value 0.
        )
      )
    )
    else
      &bin=1.
  )
  else
    &bin=1.
  if &bin==1.
    &mask="0y"
  else
    &mask="0x"
  &xbeg=&x+1.
  ; jetzt geht erst die Maske los
  &x=string.scan("&value","x",&xbeg)
  if &x==-1.
  (
    &x=string.scan("&value","X",&xbeg)
    if &x==-1.
      return &value 0.
  )

  while &x!=-1
  (
    &xend=&x+1.
    &x=&x-&xbeg
    if &bin==1.
      &mask="&mask"+string.mid("&value",&xbeg,&x)+"1"
    else
      &mask="&mask"+string.mid("&value",&xbeg,&x)+"f"
    &xbeg=&xend
    &x=string.scan("&value","x",&xbeg)
    if &x==-1.
      &x=string.scan("&value","X",&xbeg)
  )
  if &xbeg<string.len("&value")
  (
    &x=string.len("&value")-&xbeg
    if &bin==1.
      &mask="&mask"+string.mid("&value",&xbeg,&x)
    else
      &mask="&mask"+string.mid("&value",&xbeg,&x)
  )
  if "&mask"!=""
    &value=&mask
  RETURN &value &mask ; END DataToMask

WriteCOUNT:
  LOCAL &sa1 &datac &data &ret &res &noco &cmax &x &y &lmask &rmask &level
  ENTRY &noco

  &res=0.
  &level=-1.
  &sa1=dialog.string(EC0)
  if "&sa1"!=""
  (
    on error gosub
      return

    if etm.counters()==0.
    (
      print %ERROR "Could not write counter definition COUNT0. No counters available!"
      &sa1=-1.
    )
    else
    (
      EVAL &sa1
      &ret=eval.type()
      if &ret!=&DInteger&&&ret!=&DIntNum&&&ret!=&DFloat&&&ret!=&DBinary&&&ret!=&DHex&&&ret!=&DNumeric
      (
        &ret="&sa1"+" no valid count value!"
        print %ERROR "&ret"
        &sa1=-1.
      )
    )

    if &sa1>-1.
    (
        &datac=";#C0,"+"&sa1"
        WRITE #1 "&datac"
      &data="ETM.SET COUNT 1 "+"&sa1"
      WRITE #1 "&data"
      &res=0y10
    )
    else
      &res=-1.
  )
  &sa1=dialog.string(EC1)
  if "&sa1"!=""&&&res>=0.
  (
    on error gosub
      return
    if etm.counters()==0.
    (
      print %ERROR "Could not write counter definition COUNT1. No counters available!"
      &sa1=-1.
    )
    else if etm.counters()==1.
    (
      print %ERROR "Could not write data definition COUNT1. Only 1 counter available!"
      &sa1=-1.
    )
    else
    (
      EVAL &sa1
      &ret=eval.type()
      if &ret!=&DInteger&&&ret!=&DIntNum&&&ret!=&DFloat&&&ret!=&DBinary&&&ret!=&DHex&&&ret!=&DNumeric
      (
        &ret="&sa1"+" no valid count value!"
        print %ERROR "&ret"
        &sa1=-1.
      )
    )
    if &sa1>-1.
    (
        &datac=";#C1,"+"&sa1"
        WRITE #1 "&datac"
      &data="ETM.SET COUNT 2 "+"&sa1"
      WRITE #1 "&data"
      &res=&res+0y1
    )
    else
      &res=-1.
  )
  RETURN &res ; END WriteCount
  ; RETURN &res -> -1 if error  mask 0yxx if Countx is defined

WriteStates:
  LOCAL &x &y &addr &addrc &state &ad &z &add &co &cx &sta &noco &amask &maxc
  ENTRY &ad &noco &amask &maxc

  &z=0.
  &y=0.
  &add=&ad
  &cx=0.
  &sta=0y0
  &co=0y00
  while &y<3.
  (
    LOCAL &xende &a &r &rw &d &e &c
    &addr=""
    &x=1.
    if &y==0.
      &xende=5.
    else
      &xende=4.
    while &x<&xende
    (
      if &x==1.
      (
        gosub GetXLineY &y &x
        ENTRY &a &r &rw &d &c &e
      )
      else
      (
        if &addr!=""
        (
          LOCAL &coloc &stateloc
          gosub WriteActLine &y &x &addr &a &addrc &noco
          ENTRY &coloc &stateloc
          if &coloc==-1.&&&stateloc==-1.
          (
            &z=-1.
            RETURN &z &y &x
          )
          &co=&coloc|&co
          &sta=&stateloc|&sta
          &z=&z+1.
        )
        gosub GetXLineY &y &x
        ENTRY &a &r &rw &d &c &e
      )
      if "&sa1"!=""
      (
        LOCAL &addx &ar &t0 &t1 &t2
        &t1=string.cut("&y",-1.)
        &t2=string.cut("&x",-1.)
        &ar="S"+"&t1"+"AR"+"&t2"
        &t0=dialog.string(&ar)

        if "&t0"!=""
        (
          gosub CheckARange "&t0" &y &x
          ENTRY &ar
        )
        else
          &ar=0.
        gosub CheckBed &ad &add &amask &maxc &y &a &r &rw &d &c &e
        ENTRY &addx &add
        if &addx!="1"&&&addx!=""&&&ar==0.
        (
          gosub CollectAnd " && " &y &x &amask &maxc &r &rw &d &c &e
          ENTRY &addr &addrc
          if &addr==""
          (
            &addr="""TRUE"""
            &addrc=",,,"
          )
          else if &addr=="0."
          (
            &z=-1.
            RETURN &z &y &x
          )
        )
        else if &addx=="1"||&ar>0.
        (
          &z=-1.
          RETURN &z &y &x
        )
        else
          &addr=""""""
      )
      else
        &addr=""""""
      if &addr!=""
      (
        LOCAL &cxloc &cmask0 &cmask1 &cmask2
        &cmask0=&cx&0y001
        &cmask1=&cx&0y010
        &cmask2=&cx&0y100
        if (&y==0.&&&cmask0==0.)||(&y==1.&&&cmask1==0.)||(&y==2.&&&cmask2==0.)
        (
          LOCAL &state &t1
          WRITE #1 " "
          &t1=string.cut("&y",-1.)
          &state=";LEVEL"+"&t1"+":"
          WRITE #1 "&state"
          if &y==0.
            &cxloc=0y1
          else if &y==1.
            &cxloc=0y10
          else if &y==2.
            &cxloc=0y100
          &cx=&cxloc|&cx
        )
      )
      &x=&x+1.
    )
    if &addr!=""; letzte Zeile im Level
    (
      ;&x=&x-1.
      LOCAL &coloc &stateloc
      gosub WriteActLine &y &x &addr &a &addrc &noco
      ENTRY &coloc &stateloc
      if &coloc==-1.&&&stateloc==-1.
      (
        &z=-1.
        RETURN &z &y &x
      )
      &co=&coloc|&co
      &sta=&stateloc|&sta
      &z=&z+1.
    )
    &y=&y+1.
  )
end:

  if &sta!=&cx
  (
    LOCAL &mask1 &mask2 &maskl0 &maskl1 &txt
    &txt=""
    &maskl0=&cx&0y001
    &maskl1=&cx&0y010
    &mask1=&sta&0y010
    if &mask1==0y0&&&maskl1!=0y0
      &txt="Level1 defined but not referenced!"
    else if &mask1!=0.&&&maskl1==0.
      &txt="Level1 referenced but not defined!"
    &mask1=&sta&0y100
    &mask2=&cx&0y100
    if &mask1==0y0&&&mask2!=0y0&&(&mask1!=0y0||&maskl1!=0y0)
      &txt="Level2 defined but not referenced!"
    else if &mask1!=0.&&&mask2==0.
      &txt="Level2 referenced but not defined!"
    if "&txt"!=""
    (
      if &mask1!=0.
      (
        gosub ShowError "&txt"
        &z=0.
      )
      else
        gosub ShowWarn "&txt"
      if &add==&ad
        &ad=&ad+0y1110000
    )
  )
  if &z==0.&&&add==&ad
    gosub ShowWarn "No definition used!"
  else if &z>0.&&&add>0.
  (
    gosub ShowUnusedDefs &add
    ENTRY &xende
    if &xende<0.
      &z=-1.
  )
  &add=&ad&0y0000010
  &cx=&co&0y00000001
  if &z>0.&&(&add==0y0000010)&&(&cx!=0y01)
  (
    gosub ShowWarn "Count0 used but never incremented!"
  )
  &add=&ad&0y0000001
  &cx=&co&0y00000010
  if &z>0.&&&add==0y00000001&&(&cx!=0y10)
  (
    gosub ShowWarn "Count1 used but never incremented!"
  )
  RETURN &z &y &x ; END WriteStates
  ; RETURN &z -> No of lines written -1 if error  &y -> No of level   &x -> No of line in level


GetXLineY:
  LOCAL &x &y &d1 &d2 &d3 &d4 &d5 &d6 &a1 &a2 &a3 &a4 &a5 &a6 &t1 &t2
  ENTRY &x &y
  &t1=string.cut("&x",-1.)
  &t2=string.cut("&y",-1.)
  &d1="S"+"&t1"+"A"+"&t2"
  &d2="S"+"&t1"+"AR"+"&t2"
  &d3="S"+"&t1"+"WR"+"&t2"
  &d4="S"+"&t1"+"D"+"&t2"
  &d5="S"+"&t1"+"C"+"&t2"
  &d6="S"+"&t1"+"E"+"&t2"
  &a1=""
  &a2=""
  &a3=""
  &a4=""
  &a5=""
  &a6=""
  &a1=dialog.string(&d1)
  &a2=dialog.string(&d2)
  gosub ConvRange "&a2"
  ENTRY &a2
  &a3=dialog.string(&d3)
  if "&a3"=="ACCESS"
    &a3="(READ||WRITE)"
  &a4=dialog.string(&d4)
  gosub ConvDaCo "&a4"
  ENTRY &a4
  &a5=dialog.string(&d5)
  gosub ConvDaCo "&a5"
  ENTRY &a5
  &a6=dialog.string(&d6)
  gosub ConvDaCo "&a6"
  ENTRY &a6
  RETURN "&a1" "&a2" "&a3" "&a4" "&a5" "&a6" ; END GetXLineY

ConvRange:
  LOCAL &ran
  ENTRY &ran

  if &ran!=""
  (
    if &ran=="A-Range"
      &ran="AB"
    else if &ran=="B-Range"
      &ran="BB"
    else if &ran=="C-Range"
      &ran="CB"
    else if &ran=="AB-Range"
      &ran="AB&&BB"
    else if &ran=="AC-Range"
      &ran="AB&&CB"
    else if &ran=="BC-Range"
      &ran="BB&&CB"
    else if &ran=="NOT A-Range"
      &ran="!AB"
    else if &ran=="NOT B-Range"
      &ran="!BB"
    else if &ran=="NOT C-Range"
      &ran="!CB"
    else
      &ran="ERROR"
  )
  else
    &ran=""
  RETURN &ran ; END ConvRange
  ; RETURN &ran -> "AB" or "BB" ... "ERROR" if wrong condtion

ConvDaCo:
  LOCAL &bed &not
  ENTRY &bed
  if &bed!=""
  (
    &not=string.mid(&bed,0.,3.)
    if "&not"=="NOT"
    (
      &not=string.mid(&bed,4.,2.)
      if "&not"=="DA"
        &not=string.mid(&bed,4.,5.)
      else if "&not"=="CO"
        &not=string.mid(&bed,4.,6.)
      else if "&not"=="EX"
        &not=string.mid(&bed,4.,5.)
      &bed="!"+"&not"
    )
    else
    (
      &not=&bed
      &bed="&not"
    )
  )
  else
    &bed=""
  RETURN &bed ; END ConvDaCo
  ; RETURN &bed -> "! COUNTx" or "! DATAx" or "DATAx" or "COUNTx" or ""


WriteActLine:
  LOCAL &addr &sact &addrc &out &count &level &noco &continue &lev &lin &ret &orpos
  ENTRY &lev &lin &addr &sact &addrc &noco

  &count=0y00
  &level=0y00
  &ret=0.
  if &sact!=""
  (
    LOCAL &l
    &l=&lin-1.
    gosub CheckActTwice 1. &sact &lev &l
    ENTRY &ret
    if &ret>0.&&(&addr==""||&addr=="TRUE"||&addr=="seq 1"||&addr=="seq 2"||&addr=="seq 3")
    (
      &ret=&ret/10.
      &lev=&lev+1.
      if &addr!=""&&&addr!="TRUE"
      (
        &addr=&addr+" || seq "+format.hex(1.,&ret)
        &addr="""&addr"""
      )
    )
    if &ret<0.
    (
      &count=-1.
      &level=-1.
    )
  )

  if &sact=="Trace"
  (
      &out=";#SE,"+"&addrc"
      WRITE #1 "&out"
    if &addr==""||&addr=="TRUE"
      &out="ETM.SET TRACEENABLE TRUE"
    else
      &out="ETM.SET TRACEENABLE "+&addr
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
  )
  else if &sact=="Trace (Global)"
  (
      &out=";#SG,"+"&addrc"
      WRITE #1 "&out"
      if (&addr==""||&addr=="TRUE")
       &out="ETM.SET TRACEREGIONINCLUDE"
      else if string.mid(&addr,0,6)=="RANGE "
      (
       &out="ETM.SET TRACEREGIONINCLUDE "+string.mid(&addr,6.,4.)
       &orpos=string.scan(&addr,"||",0.)
       if &orpos!=-1
         &out="&out"+" "+string.mid(&addr,&orpos+9.,4.)
      )
      else
      (
       &out="ETM.SET TRACEADDRESSINCLUDE "+string.mid(&addr,8.,4.)
       &orpos=string.scan(&addr,"||",0.)
       if &orpos!=-1
         &out="&out"+" "+string.mid(&addr,&orpos+11.,4.)
      )
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
  )
  else if &sact=="TraceON (Global)"
  (
      &out=";#ST,"+"&addrc"
      WRITE #1 "&out"
      if !(&addr==""||&addr=="TRUE")
      (
       &out="ETM.SET TRACEON "+string.mid(&addr,8.,4.)
       &orpos=string.scan(&addr,"||",0.)
       if &orpos!=-1
         &out="&out"+" "+string.mid(&addr,&orpos+11.,4.)
      )
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
  )
  else if &sact=="TraceOFF (Global)"
  (
      &out=";#SF,"+"&addrc"
      WRITE #1 "&out"
      if !(&addr==""||&addr=="TRUE")
      (
       &out="ETM.SET TRACEOFF "+string.mid(&addr,8.,4.)
       &orpos=string.scan(&addr,"||",0.)
       if &orpos!=-1
         &out="&out"+" "+string.mid(&addr,&orpos+11.,4.)
      )
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
  )
  else if &sact=="View Data"
  (
      &out=";#DE,"+"&addrc"
      WRITE #1 "&out"
    if &addr==""||&addr=="TRUE"
      &out="ETM.SET VIEWDATA TRUE"
    else
      gosub WriteViewData &addr &lev &ret
  )
  else if &sact=="Trigger TRACE"
  (
      &out=";#TT,"+"&addrc"
      WRITE #1 "&out"
    if &addr==""||&addr=="TRUE"
      &out="ETM.SET TRIGGER TRUE"
    else
      &out="ETM.SET TRIGGER "+&addr
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
  )
  else if &sact=="Goto Level1"||&sact=="Goto Level2"||&sact=="Goto Level0"
  (
    LOCAL &snr
    &snr=string.mid(&sact,10.,1.)
      &out=";#GS"+"&snr"+","+"&addrc"
      WRITE #1 "&out"
    if &addr==""||&addr=="TRUE"
    (
      print %ERROR "Error at Goto LevelX always true!"
      &count=-1.
      &level=-1.
    )
    else
    (
      if &sact=="Goto Level0"
      (
         if "&lev"=="1." ;State2 -> State1
           &out="ETM.SET SEQ2TO1 "+&addr
         else if "&lev"=="2." ;State3 -> State1
           &out="ETM.SET SEQ3TO1 "+&addr
      )
      else if &sact=="Goto Level1"
      (
         if "&lev"=="0." ;State1 -> State2
           &out="ETM.SET SEQ1TO2 "+&addr
         else if "&lev"=="2." ;State3 -> State2
           &out="ETM.SET SEQ3TO2 "+&addr
      )
      else if &sact=="Goto Level2"
      (
         if "&lev"=="0." ;State1 -> State3
           &out="ETM.SET SEQ1TO3 "+&addr
         else if "&lev"=="1." ;State2 -> State3
           &out="ETM.SET SEQ2TO3 "+&addr
      )
    )
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
    if &snr==0.
      &level=0y1
    else if &snr==1.
      &level=0y10
    else if &snr==2.
      &level=0y100
  )
  else if &sact=="Decr COUNT0"||&sact=="Decr COUNT1"
  (
    LOCAL &snr
    &snr=string.mid(&sact,10.,1.)
      &out=";#IC"+"&snr"+","+"&addrc"
      WRITE #1 "&out"
    if &addr==""||&addr=="TRUE"
    (
      if &sact=="Decr COUNT0"
        &out="ETM.SET COUNTENABLE 1 TRUE"
      else
        &out="ETM.SET COUNTENABLE 2 TRUE"
    )
    else
    (
      if &sact=="Decr COUNT0"
        &out="ETM.SET COUNTENABLE 1 "+&addr
      else
        &out="ETM.SET COUNTENABLE 2 "+&addr
    )
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
    if &snr==0.
      &count=0y01
    else
      &count=0y10
  )
  else if &sact=="Reload COUNT0"||&sact=="Reload COUNT1"
  (
    LOCAL &snr
    &snr=string.mid(&sact,12.,1.)
    &out=";#RC"+"&snr"+","+"&addrc"
    WRITE #1 "&out"
    if &addr==""||&addr=="TRUE"
    (
      print %err "Reload Count without event not allowed"
      &count=-1.
      &level=-1.
    )
    else
    (
      if &sact=="Reload COUNT0"
        &out="ETM.SET COUNTRELOAD 1 "+&addr
      else
        &out="ETM.SET COUNTRELOAD 2 "+&addr
    )
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
    if &snr==0.
      &count=0y01
    else
      &count=0y10
  )
  else if &sact=="Set EXT0"||&sact=="Set EXT1"
  (
    LOCAL &snr
    &snr=string.mid(&sact,7.,1.)
    &out=";#EX"+"&snr"+","+"&addrc"
    WRITE #1 "&out"
    if &sact=="Set EXT0"
      &out="ETM.SET EXTERNAL 1 "+&addr
    else
      &out="ETM.SET EXTERNAL 2 "+&addr
    if &ret==0.||&ret>&lev ; nur 1x schreiben
      WRITE #1 "&out"
  )
  RETURN &count &level ; END WriteActLine
  ;  RETURN &count -> 0y00,0y10,0y01,0y11 for count changed;  &count -1. &level -1. -> error

WriteViewData:
  ; Event in ViewDataInclude und ViewDataRangeInclude umwandeln
  LOCAL &addr &lev &ret
  ENTRY &addr &lev &ret

  if (&ret==0.||&ret>&lev)&&&addr!=""&&&addr!="TRUE" ; nur 1x schreiben
  (
    LOCAL &a1 &a2 &at &r1 &r2 &rt &excl
    &excl=STRING.SCAN(&addr,"!",0.)
    &a1=STRING.SCAN(&addr,"ADDRESS",0.)
    &at=&a1+1.
    if &a1>=0.
      &a2=STRING.SCAN(&addr,"ADDRESS",&at)
    else
      &a2=-1.
    &r1=STRING.SCAN(&addr,"RANGE",0.)
    &at=&r1+1.
    if &r1>=0.
      &r2=STRING.SCAN(&addr,"RANGE",&at)
    else
      &r2=-1.
    if &a1>=0.
    (
      gosub GetId "ADDRESS" &addr &a1
      ENTRY &a1
    )
    else
      &a1=""
    if &a2>=0.
    (
      gosub GetId "ADDRESS" &addr &a2
      ENTRY &a2
    )
    else
      &a2=""
    if &r1>=0.
    (
      gosub GetId "RANGE" &addr &r1
      ENTRY &r1
    )
    else
      &r1=""
    if &r2>=0.
    (
      gosub GetId "RANGE" &addr &r2
      ENTRY &r2
    )
    else
      &r2=""

    if &excl>=0
     &out="ETM.SET VDE"
    else
     &out="ETM.SET VDI"
    if "&a1"!=""
      &out="&out"+" "+"&a1"
    if "&a2"!=""
      &out="&out"+" "+"&a2"
    if string.len("&out")>string.len("ETM.SET VDI")
      WRITE #1 "&out"
    if &excl>=0
     &out="ETM.SET VDRE"
    else
     &out="ETM.SET VDRI"
    if "&r1"!=""
      &out="&out"+" "+"&r1"
    if "&r2"!=""
      &out="&out"+" "+"&r2"
    if string.len("&out")>string.len("ETM.SET VDRI")
      WRITE #1 "&out"
  )
  RETURN ; END WriteViewData

GetId:
  LOCAL &addr &type &a1 &at
  ENTRY &type &addr &a1
  ; Numbers are in HEX
  if &type=="RANGE"
    &a1=&a1+8.
  else
    &a1=&a1+10.
  &a1=string.mid(&addr,&a1,2.)
  RETURN &a1 ; END GetId
  ; RETURN &a1 -> ID number as string like "1" or "10"

CheckBed:
  LOCAL &ad &add &a &r &rw &d &c &addr &mask &level &amask &maxc &c &d &e
  ENTRY &ad &add &amask &maxc &level &a &r &rw &d &c &e

  &addr="x"
  if &a==""&&&r==""&&&rw==""&&&d==""&&&c==""&&&e==""
    &addr=""

  if (&a=="Reload COUNT0"||&a=="Decr COUNT0")
  (
    &mask=&ad&0y0000010
    if &mask==0y0
    (
      gosub ShowError "Action on COUNT0: COUNT0 not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y1111101
  )
  if (&a=="Reload COUNT1"||&a=="Decr COUNT1")
  (
    &mask=&ad&0y0000001
    if &mask==0y0
    (
      gosub ShowError "Action on COUNT1: COUNT1 not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y1111110
  )
  if (&c=="COUNT0"||&c=="!COUNT0")
  (
    &mask=&ad&0y0000010
    if &mask==0y0
    (
      gosub ShowError "Condition COUNT0: COUNT0 not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y1111101
  )
  if (&c=="COUNT1"||&c=="!COUNT1")
  (
    &mask=&ad&0y0000001
    if &mask==0y0
    (
      gosub ShowError "Condition COUNT1: COUNT1 not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y1111110
  )
  if (&d=="DATA0"||&d=="!DATA0")
  (
    &mask=&ad&0y0001000
    if &mask==0y0
    (
      gosub ShowError "Condition DATA0: DATA0 not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y1110111
  )
  if (&d=="DATA1"||&d=="!DATA1")
  (
    &mask=&ad&0y0000100
    if &mask==0y0
    (
      gosub ShowError "Condition DATA1: DATA1 not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y1111011
  )
  if (&r=="AB"||&r=="AB&&BB"||&r=="AB&&CB"||&r=="!AB")
  (
    &mask=&ad&0y1000000
    if &mask==0y0
    (
      gosub ShowError "Condition A-Range: A-Range not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y0111111
  )
  if (&r=="BB"||&r=="AB&&BB"||&r=="BB&&CB"||&r=="!BB")
  (
    &mask=&ad&0y0100000
    if &mask==0y0
    (
      gosub ShowError "Condition B-Range: B-Range not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y1011111
  )
  if (&r=="CB"||&r=="AB&&CB"||&r=="BB&&CB"||&r=="!CB")
  (
    &mask=&ad&0y0010000
    if &mask==0y0
    (
      gosub ShowError "Condition C-Range: C-Range not/wrong defined!"
      &addr="1"
    )
    &add=&add&0y1101111
  )
  if (&r=="AB&&BB"||&r=="BB&&CB"||&r=="AB&&CB")&&(&c!=""||&e!=""||&level>0)
  (
    gosub ShowError "Too many conditions defined!"
    &addr="1"
  )
  RETURN "&addr" &add

ShowUnusedDefs:
  LOCAL &def &ret
  ENTRY &def
  &ret=0.
  &mask=&def&0y0000010
  if &mask!=0y0
    gosub ShowWarn "COUNT0 defined but not used!"
  &mask=&def&0y0000001
  if &ret==0.&&&mask!=0y0
    gosub ShowWarn "COUNT1 defined but not used!"

  &mask=&def&0y0000100
  if &ret==0.&&&mask!=0y0
    gosub ShowWarn "DATA1 defined but not used!"
  &mask=&def&0y0001000
  if &ret==0.&&&mask!=0y0
    gosub ShowWarn "DATA0 defined but not used!"
  &mask=&def&0y1000000
  if &ret==0.&&&mask!=0y0
    gosub ShowWarn "A-Range defined but not used!"
  &mask=&def&0y0100000
  if &ret==0.&&&mask!=0y0
    gosub ShowWarn "B-Range defined but not used!"
  &mask=&def&0y0010000
  if &ret==0.&&&mask!=0y0
    gosub ShowWarn "C-Range defined but not used!"
  RETURN &ret


InitProgram:
  if &subdialog!=0.
  (
    LOCAL &text
    if &subdialog==1.
      &text="Please close Define Address Window first!"
    else if &subdialog==2.
      &text="Please close Define Data Window first!"
    else if &subdialog==3.
      &text="Please close Define Count Window first!"
    print %ERROR "&text"
  )
  else
  (
    gosub InitDialogS1
    if &test==1.
      do "~~~~/readetmprogram.cmm" &adir
    else
      do "~~/demo/analyzer/trigger/readetmprogram.cmm" &adir
    ENTRY %line &adir
  )
  RETURN

SetDir:
  LOCAL &i &j &p &file
  ENTRY %line &file
  &i=0.
  &j=string.len("&file")-1.
  &p=string.mid("&file",&j,1.)
  while "&p"!="/"&&"&p"!="\"&&&j>0.
  (
    &j=&j-1.
    &p=string.mid("&file",&j,1.)
    &i=&i+1.
  )
  if &j>0.
  (
    &adir=string.cut("&file",-&i)
    &adir="&adir"+"*.cmm"
  )
  else
    &adir="*.cmm"
  RETURN
; --------------------------------------------------------------------------------
;                               Diverse Hilfsroutinen
; --------------------------------------------------------------------------------
ConvErrString:
  LOCAL &msg &omsg &a
  ENTRY &a
  &msg=string.scan("&a","""",0.)
  if &msg>=0.
  (
    LOCAL &tstr &tstr2
    &tstr=string.mid("&a",0.,&msg)
    &omsg=string.scan("&a","""",&msg+1.)
    while &omsg>=0.
    (
      &tstr2=string.mid("&a",&msg+1.,&omsg-&msg-1.)
      &tstr="&tstr"+"&tstr2"
      &msg=&omsg
      &omsg=string.scan("&a","""",&msg+1.)
    )
    &tstr2=string.mid("&a",&msg+1.,string.len("&a")-&msg-1.)
    &tstr="&tstr"+"&tstr2"
    &omsg="&tstr"
  )
  else
    &omsg="&a"
 RETURN &omsg

CheckData:
  LOCAL &dval &dtype &dmax &x
  ENTRY &dval &dtype
  on error gosub
  (
    &dval=""
    return
  )
  EVAL &dval
  &x=eval.type()
  if (&x!=&DBinary&&&x!=&DBitmask&&&x!=&DHex&&&x!=&DFloat&&&x!=&DIntNum&&&x!=&DNumeric&&&x!=&DInteger)&&"&dval"!=""
  (
    &x="&dval"+" no valid data value! Only single value or mask allowed."
    print %ERROR "&x"
    &dval=""
  )
  else
  (
    LOCAL &dmask
    gosub GetMaxDataVal &dtype
    ENTRY &dmax
  )

  RETURN &dval

GetMaxDataVal:
  LOCAL &smax &type &type1
  ENTRY &type
  if "&type"!=""
  (
    &type1=string.mid("&type",0.,1.)
    if "&type1"=="B"
      &smax=0xff
    else if "&type1"=="W"
      &smax=0xffff
    else if "&type1"=="L"
      &smax=0xffffffff
  )
  else
    &smax=-1.
  RETURN &smax

ShowVersion:
; --------------------------------------------------------------------------------
;                                Dialoge
; --------------------------------------------------------------------------------
  print "&version"
  RETURN

WarnCancel:
   LOCAL &stext &text &ret
   ENTRY &stext
   &ret=1.
   &text=&stext
   print ""
   dialog
   (
          header "WARNING"
          pos 1. 1. 50.
     text:EDIT "" ""
          pos 1. 3. 5.
          DEFBUTTON "OK" "goto oclose"
          pos 7. 3. 6.
          BUTTON "CANCEL" "goto canclose"
          CLOSE "goto oclose"
   )
   dialog.set text "&text"
   stop
canclose:
   &ret=0.
oclose:
   dialog.end
   RETURN &ret

ShowError:
   LOCAL &stext &text
   ENTRY &stext
   &text=&stext
   print ""
   dialog
   (
     header "ERROR"
     pos 1. 1. 40.
text:EDIT "" ""
     pos 1. 3. 5.
     DEFBUTTON "OK" "goto occlose"
     CLOSE "goto occlose"
   )
   dialog.set text "&text"
   stop
occlose:
   dialog.end

   RETURN

ShowWarn:
  LOCAL &stext &text
  ENTRY &stext
  &text=&stext
  print ""
  dialog
  (
         header "WARNING"
         pos 1. 1. 50.
    text:EDIT "" ""
         pos 1. 3. 5.
         DEFBUTTON "OK" "goto occclose"
         CLOSE "goto occclose"
  )
  dialog.set text "&text"
  stop
occclose:
  dialog.end

  RETURN








