8-way scrolling from SD uncompressed demo

Use this forum to share and discuss Uzebox games and demos.
Post Reply
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

8-way scrolling from SD uncompressed demo

Post by Artcfox »

I had planned on keeping this under wraps before this year's Uzebox Code Contest, but I realized that I need some real-world testing with real SD cards (not emulation) before I can rely on this technique in a finished game.

I started off with the horizontal and vertical scrolling code from my Uzebox Mode 3 with Scrolling Guide, which stored the level uncompressed in PROGMEM, but I quickly realized that I wouldn't be able to store large, or many levels that way.

Level compression always seems to complicate things, and it makes the level format very rigid and tough to quickly add features, and it makes it that much harder for people to understand what's going on without really studying the code.

One of my goals was to be able to get 8-way scrolling working completely from the SD card, without having to use any kind of level compression, so that I could use the extra bits in each tile for things like enemy spawns, or triggers, or any other game extension one might think up. The attached demo is far from optimized, but if it works as it is, that means that I'll be able to squeeze a lot more out of it after optimizations. It's actually reading an extra 384 bytes from the SD card each time you scroll past a tile boundary, so I actually have a crap-ton of room leftover to store additional stuff for the level, or if I don't need to store anything in those extra bytes, I can reduce the size of the data file by a factor of 4.

What I really want to know is when you guys run this on real hardware, does any part of the 4x3 mega sprite flicker as you run and jump around?

The code determines if the camera has scrolled enough to need additional level data loaded into a hidden row and/or column, and then it cue's up the sector containing that level data, by issuing a CMD17, but it doesn't actually look for the data ready token until sometime during the next frame, allowing:

Code: Select all

    Player_render(&p);
    ProcessSprites();
    WaitVsync(1);
    RestoreBackground();
    ReadControllers();
to run while the SD card is busy getting ready to read that sector. The idea is that we can be doing useful work instead of spinning waiting for the data ready token, and then once many tens of thousands of clocks have passed (all those functions listed above), the SD card should then immediately respond with the data ready token and we can clock out the actual data stored in that sector with no delay. Since we are only writing to hidden rows and/or columns, the fact that we don't actually write that tile data out until the beginning of the next frame doesn't make any difference. Those tiles will still get written before they are seen, but we are spreading the work out across multiple frames, and not sitting around waiting for the SD card, which (at least for me with my SD card) allows everything to run at 60fps.

One caveat of this technique is that I needed to make sure that no matter how big the level is, or which of the 8 directions the camera gets scrolled in, I only ever need to read a single sector's worth of data.

That's the theory behind it anyway. Whether it works in practice on more than just my SD card is what I need people to help me test out.

Controls are as follows:

Code: Select all

LEFT/RIGHT = Move
DOWN       = Drop through the thin one-way tiles
A          = Jump
B          = Run
LS         = Attack
Make sure you hold B to run!

(If you do run this under cuzebox, you'll need to have a cuzebox built with the latest changes as of April 9, 2018; it will run fine with uzem.)
Attachments
8-way-scrolling-from-sd-uncompressed-demo.zip
the level file needs to be contiguous on the SD card
(25.03 KiB) Downloaded 590 times
User avatar
Jubatian
Posts: 1564
Joined: Thu Oct 01, 2015 9:44 pm
Location: Hungary
Contact:

Re: 8-way scrolling from SD uncompressed demo

Post by Jubatian »

I gave it a try using four different SD cards (one SDSC, three SDHC). It did work with all these cards, however with one of the SDHC cards, the sprite flickers.

What did you settle for about CRC by the way? I guess you turn it off. I think you could possibly leave it on (I mean don't turn it off with CMD59), the command is only a few bytes so the CRC calculation's overhead isn't a big deal, and it makes things safer (especially if you will also want to write the card). You can still discard the data CRC then.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: 8-way scrolling from SD uncompressed demo

Post by Artcfox »

Jubatian wrote: Wed Apr 11, 2018 10:30 am I gave it a try using four different SD cards (one SDSC, three SDHC). It did work with all these cards, however with one of the SDHC cards, the sprite flickers.

What did you settle for about CRC by the way? I guess you turn it off. I think you could possibly leave it on (I mean don't turn it off with CMD59), the command is only a few bytes so the CRC calculation's overhead isn't a big deal, and it makes things safer (especially if you will also want to write the card). You can still discard the data CRC then.
Awesome! Thanks for testing it. Do you know whuch brand SD card it flickered with? I guess I'm okay with it requiring a fast card, since SDHC cards are so cheap and plentiful.

I turned off the CRC, even for the command. I planned on re-enabling it before doing any writing to the card. It saved me several hundred clocks which made a difference when I was doing everything in a single frame. Now that I'm splitting the work across multiple frames it might be possible to add that back, and keep CRC enabled, but skip the verification on the reads that I need to keep fast.

I can probably split the second half of the function in half again, since the second half of the read will just be discarded anyway, and move the clocking out of the part I don't care about off into a third frame, and then it should work, even on your slowest card (assuming that the avatar isn't moving fast enough to trigger the first part of the cueing of the next sector in the same frame it is finishing up the read, though that actually might be fine if it does).
User avatar
D3thAdd3r
Posts: 3222
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: 8-way scrolling from SD uncompressed demo

Post by D3thAdd3r »

Very cool that you have this working! I am curious as it doesn't seem to run on my (probably horribly outdated) CUzeBox build, any thoughts?
User avatar
ry755
Posts: 226
Joined: Mon May 22, 2017 6:01 am

Re: 8-way scrolling from SD uncompressed demo

Post by ry755 »

I tried it with the cheap SD card that comes with the kit and it worked fine. I didn't notice any flickering and the scrolling was nice and smooth.
User avatar
Jubatian
Posts: 1564
Joined: Thu Oct 01, 2015 9:44 pm
Location: Hungary
Contact:

Re: 8-way scrolling from SD uncompressed demo

Post by Jubatian »

Artcfox wrote: Wed Apr 11, 2018 12:08 pmDo you know whuch brand SD card it flickered with? I guess I'm okay with it requiring a fast card, since SDHC cards are so cheap and plentiful.
It is a PQI 4Gb Class 4 SDHC card.
D3thAdd3r wrote: Thu Apr 12, 2018 3:03 amVery cool that you have this working! I am curious as it doesn't seem to run on my (probably horribly outdated) CUzeBox build, any thoughts?
There was a bugfix related to the SD CRC on/off feature a few days ago, Artcfox mentions it at the bottom of the opening post, I also updated the binaries accessible from the CUzeBox topic.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: 8-way scrolling from SD uncompressed demo

Post by Artcfox »

D3thAdd3r wrote: Thu Apr 12, 2018 3:03 am Very cool that you have this working! I am curious as it doesn't seem to run on my (probably horribly outdated) CUzeBox build, any thoughts?
Thanks!

Just pull the latest changes to cuzebox, and it'll work.
ry755 wrote: Thu Apr 12, 2018 3:04 am I tried it with the cheap SD card that comes with the kit and it worked fine. I didn't notice any flickering and the scrolling was nice and smooth.
Awesome, thanks for testing it out!
Jubatian wrote: Thu Apr 12, 2018 5:18 am
Artcfox wrote: Wed Apr 11, 2018 12:08 pmDo you know whuch brand SD card it flickered with? I guess I'm okay with it requiring a fast card, since SDHC cards are so cheap and plentiful.
It is a PQI 4Gb Class 4 SDHC card.
Cool, somewhere I think there is a list of known good and known bad card brands, we might want to put that particular one on the "May be too slow to support all games" list.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: 8-way scrolling from SD uncompressed demo

Post by Artcfox »

Before it gets lost, I figured I'd attach the source code for this demo.

This was a proof-of-concept, so it's not the prettiest, but it does show all of the debugging stuff that I used to get it working properly. The whisper console was used to dump sectors from the SD card, but the debug stuff should be commented out.
Attachments
sdmap-split-read.zip
(1.05 MiB) Downloaded 409 times
Post Reply