
	page
;	*************************************************
;	-						-
;	-	BIOS8502 code (for read only)		-
;	-						-
;	*************************************************
;
;	10 May 85
;
;      COMMON EQUATES
;
pointer	equ	20h

datchn	equ	11		; use data channel #11
cmdchn	equ	15		; use comand channel #15
;
;	KERNAL EQUATES
;
serial		equ	00A1Ch
D2PRA		equ	0DD00h
D1SDR		equ	0DC0Ch
D1ICR		equ	0DC0Dh
clk$bit		equ	10h


K$set$bnk	equ	0FF68h

K$setlfs	equ	0FFBAh	; setup a logical file
				;I A=logical file #
				;  X=device # (0-31)
				;  Y=seconday command (FF if nane)

K$setnam	equ	0FFBDh	; set up file name for OPEN
				;I A=name length
				;  X=low byte pointer to name
				;  Y=high byte pointer to name

K$open		equ	0FFC0h	; open a logical file (after SETLFS & SETNAM)
				;O A = error # (1,2,4,5,6,240)

K$chkout	equ	0FFC9h	; open a channel for output
				;I X = logical file #
				;O A = error #(0,3,5,7)

K$clrchn	equ	0FFCCh	; clears ALL I/O channel 

K$chkin		equ	0FFC6h	; open a channel for input
				;I X = logical file #
				;O A = errors #(0,3,5,6)

K$chrin		equ	0FFCFh	; get a character from input channel
				;O A=input character 

K$chrout	equ	0FFD2h	; output a character to output channel
				;I A =output char

K$clall		equ	0FFE7h	; close all open files

K$close		equ	0FFC3h	; close a logical file
				;I A = logical channel # to be closed
				;O A = error #(0,240)

K$readst	equ	0FFB7h	; read status byte
				;O A = status
	PAGE
;
;
; **** THIS IS THE COMMAND LOOP ****
;
boot$02$code:
	@lda	0,#		; turn on the kernal
	@sta	force$map	;
	@jmp	(0fffch)	; jmp to its start

boot$size	equ	$-boot$02$code

BB	equ	bios$02-$	; BIOS BIAS

;
bios$65$code:
	@lda	0,#		;-K
	@sta	vic$data	;-K
	@JSR	VICIO+BB	;-K  go find and do requested operation
CMDLP:
	@sei
	@lda	3eh,#		;?K  set up Z80 memory map as required
	@sta	force$map	;-K
	@jmp	enable$z80	;-K

	PAGE
;
;
;
;
; **** IO COMMAND DISPATCH ROUTINE ****
;
VICIO:
	@CLD			;-K  clear to binary mode
	@LDA	vic$cmd		;-K  get the command
	@bne	read		;-K   0=initialize
				;     1=read
	page
;
;
;
initilize:			;   initialize the 8502
	@ldx	0,#		;-K
	@stx	force$map	;-K  enable the kernal
	@stx	VIC+26		;+K  turn off VIC interrupts

	@ldx	low(irqs+BB),#
	@ldy	high(irqs+BB),#
	@stx	314h		;+K  IRQ vector
	@sty	315h
	@stx	316h		;+K  BRK vector
	@sty	317h
	@stx	318h		;+K  NMI vector
	@sty	319h
	@jmp	opencm+BB	;+K  go open channel to disk drive

	PAGE
;
; **** DISK SECTOR READ ****
;
READ:
	@lda	@dma		;-K
	@sta	pointer		;-K
	@lda	@dma+1		;-K
	@sta	pointer+1	;-K

setdrv:
	@lda	vic$trk		;-K
	@sta	F$rd$trk+BB	;-K
	@JSR	BINASC+BB	;-K
	@STX	dskcmd$T$h+BB	;-K
	@STA	dskcmd$T$l+BB	;-K

	@lda	vic$sect	;-K
	@sta	F$rd$sect+BB	;-K
	@JSR	BINASC+BB	;-K
	@STX	dskcmd$S$h+BB	;-K
	@STA	dskcmd$S$l+BB	;-K

	@lda	fast		;-K
	@bne	read$F		;-K
	@sta	force$map	;-K A=0 if we did not branch
	@ldx	datchn,#	;+K
	@jsr	K$chkin		;+K
	@bcs	disk$changed	;+K
	@jsr	K$clrchn	;+K  clear the input channel for now

	@JSR	SETUP+BB	;+K
	@JSR	CKINDT+BB	;+K

	@ldy	0,#		;+K
READ1:
	@JSR	K$chrin		;+K  get a byte from the KERNAL
	@STA	(pointer),y	;-K  save it to the DMA pointer
	@iny			;+K  advance the buffer pointer
	@BNE	READ1		;+K  loop back if not past buf end
	@jmp	K$clrchn	;+K  CLEAR CHANNEL


DISK$CHANGED:
	@lda	0FFh,#		;?K
	@skip2

fst$error:
	@lda	0dh,#
	@sta	vic$data	;?K
	@jmp	CMDLP+BB

;
;
;
read$F:
	@lda	0,#		;-K
	@sta	force$map	;-K
	@ldx	cmdchn,#	;+K
	@jsr	K$chkout	;+K
	@bcs	fst$error	;+K
;	@ldx	0,#		;+K
	@ldy	Fcmd$lng,#	;+K
sendf:
	@lda	Fcmd$buf+BB-1,y	;+K
	@jsr	K$chrout	;+K
;	@inx
	@dey
	@bne	sendf

	@jsr	K$clrchn	;+K
	@bit	D1ICR		;+K
	@ldx	F$rd$count+BB	;+K
rd$sector:
	@jsr	read$byte+BB	;+K
	@and	0eh,#		;+K  mask off error bits
	@bne	fst$error

	@ldy	0,#
rd$buffer:
	@jsr	read$byte+BB	;+K
	@sta	(pointer),y	;+K
	@iny			;+K
	@bne	rd$buffer	;+K

	@inc	pointer+1	;+K
	@dex			;+K
	@bne	rd$sector	;+K

clk$hi:
	@lda	d2pra		;+K
	@and	0ffh-clk$bit,#	;+K
	@sta	d2pra		;+K
	@rts			;+K

;
;  * DEVICE MISSING, CLEAN UP ERROR *
;
MISDSK:
	@LDA	00fh,#		;+K  SET ERROR CODE
	@STA	vic$data	;+K  writes to RAM under ROM
	@JMP	CMDLP+BB	;+K

	PAGE
;
; **** OPEN DISK COMMAND CHANNEL ****
;
opencm:
	@LDA	cmdchn,#	;+K
	@CLC			;+K
	@JSR	K$close		;+K
	@LDA	cmdchn,#	;+K
	@sta	fast		;+K set fast flag 
	@LDX	8,#		;+K
	@TAY			;+K
	@JSR	K$setlfs	;+K

	@LDA	0,#		;+K
	@sta	serial		;+K clear fast serial indicator 
	@TAX			;+K
	@JSR	K$set$bnk	;+K

	@LDA	4,#			;+K
	@LDX	low(U0POINT+BB),#	;+K
	@LDY	high(U0POINT+BB),#	;+K
	@JSR	K$setnam		;+K
	@JSR	K$open			;+K
	@bcs	misdsk

	@jsr	K$readst
	@ROL	A		;+K  GET MSB TO CARRY
	@BCS	MISDSK		;+K  DEVICE MISSING IF CARRY SET
	@bit	serial		;+K  test for fast device
	@bvs	no$dt$open	;+K  do not open data channel if fast

	page
;
; **** OPEN DISK DATA CHANNEL ****
;
OPENDT:
	@LDA	datchn,#	;+K
	@CLC			;+K
	@JSR	K$close		;+K
	@LDA	datchn,#	;+K
	@LDX	8,#		;+K
	@LDY	8,#		;+K
	@JSR	K$setlfs	;+K

	@LDA	0,#		;+K
	@sta	fast		;+K  clear fast flag
	@TAX			;+K
	@JSR	K$set$bnk	;+K

	@LDA	1,#			;+K
	@LDX	low(POUND+BB),#		;+K
	@LDY	high(POUND+BB),#	;+K
	@JSR	K$setnam		;+K
	@JSR	K$open			;+K
	@bcs	misdsk
no$dt$open:
	@rts

	PAGE
;
;
SETUP:
	@JSR	CKOTCM+BB	;+K
	@LDY	dskcmd$lng,#	;+K
SETUP2:
	@LDA	DSKCMD+BB-1,y	;+K
	@JSR	K$chrout	;+K
	@DEY			;+K
	@BNE	SETUP2		;+K

	@JSR	K$clrchn	;+K
	@JSR	CKINCM+BB	;+K
	@BEQ	SETUP3		;+K

SETUP5:
	@LDA	0dh,#		;+K  get error flag
	@STA	vic$data	;+K  writes to RAM under ROM	
setup3:
	@JMP	K$clrchn	;+K


	page
;
;
;
read$byte:
	@sei
	@lda	d2pra
	@eor	clk$bit,#
	@sta	d2pra
	@lda	8,#
in$1:
	@bit	d1icr
	@beq	in$1
	@lda	d1sdr
	@rts


	page
;
;	handle all interrupts in BIOS 8502 (throw them away)
;
irqs:
	@lda	CIA$1+int$ctrl
	@lda	CIA$2+int$ctrl
	@lda	0fh,#
	@sta	VIC+25

;
;	system saved memory config, Y, X and A before getting here
;
	@pla
	@sta	force$map
	@pla
	@tay
	@pla
	@tax
	@pla
	@rti

;
;
	PAGE
;
; **** CONVERT BINARY TO ASCII ****
;
BINASC:
	@CLD			;-K
	@LDX	'0',#		;-K
	@SEC			;-K
BA0:
	@SBC	10,#		;-K
	@BCC	BA1		;-K
	@INX			;-K
	@BCS	BA0		;-K
BA1:
	@ADC	3Ah,#		;-K
	@RTS			;-K

	PAGE
;
; **** SELF CORRECTING CHECK IO ROUTINES ****
;
CKICM:
	@JSR	OPENCM+BB	;+K
CKINCM:
	@LDX	cmdchn,#	;+K
	@JSR	K$chkin		;+K
	@BCS	CKICM		;+K

	@JSR	K$chrin		;+K
	@CMP	'0',#		;+K
	@RTS			;+K
;
;
;
CKIDT:
	@JSR	OPENDT+BB	;+K
CKINDT:
	@LDX	datchn,#	;+K
	@JSR	K$chkin		;+K
	@BCS	CKIDT		;+K
	@RTS			;+K

;
;
;
CKOCM:
	@jsr	OPENCM+BB	;+K
CKOTCM:
	@LDX	cmdchn,#	;+K
	@JSR	K$chkout	;+K
	@BCS	CKOCM		;+K

	@RTS			;+K

	PAGE


dskcmd:		db	CR		;
dskcmd$S$l:	db	's'		;
dskcmd$S$h:	db	's '		;
dskcmd$T$l:	db	't'		;
dskcmd$T$h:	db	't 0 8:1'

U0POINT:	db	'U'	;
dskcmd$lng	equ	$-DSKCMD
		db	'0',4Ch,0	; reset disk change status (open)

POUND:		db	'#'

F$cmd$buf:
F$rd$count:	db	1		; 5 1st read always one sector
F$rd$sect:	db	0		; 4 filled in
F$rd$trk:	db	0		; 3 filled in
F$cmd:		db	0		; 2 read=0
		db	'0U'		; 1
Fcmd$lng	equ	$-F$cmd$buf

bios$size	equ	$-bios$65$code


