BAUD: DO;

$INCLUDE (:F1:COMMON.LIT)

$INCLUDE (:F1:ISS.COM)

DECL SIO1DAT    LIT '0F4H';
DECL SIO1STAT   LIT '0F5H';
DECL SIO2DAT    LIT '0F6H';
DECL SIO2STAT   LIT '0F7H';

DECL SIO$RST    LIT '040H';
DECL UM		LIT '0CH';
DECL USRT$MD0   LIT '0CFH';     /*USART MODE DEVIDE BY 64*/
DECL USRT$MD1   LIT '0CEH';     /*USART MODE DIVIDE BY 16*/
DECL USRT$CTRL  LIT '027H';
DECL S1		LIT '040H';	/*1 STOP BIT */
DECL S2		LIT '0C0H';	/*2 STOP BIT */
DECL D16	LIT '02H';	/*DIV BY 16 BAUD FACTOR*/
DECL D64	LIT '03H';	/*DIV BY 64 BAUD FACTOR*/

DECL CNTR$CTRL  LIT '0F3H';
DECL CNTR0      LIT '0F0H';
DECL CNTR1      LIT '0F1H';
DECL CNTR2      LIT '0F2H';

DECL C0         LIT '0';
DECL C1         LIT '040H';
DECL C2         LIT '080H';
DECL CNT$MD     LIT '036H';


DECL BAUD$RATE(*) BYTE
	/*B110(8) BYTE,
	 B300(8) BYTE,
	 B1200(8) BYTE,
	 B2400(8) BYTE,
	 B4800(8) BYTE,
	 B9600(8) BYTE*/ DATA
	(3,'110',0,0,0,0,3,'300',0,0,0,0,4,'1200',0,0,0,
	 4,'2400',0,0,0,4,'4800',0,0,0,4,'9600',0,0,0);


DECL BAUD$FACTORS(8) ADDRESS DATA (698,256,64,32,16,08,0);
DECL USRT$FACTORS(8) BYTE DATA (0CEH,04EH,04EH,04EH,04EH,04EH,0);

DECL ERMSG(*) BYTE DATA (CR,LF,'NOT A VALID FACTOR.  ALLOWABLE FACTORS ARE:',
  CR,LF,'110,300,1200,2400,4800,9600',CR,LF);
DECL AFT$IN ADDRESS;
DECL ACTL ADDRESS;
DECL CONSL$INP(*) BYTE DATA (':CI:',0);
DECL COUNT BYTE;
DECL I BYTE;
DECL J BYTE;
DECL K BYTE;
DECL SGN$ON(*) BYTE DATA ('SERIES II BAUD CHANGE UTILITY V1.0',0DH,0AH);
DECL SRPORT BYTE INITIAL (02);
DECL STATUS ADDRESS;
DECL KEYBUFFER(122) BYTE;


 CALL WRITE (CO$AFT,.SGN$ON,LENGTH(SGN$ON),.STATUS);
  CALL OPEN (.AFT$IN,.CONSL$INP,READ$AC,LN$EDT$CO,.STATUS);
   IF STATUS > 0 THEN CALL ERROR(STATUS);
  CALL READ (AFT$IN,.KEYBUFFER,122,.ACTL,.STATUS);
   IF STATUS > 0 THEN CALL ERROR(STATUS);

  I,J,K = 0;

	DO WHILE KEYBUFFER(I) = SPACE;
	 I = I+1;
	END;
  IF KEYBUFFER(I) = CR THEN
	CALL EXIT;

	DO WHILE K < LENGTH (BAUD$RATE);
	 COUNT = BAUD$RATE(K);
		DO WHILE KEYBUFFER(I+J) = BAUD$RATE(K+J+1);
		 J = J+1;
		 IF COUNT-J = 0 THEN GOTO MATCH;
		END;
	 K = K+8;
	END;
 CALL WRITE(CO$AFT,.ERMSG,LENGTH(ERMSG),.STATUS);
 IF STATUS > 0 THEN CALL ERROR(STATUS);
  CALL EXIT;

MATCH: I = I+J;
  K = SHR(K,3);
	DO WHILE KEYBUFFER(I) = SPACE;
	 I = I+1;
	END;

  IF KEYBUFFER(I) <> CR THEN
	DO;
	 IF KEYBUFFER(I) = '1' THEN
	 SRPORT = 1;
	END;

  IF SRPORT = 2 THEN
	DO;
	 OUTPUT(CNTR$CTRL) = (CNT$MD OR C1);
	 OUTPUT(CNTR1) = (BAUD$FACTORS(K) AND 0FFH);
	 OUTPUT(CNTR1) = (SHR(BAUD$FACTORS(K),8) AND 0FFH);
	 OUTPUT(SIO2STAT) = SIO$RST;
	 OUTPUT(SIO2STAT) = USRT$FACTORS(K);
	 OUTPUT(SIO2STAT) = USRT$CTRL;
	 CALL EXIT;
	END;

  IF SRPORT = 1 THEN
	DO;
	 OUTPUT(CNTR$CTRL) = (CNT$MD OR C0);
	 OUTPUT(CNTR0) = (BAUD$FACTORS(K) AND 0FFH);
	 OUTPUT(CNTR0) = (SHR(BAUD$FACTORS(K),8) AND 0FFH);
	 OUTPUT(SIO1STAT) = SIO$RST;
	 OUTPUT(SIO1STAT) = USRT$FACTORS(K);
	 OUTPUT(SIO1STAT) = USRT$CTRL;
	 CALL EXIT;
	END;
  CALL EXIT;
END;
EOF
