;------------------------------------------------------------------------
;
;		Copyright 2009 Intergraded Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;		Editfile utility for real.dos v1.0) 
;
;------------------------------------------------------------------------
;                         Real.dos file2pc.com
;			This program is to replace
;		      		file2pc.com
;
;       FILE2PC - REV. 1.1
;
; ATTEMPTING REVISION DUE TO BUG
; REPORTS: 2/25/92
; REVISING 3/23/92 DUE TO ERROR
; SENDING FILE: GOING TO ADD A RETRY
; COUNTER AND 3 SECOND DELAY FOR
; RETRIES: REV 1.1
; A PROGROM TO TRANSFER A FILE FROM
; THE ATARI TO THE PC VIA SIO2PC
;
; SIO2PC IS ADDRESSED AS DISK #$A
; WHICH IS SIO BUS DEVID $3A
;
;
;	Code History.     Init design,   11/09/2005   sjc
;			 cleaned upcode formatting so it is easery to follow  5/21/2009
;
;Updated the screen Print Utility so it will work with the exp-80
; This change will also help "Print P:"		8/17/2012	sjc
;
;
;Also added code for Control+1 pause of screen to the high speed screen
;handling.		8/24/2012	sjc
; 
;
;
;
;
;
;------------------------------------------------------------------------
; Notes: o This source code MAY NOT be placed for download on any BBS's!
;        o "mea" is a macro that loads the address of the first field
;          into the pointer specified by the second field.
;------------------------------------------------------------------------
; Assembler:  MADMAC (tm) ST Cross Assembler (Atari Corp)
;             XASM   st and IBM VERSIONS
;------------------------------------------------------------------------

com_file_name:		.macro
;			dc.b   13,"filename.ext",$9b
			dc.b   12,"File2pc.com ",$9b
			.endm

;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; File revision history. Version Number in hex
;
file_ver:		equ   $15
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the Month in dec for the first time this revision 
;was compiled
;
c_month:		equ   8
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the day in dec for the first time this revision 
;was compiled
c_day:			equ   24
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the year in dec for the first time this revision 
;was compiled
c_year:			equ   2012
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Type of compiler used in program
;
; 0 = unknown Compiler
; 1 = xasm
; 2 = mac_65
; 3 = basic
; 4 = compiled basic xl
; 5 = C65
; 6 = Action
;
; We can add more as time goes on!
;
;
;
xasm:			equ	1
mac_65:			equ	2 
basic:			equ	3
basicxl:		equ	4
c65:			equ	5
action:			equ	6

file_compiler:		equ	xasm
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; is this relocatable code ?
; anyother value other than 1 or 2 would be unknown
r..yes:			equ	1
r..no:			equ	2
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define if it can be relocated
l_relocatable:		equ	r..no
;
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define our Header control Byte
;$ff =bypass intro
;$15 = Bypass Dos check
r..crl:			equ	$7d
r..crlf:		equ	$9b
r..space:		equ	$20
r..bypassDOS:		equ	$15
r..bypass:		equ	$ff

l_frstscreenbyte:	equ	r..space
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


			.include	equates
			.include	globals
			.include	macros

			.6502

			.org    $5000
win_start:
header_info:
			.include	header			; this reads info out of the header and displays to screen


INIT:

			LDA 	i.parm_1
			CMP 	#4
			BCS 	GO_ON
;
GO_ON
			LDA 	#$00
			STA 	RC
			JSR 	printsi
			dc.b 	$9b," SIO2PC File transfer to PC Program",$9B,$9b,-1

top:
			ldx 	#disk1
			jsr 	close
			ldx	#disk2
;
			JSR 	cr_lf
			JSR 	cr_lf
			lda     #$01
			cmp     i.in_local
			bne     jump1
       			JSR 	KEYCK				; ESCAPE ABORT?
			BCC 	KOK1
			JMP 	QUIT
KOK1:
			jsr 	printsi
			dc.b 	$9b,$9b,"Please enter the Atari filespec",$9b
			dc.b 	"or press ESC to quit.",$9b
			dc.b 	" >",-1

; WAIT FOR KEY OTHER THAN ESCAPE..
;
ESKY:			LDA 	CH
			CMP 	#$1C			; KYBD CODE FOR ESC
			BNE 	NT91
			JMP 	QUIT
NT91:			CMP 	#$FF
			BEQ 	ESKY
;
			mea     i.parm_1,in_ptr
			lda     #64
			ldx     #0
			ldy     #1
			jsr     get_string

jump1:
			mea     i.parm_1,s_ptr
			mea     fspc,d_ptr
			jsr     toascii
;
			JSR 	cr_lf

; NOW, OPEN THE ATARI FILE TO READ:

			ldx     #disk2
			jsr     close
			LDA 	FSPC+1
			CMP 	#':'
			BEQ 	YCOLON
			LDA 	FSPC+2
			CMP 	#':'
			BEQ 	YCOLON

			mea     prefs,fn_ptr
			ldx     #disk2
			jsr     open_in
			JMP 	TESTOP
YCOLON:
			mea     fspc,fn_ptr
			ldx     #disk2
			jsr     open_in


TESTOP:
			BPL 	OKOP
			ldx     #disk1
			jsr     close
			ldx     #disk2
			jsr     close
			jsr 	printsi
			dc.b 	$9b," ERROR in opening the Atari file",$9b,-1
			lda     #$01
			cmp     i.in_local
			beq     .a
			JMP 	quit
.a:			jmp 	top

OKOP:
			LDA 	CH
			CMP 	#$1C			; KYBD CODE FOR ESCAPE
			BNE 	KKOK2
			JMP 	top

; GET PC PATHNAME FROM USER:

KKOK2:
			jsr 	printsi
			dc.b 	$9b,$9b,"Please enter the PC pathname for file",$9B
			dc.b    " >",-1
			mea     i.parm_2,in_ptr
			lda     #80
			ldx     #0
			ldy     #1
			jsr     get_string
kkok2a:
			jsr     convert_it

			jsr     cr_lf

			mea     i.parm_2,pr_ptr
			jsr     echos

			jsr     cr_lf

			mea     i.parm_2,s_ptr
			mea     buffer,d_ptr
			jsr     toascii
;

; FIRST, GET PC'S STATUS

GPCS:
			JSR 	GETSTAT
			BEQ 	PCSOK
			jsr     printsi
			dc.b 	$9b," BAD STATUS from PC. Restarting...",$9b,-1
.a:			jmp top

; SEND IT TO THE PC ...

PCSOK:			LDA 	#'W'
			STA 	DCOMND
			LDA 	#1
			STA 	DAUX1
			LDA 	#0
			STA 	DAUX2
			JSR 	GOSIO

; NOW, WAIT 6 SECONDS FOR PC TO CREATE
; OR OPEN THE FILE...
; REV. 1.0 CORRECTED LDA BELOW TO LDX

			jsr 	printsi
			dc.b 	$9b," STANDBY 6 sec. - PC CREATING FILE",$9B,-1
			LDX 	#255			; 240 JIFFIES=4s
			JSR 	JIFFY
			LDX 	#127
			JSR 	JIFFY
	

			JSR 	GETSTAT
			BEQ     .b
			jsr 	printsi
			dc.b 	" BAD STATUS from PC. Restarting...",$9b,-1
.a:			jmp 	top

; NOW START PUTTING THE ATARI FILE TO
; THE PC:
.b:
			jsr     printsi
			dc.b    $9b," Copying file to PC ....",-1


PCOK:
			LDX 	#disk2
			LDA 	#7			; CMND FOR GET BYTES
			STA 	ICCOM,X
			LDA 	#0
			STA 	ICBLH,X
			LDA 	#$80			; 128 BYTES
			STA 	ICBLL,X
			LDA 	#LOW BUFFER
			STA 	ICBAL,X
			LDA 	#HIGH BUFFER
			STA 	ICBAH,X
			JSR 	CIOV

			BMI 	SHOP
			CPY 	#03			; Impending EOF: OS PG. 79
			BEQ 	LAST1			; REV 1.1
			JMP 	GMORE
SHOP:			CPY 	#$88			; MEANS END OF FILE
			BEQ 	LAST1
			DEC 	RETRY			; RETRY COUNTER - REV 1.1
			BMI 	NO_TRY			; IF NOT NEG, RETRY
			jsr     printsi
			dc.b 	$9b,"ERROR: 3 SEC DELAY AND RETRY",$9B,-1

			LDX 	#180			; 3 SECOND DELAY
			JSR 	JIFFY
			JMP 	PCOK
NO_TRY:			LDA 	TRIES			; RESET RETRY COUNTER
			STA 	RETRY
			JSR 	printsi
			dc.b 	$9b,"  ERROR in reading Atari file",$9b,-1
			JSR 	SAYBAD
			lda     #$01
			cmp     i.in_local
			beq     .a
			JMP 	quit
.a:			jmp 	top

LAST1:			JSR 	GETSTAT
			BEQ 	LUST1
			JSR 	bsa
			jmp 	top

LUST1:			LDA 	#'W'
			STA 	DCOMND
			LDA 	#1			; MEANS PARTIAL RECORD
			STA 	DAUX1			; AND DAUX2 HAS # OF
			LDX 	#$20			; REV. 1.0 CHANGE
			LDA 	ICBLL,X			; BYTES
			STA 	DAUX2
			JSR 	GOSIO


; ************** HANDLE ERRORS*******
			jsr     printsi
			dc.b    156,"          File Transfer Complete!     ",$9B,-1
			jmp 	top
;
GMORE:			JSR 	GETSTAT
			BEQ 	GMIRE
			jsr     bsa
.a:
			jsr     printsi
			dc.b    " Escape key exits this program",$9b,-1
			JMP 	top

GMIRE:			LDA 	#'W'
			STA 	DCOMND
			LDA 	#0			; MEANS FULL RECORD
			STA 	DAUX1
			JSR 	GOSIO

;*********** HANDLE ERRORS *********

			JMP	PCOK			; GET NEXT RECORD


QUIT:
			ldx     #disk1
			jsr     close
			ldx     #disk2
			jsr     close
			RTS				; BACK TO DOS



;

KSPEC:			dc.b 	"K:",$9B
RETRY:			dc.b 	1
TRIES:			dc.b 	1			; USED TO RESET ABOVE COUNTER
i.in_local:		dc.b	1

i.parm_1:			ds.b	255              	;passed by DOSshell
i.parm_2:			ds.b    255             	;passed by DOSshell
i.in_str:			ds.b    255              	;
i.instr:			ds.b    80              	;

; PLACE FOR RECORD:

BUFFER:			ds.b 	128

;        ASSERT LOW BUFFER <> $FF
;        ASSERT *-1 <> $FF

PREFS:			dc.b 	"D:"			; DEFAULT DRIVE


FSPC:			ds.b 	255			; FOR ATARI FILESPEC

;       SUBROUTINES
;
bsa:
			jsr     printsi
			dc.b 	"BAD STATUS from PC. Restarting...",$9b,-1
			rts


; SUBROUTINE TO SEE IF KEY HAS BEEN
; PRESSED AND RETURN WITH CARRY SET
; IF THE KEY IS 'ESCAPE' KEYBOARD MUST
; BE OPEN VIA IOCB #1

KEYCK:			LDA 	CH
			CMP 	#$FF
			BEQ 	KOKY
;			ldx 	#disk1
			JSR 	get_key
			CMP 	#27			; ESCAPE?
			BNE 	KOKY
			SEC
			RTS
KOKY:			CLC
			RTS


; THIS SUB DELAYS # OF JIFFIES IN X REG

JIFFY:			PHA
.JIF1:			LDA 	RTCLOK+2		; INC'S EVERY 1/60
.CHECK:			CMP 	RTCLOK+2
			BEQ 	.CHECK
			DEX
			BNE 	.JIF1
			PLA
			RTS



; SUBROUTINE TO SEND COMMAND TO PC
; USER SET COMMAND 'W' OR 'R' TO DCOMND
; AND SECTOR # TO DAUX1 & DAUX2.

; SEND RECORD TO SIO2PC:

GOSIO:
			LDA 	#$A			; DISK $#A IS FILE XFR
			STA 	$301			; FUNCTION.
			LDA 	#$80
			STA 	$303
			LDA 	#LOW BUFFER
			STA 	$304
			LDA 	#HIGH BUFFER
			STA 	$305
			JSR 	$E453			; DISKINV
			RTS

; GET STATUS FROM SIO2PC:
; RETURNS "NOT EQUAL" IF ERROR
; NOTE: IT IS ASSUMED THAT ALL VERSIONS
; OF OS PUT STATUS AT 02EA

GETSTAT:
			LDA 	#0
			STA 	$02EA			; 1.1 - INITIALIZE SATAUS
			LDA 	#'S'
			STA 	DCOMND
			LDA 	#$A			; DISK #$A
			STA 	$301
			LDA 	#$40			; MEANS "READ" DIRECTION
			STA 	$303
			LDA 	#4			; LENGTH OF STATUS FRAME
			STA 	$308
			LDA 	#0
			STA 	$309
			JSR 	$E453
			LDA 	$2EA			; GET FIRST STATUS BYTE
			CMP 	#$10			; $10 MEANS GOOD
			bne     saybad  		; added code
			RTS				; NOT EQUAL MEANS ERROR


; A SUBROUTINE TO TELL THE PC THAT A
; FATAL ERROR HAS OCCURRED AND IT
; SHOULD ABORT THE PROCESS. DAUX1 = 2
; OF A SECTOR WRITE TELLS THE PC IT'S
; ALL OVER. SECTOR DATA IS IRRELEVANT

SAYBAD:
	
			LDA 	#2
			STA 	DAUX1
			LDA 	#0
			STA 	DAUX2
			LDA 	#'W'
			STA 	DCOMND
			JSR 	GOSIO
			RTS


convert_it:
			lda     i.in_local
			cmp     #$01
			beq     .c
			jsr     pull_it
.c:
			ldy     i.parm_2
.b:
			lda     i.parm_2,y
			cmp     #'>'
			bne     .a
			lda     #'\'
			sta     i.parm_2,y
.a:
			dey
			cpy     #2
			bcs     .b
			rts
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; dx:
pull_it:
			ldx 	#$01
			ldy 	#$01
			lda     i.parm_2+3
			cmp     #':'
			beq     .a
			ldy 	#$04
			ldx 	#$01
.a:			lda     i.parm_2,y
			sta     i.parm_2,x
			cpy     i.parm_2
			beq     .b
			iny
			inx
			jmp 	.a
.b:
			stx     i.parm_2
			rts



; 
;--------------------------------------------
;printsi Routeen macro             
;this is a slow print to the screen
;usage jsr	printsi
;    .byte $9b," string to be printed",$ff
;--------------------------------------------
;
printsi:
			pla
			sta	.pstr+1
			pla
			sta	.pstr+2
.prsl:			inc	.pstr+1
			bne	.pstr
			inc	.pstr+2
.pstr:			lda	$ffff
			cmp	#$ff
			beq	.estri
			jsr	fast_output
			jmp	.prsl
.estri:			lda	.pstr+2
			pha
			lda	.pstr+1
			pha
			rts
;
;	
;
;-------------------------	
;actual screen handler    	
;screen offset definitions	
;-------------------------	
scr_offset:		dc.w	0
			dc.w	40
			dc.w	80
			dc.w	120
			dc.w	160
			dc.w	200
			dc.w	240
			dc.w	280
			dc.w	320
			dc.w	360
			dc.w	400
			dc.w	440
			dc.w	480
			dc.w	520
			dc.w	560
			dc.w	600
			dc.w	640
			dc.w	680
			dc.w	720
			dc.w	760
			dc.w	800
			dc.w	840
			dc.w	880
			dc.w	920
;
;echo:
fast_output:
			sta	$5a
			lda	sc.redirect
			cmp	#print_p
			beq	.go_cio
.leo:
			lda 	$02ff   	; CONTROL 1 FLAG
			cmp 	#$00
			bne 	.leo

			lda	$5a
			cmp	#32
			bcc	.do_look
			cmp	#125
			bcc	.do_here
.do_look:		ldy	#0
.lookup:		lda	.char_table,y
			beq	.do_here
			cmp	$5a
			beq	.go_cio
			iny
			bne	.lookup


.go_cio:		lda	$5a
			jmp	putlocal


.do_here:		ldy	#0
			lda	$5d
			sta	($5e),y
			lda	$5a
			cmp	#$9b
			beq	.docr
.notcr:			jsr	.get_adr
			lda	$5a
			and	#$7f
			cmp	#32
			bcc	.add64
			cmp	#96
			bcc	.sub32
			jmp	.asis
.add64:			clc
			adc	#64
			jmp	.asis
.sub32:			sec
			sbc	#32
.asis:			bit	$5a
			bpl	.xxlate
			ora	#$80
.xxlate:		ldy	#0
			sta	($5e),y
			inc	$55
			inc	$63
			lda	$55
			cmp	#40
			bcs	.next_row
			iny
			lda	($5e),y
			sta	$5d
			ora	#$80
			ldx	752
			bne	.nocurs1
			sta	($5e),y
.nocurs1:		inc	$5e
			bne	.nooav
			inc	$5e+1
.nooav:			rts
.docr:			lda	#0
			sta	$63
.next_row:		lda	$52
			sta	$55
			inc	$54
			ldy	$54
			cpy	#24
			bcs	.scroll
			jsr	.get_adr
			ldy	#0
			lda	($5e),y
			sta	$5d
			ldx	752
			bne	.nocurs2
			ora	#$80
			sta	($5e),y
.nocurs2:		rts

.scroll:		dec	$54
			lda	$58
			sta	$68
			clc
			adc	#40
			sta	$5e
			lda	$58+1
			sta	$68+1
			adc	#0
			sta	$5e+1
			ldy	#0
			ldx	#3
.scloop:		lda	($5e),y
			sta	($68),y
			iny
			bne	.scloop
			inc	$5e+1
			inc	$68+1
			dex
			bne	.scloop
.scloop2:		lda	($5e),y
			sta	($68),y
			iny
			cpy	#152
			bcc	.scloop2
			lda	$58
			clc
			adc	#920&$ff
			sta	$5e
			lda	$58+1
			adc	#920/256
			sta	$5e+1
			ldy	#0
			tya
.clear:			sta	($5e),y
			iny
			cpy	#40
			bcc	.clear
			ldx	752
			bne	.nocurs3
			lda	#$80
			ldy	$55
			sta	($5e),y
.nocurs3:		lda	#0
			sta	$5d
			lda	$5e
			clc
			adc	$55
			sta	$5e
			lda	$5e+1
			adc	#0
			sta	$5e+1
			rts
.get_adr:		lda	$54
			asl
			tay
			lda	$58
			clc
			adc	scr_offset,y
			sta	$5e
			lda	$58+1
			adc	scr_offset+1,y
			sta	$5e+1
			lda	$5e
			clc
			adc	$55
			sta	$5e
			lda	$5e+1
			adc	#0
			sta	$5e+1
			rts
.char_table:
			dc.b	27,28,29,30,31,125,126,127
			dc.b	156,157,158,159,253,254,255,0


putlocal:		ldx	#11
			stx	$0342
			ldx	#0
			stx	$0348
			stx	$0349
			jmp	$e456
;


;------------------------------	
;   GET KEY  ROUTEEN           	
;this routeen get's one key and	
;return it to the accumlator   	
;------------------------------	
;
get_key:		stx 	$b6
			sty 	$b7
			jsr 	g_fff
			ldx 	$b6
			ldy 	$b7
			rts
g_fff:			lda 	$e420+5
			pha
			lda 	$e420+4
			pha
			rts
;



cr_lf:			jsr 	printsi
			dc.b 	$9b,-1
			rts

;  prints: prints string with address in PR_PTR, length byte in 0th byte
;  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

prints:			ldy     #$80
			bne     pri2
echos:			ldy     #0

pri2:			sty     pr_mode
			ldy     #0
			lda     (pr_ptr),y		;get length to print
			beq     .xpri			;exit, null string
			sta     .maxlth
			iny
			sty     .tempy

.pril:			ldy     .tempy
			lda     (pr_ptr),y
			bit     pr_mode
			bmi     .prloc
			jsr     fast_output
			jmp     .prnext
.prloc:			jsr     fast_output
.prnext:		inc     .tempy
			dec     .maxlth
			bne     .pril
.xpri:			rts

.tempy:			dc.b    1
.maxlth:		dc.b    1

pr_mode:		dc.b	0



open_in:		lda 	#4
			jmp 	do_open

open_out:		lda 	#8
			jmp 	do_open

open_app:		lda 	#9
			jmp 	do_open

open_updt:		lda 	#12
			jmp 	do_open

open_dir:		lda 	#6
			jmp 	do_open

open_raw:		lda 	#20			;open raw directory
			jmp 	do_open

do_open
			pha 		        	;open filespec pointed to by fn_ptr
			txa				;
			pha
			jsr 	close			;make sure device is closed
			pla
			tax
			pla
			sta 	icaux1,x
			lda 	#ccopen
			sta 	iccom,x
			lda	fn_ptr
			sta 	icbal,x
			lda 	fn_ptr+1
			sta	icbah,x
			lda	#80
			sta	icbll,x
			lda	#0
			sta 	icblh,x
			lda 	#128			;for long directory (shouldn't hurt??)
			sta	icaux2,x
			jmp 	ciov

close:
			LDA	#$0C
			STA	ICCOM,X
			JSR	CIOV
			rts


get_string:		cmp     #83                     ;max < 83?
			bcc     .gsentry		;yes
			lda     #80
.gsentry:		sta     .maxlth                 ;save max length
			stx     .mask
			sty     .return

			lda     #0
			sta     attract


			lda     in_ptr
			sta     .address		;hold address of string
			lda     in_ptr+1		; to input into
			sta     .address+1
			lda     #0
			sta     .i.instr			;zero length byte

.gsloop:		jsr     get_key			;get a keypress
;			cmp     #'\'			;can't allow this key!
;			beq     .gsloop			;yes, ignore
			cmp     #$9b			;carriage return?
			bne     .nogs			;nope
			jmp     .gscr			;yes
.nogs:			cmp     #14			;treat ^N as a C/R
			bne     .not14
			jmp     .gscr
.not14:			cmp     #126			;backspace?
			beq     .bspace
			cmp     #30
			beq     .bspace
			cmp     #24                     ;^X?
			bne     .nowipe
			jmp     .wipeout
.nowipe:		and     #$7f                    ;strip inverse bit
			sta     .key                    ;hold it

;       now, see if it falls within requested mask
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


			ldx     .mask                   ;get mask
			beq     .alphanum
			cpx     #1
			beq     .numeric

.alphanum:		cmp     #32  			; < space?
			bcc     .gsloop			;yes, ignore
			cmp     #'z'+1			; > 'z'
			bcs     .gsloop                 ;yes, ignore
			jmp     .gsstore

.numeric:		cmp     #'0'			; < '0'?
			bcc     .gsloop                 ;yes, ignore
			cmp     #'9'+1                  ; > '9'
			bcs     .gsloop                 ;yes, ignore
			jmp     .gsstore

.gsstore:		lda     .i.instr                  ;get length byte
			cmp     .maxlth
			beq     .gsloop                 ;ignore it..at
			bcs     .gsloop                 ; max length now
			tay
			iny
			lda     .key
			cmp     #32 			;is it a space?
			bne     .notspc                 ;nope
			cpy     #1  			;1st char in line?
			bne     .notspc
			jmp     .gsloop 		;yes..don't allow it
.notspc:		sta     .i.instr,y		;save key into string
			inc     .i.instr  		;increase length
			bit     hide			;hide input?
			bpl     .nohide
			lda     #'*'
			bne     .showit
.nohide:		lda     .key
.showit:		jsr     fast_output
			lda     .maxlth 		;one-key input?
			cmp     #1
			bne     .nohot  		;nope
			lda     .i.instr
			cmp     #1  			;1 byte entered?
			bne     .nohot
			lda     u_flags
			and     #f_hotkey               ;user want hotkeys?
			beq     .nohot  		;nope..
			jmp     .gscr   		;yes - auto carriage return!
.nohot:			jmp     .gsloop 		;keep cookin..

.bspace:		lda     .i.instr  		;can't backspace
			cmp     #0  			; if no length!
			bne     .nt0
			jmp     .gsloop
.nt0:			dec     .i.instr  		;-1 to length byte
			lda     #126			;output a BSpace
			jsr     fast_output
			jmp     .gsloop

.wipeout:		lda     .i.instr  		;can't backspace if no length
			bne     .wt0
			jmp     .gsloop
.wt0:			dec     .i.instr  		;-1 to length byte
			lda     #126			;output a BSpace
			jsr     fast_output
			jmp     .wipeout		;go until line cleared

.gscr:			lda     .i.instr  		;get length
			bne     .nonull 		;its not null
			ldx     .return 		;allow return only?
			bne     .nonull 		;yes..exit
			jmp     .gsloop 		;nope..ignore it

;       done, so move our string into where they want it.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.nonull:		lda     s_ptr
			sta     .holds
			lda     s_ptr+1
			sta     .holds+1
			lda     d_ptr
			sta     .holdd
			lda     d_ptr+1
			sta     .holdd+1
			mea     .i.instr,s_ptr
			lda     .address
			sta     d_ptr
			lda     .address+1
			sta     d_ptr+1
			jsr     move_str		;move to their area..
			lda     .holds
			sta     s_ptr
			lda     .holds+1
			sta     s_ptr+1
			lda     .holdd
			sta     d_ptr
			lda     .holdd+1
			sta     d_ptr+1
			lda     #0
			sta     hide
			lda     .i.instr  		;return with lth in A
			rts


.address:		ds.b    2
.holds:			ds.b    2
.holdd:			ds.b    2
.maxlth:		ds.b    1
.mask:			ds.b    1
.return:		ds.b    1
.key:			ds.b    1
.i.instr:			ds.b    120



;     Make an ASCII ($9b terminated) string out of a lth-byte string
;     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

toascii:		ldy     #0
			lda     (s_ptr),y               ;get lth byte
			sta     .lth
			beq     .null
			iny
			inc     .lth
.loop:			lda     (s_ptr),y
			dey
			sta     (d_ptr),y
			iny
			iny
			cpy     .lth
			bne     .loop
.done:			ldy     .lth
			dey
.rtn:			lda     #$9b
			sta     (d_ptr),y
			rts
.null:			ldy     #0
			beq     .rtn

.lth:			dc.b    1

;       Move string pointed to by s_ptr to string pointed to by d_ptr
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


move_str:		ldy     #0
			lda     (s_ptr),y               ;get lth byte
			tay			        ;go backwards
.loop:			lda     (s_ptr),y
			sta     (d_ptr),y
			cpy     #0
			beq     .done
			dey
			jmp     .loop
.done:			rts



hide:			ds.b    1			;PRIVATE  1 means echo '*' on input keys
u_flags:		ds.b    2			; 16 individual flags
rc:			ds.b    1			;return code from scheduled event


win_end:		ds.b	0
;
;







