How much memory does SP1 need?

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
sdolotom
New member
Posts: 7
Joined: Mon Mar 08, 2021 1:15 pm

How much memory does SP1 need?

Post by sdolotom »

Hi folks,

I'm trying to put few 32x16 sprites with mask on screen, and at some point sp1_CreateSpr starts to return NULL. I tried to experiment with heap size, and it seems that SP1 consumes around 400 bytes of heap per sprite. The sprite itself is 128 bytes. How much more SP1 needs on top of that? Is there a way to optimize memory use?
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: How much memory does SP1 need?

Post by Timmy »

Fortunately WoS is still working: https://worldofspectrum.org/forums/discussion/36241/

I'm copy and pasting in case the information gets lost.
Generally, how much memory should I allocate with sbrk()? (How much will a sprite generally use?)
See the header file for struct sizes.

Each sprite needs one "struct sp1_ss" (20 bytes) and one "struct sp1_cs" (24 bytes) for each character square in the sprite. In addition, malloc has a two byte overhead for each allocated block.

So a 3 char x 3 char sprite requires:

1 struct sp1_ss: 1 * (20+2) = 22 bytes
9 struct sp1_cs: 9 * (24+2) = 234 bytes

256 bytes total. This can become expensive depending on what you are doing.

If memory (and maybe speed) is going to be a problem, an alternative to sprites where smooth movement is not needed is to print as tiles at character coordinates. Since sp1 doesn't draw until the screen is complete, there is no flicker issue as would be the case in basic. This was done in Sgt Helmet Zero.
As for your other question:
Is there a way to optimize memory use?
There is a link on that page that links to https://worldofspectrum.org/forums/discussion/36180/

(I should copy and paste the whole thread to this forum, probably. I'm a bit too busy right now, though.)
sdolotom
New member
Posts: 7
Joined: Mon Mar 08, 2021 1:15 pm

Re: How much memory does SP1 need?

Post by sdolotom »

Thank you! Does "per square" include squares reserved for rotations? For example, if I create a sprite 4x2 like this:

Code: Select all

    struct sp1_ss * sp = sp1_CreateSpr(SP1_DRAW_MASK2LB, SP1_TYPE_2BYTE, 3, 0, 0);
    sp1_AddColSpr(sp, SP1_DRAW_MASK2, SP1_TYPE_2BYTE, offset1, 0);
    sp1_AddColSpr(sp, SP1_DRAW_MASK2, SP1_TYPE_2BYTE, offset2, 0);
    sp1_AddColSpr(sp, SP1_DRAW_MASK2, SP1_TYPE_2BYTE, offset3, 0);
    sp1_AddColSpr(sp, SP1_DRAW_MASK2RB, SP1_TYPE_2BYTE, 0, 0);
I put a breakpoint to malloc, and can see that it's called once with 20 (for sp1_ss) and 15 times with 24 (for sp1_cs). Is that correct?
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: How much memory does SP1 need?

Post by Timmy »

You are Correct. :cool:

I found another comment (from 2009) at https://worldofspectrum.org/forums/disc ... ent_405903:
A program fragment like this:

Code: Select all

   86       s = sprtbl[i].s = sp1_CreateSpr(SP1_DRAW_MASK2LB, SP1_TYPE_2BYTE, 3, 0, i);
   87       sp1_AddColSpr(s, SP1_DRAW_MASK2, 0, 48, i);
   88       sp1_AddColSpr(s, SP1_DRAW_MASK2RB, 0, 0, i);
will create a 3 rows tall by 3 column wide sprite for a total of 9 sprite characters. So that's one "struct sp1_ss" for the sprite (a pointer to which is stored in variable 's' above) and 9 "struct sp1_cs"s which are linked to the "struct sp1_ss" by the CreateSpr and AddColSpr code. This one sprite requires: (20+2) + (24+2)*9 = 256 bytes. The +2 is the overhead required by malloc for each allocated block of memory (that's how it knows how big the block is when free() is called to free the block).
(Note that I would not recommend some other things said in that comment. But the above part is correct.)

Therefore, if a 2x2 sprites needs 9 sprite characters, a 4x2 sprite will have 5x3 sprite characters.

===

Before I forget, if you are using the latest version of z88dk, there are also additional ways to reduce memory usage, see the FBS thread for more details.
Post Reply