Login
Back to forumReply to this topicGo to last reply

Posted By

indi
on 2006-01-24
14:58:03
 Fast allocator....

I thought I'd share this.... I've no idea if anyone else does this already, but since I just stumbled over it....I thought I'd "share and enjoy".

In the old days (when I was doing C64 stuff), my allocation routines tended to be bog standard, and very simple, like this....(in fact...I still tend to do this now...)


Alloc: ldx #MAX_OBJECTS
CheckAll:
lda InUse,x
beq GotOne
dex
bpl CheckAll
rts
GotOne:
lda #$ff
sta BullInUse,x
rts


Now this is all very well and good, but the more "slots" you have the worse it'll get. The easy answer is a simple linked list or something... but this requires you to link and unlink, and setup is't as simple either, not to mention the overhead of the links themselves.

So, while I was struggling to find a solution, I stated thinking of some sort of "stack" method... and stumbled into this method- timings were based on 16 slots. (hope the code comes out readable)



;****************************************************************************************
;
; Allocate/Free a bullet.
; Out: X=spare slot or -1 for error
;
; Alloc - BEST/WORST - 12/25
; Free - Best/Worst - 24 (constant)
;
;****************************************************************************************
FreeList db 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,$ff

;
; Usage:- AllocBullet [x|y] Specify the X or Y register that hold the bullet to free
;
AllocBullet macro
ld FreeIndex ; 3 ldx FreeIndex
lda FreeList, ; 4 lda FreeList
bmi !NoneFree ; 2 bmi !NoneFree
in ; 2 inx
st FreeIndex ; 3 stx FreeIndex
ta ; 2 tax
dec BullInUse, ; 7 dec BullInUse,x
!NoneFree: ; tax
ta ; 2 get next free bullet (or $ff for none left)
endm

;
; Usage:- FreeBullet [x|y] Specify the X or Y register that hold the bullet to free
;
FreeBullet macro
ta ; 2 txa
ld FreeIndex ; 3 ldx FreeIndex
de ; 2 dex
st FreeIndex ; 3 stx FreeIndex
sta FreeList, ; 5 sta FreeList,x
ta ; 2 tax - restore index
lda #0 ; 2 lda #0
sta BullInUse, ; 5 sta BullInUse,x
endm



Now, the best thing about it is that it doesn't matter how many you have, its aconstant speed, and all you need to do is setup an array with the freelist in it. You can type it in here as I've done, or calculate it at runtime... In effect its a simple stack, but it never occured to me to use a stack as an allocator before, its very quick and thanks to macros, you can use X or Y to allocate depending on your needs....

I used this for bullet allocation, and when firing many at once, I took a huge hit... now allocation is virtually invisible... which is nice happy

Posted By

Luca
on 2006-01-24
16:13:59
 Re: Fast allocator....

Copied to the XeO3 WIP Weblog too.

Posted By

indi
on 2006-01-24
16:50:19
 Re: Fast allocator....

I'd just like to point out, 10 days of blog and 650 hits! 65 a day! Way to go Luca!!

Just shows how much interest these kinds of things still have!

Posted By

Luca
on 2006-01-24
17:56:43
 Re: Fast allocator....

You're right, Mike. And keep in count that the blog had been off for 36 hours! happy
An example: some japanese MSX retrocoders emailed me about XeO3, encouraging the project.
Moreover, as written in a recent post, I wrote that James McKay did the same. James is working on a similar project, I like it very much: Klass of '99 for Spectrum 128K. But that would be one example only, when just yesterday some guys posted on the Commodore Scene Database (CSDb) some requests about musicians/coders for a Battlestar Galactica's C64 game.
At this point, the direct consequence I see is a solid 8bit retrocoding webring, in order to link all those projects.

Posted By

indi
on 2006-01-24
18:14:56
 Re: Fast allocator....

BTW.... I just added joystick support and I like to say Plus4 joysticks SUCK!

I'll be playing on keys I think.....

Posted By

Luca
on 2006-01-24
19:49:49
 Re: Fast allocator....

LOL! :D
Why? Omg are you using the original Commodore joystick for C16 and +4? That wasn't a joystick really, but a tree trunk :D Use a c64 one with an adaptor, instead, like the wonderful Koenig Speeding.

Posted By

indi
on 2006-01-25
03:05:03
 Re: Fast allocator....

Well, I dont have an adaptor.... so Im stuck with the Plus4 sticks..........which suck.

Have I mentioned that already?

Posted By

Crown
on 2006-01-25
05:03:06
 Re: Fast allocator....

What about a circular buffer, to contain the free slots? It's pretty much the same code, you just have two update separate indexes at allocation and deallocation.

Posted By

TMR
on 2006-01-25
07:10:21
 Re: Fast allocator....

Oooh... 8-bit coding webring, perhaps i should start it as a facet of Oldschool Gaming...?

Posted By

Luca
on 2006-01-25
08:51:39
 Re: Fast allocator....

Jason, OSG should be the best harbour for a 8bit games retroring's first sailing .

Posted By

indi
on 2006-01-25
12:39:33
 Re: Fast allocator....

circular queues are okay, but like linked-lists theres an overhead. Checking for the end of the queue and checking for head/tail cross overs etc... This doesn't have any of these problems. You load the value, inx and your done...just about.



Back to topReply to this topic


Copyright © Plus/4 World Team, 2001-2024. Support Plus/4 World on Patreon