;       SBAUD.M68       07 Apr 1983
;
;       Program to set the baud rate of any terminal port.
;
;       Syntax:
;
;       SBAUD terminal'name = baud'rate
;
;       Optional arguments:
;
;               terminal'name - defaults to the user's terminal
;                               the "=" is mandatory
;               spaces are optional
;
;Edit History:
;
;[100]  07 Apr 1983
;       Converted from 100/T SBAUD
;[101]  13 Apr 1983
;       Fixed IDVTB entry for AM1000
;[102]  30 Dec 1983     RAH
;       fixed AM300 baud routine
;
;
       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM

VEDIT=  102.                            ; 30 Dec 83

       .OFINI
       .OFDEF  TERM,6                  ; packed terminal'name
       .OFSIZ  IMPURE

       PHDR    -1,PV$RSM!PV$WSM,PH$REE!PH$REU

       GETIMP  IMPURE,A4

       BYP
       LIN                             ; end of line?
       BEQ     SYNTAX                  ; yes
       JOBIDX  A0
       MOV     JOBTRM(A0),A6           ; get default TDB
       MOV     -4(A6),TERM(A4)         ; get default terminal'name
       CMPB    @A2,#'=                 ; terminal'name given?
       BEQ     HAVTN                   ; no
       FILNAM  TERM(A4),$$$            ; yes, pack it
       BEQ     SYNTAX                  ; error
       CLRW    TERM+4(A4)              ; clear EXT word
       BYP
       CMPB    @A2,#'=
       BEQ     HAVTN
SYNTAX:
       TYPECR  Syntax: SBAUD terminal'name = baud'rate
       BR      BEX
HAVTN:
       ADD     #1,A2                   ; pass "="
       BYP
       GTDEC                           ; get baud'rate
       BMI     BBR                     ; overflow
       LEA     A6,BAUDTB               ; index baud rate table
8$:
       TSTW    @A6                     ; find baud rate in table
       BEQ     BBR
       CMPW    D1,(A6)+
       BEQ     10$
       ADDW    #2,A6
       BR      8$
10$:
       MOVW    @A6,D0                  ; get baud code
       CMPW    D0,#3                   ; is it 3, 134.5
       BNE     12$                     ; no
       CMPB    (A2)+,#'.               ; yes, is dot there?
       BNE     BBR                     ; no
       CMPB    (A2)+,#'5               ; is '5' there?
       BNE     BBR
12$:    BYP
       LIN                             ; end of line?
       BNE     SYNTAX                  ; no, error
       BR      FT

BBR:    TYPECR  Bad baud'rate.
BEX:    LCC     #0                      ; clear Z flag
       EXIT

FT:                                     ; find terminal def blk
       MOV     TRMDFC,A5               ; get start of term def chain
       MOV     TERM(A4),D6             ; get packed term name
2$:
       CMP     D6,4(A5)                ; is this TDB we want?
       BEQ     4$                      ; yes
       MOV     @A5,A5                  ; no, go to next
       MOV     A5,D7                   ; end of chain?
       BNE     2$                      ; no
       TYPESP  ?Can't find             ; yes, error
       BR      PNAM
4$:
       ADD     #10,A5                  ; index start of TDB
       MOV     T.IDV(A5),A3            ; get interface driver addr
       MOV     -4(A3),D6               ; get IDV name
       LEA     A6,IDVTB                ; index IDV name table
6$:
       CMP     D6,(A6)+                ; is this IDV we want?
       BEQ     FIDV                    ; yes
       ADD     #2,A6                   ; no, pass offset
       TSTW    @A6                     ; end of table?
       BNE     6$                      ; no
USIDV:
       TYPESP  ?Unsupported interface driver -
       MOV     -4(A3),TERM(A4)
PNAM:
       PRNAM   TERM(A4)
       CRLF
       BR      BEX
FIDV:                                   ; found IDV in table
       ADDW    @A6,A6                  ; index action routine
       JMP     @A6                     ; go to it

PORTB=  ^H0FFFE28                       ; baud rate generator port
AM100L:                                 ; get baud rate generator code
       MOVB    BT100L[~D0],D7          ; is it valid?
       JMI     BBR                     ;   no - invalid baud rate
       TST     T.IHM(A5)               ; are we selecting port 0?
       BEQ     10$                     ;   yes -
;Set up port 1 baud rate load
       MOVB    #40,D2                  ;   no - select port 1 baud rate load
       BR      SBRG

;Set up port 0 baud rate load
10$:    MOVB    #20,D2                  ; get port 0 select bit

;Send baud rate code to baud rate generator
SBRG:   ORB     D7,D2                   ; combine selector code with baud rate
       MOVB    D2,PORTB                ; send to baud rate generator
       MOVW    D0,T.BAU(A5)            ; save baud code in TDB
       LCC     #4                      ; set Z flag for success
       EXIT

AM1000:
       MOVB    BT100L[~D0],D7          ; get baud rate generator code
       JMI     BBR                     ; bad baud rate for AM1000
       MOV     #20,D2                  ; selector code port 0
       MOV     T.IHM(A5),D6            ; get which port
       BEQ     SBRG                    ; it's zero, go
       MOV     #40,D2                  ; selector code port 1
       SUB     #1,D6                   ; port 1?
       BEQ     SBRG                    ; yes, go
       MOVW    #300,D2                 ; selector code port 2
       BR      SBRG                    ; go

;Define baud rate table for AM100L and AM1000

BT100L: BYTE    0                       ; 50 baud
       BYTE    1                       ; 75 baud
       BYTE    2                       ; 110 baud
       BYTE    3                       ; 134.5 baud
       BYTE    4                       ; 150 baud
       BYTE    -1                      ; 200 baud (not valid on 1941-05)       [100]
       BYTE    5                       ; 300 baud
       BYTE    6                       ; 600 baud
       BYTE    7                       ; 1200 baud
       BYTE    10                      ; 1800 baud
       BYTE    11                      ; 2000 baud                             [100]
       BYTE    12                      ; 2400 baud
       BYTE    13                      ; 3600 baud
       BYTE    14                      ; 4800 baud
       BYTE    15                      ; 7200 baud
       BYTE    16                      ; 9600 baud
       BYTE    17                      ; 19200 baud
       EVEN

AM120:
       MOV     T.IHW(A5),A3            ; get board port addr
       LEA     A6,BT310                ; get baud select code
       ADD     D0,A6
       ADD     D0,A6
       MOVW    @A6,D1                  ; mode reg's 1 and 2
       JEQ     BBR                     ; invalid baud rate
       MOVW    D0,T.BAU(A5)            ; save baud code in TDB
       SUPVR
       SVLOK                           ; disable interrupts
       MOVB    3(A3),D7                ; dummy read
       MOVB    D1,2(A3)                ; set mode register 1
       RORW    D1,#8.
       MOVB    D1,2(A3)                ; set mode register 2
       LSTS    #4
       EXIT

AM300:
       MOVB    BT300[~D0],D1           ; get baud select code
       JMI     BBR                     ; invalid
       MOVW    D0,T.BAU(A5)            ; save baud code in TDB
       SUPVR
       SVLOK                           ; disable interrupts
       MOV     T.IHW(A5),A3            ; get board port addr
       MOV     #7,D3
       ANDW    T.IHM+2(A5),D3          ; port #
       ORB     #10,D3                  ; baud rate control cmd
       MOVB    D3,4(A3)
;[102]  MOVB    D0,@A3
       MOVB    D1,@A3                  ;               correct reg     [102]
       MOVB    #20,4(A3)               ; enable interrupts
       LSTS    #4
       EXIT

;Define baud rate table for AM_300
BT300:  BYTE    0                       ; 50 baud
       BYTE    1                       ; 75 baud
       BYTE    2                       ; 110 baud
       BYTE    3                       ; 134.5 baud
       BYTE    4                       ; 150 baud
       BYTE    5                       ; 200 baud
       BYTE    6                       ; 300 baud
       BYTE    7                       ; 600 baud
       BYTE    10                      ; 1200 baud
       BYTE    11                      ; 1800 baud
       BYTE    -1                      ; 2000 baud (not valid on AM-300)
       BYTE    12                      ; 2400 baud
       BYTE    13                      ; 3600 baud
       BYTE    14                      ; 4800 baud
       BYTE    15                      ; 7200 baud
       BYTE    16                      ; 9600 baud
       BYTE    17                      ; 19200 baud
       EVEN

AM310:                                  ; A3=AM310.IDV
       LEA     A6,BT310                ; chk baud'rate
       ADD     D0,A6
       ADD     D0,A6
       MOVW    @A6,D1
       JEQ     BBR                     ; bad baud'rate
       MOVW    D0,T.BAU(A5)
       SUPVR
       SVLOK                           ; disable interrupts
       CALL    2(A3)                   ; call INIT routine
       LSTS    #4
       EXIT

;AM330:
;       TYPECR  NO'AM330
;       JMP     BEX


IDVTB:  WORD    [AM1],[00L],AM100L-.
       WORD    [AM1],[000],AM1000-.    ;                               [101]
;[101]  WORD    [AM1],[100],AM1000-.
       WORD    [AM1],[20 ],AM120-.
       WORD    [AM3],[00 ],AM300-.
       WORD    [AM3],[10 ],AM310-.
;       WORD    [AM3],[30 ],AM330-.
       WORD    0

;Baud rate table, AM310 or AM120
BT310:  WORD    30116                   ;  0 -    50 baud
       WORD    30516                   ;  1 -    75 baud
       WORD    31316                   ;  2 -   110 baud
       WORD    31516                   ;  3 -   134.5 baud
       WORD    32116                   ;  4 -   150 baud
       WORD    0                       ;  5 -   200 baud (not supported on AM-310)
       WORD    32516                   ;  6 -   300 baud
       WORD    33116                   ;  7 -   600 baud
       WORD    33516                   ; 10 -  1200 baud
       WORD    34116                   ; 11 -  1800 baud
       WORD    34516                   ; 12 -  2000 baud
       WORD    35116                   ; 13 -  2400 baud
       WORD    35516                   ; 14 -  3600 baud
       WORD    36116                   ; 15 -  4800 baud
       WORD    36516                   ; 16 -  7200 baud
       WORD    37116                   ; 17 -  9600 baud
       WORD    37516                   ; 20 - 19200 baud


BAUDTB:
       50.,0
       75.,1
       110.,2
       134.,3
       150.,4
       200.,5
       300.,6
       600.,7
       1200.,10
       1800.,11
       2000.,12
       2400.,13
       3600.,14
       4800.,15
       7200.,16
       9600.,17
       19200.,20
       0

       END