Author Topic: What Needs Fixed In Huc?  (Read 2677 times)

touko

  • Hero Member
  • *****
  • Posts: 953
Re: What Needs Fixed In Huc?
« Reply #15 on: October 05, 2011, 08:38:11 PM »

Rover, or Touko, do you have any C GOTO examples that would really sway my opinion here?

Yes, a simply exemple, checking pad entries .
In C you must cascading if else, elseif .
Many generating code to do that, in ASM a single jmp do the same.

another one, when your entry was tested, a simple pointer jmp avoids to recheck all entries, untill previous action was finished.

And a code with GOTO and good comments, is better than a lack pure C one IMO .

In a real C application, if GOTO is bad, globals variables are bad too .
« Last Edit: October 05, 2011, 08:48:43 PM by touko »

spenoza

  • Hero Member
  • *****
  • Posts: 2751
Re: What Needs Fixed In Huc?
« Reply #16 on: October 06, 2011, 06:36:36 PM »
Actually, I've heard a lot of good arguments that global variables are indeed bad...
<a href="http://www.pcedaisakusen.net/2/34/103/show-collection.htm" class="bbc_link" target="_blank">My meager PC Engine Collection so far.</a><br><a href="https://www.pcenginefx.com/forums/" class="bbc_link" target="_blank">PC Engine Software Bible</a><br><a href="http://www.racketboy.com/forum/" c

nodtveidt

  • Guest
Re: What Needs Fixed In Huc?
« Reply #17 on: October 07, 2011, 02:36:03 AM »
Global variables... depends on the application. I've yet to actually hear any real arguments against them outside of "compiler confusion" and "name collision". The first can be fixed by forcing compiler makers to get off their lazy asses and fix their shit, and the second can be fixed by forcing coders to get off their lazy asses and fix their shit. You obviously don't always use globals, because in normal coding, local variables are often more efficient, and the larger the application, the more noticeable it becomes. However, in HuC, it's the other way around... globals are noticeably better for a multitude of reasons, not the least of which being the fact that HuC has *still more bugs* when it comes to variables... I discovered this when fully entrenched in MSR's code. It's hard to explain and even harder to trace, but it worked like this...

In two cases, global variables declared at the module level were not acting as designed. This was noticeable when doing cutscenes. The values of two variables were being ignored by if statements and were incrementing to their full values before overflowing and coming back to levels where if would recognize their value range. This was especially apparent in the case where I was using one to do a palette rotation... the colors spazzed out for quite awhile before they finally started cycling correctly. However, this problem only manifested when the game had been running for awhile... if you just started up the game and used the debugger to go directly to the parts where the bug happened, they worked as designed. So I figure it was memory corruption of some kind. However, by moving these variables out of the module and into globals.h, the problem disappeared entirely and things worked as designed all the time.

TheOldMan

  • Hero Member
  • *****
  • Posts: 958
Re: What Needs Fixed In Huc?
« Reply #18 on: October 07, 2011, 02:38:44 AM »
Don't confuse HuC with a 'real' C compiler.....

In a real c compiler, if could do something like

jv = joy(0);
mask = 0x01;
for( i = 0; i < 8; i ++ )
{
    if( jv & mask )
   {
        *(f)();
        return;
   }
   mask <<= 1;
}
and have a seperate routine for each joystick button. Compact, and on a real compiler, pretty efficient.

If you need gotos to do joystick handling, you might want to re-think your code. Either use a case statement, or subroutines for the inner part of the ifs. Long, nested ifs themseleves aren't bad: but too much code between the if and the else can make it hard to follow.

And yeah, global variables are another one of those good/bad situations. No one wants to type in 50 parameters to a function call, so they have their uses, even in 'real' C. But they definately make things harder to follow and debug.
...........................................................................

Unfortunately, no better options exist in HuC. Live with it. Let's just decide what -can- be fixed :)

[The opinions expressed here are those of the author. You have the right to your own opinion. Don't flame me for what I think unless you have a really good reason for another viewpoint. Personally, I have been programming for 20+ years, and only used a goto -twice-. Of course, I'm not trying to write the most optimized code, either. I am with Ritchie (of K&R): Make it work first. Then, go measure it so you know what needs speeded up. Then, and -only- then, go back and make it fast :) ]

nodtveidt

  • Guest
Re: What Needs Fixed In Huc?
« Reply #19 on: October 07, 2011, 02:51:03 AM »
I've noticed that switch generates a ton of overhead. It seems like it's actually better to use a series of if comparisons rather than a switch. This can present major problems when writing state machines or control schemes.

TheOldMan

  • Hero Member
  • *****
  • Posts: 958
Re: What Needs Fixed In Huc?
« Reply #20 on: October 07, 2011, 03:05:25 AM »
Quote
I've noticed that switch generates a ton of overhead. It seems like it's actually better to use a series of if comparisons rather than a switch.

In Huc, probably (lol). In 'real' C, not so much. At least I don't think so because I've never measured it.

I would have to check how many if/elseif/else statements it would take to match the overhead of a switch in HuC.
But, at some point, a switch -is- going to be 'better'.

And if you're writing state machines in HuC using if's, it's not going to be very efficient. At least, not until the pointer problems get fixed.

nodtveidt

  • Guest
Re: What Needs Fixed In Huc?
« Reply #21 on: October 07, 2011, 03:33:10 AM »
At present, I'm writing state machines with switch but it consumes buttloads of code space. I've had to split complex FSMs across two functions because of the huge amount of code waste it generates.

TheOldMan

  • Hero Member
  • *****
  • Posts: 958
Re: What Needs Fixed In Huc?
« Reply #22 on: October 07, 2011, 03:51:07 AM »
[Personal opinion below. Not commenting about your code. Just my approach to the problem]

Quote
I've had to split complex FSMs across two functions
Only two?? The point of a FSM is using the 'state' as an index to a function call. I could -never- keep that much code straight in my head! Somewhere here I have an 8 state machine that handles double-jumping and acceleration for sprite movement, and it's probably 14-15 subroutines. I can't imagine trying to do that in just 2!


nodtveidt

  • Guest
Re: What Needs Fixed In Huc?
« Reply #23 on: October 07, 2011, 04:10:40 AM »
I've never had to use more than 3 functions to do a complete entity system. Monolith uses this three-function setup for the player entity. Most of the state handler is interlaced with the controller scheme, which uses two functions. The third function handles all the states that are not affected by the controller scheme. Other entities require a single function for their state machines. Valkyrie Midnight uses a two-function system with independent controller scheme and state handler, but that's fine because the system is way less complex. Monolith uses almost twice the number of states so I made it as efficient as possible. Right now, I'm also working on a Bomberman-type game, and the state machines only have 5 states... two functions only, and they will both fit in the same code bank. If I wanted to be even more efficient, I could put the entire thing into one function... but I think that that's overkill, and would require a redesign anyway. No sense in breaking something that already works great. :D

MotherGunner

  • Hero Member
  • *****
  • Posts: 2991
Re: What Needs Fixed In Huc?
« Reply #24 on: October 07, 2011, 06:13:05 AM »
Good read even though I don't understand it much, thanks guys.  Btw seeing both of you old men go back and forth on this thread reminded me of Trading Places!  Ha ha ha!

But who is Randolph and who is Mortimer?  :twisted:

-MG

SI VIS PACEM, PARA BELLUM (If you want peace, Prepare for war)
SI VIS BELLUM, PARA MATRIMONIUM (If you want war, Prepare for marriage)

nodtveidt

  • Guest
Re: What Needs Fixed In Huc?
« Reply #25 on: October 07, 2011, 06:17:27 AM »
Dunno... I'm probably Mortimer. :D

Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: What Needs Fixed In Huc?
« Reply #26 on: October 07, 2011, 07:25:21 AM »
switches are faster than if's if you set them up right.  I did experiments with that in both C and C#, lol.  It's not a huge speedup, but it's a speedup. 

go figure its the opposite in HuC.

globals are great, as long as you aren't passing the code around for others to work on also.   For a game project or something of this caliber, globals are fine.  You'll have 1 to 3 people actually using them, and if you can't keep it straight between the 3 of you, you're all retarded.

:) lol


Pointers to functions would be great.  then we could do some real programming.
[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

TheOldMan

  • Hero Member
  • *****
  • Posts: 958
Re: What Needs Fixed In Huc?
« Reply #27 on: October 07, 2011, 07:33:15 AM »
Quote
Pointers to functions would be great.  then we could do some real programming.
Working pointers (to functions or not) would be good. <lol>

I think I'll dump the HuC library files to a printer and start modularizing that, first.
Then, maybe, optimizing code packing.

Oh, and I'm the one who's the flunky. The guy standing up, iirc.
« Last Edit: October 07, 2011, 07:35:03 AM by TheOldMan »

nodtveidt

  • Guest
Re: What Needs Fixed In Huc?
« Reply #28 on: October 07, 2011, 08:01:25 AM »
Pointers to functions would be great.  then we could do some real programming.
set_joy_callback() is the only place I've seen it so far. I use it in MSR during cutscenes so they can be skipped.

touko

  • Hero Member
  • *****
  • Posts: 953
Re: What Needs Fixed In Huc?
« Reply #29 on: October 07, 2011, 08:47:50 AM »
With huc switch/case are very slow, i don't use that statement in huc ..
i have tested with if, elseif, and it's more efficient in term of speed, but code is very dirty too .

You can test, with a single proc call in a vsync ..
test your proc with if, elseif, and with switch case ..

Another strange thing with Huc ..

spr_set(1) (not the only one), huc convert this function in asm with :

Code: [Select]
ldx #low(1)
lda #high(1)

call _spr_set
Just fine  :-" ..

just with this liltle routine you can loose up to 128 cycles/frame .
« Last Edit: October 07, 2011, 09:50:55 AM by touko »