;------------------------------------------------------------------------
;(Change the date in vtoc General DOS uttility for real.dos v1.0) 
;
;		Copyright 2005 Intergraded Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;                         Real.dos 
;		This program is to replace Bank_bb.COM with
;		      		bank_bb.com
;
;			Code History.
;
;Init design,   	10/15/2012	sjc
;
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;------------------------------------------------------------------------
; 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,"Bank_bb.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 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..crlf
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;;----------------------------
; pbi common Routines
;RAMPAGE:			equ	$D1BC
;CFG_PAGE:			equ	$F5
;MISC_PAGE:			equ	$FE
;DUMMYPAGE:			equ	$FF
;BB_Sensel:			equ	$D1C0
;PBIBANK:			equ	$D1FF
;PBISHADOW:			equ	$0248
;HDCODE_BANK:			equ	$04
;


			.include	equates
			.include	globals
			.include	macros
;
         		.org  $4000				;   Where the program Starts in memory.
;

win_start:
header_info:
			.include	header			; this reads info out of the header and displays to screen
								; let'S see if anything is on the parm 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	.a				; let'S see if they are playing with the consol
			iny
			jmp	mux_1a
.a:			jmp	info			; tell them how to use the file

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

.a:			lda	(comtab),y			; well got a byte
			jsr	upcase 				; let'S make sure it is upper case
			cmp	#$9b
			beq	.noparm
			cmp	#'D'
			beq	.got_d				; going to the help menu



			inx
			stx	.holdx
			iny
			jmp	.a

.holdx:			dc.b	0

.noparm:		jmp	info			;tell them how to use the file

.got_d:			iny
			lda	(comtab),y			; well got a byte
			cmp	#'1'-1
			bcc	.noparm
			cmp	#'9'+1
			bcs	.noparm
			sta	my_drive
			sbc	#48				; turn it into a hex #
			STA	Black_box_drv
			ldx	#$00
;			inx
			iny					;":"
			iny					;" "
			iny					;"1-9"
			lda	(comtab),y			;D9: 
			sta	partition_ascii,x
;			jsr	echo
			cmp	#$9b
			beq	.done
			cmp	#'0'-1
			bcc	.noparm
			cmp	#'9'+1
			bcs	.noparm
			iny
			inx
			lda	(comtab),y			;D9: 
			sta	partition_ascii,x
;			jsr	echo
			cmp	#$9b
			beq	.done
			cmp	#'0'-1
			bcc	.noparm
			cmp	#'9'+1
			bcs	.noparm
.done:
			jsr	get_number_cli
			sta	partition_number
			cmp	#96
			bcs	info
			cmp	#$00
			beq	info
;			lda	#$9b
;			jsr	echo
;			lda	partition_number
;			jsr	pr_byte
;
;			lda	#$9b
;			jsr	echo
;			lda	Black_box_drv
;			jsr	pr_byte

			jmp	start


.myhold:		dc.b	0			
;

info:
			JSR	printsi
			dc.b	$9b,$9b
			dc.b	<+128>,"Black Box all Types are Required!"
			dc.b	$9b
			dc.b	"Usage:",$9B
			dc.b	"Bank_bb Dn: <Partition>",$9B
			dc.b	"  Dn: n=1 to 9",$9b
			dc.b	"Partition = 1 to 96",$9b
			dc.b	$9b,$9b,$9b,-1
			rts



;-----------------------------
;CHANGES THE CHARTER TO UPCASE
;-----------------------------
upcase:
			cmp	#$61
			bcc	.a
			cmp	#$7a
			bcs	.a
			sbc	#$1f
.a:			rts



my_drive:		dc.b	0
;

Black_box_drv:			dc.b	0

partition_info:			ds.b	9		; name of partition
				ds.b	1		; bit 7 = Floppy
par_density:			ds.b	1		; Bit 5 = 1 dd else sd
				ds.b	4		; actual sector number in 512 mode
par_sect_size:			ds.w	1		; number of sector+1
				ds.b	1
				ds.b	1
;
my_drive_lo:
				dc.b	BBHD1
				dc.b	BBHD2
				dc.b	BBHD3
				dc.b	BBHD4
				dc.b	BBHD5
				dc.b	BBHD6
				dc.b	BBHD7
				dc.b	BBHD8
				dc.b	BBHD9

my_drive_high:
				dc.b	BBHD1 / 256
				dc.b	BBHD2 / 256
				dc.b	BBHD3 / 256
				dc.b	BBHD4 / 256
				dc.b	BBHD5 / 256
				dc.b	BBHD6 / 256
				dc.b	BBHD7 / 256
				dc.b	BBHD8 / 256
				dc.b	BBHD9 / 256

;
;
;       gets a CARD value - returned in A/X
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

get_number_cli:
                	mea    partition_ascii,inbuff
                	lda     #0			;1
                	sta     cix
                	jsr     afp                     ;ascii --> floating pt.
                	bcs     .default
                	jsr     fpi                     ;floating --> integer
                	bcs     .default
                	lda     fr0
                	ldx     fr0+1
                	rts
.default:       	lda     #0
                	tax
                	rts

.work:          	ds.b    20


partition_ascii:		dc.b	0,$9b,$9b,$9b,$9b,$9b
partition_number:		dc.b	0
;
start:
				jsr	black_box
				lda	pbi_type
				cmp	#$02
				beq	.a
				jmp	info
.a:
	
				ldy	Black_box_drv
				lda	my_drive_lo,y
				sta	bb_set_drv+1
				lda	my_drive_high,y
				sta	bb_set_drv+2

;
				LDA	partition_number	;map in partition 20
;
SET_PART:
				CLC 
				ADC	#$0F    	;the BB numbers them $10-$6f
				LDX	#MISC_PAGE 	;set ram window for miscellaneous
				STX	RAMPAGE
				STA	$D653   	;we needed to set RAMPAGE FIRST, else this
;				     			;store might have ended up in the wrong bank
				LDA	#HDCODE_BANK 	;bank for the hd low level code
				STA	PBISHADOW 	; store in shadow reg first
				STA	PBIBANK 	;puts the BB's HD code into $d800-$dfff
				JSR	$D82C   	;** Address for Read Partition Subroutine **
				LDA	#0
				STA	PBISHADOW
				STA	PBIBANK
				BCS	SET_ERROR
				LDA	$D656   	;if bit 7(minus flag)is set, then
				BMI	SET_ERROR 	;partition not used
				LDY	#0      	;prepare to copy the bytes
SET_1:				LDX	#MISC_PAGE 	;get one byte from the
				STX	RAMPAGE 	;partition data buffer in the
				LDA	$D656,Y 	;miscellaneous page
				LDX	#CFG_PAGE 	;now put the byte in the main
				STX	RAMPAGE 	;configuration, in the config page
bb_set_drv:
				STA	$ffff,Y 	;$d67e is the first byte for D8:
				sta	partition_info,y
				INY 
				CPY	#18     	; copy 18 bytes this way
				BCC	SET_1
				CLC         		;We've completed with success!
				BCC	SET_2		;skip the set carry
SET_ERROR:			SEC			;set the carry upon an error
SET_2:				LDA	#DUMMYPAGE	;return with the dummy page
				STA	RAMPAGE

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

				lda	#'"'
				jsr	echo
				lda     partition_info
				cmp	#$00
				beq	.n2
				jsr	echo
.n2:
				lda     partition_info+1
				cmp	#$00
				beq	.n3
				jsr	echo
.n3:
				lda     partition_info+2
				cmp	#$00
				beq	.n4
				jsr	echo
.n4:
				lda     partition_info+3
				cmp	#$00
				beq	.n5
				jsr	echo

.n5:
				lda     partition_info+4
				cmp	#$00
				beq	.n6
				jsr	echo

.n6:
				lda     partition_info+5
				cmp	#$00
				beq	.n7
				jsr	echo

.n7:
				lda     partition_info+6
				cmp	#$00
				beq	.n8
				jsr	echo

.n8:
				lda     partition_info+7
				cmp	#$00
				beq	.n9
				jsr	echo

.n9:
				lda     partition_info+8
				cmp	#$00
				beq	.na
				jsr	echo
.na:
				lda	#'"'
				jsr	echo

				jsr	printsi
				dc.b	" Mapped to D",-1
				lda	my_drive
				jsr	echo
				jsr	printsi
				dc.b	":",$9b,$9b,-1
				

				RTS			; and exit
;
; 
;================================= 
;       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	$347
		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 
;
;
;  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	#$02
			sta	pbi_type

;
.d
			CLD
			LDa	#0
			STa	BB_Sensel
			CLI
			RTS
;
.e			dc.b	$D0,$08,$29,$00
; 
;----------------------------------------------------------------
; 1= mio
; 2= Black Box
; 3= kpi
; 4= IDEa
;
pbi_type:		dc.b	0
;================================================================
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Prints a byte or a card to the screen
;
;
pr_byte:
			ldx	#$00
pr_card:
			sta	fr0
			stx	fr0+1
			lda	#$00
			sta	cix
			jsr	ifp
			jsr	fasc
			ldy	#$00
.aprbloop:
			lda	(inbuff),y
			php
			and	#$7f
			iny
			jsr	echo
			plp
			bpl	.aprbloop
			rts


; 
win_end:		ds.b	0
;
