; This source code in this file is licensed to You by Castle Technology
; Limited ("Castle") and its licensors on contractual terms and conditions
; ("Licence") which entitle you freely to modify and/or to distribute this
; source code subject to Your compliance with the terms of the Licence.
; 
; This source code has been made available to You without any warranties
; whatsoever. Consequently, Your use, modification and distribution of this
; source code is entirely at Your own risk and neither Castle, its licensors
; nor any other person who has contributed to this source code shall be
; liable to You for any loss or damage which You may suffer as a result of
; Your use, modification or distribution of this source code.
; 
; Full details of Your rights and obligations are set out in the Licence.
; You should have received a copy of the Licence with this source code file.
; If You have not received a copy, the text of the Licence is available
; online at www.castle-technology.co.uk/riscosbaselicence.htm
; 
        SUBT    Arthur.  File ==> &.Arthur.Econet.Commands
        TTL     Econet for Arthur.

HelpTable
        DCB     "Station", 0
        ALIGN
        DCD     0                               ; No code here
        InfoWord 0, 0, Help_Is_Code_Flag
        DCD     0                               ; No syntax
        DCD     DoStarHelpStation - Origin
        [       ErrorInfo
        DCB     "Econet_Info", 0
        ALIGN
        DCD     DoStarErrorInfo - Origin
        InfoWord 0, 0, 0
        DCD     SyntaxOfStarErrorInfo - Origin
        DCD     HelpForStarErrorInfo - Origin
        ]
        [       ControlBlocks
        DCB     "Econet_ControlBlocks", 0
        ALIGN
        DCD     DoStarControlBlocks - Origin
        InfoWord 0, 0, 0
        DCD     SyntaxOfStarControlBlocks - Origin
        DCD     HelpForStarControlBlocks - Origin
        ]
        [       PortInfo
        DCB     "Econet_Ports", 0
        ALIGN
        DCD     DoStarPortInfo - Origin
        InfoWord 0, 0, 0
        DCD     SyntaxOfStarPortInfo - Origin
        DCD     HelpForStarPortInfo - Origin
        ]
        [       Variables
        DCB     "Econet_Variables", 0
        ALIGN
        DCD     DoStarVariables - Origin
        InfoWord 0, 0, 0
        DCD     SyntaxOfStarVariables - Origin
        DCD     HelpForStarVariables - Origin
        ]
        DCD     0

DoStarHelpStation ROUT
        [       UseMsgTrans
        Push    "r0-r6, lr"
        ;       R0 is a buffer address
        ;       R1 is the buffer size
        LDR     wp, [ r12 ]
        SWI     XEconet_ReadTransportType               ; Quick one to ensure we are running and Status2 is up-to-date
        DEC     sp, 28                                  ; Make a stack frame for the data & resultant strings
        ;       SP + 00  ==> Full station address string
        ;       SP + 10  ==> Network speed as a string or zero
        ;       SP + 20  ==> Station number
        ;       SP + 24  ==> Network number
        MOV     r14, #0
        STRB    r14, [ sp, #10 ]                        ; Initialise the second string
        BL      NetworkParameters                       ; Can't SWI because of AUN
        BVS     JustStationNumber
        MOVS    r0, r1                                  ; Value
        BEQ     JustStationNumber
        LD      r1, Status2
        TST     r1, #DCD
        BNE     JustStationNumber                       ; No clock means no frequency
        ADD     r1, sp, #10                             ; Pointer to buffer
        MOV     r2, #10
        SWIVC   XOS_ConvertCardinal2
        ADR     r6, Token_HelpStationFrequency
        B       LookUpHelp

JustStationNumber
        LD      r1, Status2
        TST     r1, #DCD
        ADRNE   r6, Token_HelpStationNoClock
        ADREQ   r6, Token_HelpStation
LookUpHelp
        SWI     XEconet_ReadLocalStationAndNet
        ADD     r2, sp, #20                             ; Pointer to data area
        STMVCIA r2, { r0, r1 }
        MOVVC   r0, r2                                  ; Point at the station number
        MOV     r1, sp
        MOV     r2, #10
        SWIVC   XOS_ConvertNetStation
        MOV     r1, r6                                  ; Token to use
        ADD     r14, sp, #28                            ; Address of incoming R0 and R1
        LDMVCIA r14, { r2, r3 }                         ; Buffer address and length
        MOV     r4, sp                                  ; Station number as text
        ADD     r5, sp, #10                             ; Frequency
        BLVC    MessageTransGSLookup2
ExitStarHelp
        INC     sp, 28
        STRVS   r0, [ sp, #0 ]
        Pull    "r0-r6, pc"

Token_HelpStation
        DCB     "HelpStn", 0
Token_HelpStationNoClock
        DCB     "StnNClk", 0
Token_HelpStationFrequency
        DCB     "StnFreq", 0
        ALIGN
        |
        Push    "r0-r2, lr"
        ;       R0 is a buffer address
        ;       R1 is the buffer size
        LDR     wp, [ r12 ]
        MOV     r2, r1                                  ; Buffer length
        MOV     r1, r0                                  ; Buffer address
        BL      AddString
        DCB     "Econet station ", 0
        ALIGN
        Push    r1
        SWIVC   XEconet_ReadLocalStationAndNet
        Push    "r0, r1"
        MOVVC   r0, sp                                  ; Point at the station number
        LDRVC   r1, [ sp, #8 ]
        SWIVC   XOS_ConvertNetStation
        INC     sp, 12                                  ; Trash two data values and the saved R1
        BVS     ExitHelpCode
        LD      r0, Status2
        TST     r0, #DCD
        BEQ     ExitHelpCode
        BL      AddString
NoClockString
        DCB     " no clock", 0
        ALIGN
ExitHelpCode
        STRVS   r0, [ sp, #0 ]
        Pull    "r0-r2, pc"
        ]

        [       ErrorInfo
HelpForStarErrorInfo
        DCB     "*"
        DCB     TokenEscapeChar,Token0
        DCB     " prints out information about the number"
        DCB     " of errors since starting up.", 13
SyntaxOfStarErrorInfo
        DCB     "Syntax: *"
        DCB     TokenEscapeChar,Token0
        DCB     0

RxMessage
        DCB     "Receive information:", 10, 13, 0
TxMessage
        DCB     10, 13, "Transmit information:", 10, 13, 0
        [       Debug
NetErrorListMessage
        DCB     10, 13, "Net Error Listing:", 10, 13, 0
        ]
        ALIGN

DoStarErrorInfo ROUT
        LDR     wp, [ r12 ]
        Push    "r0-r10, lr"
        MOV     r3, #0
        MOV     r4, #0
        ADR     r0, RxMessage
        SWI     XOS_Write0
        ADRL    r6, RxErrors
        BLVC    PrintErrorStats                         ; Increments R6 to point to TxErrors
        ADRVC   r0, TxMessage
        SWIVC   XOS_Write0
        BLVC    PrintErrorStats
        [       Debug
        ADRVCL  r0, NetErrorListMessage
        SWIVC   XOS_Write0
        BVS     ExitErrorInfo
        ADRL    r4, NetErrorList
NetErrorListLoop
        LDR     r3, NetErrorListPointer
        CMP     r4, r3
        ADRGEL  r4, NetErrorList
        STRGE   r4, NetErrorListPointer
        BGE     ExitErrorInfo
        ADRL    r1, TextBuffer
        MOV     r2, #?TextBuffer
        BL      AddString
        DCB     "Time &", 0
        ALIGN
        LDRVC   r0, [ r4, #8 ]
        SWIVC   XOS_ConvertHex8
        BVS     ExitErrorInfo
        BL      AddString
        DCB     ", PC &", 0
        ALIGN
        LDRVC   r0, [ r4, #4 ]
        BICVC   r0, r0, #ARM_CC_Mask
        SWIVC   XOS_ConvertHex8
        BVS     ExitErrorInfo
        LDR     r0, [ r4, #12 ]
        CMP     r0, #-1
        BEQ     NetErrorNoHandle
        BL      AddString
        DCB     ", Handle &", 0
        ALIGN
        SWIVC   XOS_ConvertHex8
        BVS     ExitErrorInfo
        BL      AddString
        DCB     ", &", 0
        ALIGN
        LDRVCB  r0, [ r4, #3 ]
        SWIVC   XOS_ConvertHex2
        BVS     ExitErrorInfo
        BL      AddString
        DCB     ".&", 0
        ALIGN
        LDRVCB  r0, [ r4, #2 ]
        SWIVC   XOS_ConvertHex2
        BVS     ExitErrorInfo
NetErrorNoHandle
        LDR     r0, [ r4, #0 ]
        TEQ     r0, #255
        BEQ     NetErrorNoSReg1
        BL      AddString
        DCB     ", SReg1 &", 0
        ALIGN
        SWIVC   XOS_ConvertHex2
        BVS     ExitErrorInfo
NetErrorNoSReg1
        LDR     r0, [ r4, #1 ]
        TEQ     r0, #255
        BEQ     NetErrorNoSReg2
        BL      AddString
        DCB     ", SReg2 &", 0
        ALIGN
        SWIVC   XOS_ConvertHex2
        BVS     ExitErrorInfo
NetErrorNoSReg2
        ADRL    r0, TextBuffer
        SWI     XOS_Write0
        SWIVC   XOS_NewLine
        INC     r4, 16
        BVC     NetErrorListLoop
        ]
ExitErrorInfo
        STRVS   r0, [ sp ]
        Pull    "r0-r10, pc"

PrintErrorStats
        Push    lr
        MOV     r7, #0
10
        ADRL    r1, TextBuffer
        MOV     r2, #?TextBuffer
        LDR     r8, [ r6 ], #4
        TEQ     r8, #0
        BEQ     %20
        MOV     r0, r7
        SWI     XEconet_ConvertStatusToString
        Pull    pc, VS
        BL      AddString
        DCB     " = ", 0
        ALIGN
        MOVVC   r0, r8
        SWIVC   XOS_ConvertSpacedCardinal4
        ADRVCL  r0, TextBuffer
        SWIVC   XOS_Write0
        SWIVC   XOS_NewLine
        Pull    pc, VS
20
        INC     r7
        TEQ     r7, #Status_MaxValue+1
        BNE     %10
        ADRL    r1, TextBuffer
        MOV     r2, #?TextBuffer
        BL      AddString
        DCB     "Total = ", 0
        ALIGN
        LDRVC   r0, [ r6 ], #4
        SWIVC   XOS_ConvertSpacedCardinal4
        ADRVCL  r0, TextBuffer
        SWIVC   XOS_Write0
        SWIVC   XOS_NewLine
        Pull    pc
        ]

        [       ControlBlocks
HelpForStarControlBlocks
        DCB     "*"
        DCB     TokenEscapeChar,Token0
        DCB     " prints out info about all currently open"
        DCB     " receive and transmit blocks.", 13
SyntaxOfStarControlBlocks
        DCB     "Syntax: *"
        DCB     TokenEscapeChar,Token0
        DCB     0

DoStarControlBlocks ROUT
        LDR     wp, [ r12 ]
        Push    lr
        LDR     r10, TxCBList
        TEQ     r10, #NIL
        BEQ     ShowOpenRxCBs
        ADR     r0, OpenTxMessage
        SWI     XOS_Write0
        Pull    pc, VS
ShowTxBlockLoop
        LDR     r1, =TxCBIdentifier
        LDR     r2, [ r10, #Offset_Identifier ]
        TEQ     r2, r1
        Pull    "lr", NE
        BNE     InternalErrorExit
        ADRL    r1, TextBuffer
        MOV     r2, #?TextBuffer
        BL      AddCBHandleAndEvent
        Pull    pc, VS
        BL      AddString
        DCB     ", Station ", 0
        ALIGN
        LDRVCB   r6, [ r10, #Offset_Station ]
        LDRVCB   r7, [ r10, #Offset_Network ]
        Push    "r6, r7"
        MOVVC   r0, sp
        SWIVC   XOS_ConvertNetStation
        INC     sp, 8
        Pull    pc, VS
        BL      AddString
        DCB     ", Port &", 0
        ALIGN
        LDRVCB  r0, [ r10, #Offset_Port ]
        SWIVC   XOS_ConvertHex2
        BLVC    AddCBStatus
        ADRVCL  r0, TextBuffer
        SWIVC   XOS_Write0
        SWIVC   XOS_NewLine
        Pull    pc, VS
        LDR     r10, [ r10, #Offset_Link ]
        TEQ     r10, #NIL
        BNE     ShowTxBlockLoop
ShowOpenRxCBs
        LDR     r10, RxCBList
        TEQ     r10, #NIL
        Pull    pc, EQ
        ADR     r0, OpenRxMessage
        SWI     XOS_Write0
ShowRxBlockLoop
        LDR     r1, =RxCBIdentifier
        LDR     r2, [ r10, #Offset_Identifier ]
        TEQ     r2, r1
        Pull    "lr", NE
        BNE     InternalErrorExit
        ADRL    r1, TextBuffer
        MOV     r2, #?TextBuffer
        BL      AddCBHandleAndEvent
        Pull    pc, VS
        BL      AddString
        DCB     ", Station ", 0
        ALIGN
        Pull    pc, VS
        TEQ     r5, #Status_Received
        LDREQB   r6, [ r10, #Offset_Station ]
        LDRNEB   r6, [ r10, #Offset_RxStation ]
        LDREQB   r7, [ r10, #Offset_Network ]
        LDRNEB   r7, [ r10, #Offset_RxNetwork ]
        Push    "r6, r7"
        MOVVC   r0, sp
        SWIVC   XOS_ConvertNetStation
        INC     sp, 8
        Pull    pc, VS
        BL      AddString
        DCB     ", Port &", 0
        ALIGN
        Pull    pc, VS
        TEQ     r5, #Status_Received
        LDREQB  r0, [ r10, #Offset_Port ]
        LDRNEB  r0, [ r10, #Offset_RxPort ]
        SWI     XOS_ConvertHex2
        BLVC    AddCBStatus
        ADRVCL  r0, TextBuffer
        SWIVC   XOS_Write0
        SWIVC   XOS_NewLine
        Pull    pc, VS
        LDR     r10, [ r10, #Offset_Link ]
        TEQ     r10, #NIL
        BNE     ShowRxBlockLoop
        Pull    pc

OpenTxMessage
        DCB     "Open Transmit blocks:", 10, 13, 0
OpenRxMessage
        DCB     "Open Receive blocks:", 10, 13, 0
        ALIGN

AddCBHandleAndEvent
        Push    "lr"
        BL      AddString
        DCB     "Handle &", 0
        ALIGN
        LDRVC   r0, [ r10, #Offset_Handle ]
        SWIVC   XOS_ConvertHex8
        Pull    "pc", VS
        BL      AddString
        DCB     ", Event &", 0
        ALIGN
        LDRVC   r0, [ r10, #Offset_Event ]
        SWIVC   XOS_ConvertHex8
        ADRVCL  r0, TextBuffer
        Pull    "pc"

AddCBStatus
        Push    "r3, r4, lr"
        MOV     r3, r1
        MOV     r4, r2
        BL      AddString
        DCB     ", ==> ", 0
        ALIGN
        LDRVCB  r0, [ r10, #Offset_Status ]
        SWIVC   XEconet_ConvertStatusToString
        Pull    "r3, r4, pc", VC
        MOV     r1, r3
        MOV     r2, r4
        LDR     r3, [ r0 ]
        LDR     r4, =ErrorNumber_BadStatus
        TEQ     r3, r4
        Pull    "r3, r4, pc", NE                                ; Exit, it isn't the error we want
        BL      AddString
        DCB     "Status &", 0
        ALIGN
        LDRVCB  r0, [ r10, #Offset_Status ]
        SWIVC   XOS_ConvertHex2
        Pull    "r3, r4, pc"
        ]

        [       PortInfo
HelpForStarPortInfo
        DCB     "*"
        DCB     TokenEscapeChar,Token0
        DCB     " prints out information about the current"
        DCB     " status of the Port Allocator.", 13
SyntaxOfStarPortInfo
        DCB     "Syntax: *"
        DCB     TokenEscapeChar,Token0
        DCB     0

DoStarPortInfo  ROUT
        LDR     wp, [ r12 ]
        Push    lr
        SWI     XOS_WriteS
        DCB     "Current port hint is ", 0
        ALIGN
        ADRL    r5, TextBuffer
        LD      r0, PortHint, VC
        MOVVC   r1, r5                                  ; TextBuffer
        MOVVC   r2, #?TextBuffer
        SWIVC   XOS_ConvertHex2
        SWIVC   XOS_Write0
        Pull    pc, VS
        SWI     XOS_WriteS
        DCB     13, 10, "Port allocations:", 13, 10
        ALIGN
        Pull    pc, VS
        MOV     r4, #-1                                 ; The port we're looking at
PortInfoLoop
        INC     r4
        CMP     r4, #256                                ; Clears V
        Pull    pc, EQ
        MOV     r0, r4
        BL      FindPort
        LDR     r3, [ r1 ]
        MOV     r3, r3, LSR r2                          ; Put the state at the bottom
        ANDS    r3, r3, #2_11                           ; Mask to get only this value
        BEQ     PortInfoLoop                            ; Zero is the free state
        MOV     r0, r4
        MOV     r1, r5                                  ; TextBuffer
        MOV     r2, #?TextBuffer
        SWI     XOS_ConvertHex2
        Pull    pc, VS
        BL      AddString
        DCB     "  ", 0
        ALIGN
        MOV     r0, r5                                  ; TextBuffer
        SWIVC   XOS_Write0
        Pull    pc, VS

        TEQ     r3, #2_01
        ADREQ   r0, String_Allocated
        BEQ     PrintPortStatus
        TEQ     r3, #2_10
        ADREQ   r0, String_Claimed
        ADRNE   r0, String_Reserved
PrintPortStatus
        SWI     XOS_Write0
        SWIVC   XOS_NewLine
        Pull    pc, VS
        B       PortInfoLoop

String_Allocated
        DCB     "Allocated", 0
String_Claimed
        DCB     "Claimed", 0
String_Reserved
        DCB     "Reserved", 0
        ALIGN
        ]

        [       Variables
HelpForStarVariables
        DCB     "*"
        DCB     TokenEscapeChar,Token0
        DCB     " prints out the values of all the"
        DCB     " important internal variables.", 13
SyntaxOfStarVariables
        DCB     "Syntax: *"
        DCB     TokenEscapeChar,Token0
        DCB     0

DoStarVariables  ROUT
        LDR     wp, [ r12 ]
        Push    lr
        ADR     r0, String_Status
        LDRB    r1, Status2
        BL      PrintNameAndValueAsByte
        ADRVC   r0, String_Time
        LDR     r1, Time
        BLVC    PrintNameAndValueAsWord
        ADRVC   r0, String_PowerDownTime
        LDR     r1, PowerDownTime
        BLVC    PrintNameAndValueAsWord
        ADRVC   r0, String_FIQBusy
        LDRB    r1, FIQBusy
        BLVC    PrintNameAndValueAsByte
        ADRVC   r0, String_FIQStatus
        LDRB    r1, FIQStatus
        BLVC    PrintNameAndValueAsByte
        ADRVC   r0, String_Event
        LDR     r1, EventSequenceNumber
        BLVC    PrintNameAndValueAsWord
        ADRVC   r0, String_Speed
        LDR     r1, BroadcastTime
        BLVC    PrintNameAndValueAsWord

        Pull    pc

String_Status
        DCB     "Status register 2 = &", 0
String_Time
        DCB     "Time = &", 0
String_PowerDownTime
        DCB     "PowerDownTime = &", 0
String_FIQBusy
        DCB     "FIQ busy flag = &", 0
String_FIQStatus
        DCB     "FIQ status flag = &", 0
String_Event
        DCB     "Event sequence number = &", 0
String_Speed
        DCB     "Clock period = &", 0
        ALIGN

PrintNameAndValueAsWord
        ;       R0 => Pointer to name string.
        ;       R1 => The value to print out.
        Push    "r2, lr"
        SWI     XOS_Write0
        MOVVC   r0, r1
        ADRVCL  r1, TextBuffer
        MOVVC   r2, #?TextBuffer
        SWIVC   XOS_ConvertHex8
        SWIVC   XOS_Write0
        SWIVC   XOS_NewLine
        Pull    "r2, pc"

PrintNameAndValueAsByte
        ;       R0 => Pointer to name string.
        ;       R1 => The value to print out.
        Push    "r2, lr"
        SWI     XOS_Write0
        MOVVC   r0, r1
        ADRVCL  r1, TextBuffer
        MOVVC   r2, #?TextBuffer
        SWIVC   XOS_ConvertHex2
        SWIVC   XOS_Write0
        SWIVC   XOS_NewLine
        Pull    "r2, pc"

        ]

        [       (:LNOT: UseMsgTrans) :LOR: ErrorInfo :LOR: ControlBlocks
AddString       ROUT
        Push    r1
        CLRV
        BIC     lr, lr, #ARM_CC_Mask
10
        TEQ     r2, #0
        SETV    EQ
        LDRB    r0, [ lr ], #1
        STRVCB  r0, [ r1 ], #1
        SUBVC   r2, r2, #1                              ; One less byte in the buffer
        TEQ     r0, #0
        BNE     %10
        DEC     r1                                      ; Point back at the zero
        INC     lr, 3                                   ; Get to the right word boundary
        BIC     lr, lr, #3                              ; Align afterwards
        Pull    r0
        [       UseMsgTrans
        MOVVC   pc, lr
        ADR     r0, ErrorBufferOverflow
        B       MakeError

ErrorBufferOverflow
        DCD     ErrorNumber_CDATBufferOverflow
        DCB     "BufOFlo", 0
        ALIGN
        |
        ADRVSL  Error, ErrorCDATBufferOverflow
        MOV     pc, lr
        ]
        ]

        END