;Moves 1k bytes from any bank to bank 1 for use with SID
;
;	BANKLOOK	Rev 5.18.86
;
; (C)	Gordon Wilk		6707 Springpark Av
;1986	Los Angeles CA 90056	(213) 645-6419
; May be used for non-commercial purpoese without fee
;
;=======================================================
;	EQUATES
;=======================================================

BOOT	EQU	00
BDOS	EQU	05	
CR	EQU	0Dh	;Carriage return
LF	EQU	0Ah	;Line feed
DBuff	EQU	080h	;Adr of default dma 
;	BIOS Addresses
XMOVE	EQU	0FB8Ah	;Set source/dest banks
MOVE	EQU	0FB3Bh	;cross bank move

;========================================================
;	MACROS
;========================================================
;
Print	MACRO	ADR		;;print string
	LXI	D,ADR		;; @adr
	CALL	PSTR
	ENDM
;  
Reply	MACRO	BUFF,LEN 	;;Get console input
	LXI	D,BUFF		;; to @BUFF
	MVI	A,LEN	
	STAX	D
 	MVI	C,0AH		
	CALL 	BDOS		
	ENDM

;========================================================
;	MAIN PROCEDURE
;=========================================================
;
	ORG	100H
	LXI	SP,STAK
	PRINT	Header
MainLoop:
			;GET SOURCE BANK. STACK SRC/DEST BANKS 
	PRINT	SBankQ		;Source bank?
	CALL	GETC
	SBI	'0'		;to binary
	LXI	H,BnkTbl	;translate to bank byte
	MOV	L,A
	MOV	C,M		;C=Source Bank	for XMOVE
	MVI	B,1		;B=Dest Bank 1	for XMOVE
	PUSH	B		; Save
			;GET AND SAVE SOURCE ADDRESS
	PRINT	SAdrQ		;Source address?
	REPLY	DBuff,4
	CALL	ASCBIN		;returns binary in HL
	SHLD	SAdr		;Save Src Adr
			;GET SOURCE LENGTH AND SAVE
	PRINT	SLenQ		;Length(bytes)?
	REPLY	DBuff,4
	CALL	ASCBIN
	SHLD	SLen		;Save length to copy
			;GET AND SAVE DESTINATION ADDRESS
	PRINT	DAdrQ		;Dest. Address?
	REPLY	DBuff,4
	CALL	ASCBIN	
	SHLD	DAdr		; save Dest Adr
			;SET SRC/DEST BANKS
	POP	B		;XMOVE must be called
	CALL	XMOVE		; just before MOVE
			;SETUP FOR MOVE CALL
	LHLD	SLen
	MOV	B,H
	MOV	C,L		;BC=length 	for MOVE
	LHLD	SAdr
	XCHG			;DE->src	for MOVE
	LHLD	DAdr		;HL->dest	for MOVE
	CALL	MOVE		;move it
			;GOTO SID FOR VIEWING 
	RST	7		
	JMP	MainLoop

;========================================================
;	SUBROUTINES
;========================================================

;SUBROUTINE GETC
;Gets character from console and uppercases it
;Exits to CPM if char is ^C
GETC	MVI	C,01	 	;get char into A
	CALL	BDOS	
	CPI	03		;^C to exit
	JZ	BOOT
	RET

;Subroutines PSTR -- Print String @(DE)
PSTR:
	MVI	C,09		;display string pointed 
	jmp	BDOS		;to by DE

;Subroutine ASCBIN -- Convert ASCII string to binary number
;INPUT	ASCII decimal string in DBuff+2. Char count in DBuff+1. 
;OUTPUT HL=binary value

AscBin:
	MVI	B,0
	LDA	DBuff+1
	ADI	82h
	MOV	L,A		;Terminate string with null
	MOV	H,B
	MOV	M,B
	LXI	D,DBuff+2	;DE->string
ASCB2:
	LDAX	D		;A=digit
	ORA	A		;IF null
	RZ			;	done
	CALL	A2B		;convert to binary
	MOV	C,A		;extend to 16bits in BC
	DAD H ! DAD H 
	DAD H ! DAD H		;HL=16*HL
	DAD	B		;HL=HL+digit
	INX	D		;next digit
	jmp	ASCB2		;LOOP
A2B:
	SUI	'0'		;Convert
	CPI	10		;IF < 11
	RC			;	done
	SUI	7		;ELSE	adjust
	RET

;=================================================================
;	DATA
;==================================================================

Header  DB	CR,LF,'Enter addresses and length in hex',CR,LF,'$'
SBankQ	DB	CR,LF,'Source Bank   : $'
SAdrQ	DB	CR,LF,'Source Address: $'
SLenQ	DB	CR,LF,'Length (bytes): $'
DAdrQ	DB	CR,LF,'Dest Address  : $'
SAdr	DW	00		;Source address
SLen	DW	00		;Length to copy
DAdr	DW	00		;Destination address

			;Create alignment on page boundary
MEMRY	EQU	( $+16+256 ) AND 0FF00h
			;Stack is between code and page end 
			;Guarantee at least 12 bytes
STAK	EQU	MEMRY-4			

	ORG	MEMRY		;Align on page boundary
BnkTbl 	DB	0000$0000B	;Bank 0
	DB	0000$0001B	;Bank 1
	DB	0000$0010B
	DB	0000$0100B
	DB	0000$1000B
	DB	0001$0000B
	DB	0010$0000B
	DB	0100$0000B	;Bank 7
	DB	1000$0000B	;Bank 8
	
	END	100h

	DB	0000$0100B
	DB	000