changeset 1794:277b53362535

Additions by Phill
author boisy
date Sun, 10 Apr 2005 13:02:33 +0000
parents 85ee5522dbc1
children f5397babb085
files level1/defsfile.dragon level1/modules/clock.asm level1/modules/co51.asm
diffstat 3 files changed, 348 insertions(+), 255 deletions(-) [+]
line wrap: on
line diff
--- a/level1/defsfile.dragon	Sat Apr 09 23:01:29 2005 +0000
+++ b/level1/defsfile.dragon	Sun Apr 10 13:02:33 2005 +0000
@@ -4,6 +4,5 @@
          use   scfdefs
          use   rbfdefs
          use   systype
-         use   releasedefs
 		 use   dgndefs
 		 
--- a/level1/modules/clock.asm	Sat Apr 09 23:01:29 2005 +0000
+++ b/level1/modules/clock.asm	Sun Apr 10 13:02:33 2005 +0000
@@ -30,6 +30,17 @@
 * Fixed incorrect value for PIA initialization.  Robert indicated
 * that it should be $3434, not $3435.
 *		
+*   9r7    2005/04/08  Phill Harvey-Smith
+* Made the above level dependent as having PIAs inited with $3434
+* will disable the IRQ from them, this is ok for Level 2/CoCo 3 as the
+* IRQ is later enabled from the GIME, however the CoCo 1,2 and Dragon
+* do not posses a GIME so anything dependent on the clock tick will 
+* hang. So changed to conditionaly compile based on level :-
+*
+* Level 1  $3435
+* Level 2  $3434
+*
+
 	nam	Clock     
 	ttl	OS-9 System Clock
 		
@@ -176,8 +187,15 @@
 	sta	3,x		enable DDRB
 	coma	
 	sta	2,x		set port B all outputs
+
 ;	ldd	#$343C		[A]=PIA0 CRA contents, [B]=PIA0 CRB contents
+
+	IFGT	Level-1
 	ldd	#$3434		as per Robert Gault's suggestion
+	ELSE
+	ldd	#$3435		IRQ needs to be left enabled for Level1, as no GIME generated IRQ
+	ENDIF
+	
 	sta	1,x		CA2 (MUX0) out low, port A, disable HBORD high-to-low IRQs
 	stb	3,x		CB2 (MUX1) out low, port B, disable VBORD low-to-high IRQs
 
--- a/level1/modules/co51.asm	Sat Apr 09 23:01:29 2005 +0000
+++ b/level1/modules/co51.asm	Sun Apr 10 13:02:33 2005 +0000
@@ -1,5 +1,5 @@
 ********************************************************************
-* drvr51 - Driver for The 51 column by 24 line video display
+* CO51 - 51x24 Graphics Console Output Subroutine for CCIO
 *
 * $Id$
 *
@@ -23,12 +23,15 @@
 * Finished converting to c051 driver, moved all variable 
 * storage into ccio module (defined in cciodefs).
 *
-
+* 		   2005/04/09  P.Harvey-Smith.
+* Replaced all ; comment chars with * for benefit of native 
+* asm. Re-implemented (hopefully) non-destructive cursor which
+* is XORed onto the screen. Commented character drawing routines
+* and replaced the V51xx names with more meaningful ones.
+*
 
-         nam   c051
-         ttl   co Driver for The 51 column by 24 line video display
-
-* Disassembled 02/07/06 21:17:23 by Disasm v1.6 (C) 1988 by RML
+         nam   CO51
+         ttl   51x24 Graphics Console Output Subroutine for CCIO
 
          ifp1
          use   defsfile.dragon
@@ -42,30 +45,14 @@
 
          mod   eom,name,tylg,atrv,start,size
 
-;V.51EscSeq    rmb   1
-;V.51CtrlDispatch    rmb   2
-;V.51ScrnA    rmb   2
-;V.51XPos    rmb   1		; X position of cursor
-;V.51YPos    rmb   1		; Y position of cursor
-;V.5126    rmb   1
-;V.5130    rmb   1
-;V.5131    rmb   1
-;V.5132    rmb   1
-;V.5133    rmb   1
-;V.5134    rmb   1
-;V.5135    rmb   1
-;V.5136    rmb   1
-
-;V.51ReverseFlag    rmb   1
-;V.51UnderlineFlag    rmb   1
 size     equ   .
 
          fcb   UPDAT.
 
 
-ScreenSize	EQU	$1800	; Screen Size in Bytes
+ScreenSize	EQU	$1800	* Screen Size in Bytes
 
-name     fcs   /co51/
+name     fcs   /CO51/
          fcb   edition
 
 start    lbra  Init
@@ -74,32 +61,36 @@
          lbra  SetStat
          lbra  Term
 
-Init    pshs	y
-	ldy	#$aa55
-	puls	y
+Init 
+*	pshs	y
+*	ldy	#$aa69
+*	puls	y
+
 	pshs  u,a
 
-	 ldd   #ScreenSize+$100	; Request a screenful of ram + $100 bytes
+	 ldd   #ScreenSize+$100	* Request a screenful of ram + $100 bytes
          os9   F$SRqMem 
-         bcs   InitExit		; Error : exit
+         bcs   InitExit		* Error : exit
          tfr   u,d
-         ldu   $01,s		; Restore saved u
+         ldu   $01,s		* Restore saved u
          tfr   d,x
-         bita  #$01		; Check that memory block starts at even page
-         beq   L0066		; Yes base of screen = base of memory block
-         leax  >$0100,x		; no Move to next page
+         bita  #$01		* Check that memory block starts at even page
+         beq   L0066		* Yes base of screen = base of memory block
+         leax  >$0100,x		* no Move to next page
          bra   L0068
 L0066    adda  #$18
 L0068    stx   V.51ScrnA,u
          tfr   d,u
          ldd   #$0100
-         os9   F$SRtMem		; Return unneeded page to OS 
+         os9   F$SRtMem		* Return unneeded page to OS 
          ldu   $01,s
+	 
+	 clr	V.51CursorOn,u	* Flag cursor off
 	 lbsr  DoHome		
 	 lbsr  DoReverseOff
          lbsr  DoCLS
 	 ldb   V.COLoad,u
-         orb   #ModCo51		; set to CO51 found (?)
+         orb   #ModCo51		* set to CO51 found (?)
 InitSaveExit
 	 stb   V.COLoad,u
          clrb  
@@ -110,13 +101,13 @@
 InitFlag fcb   $00
 
 Term     pshs  y,x
-         pshs  u		; save U
-         ldd   #ScreenSize	; Graphics memory size
-         ldu   V.51ScrnA,u 	; get pointer to memory
-         os9   F$SRtMem 	; return to system
-         puls  u		; restore U
+         pshs  u		* save U
+         ldd   #ScreenSize	* Graphics memory size
+         ldu   V.51ScrnA,u 	* get pointer to memory
+         os9   F$SRtMem 	* return to system
+         puls  u		* restore U
          ldb   V.COLoad,u
-         andb  #~ModCo51	; Set co51 unot loaded
+         andb  #~ModCo51	* Set co51 unot loaded
          bra   InitSaveExit
 
 * Write
@@ -124,29 +115,31 @@
 *        Y = path desc ptr
 
 Write 
-	pshs	y
-	ldy	#$aa56
-	puls	y
+*	pshs	y
+*	ldy	#$aa56
+*	puls	y
 	
-L012C    ldb   V.51EscSeq,u
+L012C   ldb   V.51EscSeq,u
          bne   L0165
-         cmpa  #$1B		escape?
-         bne   L013A
-         inc   V.51EscSeq,u		flag ESC seq
+         cmpa  #$1B		* escape?
+         bne   CheckForNormal
+         inc   V.51EscSeq,u	* flag ESC seq
          clrb  
 L0139    rts   
 
-L013A    cmpa  #$20
-         bcs   DoCtrlChar			; Control charater ?
+CheckForNormal    
+	 cmpa  #$20
+         bcs   DoCtrlChar			* Control charater ?
          cmpa  #$7F			
-         bcc   DoCtrlChar			; or upper bit set	
+         bcc   DoCtrlChar			* or upper bit set	
          bra   DoNormalChar
 
 DoCtrlChar    
 	leax  >CtrlCharDispatch,pcr
 L0148    tst   ,x
          bne   L0150
-L014C    clr   V.51EscSeq,u
+CancelEscSequence    
+	 clr   V.51EscSeq,u
          rts   
 
 L0150    cmpa  ,x+
@@ -166,101 +159,124 @@
          beq   L0148
          jmp   [V.51CtrlDispatch,u]
 
-DoNormalChar    
-         inc   V.5132,u
-         bsr   L01B3
-         tst   V.51UnderlineFlag,u
-         beq   L0185
-         lda   #$F8
+DoNormalChar   
+	 pshs	y,a
+	 lbsr	DoEraseCursor
+	 puls	y,a
+         inc   V.51CursorChanged,u
+         bsr   DrawCharacter
+         tst   V.51UnderlineFlag,u	* Are we underlining ?
+         beq   L0185			* no : update cursor
+         lda   #$F8			* Yes : do underline, then update cursor
          leay  <-$40,y
          lbsr  L0236
 
-L0185    lda   V.51XPos,u
+L0185    lda   V.51XPos,u		* Get current X pos
+         inca  				* increment it
+         cmpa  #$33			* past end of line ?
+         bcs   L01A2			* no : continue
+         clr   V.51XPos,u		* Yes reset x=0
+         lda   V.51YPos,u		* increment y pos
          inca  
-         cmpa  #$33
-         bcs   L01A2
-         clr   V.51XPos,u
-         lda   V.51YPos,u
-         inca  
-         cmpa  #$18
-         bcs   L019D
-         lbsr  SoScrollScreen
+         cmpa  #$18			* Past last line ?
+         bcs   L019D			* No : continue
+         lbsr  SoScrollScreen		* Yes : scroll screen one line
          bra   L01A5
-L019D    sta   V.51YPos,u
+L019D    sta   V.51YPos,u		* Ypdate Y pos
          bra   L01A5
 	 
-L01A2    sta   V.51XPos,u
-L01A5    clr   V.5133,u
-         ldd   V.51XPos,u
-         std   V.5130,u
-         dec   V.5132,u
-         clrb  
-         rts   
-L01B3    tfr   a,b
-         subb  #$20
+L01A2    sta   V.51XPos,u		* Update X pos
+L01A5    
+         ldd   V.51XPos,u		* Update old Cursor pos
+         std   V.51OldCursorPosX,u
+         dec   V.51CursorChanged,u
+	 lbsr	DoDisplayCursor		* Display cursor
+	 clrb  				* Flag no error
+         rts   				* Return to caller
+	 
+*
+* Draw the normal character $20..$7f, in the a register
+* at position stored in V.51Xpos,V.51Ypos
+*
+
+DrawCharacter    
+	 tfr   a,b
+         subb  #$20			* Make b an offset into table
          clra  
-         leax  >L06A0,pcr
-         lslb  
+         leax  >CharacterShapes,pcr	* point to character shape table
+         lslb  				* Multiply b by 4 into d (4 bytes/character) 
          rola  
          lslb  
          rola  
-         leax  d,x
-         ldb   #$05
+         leax  d,x			* Point X at required character's bitmap
+         ldb   #$05			* Work out pixel X co-ordinate of current cursor
          lda   V.51XPos,u
          mul   
-         pshs  b
-         lsra  
+         pshs  b			* Save pixel x
+         lsra  				* Divide pixel-x by 8, to get byte offset into line 
          rorb  
          lsra  
          rorb  
          lsra  
          rorb  
-         puls  a
-         anda  #$07
+         puls  a			* restore pixel X
+         anda  #$07			* Calculate offset within byte where character begins
          pshs  b
-         sta   V.5126,u
-         tst   V.5134,u
+         sta   V.51BytePixOffset,u
+         tst   V.51XORFlag,u
          bne   L01FF
-         tfr   a,b
-         lda   #$F8
+         tfr   a,b			* Calculate a mask for character data 
+         lda   #$F8			* shifts $f8 right b times
          tstb  
-         beq   L01FA
-L01E5    lsra  
-         decb  
-         bhi   L01E5
-         bne   L01EE
-         rorb  
+         beq   L01FA			* Done all bits ?	
+L01E5    lsra  				* shift mask right
+         decb  				* decrement count
+         bhi   L01E5			* done all ?
+         bne   L01EE			* have we shifted any mask bits off right hand end ?
+         rorb  				
          bra   L01FA
-L01EE    pshs  b
-         ldb   #$80
-L01F2    lsra  
+
+L01EE    pshs  b			* Save count on stack
+         ldb   #$80			* start to build mask for second byte as well
+L01F2    lsra  				* shift bits from bottom of a to top of b
          rorb  
-         dec   ,s
-         bne   L01F2
-         leas  $01,s
+         dec   ,s			* decrement count
+         bne   L01F2			* if any shifts left loop again
+         leas  $01,s			* drop count
+	 
+* When we reach here we should have a pair of bytes in d which indicate where exactly the
+* character should be drawn, this may be partly in each
+	 
 L01FA    coma  
          comb  
-         std   V.5135,u
-L01FF    ldy   V.51ScrnA,u
+         std   V.51ScreenMask1,u	* Save screen mask
+
+* The code below works out the offset of the character cell to be updated, this works because
+* the y co ordinate is loaded into the high byte of d, effectivley multiplying it by 256, since
+* each screen line is 32 bytes wide, and each character is 8 pixels tall this works out as 8x32=256 
+
+L01FF    ldy   V.51ScrnA,u		* Point y at screen memory address
          lda   V.51YPos,u
-         ldb   ,s+
-         leay  d,y
-         lda   #$04
+         ldb   ,s+			* Retrieve byte offset from stack 
+         leay  d,y			* calculate screen address.
+         lda   #$04			* get character data byte count, 4 bytes of 8 nibbles
          pshs  a
-         inc   V.5132,u
-L0211    lda   ,x
-         anda  #$F0
-         bsr   L0236
-         lda   ,x+
-         anda  #$0F
-         bsr   L0227
-         dec   ,s
-         bne   L0211
-         dec   V.5132,u
-         clrb  
-         puls  pc,b
+         inc   V.51CursorChanged,u	* flag character at cursor being changed
 
-L0227    ldb   V.5126,u
+L0211    lda   ,x			* get a byte from character data
+         anda  #$F0			* mask out even line 
+         bsr   L0236			* update screen
+         lda   ,x+			* Get again
+         anda  #$0F			* mask out odd line
+         bsr   L0227			* update screen
+         dec   ,s			* Decrement character data byte counter
+         bne   L0211			* all done ?
+         dec   V.51CursorChanged,u	* Flag character update finished
+         clrb  				* flag no error
+         puls  pc,b			* return to caller
+	 
+
+L0227    ldb   V.51BytePixOffset,u	
          subb  #$04
          bhi   L023B
          beq   L0250
@@ -268,10 +284,12 @@
          incb  
          bne   L0230
          bra   L0250
-L0236    ldb   V.5126,u
+	 
+L0236    ldb   V.51BytePixOffset,u	* Retrieve byte pixel offset
          beq   L0250
-L023B    lsra  
-         decb  
+	 
+L023B    lsra  				* manipulate character data into correct position
+         decb  				* in a similar way to the mask above
          bhi   L023B
          bne   L0244
          rorb  
@@ -283,30 +301,33 @@
          dec   ,s
          bne   L0248
          leas  $01,s
-L0250    tst   V.5134,u
-         bne   L0273
-         tst   V.51ReverseFlag,u
-         beq   L0262
-         coma  
+	 
+L0250    tst   V.51XORFlag,u		* are we XORing data direct to screen ?
+         bne   L0273			* Yes : just do it
+         tst   V.51ReverseFlag,u	* are we in reverse mode ?
+         beq   L0262			* no : just output data
+         coma  				* set mask up for reverse mode
          comb  
-         eora  V.5135,u
-         eorb  V.5136,u
+         eora  V.51ScreenMask1,u
+         eorb  V.51ScreenMask2,u
 
-L0262    pshs  b,a
-         ldd   V.5135,u
+L0262    pshs  b,a			* combine mask and screen data
+         ldd   V.51ScreenMask1,u
          anda  ,y
          andb  $01,y
          addd  ,s++
-L026D    std   ,y
+
+L026D    std   ,y			* screen update
          leay  <$20,y
          rts   
-L0273    eora  ,y
+
+L0273    eora  ,y			* XOR onto screen
          eorb  $01,y
          bra   L026D
 
-;
-; $07 - BEL (ding!)
-;
+*
+* $07 - BEL (ding!)
+*
 
 DoBell    
 	ldx   #$FF20
@@ -321,31 +342,41 @@
          bne   L0286
          decb  
          bne   L027E
-         lbra  L014C
-;
-; $08 - BS (left arrow)
-;
+         lbra  CancelEscSequence
+*
+* $08 - BS (left arrow)
+*
 DoBackspace    
+	lbsr  DoEraseCursor
 	dec   V.51XPos,u
-         bpl   L02A6
-         lda   #$32
-         sta   V.51XPos,u
-;
-; $1b44 - (cursor up)
-;
-DoCursorUp    
-	dec   V.51YPos,u
+        bpl   DoBSUpdateCursor
+        lda   #$32
+        sta   V.51XPos,u
+	bra   DoCursorUp1	 
+*
+* $1b44 - (cursor up)
+*
+DoCursorUp
+	lbsr   DoEraseCursor
+DoCursorUp1	
+	dec    V.51YPos,u
          bpl   L02A6
          clr   V.51YPos,u
          lbsr  L035E
 L02A6    lbsr  L0484
-         lbra  L014C
+         lbra  CancelEscSequence
 
-;
-; $0a, $1b45 - LF, (cursor down)
-;
+DoBSUpdateCursor
+	lbsr   L0484
+	lbsr   DoDisplayCursor
+	rts
+
+*
+* $0a, $1b45 - LF, (cursor down)
+*
 
 DoLineFeed    
+	lbsr	DoEraseCursor
 	 lda   V.51YPos,u
          inca  
          cmpa  #$18
@@ -353,34 +384,36 @@
          lbsr  SoScrollScreen
          bra   L02BC
 L02B9    sta   V.51YPos,u
-L02BC    bra   L02A6
+L02BC   lbsr	DoDisplayCursor 
+	bra   L02A6
 
-;
-; $0d - CR (return)
-;
+*
+* $0d - CR (return)
+*
 
 DoCarrageReturn    
+	lbsr   DoEraseCursor
 	 clr   V.51XPos,u
          bra   L02A6
 
-;
-; $0c - FF (clear screen)
-;
-DoCLS    ldy   V.51ScrnA,u
+*
+* $0c - FF (clear screen)
+*
+DoCLS  
+	ldy    V.51ScrnA,u
          leay  >$0080,y
          lda   #$18
          pshs  a
-         inc   V.5132,u
+         inc   V.51CursorChanged,u
 L02D2    bsr   L0314
          dec   ,s
          bne   L02D2
          leas  $01,s
          clra  
          clrb  
-         sta   V.5133,u
-         std   V.5130,u
+         std   V.51OldCursorPosX,u
          std   V.51XPos,u
-         dec   V.5132,u
+         dec   V.51CursorChanged,u
          ldx   #$FF20
          lda   $02,x
          ora   #$F0
@@ -393,7 +426,10 @@
          lsra  
          ldb   #$07
          bsr   L0305
-         lbra  L014C
+        clr	V.51CursorOn,u		* Flag cursor is off
+	lbsr	DoDisplayCursor		* Display cursor
+	lbra  CancelEscSequence
+
 L0305    lsra  
          bcc   L030E
          leax  $01,x
@@ -402,7 +438,8 @@
 L030E    sta   ,x++
 L0310    decb  
          bne   L0305
-         rts   
+	rts   
+	
 L0314    lda   #$10
 L0316    pshs  a
          lda   V.51ReverseFlag,u
@@ -418,33 +455,33 @@
          dec   ,s
          bne   L031D
          leay  >$00E0,y
-         puls  pc,b
+	puls  pc,b
 
 SoScrollScreen    
 	 ldy   V.51ScrnA,u
-         inc   V.5132,u
+         inc   V.51CursorChanged,u
          pshs  u
          leau  >$0100,y
          lda   #$10
          bsr   L037C
          puls  u
-         dec   V.5131,u
+         dec   V.51OldCursorPosY,u
 
 L0354    leay  >$0080,y
          bsr   L0314
-         dec   V.5132,u
+         dec   V.51CursorChanged,u
          rts   
 
 L035E    ldy   V.51ScrnA,u
          leay  >$17F0,y
-         inc   V.5132,u
+         inc   V.51CursorChanged,u
          pshs  u
          leau  >-$0100,y
          lda   #$F0
          bsr   L037C
          leay  ,u
          puls  u
-         inc   V.5131,u
+         inc   V.51OldCursorPosY,u
          bra   L0354
 
 L037C    ldb   #$17
@@ -475,16 +512,16 @@
          bne   L0380
          puls  pc,b
 
-;
-; $1b42 - clear to end of line
-;
+*
+* $1b42 - clear to end of line
+*
 
 DoClrEOL    
-	inc   V.5132,u
+	inc   V.51CursorChanged,u
          bsr   L03BA
-         dec   V.5132,u
-         lbra  L014C
-L03BA    clr   V.5133,u
+         dec   V.51CursorChanged,u
+         lbra  CancelEscSequence
+L03BA    
          ldb   V.51XPos,u
          pshs  b
          bitb  #$07
@@ -495,7 +532,7 @@
 L03CB    lda   #$01
          pshs  a
 L03CF    lda   #$20
-         lbsr  L01B3
+         lbsr  DrawCharacter
          lda   V.51XPos,u
          inca  
          sta   V.51XPos,u
@@ -525,12 +562,13 @@
          lbsr  L0316
 L040D    puls  a
          sta   V.51XPos,u
-         rts   
-;
-; $1b4A - clear to end of screen
-;
+	lbsr	DoDisplayCursor
+	rts   
+*
+* $1b4A - clear to end of screen
+*
 DoClearEOS    
-	inc   V.5132,u
+	inc   V.51CursorChanged,u
          bsr   L03BA
          lda   #$17
          suba  V.51YPos,u
@@ -540,21 +578,25 @@
          dec   ,s
          bne   L0421
          leas  $01,s
-L042A    dec   V.5132,u
-         lbra  L014C
+L042A    dec   V.51CursorChanged,u
+	 lbsr  DoDisplayCursor
+         lbra  CancelEscSequence
 
-;
-;$0b - (cursor home)
-;
+*
+*$0b - (cursor home)
+*
 DoHome    
+	lbsr  DoEraseCursor
 	clr   V.51XPos,u
         clr   V.51YPos,u
-        lbra  L02A6
+	lbsr  DoDisplayCursor
+	lbra  L02A6
 
-;
-; $1b41xxyy - move cursor to col xx (0-50) row yy (0-23)
-;
+*
+* $1b41xxyy - move cursor to col xx (0-50) row yy (0-23)
+*
 DoGotoXY    
+	lbsr	DoEraseCursor
 	ldb   V.51EscSeq,u
          subb  #$02
          bne   L0442
@@ -572,76 +614,110 @@
          bcs   L0456
          lda   #23
 L0456    sta   V.51YPos,u
-L0459    lbra  L02A6
+L0459   lbsr  DoDisplayCursor
+	lbra  L02A6
 
-;
-; $1b43 - (cursor right)
-;
+*
+* $1b43 - (cursor right)
+*
 DoCursorRight    
+	lbsr	DoEraseCursor
 	inc   V.51XPos,u
          lda   V.51XPos,u
          cmpa  #$33
          bcs   L0459
          clr   V.51XPos,u
          lbra  DoLineFeed
-;
-; $1b46 - reverse on
-;
+*
+* $1b46 - reverse on
+*
 DoReverseOn    
 	lda   #$FF
          coma  
 L046F    sta   V.51ReverseFlag,u
-         lbra  L014C
+         lbra  CancelEscSequence
 
-;
-; $1b47 - reverse off
-;
+*
+* $1b47 - reverse off
+*
 DoReverseOff    
 	lda   #$FF
          bra   L046F
 
-;
-; $1b48 - underline on
-;
+*
+* $1b48 - underline on
+*
 DoUnderlineOn    
 	lda   #$FF
 L047B    sta   V.51UnderlineFlag,u
-         lbra  L014C
+         lbra  CancelEscSequence
 
-;
-; $1b49 - underline off
-;
+*
+* $1b49 - underline off
+*
 DoUnderlineOff    
 	clra  
-         bra   L047B
+        bra   L047B
+	 
 	 
 L0484    ldd   V.51XPos,u
-         inc   V.5132,u
-         tst   V.5133,u
-         bne   L0494
-         std   V.5130,u
+         inc   V.51CursorChanged,u
+         std   V.51OldCursorPosX,u
          bra   L04B9
 	 
 L0494    pshs  b,a
-         ldd   V.5130,u
-         inc   V.5134,u
+         ldd   V.51OldCursorPosX,u
+         inc   V.51XORFlag,u
          tstb  
          bmi   L04AB
          cmpb  #$18
          bcc   L04AB
          std   V.51XPos,u
          lda   #$7F
-         lbsr  L01B3
+         lbsr  DrawCharacter
 
 L04AB    puls  b,a
          std   V.51XPos,u
-         std   V.5130,u
-         clr   V.5133,u
-         dec   V.5134,u
-L04B9    dec   V.5132,u
+         std   V.51OldCursorPosX,u
+         dec   V.51XORFlag,u
+L04B9    dec   V.51CursorChanged,u
          clrb  
          rts   
 
+*
+* Display and Erase cursor routines, work by xoring cursor character onto the
+* screen, the variable V.51CursorOn, is implemented such that it prevents
+* multiple calls to these routines from acting as an inverse, so that they 
+* may be called from any code, irispective of if the cursor is already on/off.
+*
+
+*
+* Display Cursor.
+*
+DoDisplayCursor
+	tst	V.51CursorOn,u		* Get cursor on flag
+	bne	DoCursorOnEnd		* Yes : don't re-display
+	bsr	DoCursorCommon		* Display cursor
+	inc	V.51CursorOn,u		* Flag cursor on
+DoCursorOnEnd	
+	rts
+*
+* Erase cursor
+*
+DoEraseCursor
+	tst	V.51Cursoron,u		* Get cursor on flag
+	beq	DoEraseCursorEnd	* no : don't atempt to turn off
+	bsr	DoCursorCommon		* Hide cursor
+	clr	V.51CursorOn,u		* Flag cursor off
+DoEraseCursorEnd	
+	rts
+
+DoCursorCommon
+	lda	#$7f			* Cursor character $7f = block
+	inc	V.51XORFlag,u		* Flag xor on screen
+	lbsr	DrawCharacter		* Draw it
+	dec	V.51XORFlag,u		* Flag no xor on screen
+	rts
 
 L04CA    clrb  
          rts   
@@ -663,45 +739,45 @@
 
 * control characters dispatch table
 CtrlCharDispatch  
-	fcb 	$07		BEL 		; (beep)
+	fcb 	$07		BEL 		* (beep)
 	fdb 	DoBell-CtrlCharDispatch	$FC0B
-        fcb 	$08		BS 		; (left arrow)
-        fdb 	DoBackspace-CtrlCharDispatch	; $FC23
-        fcb 	$0A		LF 		; (down arrow)
-        fdb 	DoLineFeed-CtrlCharDispatch	; $FC3E
-        fcb 	$0D		CR 		; (return)
-        fdb 	DoCarrageReturn-CtrlCharDispatch	;$FC50
-        fcb 	$0C		FF 		; (clear screen)
+        fcb 	$08		BS 		* (left arrow)
+        fdb 	DoBackspace-CtrlCharDispatch	* $FC23
+        fcb 	$0A		LF 		* (down arrow)
+        fdb 	DoLineFeed-CtrlCharDispatch	* $FC3E
+        fcb 	$0D		CR 		* (return)
+        fdb 	DoCarrageReturn-CtrlCharDispatch	*$FC50
+        fcb 	$0C		FF 		* (clear screen)
         fdb 	DoCLS-CtrlCharDispatch	$FC55
-        fcb 	$0B				; (cursor home)
+        fcb 	$0B				* (cursor home)
         fdb 	DoHome-CtrlCharDispatch	$FDC2
         fcb 	$00
 
 * escape sequences dispatch table
 EscCharDispatch  
-	fcb 	$41				; cursor xy
-        fdb 	DoGotoXY-CtrlCharDispatch	; $FDCB
-        fcb 	$42				; clear EOL
-        fdb 	DoClrEOL-CtrlCharDispatch	; $FD41
-        fcb 	$43				; cursor right
-        fdb 	DoCursorRight-CtrlCharDispatch	; $FDEE
-        fcb 	$44				; cursor up
-        fdb 	DoCursorUp-CtrlCharDispatch	; $FC2D
-        fcb 	$45				; cursor down
-        fdb 	DoLineFeed-CtrlCharDispatch	; $FC3E
-        fcb 	$46				; reverse on
-        fdb 	DoReverseOn-CtrlCharDispatch	; $FDFE
-        fcb 	$47				; reverse off
-        fdb 	DoReverseOff-CtrlCharDispatch	; $FE07
-        fcb 	$48				; underline on
-        fdb 	DoUnderlineOn-CtrlCharDispatch	; $FE0B
-        fcb 	$49				; underline off
-        fdb 	DoUnderlineOff-CtrlCharDispatch	; $FE13
-        fcb 	$4A				; clear EOS
-        fdb 	DoClearEOS-CtrlCharDispatch	; $FDA5
+	fcb 	$41				* cursor xy
+        fdb 	DoGotoXY-CtrlCharDispatch	* $FDCB
+        fcb 	$42				* clear EOL
+        fdb 	DoClrEOL-CtrlCharDispatch	* $FD41
+        fcb 	$43				* cursor right
+        fdb 	DoCursorRight-CtrlCharDispatch	* $FDEE
+        fcb 	$44				* cursor up
+        fdb 	DoCursorUp-CtrlCharDispatch	* $FC2D
+        fcb 	$45				* cursor down
+        fdb 	DoLineFeed-CtrlCharDispatch	* $FC3E
+        fcb 	$46				* reverse on
+        fdb 	DoReverseOn-CtrlCharDispatch	* $FDFE
+        fcb 	$47				* reverse off
+        fdb 	DoReverseOff-CtrlCharDispatch	* $FE07
+        fcb 	$48				* underline on
+        fdb 	DoUnderlineOn-CtrlCharDispatch	* $FE0B
+        fcb 	$49				* underline off
+        fdb 	DoUnderlineOff-CtrlCharDispatch	* $FE13
+        fcb 	$4A				* clear EOS
+        fdb 	DoClearEOS-CtrlCharDispatch	* $FDA5
         fcb 	$00
 
-L06A0
+CharacterShapes
 * 4x8 bitmap table for characters $20-$7f
 * each nibble represents a row of 4 dots
 * chars 20-27