; Set Baudrate of serial port. Call this after all other handlers
; Call Like
; SetBaud 2400          or         SetBaud 9600
      INCLUDE "includes/MACS.ASM"

      INIT_AMIGA


        CMPI.B #' ',(A0)  ; check for any characters
        BLT.S Instructions ; print out instructions and exit
        BSR    DezA0_D0
        MOVE.L #3579546,D1 ; D1=1/2.79365E-7
        DIVU   D0,D1       ; D1=D1/baudrate
        AND.L  #$FFFF,D1   ; mask out remainder
        SUBQ.W #1,D1       ; To be exact
;        ORI.W  #$8000,D1   ; we may receive two stop bits
        MOVE.W D1,$DFF032  ; write to SERPER
        BRA    Exit_prog   ; exit programm
Instructions:             ; print out instructions and exit
        BSR    PRINT
 DC.B '(C) 08.88 by RAKO',10
 DC.B 'Usage: SetBaud rate         for example: SetBaud 2400',10,0
        MOVEQ  #$64,D0    ; signal error
        BRA    Exit_prog  ; and exit
DezA0_D0:                 ; get Hex number from text (A0)  -> D0
        CLR.L D0
; Hack leading spaces
Hack_Blank:
         CMP.B    #' ',(A0)+
         BEQ.S    Hack_Blank
         SUBQ.L   #1,A0
LC6642C:
        MOVE.B (A0)+,D1
        CMPI.B #' ',D1    ; space ?
        BEQ.S LC66454
        CMPI.B #10,D1     ; Line Feed ?
        BEQ.S LC66454
        SUBI.B #$30,D1    ; generate number from 0 to 9
LC6644A:
        ANDI.B #$F,D1     ; only numbers from 0 to 10 are relevant
        MULU   #10,D0     ; next digit
        ADD.B  D1,D0      ; insert next digit
        BRA.S LC6642C     ; continue until end of Number
LC66454:
        RTS

Exit_prog:
        EXIT_AMIGA

        END


