;------------------------------------------------------------------------
;        title   'RamDisk handler for SpartaDOS and	Real.dos'
;
;		Copyright 2010 Intergrated Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;  Please do not share this source!
;------------------------------------------------------------------------
;			This program is to replace
;		      		rd.com
;			With Ram_disk.com
;
;	Code History.     Init design,   August 2005
;			  last updaed 8/21/2005	sjc
;		Added registration code 6/19/2010
;
;
;
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;------------------------------------------------------------------------
; 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
;------------------------------------------------------------------------

; File revision history. Varables

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


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; File revision history. Version Number in hex
;
file_ver:		equ   $12
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the Month in dec for the first time this revision 
;was compiled
;
c_month:		equ   8
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the day in dec for the first time this revision 
;was compiled
c_day:			equ   28
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the year in dec for the first time this revision 
;was compiled
c_year:			equ   2012
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Type of compiler used in program
;
; 0 = unknown Compiler
; 1 = xasm
; 2 = mac_65
; 3 = basic
; 4 = compiled basic xl
; 5 = C65
; 6 = Action
;
; We can add more as time goes on!
;
;
xasm:			equ	1
mac_65:			equ	2 
basic:			equ	3
basicxl:		equ	4
c65:			equ	5
action:			equ	6

file_compiler:		equ	xasm
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; is this relocatable code ?
; anyother value other than 1 or 2 would be unknown
r..yes:			equ	1
r..no:			equ	2
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define if it can be relocated
l_relocatable:		equ	r..yes
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define if it can be relocated
r..crl:			equ	$7d
r..crlf:		equ	$9b
r..space:		equ	$20

l_frstscreenbyte:	equ	r..space
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;
;
;

			.include	equates
			.include	globals
			.include	macros

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

			jsr	os_off
			mea	tsr,s_ptr
			lda	#ramdisk
			jsr	seclvl 			; call routing checking 
			bmi 	.aa

			jsr	os_on
	 		jsr	printsi
			dc.b	$9b
			dc.b	"   "
			dc.b	<+128>,"==>"
			dc.b	<+128>," Ramdisk already Present  "
			dc.b	<+128>,"<=="
			dc.b	$9b
			dc.b	"   "
			dc.b	<+128>,"==>"
			dc.b	<+128>,"And Registered with the OS"
			dc.b	<+128>,"<=="
			dc.b	$9b,-1
			rts				; we are gonna Exit

.aa:
bank_finder:
			lda	PORTB
			PHA 
			lda	#0
			sta	SDMCTL
			jsr	jiffy
			ldx	#31		;$1F
.a:			lda	ram_lookup,X
			eor	PORTB
			and	#$7E
			eor	PORTB
			sta	PORTB
			lda	$7000
			sta	ram_ref,X
			lda	ram_lookup,X
			sta	$7000
.b:			lda	$7001
			sta	Reference,X
			lda	ram_lookup,X
			asl
			sta	$7001
			DEX 
			bpl	.a
			lda	#$FF
			sta	$3000
			lda	#$EE
			sta	$3001
.bb:			INX 
			lda	ram_lookup,X
			eor	PORTB
			and	#$7E
			eor	PORTB
			sta	PORTB
			lda	ram_lookup,X
			cmp	$7000
			bne	.c
			lda	ram_lookup,X
			asl
			cmp	$7001
			bne	.c
			cpx	#$1F
			bne	.bb
			INX 
.c:			stx	total_ram_banks
			ldx	#0
.d:			lda	ram_lookup,X
			eor	PORTB
			and	#$7E
			eor	PORTB
			sta	PORTB
			lda	ram_ref,X
			sta	$7000
			lda	Reference,X
			sta	$7001
			INX 
			cpx	#$20
			bne	.d
			PLA 
			sta	PORTB
			lda	#$22
			sta	SDMCTL
			jsr	jiffy




;			jsr	printsi
;			dc.b	"Ramdisk  Ver 1.0  9-10-2005"
;			dc.b	$9b
;			dc.b	"Copyright 2010 ILS"
;			dc.b	" "
;			dc.b	$9b,$9b
;			dc.b	-1
Show_machine: 

			jsr	printsi
			dc.b	$9b,$9b

			dc.b	"Computer is: "
			dc.b	-1

			ldx	total_ram_banks
			cpx	#4
			bcc	.a
			bne	.b
			jsr	printsi
			dc.b	"128K - 130XE  (64K)",$9b
			dc.b	-1

			jmp	look_for_drv_num
;
.a:			jsr	printsi
			dc.b	"800XL unmodified",$9b
			dc.b	"-- No RAM Disk Installed",$9b
			dc.b	-1
			RTS 
;
.b:			cpx	#$0C
			beq	.c
			bcs	.d
			jsr	printsi
			dc.b	"192K - 130XE  (128K)",$9b
			dc.b	-1

			jmp	look_for_drv_num
;
.c:			jsr	printsi
			dc.b	"256K - 800/1200XL (192K)",$9b
			dc.b	-1

			jmp	look_for_drv_num
;
.d:			cpx	#$10
			bne	.e
			jsr	printsi
			dc.b	"320K - 130XE  (256K)",$9b
			dc.b	-1


			jmp	look_for_drv_num
;
.e:			inc	lotta_ram
			inc	lotta_ram
			jsr	printsi
			dc.b	"576K - 130XE (512K)",$9b
			dc.b	-1



look_for_drv_num:	ldy	# bufoff
			lda	(comtab),Y
			CLC 
			adc	#lbuf
			TAY 
			lda	(comtab),Y
			cmp	#$9B
			bne	read_cli

			jsr	printsi
			dc.b	"-- No Drive Number Specified",$9b
			dc.b	-1
			RTS 
;
I_ZCRNAME:		JMP	$ffff	;8ae
;
no.format:		dc.b	-1
L32DC:			dc.b	$00


read_cli:		lda	comtab
			CLC 
			adc	# low ZCRNAME
			sta	I_ZCRNAME+1
			lda	comtab+1
			adc	# high ZCRNAME
			sta	I_ZCRNAME+2
			jsr	I_ZCRNAME
			bne	.parm.error
			ldy	#COMFNAM+1
			lda	(comtab),Y
			and	#$0F
			sta	ram_drive
			jsr	I_ZCRNAME
			beq	.y
			ldy	#COMFNAM+3
			lda	(comtab),Y
			cmp	#'/'		;$2F
			bne	.parm.error
.a:			INY 
			lda	(comtab),Y
			cmp	#$9B
			beq	.y
			cmp	#'E'		;$45
			bne	.bb
			lda	#4
			sta	reserve_ram
			bne	.a
.bb:			cmp	#'N'		;$4E
			bne	.parm.error
			inc	no.format
			jmp	.a
;
.parm.error:		jsr	printsi
			dc.b	"-- Parameter Error",$9b
			dc.b	-1
			RTS 
;
.y:			lda	DOSINI
			sta	initz+1
			lda	DOSINI+1
			sta	initz+2
			lda	reserve_ram
			beq	install.drv
			lda	total_ram_banks
			cmp	#4
			bne	.z
			jsr	printsi
			dc.b	"Error -- No Extra RAM For RAMDISK",$9b
			dc.b	-1
			RTS 
;
.z:			jsr	printsi
			dc.b	"<RAMDISK Uses 64K Less Memory>",$9b
			dc.b	-1


install.drv:


			
			lda	memlo			; setting some stuff up for the relocator
			sta	segtab+dorgadr		; Passing Stuff to the relocator
			sta	segtab+blkdes		; Passing Stuff to the relocator
			lda	memlo+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!!!!!!!

			jsr	set_ramdisk		;added code

lb01:			lda	# low initz
			sta	DOSINI
hb01:			lda	# high initz
			sta	DOSINI+1
			SEC 
			lda	comtab
			sbc	# low LSIO
			sta	BUFRLO
			lda	comtab+1
			sbc	# high LSIO
			sta	BUFRLO+1
			ldy	#0
			lda	(BUFRLO),Y
w01:			sta	dos_xiov+1
lb02:			lda	# low ram_siov
			sta	(BUFRLO),Y
			INY 
			lda	(BUFRLO),Y
w02:			sta	dos_xiov+2
hb02			lda	# high ram_siov
			sta	(BUFRLO),Y
w03:			jsr	L356D
			bit	no.format
			bmi	show.fmt
			RTS 
;
jiffy:			lda	RTCLOK+2
.a:			cmp	RTCLOK+2
			beq	.a
			RTS 
;
show.fmt:		jsr	printsi
			dc.b	"   <RAM Disk Formatted SD>",$9b
			dc.b	-1

format.sd:		lda	PORTB
			PHA 
			ldx	reserve_ram
			and	#$FD
			eor	ram_lookup,X
			and	lotta_ram
			eor	PORTB
			sta	PORTB
			lda	#0
			TAY 
.a:
			STA 	ram_window,Y
			STA 	ram_window+256,Y
			STA 	ram_window+512,Y
			STA 	ram_window+768,Y
			INY 
			bne	.a


			ldy	#42
.b:			lda	boot_sector,Y
			sta	ram_window,Y
			DEY 
.c:			bpl	.b
			lda	total_ram_banks
			SEC 
			sbc	reserve_ram
			TAX 
			stx	L32DC
			ldy	#0
.d:			lda	.j+1,X
			sta	ram_window+11,Y
			INX 
			INY 
			cpy	#4
			bne	.d
			lda	L32DC
			lsr
			lsr
			TAX 
.e:			jsr	.h
			lda	.i+1
			CLC 
			adc	#$40
			sta	.i+1
			lda	.i+2
			adc	#0
			sta	.i+2
			DEX 
			bne	.e
			ldx	L32DC
			lda	L34D4,X
			sta	ram_window+15
			CLC 
			adc	#2
			sta	ram_window+9
			lda	L34D5,X
			sta	ram_window+128
.f:			lda	L34D6,X
			sta	BUFRLO
			lda	L34D7,X
			sta	BUFRLO+1
			CLC 
			ldy	#4
			lda	ram_window+9
			adc	#1
			sta	(BUFRLO),Y
			ldx	#0
			ldy	#$80
.g:			lda	r_directory,X
			sta	(BUFRLO),Y
			INX 
			INY 
			cpx	#$11
			bne	.g
			PLA 
			sta	PORTB
			RTS 
;
.h:			ldy	#0
			lda	#$FF
.i:			sta	ram_window+128,Y
			INY 
.j:			cpy	#$40
			bne	.i
			RTS 
;
l34b8:
			dc.b	$00,$02,$FB,$01,$00,$04,$FB,$03
			dc.b	$00,$06,$FA,$05,$00,$08,$FA,$07
			dc.b	$00,$0A,$F9,$09,$00,$0C,$F9,$0B
			dc.b	$00,$0E,$F8,$0D

L34D4:			dc.b	$00
L34D5:			dc.b	$10
l34d6:			dc.b	$f8
L34D7:			dc.b	$0F

			dc.b	$01,$07,$00,$41,$01,$07,$00,$41
			dc.b	$02,$03,$80,$41,$02,$03,$80,$41
			dc.b	$03,$01,$00,$42,$03,$01,$00,$42
			dc.b	$04,$00,$80,$42,$04,$00,$80,$42
r_directory:
			dc.b	$08,$00,$00,$11,$00,$00
			dc.b	"MAIN       "

boot_sector:
			dc.b	$00,$03,$00,$07,$e0,$07,$4c
			dc.b	$80,$30,$03,$00,$00,$04,$fb,$03
			dc.b	$01,$02,$00,$20,$00,$00,$00
			dc.b	"RAM-DISK",$00
			dc.b	$80,$22,$00,$00,$00,$00,$00,$00
			dc.b	$16,$00,$00,$00


printsi:		PLA 
			sta	.b+1
			PLA 
			sta	.b+2
.a:			inc	.b+1
			bne	.b
			inc	.b+2
.b:			lda	$FFFF
			cmp	#$FF
			beq	.c
			jsr	echo
			jmp	.a
;
.c:			lda	.b+2
			PHA 
			lda	.b+1
			PHA 
			RTS 
;
echo:			ldx	#0
			stx	ICBLL
			stx	ICBLL+1
			ldy	#$0B
			sty	ICCOM
			jmp	CIOV
;
initz:			jsr	$0000


L356D:
lb03:			lda	# low zend
			sta	MEMLO
hb03:			lda	# high zend
			sta	MEMLO+1
			RTS 
;
ram_siov:
			lda	DDEVIC
			cmp	#$31
			bne	dos_xiov
			lda	DUNIT
			cmp	ram_drive
			beq	ram_driver
dos_xiov:		jmp	$0000


ram_driver:		ldx	PORTB
			SEI 
			lda	#0
			sta	NMIEN
			lda	DBUFLO
			sta	BFENLO
			lda	DBUFLO+1
			sta	BFENLO+1
			lda	DCOMND
			cmp	#'S'		;$53
			bne	ck_rc
w09:			lda	byte.sector.low
			lsr
			lsr
			eor	#48		;$30
w10:			ldy	sectors_per_trac
			cpy	#26		;$1A
			bne	.a
			ORA	#$80
.a:			ldy	#0
			sta	(BFENLO),Y
			INY 
			lda	#$FF
			sta	(BFENLO),Y
			bmi	show_stat
ck_rc:			cmp	#'N'
			bne	ck_set_cfg
			ldy	#$0B
ck_rc.a:
w11:			lda	percom_set,Y
			sta	(BFENLO),Y
			DEY 
			bpl	ck_rc.a
			bmi	show_stat
ck_set_cfg:		cmp	#'O'
			bne	ck_fmt
			ldy	#7
			lda	(BFENLO),Y
w12:			sta	byte.sector.low
			ldy	#3
			lda	(BFENLO),Y
w13:			sta	sectors_per_trac
show_stat:
w14:			jmp	d_status
;
ck_fmt:			cmp	#'!'			;$21
			bne	ck_fdd
format_sectors:		ldy	#0
			lda	#$FF
			sta	(BFENLO),Y
			INY 
			sta	(BFENLO),Y
			bne	show_stat


ck_fdd:			cmp	#$22		;"'" dual density
			bne	sector_rw
			lda	#26
w15:			sta	sectors_per_trac
			lda	#$80
w16:			sta	byte.sector.low
			bne	format_sectors



sector_rw:		cmp	#'R'
			beq	Read.write_sec
			cmp	#'W'
			beq	Read.write_sec
			cmp	#'P'
			beq	Read.write_sec
w20:			jmp	error_139
;
Read.write_sec:		lda	#0
			sta	BUFRLO
			lda	DAUX1
			SEC 
			sbc	#1
			sta	BUFRLO+1
			lda	DAUX1+1
			sbc	#0
			ASL	BUFRLO+1
			rol
			bit	DBYTLO
			bmi	rw.a
w17:			ASL	byte.sector.low
			ASL	BUFRLO+1
			rol
rw.a:			PHA 
			lda	BUFRLO+1
			lsr
			lsr
			ROR	BUFRLO
			ORA	#$40
			sta	BUFRLO+1
			TXA 
			and	#$FE
			sta	PORTB
			lda	DCOMND
			cmp	#'R'		;$52
			beq	rw.c
			ldy	#0
rw.b:			lda	(BFENLO),Y
w05:			sta	rd_buffer,Y
			INY 
			cpy	DBYTLO
			bne	rw.b
rw.c:			PLA 
			CLC 
w21:			adc	reserve_ram
w18:			cmp	total_ram_banks
			bcs	error_139


			TAY 
			lda	PORTB
			and	#$FD
w19:			eor	ram_lookup,Y
w22:			and	lotta_ram
			eor	PORTB
			sta	PORTB



			ldy	#0
			lda	DCOMND
			cmp	#'R'		;$52
			bne	rw.f
rw.d:			lda	(BUFRLO),Y
w06:			sta	rd_buffer,Y
			INY 
			cpy	DBYTLO
			bne	rw.d
			TXA 
			and	#$FE
			sta	PORTB
			ldy	#0
rw.e:
w08:			lda	rd_buffer,Y
			sta	(BFENLO),Y
			INY 
			cpy	DBYTLO
			bne	rw.e
			beq	d_status
rw.f:
w07:			lda	rd_buffer,Y
			sta	(BUFRLO),Y
			INY 
			cpy	DBYTLO
			bne	rw.f
d_status:		ldy	#1
			dc.b	$2c
error_139:		ldy	#139
			stx	PORTB
			lda	#$E0
			sta	NMIEN
			CLI 
			TYA 
			RTS 
;
total_ram_banks:	dc.b	$00
reserve_ram:		dc.b	$00


percom_set:		dc.b	$28
			dc.b	$01
			dc.b	$12
sectors_per_trac:	dc.b	$00
			dc.b	$00
			dc.b	$00
			dc.b	$00
byte.sector.low:	dc.b	$80
			dc.b	$FF
			dc.b	$00
			dc.b	$00
			dc.b	$00


;ram_drive:		dc.b	$01

ram_lookup:		dc.b	$60
			dc.b	$64
			dc.b	$68
			dc.b	$6c
			dc.b	$20
			dc.b	$24

			dc.b	$28
			dc.b	$2c
			dc.b	$40
			dc.b	$44
			dc.b	$48
			dc.b	$4c
			dc.b	$00
			dc.b	$04

			dc.b	$08
			dc.b	$0C
			dc.b	$62
			dc.b	$66
			dc.b	$6a
			dc.b	$6e
			dc.b	$22
			dc.b	$26
			dc.b	$2a
			dc.b	$2e
			dc.b	$42
			dc.b	$46
			dc.b	$4a
			dc.b	$4e
			dc.b	$02
			dc.b	$06
			dc.b	$0a
			dc.b	$0E


lotta_ram:		dc.b	$7C


rd_buffer:		ds.b	256

zend:			ds.b	0
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;End of handler
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ram_ref:		ds.b	32
Reference:		ds.b	32
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;		       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..
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
rtable:

			dc.w	w01+1
			dc.w	w02+1
			dc.w	w03+1
;			dc.w	w04+1
			dc.w	w05+1
			dc.w	w06+1
			dc.w	w07+1
			dc.w	w08+1
			dc.w	w09+1
			dc.w	w10+1
			dc.w	w11+1
			dc.w	w12+1
			dc.w	w13+1
			dc.w	w14+1
			dc.w	w15+1
			dc.w	w16+1
			dc.w	w17+1
			dc.w	w18+1
			dc.w	W19+1
			dc.w	w20+1
			dc.w	w21+1
			dc.w	w22+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	lb01+1
			dc.w	lb02+1
			dc.w	lb03+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	hb01+1
			dc.b	initz	;This will store just the low byte for reference
			dc.w	Hb02+1
			dc.b	ram_siov
			dc.w	hb03+1
			dc.b	zend
			dc.w	0			; ok we are done
			dc.b	0			; ok we are done


mepoint:		ds.b	0


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;			               *** END OF HANDLER ***
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;
;       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
;
;       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	#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

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
;
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;End of relocation Table!!!
;
;
segtab:
			dc.w	rtable				; address of relocater table
			dc.w	initz				; assembly start address
			dc.w	0				; relocate to address (filled in)
			dc.w	initz				; absolute start address of block
			dc.w	zend-initz			; size of block
			dc.w	0				; destination of block (filled in)
			dc.w	0				; END of segments


;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;going to see if the wedge is present, if it is present then
;map out the extended ram
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check_for_wedge:
			jsr	os_off
			mea	tsr,s_ptr
			lda	#wedge
			jsr	seclvl 			; call routing checking 
			bmi 	h.a
			LDA 	#4
			STA 	reserve_ram
	 		jsr	printsi
			dc.b	" "
			dc.b	<+128>,"=========>"
			dc.b	<+128>,"Wedge detected"
			dc.b	<+128>,"<========="

			dc.b	$9b,-1
h.a:
			jsr	os_on
			rts




;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set_ramdisk:
			jsr	os_off
			mea	tsr,s_ptr
			lda	#ramdisk
			jsr	setlvl
w31:			lda 	reserve_ram
			cmp	#$04
			bne	.a

			mea	tsr,s_ptr
			lda	#ram_ex
			jsr	setlvl
.a:
			jsr	os_on

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

seclvl:	    		sty	.tempy
			stx	.tempx
			cmp	#0			;sec lvl of zero?
			beq	.passed			;yes..they have it
			tay				;-1 for shifting
			lda	#128
			sta	.temp			;initialize mask
			lda	#0
			sta	.temp+1
			sta	.temp+2
			sta	.temp+3
			dey
			beq	.mask			;if it was one, no need to shift
.shloop:	   	lsr	.temp			;shift bits down to form mask
			ror	.temp+1
			ror	.temp+2
			ror	.temp+3
			dey
			bne	.shloop
			ldy	#0
.mask:			lda	.temp,y			;now, AND off all the bits
			and	(s_ptr),y
			sta	.temp,y
			iny
			cpy	#4
			bne	.mask
.check:			lda	.temp			;next, OR all the bytes...if any one
			ora	.temp+1			; is > 0, then they passed (whew!)
			ora	.temp+2
			ora	.temp+3
			cmp	#0
			beq	.failed
.passed:	   	lda	#0
			beq	.return
.failed:	   	lda	#$80
.return:	   	php
			ldy	.tempy
			ldx	.tempx
			plp
			rts

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


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

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

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

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

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





win_end:		ds.b	0
;
;

