;flash rom selector for alternative tape load
; with a warrior's cartridge mkII
; PC-6001mkII/6601/SR

;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	"CD"
	dw	4004h

;initialize i/o port
	ld	a,40h
	out	(30h),a		;0000-1fff:external RAM bank #0
	inc	a
	out	(31h),a		;2000-3fff:external RAM bank #1

;	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	a,56h
	out	(0f2h),a	;0000-3fff:external (write)

;BASIC ROM
	ld	hl,0000h
	ld	d,h
	ld	e,l
	ld	bc,4000h
	ldir

;tape load routine
	ld	hl,LOAD
	ld	de,1a70h
	ld	bc,LEND-LOAD
	ldir

;change 6000-7fff bank routine
	ld	hl,CHG6000
	ld	de,2698h
	ld	bc,CHG6END-CHG6000
	ldir

;tape open routine
	ld	a,0c9h
	ld	(1a61h),a

;outi data
	ld	a,77h		;0000-7fff:external (read)
	ld	(2c9ch),a


;	ld	a,77h		;0000-7fff:external (read)
	out	(0f0h),a

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

	ld	a,55h		;0000-3fff:internal (write)
	out	(0f2h),a

	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
LOADZ:
	ld	a,77h		;0000-7fff:external (read)
	out	(0f0h),a
	ld	a,(hl)
	inc	hl
	ld	(ADDR),hl
	ld	h,a		;;
	ld	a,17h		;0000-3fff:external 4000-7fff:BASIC ROM (read)
	out	(0f0h),a
	ld	a,h		;;
	pop	hl
	cp	a		;set z-flag
	ret
LEND:


;change bank for checking 6000h (2698h)
CHG6000:
	or	06h		;x1h->x7h (0000-3fff:BASIC ROM -> external)
	di
	out	(0f0h),a
	ld	(0fe64h),a
	ei
	ret
CHG6END:


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
