.include "declare.s"
.org 0
free:
pha.q
phb.q
phx.q
phy.q
phe.q
and
tab
tax
tay
mov a, d
bne @getrealblk
bra @end
@getrealblk:
sub
mov.q a, (a)
mov.q b, (a+ublk.size)
lea e, (a+b)
cmp.q e, heapptr
bne @heapadd
@dectop:
mov.q heapptr, (a)
@chklastblk:
mov.q b, heapl
beq @end
ldy
mov.q a, (b+fblk.size)
lea e, (a+b)
cmp.q e, heapptr
bne @end
@delblk:
mov.q heapptr, b
@correctblk:
mov.q a, (b+fblk.prev)
sta.q heapl
bne @delnxtblk
sta.q heapf
bra @end
@delnxtblk:
lea e, (a+fblk.next)
stz.q (e)
@end:
ple.q
ply.q
plx.q
plb.q
pla.q
rts
@heapadd:
mov.q y, heapf
bne @srchflst
@empty:
mov.q (a+fblk.next), y
mov.q (a+fblk.prev), y
sta.q heapf
sta.q heapl
bra @end
@srchflst:
@loop:
cmp y, a
beq @nextright
bcs @chkrmerge
@nextright:
tyx
mov.q y, (y+fblk.next)
bne @loop
@st_lmerge2:
mov.q (a+fblk.next), y
sta.q heapl
bra @chklmerge2
@chkrmerge:
lea e, (a+b)
cmp e, y
bne @normerge
@rmerge:
mov e, b
add.q e, (y+fblk.size)
mov.q (a+fblk.size), e
@rmerge2:
lea fblk.next
mov.q (a+e), (y+e)
mov.q b, (y+e)
beq @setheapl
@setprev:
mov.q (b+fblk.prev), a
bra @chklmerge
@setheapl:
sta.q heapl
bra @chklmerge
@normerge:
mov.q (a+fblk.next), y
mov.q (y+fblk.prev), a
@chklmerge:
and x, x
bne @chklmerge2
@newstart:
mov.q (a+fblk.prev), x
sta.q heapf
bra @end2
@chklmerge2:
mov.q e, (x+fblk.size)
add e, x
cmp e, a
bne @nolmerge
@lmerge:
lea fblk.size
add.q (x+e), (a+e)
@lmerge2:
lea fblk.next
mov.q (x+e), (a+e)
mov.q b, (a+e)
beq @newlast
@lprev:
mov.q (b+fblk.prev), x
bra @end2
@newlast:
stx.q heapl
bra @end2
@nolmerge:
mov.q (x+fblk.next), a
@nolmerge2:
mov.q (a+fblk.prev), x
@end2:
ple.q
ply.q
plb.q
pla.q
rts
a
q