; Translator package - version 24.12.88

               XDEF    LinkTop
               XDEF    Translate
               XDEF    TransLibName
               XDEF    TransBase

               include 'package.i'
               include 'Exec.i'

; Equates
ExecBase:      EQU     4
TranslateOff:  EQU     -30

; Initialization section
               MOVE.L  D0,A0                  ; Address of package structure
               MOVE.L  #LinkTop,PckTopId(A0)  ; Address of top-link
               MOVE.L  #Signal,PckSignal(A0)  ; Set address of Signal
               MOVE.L  ExecBase,A6
               CLR.L   D0
               LEA     TransLibName,A1
               JSR     _LVOOpenLibrary(A6)    ; Open translator lib
               MOVE.L  D0,TransBase           ; Store base address
               BNE.S   InitOk                 ; Ok if not zero
               MOVE.W  #199,D0                ; Initialization error
               BRA.S   InitRet
InitOk:        CLR.W   D0                     ; No error
InitRet:       RTS

; Signal routine
Signal:        CMP.W   #2,D0           ; DISCARD ?
               BEQ.S   CloseTrans      ; Yes -> close translator lib
               CMP.W   #10,D0          ; BYE ?
               BEQ.S   CloseTrans      ; Yes -> close translator lib
SignalRet:     RTS

CloseTrans:    MOVE.L  ExecBase,A6
               MOVE.L  TransBase,D0    ; Get lib base
               BEQ.S   SignalRet
               MOVE.L  D0,A1
               JSR     _LVOCloseLibrary(A6) ; Close library
               CLR.L   TransBase
               RTS

; Translator function
Translate:     MOVE.L  -(A5),D3        ; Get length ..
               MOVE.L  -(A5),D2        ; .. and address of string
               TST.W   D3              ; Zero length ?
               BEQ.S   TranslateRet    ; Yes -> return
               MOVE.L  D2,A0           ; Address of string to A0
               MOVE.L  D3,D0           ; .. and length to D0
               MOVE.L  A5,A1           ; Buffer address
               MOVE.L  #512,D1         ; .. and length
               MOVE.L  TransBase,A6
               JSR     TranslateOff(A6); Translate
               MOVE.L  A5,D2           ; Address of translated string
               MOVE.L  A5,D3           ; Start calculating length
1$:            TST.B   (A5)+
               BNE.S   1$              ; Loop until end
               SUB.L   A5,D3
               NOT.L   D3              ; D3.L = length
TranslateRet:  RTS                     ; Return

               SECTION TranslateData,DATA

TranslateLink: DC.L    0
               DC.L    TransName
               DC.W    strgcode        ; String function
               DC.W    1               ; One argument
               DC.W    strgval         ; Text value
               DC.L    Translate       ; Routine address
TransName:     DC.B    'TRANSLATE$',0

               CNOP    0,2
LinkTop:       DC.L    TranslateLink

TransLibName:  DC.B    'translator.library',0

               SECTION TranslateBlock,BSS
;
; Internal workspace
;
TransBase:     DS.L    1               ; Translator base

               END 
