;----------------------------------------------------------------
;		Copyright 2012 Integrated Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;----------------------------------------------------------------
;                              sio2ide.com
;
;This is a program is for testing a detection routeen
;
;
;****************************************************************
; Copyright (C), 2001 MMSoft, All rights reserved
;****************************************************************
;Parts of this file are copyright MMSoft & ILS
;****************************************************************
;
; SOURCE FILE: SIOSRV.C
;
; MODULE NAME: SIOSRV
;
; PURPOSE:     SIO service.
;
; AUTHOR:      Marek Mikolajewski (MM)
;
; convert from c to assemble BY:Stephen J. Carden
;
; HISTORY:     Ver   Date       Sign   Description
;
;              001   24-01-2002 MM     Created
;
;		init	9/10/2012	sjc new design
;Converted c code to assembler and Added the sio2ide info 
;that Marek Mikolajewski sent me
;
;----------------------------------------------------------------

com_file_name:		.macro
;			dc.b   13,"filename.ext",$9b
			dc.b   12,"SIO2IDE.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   12
;----------------------------------------------------------------
; 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
;----------------------------------------------------------------
;	SIOV commands that SIO2IDE use
;
;      SIO commands accepted by SIO2IDE FS
;
pgm_sio2ide:	equ	$71		; Program sio2ide internal commands
SIOC_NONE:	equ	$00		;
SIOC_GCFG:	equ	$4E		; Fixed Config
SIOC_PCFG:	equ	$4F		; Dummy Only ACK
SIOC_STAT:	equ	$53		; Fixed Status
SIOC_PUT:	equ	$50		; Put data
SIOC_PUTV:	equ	$57		; Put data with verify
SIOC_GET:	equ	$52		; Get data
SIOC_FORMAT:	equ	$21		; Format (always OK)
SIOC_HS:	equ	$3F		; ? US Doubler
;
SIOC_GETDSK:	equ	$11		; Get the Disk description (aux1)
SIOC_PUTDSK:	equ	$12		; Put the Disk description (aux1)
SIOC_FATR:	equ	$13		; Get First ATR file
SIOC_NATR:	equ	$14		; Get Next ATR file
SIOC_OFFDSK:	equ	$15		; Switch Off selected Disk (aux1)
SIOC_SCFG:	equ	$16		; Save Config file to HDD
SIOC_GETDRV:	equ	$17		; Get Drive parameters
SIOC_GETFSI:	equ	$18		; Get FS info
SIOC_FDIR:	equ	$19		; Get First Dir entry (from current dir)
SIOC_NDIR:	equ	$1A		; Get Next Dir entry (from current dir)
SIOC_SCDIR:	equ	$1B		; Set Current working Dir
SIOC_GCDIR:	equ	$1C		; Get Current working Dir
SIOC_ICDIR:	equ	$1D		; Initialise Current working Dir
;
SIOC_SSECT:	equ	$61		; Set Phisical Disk Sector Number or File Start Sect for SIOC_GLFN command
;
SIOC_GSECT:	equ	$62		; Get Phisical Disk Sector
SIOC_PSECT:	equ	$63		; Put Phisical Disk Sector
SIOC_GLFN:	equ	$64		; Get Long File Name (first use SIOC_SSECT)
SIOC_CDRV:	equ	$65		; Change Current Drive ($01=Master, $02=Slave)
;
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_disk2:	equ	2		; Emulated Disk 2
ide_disk3:	equ	3		; Emulated Disk 3
ide_disk4:	equ	4		; Emulated Disk 4
ide_disk5:	equ	5		; Emulated Disk 5
ide_disk6:	equ	6		; Emulated Disk 6
ide_disk7:	equ	7		; Emulated Disk 7
ide_disk8:	equ	8		; Emulated Disk 8
;
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  $4000

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

;
Start:

get_sector_count:
			LDA 	COMTAB  		; Grabbing Comtab calc address of SIO 
			SEC 
			SBC 	# low lsio		; Subtracting 10 which is our patched siov
			STA 	XSIO+1			; Storing the results into a location we can use
			LDA 	COMTAB+1		; now for the high byte
			SBC 	# high lsio		; Subtracting 10 for out siov
			STA 	XSIO+2			; storing it into a location we can use easy
			lda	#$6c			; Hex equ for Jmp
			sta	xsio			; We have created a "jmp (comtab-10)"


sio2ide:
			lda	#ide_master
			sta	$301			; DUnit
			jsr	get_sio2ide

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

.a:			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
			bne	.bb

.aa:			jsr	printsi
			dc.b	"Sio2ide "
			dc.b	<+128>,"Slave"
			dc.b	" Detected!",$9b
			dc.b	-1
.bb:			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





XSIO:			jMP	$ffff

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Working buffer
;
Data_buffer:
			ds.b	1024


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








win_end:		ds.b	0
;
;
;