    ! EDWIN driver for Westward C2014  (Tektronix compatible?) terminals.

    !############################################################################
    !#                                                                          #
    !#  This is a module from the EDWIN Graphics Package, which was developed   #
    !#  in the Department of Computer Science, at Edinburgh University, from    #
    !#  1978 to the present day, release 5 of EDWIN in October 1984.            #
    !#                                                                          #
    !#  The principal author of the EDWIN Graphics Package was J Gordon Hughes, #
    !#  while working for the Edinburgh University Computer Sceince Department. #
    !#  Parts of EDWIN have been produced by many different people, too many    #
    !#  too mention, working for different departments of Edinburgh and Leeds   #
    !#  Universities.                                                           #
    !#                                                                          #
    !#  This module is regarded as being in the public domain, and the authors  #
    !#  and accept no responsibility regarding the uses to which the software   #
    !#  will be put.                                                            #
    !#                                                                          #
    !############################################################################

    from Edwin include Device
    from Edwin include Icodes

    ! Control characters
    const integer INVOKE CURSOR = 26
    const integer ESCAPE = 27
    const integer GRAPHMODE = 29
    const integer ALPHAMODE = 31
    const integer ERASE SCREEN = 12
    const integer GRAPHICS STATE = 12
    const integer VDU STATE = 8_62
    const integer NUL = 0
    const integer DEL = 127

    ! Screen information
    own integer AMODE = 0        { Advanced mode for 4014
    own integer MODE = 0         { 0 if alphamode
    own integer SX = 0, SY = 0   { Current device position
    own integer XL = 0           { Left hand side of device window
    own integer XR = 1023        { Right hand side of device window
    own integer YB = 0           { Bottom side of device window
    own integer YT = 1023        { top side of device window
    own integer VIS = 0          { 0 if CVP inside VW
    own integer TCS = 13         { char size.

    routine PAD (integer SYM,NO)
       integer I
       TTPUT (SYM) for I = 1,1,NO
    end

    routine UPDATE
       PAD (NUL,4)
       TTPUT (ALPHA MODE)
       TTPUT (25) if TCS=24;  ! Sets big chars on 4002
       FLUSH OUTPUT
       MODE=0
    end

    external routine SET COLOUR MAP alias "EDWIN___W_MAP" (integer ADR, RED, BLUE, GREEN)
       ! Still to be written
    end

    external routine WC2014 alias "EDWIN___W" (integer COM, X, Y)
       own integer WX, WY
       switch SW(0:MAX COM)

       routine SWAP (integer name  A, B)
          integer C
          C = A;   A = B;   B = C
       end

       routine PUT CHAR
          ! Put out a text character properly.
          UPDATE if MODE#0
          TTPUT (X)
          TTPUT (0)
          SX = SX + TCS
          VIS = 1 if SX>XR
       end

       routine GOTO(integer X,Y);   !Code up coordinates and send to TTY
          constinteger HI=32,LY=96,LX=64,ENH=7
          TTPUT (Y>>5&31!HI); TTPUT (Y&31!LY)
    !      TTPUT ((ENH<<4)!(Y>>10&3)!(X>>10&3)) %if AMODE#0
          TTPUT (X>>5&31!HI); TTPUT (X&31!LX)
       end

       routine CHANGE ATTRIBUTE
          switch SW(0:ATT MAXIMUM)
          -> SW(X)

    SW(att Colour):
           TTPUT (ESCAPE)
           TTPUT ('[')
           TTPUT ('C')
           TTPUT (Y+'@')
           return

    SW(att Line style):
           Y = 0 unless 0<=Y<=4
           TTPUT (ESCAPE)
           TTPUT (Y + 96)
           return

    SW(att Char size):
           TTPUT (ESCAPE)
           if Y <  8 start
               TTPUT (';');   TCS=7
           finish else if Y < 10 start
               TTPUT (':');   TCS=9
           finish else if Y < 13 start
               TTPUT ('9');   TCS=13
           finish else start
               TTPUT ('8');   TCS=14
           finish

    SW(*): ! all other attributes ignored
       end

       -> SW(COM)

    SW(0): ! Initialise
           DEV DATA_NAME = "a Westward 2014 terminal"
           DEV DATA_MVX = 1023
           DEV DATA_MVY = 767
           DEV DATA_DVX = 1023
           DEV DATA_DVY = 767
           TTMODE (1)
           TTPUT (ESCAPE);   TTPUT (GRAPHICS STATE)
           return

    SW(1): !Terminate
    !        PAD (NUL,4)
    !        TTPUT (ALPHA MODE)
            TTPUT (ESCAPE);   TTPUT (VDU STATE)
            TTPUT (13)
            TTPUT (10)
            FLUSH OUTPUT
            TTMODE (0)
            return

    SW(2): ! Update
           UPDATE
           return

    SW(3): ! New frame
           TTPUT (ESCAPE)
           TTPUT (ERASE SCREEN)
    PAD(0,120)
    !       PAD (DEL,960)
           SX=0; SY=0; MODE=0
           return

    SW(4): ! Move Abs
           TTPUT (GRAPH MODE)
           GOTO (X,Y)
           SX=X;  SY=Y
           MODE = 1; VIS=0
           return

    SW(5): ! Line Abs
           if MODE=0 start
               TTPUT (GRAPH MODE)
               GOTO (SX,SY)
           finish
           GOTO (X,Y)
           MODE = 1
           SX=X; SY=Y
           VIS=0
           return

    SW(6): ! Character
           PUT CHAR if VIS=0
           return

    SW(7): ! Attribute  Change
           CHANGE ATTRIBUTE
           return

    SW(8): ! Ignore lower window bounds settings
           return

    SW(9): ! Upper window bounds
           XR=X

    SW(10): SW(11): ! Ignored
            return

    SW(12): ! Lower box bounds
            WX = X;   WY = Y
            return

    SW(13): ! Upper box bounds, and do the box
            swap (wx, x) if wx > x
            swap (wy, y) if wy > y
            return if WX > XR or X < XL or WY > YT or Y < YB
            WX = XL if WX < XL
            WY = YB if WY < YB
            X = XR if X > XR
            Y = YT if Y > YT
            ! Box now clipped into the screen.
            TTPUT (ESCAPE);   TTPUT (2);   ! Box Mode
            MODE = 1
            WC2014 (5, WX, WY)
            WC2014 (5, X, Y)
            TTPUT (ESCAPE);   TTPUT (3);   ! Box Mode off again
            return

    SW(*):
    end

    external routine W REQ alias "EDWIN___W_REQ" (integer name CH, X, Y)
       UPDATE
       TEK INPUT (CH, X, Y, INVOKE CURSOR)
    end

    end of file