; Console device package - version 31.12.88

               XDEF    LinkTop
               XDEF    ComalStr
               XDEF    DosBase
               XDEF    ConHandle
               XDEF    ConStruc
               XDEF    CursorString

               XDEF    Open,Close,Write,Cursor

CALL     MACRO
            MOVEM.L  D1/A0-A1/A6,-(A7)
            MOVE.L   \1Base,A6
            JSR      _LVO\2(A6)
            MOVEM.L  (A7)+,D1/A0-A1/A6
         ENDM

               include 'package.i'
               include 'dos.i'

; Initialization section
               MOVE.L  D0,A0                 ; Package structure
               MOVE.L  #LinkTop,PckTopId(A0) ; Set  top-link
               MOVE.L  #Signal,PckSignal(A0) ; .. Signal routine
               MOVE.L  PckCmlStr(A0),A1
               MOVE.L  A1,ComalStr           ; Comal structure
               MOVE.L  DosBaseOff(A1),DosBase
               LEA     ConStruc,A0
               JSR     AddCmlDev(A1)         ; Add new device
               CLR.W   D0                    ; No error
               RTS

; Signal routine
Signal:        CMP.W   #2,D0                 ; DISCARD ?
               BEQ.S   Signal1
               CMP.W   #10,D0                ; BYE ?
               BNE.S   SignalEnd
Signal1:       LEA     ConStruc,A0
               MOVE.L  ComalStr,A1
               JSR     RemCmlDev(A1)         ; Remove device
               MOVE.L  ConHandle,D1
               BEQ.S   SignalEnd
               CALL    Dos,Close
               CLR.L   ConHandle
SignalEnd:     RTS

; Open device
Open:          TST.L   ConHandle
               BNE     DevUsed
               MOVEM.L D2-D3/A0/A5,-(A7)
               MOVE.L  D2,A0           ; Address of device name
               MOVE.L  A5,D1          
1$:            MOVE.B  (A0)+,(A5)+     ; Move to workspace
               DBRA    D3,1$
               CLR.B   -(A5)           ; .. and terminate
               MOVE.L  #1005,D2        ; Mode_old
               CALL    Dos,Open        ; Open window
               MOVEM.L (A7)+,D2-D3/A0/A5
               MOVE.L  D0,D1           ; Handler to D1
               BEQ     IoErr
               MOVE.L  D1,ConHandle
               CLR.W   D0
               RTS

; Close device
Close:         CALL    Dos,Close
               CLR.L   ConHandle
               CLR.W   D0
               RTS

; Write to device
Write:         EXT.L   D3
               CALL    Dos,Write
               TST.L   D0
               BMI     IoErr
               CLR.W   D0
               RTS

; Set/get cursor
Cursor:        TST.L   D0
               BPL     SetCursor
               CLR.L   D0                ; No 'Get' implemented
               RTS

SetCursor:     MOVEM.L D0-D3/A0,-(A7)
               LEA     CursorString+1,A0
               MOVE.L  D0,D1
               SWAP    D1
               BSR.S   MakeASCII
               ADDQ.L  #1,A0
               MOVE.W  D0,D1
               BSR.S   MakeASCII
               MOVE.L  ConHandle,D1
               BEQ     SetCursor1
               MOVE.L  #CursorString,D2
               MOVEQ   #7,D3
               CALL    Dos,Write
SetCursor1:    MOVEM.L (A7)+,D0-D3/A0
               RTS

MakeASCII:     EXT.L   D1
               DIVU    #10,D1
               ADD.B   #'0',D1
               MOVE.B  D1,(A0)+
               SWAP    D1
               ADD.B   #'0',D1
               MOVE.B  D1,(A0)+
               RTS

IoErr:         CALL    Dos,IoErr
               RTS
               
DevUsed:       MOVE.W  #202,D0
               RTS

               SECTION DDATE,DATA

LinkTop:       DC.L    0

; Device structure
ConStruc:      DC.L    0               ; Room for link
               DC.L    ConName         ; Pointer to name
               DC.W    1               ; CRT-device
               DC.W    0               ; Unused
               DC.L    Open
               DC.L    Close
               DC.L    0               ; No read
               DC.L    Write
               DC.L    Cursor          ; Set/get cursor

ConName:       DC.B    'con:',0
CursorString:  DC.B    $9B,'00;00H'

               SECTION BLOCK,BSS
;
; Internal workspace
;
ComalStr:      DS.L    1               ; Pointer to Comal structure
DosBase:       DS.L    1
ConHandle:     DS.L    1

               END 
