;ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ
;Û                                                                           Û
;Û                             MEMORY PROCEDURES                             Û
;Û                                                                           Û
;Û                    These procedures are not optimized.                    Û
;Û                                                                           Û
;Û       NOTE: '@' - this char can use only with PROC/PROCS and MACRO.       Û
;Û                                                                           Û
;ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß
;-----------------------------------------------------------------------------
; Check the number of memory.
; out: X - the number of memory's banks with basis bank. (each - 16KB).
;      MemoryBank - All bank switches. ($FF - the last, main bank)
; NOTE: Assembly this procedure at safe address $0000-$3FFF or $8000-$9FFF.
; For example:
;  64KB  -  X=1
; 128KB  -  X=5
; 320KB  -  X=17 (16*16+1*64)
;-----------------------------------------------------------------------------
TotalMemory PROC 

* author: Fox/Tqa, 23/08/99

_xmb0_ equ $0000
_xmbx_ equ $4000
_xmb2_ equ $8000
_xmb3_ equ $c000
MemoryBank Org *+256
_xmdt_     Org *+256

 ldx #0
 ldy #0
 sei
 sty $d40e
 sty $d400
 lda $d301
 pha
 lda _xmb3_
 pha
 lda _xmb2_
 pha
 lda _xmb0_
 pha
_xmd1_ sty $d301
 lda _xmbx_
 sta _xmdt_,y
 iny
 bne _xmd1_
_xmd2_ sty $d301
 sty _xmbx_
 iny
 bne _xmd2_
 lda #$ff
 sta _xmb0_
 sta _xmb2_
 sta _xmb3_
_xmd3_ sty $d301
 cpy _xmbx_
 bne _xmd4_
 tya
 sta MemoryBank,x
 inx
_xmd4_ iny
 bne _xmd3_
_xmd5_ sty $d301
 lda _xmdt_,y
 sta _xmbx_
 iny
 bne _xmd5_
 pla
 sta _xmb0_
 pla
 sta _xmb2_
 pla
 sta _xmb3_
 pla
 sta $d301
 lsr $d40e
 cli
 rts
ENDP
;-----------------------------------------------------------------------------
; Copy memory.
; in: ((_from_)) - from.
;       ((_to_)) - to.
;   ((_length_)) - total length in bytes.
;-----------------------------------------------------------------------------
CopyMemory PROC ((_from_)),((_to_)),((_length_))
 ldx _to_+1
 lda _length_
 bne _copmem1_
 ldy _length_+1
 BNE _copmem1_
 rts
_copmem1_:
 cpx _from_+1
 bmi _copmem5_
 bne _copmem2_
 ldx _to_
 cpx _from_
 bmi _copmem5_
 beq _copmem5_

_copmem2_:     ;end
 lda _from_+1
 add _length_+1
 sta _from_+1

 lda _to_+1
 add _length_+1
 sta _to_+1

 ldy _length_
 beq _copmem4_
_copmem3_:
 DEY
 lda (_from_),y
 sta (_to_),y
 cpy #0
 bne _copmem3_
_copmem4_:
 dec _from_+1
 dec _to_+1
 lda _length_+1
 bne _copmem3_
 rts

_copmem5_: ;front
 ldy #0
_copmem6_:
 lda (_from_),y
 sta (_to_),y
 iny
 cpy _length_
 bmi _copmem6_
 lda _length_+1
 bne _copmem7_
 rts

_copmem7_: 
 lda _from_
 add _length_
 sta _from_
 lda _from_+1
 adc #0
 sta _from_+1

 lda _to_
 add _length_
 STA _to_
 lda _to_+1
 adc #0
 sta _to_+1
_copmem8_:
 ldy #0
_copmem9_:
 lda (_from_),y
 sta (_to_),y
 iny
 bne _copmem9_
 inc _from_+1
 inc _to_+1
 dec _length_+1
 bne _copmem8_
 rts
_from_=32    ;(2)
_to_=34      ;(2)
_length_=36  ;(2)
ENDP

