;------------------------------------------------------------------------
;                       TCPIP Express!
;               Copyright (c) 2012 by Intergrated Logic Systems
;
;
;
;
;		Source Code is Copyright 1997 Steven J. Tucker
;
; Converted to xasm   2/9/2005  By Stephen J. Carden
;
;
;------------------------------------------------------------------------
;                            Apetime.S
;                   Written By Steven J. Tucker
;             Apetime.com   A status file for the multiplexer
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;
;
;
; APETIME Time Downloader
;
; (c)1997 Steven J Tucker
;
;         9731 Sunrise M-33
;         North Royalton, Ohio  44133
;         440-237-5308
;
; Uses the spartados comtab to set the time and date via the entry vectors
;  under the OS that are installed by Spartados and Spartados X.  This
;  should work in any version of Spartados.  Routines fot both machines
;  with memory under the OS and those without are included.
;
; The programmed time is downloaded from the attached APE software.  If APE
;  is not present, the loader complains & exits without disturbing the time
;
;------------------------------------------------------------------------
; 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	12,"filename.ext",$9b
			dc.b	12,"Ape_hnd.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   11
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; 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..yes
;
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define if it can be relocated
r..crl:			equ	$7d
r..crlf:		equ	$9b
r..space:		equ	$20

l_frstscreenbyte:	equ	r..crl
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;
;		 program constants
;		~~~~~~~~~~~~~~~~~~
;
dateoff:	equ    $000D   ;; DATER offset


			.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
			jsr	get_xsio		; Grab our Xsiov vector
			jsr	Ape_time
			rts



get_xsio:		LDA	COMTAB			; calc address of SIO 
			SEC 
			SBC	#low lsio		; Taking the low byte and subtract it
			STA	XSIO+1			; Taking the results and storing it into a vector I can use
			LDA	COMTAB+1		; Now the high byte
			SBC	#high lsio		; Subing it again
			STA	XSIO+2			; putting it into my vector
			lda	#$6c			; hex for JMP
			sta	xsio
			rts

XSIO:			jMP	siov

hold_stack:		dc.b	0

;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;load the acc with the flag you want to set
;	lda	#Value
;	jsr	set_hw
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

set_hw:
			sta	.hold
			jsr	os_off
			mea	int.hardware,s_ptr
			lda	.hold
			jsr	setlvl
			jsr	os_on
			rts

.hold:			dc.b	0


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;   OS on and off ROUTEEN
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;

os_off:
			SEI	
			LDA	PORTB
			AND	#$FE
			STA	PORTB
			CLI	
			RTS	
;
os_on:
			SEI	
			LDA	PORTB
			ORA	#1
			STA	PORTB
			CLI	
			RTS	




;     sets the bit specified (in the A register) of the security
;     level pointed to by S_PTR to a TRUE value.
;     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

setlvl:			sty	.tempy
			stx	.tempx
			cmp	#0			;sec lvl of zero?
			beq	.return			;yes..just exit (shouldn't happen!)
			tay				 ;-1 for shifting
			lda	#128
			sta	.temp			;initialize mask
			lda	#0
			sta	.temp+1
			sta	.temp+2
			sta	.temp+3
			dey
			beq	.mask			;if it was one, no need to shift
.shloop:	   	lsr	.temp			;shift bits down to form mask
			ror	.temp+1
			ror	.temp+2
			ror	.temp+3
			dey
			bne	.shloop
			ldy	#0
.mask:			lda	.temp,y			;now, OR the bit on
			ora	(s_ptr),y
			sta	(s_ptr),y
			iny
			cpy	#4
			bne	.mask
.return:	   	rts

.tempy:			dc.b    0
.tempx:			dc.b    0
.temp:			dc.b    0,0,0,0



	

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Ape time Source
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ape_time:
        	jsr 	printsi				; display copyright message
		dc.b 	$9b,$9b
		dc.b	"Pulling Time/Date from APE interface!",$9b,$9b
		dc.b	"APE Interface is Copyright",$9b

		dc.b	"1997 Steven J. Tucker",$9b,-1

       		jsr 	gettime				; get date/time
        	bmi 	tranerr				; error during time download!
        	jsr 	settime				; set time

			lda	#G_Ape
			jsr	set_hw

		jsr	printsi
		dc.b 	$9b
		dc.b 	<+128>"Time/Date"
		dc.b	" Updated from IBM PC",$9b,$9b,-1
        	rts					; exit program.

tranerr:

		jsr 	Printsi
		dc.b 	$9b
		dc.b 	<+128>,"Download failed!"
		dc.b 	$9b," Check Your cables and your",$9b
		dc.b	"Software on the IBM PC",$9b
		dc.b	"Run 'Apetime' to try again!"


		dc.b 	$9b,$9b,-1
        	rts					; exit program.

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;		Attempt to download time from APE.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

gettime: 	ldx 	#$0B			; setup the download command
setloop: 	lda 	cmdtab,x		;
        	sta 	$300,x			;
        	dex				;
        	bpl 	setloop			;
        	clc				; download directly into COMTAB.
        	lda 	comtab			; calculate destination address
        	adc 	#dater			;
        	sta 	$304			;
        	lda 	comtab+1		;
        	adc 	#$00			;
        	sta 	$305			;
        	jmp 	xsio			; get time from APE

cmdtab:  	dc.b $45, $01, $93, $40, $00, $00, $00, $00, $06, $00, $EE, $A0


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; Enters spartados kernel routine to set time.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

settime: 	lda 	portb			; get into ram portion
        	pha				;
        	and 	#$FE			;
        	sta 	portb			;
        	jsr 	vsettd			; enter spartados or real.dos under the os
        	pla				;
        	sta 	portb			; restore OS
        	rts				; exit

;
;--------------------------------------------
;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	echo
			jmp	prsl
estri:			lda	pstr+2
			pha
			lda	pstr+1
			pha
			rts
;
;
;------------
; PRINT BYTE 
;------------
echo:
sprint:			sta	$b8
			lda	#$04
			sta	$034a
			stx	$b6
			sty	$b7
			ldx	#$06
			lda	$e400+1,x
			sta	$b3
			lda	$e400,x
			sta	$b2
			lda	$b8
			jsr	.a
			ldx	$b6
			ldy	$b7
			lda	#$0c
			sta	$034a
			lda	$b8
			rts
;
.a:			inc	$b2
			jmp	($b2)
			rts





Data_buffer:		ds.b	128

win_end:
        .end
