;------------------------------------------------------------------------
;		Copyright 2012 Integrated Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;                              SDMesage.com
;
;This is a program that sends one text strings to the display of the SIO2SD
; from what is in quotes
;		init	10/16/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,"SDMesage.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   10
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; 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..crl
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


			.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
Command_line:

			ldy	#LBUF    			; loading the offset into comtab
mux_1a:			lda	(comtab),y
			cmp	#' '				; looking for the space in the parm line
			beq	mux_1b 				; got the space let'S see if there is anything we can use
			cmp	#$9b				; nope!!  no parm
			beq	.b				; let'S see if they are playing with the consol
			cmp	#'?'
			beq	.a
			iny
			jmp	mux_1a

.a:			jmp	parm_errors			; go to format code
.b:			jmp	myhold

mux_1b:
			ldx	#$00
			iny					; let'S get one more byte of contab+63,y

.a:			lda	(comtab),y			; well got a byte
			cmp	#$9b
			beq	.noparm
			cmp	#'"'
			beq	.got_d				; going to the help menu
			iny
			jmp	.a

.holdx:			dc.b	0

.noparm:		jmp	parm_errors			; format with a menu

.got_d:
			ldx	#$01
			iny
			lda	(comtab),y			; well got a byte
			sta	parm_1,x
			cmp	#$9b
			beq	.noparm
			cmp	#'"'
			beq	.noparm
.loop
			inx
			stx	parm_1
			iny
			lda	(comtab),y
			sta	parm_1,x
			cpx	#28
			beq	.post
			cmp	#'"'
			bne	.loop
.post:			lda	#$9b
			sta	parm_1,x
			

.done:
done_1:
			mea	parm_1,pr_ptr
			jsr	echos.unify
			mea	parm_1,s_ptr
			mea	parm_2,d_ptr
			jsr	move_str
			jsr	printsi
			dc.b	$9b,$9b
			dc.b	" Your Message to be sent to your SIO2SD",$9b
			dc.b	" ==>"
			dc.b	$22
			dc.b	-1			
			mea	parm_2,pr_ptr
			jsr	echos
			jsr	printsi
			dc.b	$22,$9b,$9b,-1

			jmp	start


myhold:			jmp	done_1			
;


cli.crunch:		jmp	$0000

parm_errors:
			mea	parm_1,pr_ptr
			jsr	echos.unify

			mea	parm_1,s_ptr
			mea	parm_2,d_ptr
			jsr	move_str
;
			jsr	printsi
			dc.b	$9B,$9B
			dc.b	" Syntax for use:",$9b
			dc.b	" SDMesage "
			dc.b	$22
			dc.b	"Your Message!"
			dc.b	$22,$9b
			dc.b	" SDMesage "
			dc.b	$22
			dc.b	" Your Message!"
			dc.b	$22,$9b
			dc.b	" SDMesage "
			dc.b	$22



			dc.b	-1

			mea	parm_2,pr_ptr
			jsr	echos

			jsr	printsi
			dc.b	$22
			dc.b	$9b," Message Limit =20 Bytes"
			dc.b	$9b,$9b,$9b

			dc.b	$9b,-1
			RTS

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; My Xio work Vars and temp registers for all ciov
;
;
need_pause:		dc.b	0
FILENAME:		ds.b	80

parm_1:			dc.b	17,"RealDos Build 30",$9b
			ds.b	40

parm_2:			ds.b	60
;

;
Start:

			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





SIO2SD_message:
			lda	#$70
			sta	$300
			lda	#$03
			sta	$301
			lda	#$10				;$4e
			sta	$0302
			lda	#$80
			sta	$0303
			lda	# low parm_1+1
			sta	$0304
			lda	# high parm_1+1
			sta	$0305
			lda	#40
			sta	$0308
			lda	#$00
			sta	$0309
			lda	#0
			sta	$030a
			lda	#$00
			sta	$030b

status_device:		jsr	xsio
			lda	$303
			cmp	#$01
			bne	sio2sd_error
good_message:
			jsr	printsi
			dc.b	$9b,$9b,$9b
			dc.b	"  Sio2SD "
			dc.b	<+128>,"Present!"
			dc.b	$9b
			dc.b	-1
			rts
;
sio2sd_error:
			jsr	printsi
			dc.b	$9b,$9b,$9b
			dc.b	" Sio2SD "
			dc.b	<+128>,"Not Present"
			dc.b	$9b
			dc.b	-1
			jsr	driverror
			rts

;
XSIO:			jMP	$ffff

driverror:
			pha
			jsr	printsi
			dc.b	$9b," Siov Error # ",$FF
			pla
			jsr	pr_byte
			jsr	printsi
			dc.b	$9b,-1
			rts

; 
;       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
			cmp	#$9b
			bne	.k
			lda	#$93
.k:			sta     (d_ptr),y
			cpy     #0
			beq     .done
			dey
			jmp     .loop
.done:			rts





;
;
;
pr_byte:
			ldx	#$00
pr_card:
			sta	$d4
			stx	$d4+1
			lda	#$00
			sta	$f2
			jsr	$d9aa
			jsr	$d8e6
			ldy	#$00
.a:
			lda	($f3),y
			php
			and	#$7f
			iny
			jsr	echo
			plp
			bpl	.a
			rts
; 

;  this unifys the length string
;  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

			ldy	.tempy
			lda	(pr_ptr),y
			jsr	upcase
			sta	(pr_ptr),y
			jmp	.prnext

.pril:			ldy	.tempy
			cpy	#$02
			bcc	.prnext
			lda	(pr_ptr),y		; changing the bytes to lower case.
			jsr	locase
			sta	(pr_ptr),y

			cmp	#' '			; look for the space
			bne	.prnext
			inc	.tempy
			dec	.maxlth
			ldy	.tempy			
			lda	(pr_ptr),y		; make the char right after the space upper case
			jsr	upcase
			sta	(pr_ptr),y


.prnext:		inc	.tempy
			dec	.maxlth
			bne	.pril
.xpri:			rts

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


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


locase:			cmp     #'A'
			bcc     .xlocase
			cmp     #'Z'+1
			beq     .xlocase
			bcs     .xlocase
			clc
			adc     #32
.xlocase:		rts




;
;  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
			cmp	#$00
			beq	.xpri
			jsr	echo
.prnext:		inc	.tempy
			dec	.maxlth
			bne	.pril
.xpri:			rts

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

pr_mode:		dc.b	0


; 
; 
;================================= 
;       print/input routines 
;================================= 
; 
;       Print character 
;       --------------- 
; in: 
;       A       = character to print 
; out: 
;       all registers preserved 
; 
ECHO:			JSR	SAVER
			JSR	ZOUT
			JMP	RESALL
; 
ZOUT:			STA	ZTEMP+1
			LDA	$0347
			PHA 
			LDA	$0346
			PHA 
ZTEMP:			LDA	#0
			LDX	#0			;1      ; force NO output to PRINT Dn: 
			RTS 
; 
; 
;       Print text 
;       ---------- 
; out: 
;       all registers preserved 
; notes: 
;       This print routine will print all characters 
;       following the JSR PRINT until a delimiter of 
;       -1 ($FF) is reached. PRINT will return to the 
;       point one byte beyond the delimiter. 
; 
printsi:		JSR	SAVER
			TSX 
			LDA	$0105,X
			STA	ZOCH+1
			LDA	$0106,X
			STA	ZOCH+2
; 
			LDY	#1
ZOCH:			LDA	$FFFF,Y
			CMP	#$FF
			BEQ	ZECHO
			JSR	ECHO
			INY 
			BNE	ZOCH
ZECHO:			TYA 
			CLC 
			ADC	$0105,X
			STA	$0105,X
			LDA	$0106,X
			ADC	#0
			STA	$0106,X
			JMP	RESALL
; 
; 
;       SAVE & RESTORE registers 
;       ------------------------ 
; 
SAVER:			PHP 
			PHA 
			PHA 
			PHA 
			PHP 
			PHA 
			TXA 
			PHA 
			TSX 
			LDA	$0109,X
			STA	$0105,X
			LDA	$0107,X
			STA	$0109,X
			LDA	$0101,X
			STA	$0107,X
			LDA	$0108,X
			STA	$0104,X
			LDA	$0106,X
			STA	$0108,X
			TYA 
			STA	$0106,X
			PLA 
			TAX 
			PLA 
			PLP 
			RTS 
; 
RESALL:			PLA 
			TAY 
			PLA 
			TAX 
			PLA 
			PLP 
			RTS 
; 
; 
; 



win_end:		ds.b	0
;
;
;