Page 1 of 1

My scrolling routine is bad

Posted: Tue Aug 03, 2010 7:40 pm
by Jaklub
I've got another problem I'm trying to solve without good results.
I want to create an illusion of 2 layers of background using only 1 plane. Horizontal scrolling per single line is used.
Here's my C code:

Code: Select all

volatile u16 *pw;
volatile u32 *pl;
pw = (u16 *) GFX_DATA_PORT;
pl = (u32 *) GFX_CTRL_PORT;
s16 i;
bgval[0]++;
bgval[1]+=2;
if(bgval[0]>=512) bgval[0]-=512;
if(bgval[1]>=512) bgval[1]-=512;
// when I cut the code here, no slowdown occurs
for(i=0; i<224; i+=2) {
*pl = ((0x4000 + ((HSCRL + ((i & 0xFF) * 4)+2) & 0x3FFF)) << 16) + (((HSCRL + ((i & 0xFF) * 4)+2) >> 14) | 0x00); *pw = bgval[0];
}
for(i=1; i<224; i+=2) {
*pl = ((0x4000 + ((HSCRL + ((i & 0xFF) * 4)+2) & 0x3FFF)) << 16) + (((HSCRL + ((i & 0xFF) * 4)+2) >> 14) | 0x00); *pw = bgval[1];
}
As you can see, I want to change 224 values of scrolling at once. That's a lot, and in later progress I'll probably need whole 512. Using many subroutines is too slow, so I try to minimalize the amount of them. Basically, this is modified code of VDP_setHorizontalScroll script from Stef's devkit. While this works fine for a menu, where practically no other code is executed, it slows the game down.
The question is: what is the fastest way to change a scroll value in C / at all? I've tried to get the needed info from Charles MacDonald's documentation, but failed miserably, I'm sure I have not enough technical knowledge yet. =P Thanks in advance.

Posted: Tue Aug 03, 2010 7:44 pm
by Shiru
Why you calculate all the math for *pl every time? You can precalculate it once, and also do everything in single loop, which is faster.

Posted: Tue Aug 03, 2010 9:40 pm
by Jaklub
After your post I realised *pl indeed can be set once and then only modified, but because I suck at using binary operators (I never could really understand them), I'll have hard time trying to make a good loop. Thanks for your post.

Posted: Wed Aug 04, 2010 2:00 am
by Gigasoft
The VDP address autoincrements by the amount set in register 15. So, you only need to set the address once.

Posted: Wed Aug 04, 2010 9:50 am
by Jaklub
Thank you, now I have a single loop. Problem solved.

Posted: Thu Aug 05, 2010 1:30 pm
by 8bitwizard
I use these for talking to the VDP:

Code: Select all

#define VDP_DATA     ((volatile unsigned short *) 0xC00000)
#define VDP_CTRL     ((volatile unsigned short *) 0xC00004)
#define VDP_STAT     ((volatile unsigned short *) 0xC00004)

#define VDP_DATA_L   ((volatile unsigned long *) 0xC00000)
#define VDP_CTRL_L   ((volatile unsigned long *) 0xC00004)
Then I can do things like "*VDP_DATA = *p++;"

I have a whole .h file full of hardware addresses like that.