;------------------------------------------------------------------------
;		Copyright 2012 Integrated Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;                              SetUTime.com
;
;This is a program gets or sets the time clock in the sio2usb
;		init	9/09/2012	sjc new design
;
;
;
;------------------------------------------------------------------------
; 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,"SetUTime.com",$9b
			.endm

;
;----------------------------------------------------------------
; File revision history. Version Number in hex
;
file_ver:		equ   $10
;----------------------------------------------------------------
; the Month in dec for the first time this revision 
;was compiled
;
c_month:		equ   9
;----------------------------------------------------------------
; the day in dec for the first time this revision 
;was compiled
c_day:			equ   16
;----------------------------------------------------------------
; 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 if it can be relocated
r..crl:			equ	$7d
r..crlf:		equ	$9b
r..space:		equ	$20
r..bypassdos:		equ	$15	; bypass dos check
r..bpasintro:		equ	$ff


l_frstscreenbyte:	equ	r..bypassdos
;----------------------------------------------------------------
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;----------------------------------------------------------------
;Programming equates for the sio2usb!
;
;
;
usbF_Read_Only:		equ	$01	; Read Only x0000000 1 or 0
usbF_Archive:		equ	$32	; Archive Bit 0000x000 32 or 0
;----------------------------------------------------------------
;
usb_Get_stat:		equ	$00	; 4.4.1 Get U Status info 
usb_Set_RTC:		equ	$01	; 4.4.2 Set Real Time Clock Length 6 bytes
usb_Read_RTC:		equ	$02	; 4.4.3 Read Real Time Clock
usb_CD:			equ	$03	; 4.4.4 Change Media Directory (CD)
usb_RFATDIR:		equ	$04	; 4.4.5 Read FAT Directory Entry
usb_SET_ATARI_DRV_CFG:	equ	$05	; 4.4.6 Set ATARI drive Configuration
usb_Read_ATARI_DRV_CFG:	equ	$06	; 4.4.7 Read ATARI drive Configuration
usb_Create_ATR:		equ	$07	; 4.4.8 Create ATR FAT Filenor make FAT Directory (MD)
usb_Read_Free:		equ	$08	; 4.4.9 Read amount of free media bytes
usb_Delete_fd:		equ	$09	; 4.4.10 Delete FAT file or remove FAT Directory (RD)
usb_Read_VFAT_dir:	equ	$0A	; 4.4.11 Read VFAT Directory Entry
usb_Rename_FAT:		equ	$0B	; 4.4.12 Rename FAT file or FAT directory
;usb_??:		equ	$0C	; unknow
usb_Copy_FAT_file:	equ	$0D	; 4.4.13 Copy FAT file
usb_Change_Attrib:	equ	$0F	; 4.4.14 Change FAT file attribute


;----------------------------------------------------------------
;
pgm_sio2usb:		equ	$71		; Program sio2usb internal commands
siov_read:		equ	$40		; Read
siov_write:		equ	$80		; write
;----------------------------------------------------------------
;
usb_disk1:		equ	1		; Emulated Disk 1
usb_disk2:		equ	2		; Emulated Disk 2
usb_disk3:		equ	3		; Emulated Disk 3
usb_disk0:		equ	0		; Emulated Disk 5
;
usb_timeout:		equ	7		; sio timeout
xfersize:		equ	128		; Buffer size we are getting or putting
;
usb_inuse:		equ	usb_disk0	; USB device we are programming for
siov_do:		equ	siov_write
usb_disk:		equ	usb_disk0	; 
;----------------------------------------------------------------
ZPF0:			equ	$F0
ZPF1:			equ	ZPF0+1
SDBASE:			equ	$700
SDDEV:			equ	$761
SDDATE:			equ	$77B
SDTIME:			equ	SDDATE+3
EDITRV:			equ	$E400



			.include	equates
			.include	globals
			.include	macros

			.org  $4000

win_start:
header_info:
			.include	header			; this reads info out of the header and displays to screen
;
Start:	;		jmp	parm_errors		; debug
			lda	#$60
			sta	start
			jsr	r_time
			LDA 	COMTAB				; calc address of SIO 
			SEC 
			SBC 	# low lsio
			STA 	XSIO+1
			LDA 	COMTAB+1
			SBC 	# high lsio
			STA 	XSIO+2
			lda	#$6c
			sta	xsio

			lda	comtab
			CLC 
			adc	# low ZCRNAME
			sta	cli.crunch+1
			lda	comtab+1
			adc	# high ZCRNAME
			sta	cli.crunch+2
			jsr	cli.crunch
			bne	.get_parm1		; nothing there
			jmp	go_to_it		;go get the time from the sio2usb pass it to TDLine

.get_parm1:
			lda	comtab
			CLC 
			adc	# low COMFNAM		; parm_1 need "SET"
			sta	s_ptr
			lda	comtab+1
			adc	# high COMFNAM
			sta	s_ptr+1
			mea	parm_1,d_ptr		;get Command Parm from Cli
			jsr	tostr			; copy it to a length byte string into parm_1

;			jsr	printsi
;			dc.b	$9b,$9b,".1",-1
;
			mea	parm_1,s_ptr
			mea	set_data,d_ptr
			jsr	substr			; Check for 'SET'
			cmp	#$00	 		; if greater than 1 or more we have 'SET'
			bne	.a~a			; go get the rest of the parms
			jmp	menu			; Got a command go to menu
.a~a:
			jsr	cli.crunch		; Get the next parm
			bne	.get_parm2		; if zero then no parm
			jmp	key_in			; go to menu  Need to check the rest

.get_parm2:
;			jsr	printsi
;			dc.b	".2",-1

			lda	comtab
			CLC 
			adc	# low COMFNAM		; Moving parm to a
			sta	s_ptr
			lda	comtab+1
			adc	# high COMFNAM		; Parm to move
			sta	s_ptr+1
			mea	parm_2,d_ptr		; Destination string
			jsr	tostr			; move 'date' from cli to parm_2

			jsr	cli.crunch		; let us see if we have another word
			bne	.get_parm3		; yes we have another word on the cli

			jmp	missing_parm3		; Missing 'Time' Cluster

.get_parm3:
;			jsr	printsi
;			dc.b	".3",-1

			lda	comtab
			CLC 
			adc	# low COMFNAM		; setting to cli source 'time' word
			sta	s_ptr
			lda	comtab+1
			adc	# high COMFNAM		;  setting to cli source 'time' word
			sta	s_ptr+1
			mea	parm_3,d_ptr		; Destination String
			jsr	tostr			; copy 'TIME' string and turn it to a length byte string

			jsr	cli.crunch		; see if we have another word on the cli
			bne	.exit			; Yes we have another word

			jmp	cli_get_Time.Date	; ok we can set the time from the command line
;			jmp	.exit1

.exit:
			lda	comtab
			CLC 
			adc	# low COMFNAM		; setting to source word
			sta	s_ptr
			lda	comtab+1
			adc	# high COMFNAM		; setting to source word
			sta	s_ptr+1
			mea	parm_4,d_ptr		; Destination String
			jsr	tostr			; copy string and turn it to a length byte string

			mea	parm_1,s_ptr
			mea	filename,d_ptr
			jsr	toascii

.exit1:
;			lda	consol
;			cmp	#consol_start		;consol_any
;			beq	.no_tell
;			cmp	#consol_select
;			beq	.give_menu
;			cmp	#consol_option
;			beq	.give_cli_info		;any two consol keys give debug routine


			jsr	printsi		
			dc.b	$9b," CLI Parms"
			dc.b	$9b,"Parm_1 =",-1
			mea	parm_1,pr_ptr
			jsr	echos

			jsr	printsi
			dc.b	$9b,"Parm_2 =",-1
			mea	parm_2,pr_ptr
			jsr	echos

			jsr	printsi
			dc.b	$9b,"Parm_3 =",-1
			mea	parm_3,pr_ptr
			jsr	echos

			jsr	printsi
			dc.b	$9b,"Parm_4 =",-1
			mea	parm_4,pr_ptr
			jsr	echos
			rts

.no_tell:		jmp	text_to_screen		;get the time from the command line
.give_menu:		jmp	key_in			;Key in time and date from a menu
.give_cli_info:		jmp	parm_errors

cli.crunch:		jmp	$0000

parm_errors:

			jsr	printsi
			dc.b	$7d
			dc.b	$9B
			dc.b	" Syntax for use:",$9b
			dc.b	" SetUTime [Parm] <Date> <Time>",$9b,$9b
			dc.b	" <Parm> = "
			dc.b	" 'Menu' "
			dc.b	<+128>,"[Sets Time/Date]   "
			dc.b	$9b
			dc.b	"        = "
			dc.b	" 'Help' "
			dc.b	<+128>,"[This Help Screen] "
			dc.b	$9b
			dc.b	"        = "
			dc.b	" 'Info' "
			dc.b	<+128>,"[Notes from Author]"
			dc.b	$9b
			dc.b	"        = "
			dc.b	" '/?'   "
			dc.b	<+128>,"[This Screen]"
			dc.b	$9b
			dc.b	"        = "
			dc.b	" 'Read' "
			dc.b	<+128>,"[UPDATE TDLine]"
			dc.b	$9b
			dc.b	"        = "
			dc.b	" 'Set'  "
			dc.b	<+128>,"[Sets Time Menu]"
			dc.b	$9b
			dc.b	"        = "
			dc.b	" 'Set'  "
			dc.b	<+128>,"'<Date>'"
			dc.b	" "
			dc.b	<+128>,"'<Time>'"
			dc.b	" "
			dc.b	$9b
			dc.b	" "
			dc.b	<+128>,"Updates SIO2USB from the CLI!"
			dc.b	$9b
			dc.b	$9b
			dc.b	" "
			dc.b	<+128>,"Date"
			dc.b	" Format 'MM-DD-YY' '09-10-12'"
			dc.b	$9b 
			dc.b	" "
			dc.b	<+128>,"Time"
			dc.b	" Format 'HH:MM:SS' '13:10:00'"
			dc.b	$9b
			dc.b	" MM=Month 01-12 Two Number Format"
			dc.b	$9b
			dc.b	" DD=Day   01-31 Two Number Format"
			dc.b	$9b
			dc.b	" YY=Year  00-99 Two Number Format"
			dc.b	$9b,$9b
			dc.b	" "
			dc.b	<+128>,"Example"
			dc.b	$9b
			dc.b	" SetUTime Set 09-14-12 07:30:00"
			dc.b	$9b
			dc.b	" SetUTime "
			dc.b	<+128>,"No Parm"
			dc.b	" Updates TDLine2!"

			dc.b	$9b
			dc.b	$9b
			dc.b	-1
			RTS
missing_parm3:

			jsr	printsi
			dc.b	$9B,$9B
			dc.b	"Missing [MM-DD-YY] and [HH:MM:SS] parm!"
			dc.b	$9b
			dc.b	-1
			rts

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; My Xio work Vars and temp registers for all ciov
;
;
set_data:		dc.b	3,"SET"
set_help:		dc.b	4,"Help"
set_info:		dc.b	4,"info"
set_question:		dc.b	2,"/?"
set_read:		dc.b	4,"Read"
set_menu:		dc.b	4,"Menu"

need_pause:		dc.b	0
FILENAME:		ds.b	64

parm_1:			ds.b	64
parm_2:			ds.b	64
parm_3:			ds.b	64
parm_4:			ds.b	64
in_str:			ds.b	128
instr:			ds.b	128
;
;==========================================================
;
;I am going to read sio2usb time/date and set tdline2
;
go_to_it:
			jmp	sio2usb
			rts

;==========================================================
;gotta check the parms and menu's help, info ect
;
;===============================================================
;Search the string pointed to by S_PTR for the substring
;pointed to by D_PTR.  Returns a 0 if substring not found.
;===============================================================
;
menu:
			mea	parm_1,s_ptr
			mea	set_data,d_ptr
			jsr	substr
			cmp	#$00
			beq	.a
			jmp	key_in		; set time date from cli
.a:
			mea	set_help,d_ptr
			jsr	substr
			cmp	#$00
			beq	.b
			jmp	parm_errors
.b:
			mea	set_info,d_ptr
			jsr	substr
			cmp	#$00
			beq	.c
			jmp	text_to_screen
.c:
			mea	set_read,d_ptr
			jsr	substr
			cmp	#$00
			beq	.d
			jmp	sio2usb
.d:
			mea	set_menu,d_ptr
			jsr	substr
			cmp	#$00
			beq	.e
			jmp	key_in
.e:
			mea	set_question,d_ptr
			jsr	substr
			cmp	#$00
			beq	.f
			jmp	parm_errors
.f:


			jmp	parm_errors

;===========================================================
;it take some work to get the sio2usb ready for 
;read time set time
key_in:
			jsr	get_usb_time

			lda	#pgm_sio2usb
			sta	$300			; DDevic
			lda	#usb_Set_RTC		;
			sta	$0302			; setting our sio2usb command
			lda	#siov_write
			sta	$0303
			jsr	clock_io
			jsr	clock_error
			rts



clock_io:		lda	#usb_disk
			sta	$301			; DUnit

			lda	# low usb_Time_date
			sta	$0304			; Data BUFfer LO
			lda	# high usb_Time_date
			sta	$0305			; Data BUFfer HI

			lda	#usb_timeout
			sta	$0306			; TimeOut
			lda	#$00
			sta	$0307			; Dunuse


			lda	#low xfersize
			sta	$0308			; Number Byte lo
			lda	#high xfersize
			sta	$0309			; Number Byte hi
			lda	#$00
			sta	$030a 			; DAUX 1
			lda	#$00
			sta	$030b 			; DAUX 2
			jsr	xsio
			rts


clock_error:
			lda	$303
			cmp	#$01
			beq	ok_time_set
sio2usb_error:		jsr	printsi
			dc.b	$9b,$9b
			dc.b	"Error $"
			dc.b	-1
			lda	$303
			jsr	drive_error
			jsr	printsi
			dc.b	$9b,$9b,-1
			rts

ok_time_set:
			jsr	sio2usb.SETTIME
			jsr	printsi
			dc.b	$9b,$9b
			dc.b	"Sio2USB "
			dc.b	<+128>,"Time Date Set"
			dc.b	$9b
			dc.b	-1
.b:
			rts

XSIO:			jMP	$ffff

;=============================================================
;read time out of sio2usb
;
get_usb_time:
			lda	#pgm_sio2usb
			sta	$300			; DDevic
			lda	#usb_Read_RTC		;
			sta	$0302			; setting our sio2usb command
			lda	#siov_read
			sta	$0303
			jsr	clock_io

			lda	$303
			cmp	#$01
			beq	.a
			jsr	printsi
			dc.b	$9b,$9b
			dc.b	"SIO2USB "
			dc.b	<+128>,"Not found"
			dc.b	-1
			jmp	sio2usb_error

.a:			jsr	get_Time.Date
			rts
;=============================================================
;Time Date menu
;
;it not in the Doc's but this is a length byte String
;and you have to transfer 128 bytes not 6.
;
;gonna read sio2usb clock
;get input from user
;Set the sio2usb time date
;

get_Time.Date:

.a:			Jsr	printsi
			dc.b	$9b,$9b
			dc.b	" Month Format 01-12",$9b
			dc.b	"Enter Month ==> "
			dc.b	-1
			jsr	Ascii_dec		; get_byte
			sta	usb_month

			Jsr	printsi
			dc.b	$9b,$9b
			dc.b	" Day Format 01-31",$9b
			dc.b	"Enter Day   ==> "
			dc.b	-1
			jsr	Ascii_dec		; get_byte
			sta	usb_day

			Jsr	printsi
			dc.b	$9b,$9b
			dc.b	"Enter Year 20??",$9b
			dc.b	" Year Format 00-99",$9b
			dc.b	"Enter Year  ==> "
			dc.b	-1
			jsr	Ascii_dec		; get_byte
			sta	usb_year

			Jsr	printsi
			dc.b	$9b,$9b
			dc.b	"Enter Time 0 to 23",$9b
			dc.b	"Enter Hour  ==> "
			dc.b	-1
			jsr	Ascii_dec		; get_byte
			sta	usb_Hour

			Jsr	printsi
			dc.b	$9b
			dc.b	"Enter Minute => "
			dc.b	-1
			jsr	Ascii_dec		; get_byte
			sta	usb_Minutes

			rts
;=============================================================
;gonna read sio2usb clock
;read command line interface
;Set the sio2usb time date
;
cli_get_Time.Date:
			lda	#pgm_sio2usb
			sta	$300			; DDevic
			lda	#usb_Read_RTC		;
			sta	$0302			; setting our sio2usb command
			lda	#siov_read
			sta	$0303
			jsr	clock_io

			lda	$303
			cmp	#$01
			beq	.a
			jsr	printsi
			dc.b	$9b,$9b
			dc.b	"SIO2USB "
			dc.b	<+128>,"Not found"
			dc.b	-1
			jmp	sio2usb_error

.a:
			jsr	printsi
			dc.b	$9b," Date ",-1
			ldx	parm_2+4
			lda	parm_2+5
			jsr	Ascii_dec_cli
			sta	usb_month
	
			jsr	printsi
			dc.b	"-",-1
			ldx	parm_2+7
			lda	parm_2+8
			jsr	Ascii_dec_cli
			sta	usb_day

			jsr	printsi
			dc.b	"-",-1
			ldx	parm_2+10
			lda	parm_2+11
			jsr	Ascii_dec_cli
			sta	usb_year
	
			jsr	printsi
			dc.b	"  Time ",-1
			ldx	parm_3+1
			lda	parm_3+2
			jsr	Ascii_dec_cli
			sta	usb_Hour

			jsr	printsi
			dc.b	":",-1
			ldx	parm_3+4
			lda	parm_3+5
			jsr	Ascii_dec_cli
			sta	usb_Minutes

			jsr	printsi
			dc.b	":",-1
			ldx	parm_3+7
			lda	parm_3+8
			jsr	Ascii_dec_cli
			sta	usb_second

			lda	#pgm_sio2usb
			sta	$300			; DDevic
			lda	#usb_Set_RTC		;
			sta	$0302			; setting our sio2usb command
			lda	#siov_write
			sta	$0303
			jsr	clock_io
			jsr	clock_error
			jsr	sio2usb.SETTIME
;			jsr	sio2usb
			rts
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Ascii to Dec Conversion Routien
;input from keyboard needed!
;
Ascii_dec:
			lda	#$00
			sta	.temp
			sta	.echo

.a:			jsr	get_key
			sta	.acc
			cmp	#'0'-1
			bcc	.a
			cmp	#'9'+1
			bcs	.a
			lda	.acc
			jsr	echo
			lda	.acc
;			SEC 
			SBC 	#$2f
			sta	.y
			tay
			lda	.ascii_high,y
			sta	.temp
;			jsr	drive_error

.b:			jsr	get_key
			sta	.acc1
			cmp	#'0'-1
			bcc	.b
			cmp	#'9'+1
			bcs	.b
			lda	.acc1
			jsr	echo
			lda	.acc1

;			sec
			sbc	#$2f
			sta	.y
			tay
			lda	.ascii_low,y
			eor	.temp
			sta	.echo
			jsr	printsi
			dc.b	$9b,$9b,-1
			lda	.echo
			rts


.Ascii_high:		dc.b	$00,$10,$20,$30,$40,$50,$60,$70,$80,$90
.ascii_low:		dc.b	$00,$01,$02,$03,$04,$05,$06,$07,$08,$09
.acc:			dc.b	0
.acc1:			dc.b	0
.y:			dc.b	0
.x:			dc.b	0
.temp:			dc.b	0
.temp1:			dc.b	0
.echo:			dc.b	0

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Ascii to Dec Conversion Routien
;input from cli
;
Ascii_dec_cli:
			stx	.high_byte
			sta	.low_byte

			lda	#$00
			sta	.temp
			sta	.echo

.a:			lda	.high_byte
			sta	.acc
			jsr	echo		;debug routine
			lda	.acc
			SBC 	#$2f
			sta	.y
			tay
			lda	.ascii_high,y
			sta	.temp

.b:			lda	.low_byte
			sta	.acc1
			jsr	echo		;debug routine
			lda	.acc1

;			sec
			sbc	#$2f
			sta	.y
			tay
			lda	.ascii_low,y
			eor	.temp
			sta	.echo

;			jsr	printsi
;			dc.b	$9b,$9b,-1
			lda	.echo
			rts


.Ascii_high:		dc.b	$00,$10,$20,$30,$40,$50,$60,$70,$80,$90
.ascii_low:		dc.b	$00,$01,$02,$03,$04,$05,$06,$07,$08,$09
.acc:			dc.b	0
.acc1:			dc.b	0
.y:			dc.b	0
.x:			dc.b	0
.temp:			dc.b	0
.temp1:			dc.b	0
.echo:			dc.b	0
.high_byte:		dc.b	0
.low_byte:		dc.b	0


;------------------------------
sio2usb.READRTC:
			dc.b 	$71
			dc.b 	$00
			dc.b 	$02
			dc.b 	$40
        		dc.w 	su2.buf
			dc.w 	7
        		dc.w 	128
			dc.b 	0
			dc.b 	0
			dc.b	0
			dc.b	0
			dc.b	0

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Time Date Working buffer
;
;it not in the Doc's but this is a length byte String
;and you have to transfer 128 bytes not 6.
;
su2.buf:
usb_Time_date:		dc.b	6		
usb_second:		dc.b	0
usb_Minutes:		dc.b	52
usb_Hour:		dc.b	11
usb_day:		dc.b	13
usb_month:		dc.b	9
usb_year:		dc.b	12
Data_buffer:		ds.b	128


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

echos:
			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
			jsr	fast_output
.prnext:		inc	.tempy
			dec	.maxlth
			bne	.pril
.xpri:			rts

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



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

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




;------------------------
;JSR GET_CARD
;
;------------------------
gtbyte:			dc.b	$00,"       "
qstrin:			dc.b	"       "
gtwr:			dc.b	$03,$06


get_byte:
			lda	gtwr
			sta	gt1+1
			ldy	#$00
			sty	gtbyte
			cmp	gtwr
			beq	gt1
get_card:
			lda	gtwr+1
			sta	gt1+1
			ldy	#$00
			sty	gtbyte
gt1:			cpy	#$ff
			beq	gt3
			jsr	get_key
			jsr	upcase
			cmp	#$9b
			beq	gt3
			cmp	#$7e		; BACKSPACE
			beq	gt2
			cmp	#'0'
			bcc	gt1
			cmp	#':'
			bpl	gt1
			ldy	gtbyte
			iny
			sty	gtbyte
			sta	gtbyte,y
			jsr	echo
			cmp	#$ff
			bne	gt1
			beq	gt1
gt2:			ldy	gtbyte		; BACKSPACE
			cpy	#$00
			beq	gt1
			lda	#$7e
			jsr	echo
			dec	gtbyte
			cmp	#$ff
			bne	gt1
			beq	gt1
;
;-MATH Stuf FOR CARD CONVERSION-
gt3:
			lda	#$9b
			ldy	gtbyte
			iny
			sta	gtbyte,y
			sty	gtbyte
			lda	# low gtbyte
			sta	s_ptr
			lda	# high gtbyte
			sta	s_ptr+1
			lda	# low qstrin
			sta	d_ptr
			lda	# high qstrin
			sta	d_ptr+1
			jsr	toascii
			lda	# low qstrin
			sta	$f3
			lda	# high qstrin
			sta	$f3+1
			lda	#$00
			sta	$f2
			jsr	$d800
			jsr	$d9d2
			lda	$d4
			ldx	$d4+1
			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

;

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


;------------------------------
;   GET KEY  ROUTEEN 
;this routeen get's one key and
;return it to the accumlator   
;------------------------------
;
drive_error:		pha
			lsr
			lsr
			lsr
			lsr
			jsr	.a
			pla
			and	#$0f
.a:			cmp	#$0a
			bcs	.b
			ora	#$30
			bne	.c
.b:			adc	#$36
.c:			jsr	echo
			rts
;

;------------------------------
;   GET KEY  ROUTEEN 
;this routeen get's one key and
;return it to the accumlator   
;------------------------------
;
get_key:		stx	$b6
			sty	$b7
			jsr	.a
			ldx	$b6
			ldy	$b7
			rts
.a:			lda	$e420+5
			pha
			lda	$e420+4
			pha
			rts
;
;-----------------------------
;CHANGES THE CHARTER TO UPCASE
;-----------------------------
upcase:
			cmp	#$61
			bcc	.a
			cmp	#$7a
			bcs	.a
			sbc	#$1f
.a:			rts
;
;-----------------------------
;CHANGES THE CHARTER TO LOCASE
;-----------------------------
locase:
			cmp	#$41
			bcc	.a
			cmp	#$5a
			bcs	.a
			adc	#$1f
.a:			rts
;-------------------
;NO INVERSE  INV_OFF
;-------------------
inv_off:		cmp	#$80
			bmi	.a
			sbc	#$80
.a:			rts
;----------------------
;FORCED INVERSE  INV_ON
;----------------------
inv_on:			cmp	#$80
			bpl	.a
			and	#$80
.a:			rts
;

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; SIO2USB Source
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sio2usb:
			jsr	printsi
			dc.b	$9b
			dc.b	"SIO2USB (C) 2008 ABBUC/RAF"
			dc.b	$9B
			dc.b	-1
sio2usb1:		jsr	sio2usb.COUTINIT
			jsr	sio2usb.GETTIME
			bmi	sio2usb.SIOERROR
			jsr	sio2usb.TESTDATA
			jsr	sio2usb.SETTIME
			jmp	sio2usb.EXIT
;------------------------------
sio2usb.ERRORMSG:
			jsr	printsi
			dc.b	$FD 			; Beep
			dc.b	"Error: "
			dc.b	$ff
			RTS
;------------------------------
sio2usb.SIOERROR:
			jsr	sio2usb.ERRORMSG
			jsr	printsi
			dc.b	"during SIO Communication"
			dc.b	$ff
			jmp	sio2usb.EEXIT
;------------------------------
sio2usb.DATAERROR:
			jsr	sio2usb.ERRORMSG
			jsr	printsi
			dc.b	"wrong or incomplete data recieved"
			dc.b	$ff
			jmp	sio2usb.EEXIT
;------------------------------
sio2usb.EEXIT:
			jsr	printsi
			dc.b	$9B
			dc.b	"Date/Time not set!"
			dc.b	$9B
			dc.b	-1
			rts


sio2usb.COUTINIT:
			ldy	#$1E
.1:			lda	HATABS,Y
			cmp	#'E'
			beq	.3
			DEY
			DEY
			DEY
			bpl	.1
			jmp	.2
.3:			lda	HATABS+1,Y
			sta	$f0
			lda	HATABS+2,Y
			sta	$f1
			ldy	#7
			lda	($f0),Y
			sta	sio2usb.COUT2+1
			DEY
			lda	($f0),Y
			sta	sio2usb.COUT1+1
.2:			RTS
;------------------------------
sio2usb.COUT:
			TAX
sio2usb.COUT2:		lda	#$F2
			PHA
sio2usb.COUT1:		lda	#$AF
			PHA
			TXA
			RTS
;------------------------------
sio2usb.GETTIME:
			ldx	#$0B
.1:			lda	sio2usb.READRTC,X
			sta	DDEVIC,X
			DEX
			bpl	.1
			jmp	SIOV
;------------------------------
sio2usb.TESTDATA:
			lda	su2.buf
			cmp	#6
			bne	.3
			lda	su2.buf+7
			cmp	#$FF
			beq	.2
.3:			jmp	sio2usb.DATAERROR
; CONVERT BCD -> DEC
.2:			ldx	#5
.1:			lda	su2.buf+1,X
			AND	#$F0
			LSR
			LSR
			sta	$f0
			LSR
			adc	$f0
			SBC	su2.buf+1,X
			EOR	#$FF
			sta	su2.buf+1,X
			DEX
			bpl	.1
;
			ldx	#2
			ldy	#$0F
.4:			lda	su2.buf+4,X
			sta	(DOSVEC),Y
			DEY
			DEX
			bpl	.4
;
			ldy	#$10
			lda	su2.buf+3
			sta	(DOSVEC),Y
			INY
			lda	su2.buf+2
			sta	(DOSVEC),Y
			INY
			lda	su2.buf+1
			sta	(DOSVEC),Y
			RTS
;------------------------------
sio2usb.SETTIME:
			lda	PORTB
			PHA
			AND	#$FE
			sta	PORTB
			jsr	VSETTD	
			PLA
			sta	PORTB
			RTS
;------------------------------
; FALLTHROUGH
;------------------------------
sio2usb.EXIT:
			jsr	printsi
			dc.b	"Date/Time set"
			dc.b	$9B,$ff
; FALLTHROUGH
;------------------------------
sio2usb.TODOS:
			rts

;=======================================================================
;
;Info screen
;

text_to_screen:
			lda	#$01
			sta	752
;			jsr	start-3

;========================================================================
;	                                  1111111111222222222233333333334
;	                         1234567890123456789012345678901234567890
			jsr	printsi

			dc.b 	$9b
			dc.b 	"If you would like the source code to ",$9B
			dc.b	" this file please contact the Author",$9b,-1
			lda	f.Authors_name
			sta	pr_ptr
			lda	f.Authors_name+1
			sta	pr_ptr+1
			jsr	echos

			jsr	printsi
			dc.b	" ",$9b,-1

			lda	f.Command_name
			sta	pr_ptr
			lda	f.Command_name+1
			sta	pr_ptr+1
			jsr	echos

			jsr	printsi
			dc.b 	" Can ONLY be distributed as",$9b
			dc.b	"part of the RealDos Construction set.",$9b
			dc.b	"RealDos can not be a part of any",$9b

			dc.b 	"program sold profit!",$9b
			dc.b 	"including shareware disk's",$9b,$9b
			dc.b 	<+128>,"The Three exception to the are:"
			dc.b 	$9b
			dc.b 	<+128>,"AtariMax and/or Steven Tucker"
			dc.b 	$9b
			dc.b 	<+128>,"ABBUC User Group and/or Thomas Grasel"
			dc.b 	$9b
			dc.b 	<+128>,"Registered to WASEO for Dictionary++"
			dc.b 	$9b,$9b
			dc.b	-1

			jsr	pause
			jsr	printsi
			dc.b	$9b," Should a company wish to use this",$9b
			dc.b	"I will provide a site license for a",$9b 
			dc.b	"cost $100.00 US Dollars!",$9b 
			dc.b	"Atari usergroups may use it FREE of charge",$9b
			dc.b	"for there BBS Systems and disk of ",$9b 
			dc.b	"the month.",$9b,-1

			jsr	pause
			jsr	printsi
			dc.b	$9b,$9b
			dc.b	"If you wish to have the source",$9B
			dc.b	"code then contact:",$9B,$9B
			dc.b	"       Stephen J. Carden",$9B
			dc.b	"       3321 Jonathan Cir.",$9B
			dc.b	"       Augusta, Ga. 30906",$9B
			dc.b	" Paypal 'sjcardensr@bellsouth.net'",$9B,$9b
;
			dc.b	"Or send a donation of $10.00US per program",$9b 
			dc.b	"file the above address.",$9B,$FF

			jsr	printsi
			dc.b	$9b,$9b
			dc.b	" Press "
			dc.b	<+128>,"[Return]"
			dc.b	" any other key ",$9B,-1
			jsr	get_key
			lda	#$00
			sta	752
			rts	
;
pause:    ;           a more command
more:
			jsr 	printsi
			dc.b 	<+128>,"[ - MORE - ]"
			dc.b 	-1
			jsr 	get_key
			cmp 	#$7e    	; back space key out of here
			beq 	.esc_more
			cmp	#$1b			;esc key
			beq	.esc_more
;			cmp	#$1c			;up arrow
;			beq	.esc_more

			jsr 	printsi
			dc.b 	$7e,$7e,$7e,$7e
			dc.b 	$7e,$7e,$7e,$7e
			dc.b 	$7e,$7e,$7e,$7e
			dc.b 	-1
			rts

.esc_more:
			lda	#$00
			sta	752
			pla
			pla
			rts

;
;
r_time:
			lda	$14
.r_1:			cmp	$14
			beq	.r_1
			lda	portb
			pha
			and	#$fe
			sta	portb
			clc
			ldy	#$01
			jsr	vtdon
			bcs	r_time_not
			pla
			sta	portb
			rts
;
r_time_not:
			pla
			sta	portb
			jsr	start-3
			jsr	printsi
			dc.b	$9b,$9b
			dc.b	" ‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’…",$9B
			dc.b	" ü"
			dc.b	<+128>," R-time Handler not Present! "
			dc.b	"ü",$9B
			dc.b	" ü"
			dc.b	<+128>," Install tdline2.com before  "
			dc.b	"ü",$9B
			dc.b	" ü"
			dc.b	<+128>," trying to read the time and "
			dc.b	"ü",$9B
			dc.b	" ü"
			dc.b	<+128>," date from the SIO2USB and   "
			dc.b	"ü",$9B
			dc.b	" ü"
			dc.b	<+128>," and updating your TDLine!   "
			dc.b	"ü",$9B
			dc.b	" ü"
			dc.b	<+128>,"     Press Reset to exit     "
			dc.b	"ü",$9B
			dc.b	" š’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ƒ",$9B,$9B,$FF
			pla
			pla
			rts

;
;

win_end:		ds.b	0
;
;
;