;------------------------------------------------------------------------
;
;		Copyright 2012 Intergraded Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;        title   'Black box dir builder'
; This program looks at your partition table. Loads the pertition to the drive number 
; you select and then builds the directory with the name it pulls out of the partition 
; table.
;
;------------------------------------------------------------------------
;                              dirbuild.s
;
;       ------------------------------------------------
;------------------------------------------------------------------------
; 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
;------------------------------------------------------------------------
; macros
mea:            		.macro  field,ptr
				lda     #low %1
				sta     %2
				lda     #high %1
				sta     %2 + 1
				.endm

my_companyid:		.macro
			dc.b	24,"Integrated Logic Systems",$9b,-1
			.endm

; this can be the name of a company or user
Author_name:		.macro		; always 31 bytes
			dc.b	17
			dc.b	"Stephen J. Carden"
			ds.b	13
			.endm

register_user:		.macro		; always 31 bytes
			ds.b	31
			.endm
;============================================================================
;  Relocator Vars
;
;
rettad:  			equ	0       	; relocater table address
orgadr:  			equ	2       	; originate address
dorgadr:  			equ	4       	; destination org address
blkadr:  			equ	6       	; address of block
blkbyt:  			equ	8       	; bytes in block
blkdes:  			equ	10      	; destinatin of block
seglen:  			equ	12      	; Length of segment descriptor

segaddress:  			equ	14		; this is the word location adjust memory
segmove:  			equ	15		; turns the memory mover on or off

seg_on:  			equ	0		; do relocation of data
seg_off:  			equ	$ff		; do not relocatate file but calculate offset

t_off1:				equ	Split-$3000	; had to do this since there is no .set 6

;------------------------------------------------------------------------
;LSIO			[COMTAB-10]
;This location contains the address of the SIO routine SpartaDos uses. 
;This is actually a vector, so U may replace this address with your own. 
;The ramdisk patches in here to trap access to the drive it is emulating. 
;Many commands use this vector to run the dos's high speed SIO routine.
LSIO:				equ	10
;------------------------------------------------------------------------
;ECHOFLG		[COMTAB-8]
;This location contains the index into HATABS <table of handler IDs and 
;addresses> of the file SpartaDos is echoing output to. A value of $FF 
;indicates that echoing is inactive. This Location Is Valid Only While 
;Running Under SpartaDos 2.x
ECHOFLG:			equ	8
;------------------------------------------------------------------------
;BATFLG			[COMTAB-6]
;This location contains the index into HATABS <table to handler IDs and 
;addresses> of the file SpartaDos is receiving input from. A value of $FF
;indicates that no batch file is active. This Location Is Valid Only 
;While Running Under SpartaDos 2.x
BATFLG:				equ	6
;------------------------------------------------------------------------
;WRTCMD			[COMTAB-2]
;This location contains the SIO write command. A W is the write with 
;verify command, & P is the write with no verify command. This Location 
;Is Valid Only While Running Under SpartaDos 2.x.
WRTCMD:				equ	2

;------------------------------------------------------------------------
;WARMST			[COMTAB-1]
;This flag, if set, indicates that the command processor is doing a 
;cold start. It is cleared <to 0> whenever the command processor is 
;entered. It is used to trap errors when trying to open STARTUP.BAT
;or AUTORUN.SYS.
WARMST:				equ	1
;------------------------------------------------------------------------
;COMTAB			[COMTAB]
;This location contains a 6502 jump instruction 2 the command processor. 
;BASIC enters here on a dos command.
comtab:				equ	$0a
;------------------------------------------------------------------------
;ZCRNAME		[COMTAB+3]
;This location contains a 6502 jump instruction 2 the filename crunch 
;routine <CRNAME>. This is used by most external dos commands 2 fetch 
;the next filename on the command line. The command line is at LBUF & 
;the crunched filename ends up at COMFNAM. This routine supplies the 
;default drive number if necessary. The zero flag on return is SET 
;if n filename is on the command line. Each call returns the next 
;filename on the command line.
ZCRNAME:			equ	3
;------------------------------------------------------------------------
;ZDIVIO			[COMTAB+6]
;This location contains the address of the divert input/output <redirection
; of I/O> routine. From an assembly language program, U may call the routine
;through an indirect jump to ZDIVIO with the filename at COMFNAM and the 
;Y register equal to 0 if output <PRINT>, or 1 if input <-fname>.
ZDIVIO:				equ	6
;------------------------------------------------------------------------
;ZXDIVIO		[COMTAB+8]
;This location contains the address of the stop divert input/output 
;routine. From an assembly language program, U may call the routine 
;through an indirect jump to ZXDIVIO with the Y register equal to 0 if 
;stopping output <PRINT>, or 1 if stopping input <force end of file>.
ZXDIVIO:			equ	8
;------------------------------------------------------------------------
;BUFOFF			[COMTAB+10]
;This location contains the current offset into the command line. 
;CRNAME uses this pointer to fetch the next parameter on the 
;command line <at LBUF> & move it to COMFNAM.
BUFOFF:				equ	10

;------------------------------------------------------------------------
;ZORIG			[COMTAB+11]
;This location contains the start address of SpartaDos. $600 is the 
;start address of SPEED.DOS, STANDARD.DOS and $700 is the start address 
;of all other vectors.
ZORIG:				equ	11
;------------------------------------------------------------------------
;DATER		[COMTAB+13]
;This location contains the current date in DD/MM/YY format <3 bytes>. 
;This is the date that sparta inserts in the directory whenever a new file 
;or directory is created. To override this, see TDOVER.
dater:				equ	13
;------------------------------------------------------------------------
;TIMER		[COMTAB+16]
;This location contains the current time in HH/MM/SS format <3 bytes, Not 
;in BCD format, therefore it can be read with no conversion from BASIC>. 
;This is the time that sparta inserts in the directory whenever a new file 
;or directory is created. To override this, see TDOVER.
timer:				equ	16
;------------------------------------------------------------------------
;ODATER		[COMTAB+19]
;This location contains the alternate date in DD/MM/YY format <3 bytes>.s 
;Sparta uses this date instead of DATER if the TDOVER flag is set.
odater:				equ	19
;------------------------------------------------------------------------
;OTIMER		[COMTAB+22]
;This location contains the alternate time in HH/MM/SS format <3 bytes>. 
;Sparta uses this time instead of TIMER if the TDOVER flag is set.
otimer:				equ	22
;------------------------------------------------------------------------
;TDOVER		[COMTAB+25]
;This location contains the time/date override flag. It is set to 0 if 
;to use DATER and TIMER when it creates new files, and set to $FF if to 
;use ODATER and OTIMER. This is used by file copy programs <such as 
;SPCOPY/MENU> to insure that the time and date of each file is preserved.
tdover:				equ	22
;------------------------------------------------------------------------
;TRUN		[COMTAB+26]
;This location contains the RUN address of a load file. This location 
;is updated during the internal load operation, so BASIC or any other 
;program may check what the load address was. RUNLOC is updated from this
;location by the command processor only.
TRUN:				equ	26 
;------------------------------------------------------------------------
;SBUFF		[COMTAB+28] 
; This is the start address of Sparta DOS sector buffers.
sbuff:				equ	28 
;------------------------------------------------------------------------
;DDENT		[COMTAB+29]
;This location contains the density <sector size> of each drive <4 in 
;all__sparta 1.x only supports 4 drives.> A value of 0 indicates 256 byte
;sectors, and a 128 indicates 128 byte sectors. This Table Is Valid Only 
;For Version 1.x sparta. The DDENT table is inaccessible if U are using 
;sparta 2.x.
DDENT:				equ	29
;------------------------------------------------------------------------
;SMEMLO		[COMTAB+30] 
;This is the top of SpartaDOS low memory. Handlers added since boot take
;up the memory between SMEMLO and MEMLO.
smemlo:				equ	30
;------------------------------------------------------------------------
;INCOMND	[COMTAB+32)
;A -1 here indicates that we are in the command processor (entering 
;commands, etc.). A 0 indicates that we are in BASIC or some cartridge 
;program. This is used by the initialization routine to determine whether 
;to enter the command processor or not. 
incomnd:			equ	32

;------------------------------------------------------------------------
;COMFNAM	[COMTAB+33]
;This is the buffer for the output of the ZCRNAME routine. 
;It is a 28 byte long buffer and ALWAYS begins in the form dn: so if you 
;are only looking for parameters, u may start looking at COMFNAM+3.
COMFNAM:			equ	33
;------------------------------------------------------------------------
;RUNLOC		[COMTAB+61]
;This location contains the run address of a .COM file when it is loaded 
;through the command processor <as a command>. If no address is specified
;after the RUN command, this is the address too be run.
RUNLOC:				equ	61
;------------------------------------------------------------------------
;LBUF			[COMTAB+63]
;This location contains the input buffer. This is where the command line is 
;stored. LBUF is 64 bytes in length.
LBUF:				equ	63	; lbuf offset from comtab
;------------------------------------------------------------------------
;
;	READ THIS BEFORE USING VECTORS
;
;SpartaDOS 3.3 and Real.dos contains many vectors pertaining to the 
;setting, reading, displaying of the time and date, executing command: 
;lines, initializing the system and many more. These vectors 
;are : contained in the RAM under the operating system starting at address.
; $FFCO. They may be accessed by the following Instructions:
;
;			lda $0301 	; PIA 
;			pha 		; save old value of port on stack 
;			and #$FE 	; set bit 0 to OFF 1
;			sta $0301 	; enable RAM under the OS 
;			jsr VGETTO 	; call routing at $FFCO via 
;			pla
;			sta $0301 	; restore the port (enable OS) 
;
;These functions each contain a jump (JMP) instruction to the appropriate 
;function. If a function is not initially supported (as in TDON), the vector
;will contain a SEC and RTS instruction rather I. than a JMP. The following 
;vectors are currently supported: 
;
;
;------------------------------------------------------------------------
;euro_time
;this is a one byte location that hold a '50' for pal machines
;this is a one byte location that hold a '60' for ntsc machines
euro_time:			equ	$ff91
;------------------------------------------------------------------------
;vgettd ($FFCO) 
;This function returns the current time/date at TIMER and DATER 
;(locations at COMTAB-see page 110 of the Sparta DOS manual). 
;On return, the carry is SET if the function failed. When a file is 
;opened for write, Sparta DOS makes a call here to update TIMER/DATER
; so it can move this data into the directory entry. Also the TIME and
; DATE internal commands make calls here to get the current time. TDLlNE
; and ZHAND also use this vector. 
vgettd:				equ	$ffc0

;------------------------------------------------------------------------
;VSETTD ($FFC3) 
;This function sets the time/date. On entry, the new time and date are at 
;TIMER and DATER (locations at COMTAB-see page 110 of the SpartaDOS manual). 
;On return, the carry is SET if the function failed. This vector is used by 
;the commands TIME, DATE, and the :a; ZHAND set functions. 
vsettd				equ	$ffc3

;------------------------------------------------------------------------
;VTDON ($FFC6) 
;This function turns the time/date display line on or off. On entry, 
;the Y register contains 0 if to turn the line off, or 1 if to turn the 
;line on. On return, the carry is SET if the function failed. This function 
;is not supported internally by SpartaDOS. The TDLlNE handler patches . 
;into this vector for use by the TD command along with the ZHAND functions 
;38 and 39. (See the TDLlNE command.) 
vtdon:				equ	$ffc6

;------------------------------------------------------------------------
;VFMTTD ($FFC9) 
;This function returns the formatted time/date line into ' a user supplied 
;buffer. On entry, the X and Y registers 1 contain the high and low byte of
;the buffer address respectively. On exit, the carry is SET if the function 
;failed. This function is not supported internally by SpartaDOS. 
;The TDLINE handler patches into this vector for use by TDLINE and 
;ZHAND routines. (See the TDLINE command.) 
VFMTTD:				equ	$FFC9 

;------------------------------------------------------------------------
;VINITZ ($FFCC) 
;This vector is called after SpartaDOS has finished initializing itself 
;after a RESET occurs. The command AUTOBAT patches into this vector to start
;a batch file right after RESET. (By initialization, we mean as a result of 
;making a call through DOSINI-e.g. JMP (DOSINI). The OS monitor routine 
;calls this vector before it enters a cartridge or DOS.) 
VINITZ:				equ	$FFCC

;------------------------------------------------------------------------
;VINITZ2 ($FFCF) 
;This vector is called after SpartaDOS has finished initializing itself 
;after a NON-RESET occurs. Several SpartaDOS commands (such as SPCOPY and 
;UNERASE) will initialize DOS before and after they perform their function. 
;They do this by jumping through the DOSINI vector as does the OS monitor 
;routine after a RESET. 
VINITZ2:			equ	$FFCF 

;------------------------------------------------------------------------
;VXCOMLI ($FFD2) 
;This vector calls the command processor to execute a command line given 
;at LBUF. BUFOFF should be 0 on entry. Any errors that may occur as a result
;of executing the command line shall be printed as usual. No prompts are
;printed before or after command execution. This is the method that a future
;DUP.SYS for Sparta DOS could use to perform its functions. 
vxcomli:			equ	$ffd2

;------------------------------------------------------------------------~
;VCOMND ($FFD5) 
;This vector calls the main command processor program entry point. You may 
;patch into this vector if you wish to supply your own command processor. 
;(The 'I current one simply prints the prompt, inputs a line, : 
;calls VXCOMLI, and jumps back to the beginning.) This is the method DUP.SYS
;uses to gain entry from a DOS command in BASIC.
VCOMND:				equ	$FFD5
;------------------------------------------------------------------------~
;VPRINT ($FFD8) 
;This vector points to the Sparta DOS general print routine. The calling 
;method is:
;
;I use a similar routein   Jsr printsi
;
;				JSR	VPRINT 
;				dc.b	"This is a message",$9B,-1
;
;
VPRINT:				equ	$FFD8 
;------------------------------------------------------------------------~
;VKEYON ($FFDB) 
;This function turns the keyboard buffer on or off. On entry, the Y register
;contains 0 if to turn the buffer off, or 1 if to turn the buffer on. This 
;function is supported internally by SpartaDOS.
;
VKEYON:				equ	$FFDB 
;
;
;------------------------------------------------------------------------
;tsr ($fff5)
;this location registers its presents of relocatable file(s). doing this 
;should keep the relocatable file from running twice, or steping on the
;presents of another file. example wedge uses 4 banks of ram, you would
;not want the ramdisk driver allocating thoes banks for the ramdisk handler
;
Ramdisk:	equ	1		;done! ramdisk installed
tdline:		equ	2		;done! tdline installed
wedge: 		equ	3		;done! wedge installed
Ram_ex:		equ	4		;done! Ramdisk - 64k Excluded installed
iomon:		equ	5		;done!
APE_HND:	equ	6		;done!
PRC_hnd	 	equ	7		;done!
RS232:		equ	8		;done!
Rverter: 	equ	9		;done! this is already included in the code
MUXTIME:	equ	10		;done!
Turboss:	equ	11		;done!
;		equ	12
; 		equ	13
;		equ	14
;	 	equ	15
;	 	equ	16
;	 	equ	17
;	 	equ	18
;		equ	19
;		equ	20
;		equ	21
;		equ	22
dis_bat:	equ	23		;This marks the system that dis_bat has run.
ver.Real:	equ	24		;Done! Sparta.com sets this flag
prokey:	 	equ	25		;done!
gdevice: 	equ	26		;done!
dosmenu: 	equ	27		;done!
hyp_r:	 	equ	28		;done!
shutdown:	equ	29		;done!
Ape_present:	equ	30		;done! Ape R Device present!
r_handlr:	equ	31		;
p_handlr:	equ	32
;
tsr:		equ	$fff5

;------------------------------------------------------------------------
;
;		Floating point math
ifp:  				equ	$d9aa
cix:				equ	$f2
inbuff:				equ	$f3
fr0:				equ	$d4
fr1:				equ	$e0
afp:				equ	$d800
fasc:				equ	$d8e6
fpi:				equ	$d9d2
fmove:				equ	$ddb6

fsub:				equ	$da60
fadd:				equ	$da66
fmul:				equ	$dadb
fdiv:				equ	$db28
zfr0:				equ	$da44


Portb:				equ	$d301

;
;===================================================================
;        system declarations 
;==================================== 
; 
;       ************************************** 
;       * SpartaDOS extrinsic data variables * 
;       ************************************** 
; 
;SPTR:     			equ	$43     ; Pointer to sector within buffer or memory 
ADDR:     			equ	$45     ; Pointer to data for data transfers 
RSLW:     			equ	$48
CENTRY:   			equ	$49
; 
;       ******************************** 
;       * Misc. ATARI system variables * 
;       ******************************** 
; 
COLDST:   			equ	$0244   ; cold start flag, <>0 during cold start 
RUNAD:    			equ	$02E0   ; binray load run address 
INITAD:   			equ	$02E2   ; binary laod init address 
MEMHI:    			equ	$02E5   ; Top of memory pointer 
MEMLO:    			equ	$02E7   ; Low memory pointer for applications after 

;                       ;  DOS initialization 
; 
;       DOS init and run vectors 
;       ------------------------ 
; 
i_WARMST:			equ	$08     ; system in warm start flag 
BOOTQ:				equ	$09     ; Successful boot flag 
DOSINI:				equ	$0C     ; Vector for DS initialization 
zwptr:				equ	$D7
; 
; 
; 
;       ****************************** 
;       * ATARI SIO system interface *  
;       ****************************** 
;

DDEVIC:				equ	$0300   ; Unit 1 bus ID number 
DUNIT:				equ	$0301   ; Unit number 
DCOMND:				equ	$0302   ; Bus command 
DSTATS:				equ	$0303   ; Command type/status return 
DBUFLO:				equ	$0304   ; Data buffer pointer 
DBUFHI:				equ	$0305
DTIMLO:				equ	$0306   ; Device time out in 1 second units 
DBYTLO:				equ	$0308   ; Number of bytes to transfer 
DBYTHI:				equ	$0309
SECTOR:				equ	$030A   ; Sector number (for disk) 
SIO:   				equ	$E459   ; SIO interface 
; 
; 
;       ****************************** 
;       * ATARI CIO system interface *  
;       ****************************** 
; 
;       Zero page IOCB declaration 
;       -------------------------- 
; 
ICHIDZ:				equ	$20     ; Handler index number (ff = iocb free) 
ICDNOZ:				equ	$21     ; Device number (drive number) 
ICCOMZ:				equ	$22     ; Command code 
ICSTAZ:				equ	$23     ; Status of last iocb action 
ICBALZ:				equ	$24     ; Buffer address 
ICBAHZ:				equ	$25
ICPTLZ:				equ	$26     ; Put byte routine address - 1 
ICPTHZ:				equ	$27
ICBLLZ:				equ	$28     ; Buffer length 
ICBLHZ:				equ	$29
ICAX1Z:				equ	$2A     ; Auxiliary information 
ICAX2Z:				equ	$2B
ICAX3Z:				equ	$2C
ICIDNO:				equ	$2E     ; iocb number * 16 
ICCHAR:				equ	$2F     ; Character byte for current operation 
s_ptr:				equ	$a5             ;work
d_ptr:				equ	$a6             ; pointers
HATABS:				equ	$031A   ; Device handler address table 
; 
;       I/O control blocks - main 
;       ------------------------- 
; 
ICHID:				equ	$0340   ; Handler index number (ff = iocb free) 
ICDNO:				equ	$0341   ; Device number (drive number) 
ICCOM:				equ	$0342   ; Command code 
ICSTA:				equ	$0343   ; Status of last iocb action 
ICBAL:				equ	$0344   ; Buffer address 
ICBAH:				equ	$0345
ICPTL:				equ	$0346   ; Put byte routine address - 1 
ICPTH:				equ	$0347
ICBLL:				equ	$0348   ; Buffer length 
ICBLH:				equ	$0349
ICAX1:				equ	$034A   ; Auxiliary information 
ICAX2:				equ	$034B
ICAX3:				equ	$034C   ; Position bytes 
CIO:				equ	$E456
; 
;       Command codes for IOCB 
;       ---------------------- 
; 
CCOPEN:				equ	3       ; Open for input/output 
CCGTRC:				equ	5       ; Get record (text) 
CCGTCH:				equ	7       ; Get character 
CCPURC:				equ	9       ; Put record (text) 
CCPUCH:				equ	11      ; Put character 
CCCLOS:				equ	12      ; Close IOCB 
CCSTAT:				equ	13      ; IOCB status request 
CCSPEC:				equ	14      ; IOCB special entry commands 
; 
;       Special entry commands for disk 
;       ------------------------------- 
; 
SCRENA:				equ	32      ; Rename file 
SCDELE:				equ	33      ; Delete file 
SCLOCK:				equ	34      ; Lock diskette (write protect) 
SCPROT:				equ	35      ; Protect file to read only 
SCUNPR:				equ	36      ; Unprotect file 
SCPONT:				equ	37      ; Point (position into file) 
SCNOTE:				equ	38      ; Note (get position in file) 
SCLEN:				equ	39      ; Get file length 
SCLOAD:				equ	40      ; Load binary file 
SCSAVE:				equ	41      ; Save binary file 
SCCRED:				equ	42      ; Create directory 
SCDELD:				equ	43      ; Delete directory 
SCCWD: 				equ	44      ; Change working directory 
SCBOOT:				equ	45      ; Set new boot program 
SCUNLO:				equ	46      ; Unlock diskette (unwrite protect) 
SCCHKD:				equ	47      ; Check diskette 
SCQDIR:				equ	48      ; Get current pathname 
SCFORM:				equ	254     ; Format diskette 
SCMAXX:				equ	48      ; last command number 
; 
; 
;       Format of data returned from 'scchkd' 
;       ------------------------------------- 
; notes: 
;       This data returned in a buffer pointed to by 
;       icbl (data length normally). The icba (buffer address) 
;       points to the 'Dn:' like normal. The following is the 
;       format of the data returned (start address is icbl). 
; 
CDVER:				equ	0       ; version of disk 
CDDEN:				equ	1       ; density 
CDTOT:				equ	2       ; number of sectors (total) 
CDFRE:				equ	4       ; number of free sectors 
CDVOL:				equ	6       ; volume name (spartaDOS only) 
CDSEQ:				equ	14      ; sequence # (SpartaDOS 2.0 & above) 
CDRAN:				equ	15      ; random # (SpartaDOS 2.0 & above) 
CDWRT:				equ	16      ; write lock (SpartaDOS 2.0 & above) 
; 
; 
;       CIO open type flags (aux1) 
;       -------------------------- 
; 
OMAPP:				equ	$01     ;   %00000001       ; Open for append 
OMFMT:				equ	$02     ;   %00000010       ; Open for formatted directory read 
OMREA:				equ	$04     ;   %00000100       ; Open for read 
OMWRT:				equ	$08     ;   %00001000       ; Open for write 
OMDIR:				equ	$10     ;   %00010000       ; Open directory (unformatted) 
OMSUB:				equ	$20     ;   %00100000       ; Open sub directory/main directory 
; 
;       ************************* 
;       * SpartaDOS error codes * 
;       ************************* 
; 
BREAKE:				equ	$80     ; Break abort 
IOCBN:				equ	$81     ; IOCB already open 
NODEVI:				equ	$82     ; Nonexistent device 
NOREAD:				equ	$83     ; File/IOCB not open for read 
BADCOME:			equ	$84     ; Bad IOCB command (NO MSG) 
NOPER:				equ	$85     ; Channel/IOCB not open 
BADIOCB:			equ	$86     ; Bad IOCB/channel number (NO MSG) 
NOWRITE:			equ	$87     ; File/IOCB not open for write 
EOFER:				equ	$88     ; Atari EOF error message (NO MSG) 
TRUNCR:				equ	$89     ; Truncated record error 
NODRIVE:			equ	$8A     ; drive gone (timeout) 
NACK:				equ	$8B     ; drive did not acknowledge 
SFRMER:				equ	$8C     ; serial frame error (NO MSG) 
CSRRNG:				equ	$8D     ; cursor out of range error (NO MSG) 
OVRRUN:				equ	$8E     ; Serial bus overrun error (NO MSG) 
CHKSER:				equ	$8F     ; Checksum error (NO MSG) 
BADSEC:				equ	$90     ; Bad sector/write protected 
ILLSCR:				equ	$91     ; Illegal graphics screen r=est (NO MSG) 
IOCBCR:				equ	$92     ; IOCB handler does not contain function (NO MSG) 
NORAMS:				equ	$93     ; Not enough ram for screen mode (NO MSG) 
NSPDOS:				equ	$94     ; Not a spartaDOS diskette 
;rev20:  = $95     ; Diskette not version 2.0 or better 
NODIR:				equ	$96     ; Directory not found 
FILEX:				equ	$97     ; File exists 
NBINF:				equ	$98     ; Not binary file error 
LOGON:				equ	$99     ; Logon message 
;       =$9A 
;       =$9B 
;       =$9C 
;       =$9D 
;       =$9E 
;       =$9F 
DRVER:				equ	$A0     ; Drive number error 
;       =$A1     ; Too many channels open 
FULLER:				equ	$A2     ; Disk full 
ILWCAR:				equ	$A3     ; Illegal wildcard in filename 
ERAPRO:				equ	$A4     ; File erase protected 
FNAMERR:			equ	$A5     ; File name error 
RNGER:				equ	$A6     ; Position range error 
CANTDEL:			equ	$A7     ; Cannot delete directory 
INVALC:				equ	$A8     ; Illegal DOS function/ not implemented 
WRTLOK:				equ	$A9     ; Diskette is write locked 
NOFOU:				equ	$AA     ; File not found 
;         
; 
; 
; 
;       ****************************************** 
;       * SpartaDOS database offset declarations * 
;       ****************************************** 
; 
;       Sector map descriptor 
;       --------------------- 
; 
SMNL:				equ	0       ; Sector number of next sector map sector. 
SMLL:				equ	2       ; Sector number of last sector map sector. 
;                       			;  zero implies this is the first. 
SMSN:				equ	4       ; Start of sector list, an entry of zero 
;                       			;  means an empty sector (zeros). 
; 
;       Directory entry descriptor 
;       -------------------------- 
MDD:				equ	9       ; Offset in boot sector to find desmap & denbyt 
MDOFF:				equ	0       ;  This describes the main directory file 
;                       			;  when in the boot sector 
; 
DESTA:				equ	0       ; Statut of the director entry bit are as 
;                       			;  follows: 
DESSUB:				equ	OMSUB   ;    a sub directory entry 
DESUSE:				equ	$08     ;    directory entry is in use. 
DESDEL:				equ	$10     ;    entry is deleted and free 
DESPRO:				equ	$01     ;    entry is erase protected 
DESARC:				equ	$02     ;    entry is archived 
DESHID:				equ	$04     ;    endty is hidden 
; 
DESMAP:				equ	1       ; Sector number of first sector map 
DENBYT:				equ	3       ; Number of bytes in file 
DEFNAM:				equ	6       ; File name (8 bytes), space filled 
DEEXT:				equ	14      ; File name extension (3 bytes), space filled 
DEDAT:				equ	17      ; Date of creation (mm/dd/yy). 
DETIM:				equ	20      ; Time of creation/update (hh/mm/ss). 
; 
; 
; lable below was dex  but now is ddex to keep from 
; confusing the compiler 
DDEX:				equ	23      ; Length of directory entry 
; 
; 
;       Drive/diskette description block 
;       -------------------------------- 
; 
BMD:				equ	MDD+2   ; Offset in boot sector to find this record 
SDOFF:				equ	2       ; Offset to drive/diskette description 
SDTOT:				equ	0       ; Total number of sectors usable on disk 
SDFRE:				equ	2       ; Number of free sectors on disk. 
SDQBM:				equ	4       ; Number of bit map sectors 
SDBMS:				equ	5       ; First bit map sector on disk 
SDALLOC:			equ	7       ; Sector number to begin file allocation search 
SDDIR:				equ	9       ; Sector number to begin dir allocation search 
SDVOL:				equ	11      ; Volume name 
SDX:				equ	19      ; Length of descriptor 
; 
;       Drive setup parameters 
;       ---------------------- 
; 
DPD:				equ	BMD+SDX ; Offset in boot sector to find this record 
; 
SPTRK:				equ	0       ; Number of tracks on diskette, [B7]=1 if double sided 
SPDEN:				equ	1       ; Diskette density, $80 for single, $00 for double density
CPREV:				equ	2       ; Revision number of DOS 
CPBUF:				equ	3       ; Number of buffers to use (if booted) 
CPDRV:				equ	4       ; Default drive (if booted) 
CPSYS:				equ	5       ; Do AUTORUN.SYS if set 
CPEXC:				equ	6       ; Do AUTORUN.BAT if set 
CCPX:				equ	7       ; Length of setup parameters 
; 
CDOFF:				equ	SDOFF+SDX ; Offset to current directory within this table
; 
;       Extra parameters (NOT ON DOS 1.1) 
;       --------------------------------- 
; 
EXTR:				equ	DPD+CCPX
BTSEC:				equ	0       ; number of boot sectors (version 1.1 only) 
DSEQU:				equ	1       ; access sequence number (for volume fixes) 
DRAND:				equ	2       ; random number for volume purposes 
DDMAP:				equ	3       ; First sector map # of DOS 
DDLOK:				equ	5       ; Write lock flag (-1 = diskette locked) 
; 
; 
; 
DRIVE:				equ	DUNIT
srcdrv:				equ	dunit
; 
; 

;------------------------------------------------------------------------
;       boot program for DOS II 
;====================================== 
; 
DATBUF:				equ	$2E00   ; location for data 
SECMAP:				equ	$2F00   ; location for loading sector maps 
CHPTR:				equ	$90     ; character pointer 
SECPTR:				equ	$91     ; pointer to next sector 
BUF:				equ	$92     ; data buffer for program 
LENG:				equ	$94     ; length of buffer 
TEMP:				equ	$96     ; a temp 
; 
;;        System Equates
;----------------------------
;
DOSVEC:				equ	$0A
ICAX4Z:				equ	$2D
ICAX5Z:				equ	$2E
RECVDN:				equ	$39
FEOF:				equ	$3F
ERRNO:				equ	$49
FKDEF:				equ	$60
PALNTS:				equ	$62
LOGCOL:				equ	$63
ADRESS:				equ	$64
SAVADR:				equ	$68
BITMSK:				equ	$6E
SHFAMT:				equ	$6F
COLAC:				equ	$72
DAUX1:				equ	$030A
DAUX2:				equ	$030B
RANDOM:				equ	$D20A
consol:				equ	$d01f
KEYBDV:				equ	$E420
CIOV:				equ	$E456
SIOV:				equ	$E459

;----------------------------
;
; External reference Equates
;----------------------------



;
pr_ptr:				equ	$84

;----------------------------
; pbi common Routines
;
;
Ld1e0:				equ	$d1e0
Ld1e2:				equ	$d1e2
pbi_ram:			equ	$d600
pbi_rom:			equ	$D800
PBI_SIO:			equ	$D805

;----------------------------
;I am defining
; CSS Black Box Vars Here
;
;----------------------------
;
BBHD1:				equ	$D600
BBHD2:				equ	$D612
BBHD3:				equ	$D624
BBHD4:				equ	$D636
BBHD5:				equ	$D648
BBHD6:				equ	$D65A
BBHD7:				equ	$D66C
BBHD8:				equ	$D67E
BBHD9:				equ	$D690
;
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
;

; Disk Drive Numbers
;
DISK0:				equ	$00
DISK1:				equ	$10
DISK2:				equ	$20
DISK3:				equ	$30
DISK4:				equ	$40
DISK5:				equ	$50
DISK6:				equ	$60
DISK7:				equ	$70
;


;------------------------------------------------------------------------
; Current Month in dec for each time it is compiled 
;even if there is no code change
l_month:		equ   10
;------------------------------------------------------------------------
; Current day in dec for each time it is compiled 
;even if there is no code change
l_day:			equ   15
;------------------------------------------------------------------------
; Current year in dec for each time it is compiled 
;even if there is no code change
l_year:			equ   2012
;------------------------------------------------------------------------
; Current year in dec for each time it is compiled 
;even if there is no code change this is a short year format
c_year_short:		equ	12
;------------------------------------------------------------------------
; Current build so we know witch set of com files 
;goes together!
l_dos_build:		equ	31
;------------------------------------------------------------------------
; is this file registered? 
; yes = 1  no = 0   $81 site license
;
file..yes:		equ	1
file..no:		equ	0
file..site:		equ	$81
;------------------------------------------------------------------------
;Who Compiled this file: 
;UNknown:		equ	0 
;Steve Carden:		equ	1
;Ken Ames:		equ	2 
;Michael:		equ	3 
;
if.who_compiled:	equ	1
;------------------------------------------------------------------------
; Gotta define if it is registered.
file_registered:	equ	file..no
;
;
; File revision history. Varables

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


;------------------------------------------------------------------------
; File revision history. Version Number in hex
;
file_ver:		equ   $11
;------------------------------------------------------------------------
; 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   10
;------------------------------------------------------------------------
; 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 so I can print what I need
r..crl:			equ	$7d
r..crlf:		equ	$9b
r..space:		equ	$20
r..bypassDOS:		equ	$15
r..bypass:		equ	$ff

l_frstscreenbyte:	equ	r..crl
;------------------------------------------------------------------------
; Gotta define if it can be relocated
l_relocatable:		equ	r..yes
;
;------------------------------------------------------------------------
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;-------------------------------------------------------------
;
;	Start of actual code


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


			jsr	pbi_test
			lda	pbi_interface
			cmp	#$00
			beq	.a
			cmp	#$01
			beq	.mio
			cmp	#$02
			beq	.bb
			cmp	#$03
			beq	.kpi
			rts

.mio:			jmp	.show_mio

.bb:			jmp	start

.kpi:			jsr	printsi
			dc.b	253
			dc.b	$9b,$9b,"No KPI support at this time",$9b,$9b,-1
			rts

.a:			jsr	printsi
			dc.b	253
			dc.b	$9b,$9b,"No PBI device detected!",$9b,$9b,-1
			jsr	bell
			rts

.show_mio:		jsr	printsi
			dc.b	253
			dc.b	$9b,$9b,"No MIO support at this time",$9b,$9b,-1
			jsr	bell
			rts



				.org	$5000

				jmp	start

buffer:				ds.b	256

buffer_1:			ds.b	256

sector_size:			dc.w	128	
my_srcdrv:			dc.b	8
dstdrv:				dc.b	8
bb_count:			dc.b	0		; change back to zero


start_sector:			dc.w	0
end_sector:			dc.w	$ffff


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




xiov:				jmp	$ffff


start:
				LDA	#$60
				STA	start
				jsr	START_IT	; inits the relocator for the formatter

;------------------------------------	 ldy #$00

				lda	consol		; check the consol key
				cmp	#7		; looking for any key
				beq	.zzz		; nope they are not hold a key down
				jmp	help_menu	; Show help menu
.zzz:

				lda	$700
				cmp	#'R'
				beq	.real_dos
				bne	.no_dos

.real_dos:
				LDY	#LBUF

.comp:				lda	(comtab),y
				cmp	#$9b
				beq	.exit
				cmp	#' '
				beq	.cont
				iny
				jmp	.comp


.exit:				jmp	error

.no_dos:			jmp	nodos


.cont:
				iny
				iny
				LDA	(comtab),Y
				sta	par_drv_letter+1
				CMP	#'1'			;$2F
				bcc	.ea
				cmp	#'9'+1
				bcs	.ea
				jmp	.c

.ea:				jmp	error


.c:				ldx	#$00

.d:				inx
.e:				iny
				lda	(comtab),y
				cmp	#'"'
				beq	.e
				cmp	#$9b
				beq	.done_with_cli
				cmp	#'>'
				bne	.tam
				lda	#'\'
.tam
				sta	par_Name,x
				inc	par_Name
				jmp	.d

.done_with_cli:
				lda	par_drv_letter+1
				sbc	#48
				sta	my_srcdrv


				lda    my_srcdrv
				sbc    #1
				tay
				lda	my_drive_lo,y
				sta	bb_set_drv+1
				lda	my_drive_high,y
				sta	bb_set_drv+2

				jmp	start_it_now

exit_program:  
				LDA	#$9B
				JSR	echo
;				LDX	stack_save
;				TXS 
				RTS 

good_api:	
				jsr	printsi
				dc.b	" Done!",$9b,-1
				jmp	exit_program

Bad_api:
				sty	rc_data
				jsr	printsi
				dc.b	" error code #",-1
				lda	rc_data
				jsr	pr_byte
				jmp	exit_program
	


rc_data:			dc.b	0



error:
				JSR	printsi
				dc.b	$9b,"Dirbuild Dn:"
				dc.b	$9b
				dc.b	<+128>,"Requires a Black Box"
				dc.b	$9b,$9b
				dc.b 	"Dn:   n= drive id  1 to 9",$9b,$9b
				dc.b	"Syntax =",$9b
				dc.b	"dirbuild D8:"
				dc.b	$9b,$9b,-1


				RTS 
;

nodos:
				JSR	printsi
				dc.b	$9b
				dc.b	$9b,"Dirbuild "
				dc.b	<+128>,"requires"
				dc.b	" RealDos & Black Box"
				dc.b	$9b,$9b,-1
				RTS 



start_it_now:
				JSR	printsi
				dc.b	$20,$7d,-1

				jsr	Head_back-3	;redraw header
				lda	#low 0
				sta	start_sector
				lda	#high 0
				sta	start_sector
				ldy	#$00
				lda	#$00
.zero:				sta	buffer_1,y
				sta	cmdtab2,y
				iny
				cpy	#$00
				bne	.zero
				ldy	#$00
				lda	#$6c
.b6c:				sta	buffer,y
				iny
				cpy	#$00
				bne	.b6c				
				ldx     #2
				ldy     #12
				jsr     gotoxy
				jsr 	printsi
				dc.b	"Writting a Partition ",-1


bb_loop_check:
				ldx     #5
				ldy     #20
				jsr     gotoxy
				lda	bb_count
				jsr	pr_byte
				lda	bb_count

				cmp	#96
				beq	.all_done
				lda	#$00
				sta	start_sector
				sta	start_sector+1
				inc	bb_count
				lda	bb_count
				jsr	SET_PART
				bcs	bb_loop_check

				jsr	.show_name

;				jmp	.all_done		; gonna remove this

;				jsr	run_loop



				jsr	format			; this is to format the directory

				jmp	bb_loop_check

.all_done:
				jsr	printsi
				dc.b	$9b,$9b,"All Done!",$9b,-1
				rts

.show_name:
				lda	#$7d
				jsr	echo
				jsr	Head_back-3

;				ldx     #34
;				ldy     #5
;				jsr     gotoxy
;				jsr	printsi
;				dc.b    "       ",-1

				ldx     #24
				ldy     #11			;2
				jsr     gotoxy
				jsr	printsi
				dc.b	"             ",-1
				ldx     #24
				ldy     #11			;2
				jsr     gotoxy


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

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

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

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

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

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




				ldx     #5
				ldy     #14
				jsr     gotoxy
				jsr	printsi
				dc.b	"                               ",-1
				ldx     #5
				ldy     #14
				jsr     gotoxy

				jsr	printsi
				dc.b	"Total Partition Sectors ",-1

				LDA	par_sect_size
				SEC 
				SBC	# low 1
				STA	end_sector
				LDA	par_sect_size+1
				SBC	# high 1
				STA	end_sector+1

				lda	end_sector
				ldx	end_sector+1
				jsr	pr_card
				rts







run_loop:	
				LDA	start_sector
				CLC 
				ADC	# low 1
				STA	start_sector
				LDA	start_sector+1
				ADC	# high 1
				STA	start_sector+1
				lda	#$9b
				jsr	echo

				ldx     #34
				ldy     #5
				jsr     gotoxy

				lda	start_sector
				ldx	start_sector+1

				jsr	pr_card
				lda	start_sector+1
				cmp	#$00
				bne	.do_256
				lda	start_sector
				cmp	#$03
				bcs	.do_256

				lda	#low 128
				sta	sector_size
				lda	#high 128
				sta	sector_size+1
				jmp	.do_rw


.do_256:			lda	#low 256
				sta	sector_size
				lda	#high 256
				sta	sector_size+1

.do_rw:
				jsr	read_write

				lda	start_sector+1
				cmp	#$ff
				bne	run_loop
				lda	start_sector
				cmp	#$ff
				bne	run_loop

				rts






read_write:
;read
				lda 	#$31
				sta	$300
				lda	my_srcdrv
				sta	$301
				lda	#'R'		; read sector
				sta	$302		;dcomnd
				lda	#$40		;read cmd for sio or direction
				sta	$303		;I think
				lda	#low buffer
				sta	dbuflo
				lda	#high buffer
				sta	dbufhi
				lda	sector_size
				sta	$308

				lda	sector_size+1
				sta	$309
				lda	start_sector
				sta	$30a		;low byte of sector number to read/write
				lda	start_sector+1
				sta	$30b		;hi byte
				jsr	do_r_siov	; read sector siov
				ldy	#$00
				lda	#$6c

.loop:				lda	buffer,y
				cmp	#$6c
				bne	.nowrt
				iny
				cpy	#$00
				bne	.loop
				jmp	.write_it



.nowrt:				rts		



.write_it
				lda 	#$31
				sta	$300
				lda     my_srcdrv
				sta	$301
				lda	#'W'		; Write sector
				sta	$302		;dcomnd
				lda	#$80		;read cmd for sio or direction
				sta	$303		;I think
				lda	#low buffer_1
				sta	dbuflo
				lda	#high buffer_1
				sta	dbufhi
				lda	sector_size
				sta	$308

				lda	sector_size+1
				sta	$309
				lda	start_sector
				sta	$30a		;low byte of sector number to read/write
				lda	start_sector+1
				sta	$30b		;hi byte
				jsr	do_w_siov	; write sector siov

				rts


do_r_siov:
				lda	#'R'		; read sector
				sta	$302		;dcomnd
				lda	#$40		;read cmd for sio or direction
				sta	$303		;I think
				jsr 	$e459
;				lda	$303		;I think
;				cmp	#$01
;				bne	do_r_siov
				rts

do_w_siov:
				lda	#'W'		; Write sector
				sta	$302		;dcomnd
				lda	#$80		;read cmd for sio or direction
				sta	$303		;I think
				jsr 	$e459
;				lda	$303		;I think
;				cmp	#$01
;				bne	do_w_siov
				rts

;
;
;
;         LDA #20 ;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
				RTS			; and exit
;

		

override:			dc.b	$00		; a "q" here will format without question		


gotoxy:         		stx     85
				sty     84
				rts


savexy:				lda	85
				sta	save.x
				lda	84
				sta	save.y
				rts

loadxy:				lda	save.x
				sta	85
				lda	save.y
				sta	84
				rts

save.x:
save.y:

;----------------------------------;
;printsi Routeen macro             ;
;this is a slow print to the screen;
;----------------------------------;
;
sprintsi:			pla
				sta	$b0
				pla
				sta	$b1
				ldy	#$01
.a:				lda	($b0),y
				cmp	#$ff
				beq	.b
				jsr	sprint
				jsr	.c
				jmp	.a
.b:				jsr	.c
				lda	$b1
				pha
				lda	$b0
				pha
				rts
.c:				inc	$b0
				bne	.exit
				inc	$b1
.exit:				rts
;
;
;---------------------------
;this one make's a nice tone
;---------------------------

bell:
				sta	bell_m3
				sty	bell_m3+1
				stx	bell_m3+2
				ldy	#$af
				lda	#$14
				sta	$d200
bell_m1:			sty	$d201
				lda	#$00
				sta	$14
bell_m2:			lda	$14
				cmp	#$02
				bcc	bell_m2
				dey
				cpy	#$a0
				bcs	bell_m1
				lda	bell_m3
				ldy	bell_m3+1
				ldx	bell_m3+2
				rts
bell_m3:			dc.b	$00,$00,$00
;


;
;------------;
; 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
;------------------------------;
;   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
;
;-----------------------------;
;CHANGES THE CHARTER TO UPCASE;
;-----------------------------;
upcase:
				cmp	#$61
				bcc	.a
				cmp	#$7a
				bcs	.a
				sbc	#$1f
.a:				rts
;
;-----------------------------;
;CHANGES THE CHARTER TO LOCASE;
;-----------------------------;
locase:
				cmp	#$41
				bcc	.a
				cmp	#$5a
				bcs	.a
				adc	#$1f
.a:				rts
;
;----------------------------------;
;    ERROR NUMBER                  ;
;this take's the value of the a reg;
;and put a hex number to the screen;
;----------------------------------;
;
h_cardd:			dc.b	$00
;
; print a hex word value
; low byte in a reg x is high byte
;
h_card:				sta	h_cardd
				txa
				jsr	drive_error
				lda	h_cardd
				jsr	drive_error
				rts
;
;
drive_error:			pha
				lsr
				lsr
				lsr
				lsr
				jsr	.a
				pla
				and	#$0f
.a:				cmp	#$0a
				bcs	.b
				ora	#$30
				bne	.c
.b:				adc	#$36
.c:				jsr	sprint
				rts
;
;
;----------------------------;
;   jsr  printsi             ;
;this prints an inline string;
;     terminated by $ff      ;
;----------------------------;
;        jsr   prints
;       dc.b	"print this",$9B,$FF
echosi:
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
				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
;
;

;
;-------------------------------;
;        JSR ECHOS              ;
;LOAD THE LOW BYTE INTO PRPTR   ;
;LOAD THE HIGH BYTE INTO PRPTR+1;
;JSR ECHOS dc.b $00,"NAME"      ;
;-------------------------------;
;
prints:
echos:
				pha
				pha
				clc
				lda	prptr
				sta	.b+1
				sta	.c+1
				lda	prptr+1
				sta	.b+2
				sta	.c+2
				lda	#$00
				sta	prptr+2
.a:				ldy	prptr+2
				iny
				sty	prptr+2
.b:				lda	$ffff,y
				jsr	sprint
				ldy	prptr+2
.c:				cpy	$ffff
				bcc	.a
				pla
				pla
				rts
;PR_PTR
prptr:				dc.b	0,0,0
;
;
;-------------------------------;
;       JSR GETSTRING           ;
;LOAD THE LOW BYTE INTO sptr    ;
;THE HIGH BYTE INTO sptr+1      ;
;LOAD THE MAX LENGHT INTO Y REG.;
;-------------------------------;
;
;
getstring:
				lda	#$9b
				sta	x1_get+1
				sty	x_get+1
				cpx	#$ff
				bne	wq_get
				stx	x1_get+1
wq_get:				lda	sptr
				sta	g_get+1
				sta	b_get+1
				sta	s_get+1
				sta	w_get+1
				sta	t_get+1
				sta	back_s+1
				lda	sptr+1
				sta	g_get+2
				sta	b_get+2
				sta	s_get+2
				sta	w_get+2
				sta	t_get+2
				sta	back_s+2
				ldy	#$01
get_loop:			clc
				jsr	getkey
				cmp	#$1c		; CURSER UP
				bcc	g_get
				cmp	#$20
				bcc	get_loop
g_get:				sta	$ffff,y
x1_get:				cmp	#$9b
				beq	g_exit
				cmp	#$7e
				beq	back_s
s_get:				inc	$ffff
				jsr	sprint
				iny
x_get:				cpy	#$ff
				beq	g_exit
				bne	get_loop
g_exit:				jsr	sprint
t_get:				inc	$ffff
				rts
back_s:				lda	$ffff
				cmp	#$00
				beq	get_loop
b_get:				dec	$ffff
w_get:				lda	$ffff
				cmp	#$00
				bcc	get_loop
				tya
				dey
				lda	#$7e
				jsr	sprint
				cmp	#$00
				bne	get_loop
				beq	get_loop


sptr:				dc.w	0


my_d_ptr:
dptr:				dc.w	0
test_byte:			dc.b	0,0,0,0
;
;
;-----------------------------;
;COMPARE STRING'S sptr TO DPTR;
;-----------------------------;
;
cmpstr:				clc
				lda	#$01
				sta	test_byte+3
				lda	sptr
				sta	c_sptr+1
				sta	cmp_word+1
				sta	cmp_check1+1
				lda	sptr+1
				sta	c_sptr+2
				sta	cmp_word+2
				sta	cmp_check1+2
				lda	dptr
				sta	c_dptr+1
				sta	cmp_check2+1
				lda	dptr+1
				sta	c_dptr+2
				sta	cmp_check2+2
cmp_check1:			lda	$ffff
cmp_check2:			cmp	$ffff
				bne	w_cmpstr
cmp_word:			lda	$ffff
				sta	test_byte+2
				ldy	#$00
cmp_loop:			iny
				clc
				cpy	test_byte+2
				beq	cmp_exit
				clc
c_sptr:				lda	$ffff,y
				jsr	locase
				sta	test_byte
c_dptr:				lda	$ffff,y
				jsr	locase
				cmp	test_byte
				bne	w_cmpstr
				beq	cmp_loop
w_cmpstr:
				lda	#$00
				sta	test_byte+3
cmp_exit:			lda	test_byte+3
				cmp	#$01
				rts
;
;
;--------------------------------;
; LENGHT TO 0 BYTE STRING        ;
; SRC STRING sptr DES STRING DPTR;
;--------------------------------;
;
toascii:
				lda	sptr
				sta	src_asc+1
				sta	src_hold+1
				lda	sptr+1
				sta	src_asc+2
				sta	src_hold+2
				lda	dptr
				sta	des_hold+1
				sta	src_e+1
				lda	dptr+1
				sta	des_hold+2
				sta	src_e+2
				ldy	#$01
				ldx	#$00
src_hold:			lda	$ffff,y
des_hold:			sta	$ffff,x
				iny
				inx
src_asc:			cpy	$ffff
				beq	src_exit
				bne	src_hold
src_exit:			lda	#$00
src_e:				sta	$ffff,x
				rts
;
;-------------------;
; 0 TO LENGHT STRING;
;-------------------;
tostr:
				lda	sptr
				sta	tostr_sr+1
				lda	sptr+1
				sta	tostr_sr+2
				lda	dptr
				sta	tostr_dr+1
				sta	tostr_hold+1
				sta	tostr_ho+1
				lda	dptr+1
				sta	tostr_dr+2
				sta	tostr_hold+2
				sta	tostr_ho+2
				ldy	#$00
tostr_ho:			sty	$ffff
				ldx	#$01
tostr_sr:			lda	$ffff,y
				cmp	#$00
				beq	tostr_exit
tostr_dr:			sta	$ffff,x
				iny
				inx
tostr_hold:			inc	$ffff
				beq	tostr_sr
				bne	tostr_sr
tostr_exit:
				rts
;
;
;-------------------;
; JSR CONCAT APPENDS;
; SPRT TO DPTR      ;
;-------------------;
;
concat:
				lda	sptr
				sta	con_s+1
				sta	con_hold+1
				lda	sptr+1
				sta	con_s+2
				sta	con_hold+2
				lda	dptr
				sta	con_d+1
				sta	con_de+1
				sta	con_dd+1
				lda	dptr+1
				sta	con_d+2
				sta	con_de+2
				sta	con_dd+2
con_de:				ldy	$ffff
				iny
				ldx	#$01
con_s:				lda	$ffff,x
con_d:				sta	$ffff,y
con_dd:				inc	$ffff
				iny
				inx
con_hold:			cpx	$ffff
				bmi	con_s
				rts
;
;
;------------;
; JSR MOVESTR;
;------------;
movestr:
				lda	sptr
				sta	mov_s+1
				sta	mov_h+1
				lda	sptr+1
				sta	mov_s+2
				sta	mov_h+2
				lda	dptr
				sta	mov_d+1
				lda	dptr+1
				sta	mov_d+2
mov_h:				ldy	$ffff
mov_s:				lda	$ffff,y
mov_d:				sta	$ffff,y
				cpy	#$00
				beq	mov_ew
				dey
				bne	mov_s
				beq	mov_s
mov_ew:				rts
;------------------------
;JSR GETCARD
;
;------------------------
gtbyte:				dc.b	$00,"       "
qstrin:				dc.b	"       "
gtwr:				dc.b	$03,$06
getbyte:
				lda	gtwr
				sta	gt1+1
				ldy	#$00
				sty	gtbyte
				cmp	gtwr
				beq	gt1
getcard:
				lda	gtwr+1
				sta	gt1+1
				ldy	#$00
				sty	gtbyte
gt1:				cpy	#$ff
				beq	gt3
				jsr	getkey
				jsr	upcase
				cmp	#$9b
				beq	gt3
				cmp	#$7e		; BACKSPACE
				beq	gt2
				cmp	#'0'
				bcc	gt1
				cmp	#':'
				bpl	gt1
				ldy	gtbyte
				iny
				sty	gtbyte
				sta	gtbyte,y
				jsr	echo
				cmp	#$ff
				bne	gt1
				beq	gt1
gt2:				ldy	gtbyte  ; BACKSPACE
				cpy	#$00
				beq	gt1
				lda	#$7e
				jsr	echo
				dec	gtbyte
				cmp	#$ff
				bne	gt1
				beq	gt1
;
;-MATH FOR CARD CONVERSION-
gt3:
				lda	#$9b
				ldy	gtbyte
				iny
				sta	gtbyte,y
				sty	gtbyte
				lda	# low gtbyte
				sta	sptr
				lda	# high gtbyte
				sta	sptr+1
				lda	# low qstrin
				sta	dptr
				lda	# high qstrin
				sta	dptr+1
				jsr	toascii
				lda	# low qstrin
				sta	inbuff
				lda	# high qstrin
				sta	inbuff+1
				lda	#$00
				sta	cix
				jsr	afp
				jsr	fpi
				lda	fr0
				ldx	fr0+1
				rts


pr_byte:
				ldx	#$00
pr_card:
				sta	fr0
				stx	fr0+1
				lda	#$00
				sta	cix
				jsr	ifp
				jsr	fasc
				ldy	#$00
.a:
				lda	(inbuff),y
				php
				and	#$7f
				iny
				jsr	echo
				plp
				bpl	.a
				rts
;

pbi_interface:			dc.b	0			; 0= no pbi 1=mio 2=bb 3=kpi


pbi_test:
				jsr	mio_test
				jsr	kpi_test
				jsr	black_box_test
				rts





;-----------------------------------------------------------------
;
;  Checking on the black box if it is present!
;
BLACK_BOX_test:
			SEI
			LDA	#2
			STA	BB_Sensel
			LDY	#$10
.a:
			LDX	#2
			LDA	pbi_rom,Y
			CMP	.data,X
			BEQ	.b
			INY
			BNE	.a
			JMP	.no_interface
			RTS
;
.b:
			INY
			DEX
			BMI	.yes_interface
			LDA	pbi_rom,Y
			CMP	.data,X
			BNE	.a
			BEQ	.b

.yes_interface:
			lda	#$02			; yes we have an BB
			sta	pbi_interface
.no_interface:
			CLD
			LDA	#0
			STA	BB_Sensel
			CLI
			RTS
;
.data:			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	#$03			; yes we have a KPI
			sta	pbi_interface
			lda	#$00
			sta	$d1ff			; added code for Ken
no_kpi:
			cli
			rts				;or whichever way you need to return
;

;------------------------------------------------------------------
;  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	Ld1e0
			STA	M_HOLD
			LDA	Ld1e2
			STA	M_HOLD+1
			LDA	#$00
			STA	Ld1e0
			LDA	#$20
			STA	Ld1e2
			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
			ldx	mio_stack
			txs
			lda	mio_acc
			ldy	mio_y
			ldx	mio_x
			lda	mio_there
			cmp	#$00
			beq	.exit
			lda	#$01			; yes we have a mio
			sta	pbi_interface

.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


;
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;  help menu section
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help_menu:
				lda	#$01
				sta	752
				jsr	Head_back-3
				jsr	printsi
				dc.b	$9b
				dc.b	"dirbuild.s.COM version 1.1",$9b
				dc.b	"is Copyright 2012 ILS",$9b,$9b
				dc.b	"Written by Stephen J. Carden.",$9b
				dc.b	"It's intent is to build directories ",$9b
				dc.b	"on your Black Box after you low level",$9B
				dc.b	"format a new hard disk. After you",$9B
				dc.b	"you build all your directorys look"$9b
				dc.b	"at your partitions and see if you have",$9b
				dc.b	"sectors that are filled with $6c.",$9b
				dc.b	"If you do run program Purge_6c.",$9b
				dc.b	$9b,$ff
				jsr	pause
				jsr	printsi
				dc.b	$9b
				dc.b	"Purge_6c will read each sector and see",$9b
				dc.b	"if you a sector is filled with $6c if it",$9b
				dc.b	"is Purge_6c will rewrite the sector with",$9b
				dc.b	"$00.",$9B
				dc.b	$9B
				dc.b	$9b
				dc.b	"Have fun and drop me a e-mail!",$9b
				dc.b	"The Atari 8-bit is the source of",$9b
				dc.b	"many Hours of fun. So let us keep",$9B
				dc.b	"This DIEHARD machine running in some",$9b
				dc.b	" form.",$9B
				dc.b	$9b,-1
				jsr	pause
				jsr	printsi
				dc.b	$9b
				dc.b	"The source code to this file",$9B
				dc.b	"is not to be distributed",$9B
				dc.b	"Without Written consent of the",$9B
				dc.b	"author, Stephen J. Carden!",$9B
				dc.b	"dirbuild.s.com can not be distributed",$9b
				dc.b	"as part of any program sold profit!",$9b
				dc.b	"including shareware disk's",$9b
				dc.b	"You are permitted to e-mail it to",$9b
				dc.b	"anyone you WISH!!",$9b,-1
				jsr	pause
				jsr	printsi
				dc.b	$9b," Should a company wish to use this",$9b
				dc.b	"I will provide a site license for a",$9b 
				dc.b	"cost $100.00 US Dollars, with the",$9b 
				dc.b	"exception of Video 61 and Lance",$9b 
				dc.b	"Ringquist. Atari usergroups may use",$9b
				dc.b	"it FREE of charge for there",$9b 
				dc.b	"BBS Systems",$9b,-1
				jsr	pause
				jsr	printsi
				dc.b	$9b,$9b
				dc.b	"If you wish to have the source",$9B
				dc.b	"code then contact:",$9B,$9B
				dc.b	"       Stephen J. Carden",$9B
				dc.b	"       3321 Jonathan Cir.",$9B
				dc.b	"       Augusta, Ga. 30906",$9B
				dc.b	"       sjcarden@bellsouth.net",$9B,$9b
;
				dc.b	"Or send a donation of $20.00US to",$9b 
				dc.b	"the above address.",$9B,$FF
				jsr	printsi
				dc.b	$9b,$9b
				dc.b	" Press "
				dc.b	<+128>,"[Return]"
				dc.b	" any other key ",$9B,-1
				jsr	getkey
				lda	#$00
				sta	752
				rts



;
;           a more command
pause:    
				jsr	printsi
				dc.b	<+128>,"[ -More- ]" 
				dc.b	-1
				jsr	getkey
				jsr	printsi
				dc.b	"~~~~~~~~~~~~~",$FF
				rts
;

stack_save:			dc.b	0



par_drv_letter:
				dc.b	1,"n"
par_sector_size:
				dc.b	0
par_sector_size_128:
				dc.b	4,"128"
				ds.b	25
par_sector_size_256:
				dc.b	4,"256"
				ds.b	25
par_sep:			dc.b	1,"|"
par_sector_number:
				dc.b	0,"65535"
				ds.b	25
;
;
;  ibm drive letter\path\*.atr|65535|256|8 
; 
par_Name:			dc.b	0
cmdtab2:			ds.b	256
;
;
;------------------------------------------------------------------------~~~~~~~~~
;This is the actual directory formatter.
;------------------------------------------------------------------------~~~~~~~~~
;
;
;
;This is the actual directory formatter.
;
;
;
;
			.org	$7ffd			; was 3  change back to 4000
;
			jmp	START_IT  		;did this for debug

Split:
;###############################################################################
;
;
;       ==================== 
;       Stage 1 boot program 
;       ==================== 
;

initz1:
;
BOOTS:
			dc.b	$00
			dc.b	$03
			dc.b	$00
			dc.b	$30
			cpx	#$07
w_f01:			JMP	LOADER		;$3080
;
;sector map main directory
l4009:
			dc.w	0
; Total number of sectors on disk
l400b:			dc.w	0
; Number of free sectors on disk
l400d:			dc.w	0
; Number of bit map sectors
l400f:			dc.b	0
; 
			dc.w	4
l4012:			dc.w	0
L4014:			dc.w	0
l4016:			dc.b	"        "
l401e:			dc.b	0
l401f:
DENLOA:			dc.b	0
			dc.b	$20
			dc.b	6
			dc.b	1
			dc.b	-1
			dc.b	-1
			dc.b	0
			dc.b	0
l4027:			dc.b	0
L4028:
FIRMAP:			dc.w	0
			dc.b	0
l402b:			dc.b	0
l402c:			dc.b	0
l402d:			dc.w	0
l402f:			dc.b	0


NODMSG:			dc.b	"Error: No DOS",$9b
; 
; 
;       get next sector map.. initz new map # 
;       ------------------------------------- 
; 

LOANM:
w_f02:			lda	FIRMAP
			sta	SECTOR
w_f03:			lda	FIRMAP+1
			sta	SECTOR+1
			lda	# low secmap
			ldx	# high secmap
w_f04:			jsr	READS
			lda	secmap
w_f05:			sta	FIRMAP
			lda	secmap+1
w_f06:			sta	FIRMAP+1
			ldy	#4
			sty	SECPTR
; 
;       get next sector number.. 
;       ------------------------ 
; 

GNXSEC:			ldy	SECPTR
w_f07:			CPY	DENLOA
			beq	LOANM
			lda	secmap,Y
			sta	SECTOR
			lda	secmap+1,Y
			sta	SECTOR+1
			INY
			INY
			sty	SECPTR
LRTS:			rts
;
DOINITZ:		JMP	($2e2)
;
l407c:
			dc.b	0
			dc.b	0
			dc.b	0
			dc.b	0

LOADER:			ldx	#0
w_f08:			lda	DENLOA				; set ahead sector map ptr.. 
			sta	SECPTR				;  to max val.. force map load 
			sta	CHPTR				;  to max val.. force data load 
			sta	DBYTLO				; set sector size accordingly 
			BNE	ISX
			INX
ISX:			STX	DBYTHI				; set sector length high
w_f09:			jsr	f_getbyte			; get FF FF header 
			sta	temp
w_f10:			jsr	f_getbyte
			AND	temp
			CMP	#$FF
			BNE	TYPER				; Not binary file error
BEGIN:
w_lb_1:			lda	# low LRTS			; null initz 
			sta	INITAD
w_hb_1:			lda	# high LRTS
			sta	INITAD+1
w_f11:			jsr	f_getbyte
			sta	buf				; get start address 
w_f12:			jsr	f_getbyte
			sta	buf+1
			ORA	buf
			beq	STAD				; a header of zero indicates end..
w_f13:			jsr	f_getbyte
			SEC
			SBC	buf				; calc number of bytes
			PHA
			PHP
w_f14:			jsr	f_getbyte
			PLP
			SBC	buf+1
			sta	LENG+1				; set total size-1 
			PLA
			sta	LENG

w_f15:			jsr	LOABUF				;  and load buffer.. 
w_f16:			jsr	DOINITZ				; repeat for next segment 
w_f17:			jmp	BEGIN				; do start.. 
;
STAD:			jmp	($2e0)
;
; 
; 
;       error.. no DOS on diskette 
;       -------------------------- 
; 
TYPER:
w_lb_2:			lda	# low NODMSG
w_hb_2:			ldx	# high NODMSG
			sta	ICBAL
			STx	ICBAH
			STx	ICBLL
			lda	#9
			sta	ICCOM
			ldx	#0
			jsr	CIO

FOREV:
w_f18:			jmp	FOREV				; endless loop 
;
; 
; 
;       read sector at address 
;       ---------------------- 
; 
READS:
			ldy	#$40				; read data sector at XA
			sty	DSTATS
			sta	DBUFLO
			STx	DBUFHI				; note command is $52 ('R')
			lda	SECTOR
			ORA	SECTOR+1
			beq	TYPER				; if sector zero.. then error
			jsr	SIO				; use $E459 sio address.. 
			bmi	TYPER				; if error.. say no DOS 
			rts
; 
; 
;       get byte/burst routine 
;       ---------------------- 
;
;
f_getbyte:
			lda	#0
			sta	LENG+1
			sta	LENG

GETBYT:			ldx	CHPTR				; get ptr to next char to load 
w_f19:			CPx	DENLOA				; check if at end.. 
			beq	LOANS				;  if so, load next sector
			lda	datbuf,X
			INC	CHPTR
			rts
;
; 
; 
;       Load next sector in buffer/ or burst if possible 
;       ------------------------------------------------ 
; 
LOANS:
w_f20:			jsr	GNXSEC				; get next sector # 
			lda	LENG+1				; check if to do burst
			bne	OKBUR
w_f21:			lda	DENLOA
			beq	NOBUR
			lda	LENG
			bmi	OKBUR				; if >127, then burst it 

NOBUR:			lda	# low datbuf
			ldx	# high datbuf
w_f22:			jsr	READS				; read sector into data buffer.. 
			SEC
			ROl	CHPTR				; put 1 into char ptr 
			lda	datbuf				; get first char..
			rts
;
; 
;       Do burst read sector 
;       -------------------- 
; 
OKBUR:			lda	buf				; get address of where to load 
			ldx	buf+1
w_f23:			jsr	READS				; read into correct place 
			lda	buf				; add length of sector.. 
			clc
			adc	DBYTLO
			sta	buf
			lda	buf+1
			adc	DBYTHI
			sta	buf+1
			SEC					; subtract from length
			lda	LENG
			SBC	DBYTLO
			sta	LENG
			lda	LENG+1
			SBC	DBYTHI
			sta	LENG+1
w_f24:			jmp	LOANS				; go load next sector
;
; 
; 
;       Load entire buffer from file 
;       ---------------------------- 
; 

LOABUFX:		lda	LENG				; decrement length
			bne	NOB
			DEC	LENG+1
NOB:			DEC	LENG


LOABUF:
w_f25:			jsr	GETBYT  ; get byte.. 
			ldy	#0
			sta	(buf),Y
			INC	buf
			bne	SK11
			iNC	buf+1
SK11:			lda	LENG
			ORA	LENG+1
			bne	LOABUFX
			rts
			brk

zend1:			ds.b	0


BOOTI:    =   SPLIT   ; $4000   ; location of boot sectors

; 
; 
;
;========================================= 
;       Beginning and initialization 
;========================================= 
;
;
START_IT:
			jsr	rlocate

BEGINI:			lda	COMTAB  ; calc address of SIO 
			SEC 
			SBC	# low lsio
			sta	XSIO+1
			lda	COMTAB+1
			SBC	# high lsio
			sta	XSIO+2

			rts 

;			jmp	FORMAT


XSIO:			jmp	(0)
;
RETURN:
;			ldx	stackp
;			TXS 
			rts 
;
;
;================================== 
;       Main parameter input 
;================================== 

format:
;			tsx 
;			stx	stackp
			jsr	savexy

				ldx     #2
				ldy     #5
				jsr     gotoxy

			jsr	f_printsi
			dc.b	$9b
			dc.b	"      "
			dc.b	<+128>,"Multi Directory Formatter"
			dc.b	$9B
			dc.b	"    Written By Stephen J. Carden",$9B
			dc.b	"   For the Black Box all versions",$9b
			dc.b	$9b
			dc.b	" Black Box Partition being formatted",$9b
			dc.b	"     "
			dc.b	<+128>,"|Current Drive==>"
			dc.b	$9b,-1

			jsr	loadxy
			jsr	f_printsi
			dc.b	$9b,$9b
			dc.b	$9b,$9b,$9b,$9b,$9b,-1

			lda	my_srcdrv
			sta	DUNIT
			lda	consol
			cmp	#7
			beq	.hoho
			lda	#$00
			sta	override

.hoho:
			lda	override
			cmp	#'Q'
			beq	.c

			jsr	f_printsi
			dc.b	$9B,$9B
			dc.b	"Initialize... Are You Sure? ",-1

.a:			jsr	inch
			jsr	upcase
			sta	override
			CMP	#'Y'				;$59 
			BEQ	.c
			CMP	#'y'				;$79
			BEQ	.c
			cmp	#'Q'
			beq	.c
			cmp	#'A'
			beq	.abort
			CMP	#'N'				;$4E
			BEQ	.b
			CMP	#'n'				;$6E
			bne	.a
.b:
			jsr	f_printsi
			dc.b	"n",$9b,-1
			jmp	RETURN

.abort:			jmp	$e474
;
.c:
			jsr	f_printsi
			dc.b	"Y",$9b,-1




			lda	# low NAMES
			sta	DBUFLO
			lda	# high NAMES
			sta	DBUFHI
			lda	#$31
			sta	DDEVIC
			lda	#'N'				;$4E
			sta	DCOMND
			lda	#$40
			sta	DSTATS
			lda	# low 12				;$0C
			sta	DBYTLO
			lda	# high 12				;0
			sta	DBYTHI
			jsr	$e459
			BPl	.d
			jsr	f_printsi
			dc.b	"??Drive not Configurable??",$9b,-1
			jmp	RETURN
;

.d:				;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>add here
			jsr	calc_sector_count

			lda	consol
			cmp	#7		;
			beq	.tam
			lda	consol
			cmp	#$05	; select key
			beq	.damn
			lda	#08
			bne	.usp
.tam:			lda	names+3
			cmp	#$00
			bne	.e
			lda	names+2
			cmp	#$00
			bne	.e
.damn:
			lda	#$ff	; loading an ff in the sector block
			sta	names+3
			sta	names+2

.usp:
			jsr	f_printsi
			dc.b	$9b,$9b," Setting Disk to ",-1
	
			lda	f_end_sector
			ldx	f_end_sector+1
			jsr	f_pr_card


			jsr	f_printsi
			dc.b	" Sectors",$9b,$9b,-1
;
.e
			lda	#1					;NAMES this is a change
			sta	DENS
			cmp	#1
			beq	.f
			jsr	f_printsi
			dc.b	"??Shows Multiple Tracks??",$9b,-1
			jmp	RETURN
;
.f:
			lda	real_sector_count
;			lda	NAMES+3
			sta	MAXSEC

			lda	real_sector_count+1
;			lda	NAMES+2
			sta	MAXSEC+1
 
			lda	NAMES+7
			sta	DENSITY
			lda	#$FF
			sta	SECTORS
			jsr	RECALC
			jsr	WRITE
			jsr	f_printsi
			dc.b	$9b,$9b
			dc.b	"Drive Initialized...",$9b,-1
			jmp	RETURN
;
;
;End of the directory formatting
;
;

get_sector_count:
			lda	#$31
			sta	$300
			lda	srcdrv
			sta	$301
			lda	#'N'
			sta	$0302
			lda	#$40
			sta	$0303
			lda	# low huh_data
			sta	$0304
			lda	# high huh_data
			sta	$0305
			lda	#$0c
			sta	$0308
			lda	#$00
			sta	$0309
			sta	$030a
			sta	$030b
			jsr	$e459
			lda	$0303
			cmp	#$01
			beq	.huh_1
			rts
.huh_1:
			lda	consol
			cmp	#5
			beq	.tam
			rts
.tam:
			lda	#$7d
			jsr	echo
			jsr	Head_back-3

			jsr	f_printsi
			dc.b	$9b
			dc.b	"       Drive configuration table        ",$9B,$9B,$FF
			lda	huh_data
			jsr	sub_error
			jsr	f_printsi
			dc.b	"number of tracks",$9B,$FF
			lda	huh_data+1
			jsr	sub_error
			jsr	f_printsi
			dc.b	"Step rate! normaly 1",$9B,$FF
			lda	huh_data+2
			jsr	sub_error
			jsr	f_printsi
			dc.b	"Sector/Track high byte",$9B,$FF
			lda	huh_data+3
			jsr	sub_error
			jsr	f_printsi
			dc.b	"Sector/Track LOW byte",$9B,$FF
			lda	huh_data+4
			jsr	sub_error
			jsr	f_printsi
			dc.b	"Max head number",$9B,$FF
			lda	huh_data+5
			jsr	sub_error
			jsr	f_printsi
			dc.b	"Density -0=s, 4 double, 8 High",$9B,$FF
			lda	huh_data+6
			jsr	sub_error
			jsr	f_printsi
			dc.b	"Byte/sector h byte 1=256 0=128",$9B,$FF
			lda	huh_data+7
			jsr	sub_error
			jsr	f_printsi
			dc.b	"Byte/sector l byte 0=256 1=128",$9B,$FF
			lda	huh_data+8
			jsr	sub_error
			jsr	f_printsi
			dc.b	"Drive present flag -returns $ff",$9B,$FF
			lda	huh_data+9
			jsr	sub_error
			jsr	f_printsi
			dc.b	"not used",$9B,$FF
			lda	huh_data+10
			jsr	sub_error
			jsr	f_printsi
			dc.b	"not used",$9B,$FF
			lda	huh_data+11
			jsr	sub_error
			jsr	f_printsi
			dc.b	"not used",$9B
			dc.b	$9b,"Press any key to continue",$FF
			jsr	get_key
			lda	#$7d
			jsr	f_ECHO
			rts
sub_error:
			sta	.dan_k
			jsr	f_printsi
			dc.b	" $",$FF
			lda	.dan_k
			jsr	f_drive_error
			jsr	f_printsi
			dc.b	"  ",$FF
			rts
;
.dan_k:			dc.b	0
huh_data:		dc.b	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
huh_data_d:		dc.b	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0






calc_sector_count:
			jsr	get_sector_count
			lda	#$ff
			sta	sector_density
			lda	huh_data+5
			cmp	#$08
			bne	.a
			lda	#$02
			sta	percom_density
			jmp	.b
.a:			lda	#$01
			sta	percom_density
			lda	huh_data+5
			cmp	#$00
			bne	.b
			lda	#$80
			sta	sector_density

.b:
			lda	huh_data+2
			cmp	#$00
			bne	.c
			lda	huh_data+3
			cmp	#$00
			bne	.c
			lda	#$ff
			sta	huh_data+2
			sta	huh_data+3

.c:			lda	huh_data+2
			sta	sector_per_trac+1			
			lda	huh_data+3
			sta	sector_per_trac

			lda	huh_data
			cmp	#$03
			bcs	.d
			lda	# low 1
			sta	huh_data
.d:			lda	huh_data
			sta	heads
			lda	#$00
			sta	heads+1


			jsr	zfr0
			lda	heads
			sta	fr0
			lda	heads+1
			sta	fr0+1
			jsr	ifp
			lda	#$00
			sta	cix
			jsr	fmove
			lda	sector_per_trac
			sta	fr0
			lda	sector_per_trac+1
			sta	fr0+1
			lda	#$00
			sta	cix
			jsr	ifp
			jsr	fmul
			jsr	fmove
			lda	percom_density
			sta	fr0
			lda	percom_density+1
			sta	fr0+1
			lda	#$00
			sta	cix
			jsr	ifp
			jsr	fmul
			jsr	fpi
			lda	fr0
			ldx	fr0+1
			sta	real_sector_count
			stx	real_sector_count+1
			sta	f_end_sector
			stx	f_end_sector+1
			rts

heads:			dc.w	0
sector_per_trac:	dc.w	0
percom_density:		dc.w	0
real_sector_count:	dc.w	0
f_end_sector:		dc.w	0
sector_density:		dc.b	-1			; $ff = dd   $80 = sd


;
f_drive_error:
			pha
			lsr
			lsr
			lsr
			lsr
			jsr	.a
			pla
			and	#$0f
.a:			cmp	#$0a
			bcs	.b
			ora	#$30
			bne	.c
.b:			adc	#$36
.c:			jsr	f_ECHO
			rts
;

f_pr_byte:
			ldx	#$00
f_pr_card:
			sta	fr0
			stx	fr0+1
			lda	#$00
			sta	cix
			jsr	ifp
			jsr	fasc
			ldy	#$00
.a:
			lda	(inbuff),y
			php
			and	#$7f
			iny
			jsr	f_ECHO
			plp
			bpl	.a
			rts

; 
;       Input character with escape processing 
;       -------------------------------------- 
; 
INCH:			jsr	GET_KEY
			CMP	#27
			BEQ	EXI
			rts 
EXI:			JMP	RETURN
; 
; 
; 
;================================= 
;       print/input routines 
;================================= 
; 
;       Print character 
;       --------------- 
; in: 
;       A       = character to print 
; out: 
;       all registers preserved 
; 
f_ECHO:			jsr	SAVER
			jsr	ZOUT
			jmp	RESALL
; 
ZOUT:			sta	ZTEMP+1
			lda	$0347
			PHA	
			lda	$0346
			PHA 
ZTEMP:			lda	#0
			ldx	#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. 
; 
f_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	f_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 
; 
; 
; 
;       Get character 
;       ------------- 
; out: 
;       A       = character (all others preserved) 
; 
GET_KEY:		jsr	ZGETCH
ZCH:			lda	#0
			rts 
; 
ZGETCH:			jsr	SAVER
			jsr	ZPHG
			sta	ZCH+1
			jmp	RESALL
; 
ZPHG:			lda	$E425
			PHA 
			lda	$E424
			PHA 
			rts 
; 
; 
;;--------------------------------------------------------------------
; 
; 
;       Get line 
;       -------- 
; in: 
;       XA      = buffer address 
;       Y       = maximum number of characters 
; out: 
;       @XA     = data, return ends input, a backspace works 
;       all registers preserved 
; notes: 
;       This routine will first clear the input window 
; 
GETLINE:		jsr	SAVER
			STY	ZCNT
			sta	ZSTOR+1
			STx	ZSTOR+2
; 
			ldy	#0
			lda	#$20
ZCLX:			jsr	ZSTOR
			INY 
			CPY	ZCNT
			bne	ZCLX
; 
			ldy	#0
ZINLP:			jsr	GET_KEY
			cmp	#$7E
			beq	ZBS
			cmp	#$9B
			beq	ZENDZ
			cpy	ZCNT
			beq	ZINLP
			jsr	f_ECHO
			jsr	ZSTOR
			INY 
			bne	ZINLP
; 
ZBS:			cpy	#0
			beq	ZINLP
			jsr	f_ECHO
			DEY 
			lda	#$20
			jsr	ZSTOR
			bne	ZINLP
ZENDZ:			jmp	RESALL
; 
ZSTOR:			sta	$FFFF,Y
			rts 
ZCNT:			dc.b 0
; 
; 
; 
;========================================= 
;       write/calculate diskette data 
;========================================= 
;       Write all data to disk 
;       ---------------------- 
WRITE:			ldx	#1      ; Write out all sectors 
			STx	SECTOR
			DEX 
			STx	SECTOR+1 ; begin at sector 1 
; 
			ldx	# high BOOTI
			ldy	# low BOOTI
			lda	#3
			jsr	WRBLOCK ; write boot program 
; 
			ldy	BOOTI+BMD+SDQBM ; # bitmap sectors 
			sty	TEMPQX
; 
			lda	DATASEC ; Zero beginning of bit map 
			LSR
			LSR
			LSR
			TAX 
			STx	TEMPQX+1
			lda	DATASEC
			AND #7
			TAY 
			lda	MASK,Y
			EOr	#$FF
; 
CLFIR:			sta	BITMAP,X
			lda	#0
			DEX 
			BPl	CLFIR
; 
			ldx	TEMPQX+1
			INX 
			dc.b	$2C
; 
NXB:			ldx	#0      ; clear bit map 
			lda	#$FF
SEA:			sta	BITMAP,X
			INX 
			bne	SEA
; 
			ldy	TEMPQX  ; if not last, then use as is 
			DEY 
			bne	WRBITX
; 
			ldx	NIBOFF  ; offset into nibble of last sector # 
			lda	MASK,X
			ldx	BYTOFF
CLREST:			sta	BITMAP,X ; clear out end 
			lda	#0
			INX 
			bne	CLREST
; 
WRBITX:			lda	#1      ; write one bitmap sector 
			ldx	# high BITMAP
			ldy	# low BITMAP
			jsr	WRBLOCK
			DEc	TEMPQX
			bne	NXB
; 
			lda	#0
			TAX 
CLMAPS:			sta	SCMAP,X ; Clear sector map and data sector 
			sta	DATSEC,X
			INX 
			bne	CLMAPS
; 
			ldx	BOOTI+MDD ; Sector map # of main directory 
			INX 
			STx	SCMAP+4 ; Put data sector # into sector map 
			lda	#1
			ldx	# high SCMAP ; Write sector map 
			ldy	# low SCMAP
			jsr	WRBLOCK
; 
			ldx	#11-1   ; Put directory name into data sector 
MVNAM:			lda	NAMTAB,X
			sta	DATSEC+DEFNAM,X
			DEX 
			BPl	MVNAM
			lda	#DESSUB+DESUSE
			sta	DATSEC+DESTA
			lda	#DDEX
			sta	DATSEC+DENBYT
			lda	#1
			ldx	# high DATSEC
			ldy	# low DATSEC
			jmp	WRBLOCK ; Write directory sector 
; 
; 
;       Write a block of sectors 
;       ------------------------ 
; 
WRBLOCK:		STx	DBUFHI  ; address 
			sta	NUMSECS ; number sectors to save 
			sty	DBUFLO
; 
			lda	DENSITY ; get density 
			ldx	SECTOR+1
			bne	ISTHD
			ldx	SECTOR
			CPx	#4
			bcs	ISTHD
			lda	#$80
ISTHD:			sta	DBYTLO
			ldx	#0
			TAY 
			bne	ISSNG
			ldx	#1
ISSNG:			STx	DBYTHI
; 
REPOUT:			lda	#'P'            ; Assume blocks all same density 
			sta	DCOMND
			lda	#$80
			sta	DSTATS
			jsr	XSIO
			BPl	OKWRTX
			jsr	f_printsi
			dc.b	"Error writing sector",$9b,-1
			jmp	RETURN
; 
OKWRTX:			INc	SECTOR
			bne	SK1
			INc	SECTOR+1
; 
SK1:			clc 
			lda	DBUFLO
			adc	DBYTLO
			sta	DBUFLO
			lda	DBUFHI
			adc	DBYTHI
			sta	DBUFHI
; 
			DEc	NUMSECS
			bne	REPOUT
			rts 
; 
; 
MASK:			dc.b $00,$80,$C0,$E0
			dc.b $F0,$F8,$FC,$FE,$FF
; 
NAMTAB:			dc.b "MAIN       "
; 
; 
;       Calculate all sector positions for given density 
;       ------------------------------------------------ 
; 
RECALC:			lda	MAXSEC  ; Get last sector # 
			sta	TEMPQX
			lda	MAXSEC+1
			sta	TEMPQX+1
; 
			lda	#0
			ldx	#3      ; divide by 8 for # of bms 
REPROL:			LSR	tempqx+1 
			ROr	TEMPQX
			ROR
			DEX 
			bne	REPROL
			LSR
			LSR
			LSR
			LSR
			LSR
			sta	NIBOFF  ; Offset into byte 
; 
			lda	TEMPQX
			ldx	TEMPQX+1 ; Get # of bit maps and offset into last 
; 
			BIT	DENSITY
			BPl	G2
			ASL
			PHA 
			TXA 
			ROL
			TAX 
			PLA 
			LSR
; 
G2:			sta	BYTOFF
			INX 
			STx	BOOTI+BMD+SDQBM ; # bit map sectors 
			TXA 
			clc 
			adc	#4      			; Add to bit map sector to get first 
			sta	BOOTI+MDD 			;  sector map sector 
			adc	#2      			; Get first data sector # 
			sta	DATASEC
			sta	BOOTI+BMD+SDDIR 		; Free sector scan start (direct) 
;			adc	#30     			; leave lots of room for dirs 
			adc	#70     			; leave lots of room for dirs 
			sta	BOOTI+BMD+SDALLOC 		; Free sec scan (data sec) 
			BIT	CURP				; check if to make this zero 
			BPl	ISSOMD
			lda	#0				; zero indicates no DOS 
ISSOMD:			AND	#$F8				; IMPORTANT... DOS starts at a block 
;                               				;  when doing alloc search 
			sta	BOOTI+EXTR+DDMAP		; Start DOS sector # 
; 
			SEC 
			lda	MAXSEC  			; # sectors total & free 
			sta	BOOTI+BMD+SDTOT
			SBc	DATASEC
			sta	BOOTI+BMD+SDFRE
			lda	MAXSEC+1
			sta	BOOTI+BMD+SDTOT+1
			SBc	#0
			sta	BOOTI+BMD+SDFRE+1
			lda	DENSITY ; set density & tracks 
			sta	BOOTI+DPD+SPDEN
			lda	TRACKS
			sta	BOOTI+DPD+SPTRK
; 
			lda	$D20A   ; get random number 
			sta	BOOTI+EXTR+DRAND
			rts 
; 
;

DENSITY:		ds.b	1       			; bytes per sector 
SECTORS:		ds.b	1       			; number of sectors per track 
MAXSEC:			ds.b	2       			; total number of sectors 
DENS:			ds.b	1       			; drive type/density flag 
; 
INCH8:			ds.b	1       			; eight inch drive flag, -1=true 
TRACKS:			ds.b	1       			; number of tracks on disk (B7=DS) 
TRKINX:			ds.b	1       			; index into trkst 
DENINX:			ds.b	1       			; density index 
; 
COUNT:			ds.b	1       			; general counter.. 
STACKP:			ds.b	1       			; return stack ptr 
CURP:			ds.b	1       			; current DOS image ptr 
DOSP:			ds.b	1       			; current # of .DOS files 
DOSPT:			ds.b	10      			; space for 10 ptrs into names 
DIRP:			ds.b	1       			; current ptr into names 
NAMES:			ds.b	130
ENTRY:			ds.b	25      			; area for reading entries 
DOSI:			ds.b	3+8+5   			; room for Dn:fname.DOS 
US:			ds.b	1
HIGHSP:			ds.b	1
; 
DATASEC:		ds.b	1       			; Beginning of data sectors 
TEMPQX:			ds.b	2
BYTOFF:			ds.b	1
NIBOFF:			ds.b	1
NUMSECS:		ds.b	1       			; Number sectors to write 
TABLEN:			ds.b	2       			; length of DOS file... 
; 
FMTBUF:			ds.b	256     			; buffer for format data 
BITMAP:			ds.b	256     			; bitmap buffer 
SCMAP:			ds.b	256     			; sector map buffer 
DATSEC:			ds.b	256     			; first directory sector 
TABLE:			ds.b	256     			; area to load DOS file 
; 
;
;
;------------------------------------------------------------------------~~~~~
; Ok the relocation marker is to tell the DOS we are now using
;more data at memlow..  
;
;
initz:			dc.b	0	; start address


zend:			dc.b	0	;end address

;
;------------------------------------------------------------------------~~
;
;                        *** END OF HANDLER ***
;------------------------------------------------------------------------~~
;
;	The rest of this relocator Crap Took me a week to figure out!!
;
;       Relocater: main entry
;       ---------------------
; in:
;       segtab  = table of segment descriptors
;               +00 = relocater table address
;               +02 = originate address of block
;               +04 = destination originate of block
;               +06 = address of block
;               +08 = number of bytes in block
;               +10 = destination address of block
;		+12 = Length of segment descriptor
;		+14 = this is the word location addjust memory

;
;
;
;
;       reltab  = relocater table
;               list of address of words to adjust      2 bytes each entry
;               list of address low bytes to adjust     2 bytes each entry
;               list of address high bytes to adjust    3 bytes each entry
;                       followed by their low bytes
;------------------------------------------------------------------------~
rlocate:

			lda	segtab+segaddress		; setting some stuff up for the relocator
			sta	segtab+dorgadr			; Passing Stuff to the relocator
			sta	segtab+blkdes			; Passing Stuff to the relocator
			lda	segtab+segaddress+1		; setting some stuff up for the relocator
 			sta	segtab+dorgadr+1		; Passing Stuff to the relocator
 			sta	segtab+blkdes+1			; Passing Stuff to the relocator
; 			jsr	rlocate				; ok do relocation of code!!!!!!!



			lda	#0				; set ptr to segment 0
			sta	segment

segloop:		ldx	segment
			lda	segtab+rettad,x			; get address of relocater table
			sta	relget+1
			lda	segtab+rettad+1,x
			sta	relget+2
			ora	relget+1 			; if zero, then end of segtab
			bne	havseg
			rts

havseg:			sec					; calculate offset to add to segment
			lda	segtab+dorgadr,x
			sbc	segtab+orgadr,x
			sta	zoffset
			lda	segtab+dorgadr+1,x
			sbc	segtab+orgadr+1,x
			sta	zoffset+1

zwordlp:		jsr	getzwp
			beq	zlbytlp				; if zero, go adjust low bytes

			lda	(zwptr),y			; add offset to address
			clc
			adc	zoffset
			sta	(zwptr),y
			iny
			lda	(zwptr),y
			adc	zoffset+1
			sta	(zwptr),y
			jmp	zwordlp

zlbytlp:		jsr	getzwp				; get address
			beq	zhbytlp				; if zero, then adjust high bytes

			lda	(zwptr),y
			clc
			adc	zoffset
			sta	(zwptr),y
			jmp	zlbytlp

zhbytlp:		jsr	getzwp				; get address
			beq	zmovlp				; jump if end of table

			jsr	relget				; get low byte
			clc
			adc	zoffset
			lda	(zwptr),y
			adc	zoffset+1
			sta	(zwptr),y
			jmp	zhbytlp
			rts

zmovlp:

			lda	segment+segmove			; gonna check the memover
			cmp	#seg_off			; $ff we quit subroutein
			bne	.zmovlp				; most of been something other than ff do mem move
			rts					; ok time to exit subroutien 	
.zmovlp:		ldx	segment				; get block start and to address
			lda	segtab+blkadr,x
			sta	zmovfr+1
			lda	segtab+blkadr+1,x
			sta	zmovfr+2

			lda	segtab+blkdes,x
			sta	zmovto+1
			lda	segtab+blkdes+1,x
			sta	zmovto+2

                                				; get # pages to move
			ldy	segtab+blkbyt+1,x

			ldx	#0
zmovfr:			lda	$ffff,x
zmovto:			sta	$ffff,x
			inx
			bne	zmovfr
			inc	zmovfr+2
			inc	zmovto+2
			dey
			bpl	zmovfr				; move even # pages, so may move up to 256;;
								;  bytes more than needed
			lda	segment
			clc
			adc	#seglen				; go to next segment
			sta	segment
			jmp	segloop


relget:			lda	$ffff
			inc	relget+1
			bne	nc1
			inc	relget+2
nc1:			rts

getzwp:			jsr	relget				; top of word adjust loop
			sta	zwptr				; save address of word
			jsr	relget
			ldy	#0
			sta	zwptr+1
			ora	zwptr				; check if at end
			rts

segment:		dc.b	0				; current segment
zoffset:		dc.w	0				; offset to add to addresses
;
;------------------------------------------------------------------------
;
;		       Relocation data table
;       	       ---------------------
;		We resolve Word locations First!
;
;	Things like	Lda $5000
;			Sta $5000
;			lda $5000,y
;			jsr $5000
;			jmp $5000
;
;	Well you get the Idea..
;
;------------------------------------------------------------------------
;
; $3000  tabel
;
rtable1:
			dc.w	w_f01+1
			dc.w	w_f02+1
			dc.w	w_f03+1
			dc.w	w_f04+1
			dc.w	w_f05+1
			dc.w	w_f06+1
			dc.w	w_f07+1
			dc.w	w_f08+1
			dc.w	w_f09+1
			dc.w	w_f10+1

			dc.w	w_f11+1
			dc.w	w_f12+1
			dc.w	w_f13+1
			dc.w	w_f14+1
			dc.w	w_f15+1
			dc.w	w_f16+1
			dc.w	w_f17+1
			dc.w	w_f18+1
			dc.w	w_f19+1
			dc.w	w_f20+1

			dc.w	w_f21+1
			dc.w	w_f22+1
			dc.w	w_f23+1
			dc.w	w_f24+1
			dc.w	w_f25+1


			dc.w	0			; ok done with this part
;
;
;Ok these are low Byte Locations..  only one byte changed!!
;   OK Resolving Low Byte's First
;
			dc.w	w_lb_1+1
			dc.w	w_lb_2+1
			dc.w	0			; ok done with this part
;------------------------------------------------------------------------
;Now to resolve High Bytes...... Only one byte to change!
; Here is what is needed...  location of high byte in .word format
; Second thing needed is the low byte of the address you are wishing
;to adjust!
;
			dc.w	w_hb_1+1
			dc.b	LRTS
			dc.w	w_hb_2+1
			dc.b	NODMSG

			dc.w	0			; ok we are done
			dc.b	0			; ok we are done



;------------------------------------------------------------------------
;End of relocation Table!!!
;master table:
;
segtab:
			dc.w	rtable1          	; address of relocater table
			dc.w	initz1           	; assembly start address
			dc.w	0               	; relocate to address (filled in)
			dc.w	initz1           	; absolute start address of block
			dc.w	zend1-initz1      	; size of block
			dc.w	0               	; destination of block (filled in)
			dc.w	0       		; END of segments
			dc.w	$3000			;segaddress memory to adgust by.
			dc.b	seg_off			; -1 meand do not mem move


win_end:


;
