pointers and such (are these two functions the same?)

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

pointers and such (are these two functions the same?)

Post by Quzar »

Ok, I'm trying to rewrite the function below by using an assembly function draw_font that i have a C version of. The function I'm trying to replace is :

Code: Select all

__inline__ void draw_fix(u16 code, u16 colour, u16 sx, u16 sy, u16 * palette, char * fix_memory)
{
	u8 y;
	u32 mydword;
	u32 * fix=(u32*)&(fix_memory[code<<5]);
	u16 * dest;
	u16 * paldata=&palette[colour];
	u16 col;

	for(y=0;y<8;y++)
	{
		dest    = video_line_ptr[sy+y]+sx;
		mydword  = *fix++;
		
		col = (mydword>> 0)&0x0f; if (col) dest[0] = paldata[col];
		col = (mydword>> 4)&0x0f; if (col) dest[1] = paldata[col];
		col = (mydword>> 8)&0x0f; if (col) dest[2] = paldata[col];
		col = (mydword>>12)&0x0f; if (col) dest[3] = paldata[col];
		col = (mydword>>16)&0x0f; if (col) dest[4] = paldata[col];
		col = (mydword>>20)&0x0f; if (col) dest[5] = paldata[col];
		col = (mydword>>24)&0x0f; if (col) dest[6] = paldata[col];
		col = (mydword>>28)&0x0f; if (col) dest[7] = paldata[col];
	}
}
I'm trying to replace it with these two (mind you draw_font isn't alterable[black box]):

Code: Select all

INLINE void draw_fix(u16 code, u16 colour, u16 sx, u16 sy, u16 * palette, char * fix_memory){
	unsigned short *br;
	unsigned short *secondthing;
	u32 * fix=(u32*)&(fix_memory[code<<5]);
	draw_font(*br, secondthing, fix);
	u16 * paldata=&palette[colour];
	u16 * dest;
	u8 y;
	for(y=0;y<8;y++)
	{
		dest = video_line_ptr[sy+y]+sx;
		if(br[0]) dest[0] = paldata[br[0]];
		if(br[1]) dest[1] = paldata[br[1]];
		if(br[2]) dest[2] = paldata[br[2]];
		if(br[3]) dest[3] = paldata[br[3]];
		if(br[4]) dest[4] = paldata[br[4]];
		if(br[5]) dest[5] = paldata[br[5]];
		if(br[6]) dest[6] = paldata[br[6]];
		if(br[7]) dest[7] = paldata[br[7]];
		br+=8;
	}

}
static __inline__ void draw_font(unsigned short *br, unsigned short *paldata, unsigned *gfxdata)
{
    int y;
    for(y=0;y<8;y++) {
	register unsigned int myword = gfxdata[0];
	br[0]=paldata[(myword)&0xf];
	br[1]=paldata[(myword>>4)&0xf];
	br[2]=paldata[(myword>>8)&0xf];
	br[3]=paldata[(myword>>12)&0xf];
	br[4]=paldata[(myword>>16)&0xf];
	br[5]=paldata[(myword>>20)&0xf];
	br[6]=paldata[(myword>>24)&0xf];
	br[7]=paldata[(myword>>28)&0xf];
	
	br+=8;
	gfxdata++;
    }
}
the second of the two crashes my DC. any idea as to why?
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has thanked: 0
Been thanked: 1 time

Re: pointers and such (are these two functions the same?)

Post by quarn »

quzar wrote:

Code: Select all

	unsigned short *br;
	unsigned short *secondthing;
	...
	draw_font(*br, secondthing, fix);
the second of the two crashes my DC. any idea as to why?
Yeah, "br" and "secondthing" does not point to anything.

Without having tested, or looked too much at the code, I'd say that you should try:

Code: Select all

	unsigned short br[8];
	unsigned short *secondthing = &pallette[colour];
	...
	draw_font(br, secondthing, fix);
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

ok, yea that made sense and i fixed a few bits based on it. it now doesnt crash, just gives incorrect data.

Code: Select all

INLINE void draw_fix(u16 code, u16 colour, u16 sx, u16 sy,u16 * palette, char * fix_memory){
	unsigned short * br1[64];
	unsigned short * br = (unsigned short *)br1;
	unsigned short * paldata=&palette[colour];
	u32 * fix=(u32*)&(fix_memory[code<<5]);

	draw_font((unsigned short *) br, paldata, fix);
	
	u16 * dest;
	u8 y;
	for(y=0;y<8;y++)
	{
		dest = video_line_ptr[sy+y]+sx;
		if(br[0]) dest[0] = paldata[br[0]];
		if(br[1]) dest[1] = paldata[br[1]];
		if(br[2]) dest[2] = paldata[br[2]];
		if(br[3]) dest[3] = paldata[br[3]];
		if(br[4]) dest[4] = paldata[br[4]];
		if(br[5]) dest[5] = paldata[br[5]];
		if(br[6]) dest[6] = paldata[br[6]];
		if(br[7]) dest[7] = paldata[br[7]];
		br+=8;
	}

}
static __inline__ void draw_font(unsigned short *br, unsigned short *paldata, unsigned *gfxdata) 
{ 
    int y; 
    for(y=0;y<8;y++) { 
   register unsigned int myword = gfxdata[0]; 
   br[0]=paldata[(myword)&0xf]; 
   br[1]=paldata[(myword>>4)&0xf]; 
   br[2]=paldata[(myword>>8)&0xf]; 
   br[3]=paldata[(myword>>12)&0xf]; 
   br[4]=paldata[(myword>>16)&0xf]; 
   br[5]=paldata[(myword>>20)&0xf]; 
   br[6]=paldata[(myword>>24)&0xf]; 
   br[7]=paldata[(myword>>28)&0xf]; 
    
   br+=8; 
   gfxdata++; 
    } 
}
That is the new replacement set, but its still slightly wrong somehow :?
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has thanked: 0
Been thanked: 1 time

Post by quarn »

Code: Select all

INLINE void draw_fix(u16 code, u16 colour, u16 sx, u16 sy,u16 * palette, char * fix_memory){
   unsigned short br1[64]; // removed *
   unsigned short * br = (unsigned short *)br1; // and this is not really needed..
   unsigned short * paldata=&palette[colour];
   u32 * fix=(u32*)&(fix_memory[code<<5]);

   // paldata isn't really used in draw_font anymore.. see below
   draw_font((unsigned short *) br, paldata, fix);
   
   u16 * dest;
   u8 y;
   for(y=0;y<8;y++)
   {
      dest = video_line_ptr[sy+y]+sx;
      if(br[0]) dest[0] = paldata[br[0]];
      if(br[1]) dest[1] = paldata[br[1]];
      if(br[2]) dest[2] = paldata[br[2]];
      if(br[3]) dest[3] = paldata[br[3]];
      if(br[4]) dest[4] = paldata[br[4]];
      if(br[5]) dest[5] = paldata[br[5]];
      if(br[6]) dest[6] = paldata[br[6]];
      if(br[7]) dest[7] = paldata[br[7]];
      br+=8;
   }

}
static __inline__ void draw_font(unsigned short *br, unsigned short *paldata, unsigned *gfxdata)
{
    int y;
    for(y=0;y<8;y++) {
   register unsigned int myword = gfxdata[0];
    // original code does not do two pallette lookups..
   br[0]=/*paldata[*/(myword)&0xf/*]*/;
   br[1]=/*paldata[*/(myword>>4)&0xf/*]*/;
   br[2]=/*paldata[*/(myword>>8)&0xf/*]*/;
   br[3]=/*paldata[*/(myword>>12)&0xf/*]*/;
   br[4]=/*paldata[*/(myword>>16)&0xf/*]*/;
   br[5]=/*paldata[*/(myword>>20)&0xf/*]*/;
   br[6]=/*paldata[*/(myword>>24)&0xf/*]*/;
   br[7]=/*paldata[*/(myword>>28)&0xf/*]*/;

   br+=8;
   gfxdata++;
    }
}
What's wrong with the original code btw?
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

the point was to write the draw_fix to work around the draw_font function =\. There is nothing wrong at all with the original, except i have a fast asm version of the draw_font function, and am trying to make a wrapper around it to fill in what it doesnt do (someone wrote it, i have no clue AT ALL with asm, and i think itll be slightly faster) .

based on what you did, i figured out what my problem was though (kindah) The following works a lot better, but is still slightly off.

Code: Select all

INLINE void draw_fix2(u16 code, u16 colour, u16 sx, u16 sy,u16 * palette, char * fix_memory){
	u16 * br1[64];
	u16 * br = (u16 *)br1;
	unsigned short * paldata=&palette[colour];
	u32 * fix=(u32*)&(fix_memory[code<<5]);

	draw_font((unsigned short *) br, paldata, fix);
	
	u16 * dest;
	u8 y;
	for(y=0;y<8;y++)
	{
		dest = video_line_ptr[sy+y]+sx;
		if(br[0]) dest[0] = br[0];
		if(br[1]) dest[1] = br[1];
		if(br[2]) dest[2] = br[2];
		if(br[3]) dest[3] = br[3];
		if(br[4]) dest[4] = br[4];
		if(br[5]) dest[5] = br[5];
		if(br[6]) dest[6] = br[6];
		if(br[7]) dest[7] = br[7];
		br+=8;
	}

}
static __inline__ void draw_font(unsigned short *br, unsigned short *paldata, unsigned *gfxdata)
{
    int y;
    for(y=0;y<8;y++) {
	register unsigned int myword = gfxdata[0];
	br[0]=paldata[(myword)&0xf];
	br[1]=paldata[(myword>>4)&0xf];
	br[2]=paldata[(myword>>8)&0xf];
	br[3]=paldata[(myword>>12)&0xf];
	br[4]=paldata[(myword>>16)&0xf];
	br[5]=paldata[(myword>>20)&0xf];
	br[6]=paldata[(myword>>24)&0xf];
	br[7]=paldata[(myword>>28)&0xf];
	
	br+=8;
	gfxdata++;
    }
}
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has thanked: 0
Been thanked: 1 time

Post by quarn »

I would remove the "*" from the br1 declaration. Having it there declares br1 to be an array of pointers to u16 values. Meaning, that br[0] and br[1] is br1[0], br[2] and br[3] is br1[1] etc as pointers take 4 bytes. So, remove the "*" from the br1 declaration.

Second, the if (br[x])-tests does not really make sense unless you have black as transparent colour...

Anyway, I was bored so I wrote an asm-version for you. Though, I have not tested it so it might crash :)

Code: Select all

	.align 2
	.global _draw_fix_asm
	! draw_fix asm version for quzar. not really tested... sorry
	! r4 = code (unsigned short)
	! r5 = colour (unsigned short)
	! r6 = screenx (unsigned short)
	! r7 = screeny (unsigned short)
	! r15 = pallette (unsigned short*)
	! r15+1 = fix_memory (char *)
_draw_fix_asm:
	mov.l	@r15+,r1	! pallette
	mov.l	@r15+,r2	! fix_memory
	
	add	#-4*2,r15
	mov.l	r8,@-r15

	shll2	r4
	shll2	r4
	shll	r4		! code << 5
	shll	r5		! colourindex used to index a short array. (each index worth 2 bytes)
	shll	r6		! screenx index a short array
	shll	r7		! screeny -"-

	mov.l	video_line_ptr, r8
	add	r7,r8
	
	add	r5,r1		! r1 = &pallete[colour]
	add	r4,r2		! r2 = &fix_memory[code << 5]

	mov	#8,r3		! counter
_dfa_loop:
	mov	#8,r4		! counter for inner loop
	mov.l	@r2+,r5		! "myword"
	mov.w	@r8+,r7
	add	r6, r7		! "dest"
_dfa_loop2:
	mov	r5,r0
	and	#0xf,r0
	shll	r0

	shlr2	r5
	shlr2	r5

	cmp/pl	r0
	bf	_dfa_skip

	add	r1,r0		! &paldata[myword &0xf]
	mov.w	@r0,r0
	mov.w	r0,@r7
_dfa_skip:
	add	#2,r7		! next pixel 
	dt	r4		! decrase inner counter
	bf	_dfa_loop2	! loop if inner counter > 0

	! next row
	dt	r3		! decrease counter
	bf	_dfa_loop	! loop if counter > 0

	rts
	mov.l	@r15+,r8
	
	.align 4
video_line_ptr:
	.long	_video_line_ptr
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

Thanks for the effort, it crashes my DC though =(
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has thanked: 0
Been thanked: 1 time

Post by quarn »

quzar wrote:Thanks for the effort, it crashes my DC though =(
Yeah, I thought it would... If you give me a link to the code, I might try some more when I have time.
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

quarn wrote:
quzar wrote:Thanks for the effort, it crashes my DC though =(
Yeah, I thought it would... If you give me a link to the code, I might try some more when I have time.
The code is above, unless you mean the asm version of draw_font, which shouldnt matter.

Its the file video/draw_fix.c inside the following zip.
http://www.screamcast.net/publicdownloa ... Cv2src.zip
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has thanked: 0
Been thanked: 1 time

Post by quarn »

Ok, I got it to compile with my kos-setup and everything, though I do not have a NEO GEO cd so I'm not sure how I am supposed to test if the code works... Are there any free (legal and good) NEO GEO cds available on the net?
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

http://www.pdroms.de/file_details.php?fn=1198

im not sure that it will show wether it works or not, because the last one you have me only had minor graphical problems with it
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has thanked: 0
Been thanked: 1 time

Post by quarn »

I get a "loading game please wait"-text and a "NEOGEO DEMO EN"-text. Then what?
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

then it should start running the program =\. if it doesnt there is something wrong with it =(.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
quarn
DC Developer
DC Developer
Posts: 80
Joined: Wed Oct 17, 2001 7:44 pm
Location: Sweden
Has thanked: 0
Been thanked: 1 time

Post by quarn »

Ok.. I guess there is nothing I can do then..
Post Reply