;------------------------------------------------------------------------
;
;		Copyright 2010 Intergraded Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;      			Build header source
;
; 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
;------------------------------------------------------------------------
;
;	init design to detect know hardware
;
;
;			last updated 3/9/2010 Added header stuff  sjc
;
;			added sio2usb detection	8/27/2012	sjc
;			
; 			added sio2sd detection 9/10/2012	sjc
;			added sio2ide detection 9/12/2012	sjc
;			added sdrive detection 9/19/2012	sjc
;
;=====================================================================

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

;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; File revision history. Version Number in hex
;
file_ver:		equ   $14
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; 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   19
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; 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
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;----------------------------------------------------------------
;	SIOV commands that SIO2IDE use
;
;      SIO commands accepted by SIO2IDE FS
;
pgm_sio2ide:	equ	$71		; Program sio2ide internal commands
SIOC_GETDSK:	equ	$11		; Get the Disk description (aux1)
;
ide_master:	equ	$01		; Master IDE Device
ide_slave:	equ	$02		; Slave IDE Device
;----------------------------------------------------------------
;
siov_read:	equ	$40		; Read
siov_write:	equ	$80		; write
;----------------------------------------------------------------
;
ide_disk1:	equ	1		; Emulated Disk 1
;
ide_timeout:	equ	20		; sio timeout
xfersize:	equ	15		; Buffer size we are getting or putting
;
ide_inuse:	equ	ide_master	; Ide we are programming for
siov_do:	equ	siov_read
ide_disk:	equ	ide_disk1	; Aux1
;----------------------------------------------------------------



			.include	equates
			.include	globals
			.include	macros

			.org	$3000


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

top:
			TSX	
			STX	hold_stack

			jsr	get_xsio		; Grab our Xsiov vector
			lda	consol			; check the consol key
			cmp	#7			; looking for any key
			beq	.a			; nope they are not hold a key down
			jmp 	help_menu		; Show help menu
.a:
			lda 	vcount
			bne 	.a

			jsr	memory_find		; this is to find the ram banks

;			jsr	cleanup			; Purge the tsr location

			jmp	show_hardware



;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;  THis is the memory Finder routeen
;
; Syntax:  jsr memory_find
;
mepoint:		dc.b	0
;
total_ram_banks:
i_t_ram_banks:		dc.w 	0
i_ram_lookup:
        		ds.b 	128
;
m_buffer:
        		ds.b 	256
;
m_buffer_2:
        		ds.b 	256




;
memory_find:
;
         		SEI
         		LDY 	#0
         		STY 	NMIEN
         		STY 	IRQEN
         		LDA 	PORTB
	 		sta 	mepoint
         		PHA
         		LDX 	#$FF
.a:    			STY 	PORTB
         		LDA 	M_4000
         		STA 	m_buffer,Y
         		STX 	M_4000
         		TXA
         		STA 	m_buffer_2,Y
         		INY
         		BNE 	.a
         		DEX
         		STX 	PORTB
         		STX 	LNFLG
         		STX 	M_4000
         		STX 	M_8000
         		STX 	M_C000
         		LDX 	#0
.b:    			STY 	PORTB
         		LDA 	M_4000
         		CMP 	#$FF
         		BNE 	.c
         		INX
         		STX 	M_4000
         		TXA
.c:    			STA 	m_buffer_2,Y
         		INY
         		BNE 	.b
         		STX 	i_t_ram_banks
         		LDX 	#1
.d:    			TXA
         		LDY 	#$FF
.e:    			CMP 	m_buffer_2,Y
         		BEQ 	.g
         		DEY
         		CPY 	#$FF
         		BNE 	.e
         		NOP
.f:    			STY 	PORTB
         		LDA 	m_buffer,Y
         		STA 	M_4000
         		DEY
         		CPY 	#$FF
         		BNE 	.f
         		PLA
         		STA 	PORTB
         		LDA 	#$C0
         		STA 	NMIEN
         		LDA 	POKMSK
         		STA 	IRQEN
         		CLI
         		RTS
;
.g:    			TYA
         		STA 	i_ram_lookup-1,X
         		INX
         		CPX 	#64+1 ;$41
         		BCC 	.d
         		LDY 	#$FE
         		BNE 	.f
         		JMP 	.h		; I can do with out this I think
;
.h:    			JSR 	memory_find
	 		lda 	I_t_ram_banks
	 		sta 	$a0
         		RTS
;-------------------------------------------------
; Buffer Table

sio2sd1_data:		dc.b	"RealDos Build 30"
			dc.b	40
sio2sd2_data:		dc.b	"SIO2SD Detected!"
			ds.b	256

ape_version:		ds.b	512

i_cmdtab2:		ds.b 	512
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Working buffer
;
;------------------------------
READRTC:
			dc.b	$71,$00,$02,$40
			dc.w	su2.buf
			dc.b	$07,$00,$80,$00
;------------------------------
su2.buf:		dc.b	$00,$00,$00,$00
			dc.b	$00,$00,$00,$00

Data_buffer:		ds.b	128




			.org	$8000


show_hardware:
;---------------------------------------
;Going to peek in and set 50/60 HZ Byte in
;RealDos info.
			jsr	pal_ntsc
;
;---------------------------------------
;looking to see if the SmartOS is present!
			jsr	TEST_SMARTOS
;
;---------------------------------------
;looking to see if a Multiplexer Slave 
;is present!

			jsr	mux_man2
;---------------------------------------
; testing for the Black Box
			jsr	BLACK_BOX
			lda	pbi_type
			cmp	#$00
			bne	.go
;---------------------------------------
; Testing for the MIO
			jsr	MIO_TEST
			lda	pbi_type
			cmp	#$00
			bne	.go

;---------------------------------------
; testing for kpi interface
			jsr	kpi_test

;---------------------------------------
; Testing for the sio2sd
.go:
			jsr	sio2sd

;---------------------------------------
; Testing for the SDRIVE
;
			jsr	Sdrive
			lda	$303
			cmp	#$01
			beq	.bypass
;---------------------------------------
; Testing for the sio2ide
;
			jsr	sio2ide
;---------------------------------------
; Testing for the sio2usb
;
			jsr	sio2usb

.bypass:
;---------------------------------------
;lets see if the is an ape interface
;Be carefule with this on. a low level
; siov call is made.
;since we are looking at the time clock
;function this routine also looks for
;the sio2usb time clock.
			jsr	ape_run
;---------------------------------------
;this will pull info out of the ape 
;telling if it is a shareware or
;fully functional Ape device.
;
;
			jsr	ape_ver
;---------------------------------------
;Let's see if we have an R: and P: port
			jsr	modem_run
			jsr	printer_run
;---------------------------------------
;Let's see if we are running an emulator
			jsr	pc_run
;---------------------------------------
;Testing for ram at $d700
			jsr	USP


			jsr	ck_for_rtime
;---------------------------------------
; Let's See What Kind of OS
; We have here!
			jsr	TEST_OS
;---------------------------------------
			jsr	show_computer
			LDX	hold_stack
			TXS	

			jmp	($A)

			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



;
;  Checking on the black box if it is present!
;
BLACK_BOX
			SEI
			LDa	#2
			STa	BB_Sensel
			ldy	 #$10
.a
			ldx	#2
			LDa	bb_rom,Y
			cmp	.e,X
			beq	.b
			INY
			bne	.a
			jmp	.d
			RTS
;
.b
			INY
			DEX
			bmi	.c
			LDa	bb_rom,Y
			cmp	.e,X
			bne	.a
			beq	.b
.c

			lda	#G_blackbox
			jsr	set_hw
			lda	#$02
			sta	pbi_type

			jsr	printsi
			dc.b	"Black Box IS present in system!",$9B,-1
;
.d
			CLD
			LDa	#0
			STa	BB_Sensel
			CLI
			RTS
;
.e			dc.b	$D0,$08,$29,$00
;
;-----------------------------------------------------------------
;
;routine to check for the kpi
;
kpi_test
			sei
			lda	#$28
			sta	PBIBANK      ;turn on ram and printer code bank
			lda	pbi_ram+1
			cmp	#'C'
			bne	.no_kpi
			lda	pbi_ram+7
			cmp	#'K'          ;as in KProducts in the ram string
			bne	.no_kpi       ;if it ain't there then get out

			lda	#G_kpi
			jsr	set_hw

			lda	#$03
			sta	pbi_type

			jsr	printsi
			dc.b	"KPI interface IS present in system!",$9B,-1
			lda	#$00
			sta	$d1ff        ; added code for Ken
.no_kpi
			cli
			rts                    ;or whichever way you need to return
;
;
;------------------------------------------------------------------
;checking for pal/ntsc machines
;
;
pal_ntsc
			lda	$d014
			ora	#$f0 ; masking high order bytes
			cmp	#$ff ;
			beq	.ntsc
			bne	.pal
			rts
.ntsc
			jsr	OS_off
			lda	#60
			sta	euro_time
			jsr	os_ON
			jsr	printsi
			dc.b	"NTSC Detected! Setting To 60hz",$9B,-1
			RTS
.pal
			jsr	OS_OFF
			lda	#50
			sta	euro_time
			jsr	OS_ON
			jsr	printsi
			dc.b	"PAL Detected! Setting To 50hz",$9B,-1
			RTS
;
;------------------------------------------------------------------
;	Check to see if we are running on an emulator
; 
pc_run
			ldy	#$00
loap_1:			lda	$031a,y
			cmp	#'H'     ; WAS THE EDITOR
			beq	get_vec
			iny
			iny
			iny
			cpy	#36
			bne	loap_1
			rts
;
get_vec
			lda	#$ff
			sta	USP_DATA

			lda	#G_Emulator
			jsr	set_hw

			jsr	printsi
			dc.b	"PC Emulator Detected!",$9B,-1
			RTS
;
;
;------------------------------------------------------------------
Ape_ver
			lda	#$00
			ldy	#$00
.a:
			sta	i_cmdtab2,y
			sta	i_cmdtab2+256,y
			iny
			cpy	#$00
			bne	.a




make_drive: 		ldx 	#$0B
.a:	 		lda 	i_cmdtab,x
        		sta 	$300,x
        		dex
        		bpl 	.a
        		jsr 	XSIO
			lda	$303
			cmp	#$01
			beq	.got_ape

			rts



.got_ape:

			lda	#G_Ape
			jsr	set_hw

			mea	i_cmdtab2,s_ptr	
			mea	ape_version,d_ptr
			jsr	to_str
			mea	ape_version,pr_ptr
			jsr	echos
			lda	#$9b
			jsr	echo
			rts

i_cmdtab:		dc.b 	$31
			dc.b 	$05
			dc.b 	$93		;Remote control Command
			dc.b 	$40
        		dc.w 	i_cmdtab2
			dc.b 	0
			dc.b 	0
        		dc.w 	256
			dc.b 	$F1		;f8	;$fa		; remote control function
			dc.b 	$00		; Remote Control sub function




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

Ape_run:
       			jsr 	gettime			; get date/time
        		bmi 	.noape			; error during time download!
			jsr	printsi
			dc.b	"Ape or SIO2PC&AspeQt Detected!",$9b,-1
			rts				; exit Routine

.noape:			rts

sio2usb:
			jsr	sio2usb.GETTIME		; see if we have a sio2usb
			bmi	.a			; error during time download!

			lda	#G_sio2usb
			jsr	set_hw


			jsr	printsi
			dc.b	"Sio2usb Detected!",$9b,-1
.a:
        		rts				; exit Routine

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

;

tammys_os:		dc.b	0

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Attempt to download time from SIO2USB.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sio2usb.GETTIME:
			ldx	#$0B
.1:			lda	READRTC,X
			sta	$300,X
			DEX
			bpl	.1
			jmp	XSIO


;------------------------------------------------------------------
;	Check to see if we have a modem
; 
modem_run
			ldy	#$00
.a:			lda	$031a,y
			cmp	#'R'     ; WAS THE EDITOR
			beq	.b
			iny
			iny
			iny
			cpy	#36
			bne	.a
			rts
;
.b:
			lda	pbi_type
			cmp	#$01
			beq	.got_mio
			cmp	#$02
			beq	.got_bb
			bne	.got_nun

.got_bb:		lda	#G_blackbox_r
			dc.b	$2c
.got_mio:		lda	#G_mio_r
			jsr	set_hw
.got_nun:

			jsr	printsi
			dc.b	"R: handler Detected",$9B,-1
			RTS
;
;------------------------------------------------------------------
;	Check to see if we have a modem
; 
printer_run:
			ldy	#$00
.a:			lda	$031a,y
			cmp	#'P'     ; WAS THE EDITOR
			beq	.b
			iny
			iny
			iny
			cpy	#36
			bne	.a
			rts
;
.b:
			jsr	printsi
			dc.b	"P: handler Detected",$9B,-1
			RTS
;
;
;------------------------------------------------------------------
;	Check to see if we have a modem
; 
Mux_run:
			ldy	#$00
.a:			lda	$031a,y
			cmp	#'M'     ; WAS THE EDITOR
			beq	.b
			iny
			iny
			iny
			cpy	#36
			bne	.a
			rts
;
.b:

			lda	#G_Mux
			jsr	set_hw

			jsr	printsi
			dc.b	"M: handler Detected",$9B,-1
			RTS
;
; prep the multiplexer
mux_man2:
			ldx	#$20				; preping the m device
			lda	#3
			sta	$0342,x
			lda	# low mdev
			sta	$0344,x
			lda	# high mdev
			sta	$0345,x
			lda	#12
			sta	$034a,x
			lda	#0
			sta	$034b,x
			jsr	$e456
			bpl	cht030 				; hay it'S a slave
			jmp	chterr 				; either the master is booting the file or no multiplexer

cht030:			jsr	xstat				; hay let'S see what the slave number is
			tya     				; let'S move let slave number out of the y to the acc
			sta	node				; just a holding reg
			eor	#$30				; let'S make it into something we can see
			sta	node+1 				; saving the slave number just incase
			sta	node1				; let'S put the slave number into a message
			jsr	printsi
			dc.b	$9b
			dc.b	"Multiplexer Slave #"
node1:			dc.b	"x "
			dc.b	<+128>,"Detected!"
			dc.b	$9B,$FF
			ldx	#$20				; well let'S close the m device
			jsr	cclose
			rts



chterr:			tya     				; outch!!!  got an error
			pha
			ldx	#$20
			jsr	chtcls
			pla
			rts
;
cclose:  							; generic close
			lda	#$0c
			sta	$0342,x
			jsr	$e456
			rts
;
;
xstat:
			ldx	#$20				; this is a status of the slave
			lda	#13
			sta	$0342,x
			lda	#$00
			sta	$034a,x
			sta	$034b,x
			jsr	$e456
			lda	747
			sta	l747
			rts
;
;
chtcls
			ldx	#$20				; just some mux xio'S
			lda	#12
			sta	$0342,x
			jsr	$e456
			rts

;data areas that are loaded
l747:			dc.b	$00,$00
mdev:			dc.b	"M:",$9B,$00
node:			dc.b	0,0
;
;
;

;
;------------------------------------------------------------------
;  This Routeen Tests for a MIO in system
;
;
MIO_TEST
			sta	mio_acc
			sty	mio_y
			stx	mio_x
			tsx
			stx	mio_stack
			sei

			lda	$14
tammy:			cmp	$14
			beq	tammy
			SEC			; DISSABLE ALL INTRUPES
			LDa	$d1e0
			STa	M_HOLD
			LDa	$d1e2
			STa	M_HOLD+1
			LDa	#$00
			STa	$d1e0
			LDa	#$20
			STa	$d1e2
			LDa	#'C'
			cmp	pbi_ram+1
			bne	NO_MIO
			cmp	pbi_ram+10
			bne	NO_MIO
			lda	#$01
			sta	mio_there

NO_MIO
			lda	#$00    	; this reselects the mio to be enabled
			sta	$d1e0
			sta	$d1e2
			CLI

			lda	M_HOLD
			sta	$d1e0
			lda	M_HOLD+1
			sta	$d1e2


			ldx	mio_stack
			txs
			lda	mio_acc
			ldy	 mio_y
			ldx	mio_x
			lda	mio_there
			cmp	#$00
			beq	_exit


			jsr	printsi
			dc.b	"ICD MIO Present in System!",$9B,-1

			lda	#G_mio
			jsr	set_hw
			lda	#$01
			sta	pbi_type

_exit

			rts


M_HOLD:			dc.b	0,0

mio_acc:		dc.b	0	; accumlator
mio_y:			dc.b	0	; Y register
mio_x:			dc.b	0	; x register
mio_stack:		dc.b	0	; hold the stack
mio_there:		dc.b	0	; holding reg for mio test
;
;------------------------------
; Need to test for ram at D7xx
;------------------------------
;
USP_DATA:		dc.b	0,0
OS_Type:		dc.b	0
USP:
			LDa	$D79D
			STa	USP_DATA+1
			LDa	#$04
			STa	$D79D
			cmp	$D79D
			bne	NO_USP
			LDa	USP_DATA+1
			STa	$D79D
			LDa	#$FF
			STa	USP_DATA
			jsr	printsi
			dc.b	"Ram Detected at $D7xx ",$9B,-1
;
NO_USP:			RTS
;
;off = 1   on = 0
gtia_mux:
			lda	$d012
			cmp	#$01
			beq	gtia_off
			jsr	printsi
			dc.b	"Mux Disable switch "
			dc.b	<+128>,"ON"
			dc.b	$9b,-1
gtia_off:
			rts
;
gtia_usp:
			lda	$d012
			cmp	#$01
			beq	gtia_of
			jsr	printsi
			dc.b	"USP booting from ramdisk "
			dc.b	$9b,-1
gtia_of:
			rts
;
;
;
TEST_OS:
;			jsr	os_on
			LDa	$CFFE
			sta	tammys_os
			cmp	#$10			; xl os detected
			bne	.USP1
			LDa	$E459+1
			cmp	#$33
			beq	.USPA
			cmp	#$70			; gotta look for My IDE interface
			beq	.USPa
			LDa	#$00			; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$01
			sta	os_type
			jsr	printsi
			dc.b	"Standard OS Detected",$9B,-1
			RTS
;
.USPA:			jmp	.USP6
;
.USP1
			cmp	#$FE			; turboss detected
			bne	.USP2
			LDa	#$00			; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$02
			sta	os_type
			jsr	printsi
			dc.b	"Turboss OS Detected!",$9B,-1
			RTS
;
.USP2
			cmp	#$90			; Mux
			bne	.USP3
			LDa	#$FF			; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$80
			sta	os_type
			jsr	gtia_mux
			jsr	printsi
			dc.b	"Mux OS Detected!",$9B,-1
			RTS
;
.USP3
			cmp	#$88			; US+Plus
			bne	.USP4
			LDa	#$00			; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$07
			sta	os_type
			jsr	my_wait
			jsr	gtia_usp
			jsr	printsi
			dc.b	"Ultra Speed Plus OS Detected!",$9B,-1
.NNR4:			RTS
;
.USP4
;
			cmp	#$00			; omnimon
			bne	.USP5
			ldy	$CDA5
			cpy	#$60
			beq	.TBPLUS
;
			LDa	#$00			; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$04
			sta	os_type
			jsr	printsi
			dc.b	"Omnimon OS Detected!",$9B,-1
			RTS
;
.TBPLUS:
			LDa	#$00			; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$03
			sta	os_type
			jsr	printsi
			dc.b	"Turboss + OS Detected!",$9B,-1
			RTS
;
.USP5
;
			cmp	#$F3			; omniview
			bne	.USPAA
			LDa	#$00    		; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$05
			sta	os_type
			jsr	printsi
			dc.b	"OmniView OS Detected!",$9B,-1
			RTS
;
.USPAA:			jmp	.OSDT
;
.USP6
			lda	$e459+1
			cmp	#$70			; gotta look for My IDE interface
			bne	.USP7
			LDa	#$FF			; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$82
			sta	os_type
			jsr	printsi
			dc.b	"My IDE 3.1 OS Detected!",$9B,-1
			RTS
.usp7

			lda	$e459+1
			cmp	#$8f    		; gotta look for My IDE interface
			bne	.USP8
			LDa	#$FF    		; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$81
			sta	os_type
			jsr	printsi
			dc.b	"My IDE 4.2 OS Detected!",$9B,-1
			RTS




.USP8
			cmp	#$08    		; Looking for Ape Warp OS
			bne	.OSDT
			LDa	#$00    		; 00 for high speed siov $ff for slow
			STa	USP_DATA
			lda	#$06
			sta	os_type
			jsr	printsi
			dc.b	"Ape Warp + OS Detected!",$9B,-1
			RTS
.OSDT
			LDa	#$00
			STa	USP_DATA
			jsr	printsi
			dc.b	"Unknown OS Detected",$9B,-1
			RTS
;
;
;
MY_WAIT
			LDa	RTCLOK+2
.A:			cmp	RTCLOK+2
			bne	.A
			RTS
;
;------------------------------------------------
TEST_SMARTOS:
			LDa	$D011
			STa	SMOS_DATA+2
;
			jsr	MY_WAIT
			LDa	PORTB
			STa	SMOS_DATA+1
;
			PHP
			SEI
			LDa	NMIEN
			PHA
			LDa	#$00
			STa	NMIEN
;
			LDa	SMOS_DATA+2
			cmp	#$01
			beq	NOT_IN_ROM
			LDa	#$40
			STa	SMOS_DATA
;
NOT_IN_ROM:
			LDa	#$A0
			STa	SMARTOS
			LDa	#$FF
			STa	Char_Set
			LDa	Char_Set
			cmp	#$FF
			beq	SMOS_HERE
;
			PLA
			STa	NMIEN
			PLP
			LDa	SMOS_DATA+1
			STa	PORTB
			CLI
			RTS
;
SMOS_HERE:
			LDa	#$00
			STa	char_Set
			LDa	SMOS_DATA
			STa	SMARTOS
;
			PLA
			STa	NMIEN
			PLP
			LDa	SMOS_DATA+1
			STa	PORTB
			CLI
;
			jsr	MY_WAIT
			jsr	MY_WAIT
			jsr	MY_WAIT
;
			jsr	printsi
			dc.b	"Smart OS Detected in ",-1
			LDa	SMOS_DATA
			cmp	#$40
			bne	.NOT_IN_ROM
			jsr	printsi
			dc.b	"Rom Mode!",$9b,-1
			RTS
;
.NOT_IN_ROM:
			LDa	SMOS_DATA
			cmp	#$80
			bne	.NOT_IN_RAM
			jsr	printsi
			dc.b	<+128>,"Ram"
			dc.b	" Mode!",$9b,-1
.NOT_IN_RAM:
			RTS
;
SMOS_DATA:		dc.b	$80,0,0
;
;
;=================================================================
;Sio2sd Detection
;this Routine sends this text to the display on the sio2sd
;
;"RealDos Build 30"
;"SIO2SD Detected!"
;=================================================================
sio2sd:
;
			lda	#$70
			sta	$300
			lda	#$03
			sta	$301
			lda	#$10				
			sta	$0302
			lda	#$80
			sta	$0303
			lda	# low sio2sd1_data
			sta	$0304
			lda	# high sio2sd1_data
			sta	$0305
			lda	#40
			sta	$0308
			lda	#$00
			sta	$0309
			lda	#0
			sta	$030a
			lda	#$00
			sta	$030b
			jsr	xsio
			lda	$303
			cmp	#$01
			beq	.a
			rts

.a:

			lda	#G_sio2sd
			jsr	set_hw

			jsr	printsi
			dc.b	"Sio2sd Detected!",$9b,-1

			lda	#$70
			sta	$300
			lda	#$03
			sta	$301
			lda	#$10				
			sta	$0302
			lda	#$80
			sta	$0303
			lda	# low sio2sd2_data
			sta	$0304
			lda	# high sio2sd2_data
			sta	$0305
			lda	#40
			sta	$0308
			lda	#$00
			sta	$0309
			lda	#1
			sta	$030a
			lda	#$00
			sta	$030b
			jsr	xsio
			rts
;-------------------------------------------------
;Look and See if we have a sio2ide Master
; and/or slave ide device
;-------------------------------------------------
sio2ide:
			lda	#ide_master
			sta	$301			; DUnit
			jsr	get_sio2ide

			jsr	xsio
			lda	$303
			cmp	#$01
			beq	.a
			bne	.b

.a:
			lda	#G_sio2idem
			jsr	set_hw

			jsr	printsi
			dc.b	"Sio2ide "
			dc.b	<+128>,"Master"
			dc.b	" Detected!",$9b
			dc.b	-1
.b:

			lda	#ide_slave
			sta	$301			; DUnit
			jsr	get_sio2ide

			jsr	xsio
			lda	$303
			cmp	#$01
			beq	.aa
			rts

.aa:
			lda	#G_sio2ides
			jsr	set_hw
			jsr	printsi
			dc.b	"Sio2ide "
			dc.b	<+128>,"Slave"
			dc.b	" Detected!",$9b
			dc.b	-1
			rts


get_sio2ide:		lda	#pgm_sio2ide
			sta	$300			; DDevic

			lda	#SIOC_GETDSK		; setting our sio2ide command	
			sta	$0302			; DComnd
			lda	#siov_do		; $40 Read  $80 send
			sta	$0303			; DStats

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

			lda	#ide_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	#ide_disk
			sta	$030a 			; DAUX 1
			lda	#$00
			sta	$030b			; DAux 2
			rts


;============================================================
;
;This detect the sdrive
;


SDrive:

			lda	#$71
			sta	$300			; DDevic
			lda	#$01
			sta	$301			; DUnit

			lda	#$e0		
			sta	$0302			; setting our sdrive command
			lda	#siov_read
			sta	$0303
			lda	# low sdrive_data
			sta	$0304			; Data BUFfer LO
			lda	# high sdrive_data
			sta	$0305			; Data BUFfer HI

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

			lda	#low 38
			sta	$0308			; Number Byte lo
			lda	#high 38
			sta	$0309			; Number Byte hi
			lda	#low 38
			sta	$030a 			; DAUX 1
			lda	#high 38
			sta	$030b 			; DAUX 2

			jsr	xsio
			lda	$303			; Drive return Status
			cmp	#$01
			beq	.a
			rts

.a:			jsr	printsi
			dc.b	"SDrive Detected!",$9b
			dc.b	-1

			lda	#G_sdrive
			jsr	set_hw

			rts




sdrive_data:		
			ds.b	256

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


;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;checks user security level pointed to by S_PTR to see if the
;bit specified in the A register is ON.  Returns MInus if the
;user doesn't have the specified security level.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

seclvl:	    		sty	.tempy
			stx	.tempx
			cmp	#0			;sec lvl of zero?
			beq	.passed			;yes..they have it
			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, AND off all the bits
			and	(s_ptr),y
			sta	.temp,y
			iny
			cpy	#4
			bne	.mask
.check:			lda	.temp			;next, OR all the bytes...if any one
			ora	.temp+1			; is > 0, then they passed (whew!)
			ora	.temp+2
			ora	.temp+3
			cmp	#0
			beq	.failed
.passed:	   	lda	#0
			beq	.return
.failed:	   	lda	#$80
.return:	   	php
			ldy	.tempy
			ldx	.tempx
			plp
			rts

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


;     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


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

clrlvl:         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, XOR the bit low
                eor     #$ff
                sta     .temp,y
                iny
                cpy     #4
                bne     .mask
                ldy     #0
.low:           lda     .temp,y         ;now, AND the bit low
                and     (s_ptr),y
                sta     (s_ptr),y
                iny
                cpy     #4
                bne     .low
.return:        rts

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






;=================================================================

;       Start of clock operative check
;       ------------------------------

ck_for_rtime:
			lda	#$60            	; When using the SpartaDOS command processor,
			sta	ck_for_rtime		; the program will be reentered... place a
                                			; return at the beginning to prevent this
			ldx	#0
			jsr	read			; get seconds
			cmp	#60
			bcs	bsecs			; if >60 then bad...
			sta	help
			lda	$14
			clc
			adc	#90             	; 1.5 seconds about
.a:			cmp	$14
			bne	.a
			jsr     read            	; read again
			cmp     help
			beq     bsecs           	; if equal, then problems...
			sec
			sbc	help            	; make sure <3
			bcs	okc1
			adc	#60             	; wrap??
okc1:			cmp     #3
			bcc     cont1           	; too much time ticked...
bsecs:			lda     # low mesg
			sta     $344
			lda	# high mesg
			sta	$345
domsg:			lda     #80
			sta     $348
			lda	#9
			sta	$342
			ldx     #0
			jsr     $E456
			rts                     	; return to DOS (or whatever)

help:			dc.b	0       
mesg:			dc.b	"R-Time 8 Clock "
			dc.b	<+128>,"Not Present"
			dc.b	$9B
insta:			dc.b	"R-Time 8 Clock is Present",$9b


;       Continue with initz...
;       ----------------------

cont1:			lda	# low insta
			sta	$344
			lda	# high insta
			sta	$345
			jsr	domsg
			rts


;       Read a data register from clock chip
;       ------------------------------------
; in:
;       X       = register number to read
; out:
;       A       = value of the register

read:			stx	.d+1
			sty	.c+1
			ldy	banks           	; get correct bank of OSS super cart.

			jsr	waitcl          	; a max of 255 passes for waiting
			CPY	#8
			BNE	.a
			stx	port            	; set address (AVOID ENABLING CART)
			lda	port            	; read high nibble
			ldx	port            	; X = low nibble
			clc
			bcc	.b
.a:			STX	PORT+1
			LDA	PORT+1
			LDX	PORT+1

.b:			and	#15
			sta	.temp
			asl
			asl
			adc	.temp
			asl              	; temp := port * 10
			sta	.temp
			txa			; add low nibble to result
			and	#15
			adc	.temp

			cpy	#16             ; current bank code of OSS cart.
			bcs	.c
			sta	bankd,y         ; restore to correct bank of OSS bank cart.
.c:			ldy	#0              ; restore value of Y
.d:			ldx	#0              ; restore value of X
			rts


.temp:			dc.b      0       	; temp for addition



;       Wait until clock chip is NOT busy (with timout)
;       -----------------------------------------------

waitcl:			lda    #-1		; wait for a NOT busy...
			pha
.a:			CPY    #8		; Y IS BANK DATA (FROM AFFF)
			BNE    .b
			lda    port
			BCS    .c		; JUMP ALWAYS (FROM CPY #8)
.b:			LDA    PORT+1
.c:			and    #15
			beq    .d
			pla
			sec
			sbc   #1
			pha
			bne   .a
.d:			pla
			rts

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


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

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

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




;----------------------------------------------
; This is a print utility to let
;what is going on.
;
;printsi Routeen
;this is a slow print to the screen;
;usage jsr printsi
;dc.b	$9b," string to be printed",$ff
;----------------------------------------------
;
; 
;================================= 
;       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 
;
;
jiffy:			LDA 	RTCLOK+2
.a:			CMP 	RTCLOK+2
			BEQ 	.a
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
show_computer:

			JSR 	printsi
			dc.b 	$9b,"Computer is: "
			dc.b 	-1

			ldx 	total_ram_banks
			CPX 	#4
			BCC 	L800xl
			BNE 	L130xe_192
			lda 	#01
			JSR 	printsi
			dc.b 	"128K - 130XE  (64K)",$9b
			dc.b 	-1

			rts
;
l800xl:			JSR 	printsi
			dc.b 	"800XL unmodified",$9b
			dc.b 	"---- No RAM Disk----",$9b
			dc.b 	-1
			RTS 
;
L130xe_192:		CPX 	#12
			BEQ 	l800xl_192
			BCS 	l320xe
			JSR 	printsi
			dc.b 	"192K - 130XE  (128K)",$9b
			dc.b 	-1

			rts
;
l800xl_192:
			JSR 	printsi
			dc.b 	"256K - 800/1200XL (192K)",$9b
			dc.b 	-1
			rts
;
l320xe:			CPX 	#16
			BNE 	l1200xl
			JSR 	printsi
			dc.b 	"320K - 130XE  (256K)",$9b
			dc.b 	-1
			rts
;
;
l1200xl:		CPX 	#28
			BNE 	l576kxe
			JSR 	printsi
			dc.b 	"1200xe -  (512K)",$9b
			dc.b 	-1
			rts
;
;	Gotta fix the 1 meg thing here
;
l576kxe:		cpx 	#32
			bne 	l1088kxe

			JSR 	printsi
			dc.b 	"576K - 130XE (512K)",$9b
			dc.b 	-1
			rts
l1088kxe:
			JSR 	printsi
			dc.b 	"1 meg - 130XE (1024K)",$9b
			dc.b 	-1
			RTS 

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;  help menu section
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help_menu:
			.include	notice

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


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

to_str:			ldy     #0
.tfe:			lda     (s_ptr),y               ;find end of string
			beq     .gotend
			cmp     #$00			;9b
			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


;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;   OS on and off ROUTEEN	      
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cleanup:		jsr	os_off
			lda	#$00
			sta	int.hardware
			sta	int.hardware+1
			sta	int.hardware+2
			sta	int.hardware+3
			jsr	os_on
			rts

;----------------------------------------------------------------
; 1= mio
; 2= Black Box
; 3= kpi
; 4= IDEa
;
pbi_type:		dc.b	0
;================================================================


win_end:		ds.b	0


;