! Utility routines for EDWIN on EMAS

%external %integer %fn %spec IN STREAM
%external %integer %fn %spec OUT STREAM
%external %integer %fn %spec UINFI (%integer PARM)
%system %routine %spec CONSOLE (%integer FN, %integer %name A, L)

%external %string (255) BUFF = ""
%external %integer VIEWING = 0

%external %routine TTMODE (%integer STATE)
   ! Set terminal characteristics -
   ! 0 => Normal mode => SET MODE  G=OFF
   ! 1 => Graphics output mode => SET MODE  G=ON  (subsystem not seeing it!)
   ! 2 => Graphics input mode  NOT YET IMPLEMENTED.
   ! Also suppress terminal messages while in Graphmode.
   %dynamic %integer %fn %spec UINFI (%integer I)
   %dynamic %routine %spec MESSAGES (%string (255) STATE)
   %const %integer MESSAGE INFO = 22
   %own %integer MESSAGE STATE = 0
   %integer A, L
   %own %byte %integer %array SETGaF (0:4) = 2, 11, 1, 17, 1
   %own %byte %integer %array SETNoG (0:4) = 2, 11, 0 (*)

   %if UINFI(2)=2 %start; ! Batch mode
       SELECT OUTPUT (0)
       PRINT STRING ("Graphics I/O in batch mode ignored")
       NEWLINE
       VIEWING = -1
       %return
   %finish

   %if STATE = 0 %start
       ! Return to normal -
       A = ADDR (SETNoG(0))
       L = 1
       CONSOLE (17, A, L)
       MESSAGES ("ON") %if MESSAGE STATE = 0
       %return
   %finish
   %if STATE&1#0 %start
       ! Set Graphics output mode
       SETGaF (0) = 4 %if DEVICE=2 %or DEVICE=3; ! + F=ON for Charles + HP's
       A = ADDR (SETGaF(0))
       L = 1
       CONSOLE (17, A, L)
       MESSAGE STATE = UINFI (MESSAGE INFO)
       MESSAGES ("OFF") %if MESSAGE STATE = 0
       %return
   %finish
%end

%external %routine KILL INPUT
   %integer A, L
   CONSOLE (8, A, L)
%end

%external %routine FLUSH OUTPUT
   %integer A, L

   %return %if BUFF="" %or VIEWING<0

   %if VIEWING=0 %start
       A = ADDR (BUFF) + 1
       L = LENGTH (BUFF)
       CONSOLE (10, A, L)
   %finish %else %start
       L = OUT STREAM
       SELECT OUTPUT (VIEWING)
       PRINT CH (CHARNO(BUFF,A)) %for A=1,1,LENGTH(BUFF)
       SELECT OUTPUT (L)
   %finish
   BUFF = ""
%end

%external %routine TTPUT (%integer I)
   BUFF = BUFF.TO STRING (I)
   FLUSH OUTPUT %if LENGTH(BUFF)>253
%end

%external %integer %fn TTREAD
   %integer I, S
   S = IN STREAM
   SELECT INPUT (0)
   READ CH (I)
   SELECT INPUT (S)
   %result = I
%end

%external %integer %fn TTGET
   %result = TTREAD;  ! For EMAS at present.
%end

%external %integer %fn MULDIV (%integer A, B, C)
   %long %integer AB, RES, REM

   %if C#0 %start
       AB = lengthenI(A) * lengthenI (B)
       RES = AB // lengthenI(C)
       REM = AB - RES * lengthenI(C)
       RES = RES + 1 %if REM > C//2
   %finish %else RES = 0
   %result = RES
%end

%end %of %file
