/*-------------------------------------
   SCSI Routines
   
   Ripped from HDOS 1.90
   cause CMD provides no Jumptable to it...
-------------------------------------*/

#include "StartUp.XA"


VIA2		= $8400
PIO		= $8800
LED		= $8f00

L90E5		= $90E5

ParityTab = $9300

	.BSS
	
	*=$2000
/*
ParityTab:
	.DSB $100
*/
L2A07:
	.DSB 1
L2A08:
	.DSB 1
L2A10:
	.DSB 1
L2A30:
	.DSB 1
L2A55:
	.DSB 1


SCSI_Cmd:
	.DSB $50


	.TEXT

	BA($900)

	ZEROORG($b)

SCSI_BufPtr		=$68 ;ZWORD
SCSI_Buf1Ptr		=$e0 ;ZWORD
SCSI_Buf2Ptr		=$e2 ;ZWORD
SCSI_Buf3Ptr		=$e4 ;ZWORD



Cmd_SCSIDevice		ZBYTE
Cmd_Len			ZBYTE
/*
Zp0			ZBYTE
*/

/*
	sei
;	jsr TabGen
	stz SCSI_BufPtr
	lda #$26
	sta SCSI_BufPtr+1
	lda #L200End-L200
	sta Cmd_Len
	stz Cmd_SCSIDevice
	jsr LAE28
	cli
	rts
*/

	sei
	jsr $ee9d
	lda #0
	sta $68
	lda #$27
	sta $69
	ldx #5
CopyLoop:
	lda Cmd,x
	sta $2a00,x
	dex
	bpl CopyLoop
	ldx #0
	jsr LF113
	bcs Err
	jsr LEF6C
	cli
	rts
Err:
	lda #$85
	sta $2a10
	cli
	rts

Cmd:
	.DB $12,$00,$00,$00,$ff,$00


/*-----------------------------------*/

/*
TabGen:
	ldy #0
TabGen0:
	sty Zp0
	lda #0
	ldx #7
TabGen1:
	lsr Zp0
	adc #0
	dex
	bpl TabGen1
	lsr
	lda #$10
	bcc TabGen2
	lda #$00
TabGen2:
	sta ParityTab,y
	iny
	bne TabGen0
	rts
*/

/*-----------------------------------*/

LAE28:
	JSR LAE39
	STZ L2A07
	STZ L2A08
	JMP LAE57


/*-----------------------------------*/


LAE57:
	LDX Cmd_SCSIDevice
	JSR LF113
	BCS LAE65
	LDA #$85			/* SCSI Device Not Present */
	STA L2A10
	RTS
LAE65:
	;JSR LEF6C

	jsr $ef00
	
	LDA L2A10
	RTS


/*-----------------------------------*/


LF113:
	LDA #$20
LF116:
	AND VIA2+$00
	BNE LF116
	SEC
LF120:
	ROL
	DEX
	BPL LF120
	LDX #$FF
	STX VIA2+$03
	ORA L90E5

/* Byte with parity out */
	EOR #$FF
	TAX
	LDA LED
	AND #$EF
	ORA ParityTab,X
	STA LED
	STX VIA2+$01

	STZ VIA2+$00
	LDA #$FB
	LDX #$10
	STA VIA2+$0C
	STX VIA2+$00

/* Wait for reply */
	LDX #$0
	LDY #$0
LF150:
	LDA VIA2+$00
	AND #$20
	BNE LF16C
	DEX
	BNE LF150
	DEY
	BNE LF150

	LDA #$DB
	STA VIA2+$0C
	LDX #$54
LF164:
	DEX
	BNE LF164
	LDA VIA2+$00
LF16C:
	LDX #$8
	STX VIA2+$00
	LDX #$DB
	STX VIA2+$0C
	asl
	asl
	asl
	bcs LF17F

;	AND #$20
;	bne LF17F

	jsr LF0DB
	sec
	rts
LF17F:
	clc
	RTS


/*-----------------------------------*/


LF0DB:
	LDA #$DB
	STA VIA2+$0C
	STZ VIA2+$03
	LDA #$8
	STA VIA2+$00

	LDA PIO+$02
	AND #$E3
	STA PIO+$02

	lda #$10
	trb LED			;Reset Parity
/*
	LDA LED			;Reset Parity
	AND #$EF
	STA LED
*/
	RTS


/*-----------------------------------*/


LEF6C:
	JSR LEF97
	CMP #$0
	BNE LEF76
	LDA L2A10
LEF76:
	STA L2A10
	CMP #$0
	RTS


/*-----------------------------------*/


LAE39:
	JSR LEF07
/*
	LDA Cmd_Buffer
	STA SCSI_BufPtr
	LDA Cmd_Buffer+1
	STA SCSI_BufPtr+1
*/

/* Cmd starts at $206, L56 is Cmdlen from $200 */
	LDX #$6
	LDY #$0
LAE4A:
	LDA L200,X
	STA SCSI_Cmd,Y
	INY
	INX
	CPX Cmd_Len
	BCC LAE4A
LAE56:
	RTS


/*-----------------------------------*/


LEF07:
	LDX #$4F
LEF0B:
	STZ SCSI_Cmd,X
	DEX
	BPL LEF0B
LEF11:
	/* LDX #$FF */
	STX L2A10
	STX L2A30

	STZ SCSI_Buf1Ptr
	LDA #$2A
	STA SCSI_Buf1Ptr+1
	STA SCSI_Buf2Ptr+1
	STA SCSI_Buf2Ptr+1

	LDA #$10
	STA SCSI_Buf2Ptr

	LDA #$30
	STA SCSI_Buf2Ptr

	STZ SCSI_BufPtr
	LDA #$50
	STA SCSI_BufPtr+1
	RTS


/*-----------------------------------*/


LEF8F:
	lda #$10
	trb LED

LEF97:
	LDA #$20
LEF99:
	BIT VIA2+$00
	BVS LEF99
	BMI LEFA8
	BNE LEF99
	JSR LF0DB
	LDA #$0
	RTS



LEFA8:
	BEQ LEFD5
	LDA VIA2+$00
	AND #$7
	BNE LEFB6
	JMP LEFDD		;0 -> Send SCSI_BufPtr
LEFB6:
	CMP #$4
	BNE LEFBD
	JMP LF014		;4 -> Get SCSI_BufPtr
LEFBD:
	CMP #$1
	BNE LEFC4
	JMP LF038		;1 -> Get Bytes to (SCSI_Buf2Ptr)
LEFC4:
	CMP #$5
	BNE LEFCB
	JMP LF079		;5 -> Get Bytes to (SCSI_Buf2Ptr)
LEFCB:
	CMP #$7
	BNE LEFD2
	JMP LF0AA		;7 -> Get Bytes to (SCSI_Buf2Ptr)
LEFD2:
	LDA #$87
	.DB $2c
LEFD5:
	lda #$86
	PHA
	JSR LF0FB
	PLA
	RTS


/*-----------------------------------*/

LF0FB:
	JSR LF0DB
	lda #8
	tsb PIO+$02
	LDX #$28
LF108:
	DEX
	BNE LF108
	trb PIO+$02
	LDA #$0
	RTS


/*-------------------------------------
  Send SCSI_BufPtr
-------------------------------------*/

LEFDD:
	ldy #$ff
	sty VIA2+$03
	iny
	LDA #$A0
	STA VIA2+$00
LEFED:
	LDA VIA2+$00
	CMP #$A0
	BNE LF00F
	LDA (SCSI_BufPtr),Y
	EOR #$FF
	TAX
	LDA LED
	AND #$EF
	ORA ParityTab,X
	STA LED
	STX VIA2+$01
	INY
	BNE LEFED
	INC SCSI_BufPtr+1
	bra LEFED


LF00F:
	STY SCSI_BufPtr
	JMP LEF8F


/*-------------------------------------
  Get SCSI_BufPtr
-------------------------------------*/

LF014:
	STZ VIA2+$03
	LDA #$AC
	STA VIA2+$00
	ldy #0
LF022:
	LDA VIA2+$00
	CMP #$AC
	BNE LF00F
	LDA VIA2+$01
	EOR #$FF
	STA (SCSI_BufPtr),Y
	INY
	BNE LF022
	INC SCSI_BufPtr+1
	bra LF022


/*-------------------------------------
  Get Bytes to (SCSI_Buf2Ptr)
-------------------------------------*/

LF038:
	ORA #$20
	LDY #$FF
	STY VIA2+$03
	INY
	STA VIA2+$00
	STA L2A55

LF046:
	BIT VIA2+$00
	BVS LF046
	LDA L2A55
LF04E:
	CMP VIA2+$00
	BEQ LF04E
	ORA #$80
	CMP VIA2+$00
	BNE LF076

	LDA (SCSI_Buf1Ptr),Y

	/* Send with Parity */
	EOR #$FF
	TAX
	LDA LED
	AND #$EF
	ORA ParityTab,X
	STA LED
	STX VIA2+$01

	INC SCSI_Buf1Ptr
	BNE LF046
	INC SCSI_Buf1Ptr+1
	bra LF046

LF076:
	JMP LEF8F


/*-------------------------------------
  Get Bytes to (SCSI_Buf2Ptr)

  exactly the same as LF0AA
-------------------------------------*/

LF079:
	ORA #$28
	LDY #$0
	STY VIA2+$03
	STA VIA2+$00
	STA L2A55
LF086:
	BIT VIA2+$00
	BVS LF086
	LDA L2A55
LF08E:
	CMP VIA2+$00
	BEQ LF08E
	ORA #$80
	CMP VIA2+$00
	BNE LF076			; Jmp to LEF8F
	LDA VIA2+$01
	EOR #$FF
	STA (SCSI_Buf2Ptr),Y
	INC SCSI_Buf2Ptr
	BNE LF086
	INC SCSI_Buf2Ptr+1
	bra LF086


/*-------------------------------------
  Get Bytes to (SCSI_Buf2Ptr)

  exactly the same as LF079
-------------------------------------*/


LF0AA:
	ORA #$28
	LDY #$0
	STY VIA2+$03
	STA VIA2+$00
	STA L2A55
LF0B7:
	BIT VIA2+$00
	BVS LF0B7
	LDA L2A55
LF0BF:
	CMP VIA2+$00
	BEQ LF0BF
	ORA #$80
	CMP VIA2+$00
	BNE LF076			; Jmp to LEF8F
	LDA VIA2+$01
	EOR #$FF
	STA (SCSI_Buf2Ptr),Y
	INC SCSI_Buf2Ptr
	BNE LF0B7
	INC SCSI_Buf2Ptr+1
	JMP LF0B7


/*-----------------------------------*/

L200:
	.PSC "s-c"
	.DB 0, 0, 5
	.DB $12, $00, $00, $00, $ff, $00
L200End:



