 
*  ED2/SCREN  40 col          Nov / 24 / 92
 
 
*---------------------*
*   Main screen loop  *
*---------------------*
 
 
START  EQU  $
       BL   @CLRTMP           Clear temp line buffer
       MOV  R1,R8             Get line address - copy Cline
       S    R3,R8             Remove screen line Sbias
       MPY  @D86,R8           Offset in line buffer to R9
       AI   R9,CPUSCN         Construct pointer into text buffer
CPUADR EQU  $-2
       BL   @RESTLB           Construct line end/start pointers
 
*  Check for empty buffer
*------------------------
 
STAR05 MOV  @EOF,@EOFM1       Copy to (end of file - 1)
       JNE  STAR10            EoF = 0?
 
       MOV  @WDWRAP,@CRFLAG   CR's in Wdwrap mode
       CLR  R3                Sbias = 0  -> line #1 at top
       CLR  R1                Cline = 0  -> cursor on first line
       BL   @INSLIN           Stuff a blank line into buffer
STAR10 DEC  @EOFM1            Back up - not an empty file
       BL   @BUMPC            Adjust cursor and windows
 
*   Screen handler routine
*-------------------------
 
SCREEN EQU  $
       MOV  @EOFONS,R6        Does file fill screen ?
       JEQ  NOTONS            Yes
 
       LI   R12,EOFLIN        EoF data line pointer
       A    *R7,R12           Adjust for window
       MOV  R12,*R6           Write into table
 
NOTONS ABS  @REFRSH           Refresh forbidden ?
       JNE  SCWRIT            Yes
H07    EQU  $                 Byte data >07
       ABS  @OVRL             Overlay in ?
       JNE  SCWRIT            Yes - also do not disturb
 
*  Update CPU Screen buffer from text buffer
*-------------------------------------------
 
       CLR  @ICHRF            Cancel Insert mode
       MOV  @EOFONS,R6        EoF on screen ?
       JEQ  SCRN05            Don't bother
 
       CLR  *R6               Clear pointer
       CLR  @EOFONS           Reset flag
SCRN05 MOV  @SCNLEN,R6        Yes, get screen length
       MOV  R3,@LINENO        Sbias on first line number
       MOV  @TOPSCN,@BUFADR   Set up first cpu address
 
*  Fill screen buffer from text buffer
 
SCBFIL EQU  $
       BL   @GETL20           Get next line
       JMP  EOFILL            Not there, probably EOF
       DEC  R6                Count line
       JGT  SCBFIL            Any left ?
       JMP  SCWRIT            Go write it
 
*  Complete buffer for End of File on screen
*-------------------------------------------
 
EOFILL EQU  $
       MOV  @LINENO,R9        Eof line number
       DEC  R9                Back off 1
       S    R3,R9             Relative screen line number
       LI   R12,EOFLIN        Point to EoF line
       A    *R7,R12           Window adjust
       SLA  R9,1              Index by words
       AI   R9,LINTBL         Point into special line table
       MOV  @SSNLEN,R8        Get Default Screen length
       S    @SCNLEN,R8        Should be zero unless command mode.
       SLA  R8,1              Otherwise should be 2 now
       A    R8,R9             If 2, add 4 to table address
       MOV  R12,*R9           Write in EoF line pointer
       MOV  R9,@EOFONS        Address of eof message entry
       MOV  @BUFADR,R9        End of CPU buffer
       LI   R8,CPUSND         Blank rest of cpu buffer
EOFILM MOV  @DSPACE,*R9+      Get blank from PAD - will be even
       C    R9,R8             All done ?
       JL   EOFILM            Not yet
 
*  Write up video screen from CPU screen buffer
*-----------------------------------------------
*  Following code assumes in-line routine in PAD
 
SCWRIT EQU  $
       LI   R8,CPUSCN->56     Start of CPU screen buffer area
       MOVB *R4,R5            Save char under cursor
       MOVB @CSR,*R4          Replace with cursor char code
       A    *R7,R8            Allow for window
       MOV  @STPLEN,R6        Get Screen Length
       CLR  R9                Start of screen table
       BL   @SETWDA           Write VDP address 0
       LI   R12,LINTBL        Special vdp write table
       LI   R14,VDPWD         Set up for in-line VDP writes
SCWR10 AI   R8,86             Advance to next line in cpu buffer.
D86    EQU  $-2
B86    EQU  $-1
SCWR20 LI   R9,2              2 blocks of 20 per line
       MOV  *R12+,R10         Special screen message?
       JNE  SCWR50            Yes - use this
 
       MOV  R8,R10            No, output from CPU buffer
 
*  Write out a line using speedup code in PAD
 
SCWR50 BL   @FASTVW           Go to code in PAD
       DEC  R6                Line count
       JGT  SCWR10            Repeat till done
 
*  Go to COMAND on first pass, replacing with next instruction
 
RWBRN  B    @FIRST            Goes off via COMAND
*      SETO @REFRSH           Disable refresh of cpu buffer
 
*  Get key-stroke
*----------------
 
       MOV  R1,R8             Calculate VDP address of cursor
       S    R3,R8             # lines on screen
       LI   R12,>28           -------
       MPY  R12,R8            Beginning of line offset
       A    R2,R9             Add cursor column
       S    *R7,R9            Adjust for current window
       MOVB @KEYRT,@LSTKEY    Save last key as left behind
       MOVB R5,*R4            Char to buffer
       MOVB R5,R0             Set up cursor flash in R0
       SWPB R0                Reverse bytes
       MOVB @CSR,R0           Cursor as other byte
       BL   @FLACSR           Flash cursor, wait for key
       BL   @MRCHEK           Margin bell
       MOV  @PROMPT,R11       Special return for prompting
       JEQ  KYEVAL            None - evaluate key return
       RT                     GO there
 
*  Process key return
*---------------------
 
KYEVAL EQU  $
       MOVB @KEYRT,R10        Fetch key-stroke
       JLT  CONTKY            Key greater than >8000
 
       CI   R10,>1F00         Key less than >1F00
       JLT  COMDKY
BRINS  B    @NEWCHR           Character key (>20 - >7F)
 
 
*  Command key table lookup
*--------------------------
 
COMDKY EQU  $
       LI   R6,CBTBL          Command key (Function key)
COMD10 SRL  R10,7             Calculate address into key table
       MOV  @CCTABL,R9        VDP address of table
       A    R6,R9             Select sub-table
       A    R10,R9            Index into sub-table
       BL   @SETRDA           Set for read
       MOVB @VDPRD,R6         First byte
       SWPB R6                For next
       MOVB @VDPRD,R6         Read LSB
       SWPB R6                Make word address
COMD15 LI   R11,START         Set up return address
       CLR  R0                Clear error register
       CLR  @ICHRF            Clear insert character flag.
       B    *R6               Go
 
CONTKY CLR  R6                Control key offset to CTBL = 0
       AI   R10,->8000
       CI   R10,>2000         Out of range?
       JLT  COMD10            No
 
       LI   R6,CATBL          Check for control 0,1,...7
       AI   R10,->3000        Key between >9F, >B0
       JLT  BST2              Yes, ignore
 
       CI   R10,>E00          No, key between >B0,>BD
       JLT  COMD10            Yes, good key
BST2   B    @START            No, forget about it
 
 
*  ENTER LINE Processor
*----------------------
 
ENTKEY EQU  $
       CLR  @TOPPAR           Reset top of para flag
       MOV  @CMDRT,R6         Command mode?
       JEQ  ENTR02            No
       B    *R6               Yes, go to command processor.
 
ENTR02 C    R1,@EOFM1         Cline at end of file ?
       JLT  ENTR05            No
 
       MOV  @WDWRAP,@CRFLAG   Wordwrap flag determines if CR entered
       JNE  NEWP01            Yes - go to last line check
       JMP  ENTR10            No
 
ENTR05 ABS  @CRFLAG           Word wrap mode?
       JNE  NEWPAR            Yes
 
ENTR10 CLR  R2                Home cursor
       BL   @UPDATE           Update the line
       LI   R11,START         Set return as main loop
       C    R1,@EOFM1         Past end of file ?
       JHE  ENTR20            At or past end
 
       ABS  @CRFLAG           Word wrap mode?
       JEQ  SCROLL            No, go check scroll
 
ENTR15 MOV  R11,@AUTXIT+2     Establish return
       B    @AUTO60           Go into word-wrap insert routine
 
ENTR20 MOV  @EOF,@LINENO      Force insert at end
ENTR30 BL   @INSL10           Insert line into buffer
 
*   Scroll subroutine
*--------------------
 
SCRL01 LI   R11,START         Fake up return for scroll
SCROLL INC  R1                Point to next line
H81    EQU  $-1
SCRL03 MOV  R1,R8             Go to next line
       S    R3,R8             Remove start of screen bias
       C    R1,@EOFM1         At end of file ?
       JLT  SCRL04            Not yet
 
       MOV  @EOFM1,R1         Replace Cline with end of file
       INC  R8                Next line
SCRL04 C    R8,@STPLEN        Next line on screen?
       JLT  SCRL07            Yes - return directly
 
       INC  R3                Push start of screen along a line
       CLR  @REFRSH           Force refresh of cpu buffer
SCRL07 RT
 
*  Cursor down arrow
*-------------------
 
DWNAR  EQU  $
       BL   @UPDT10           Update buffer for current line
       JMP  SCRL01
 
*  Don't split last line with <enter>
 
NEWP01 MOV  R4,R8             Copy current cursor address
       MOV  R8,R9             Copy for search start
       S    R2,R9             R9 <- start of line
       AI   R9,86             R9 <- end of line
NEWP03 CB   *R8+,@DSPACE      Found nonblank after cursor?
       JNE  NEWP05            Yes, do not split
 
       C    R8,R9             End of line?
       JNE  NEWP03            No, continue checking
       JMP  NEWPAR            Yes, all blanks after cursor
 
NEWP05 MOV  R9,R4             Fake out no split
       JMP  NEWP20            Force split to end of line
 
*  Indent
*--------
 
INDENT EQU  $
       MOV  @WDWRAP,@CRFLAG   Word-wrap mode ?
       JEQ  BST2              No - back to start
 
*  Enter key is New paragraph
*----------------------------
 
NEWPAR EQU  $                 Drop through from INDENT
       SETO @NCHAR            Ensure Edit buffer gets updated
       LI   R9,TEMPM6         Temp buffer allowing for line #
       A    R2,R9             Point into buffer
       CB   *R4,@CR           Is it a CR ?
       JEQ  NEWP10            Yes
 
       MOVB *R4,*R9+          No - put in temp buffer
NEWP10 MOVB @CR,*R4+          Move data to next line, split.
NEWP20 MOV  R4,R8             Copy Cursdr
       MOV  @INDT,R2          Start at indent
       MOV  R2,@LMRG          Temp left margin for split
       SETO @GOLF             Use to flag <enter> key for insert
       JMP  SPLIT
 
*  Insert blank line ( or split line )
*-------------------------------------
 
INSCHR MOV  @WDWRAP,@CRFLAG   Word wrap mode?
       JEQ  INSC40            No
 
INSCH1 SETO @CRFLAG           Yes - allow CR to be written
       CLR  @TOPPAR
       SETO @SWSF             Effectively save Wd-wrap state
       CLR  @GOLF             Yes, split line
       MOV  R4,R8             Copy cursor address in buffer
       LI   R9,TEMPM6         Temporary
       A    R2,R9             R9 -> DADDR in tempbf
 
SPLIT  MOV  @LINEND,R6
INSC10 C    R8,R6             End of line?
       JH   INSC20            Yes, exit
 
       MOVB *R8+,*R9+         Move rest of line to tempbf
       JMP  INSC10
 
INSC20 BL   @CTOEOL           Clear to end of line
       BL   @UPDATE           Update split line
       BL   @INSL50           Insert next split line
       ABS  @GOLF             Check flag setting for enter
       JNE  SCRL01            Enter key, scroll
 
INSC30 DEC  R1                Not enter key
       JMP  SCRL01            Scroll screen
 
INSC40 SETO @ICHRF            Not word wrap so set flag
       RT
 
*  Insert a blank line ahead of present line
*------------------------------------------
*  New page chars
 
INSERT BL   @UPDATE           Last (blanked) line to buffer
       JMP  INSERU
 
PAGE   BL   @UPDATE           Last line to buffer
       LI   R9,TEMPM6         Use temp buffer
       A    @LMRG,R9          Over to left margin
       MOVB @FORMF,*R9+       Write in <new page>
       MOVB @EOT,*R9+         Write a <cr>
INSERU BL   @INSLIN           Place in text buffer
       JMP  INSC30
 
*----------------*
*    MODE KEYS   *
*----------------*
 
*  Release to full margins
 
GOLEFT EQU  $
MRREL  EQU  $
       SETO @GOLF             Allow outside margins
MRREM  MOV  @DSIX,@LMRG       Full left margin
       MOV  @D85,@RMRG        Full right margin
       RT
 
*  Set/toggle  mode flags
*------------------------
 
WORDW  EQU  $                 31 w ----
       SETO R10               Flag <ctl-0> entry
       SETO @CTLMDE           Fake up to be changed
       JMP  CTKL10
 
CMODE  EQU  $
       CLR  R10               Flag <ctl-U> entry
CTKL10 LI   R8,CCSR           Underline cursor data
       MOV  @WPMODE,R9        Fetch flag pointer
       ABS  @CTLMDE           Check control char mode ( +1 or 0 )
       DEC  @CTLMDE           Toggle it ( 0 or -1 )
       JNE  CTKL30            Go set underline cursor
 
       ABS  *R9               Normalize flag
       A    R10,*R9           Toggle it
       JEQ  CTKL20            Flag off - hollow cursor
 
       MOV  @WDPROC,@CRFLAG   Copy to CR
       LI   R8,WCSR           Wrap (or Asm) cursor
       JMP  CTKL30
 
CTKL20 LI   R8,OCSR           Hollow cursor
CTKL30 BL   @VSTRV            Write in current cursor shape
       DATA FCSRP             Pattern table address
CTKL40 B    @START            Back to start
 
*-----------------------------------------------------------
 
