;flash rom selector for alternative tape load
; with a warrior's cartridge mkII

;N60-BASIC ROM subroutine
GETC	equ	0fc4h
XY2AD	equ	11cdh
SETCSR	equ	116dh
CSRON	equ	1179h
CSROFF	equ	1181h
CLS	equ	1dfbh
PUTS	equ	30cfh

;selector program address
SCOPY	equ	0fbfch

;loader work area
BANK	equ	0fbf9h
ADDR	equ	0fbfah


	org	4000h
	db	"AB"
	dw	4004h

;initialize i/o port
	ld	a,40h
	out	(30h),a
	inc	a
	out	(31h),a
	ld	a,56h
	out	(0f2h),a

	in	a,(0f6h)
	ld	b,a
	ld	a,055h
	out	(0f6h),a
	in	a,(0f6h)
	cp	55h
	jr	z,MK2
	ld	a,66h
	out	(06h),a
MK2:
	ld	a,b
	out	(0f6h),a

;copy program
	ld	hl,0000h
	ld	d,h
	ld	e,l
	ld	bc,4000h
	ldir
	ld	hl,LOAD
	ld	de,1a70h
	ld	bc,LEND-LOAD
	ldir
	ld	a,0c9h
	ld	(1a61h),a
	ld	a,077h
	out	(0f0h),a

	ld	hl,SELECT
	ld	de,SCOPY
	ld	bc,SEND-SELECT
	ldir
	jp	SCOPY


;bank selector
SELECT:
	call	CLS
	ld	hl,TABLE
	call	PUTS
	ld	hl,6000h
	ld	(ADDR),hl
	xor	a

LOOP1:
	ld	(BANK),a
	out	(33h),a

	ld	hl,1001h
	call	XY2AD
	ex	de,hl
	ld	hl,600ah
	ld	bc,6
	ldir

	ld	l,a
	and	07h
	add	a,7
	ld	h,a
	ld	a,l
	rrca
	rrca
	rrca
	and	07h
	add	a,2
	ld	l,a
	call	SETCSR
	call	CSRON

LOOP2:
	call	GETC
	sub	' '
	jp	z,CLS
	ld	b,a
	ld	a,(BANK)
	inc	b
	jr	z,DOWN
	inc	b
	jr	z,UP
	inc	b
	jr	z,LEFT
	inc	b
	jr	nz,LOOP2

RIGHT:
	inc	a
	jr	CHGBNK
LEFT:
	dec	a
	jr	CHGBNK
UP:
	sub	8
	jr	CHGBNK
DOWN:
	add	a,8

CHGBNK:
	and	3fh
	call	CSROFF
	jr	LOOP1
SEND:


;alternative load routine (1a70h)
LOAD:
	push	hl
	ld	hl,(ADDR)
	bit	7,h
	jr	z,LOADZ
	ld	h,60h
	ld	a,(BANK)
	inc	a
	ld	(BANK),a
	out	(33h),a
	xor	a		;set z-flag
LOADZ:
	ld	a,(hl)
	inc	hl
	ld	(ADDR),hl
	pop	hl
	ret
LEND:


TABLE:
	db	"BLOCK 01234567",0dh,0ah
	db	"SECT0 ########",0dh,0ah
	db	"SECT1 ########",0dh,0ah
	db	"SECT2 ########",0dh,0ah
	db	"SECT3 ########",0dh,0ah
	db	"SECT4 ########",0dh,0ah
	db	"SECT5 ########",0dh,0ah
	db	"SECT6 ########",0dh,0ah
	db	"SECT7 ########",00h

	end
