;***************************************
; GetREUBank - allocate a REU bank
;
; Pass:
;	nothing
;
; Returns:
;	.A	REU bank allocated
;	.X	error - 0 = no error
;		non-zero - no bank
;
; Destroys:
;	.A, .X, .Y, a1L
;
;***************************************

GetREUBank:
	ldx	#0			; bank counter
	LoadB	a1L,%10000000		; mask bit
	ldy	#0
10$:	lda	ramBase,y
	and	a1L
	beq	50$			; found a hole
	inx				; nothing here - next bank
	txa
	cmp	ramExpSize		; only go as far as REU size
	bcs	99$			
	lsr	a1L			; shift to next bank
	bcc	10$
	ror	a1L			; move carry back into bit 7
	iny
	cpy	#4
	bcc	10$
	bcs	90$

50$:	lda	ramBase,y
	ora	a1L
	sta	ramBase,y		; mark the block as used
	txa				; now get the bank
	ldx	#0			; and clear the error status
	rts

90$:	ldx	#255			; report the error
	rts

;***************************************
; FreeREUBank - free up an REU bank
;
; Pass:
;	.A	REU bank to free
;
; Returns:
;	.X	error - 0 = no error
;		BAD_BAM = already free
;
; Destroys:
;	.A, .X, .Y, a1L
;
;***************************************

FreeREUBank:
	tax				; save for later
	lsr
	lsr
	lsr				; divide by 8 to get Y offset
	tay
	txa
	and	#7			; get offset within bank
	tax
	sec				; prime carry to roll into .A
	lda	#0			; and clear out .A
10$:	ror				; start shifting in the mask bit
	dex
	bpl	10$			; and loop until it's there
	sta	a1L			; now save it away
	and	ramBase,y
	beq	90$			; already free - give error
	lda	a1L
	eor	#255			; make mask to turn off bit
	and	ramBase,y		; and do it
	sta	ramBase,y		; then write it
	ldx	#0			; no errors - we're done
	rts
90$:	ldx	#BAD_BAM		; error - abort
	rts
