changeset 3235:b05e48458203

Updated Level 1 GrfDrv & VTIO for performance and bug fixes.
author David Ladd <drencor-xeen@users.sourceforge.net>
date Sat, 17 Feb 2018 18:19:27 -0600
parents d1c60028af6e
children d276fe300c74
files level1/cmds/grfdrv.asm level1/modules/vtio.asm
diffstat 2 files changed, 469 insertions(+), 398 deletions(-) [+]
line wrap: on
line diff
--- a/level1/cmds/grfdrv.asm	Thu Feb 15 23:06:22 2018 -0600
+++ b/level1/cmds/grfdrv.asm	Sat Feb 17 18:19:27 2018 -0600
@@ -19,361 +19,417 @@
          use   cocovtio.d
          endc
 
-tylg     set   Systm+Objct   
+tylg     set   Systm+Objct
 atrv     set   ReEnt+rev
 rev      set   $00
 edition  set   1
 
          mod   eom,name,tylg,atrv,start,size
 
+* Stack offsets for LINE command
          org   0
 size     equ   .
+LnPxMsk1 rmb   1              (0) Pixel mask for byte from current Gfx Cursor X,Y coord of line
+LnPxMsk2 rmb   1              (1) Pixel mask for byte from caller specified X,Y coord
+LnAddr1  rmb   2              (2-3) Address on screen for user specified X,Y coord byte
+LnCoords rmb   2              (4-5) Inited to $0000
+LnXDir   rmb   1              (6) X direction/offset (-1 or +1)
+LnYDir   rmb   1              (7) Y direction/offset (-32 or +32)
+LnXDist  rmb   2              (8-9) Distance between X coords
+LnYDist  rmb   2              ($A-$B) Distance between Y coords
+LnX1     rmb   1              ($C) User specified X coord for start of line
+LnY1     rmb   1              ($D) User specified Y coord for start of line
+LnUnk3   rmb   1              ($E) Unknown so far
+LnStkSz  equ   .              Size of Line temp stack
 
-         fcb   $07 
+         fcb   $07
 
 name     fcs   /GrfDrv/
          fcb   edition
 
 * Dispatch table
-start    lbra  Init
-         lbra  Write
-         lbra  GetStat
-         lbra  SetStat
-         lbra  Term
-
-* 128x192 4 color pixel table
-Mode1Clr fdb   $0055,$aaff
+start    bra  Term            Init will just exit w/o error
+         NOP
+         bra  Write
+         NOP
+         bra  GetStat
+         NOP
+         bra  SetStat
+         NOP
+Term     clrb                 Exit without error
+         rts
+* Shouldn't need to pad Term (last entry) with 3rd byte
+*         NOP
 
 GetStat
-SetStat  comb
+SetStat  comb                 No GetStat/SetStat calls in Grfdrv, exit with "Unknown Service" error
          ldb   #E$UnkSvc
          rts
 
-Write    suba  #$15
-         leax  <Table,pcr
-         lsla
-         ldd   a,x
-         jmp   d,x
+Write    suba  #$15           Adjust code to 0 base, for lookup table
+         leax  <Table,pcr     Point to graphics code dispatch table
+         lsla                 * 2 since 2 bytes/entry
+         ldd   a,x            Get offset
+         jmp   d,x            Jump to routine
 
-Table    fdb   Do15-Table
-         fdb   Do16-Table
-         fdb   Do17-Table
-         fdb   Do18-Table
-         fdb   Do19-Table
-         fdb   Do1A-Table
-         fdb   NoOp-Table
-         fdb   Do1C-Table
-         fdb   Do1D-Table
-         fdb   NoOp-Table
-         fdb   NoOp-Table
+Table    fdb   Do15-Table     Set graphics cursor
+         fdb   Do16-Table     Draw Line
+         fdb   Do17-Table     Erase Line
+         fdb   Do18-Table     Set Point
+         fdb   Do19-Table     Erase Point
+         fdb   Do1A-Table     Draw Circle
+         fdb   Term-Table     $1b (Escape codes) are handled by VTIO and/or CoVDG
+         fdb   Do1C-Table     Erase Circle
+         fdb   Do1D-Table     Flood Fill
+         fdb   Term-Table     $1E handled elsewhere
+         fdb   Term-Table     $1F handled elsewhere
 
 * Fix X/Y coords:
 * - if Y > 191 then cap it at 191
 * - adjust X coord if in 128x192 mode
-FixXY    ldd   <V.NChar,u	get next 2 charas
-         cmpb  #192		Y greater than max?
-         bcs   L0053		branch if lower than
-         ldb   #191
-L0053    tst   <V.Mode,u	which mode?
-         bmi   L0059		branch if 256x192
-         lsra  			else divide X by 2
-L0059    std   <V.NChar,u	and save
-         rts   
+* Entry: U=static mem ptr
+*        <V.NChar,u = X,Y coords (1 byte each)
+*        A=X coord
+*        B=Y coord
+FixXY    ldd   <V.NChar,u     get next 2 chars
+         cmpb  #192           Y greater than max?
+         blo   L0053          No, continue
+         ldb   #191           Truncate @ 191
+L0053    tst   <V.Mode,u      which mode?
+         bmi   L0059          branch if 256x192
+         lsra                 else divide X by 2 (128)
+L0059    std   <V.NChar,u     and save coords
+         rts
 
 * $15 - set graphics cursor
-Do15     leax  <SetGC,pcr	load X with return address
-GChar2   ldb   #$02		need two parameters
-         lbra  GChar
-
-SetGC    bsr   FixXY		fix coords
-         std   <V.GCrsX,u	and save new gfx cursor pos
-
-NoOp
-Init
-Term     clrb  
-         rts   
+Do15     leax  <SetGC,pcr     load X with return address
+GChar2   ldb   #2             need two parameters
+         lbra  GChar          Get the coords, then come back to SetGC routine
 
-* $19 - erase point
-Do19     clr   <V.Msk1,u
-* $18 - set point
-Do18     leax  <DrawPnt,pcr
-         bra   GChar2
-
-DrawPnt  bsr   FixXY		fix coords
-         std   <V.GCrsX,u	save as new gfx cursor pos
-         bsr   DrwPt2
-         lbra  L014A
-DrwPt2   jsr   [<V.CnvVct,u]
-L0081    tfr   a,b
-         comb  
-         andb  ,x
-         stb   ,x
-         anda  <V.Msk1,u	and with mask
-         ora   ,x
-         sta   ,x
-         rts   
+SetGC    bsr   FixXY          fix coords
+         std   <V.GCrsX,u     and save new gfx cursor pos
+         clrb
+         rts
 
 * $17 - erase line
-Do17     clr   <V.Msk1,u	clear mask value
-
+Do17     clr   <V.Msk1,u      clear color mask
 * $16 - draw line
-Do16     leax  <DrawLine,pcr	load X with return address
-         bra   GChar2		need two params
+Do16     leax  <DrawLine,pcr  load X with return address
+         bra   GChar2         Get 2 more parameter bytes, then go to DrawLine
 
-DrawLine bsr   FixXY		fix up coords
-         leas  -$0E,s		make room on stack
-         std   $0C,s		save X/Y
-         jsr   [<V.CnvVct,u]	get address given X/Y
-         stx   $02,s		save on stack
-         sta   $01,s		and pixel too
-         ldd   <V.GCrsX,u	get current graphics cursor
-         jsr   [<V.CnvVct,u]	get address given X/Y
-         sta   ,s
-         clra  
-         clrb  
-         std   $04,s
-         lda   #$BF
+DrawLine bsr   FixXY          fix up coords for screen resolution
+         leas  -LnStkSz,s     make room on stack for line vars
+         std   LnX1,s         save caller supplied (and fixed up) X,Y
+         jsr   [<V.CnvVct,u]  get address given X/Y
+         stx   LnAddr1,s      save on stack
+         sta   LnPxMsk2,s     and it's pixel mask too
+         ldd   <V.GCrsX,u     get current graphics cursor
+         jsr   [<V.CnvVct,u]  get address given X/Y
+         sta   ,s             Save pixel mask for point @ gfx cursor location
+         clra
+         clrb
+         std   LnCoords,s     Init to $0000
+         lda   #191           Y=0 on bottom, so flip Gfx Cursor Y coord around
          suba  <V.GCrsY,u
-         sta   <V.GCrsY,u
-         lda   #$BF
+         sta   <V.GCrsY,u     Save flipped version
+         lda   #191           Now, do same for caller supplied Y coord
          suba  <V.NChr2,u
-         sta   <V.NChr2,u
-         lda   #$FF
-         sta   $06,s
-         clra  
+         sta   <V.NChr2,u     Save flipped version
+         lda   #-1            Init X direction to -1
+         sta   LnXDir,s
+         clra                 D=Gfx cursor X position
          ldb   <V.GCrsX,u
-         subb  <V.NChar,u
+         subb  <V.NChar,u     Subtract (as 16 bit) caller X coord
          sbca  #$00
-         bpl   L00D6
-         nega  
-         negb  
+         bpl   L00D6          If positive # (callers is left of gfx cursor), done
+       IFNE  H6309
+         negd                 If negative, flip to positive
+       ELSE
+         nega                 If negative, flip to positive
+         negb
          sbca  #$00
-         neg   $06,s
-L00D6    std   $08,s
-         bne   L00DF
-         ldd   #$FFFF
-         std   $04,s
-L00DF    lda   #$E0
-         sta   $07,s
-         clra  
-         ldb   <V.GCrsY,u
-         subb  <V.NChr2,u
+       ENDC
+         neg   LnXDir,s       Flip X direction to +1
+L00D6    std   LnXDist,s      Save # pixels between X coords
+         bne   L00DF          Not vertical line (0 distance), skip ahead
+         ldd   #$FFFF         If vertical line, change 4,s to $FFFF (-1 / -1)
+         std   LnCoords,s
+L00DF    lda   #-32           Init Y direction/increment to -32 (1 line on screen)
+         sta   LnYDir,s
+         clra
+         ldb   <V.GCrsY,u     D=Gfx cursor Y coord
+         subb  <V.NChr2,u     Subtract (as 16 bit) caller Y coord
          sbca  #$00
-         bpl   L00F4
-         nega  
-         negb  
+         bpl   L00F4          If positive # (callers is below gfx cursor), done
+       IFNE  H6309
+         negd                 If negative, flip to positive
+       ELSE
+         nega                 If negative, flip to positive
+         negb
          sbca  #$00
-         neg   $07,s
-L00F4    std   $0A,s
-         bra   L0100
-L00F8    sta   ,s
-         ldd   $04,s
-         subd  $0A,s
-         std   $04,s
-L0100    lda   ,s
-         lbsr  L0081
-         cmpx  $02,s
-         bne   L010F
-         lda   ,s
-         cmpa  $01,s
-         beq   L0143
-L010F    ldd   $04,s
-         bpl   L011D
-         addd  $08,s
-         std   $04,s
-         lda   $07,s
-         leax  a,x
-         bra   L0100
-L011D    lda   ,s
-         ldb   $06,s
-         bpl   L0133
-         lsla  
-         ldb   <V.Mode,u	which mode?
-         bmi   L012A		branch if 256x192
-         lsla  
-L012A    bcc   L00F8
-         lda   <V.4A,u
-         leax  -$01,x
-         bra   L00F8
-L0133    lsra  
-         ldb   <V.Mode,u	which mode?
-         bmi   L013A		branch if 256x196
-         lsra  
-L013A    bcc   L00F8
-         lda   <V.MCol,u
-         leax  $01,x
-         bra   L00F8
-L0143    ldd   $0C,s
-         std   <V.GCrsX,u
-         leas  $0E,s
-L014A    lda   <V.Msk2,u
-         sta   <V.Msk1,u
-         clrb  
-         rts   
+       ENDC
+         neg   LnYDir,s       Change Y direction/offset to +32
+L00F4    std   LnYDist,s      Save # pixels between Y coords
+         bra   L0100          Skip into drawing loop
+
+* Main line drawing loop
+L00F8    sta   ,s             Save new shifted pixel mask
+         ldd   LnCoords,s
+         subd  LnYDist,s      Subtract distance between Y coords
+         std   LnCoords,s     Save updated value
+L0100    lda   ,s             Get current pixel mask for Gfx Cursor X,Y coord
+         bsr   L0081          Draw point on screen
+         cmpx  LnAddr1,s      Are we still at same address on screen as we started?
+         bne   L010F          No, skip ahead
+         lda   ,s             Get Gfx Cursor X,Y coord pixel mask back
+         cmpa  LnPxMsk2,s     Same as pixel mask for caller's X,Y coord?
+         beq   L0143          Yes, skip ahead
+L010F    ldd   LnCoords,s
+         bpl   L011D          If >0, skip ahead
+         addd  LnXDist,s      Add to distance between X coords
+         std   LnCoords,s     Save overtop as new value
+         lda   LnYDir,s       Get Y direction/increment
+         leax  a,x            Bump Y coord up or down as appropriate (SIGNED, so can't use abx)
+         bra   L0100          Draw next pixel
+
+L011D    lda   ,s             Get current pixel mask for Gfx Cursor X,Y coord
+         ldb   LnXDir,s       Get X direction/offset
+         bpl   L0133          If positive, skip ahead
+         lsla                 If negative, shift pixel mask left 1 bit
+         ldb   <V.Mode,u      Check video mode
+         bmi   L012A          If 256x192, that's all we need.
+         lsla                 4 color mode, shift 2nd time (2 bits/pixel)
+L012A    bcc   L00F8          If we didn't hit the end of the byte, do next pixel in this byte
+         lda   <V.4A,u        Get mask for last pixel in a byte for current mode
+         leax  -1,x           Bump screen ptr to left by one
+         bra   L00F8          Back to the drawing loop
+
+L0133    lsra                 Shift active pixel mask to right 1 bit
+         ldb   <V.Mode,u      Get graphics mode
+         bmi   L013A          If 2 color, we are done shifting
+         lsra                 4 color, shift 1 more (2 bits/pixel)
+L013A    bcc   L00F8          Still more pixels in current byte, do next pixel
+         lda   <V.MCol,u      Get mask for first pixel in a byte for current mode
+         leax  1,x            Bump screen ptr to right by one
+         bra   L00F8          Back to drawing loop
+
+L0143    ldd   LnX1,s         Get destination X,Y coords from caller
+         std   <V.GCrsX,u     Save them as the new graphics cursor position
+         leas  LnStkSz,s      Eat temp Line stack
+L014A    lda   <V.Msk2,u      Get full byte color mask for current foreground color?
+         sta   <V.Msk1,u      Save in another mask
+         clrb                 Return w/o error
+         rts
 
 * $1C - erase circle
-Do1C     clr   <V.Msk1,u	clear mask value
+Do1C     clr   <V.Msk1,u      clear mask value
 * $1A - draw circle
 Do1A     leax  <Circle,pcr
-         ldb   #$01		require another param -- radius
-GChar    stb   <V.NGChr,u	one more char
-         stx   <V.RTAdd,u	return address
-         clrb  
-         rts   
+         ldb   #$01           require another param -- radius
+GChar    stb   <V.NGChr,u     one more char
+         stx   <V.RTAdd,u     return address
+         clrb
+         rts
+
+* $19 - erase point - moved here to (hopefully) allow bsr in Line
+Do19     clr   <V.Msk1,u      Color mask to $00 (background color)
+* $18 - set point
+Do18     leax  <DrawPnt,pcr   Routine to come back to when we get our parameter bytes
+         lbra  GChar2         Go get 2 more bytes; then come back to DrawPnt
+
+DrawPnt  lbsr  FixXY          fix coords
+         std   <V.GCrsX,u     save as new gfx cursor pos
+         bsr   DrwPt2
+         bra   L014A
 
-Circle   leas  -$04,s		make room on stack
-         ldb   <V.NChr2,u	get radius
-         stb   $01,s		store on stack
-         clra  
-         sta   ,s
-         addb  $01,s
+DrwPt2   jsr   [<V.CnvVct,u]  Get offset into screen memory & bit mask for pixel
+L0081    tfr   a,b            Duplicate pixel mask
+         comb                 Flip to keep background pixels
+         andb  ,x
+         stb   ,x
+         anda  <V.Msk1,u      and pixel mask with color mask
+         ora   ,x             Merge foreground pixel onto background
+         sta   ,x             Save it to screen
+         rts
+
+
+Circle   leas  -4,s           make room on stack
+* 6809/6309 - Chg next 4 lines to clra / ldb <V.NChr2,u / std ,s
+         clra
+         ldb   <V.NChr2,u     get radius
+         std   ,s             store b store on stack and make D=radius (both D and on stack)
+         addb  1,s            Add to itself (D=diameter)
          adca  #$00
-         nega  
-         negb  
+       IFNE  H6309
+         negd                 Invert sign of D
+       ELSE
+         nega                 Invert sign of D
+         negb
          sbca  #$00
-         addd  #$0003
-         std   $02,s
+       ENDC
+         addd  #$0003         And add 3
+         std   2,s            Save that
 L0179    lda   ,s
-         cmpa  $01,s
+         cmpa  1,s
          bcc   L01AB
-         ldb   $01,s
+         ldb   1,s
          bsr   L01B9
-         clra  
+         clra
          ldb   $02,s
          bpl   L0193
          ldb   ,s
-         lslb  
-         rola  
-         lslb  
-         rola  
+         lslb
+         rola
+         lslb
+         rola
          addd  #$0006
          bra   L01A3
-L0193    dec   $01,s
-         clra  
+
+L0193    dec   1,s
+         clra
          ldb   ,s
-         subb  $01,s
+         subb  1,s
          sbca  #$00
-         lslb  
-         rola  
-         lslb  
-         rola  
-         addd  #$000A
-L01A3    addd  $02,s
-         std   $02,s
+         lslb                 Multiply D by 4
+         rola
+         lslb
+         rola
+         addd  #10            And add 10
+L01A3    addd  2,s
+         std   2,s
          inc   ,s
          bra   L0179
+
 L01AB    lda   ,s
-         cmpa  $01,s
+         cmpa  1,s
          bne   L01B5
-         ldb   $01,s
+         ldb   1,s
          bsr   L01B9
-L01B5    leas  $04,s
-         bra   L014A
-L01B9    leas  -$08,s
+L01B5    leas  4,s            Eat temp stack
+         lbra  L014A          Copy color mask 2 to color mask 1 & return w/o error
+
+L01B9    leas  -8,s           Allocate another 8 byte temp stack
          sta   ,s
-         clra  
-         std   $02,s
-         nega  
-         negb  
+         clra
+         std   2,s
+       IFNE  H6309
+         negd
+       ELSE
+         nega
+         negb
          sbca  #$00
-         std   $06,s
+       ENDC
+         std   6,s
          ldb   ,s
-         clra  
+         clra
          std   ,s
-         nega  
-         negb  
+       IFNE  H6309
+         negd
+       ELSE
+         nega
+         negb
          sbca  #$00
-         std   $04,s
-         ldx   $06,s
+       ENDC
+         std   4,s
+         ldx   6,s
          bsr   L0202
-         ldd   $04,s
-         ldx   $02,s
+         ldd   4,s
+         ldx   2,s
          bsr   L0202
          ldd   ,s
-         ldx   $02,s
+         ldx   2,s
          bsr   L0202
          ldd   ,s
-         ldx   $06,s
+         ldx   6,s
          bsr   L0202
-         ldd   $02,s
+         ldd   2,s
          ldx   ,s
          bsr   L0202
-         ldd   $02,s
-         ldx   $04,s
+         ldd   2,s
+         ldx   4,s
          bsr   L0202
-         ldd   $06,s
-         ldx   $04,s
+         ldd   6,s
+         ldx   4,s
          bsr   L0202
-         ldd   $06,s
+         ldd   6,s
          ldx   ,s
          bsr   L0202
-         leas  $08,s
-         rts   
-L0202    pshs  b,a
-         ldb   <V.GCrsY,u
-         clra  
-         leax  d,x
-         cmpx  #$0000
-         bmi   L0214
-         cmpx  #$00BF
-         ble   L0216
-L0214    puls  pc,b,a
-L0216    ldb   <V.GCrsX,u
-         clra  
-         tst   <V.Mode,u
-         bmi   L0221
-         lslb  
-         rola  
-L0221    addd  ,s++
-         tsta  
+         leas  8,s            Eat temp stack & return
+         rts
+
+L0202    pshs  d
+         ldb   <V.GCrsY,u     Get Y coord of graphics cursor (center of circle)
+* 6809/6309 - B is unsigned, so an abx instead of clra / leax d,x should work (-2 bytes/-7 cyc)
+*         clra
+*         leax  d,x
+         abx
+         cmpx  #$0000         Off bottom of screen?
+         bmi   L0214          Yes, return
+         cmpx  #191           Off top of screen?
+         ble   L0216          No, go draw pixels
+L0214    puls  pc,d           Off screen vertically, return
+
+L0216    ldb   <V.GCrsX,u     Get X coord of graphics cursor (center of circle) into D
+         clra
+         tst   <V.Mode,u      Check graphics mode
+         bmi   L0221          2 color, skip ahead
+         lslb                 4 color, Shift left 1 bit
+         rola
+L0221    addd  ,s++           Add to ?
+         tsta                 If <256, continue
          beq   L0227
-         rts   
-L0227    pshs  b
-         tfr   x,d
-         puls  a
-         tst   <V.Mode,u	which mode?
-         lbmi  DrwPt2		branch if 256x192
-         lsra  			else divide A by 2
+         rts                  Else return
+
+L0227    pshs  b              Save 8 bit version of value
+         tfr   x,d            Move low byte of X to B
+         puls  a              And restore value, this time as A
+         tst   <V.Mode,u      Check graphics mode
+         lbmi  DrwPt2         If 2 color, draw pixel on screen & return from there
+         lsra                 4 color, shift 1 more first
          lbra  DrwPt2
 
 * $1D - flood fill
-Do1D     clr   <V.FFFlag,u
+Do1D     clr   <V.FFFlag,u    Clear flag
          leas  -$07,s
-         lbsr  L03AB
-         lbcs  L0346
+         lbsr  L03AB          Allocate 512 byte Flood fill stack, if not already allocated
+         lbcs  L0346          Not allocated, and couldn't get it, exit with error
          lda   #$FF
          sta   <V.4F,u
          ldd   <V.GCrsX,u
          lbsr  L0351
          lda   <V.4C,u
          sta   <V.4D,u
-         tst   <V.Mode,u	which mode?
-         bpl   L0261		branch if 128x192
-         tsta  
+         tst   <V.Mode,u      which mode?
+         bpl   L0261          branch if 128x192
+         tsta
          beq   L0267
          lda   #$FF
          bra   L0267
-L0261    leax  >Mode1Clr,pcr
-         lda   a,x
-L0267    sta   <V.4E,u
+
+* 128x192 4 color pixel table - NOTE THIS IS DUPLICATED IN VTIO
+Mode1Clr fcb   $00,$55,$aa,$ff
+
+* Entry: A=color # 0-3
+L0261    leax  <Mode1Clr,pcr  Point to 4 color color mask table
+         lda   a,x            Get mask for selected color
+L0267    sta   <V.4E,u        Save copy of it
          cmpa  <V.Msk1,u
          lbeq  L0346
          ldd   <V.GCrsX,u
 L0274    suba  #$01
-         bcs   L027F
+         bcs   L027F          Wrapped negative, skip ahead
          lbsr  L0351
-         bcs   L027F
          beq   L0274
-L027F    inca  
-         std   $01,s
+L027F    inca
+         std   1,s
 L0282    lbsr  L0384
          adda  #$01
          bcs   L0290
          lbsr  L0351
          bcs   L0290
          beq   L0282
-L0290    deca  
-         ldx   $01,s
+L0290    deca
+         ldx   1,s
          lbsr  L03D3
          neg   <V.4F,u
          lbsr  L03D3
@@ -383,175 +439,197 @@
          bpl   L02B3
          subb  #$01
          bcs   L029C
-         std   $03,s
+         std   3,s
          tfr   x,d
-         decb  
+         decb
          bra   L02BD
-L02B3    incb  
-         cmpb  #$BF
-         bhi   L029C
-         std   $03,s
+
+L02B3    incb                 Bump up Y coord
+         cmpb  #191           Off top of screen?
+         bhi   L029C          Yes, jump back
+         std   3,s
          tfr   x,d
-         incb  
-L02BD    std   $01,s
+         incb
+L02BD    std   1,s
          lbsr  L0351
          bcs   L029C
 L02C4    bne   L02D2
          suba  #$01
          bcc   L02CD
-         inca  
+         inca
          bra   L02D6
+
 L02CD    lbsr  L0351
          bcc   L02C4
 L02D2    adda  #$01
-         bcs   L029C
-L02D6    cmpd  $03,s
+         bcs   L029C          Wrapped over 255, go back
+L02D6    cmpd  3,s
          bhi   L029C
          bsr   L0351
          bcs   L029C
          bne   L02D2
-         std   $05,s
-         cmpd  $01,s
-         bcc   L02FB
-         ldd   $01,s
-         decb  
-         cmpd  $05,s
+         std   5,s
+         cmpd  1,s
+         bhs   L02FB
+         ldd   1,s
+         decb
+         cmpd  5,s
          beq   L02FB
          neg   <V.4F,u
-         ldx   $05,s
+         ldx   5,s
          lbsr  L03D3
          neg   <V.4F,u
-L02FB    ldd   $05,s
-L02FD    std   $01,s
+L02FB    ldd   5,s
+L02FD    std   1,s
 L02FF    bsr   L0351
          bcs   L030B
          bne   L030B
          bsr   L0384
          adda  #$01
-         bcc   L02FF
-L030B    deca  
-         ldx   $01,s
+         bcc   L02FF          Didn't wrap 255, loop again
+L030B    deca
+         ldx   1,s
          lbsr  L03D3
-         std   $05,s
+         std   5,s
          adda  #$01
-         bcs   L0326
-L0317    cmpd  $03,s
-         bcc   L0326
+         bcs   L0326          Wrapped 255, skip ahead
+L0317    cmpd  3,s
+         bhs   L0326
          adda  #$01
          bsr   L0351
          bcs   L0326
          bne   L0317
          bra   L02FD
-L0326    inc   $03,s
-         inc   $03,s
-         ldd   $03,s
-         cmpa  #$02
-         lbcs  L029C
-         ldd   $05,s
-         cmpd  $03,s
-         lbcs  L029C
+
+L0326    lda   3,s
+         inca
+         inca
+         sta   3,s
+         cmpa  #$02           High byte 0 or 1?
+         lblo  L029C          yes, go back (note - immediately calls L03F9, which immediately reloads D. 
+         ldd   5,s
+         cmpd  3,s
+         lblo  L029C
          neg   <V.4F,u
-         ldx   $03,s
+         ldx   3,s
          lbsr  L03D3
          lbra  L029C
-L0346    leas  $07,s
-         clrb  
-         ldb   <V.FFFlag,u
-         beq   L0350
-L034E    orcc  #Carry
-L0350    rts   
-L0351    pshs  b,a
-         cmpb  #$BF
-         bhi   L0380
-         tst   <V.Mode,u	which mode?
-         bmi   L0360		branch if 256x192
-         cmpa  #$7F
-         bhi   L0380
-L0360    jsr   [<V.CnvVct,u]
-         tfr   a,b
-         andb  ,x
-L0367    bita  #$01
-         bne   L0376
-         lsra  
-         lsrb  
-         tst   <V.Mode,u	which mode?
-         bmi   L0367		branch if 256x192
-         lsra  
-         lsrb  
+
+L0346    leas  7,s            Eat temp stack
+         clrb                 Default to no error (carry clear
+         ldb   <V.FFFlag,u    Was there an error?
+         beq   L0350          No, exit
+L034E    orcc  #Carry         Yes, flag carry
+L0350    rts
+
+* Entry: A=X coord (0-127 or 0-255 depending on mode)
+*        B=Y coord (0-191)
+* Exit: Carry set if pixel would have been off screen
+*       Carry clear if on screen,
+L0351    pshs  d
+         cmpb  #191
+         bhi   L0380          If past top of screen, exit with carry set
+         tst   <V.Mode,u      which gfx mode?
+         bmi   L0360          2 color, skip ahead
+         cmpa  #127           4 color, check if we are past right side of screen
+         bhi   L0380          Yes, exit with carry set
+L0360    jsr   [<V.CnvVct,u]  Coord on screen, get ptr to byte on screen for pixel into X (and pixel mask in A)
+         tfr   a,b            Dupe pixel mask to B
+         andb  ,x             Keep background screen contents
+L0367    bita  #$01           Is far right pixel set in the mask?
+         bne   L0376          Yes, exit with carry clear and other flags set (zero, negative,etc.)
+         lsra                 No, shift pixel masks right 1 bit
+         lsrb
+         tst   <V.Mode,u      If 2 color, check again
+         bmi   L0367
+         lsra                 If 4 color, shift once more (2 bits/pixel) and check again
+         lsrb
          bra   L0367
+
 L0376    stb   <V.4C,u
          cmpb  <V.4D,u
-         andcc #^Carry
-         puls  pc,b,a
+         andcc #^Carry        Force carry off, keep rest of bits from CMP
+         puls  pc,d
+
 L0380    orcc  #Carry
-         puls  pc,b,a
-L0384    pshs  b,a
-         jsr   [<V.CnvVct,u]
-         bita  #$80
-         beq   L03A6
+         puls  pc,d
+
+L0384    pshs  d              Save X,Y coords
+         jsr   [<V.CnvVct,u]  Figure out address on screen (and pixel mask)
+         bita  #%10000000     $80 - Left pixel in byte?
+         beq   L03A6          No, skip ahead
          ldb   <V.4E,u
          cmpb  ,x
          bne   L03A6
          ldb   <V.Msk1,u
          stb   ,x
-         puls  b,a
-         tst   <V.Mode,u	which mode?
-         bmi   L03A3		branch if 256x192
+         puls  d
+         tst   <V.Mode,u      2 color mode?
+         bmi   L03A3          Yes, skip ahead
          adda  #$03
-         rts   
+         rts
+
 L03A3    adda  #$07
-         rts   
-L03A6    lbsr  L0081
-         puls  pc,b,a
-L03AB    ldx   <V.FFSTp,u	get top of flood fill stack
-         beq   L03B5		if zero, we need to allocate stack
-         stx   <V.FFSPt,u	else reset flood fill stack ptr
-L03B3    clrb  
-         rts   
+         rts
+
+L03A6    lbsr  L0081          Draw pixel on screen
+         puls  pc,d
+
+L03AB    ldx   <V.FFSTp,u     get top of flood fill stack
+         beq   L03B5          if zero, we need to allocate stack
+         stx   <V.FFSPt,u     else reset flood fill stack ptr & return
+L03B3    clrb
+         rts
 
 * Allocate Flood Fill Stack
-L03B5    pshs  u		save U for now
-         ldd   #$0200		get 512 bytes
-         os9   F$SRqMem 	from system
-         bcc   AllocOk		branch if ok
-         puls  pc,u		else pull out with error
-AllocOk  tfr   u,d		move pointer to alloced mem to D
-         puls  u		get stat pointer we saved earlier
-         std   <V.FFMem,u	save pointer to alloc'ed mem
-         addd  #512		point D to end of alloc'ed mem
-         std   <V.FFSTp,u	and save here as top of fill stack
-         std   <V.FFSPt,u	and here
-         bra   L03B3		do a clean return
+L03B5    pshs  u              save U for now
+         ldd   #$0200         get 512 bytes
+         os9   F$SRqMem       from system
+         bcc   AllocOk        branch if ok
+         puls  pc,u           else pull out with error
+
+AllocOk  tfr   u,d            move pointer to alloced mem to D
+         puls  u              get stat pointer we saved earlier
+         std   <V.FFMem,u     save pointer to alloc'ed mem
+         addd  #512           point D to end of alloc'ed mem
+         std   <V.FFSTp,u     and save here as top of fill stack
+         std   <V.FFSPt,u     and here
+         clrb
+         rts
 
-L03D3    pshs  b,a
-         ldd   <V.FFSPt,u
-         subd  #$0004
-         cmpd  <V.FFMem,u
-         bcs   L03F2
-         std   <V.FFSPt,u
-         tfr   d,y
-         lda   <V.4F,u
-         sta   ,y
-         stx   $01,y
-         puls  b,a
-         sta   $03,y
-         rts   
-L03F2    ldb   #$F5
-         stb   <V.FFFlag,u
-         puls  pc,b,a
-L03F9    ldd   <V.FFSPt,u
-         cmpd  <V.FFSTp,u		top of flood fill stack?
-         lbcc  L034E
-         tfr   d,y
-         addd  #$0004
-         std   <V.FFSPt,u
-         lda   ,y
-         sta   <V.4F,u
-         ldd   $01,y
+* Add FFill stack entry (4 bytes)
+L03D3    pshs  d
+         ldd   <V.FFSPt,u     Get current FFill stack ptr
+         subd  #$0004         Add 4 bytes to it
+         cmpd  <V.FFMem,u     Have we filled all 512 bytes?
+         blo   L03F2          Yes, error out
+         std   <V.FFSPt,u     No, Save new FFill stack ptr
+         tfr   d,y            Move new ptr to indexable register
+         lda   <V.4F,u        Get?
+         sta   ,y             Save on stack
+         stx   1,y            Save (mem ptr on screen, I think?)
+         puls  d              Get ?? back
+         sta   3,y            Save A to FFill stack entry & return
+         rts
+
+L03F2    ldb   #E$Write       $F5 Write Error if FFill stack overflows
+         stb   <V.FFFlag,u    Save error code
+         puls  pc,d
+
+* Remove FFill stack entry (4 bytes)
+L03F9    ldd   <V.FFSPt,u     Get current FFill stack ptr
+         cmpd  <V.FFSTp,u     Have we already emptied stack?
+         lbhs  L034E          Yes, stack empty, exit with carry set
+         tfr   d,y            No, move to indexable register
+         addd  #$0004         Add 4 to it (eat 4 bytes from stack)
+         std   <V.FFSPt,u     Save as new FFill stack ptr
+         lda   ,y             Get byte from original FFill stack position
+         sta   <V.4F,u        Save it
+         ldd   1,y            
          tfr   d,x
-         lda   $03,y
+         lda   3,y
          andcc #^Carry
-         rts   
+         rts
 
          emod
 eom      equ   *
--- a/level1/modules/vtio.asm	Thu Feb 15 23:06:22 2018 -0600
+++ b/level1/modules/vtio.asm	Sat Feb 17 18:19:27 2018 -0600
@@ -563,9 +563,12 @@
          pshs  b              Save B
          sta   ,-s            Save Dragon formatted keyboard on stack
          tfr   a,b            Take a copy of keycode
-* 6309 - andd #%0100000000000011 replaces next two lines
+       IFNE  H6309
+         andd  #%0100000000000011 replaces next two lines
+       ELSE
          anda  #%01000000     Top row same on both machines
          andb  #%00000011     shift bottom 2 rows up 4 places
+       ENDC
          lslb
          lslb
          lslb
@@ -760,7 +763,7 @@
 
 *6809/6309 note: If we move a call or two before GetStat above, may be able to eliminate a
 *  couple of long branch instructions below
-         
+
 L0439    cmpa  #SS.Joy        joystick?
          beq   SSJOY          branch if so
          cmpa  #SS.ScSiz      screen size?
@@ -1187,7 +1190,7 @@
          bne   L06D1          Yes, skip allocating
          bsr   GetMem         else get graphics memory
 * 6809/6309 - after below E$BMode is change to LBRA, change this to bcs L06B3
-         bcs   L06EF          Couldn't get RAM; return with error
+         bcs   L06B3          Couldn't get RAM; return with error
          std   <V.SBAdd,u     save ptr to graphics RAM
          std   <V.GBuff,u     And again
          inc   <V.Rdy,u       ok, we're ready
@@ -1202,10 +1205,7 @@
          lda   <V.NChar,u     get next char, mode byte (0-1)
          cmpa  #$01           compare against max
          bls   L06F0          branch if valid
-* 6809/6309 - change next 3 lines to LBRA BadMode (dont' care about speed on errors
-         comb
-         ldb   #E$BMode       else invalid mode specified, send error
-L06EF    rts
+         lbra  BadMode        else invalid mode specified, send error
 
 * 6809/6309 tsta redundant if we change beq L0710 to blo L0710 (lower than cmpa#1 above)
 L06F0    tsta                 test user supplied mode byte
@@ -1315,28 +1315,21 @@
 * NOTE: FOLLOWING CODE IS FOR TESTING CONCEPT AND SPEED. IF IT WORKS, WE WILL
 * MAKE IT MORE GENERIC (CURRENTLY HARDCODED FOR 6K SCREENS ONLY) AND MAKE IT
 * A VECTOR THAT CAN BE CALLED FROM VTIO,CO***, AND GRFDRV)
-*EraseGfx clrb                 Color 0 to clear with
-*L07B2    pshs  y,x,u          Save regs
-*L07B2    tfr b,a              Dupe color to D
-*         tfr d,x              Move to X&Y
-*         leay ,x
-*         ldu  <V.SBAdd,u      Get base address for screen
-*         leau >6144,u         Point to end of screen+1
-*         ldd  #$0600          6 blocks of 256 (how many 4 byte chunks to clear)
-*InCLSLp  pshu x,y             4 bytes cleared
-*         decb
-*         bne  InCLSLp         Not done 256*4 (1k) bytes
-*         deca                 Dec 1K blocks ctr
-*         bne  InCLSLp         Do till done
-*         puls x,y,u           Restore regs         
-
 Do13
-EraseGfx clrb                 value to clear screen with
-L07B2    ldx   <V.SBAdd,u
-         leax  >6144+1,x      point to end of gfx mem + 1
-L07B9    stb   ,-x            clear
-         cmpx  <V.SBAdd,u     X = to start?
-         bhi   L07B9          if not, continue
+EraseGfx clrb                 Color 0 to clear with
+L07B2    pshs  y,x,u          Save regs
+         tfr b,a              Dupe color to D
+         tfr d,x              Move to X&Y
+         leay ,x
+         ldu  <V.SBAdd,u      Get base address for screen
+         leau >6144,u         Point to end of screen+1
+         ldd  #$0600          6 blocks of 256 (how many 4 byte chunks to clear)
+InCLSLp  pshu x,y             4 bytes cleared
+         decb
+         bne  InCLSLp         Not done 256*4 (1k) bytes
+         deca                 Dec 1K blocks ctr
+         bne  InCLSLp         Do till done
+         puls x,y,u           Restore regs
 
 * Home Graphics cursor
 Do14     clra