;------------------------------------------------------------------------
;
;		Copyright 2006 Intergraded Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;		to_ascii utility for real.dos v1.0) 
;------------------------------------------------------------------------
;                         to_ascii.com
;
;
;	Code History.     Init design,   	4/22/2006	sjc
;	Cleaning up labels and formating	6/6/2006	sjc
;	Updating the cli portion of the file	6/14/2006	sjc
;	added the syntaxing ken wanted!		6/29/2006	sjc
;
;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,"To_ascii.com",$9b
			.endm

;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; File revision history. Version Number in hex
;
file_ver:		equ   $13
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; 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    $4000

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

;
.real_dos:

	 ldx #disk1
	 jsr close
	 ldx #disk2
	 jsr close

         LDA comtab
         CLC 
         ADC # low ZCRNAME		;3
         STA .a+1
         LDA comtab+1
         ADC # high ZCRNAME		;0
         STA .a+2
.a       JSR $0000

	 bne got_something		; nothing there

	 JSR printsi
	 dc.b  $9B,$9B
	 dc.b " Syntax for use:",$9b
	 dc.b " to_ascii Dn:>dir>fname.ext",$9b,$9b,-1
	 RTS


got_something:

         LDA comtab
         CLC 
         ADC # low COMFNAM		;3
         STA s_ptr
         LDA comtab+1
         ADC # high COMFNAM		;0
         STA s_ptr+1
	 mea parm_1,d_ptr
	 jsr tostr

;	 jsr cr_lf
;	 mea parm_1,pr_ptr
;	 jsr echos
;	 jsr echos


;
GO_ON
	 LDA #$00
;
		JSR printsi
		 dc.b "     Atascii --> Ascii Converter",$9B,$9B,-1


;
	 LDX #$20
	 JSR CLOSE
	 LDX #$40
	 JSR CLOSE
	 JSR printsi
	 dc.b $9B,$9B," Processing file!  ",-1
;
	 JSR BUILD_NME
;
	 JSR PROCESS
;
	 LDX #$20
	 JSR CLOSE
	 LDX #$40
	 JSR CLOSE
	  MEA  IN+1,FN_PTR
	 LDX #$20
	 JSR ERASE
	 LDX #$20
	 JSR CLOSE
	  MEA  SPACE,S_PTR
	  MEA  OUT,D_PTR
	 JSR CONCAT
	  MEA  FILENAME,S_PTR
	  MEA  OUT,D_PTR
	 JSR CONCAT
	  MEA  OUT+1,FN_PTR
	 LDX #$20
	 JSR RENAME
	 LDX #$20
	 JSR CLOSE
;
;
	 JSR CR_LF
	 RTS
;
;
;
PROCESS
	  MEA  IN+1,FN_PTR
	 LDX #$20
	 JSR OPEN_IN
	 BMI OH_DAMN
	  MEA  OUT+1,FN_PTR
	 LDX #$40
	 JSR OPEN_OUT
	 BMI OH_DAMN
	 JSR READ
	 RTS
;
OH_DAMN
	 STY RC
	 JSR printsi
	 dc.b $9B,$9B," Error Opening file",$9B,$9B,-1
	 LDA #$01
	 STA ABORTED
	 RTS
;
;
READ
	 LDX #$20
	  MEA  TEMP1,IN_PTR
	 LDX #$20
	 JSR INPUTSD
;
	 BMI .ER

	 jsr    paused

	 JSR SPLICE
	 JSR WRITE
;
	 JMP READ
;
.ER
	 JSR printsi
	 dc.b $9B,$9B," all Done",$9B,$FF
;
	 JSR SPLICE
	 JSR WRITE
	 RTS
;
SPLICE
	 LDY TEMP1
	 CPY #$00
	 BEQ .SPL
	 LDA TEMP1,Y
	 CMP #$20
	 BNE .SPL
	 LDY TEMP1
	 DEY
	 STY TEMP1
	 JMP SPLICE
.SPL
	  MEA  TEMP1,D_PTR
	  MEA  LF,S_PTR
	 JSR CONCAT
	 RTS
;
WRITE
	  MEA  TEMP1+1,PR_PTR
	 LDX #$40
	 LDA TEMP1
	 STA LNPTR
	 LDA #$00
	 STA LNPTR+1
	 JSR BPUT
;   JSR PRINTSD
	 RTS
;
;
BUILD_NME
	  MEA  PARM_1,S_PTR
	  MEA  IN,D_PTR
	 JSR MOVE_STR
	  MEA  PARM_1,S_PTR
	  MEA  DIR_MASK,D_PTR
	 JSR MOVE_STR
	 JSR STRIP_PATH
;
	  MEA  PARM_1,S_PTR
	  MEA  OUT,D_PTR
	 JSR MOVE_STR
;
	  MEA  OUT,S_PTR
	  MEA  DOT,D_PTR
	 JSR SUBSTR
	 CMP #$00
	 BEQ .BN
	 TAY
	 DEY
	 STY OUT
.BN
	  MEA  EXT,S_PTR
	  MEA  OUT,D_PTR
	 JSR CONCAT
;
;   JSR CR_LF
;    MEA  IN,PR_PTR
;   JSR ECHOS
;
;   JSR CR_LF
;    MEA  OUT,PR_PTR
;   JSR ECHOS
;
;         JSR CR_LF
	 RTS
;
; dc.b 0,"dx:filename"
STRIP_PATH
	 LDY DIR_MASK
PATH_W
	 LDA DIR_MASK,Y
	 CMP #':'
	 BEQ W_D
	 CMP #'>'
	 BEQ W_D
	 CMP #'<'
	 BEQ W_D
	 CPY #$00
	 BEQ W_D
	 CPY #80
	 BCS W_D
	 DEY
	 JMP PATH_W
;
W_D
	 STY DIR_MASK
W_N
;
;
	 LDX #$00
	 LDY DIR_MASK
W_ZZ
	 STX FILENAME
	 LDA DIR_MASK,Y
	 STA FILENAME,X
	 CPY IN
	 BCS ALLW
	 INY
	 INX
	 JMP W_ZZ
ALLW
;   JSR CR_LF
;    MEA  DIR_MASK,PR_PTR
;   JSR PRINTS
;   JSR CR_LF
;    MEA  FILENAME,PR_PTR
;   JSR PRINTS
;   JSR CR_LF
	 RTS
;
paused:
		jsr    printsi
		dc.b   126 ;' '
		dc.b  -1
		ldy    .hold
		iny
		sty    .hold
		cpy  #$04
		bcc  .tammy
		ldy  #$00
		sty .hold
.tammy:
		rts

.hold:         dc.b 0
.tab1:         dc.b $7c,$06,$12,$07,"*"
.tab:          dc.b "|/-\*"


AS_IS           equ     0
parm_1:		ds.b	40              ;passed by DOSshell
parm_2:         ds.b    40              ;passed by DOSshell
in_str:         ds.b    80              ;
instr:          ds.b    80              ;
;
aborted:	dc.b	0

rc:             ds.b    1               ;return code from scheduled event

u_vw:           dc.b    80               ;video width
u_flags:        ds.b    2               ; 16 individual flags

hide:           ds.b    1       ;PRIVATE  1 means echo '*' on input keys
trans:		dc.b 	0
i_bufoff:         ds.b    1               ;current position in input line
word:           ds.b    30              ;damn backward compatibility!   
u_handle:       ds.b    21              ;handle
u_name:         ds.b    21              ;real name
cur_date:       ds.b    15              ; Mon 13-May-87 + len_byte + $9B
cur_time:       ds.b    15              ; 11:00:00am    + len_byte + $9B
cur_month:      ds.b    1               ;1-12
cur_day:        ds.b    1               ;1-31
cur_year:       ds.b    1               ;1-99
cur_hour:       ds.b    1               ;0-23
cur_minute:     ds.b    1               ;0-59
cur_second:     ds.b    1               ;0-59
cur_weekday:    ds.b    1               ;0=sunday, 1=monday,...7=saturday
in_clock: 	dc.b 0     ;set by the Z: handler functions
tdbuff:   	ds.b 30      ;time/date buffer area
s_today:        ds.b    14              ;today's date
s_calltoday:    ds.w    1               ;total calls today

;	Steve's siov source code file

;     MY CIOV MACROS
;   THE CHANEL NUMBER #$20 AND SO ON
;  LOADED INTO THE X REGISTER FILE
; PARM'S ARE LOADED LOW BYTE INTO
;  FN_PTR HIGH BYTE FN_PTR+1 THEN A JSR
; TO THE LABLE YOU WISH TO OPEN BY
;
OPENIN
	 LDA #$04
	 STA $034A,X
	 JSR OPEN_WORK
	 RTS
;
;
OPENOUT
	 LDA #$08
	 STA $034A,X
	 JSR OPEN_WORK
	 RTS
OPENAPP
	 LDA #$09
	 STA $034A,X
	 JSR OPEN_WORK
	 RTS
OPENUPDT
	 LDA #$0C
	 STA $034A,X
	 JSR OPEN_WORK
	 RTS
OPENDIR
	 LDA #$06
	 STA $034A,X
	 JSR OPEN_WORK
	 RTS
GET
	 LDA #$05
	 STA $0342,X
	 LDA PR_PTR
	 STA $0344,X
	 LDA PR_PTR+1
	 STA $0345,X
	 LDA LNPTR
	 STA $0348,X
	 LDA LNPTR+1
	 STA $0349,X
	 LDA #$03
	 STA $034A,X
	 JSR $E456
	 RTS
;
BOOT     ;           XIO 45
	 JSR XIO_CLEAR
	 LDA #$26
	 JSR OPEN_WORK
	 RTS
;
;
STATUS1  dc.b $00
STATUS
	 STA STATUS1
	 JSR XIO_CLEAR
	 LDA STATUS1
	 STA $034A,X
	 LDA #$0D
	 JSR XIO_WORK
	 RTS
;
XIO_CLEAR
	 LDA #$00
	 STA PR_PTR
	 STA PR_PTR+1
	 STA LNPTR
	 STA LNPTR+1
	 RTS
;
OPENRAW
	 LDA #$03
	 STA $0342,X
	 LDA #$06
	 STA $034A,X
	 LDA #128
	 STA $034B,X
	 LDA FN_PTR
	 STA $0344,X
	 LDA FN_PTR+1
	 STA $0345,X
	 JSR $E456
	 RTS
;
;
OPEN_WORK
	 LDA #$03
	 STA $0342,X
	 LDA $034A,X
	 STA AUX
	 LDA $034B,X
	 STA AUX+1
	 LDA FN_PTR
	 STA $0344,X
	 LDA FN_PTR+1
	 STA $0345,X
	 JSR C_IOV
	 RTS
AUX      dc.b 0,0
;------------------------------
;you must jsr to this vector for point, note, file lenght
;
PNFDATA  dc.b "Dx:",$00
PNFDATA1 dc.b $00,$00
PNF_WORK
	 STX PNFDATA1
	 LDA FN_PTR
	 STA PNF_1+1
	 LDA FN_PTR+1
	 STA PNF_1+2
	 STY PNFDATA1+1
	 LDY #$01
PNF_1    LDA $FFFF,Y
	 STA PNFDATA+1
	 LDY PNFDATA1+1
	 LDX PNFDATA1
	 LDA #$00
	 STA $034A,X
	 LDA #$00
	 STA $034B,X
	  lda # low PNFDATA
	 STA $0344,X
	  lda # high PNFDATA
	 STA $0345,X
	 JSR C_IOV
	 RTS
;
RENAME   ;           XIO #32 RENAME FILE
	 LDA #$20
	 JSR XIO_WORK
	 RTS
;
ERASE    ;           XIO #33 ERASE FILE'S
	 JSR XIO_CLEAR
	 LDA #$21
	 JSR XIO_WORK
	 RTS
;
LOCK     ;           XIO #34 LOCK DISKETTE
	 JSR XIO_CLEAR
	 LDA #$22
	 JSR XIO_WORK
	 RTS
;
PROTECT  ;           XIO #35  PROTECT FILE'S
	 JSR XIO_CLEAR
	 LDA #$23
	 JSR XIO_WORK
	 RTS
UNPROTECT ;          XIO #36 UNPROTECT FILE'S
	 JSR XIO_CLEAR
	 LDA #$24
	 JSR XIO_WORK
	 RTS
;
POINT    ;           XIO 37  POINT
	 LDA #$25
	 STX XIO_39_HOLD
	 STA $0342,X
	 LDA OFFSET
	 STA $034C,X
	 LDA f.sector
	 STA $034D,X
	 LDA f.sector+1
	 STA $034E,X
	 JSR PNF_WORK
	 LDX XIO_39_HOLD
	 RTS
f.sector   dc.w 0
OFFSET   dc.b 0
NOTE     ;           XIO_38  NOTE
	 STX XIO_39_HOLD
	 LDA #$26
	 STA $0342,X
	 JSR PNF_WORK
	 LDX XIO_39_HOLD
	 LDA $034C,X
	 STA OFFSET
	 LDA $034D,X
	 STA f.sector
	 LDA $034E,X
	 STA f.sector+1
XIO_38E  RTS
NPSAVE1  dc.b 0,0,0
NPSAVE
	 LDA OFFSET
	 STA NPSAVE1
	 LDA f.sector
	 STA NPSAVE1+1
	 LDA f.sector+1
	 STA NPSAVE1+2
	 RTS
NPLOAD
	 LDA NPSAVE1
	 STA OFFSET
	 LDA NPSAVE1+1
	 STA f.sector
	 LDA NPSAVE1+2
	 STA f.sector+1
	 RTS
;
F_LENGTH ;           XIO#39 FILE LENGTH SPARTA ONLY
	 STX XIO_39_HOLD
	 LDA #$27
	 STA $0342,X
	 JSR PNF_WORK
	 LDX XIO_39_HOLD
	 LDA $034C,X
	 STA FNLNG
	 STA F_LNG
	 LDA $034D,X
	 STA FNLNG+1
	 STA F_LNG+1
	 LDA $034E,X
	 STA FNLNG+2
	 STA F_LNG+2
	 RTS
XIO_39_HOLD dc.b $00,$00
FNLNG    dc.b 0,0,0
F_LNG    dc.b 0,0,0
;FN_PTR dc.w 0
LNPTR    dc.w 0
;
BLOAD    ;           AND RUN _XIO_40_
	 LDA #$28
	 JSR XIO_WORK
	 RTS
;
BSAVE    ;           AND APPEND _XIO_41_
	 LDA #$29
	 JSR XIO_WORK
	 RTS
;
CREDIR   ;           XIO_42
	 JSR XIO_CLEAR
	 LDA #$2A
	 JSR XIO_WORK
	 RTS
;
DELDIR   ;           XIO_43
	 JSR XIO_CLEAR
	 LDA #$2B
	 JSR XIO_WORK
	 RTS
;
CWD      ;           [C]hange [W]orking [D]ir
; XIO_44
	 LDA #$2C
	 JSR XIO_WORK
	 RTS
;
CHKDSK   ;           XIO 47 CHECK DISK
	 LDA PR_PTR
	 STA $0348,X
	 LDA PR_PTR+1
	 STA $0349,X
;
	 LDA #$2F
	 JSR XIO_WORK
	 RTS
;
GET_DIR_PATH ;       ?DIR XIO 48
	 LDA PR_PTR
	 STA $0348,X
	 LDA PR_PTR+1
	 STA $0349,X
;
	 LDA #$30
	 JSR XIO_WORK
	 RTS
;
;
;
XIO_WORK
	 STA $0342,X
	 LDA #$04
	 STA $034A,X
	 LDA #$7F
	 STA $034B,X
	 LDA FN_PTR
	 STA $0344,X
	 LDA FN_PTR+1
	 STA $0345,X
	 JSR C_IOV
	 RTS
;
;----------------------------;
; LDA PR_PTR ; BUFFER
; STA #<BUFFER+1
;
BGET
	 LDA #$07
	 STA $0342,X
	 LDA PR_PTR
	 STA $0344,X
	 LDA PR_PTR+1
	 STA $0345,X
	 LDA LNPTR
	 STA $0348,X
	 LDA LNPTR+1
	 STA $0349,X
	 LDA #$03
	 STA $034A,X
	 LDA #$00
	 STA $034B,X
	 JSR C_IOV
	 RTS
;---------------------
;  PR_PTR=BUFFER  FN_PTR=LEINGTH OF BUFFER
;---------------------
BPUT
	 LDA #$0B
	 STA $0342,X
	 LDA PR_PTR
	 STA $0344,X
	 LDA PR_PTR+1
	 STA $0345,X
	 LDA LNPTR
	 STA $0348,X
	 LDA LNPTR+1
	 STA $0349,X
	 LDA #$03
	 STA $034A,X
	 LDA #$00
	 STA $034B,X
C_IOV
	 JSR $E456
	 LDA $0303
	 TAY
	 CPY #$81
	 BCC C_IOV_EXIT
	 JSR DRIVE_ERROR
C_IOV_EXIT RTS
	 RTS
DRIVE_ERROR
	 PHA
	 JSR printsi
	 dc.b $9B," Ciov Error # ",$FF
	 PLA
	 JSR PR_BYTE
	 JSR CR_LF
	 RTS
;
;       print the byte currently in Accum.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
pr_byte:
         ldx #$00
pr_card:
         sta $d4
         stx $d4+1
         lda #$00
         sta $f2
         jsr $d9aa
         jsr $d8e6
         ldy #$00
.aprbloop:
         lda ($f3),y
         php
         and #$7f
         iny
         jsr putlocal
         plp
         bpl .aprbloop
         rts
;


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


;Supporting stuff
; 
;--------------------------------------------
;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

;
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
;




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:  						; generic close
		lda	#$0c
		sta	$0342,x
		jsr	$e456
		rts
;
;
TEMP1   ds.b 250
;
TEMP2   ds.b 150
;
DIR_COUNT dc.b 0
;
DIR_PATH        ds.b 90 ;           dc.b "d5:>*.*",$9B,0
;
FILENAME ds.b 21
;
DIR_MASK ds.b 172
;
IN      ds.b 100        ;dc.b "d5:>memo.txt",$9B,0
;
OUT      ds.b 190       ;dc.b "d5:>memo.bak",$9B,0
;
LF       dc.b 2,13,10
DOT      dc.b 1,"."
EXT      dc.b 4,".JNK"
EXT1     dc.b 4,".KNJ"
;
SPACE    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



;       Return the length of string at s_ptr in accumulator
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

length:         ldy     #0
                lda     (s_ptr),y
                rts



;       Concatenate the string at s_ptr onto the end of string at d_ptr
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

concat:         ldy     #0
                lda     (s_ptr),y               ;get source lth
                sta     .srclth
                beq     .end                    ;null string..exit
                lda     (d_ptr),y               ;get dest lth
                sta     .desty
                inc     .srcy
                inc     .desty
                ldy     #1                      ;always start at 1 on
                sty     .srcy                   ; the source string
.loop:          ldy     .srcy
                lda     (s_ptr),y               ;get byte from source
                ldy     .desty
                sta     (d_ptr),y               ;store into dest
                lda     .srcy
                cmp     .srclth                 ;end of string?
                beq     .end                    ;yes
                inc     .srcy
                inc     .desty                  ;up indexes
                bne     .loop                   ;and keep going
.end:           ldy     #0
                lda     (d_ptr),y               ;up the length byte
                clc                             ; of the dest string
                adc     .srclth
                sta     (d_ptr),y
                rts

.srcy:          dc.b    1
.desty:         dc.b    1
.srclth:        dc.b    1



;       input a string from device in X register
;       into the string pointed to by in_ptr.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

inputsd:        sta     icbll,x         ;length is in the accum already
                lda     #ccgetrec
                sta     iccom,x
                lda     in_ptr
                clc
                adc     #1              ;offset past length byte
                sta     icbal,x
                lda     in_ptr+1
                adc     #0
                sta     icbah,x
                lda     #0
                sta     icblh,x         ;zero hi byte of length
                jsr     ciov            ;get the line
                bmi     .error
.noerror:       lda     icbll,x         ;get lth actually read in
                sec
                sbc     #1              ; minus 1
                ldy     #0
                sta     (in_ptr),y      ; and place it in length byte
                rts

.error:         sty     .holdy
                ldy     #0
                tya
                sta     (in_ptr),y
                ldy     .holdy
                rts

.holdy:         dc.b    0


;       print a string to device in X register
;       from the string pointed to by pr_ptr.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

printsd:        ldy     #0
                lda     (pr_ptr),y      ;install EOL at end of string
                tay
                iny
                lda     #$9b
                sta     (pr_ptr),y
                lda     #ccputrec
                sta     iccom,x
                lda     pr_ptr
                clc
                adc     #1              ;offset past length byte
                sta     icbal,x
                lda     pr_ptr+1
                adc     #0
                sta     icbah,x
                lda     #80
                sta     icbll,x
                lda     #0
                sta     icblh,x         ;zero hi byte of length
                jmp     ciov            ;get the line

;       Search the string pointed to by S_PTR for the substring
;       pointed to by D_PTR.  Returns a 0 if substring not found.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

substr:         ldy     #0
                lda     (s_ptr),y
                sta     .s_lth
                lda     (d_ptr),y
                sta     .d_lth
                lda     s_ptr
                sta     .comp+1
                lda     s_ptr+1
                sta     .comp+2
                lda     d_ptr
                sta     .get+1
                lda     d_ptr+1
                sta     .get+2
                lda     #1
                sta     .loc
                ldy     #0
                ldx     #0
                jsr     .up_str
                jsr     .up_sub

.comp:          lda     $ffff,y
                jsr     upcase
                sta     .temp
.get:           lda     $ffff,x
                jsr     upcase
                cmp     .temp
                beq     .match
.nomatch:       ldx     #1
                inc     .loc
                jsr     .up_str
                jmp     .comp
.match:         jsr     .up_sub
                jsr     .up_str
                jmp     .comp

.up_str:        iny
                cpy     .s_lth
                bcc     .xup
                beq     .xup
                pla
                pla
                lda     #0
.xup:           rts

.up_sub:        inx
                cpx     .d_lth
                bcc     .xsub
                beq     .xsub
                pla
                pla
                lda     .loc
.xsub:          rts

.temp:          dc.b    0
.loc:           dc.b    0
.s_lth:         dc.b    0
.d_lth:         dc.b    0

upcase:         cmp     #'a'
                bcc     xupcase
                cmp     #'z'+1
                beq     xupcase
                bcs     xupcase
                sec
                sbc     #32
xupcase:        rts

;     Make a length-byte string from an ASCII ($9b terminated) string
;     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tostr:          ldy     #0
.tfe:           lda     (s_ptr),y               ;find end of string
                beq     .gotend
                cmp     #$9b
                beq     .gotend
                iny
                bne     .tfe
.gotend:        sty     .lth                    ;save length
                cpy     #0
                beq     .rtn
                ldy     #0
.loop:          lda     (s_ptr),y
                iny
                sta     (d_ptr),y
                cpy     .lth
                bcc     .loop
.rtn:           ldy     #0
                lda     .lth
                sta     (d_ptr),y
                rts

.lth:           dc.b    1



WIN_END:		ds.b	0
;
;

