From 46f1085e2d4ae2ee16271567d06c6206b1cafd91 Mon Sep 17 00:00:00 2001
From: "ol.sc" 
Date: Tue, 23 Oct 2012 19:42:57 +0000
Subject: [PATCH] Fixed several aspects of the GEOS CONIO implementation: -
 cputc was drawing at the wrong position, therefore one line had to be removed
 as a workaround. - chline, cvline were drawing one pixel to large lines. -
 cclear was drawing an in both directions one pixel to big rect. - the cursor
 was drawn at wrong times at wrong places in a wrong size.
git-svn-id: svn://svn.cc65.org/cc65/trunk@5874 b7a2c559-68d2-44c3-8de9-860c34a00d81
---
 libsrc/geos-common/conio/Makefile   |  1 -
 libsrc/geos-common/conio/_scrsize.s | 12 +++++-------
 libsrc/geos-common/conio/cclear.s   |  9 ++++++++-
 libsrc/geos-common/conio/cgetc.s    | 20 ++++++++++++++------
 libsrc/geos-common/conio/chline.s   | 13 +++++++++++--
 libsrc/geos-common/conio/clrscr.s   |  3 ---
 libsrc/geos-common/conio/cputc.s    | 27 +++++++++------------------
 libsrc/geos-common/conio/cursor.s   | 28 ----------------------------
 libsrc/geos-common/conio/cvline.s   | 10 ++++++----
 libsrc/geos-common/conio/gotoxy.s   | 12 +++++-------
 libsrc/geos-common/system/extzp.s   |  4 +---
 11 files changed, 59 insertions(+), 80 deletions(-)
 delete mode 100644 libsrc/geos-common/conio/cursor.s
diff --git a/libsrc/geos-common/conio/Makefile b/libsrc/geos-common/conio/Makefile
index 228364759..7f53de176 100644
--- a/libsrc/geos-common/conio/Makefile
+++ b/libsrc/geos-common/conio/Makefile
@@ -11,7 +11,6 @@ S_OBJS +=	_scrsize.o	\
 		chline.o	\
 		clrscr.o	\
 		cputc.o		\
-		cursor.o	\
 		cvline.o	\
 		dummies.o	\
 		gotoxy.o	\
diff --git a/libsrc/geos-common/conio/_scrsize.s b/libsrc/geos-common/conio/_scrsize.s
index da6b2d33d..31d64e1f4 100644
--- a/libsrc/geos-common/conio/_scrsize.s
+++ b/libsrc/geos-common/conio/_scrsize.s
@@ -24,18 +24,16 @@ initscrsize:
 	.byte $2c
 L1:	lda #40			; 40 columns (more or less)
 	sta xsize
-	lda #24			; something like that for Y size
+	lda #25			; something like that for Y size
 .else
 	lda #70			; 70 columns (more or less)
 	sta xsize
-	lda #23			; something like that for Y size
+	lda #24			; something like that for Y size
 .endif
 	sta ysize
-	ldx #1
-	stx cursor_r
-	dex 
-	stx cursor_c
-	txa 
+	lda #0
+	sta cursor_c
+	sta cursor_r
 	jmp _cursor		; home and update cursor
 
 .code
diff --git a/libsrc/geos-common/conio/cclear.s b/libsrc/geos-common/conio/cclear.s
index 7b5710dd6..ac93803d9 100644
--- a/libsrc/geos-common/conio/cclear.s
+++ b/libsrc/geos-common/conio/cclear.s
@@ -30,7 +30,7 @@ _cclear:
 	lda cursor_y		; level
 	sta r2L
 	clc
-	adc #8
+	adc #7
 	sta r2H
 	txa			; right end
 	clc
@@ -40,6 +40,13 @@ _cclear:
 	ldx #r4
 	ldy #3
 	jsr DShiftLeft
+	clc			; one pixel less
+	lda r4L
+	sbc #0
+	sta r4L
+	lda r4L+1
+	sbc #0
+	sta r4L+1
 	lda curPattern		; store current pattern
 	pha
 	lda #0			; set pattern to clear
diff --git a/libsrc/geos-common/conio/cgetc.s b/libsrc/geos-common/conio/cgetc.s
index b24ca54e7..94fd55ef0 100644
--- a/libsrc/geos-common/conio/cgetc.s
+++ b/libsrc/geos-common/conio/cgetc.s
@@ -7,25 +7,25 @@
 ; unsigned char cgetc (void);
 
 	    .export _cgetc
-	    .import update_cursor
-	    .importzp cursor_x, cursor_y, cursor_flag
+	    .import cursor
+	    .importzp cursor_x, cursor_y
 
 	    .include "jumptab.inc"
 	    .include "geossym.inc"
 
 _cgetc:
 ; show cursor if needed
-	lda cursor_flag
+	lda cursor
 	beq L0
 
-	jsr update_cursor
+; prepare cursor
+	lda #7
+	jsr InitTextPrompt
 	lda cursor_x
 	ldx cursor_x+1
 	sta stringX
 	stx stringX+1
 	lda cursor_y
-	sec
-	sbc curHeight
 	sta stringY
 	jsr PromptOn
 
@@ -33,7 +33,15 @@ L0:	jsr GetNextChar
 	tax
 	beq L0
 	pha
+
+; from 'The Hitchhiker's Guide To GEOS'
+	php
+	sei
 	jsr PromptOff
+	lda #0
+	sta alphaFlag
+	plp
+
 	pla
 	ldx #0
 	rts
diff --git a/libsrc/geos-common/conio/chline.s b/libsrc/geos-common/conio/chline.s
index d4b4c20a9..c6310a11e 100644
--- a/libsrc/geos-common/conio/chline.s
+++ b/libsrc/geos-common/conio/chline.s
@@ -28,17 +28,26 @@ _chline:
 	lda cursor_x+1
 	sta r3L+1
 	lda cursor_y		; level
-	sec
-	sbc #4			; in the middle of a cell
+	clc
+	adc #4			; in the middle of a cell
 	sta r11L
 	txa			; right end
 	clc
 	adc cursor_c
 	sta cursor_c
 	sta r4L
+	lda #0
+	sta r4L+1
 	ldx #r4
 	ldy #3
 	jsr DShiftLeft
+	clc			; one pixel less
+	lda r4L
+	sbc #0
+	sta r4L
+	lda r4L+1
+	sbc #0
+	sta r4L+1
 	lda #%11111111		; pattern
 	jsr HorizontalLine
 	jsr fixcursor
diff --git a/libsrc/geos-common/conio/clrscr.s b/libsrc/geos-common/conio/clrscr.s
index e8f30b700..72fcb40ff 100644
--- a/libsrc/geos-common/conio/clrscr.s
+++ b/libsrc/geos-common/conio/clrscr.s
@@ -14,8 +14,6 @@
 	    .include "const.inc"
 
 _clrscr:
-	lda #ST_WR_FORE | ST_WR_BACK
-	sta dispBufferOn
 	lda curPattern		; save current pattern
 	pha
 	lda #0			; set pattern to clear
@@ -25,7 +23,6 @@ _clrscr:
 	stx r3H
 	stx r2L
 	stx cursor_c
-	inx
 	stx cursor_r
 	jsr fixcursor		; home cursor
 .ifdef __GEOS_CBM__
diff --git a/libsrc/geos-common/conio/cputc.s b/libsrc/geos-common/conio/cputc.s
index f0bbb766d..dbc4f45c1 100644
--- a/libsrc/geos-common/conio/cputc.s
+++ b/libsrc/geos-common/conio/cputc.s
@@ -22,7 +22,7 @@
 ; HOME = KEY_ENTER, KEY_HOME = REV_ON, 
 ; UPLINE = ?, KEY_UPARROW = GOTOY, ...
 
-	    .export _cputcxy, _cputc, update_cursor
+	    .export _cputcxy, _cputc
 	    .import _gotoxy, fixcursor
 	    .import popa
 	    .import xsize,ysize
@@ -63,38 +63,29 @@ L2:	php
 	lda cursor_x+1
 	sta r11H
 	lda cursor_y
+	clc
+	adc #6			; 6 pixels down to the baseline
 	sta r1H
 	txa
 	jsr PutChar
 	plp
-	bcs update_cursor
+	bcs fix_cursor
 
 	inc cursor_c
 	lda cursor_c
 	cmp xsize		; hit right margin?
-	bne update_cursor
+	bne fix_cursor
 	lda #0			; yes - do cr+lf
 	sta cursor_c
 do_lf:	inc cursor_r
 	lda cursor_r
 	cmp ysize		; hit bottom margin?
-	bne update_cursor
+	bne fix_cursor
 	dec cursor_r		; yes - stay in the last line
 
-update_cursor:
-	jsr fixcursor
-	lda cursor_x
-	sta r4L
-	lda cursor_x+1
-	sta r4H
-	lda cursor_y
-	sec
-	sbc curHeight
-	sta r5L
-	lda #1			; update cursor prompt position
-	sta r3L
-	jmp PosSprite
+fix_cursor:
+	jmp fixcursor
 
 do_cr:	lda #0
 	sta cursor_c
-	beq update_cursor
+	beq fix_cursor
diff --git a/libsrc/geos-common/conio/cursor.s b/libsrc/geos-common/conio/cursor.s
deleted file mode 100644
index 7ba3c6fff..000000000
--- a/libsrc/geos-common/conio/cursor.s
+++ /dev/null
@@ -1,28 +0,0 @@
-;
-; Maciej 'YTM/Elysium' Witkowiak
-;
-; 27.10.2001, 23.12.2002
-
-; unsigned char cursor (unsigned char onoff);
-
-	    .export _cursor
-	    .import update_cursor
-	    .importzp cursor_flag
-
-	    .include "jumptab.inc"
-	    .include "geossym.inc"
-
-_cursor:
-
-	tay			; onoff into Y
-	ldx #0			; High byte of result
-	lda cursor_flag		; Get old value
-	pha
-	sty cursor_flag		; Set new value
-	tya
-	beq L1
-	lda curHeight		; prepare cursor
-	jsr InitTextPrompt
-	jsr update_cursor	; place it on screen
-L1:	pla
-	rts
diff --git a/libsrc/geos-common/conio/cvline.s b/libsrc/geos-common/conio/cvline.s
index 70475cef5..e60af74a3 100644
--- a/libsrc/geos-common/conio/cvline.s
+++ b/libsrc/geos-common/conio/cvline.s
@@ -25,7 +25,7 @@ _cvline:
 	tax
 	lda cursor_x		; x position
 	clc
-	adc #4			; in the middle of cell
+	adc #3			; in the middle of cell
 	sta r4L
 	lda cursor_x+1
 	adc #0
@@ -36,10 +36,12 @@ _cvline:
 	clc
 	adc cursor_r
 	sta cursor_r
+	asl a
+	asl a
+	asl a
+	clc			; one pixel less
+	sbc #0
 	sta r3H
-	asl r3H
-	asl r3H
-	asl r3H
 	lda #%11111111		; pattern
 	jsr VerticalLine
 	jsr fixcursor
diff --git a/libsrc/geos-common/conio/gotoxy.s b/libsrc/geos-common/conio/gotoxy.s
index 585757473..1464eea27 100644
--- a/libsrc/geos-common/conio/gotoxy.s
+++ b/libsrc/geos-common/conio/gotoxy.s
@@ -20,12 +20,10 @@ _gotox:
 
 _gotoy:
 	sta cursor_r
-	inc cursor_r
 	jmp fixcursor
 
 _gotoxy:
 	sta cursor_r
-	inc cursor_r
 	jsr popa
 	sta cursor_c
 
@@ -35,12 +33,12 @@ fixcursor:
 	sta cursor_x
 	lda #0
 	sta cursor_x+1
-	lda cursor_r
-	sta cursor_y
 	ldx #cursor_x
 	ldy #3
 	jsr DShiftLeft
-	asl cursor_y
-	asl cursor_y
-	asl cursor_y
+	lda cursor_r
+	asl a
+	asl a
+	asl a
+	sta cursor_y
 	rts
diff --git a/libsrc/geos-common/system/extzp.s b/libsrc/geos-common/system/extzp.s
index 77f3e88e6..5ef1c2060 100644
--- a/libsrc/geos-common/system/extzp.s
+++ b/libsrc/geos-common/system/extzp.s
@@ -5,7 +5,7 @@
 ; zeropage locations for exclusive use by the library
 ;
 
-	    .exportzp cursor_x, cursor_y, cursor_flag
+	    .exportzp cursor_x, cursor_y
 	    .exportzp cursor_c, cursor_r
 
 .segment	"EXTZP" : zeropage
@@ -14,8 +14,6 @@ cursor_x:
 	.res 2			; Cursor column (0-319/639)
 cursor_y:
 	.res 1			; Cursor row    (0-199)
-cursor_flag:
-	.res 1			; Cursor on/off (0-off)
 
 cursor_c:
 	.res 1			; Cursor column (0-39/79)
-- 
2.39.5