Guardian Force De-Lag Patch

This is the main shmups forum. Chat about shmups in here - keep it on-topic please!
Post Reply
User avatar
trap15
Posts: 7835
Joined: Mon Aug 31, 2009 4:13 am
Location: 東京都杉並区
Contact:

Guardian Force De-Lag Patch

Post by trap15 »

The following information (and the ROM) are available at my website: http://daifukkat.su/hacks/grdforcel/

Success's hidden gem Guardian Force is a truly amazing game, but has one very unfortunate flaw: the program is extremely poorly optimized in terms of input latency. Even on the ST-V arcade hardware, the game has somewhere on the order of 6 to 7 frames of input lag. This is beyond the point of uncomfortable, and makes the game almost unbearably hard to control.

So, I had a feeling there would be some low-hanging fruit here. You don't get 6 to 7 frames of input lag if you're even remotely paying attention to how your inputs are handled. So, time to pop open IDA and MAME and get cracking. Turns out, yes there are at least 2 frames of easy-to-fix lag in software.

Here's a little tutorial on setting up the patch, since it's a little different from most things. But hopefully it should be obviously easier than most. :)

Installing

Burn the binary file (available on my website) to any 8-bit ROM with a similar pinout to the 27C010. The file size is very small, and this is the only ROM size required. So you could use a 27C020, 040, 080, etc. You don't need to even repeat the file for those ROMs, it's very forgiving. :)

Once your ROM is burned, open up the cart. You will see an empty EPROM socket on the cart's board, as shown below.

Image

Simply put the ROM you've burned in that socket, and close it back up. That's it! Sega was very clever when they designed their carts, weren't they? ;)

Image

Dev Log

So with this, I had to figure out how the patch EPROM worked on the ST-V. It turns out it's really simple, all I really had to do was take parts of the beginning of the regular ROM data (just the bootstrap, essentially) and work some patching code into it. From there, I needed to find the sources of input lag and devise how to remove them. So, it's watchpoint time.

In the end, I traced the input data coming from the hardware through 2 interstitial buffers which were implemented by Success. We'll name the points here as:
  • hw_inp, the input data exposed directly from the hardware
  • smpc_buffer, the input data which would normally come from Saturn pads
  • smpc_parsed, the input data which is derived from smpc_buffer
Now, the critical thing to know about these buffers is that they're not all updated in one place, or at one time. Normally the smpc_buffer needs to be updated by the Saturn's input reading microcontroller, but the ST-V doesn't use this for inputs. Success clearly wrote their code for Saturn first, as the code which would normally read the data back from the microcontroller actually patches in data from hw_inp into the smpc_buffer.

We can look at these buffers in two ways: who fills them, and what order are they filled? If the buffers are used optimally (in a way which does not increase latency), these should essentially flow the exact same way. In Guardian Force's case, they literally flow the opposite.

Code: Select all

Who fills who?
Hardware -fills-> hw_inp -fills-> smpc_buffer -fills-> smpc_parsed -fills-> Player's Input Code

What order are they filled?
Frame Start -> hw_inp -> Player's Input Code -> smpc_parsed -> smpc_buffer -> Frame End
For those paying attention, each one of these fills is introducing another frame of lag. The player's input code reads from smpc_parsed which isn't updated until after, meaning the data it is using is from the previous frame. The smpc_parsed buffer reads from smpc_buffer, which also isn't updated until after, meaning that too is reading the previous frame's buffer. smpc_buffer is the only one that isn't itself lagged, since hw_inp is always the current frame's data.

The obvious fix here is to invert the order of fills. In this way, each buffer reads from the buffer that was just filled, meaning it's still along the same frame's data. Thus, we remove 2 frames of input lag.

I'm sure there are more dumb sources of lag (it's still somewhere around 4 frames on hardware!), but this is such a massive improvement already that I figured it was worth posting.
@trap0xf | daifukkat.su/blog | scores | FIRE LANCER
<S.Yagawa> I like the challenge of "doing the impossible" with older hardware, and pushing it as far as it can go.
User avatar
EmperorIng
Posts: 5064
Joined: Mon Jun 18, 2012 3:22 am
Location: Chicago, IL

Re: Guardian Force De-Lag Patch

Post by EmperorIng »

Awesome, trap! I share your assessment of Guardian Force: it's amazing, and an epic adventure (one I'm not good enough to 1CC - more like 7CC lol). I wonder if the input lag extends to the Saturn port, given that it's "arcade accurate." I always wondered whether or not my eyes played tricks on me, or the tank controlled "slippery."
User avatar
trap15
Posts: 7835
Joined: Mon Aug 31, 2009 4:13 am
Location: 東京都杉並区
Contact:

Re: Guardian Force De-Lag Patch

Post by trap15 »

I would be really surprised if the Saturn version was any different in terms of lag. If anything it's worse I imagine, since it was definitely developed "first". It certainly should have felt like it was a bit slippery, with 6-7 frames of lag ;)
@trap0xf | daifukkat.su/blog | scores | FIRE LANCER
<S.Yagawa> I like the challenge of "doing the impossible" with older hardware, and pushing it as far as it can go.
User avatar
Shepardus
Posts: 3505
Joined: Sat Dec 13, 2014 10:01 pm
Location: Ringing the bells of fortune

Re: Guardian Force De-Lag Patch

Post by Shepardus »

I always thought it was a MAME flaw that introduced all the input lag, didn't know it was like that on actual hardware too. Maybe I should give it another try (with this patch, of course)...
Image
NTSC-J: You know STGs are in trouble when you have threads on how to introduce them to a wider audience and get more people playing followed by threads on how to get its hardcore fan base to play them, too.
1CCs | Twitch | YouTube
User avatar
Xyga
Posts: 7181
Joined: Tue Nov 05, 2013 8:22 pm
Location: block

Re: Guardian Force De-Lag Patch

Post by Xyga »

IIRC mame's lag mostly comes from its video layer plus the computer's gfx and driver and settings on api, that's what builds like groovy and also retroarch now intervene on to reduce lag. basically a 'normal' mame build running on direct3d or bgfx and syncing or buffering to eliminate tearing will add up to 3 frames at all times, while groovy and retroarch either with d3d9ex or opengl and built-in features like 'frame_delay' can reduce the queue to one frame and even allow you to trim some more ms on the remaining frame of the video layer.

Then it was my understanding that the lag in the game's mame hardware driver is a different matter.
while I think this is what shmupmame intervenes on, that build lacks what groovy and retroarch have, so in the end it's less efficient at reducing lag and even desyncs sprite layers in some games.

EDIT: well at least that's what I understand of it, now what the gurus of emulation tweaking really do is beyond me.
Anyway it's relatively easy to check while playing.
Strikers1945guy wrote:"Do we....eat chicken balls?!"
User avatar
donluca
Posts: 852
Joined: Sat Feb 28, 2015 8:51 pm
Location: Italy
Contact:

Re: Guardian Force De-Lag Patch

Post by donluca »

Groovymame had recently a breakthrough and demonstrated that next-frame response is possible.

It has already been implemented but doesn't work on all games due to how some game drivers were written.

Now it's been discussing that this would break a correct 100% emulation of the system since several boards have some inherent lag in them. With GroovyMAME you can now actually achieve lower lag than the original board.
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

Re: Guardian Force De-Lag Patch

Post by zak »

Amazing work trap! Thanks for sharing! :)
User avatar
OmegaFlareX
Posts: 884
Joined: Tue Jan 25, 2005 10:15 pm
Location: Virginia, USA

Re: Guardian Force De-Lag Patch

Post by OmegaFlareX »

I'm guessing both ST-V Cotton games also used this buffer-chain system? They also have hideous input lag in MAME, but I haven't played on real hardware.
User avatar
Xyga
Posts: 7181
Joined: Tue Nov 05, 2013 8:22 pm
Location: block

Re: Guardian Force De-Lag Patch

Post by Xyga »

donluca wrote:Groovymame had recently a breakthrough and demonstrated that next-frame response is possible
Current frame even!

Didn't mention because this is still experimental, but this will certainly be a nice feature to have along frame_delay, useful for someone who would have a laggy display for instance, and need to squeeze lag to a minimum.
Strikers1945guy wrote:"Do we....eat chicken balls?!"
User avatar
donluca
Posts: 852
Joined: Sat Feb 28, 2015 8:51 pm
Location: Italy
Contact:

Re: Guardian Force De-Lag Patch

Post by donluca »

Xyga wrote:Current frame even!

Didn't mention because this is still experimental, but this will certainly be a nice feature to have along frame_delay, useful for someone who would have a laggy display for instance, and need to squeeze lag to a minimum.
It's the evolution of frame_delay, which is going to be removed when they'll manage to work with the slices in a way which the MAME game drivers cooperate. They are not not be used together, unless you meant to use frame delay with games which doesn't support the new slice thing :)
User avatar
Xyga
Posts: 7181
Joined: Tue Nov 05, 2013 8:22 pm
Location: block

Re: Guardian Force De-Lag Patch

Post by Xyga »

yup the one or the other, surely both features can cohabitate in the same build. if frame slice cuts into the actual delay existing with the real hardware it indeed breaks accurate emulation so it shouldn't be the only choice for lag reduction.

anyway we'll see what happens in the future
Strikers1945guy wrote:"Do we....eat chicken balls?!"
User avatar
donluca
Posts: 852
Joined: Sat Feb 28, 2015 8:51 pm
Location: Italy
Contact:

Re: Guardian Force De-Lag Patch

Post by donluca »

Yeah, besides, these are very exciting times.

Thanks to MAME (and GroovyMAME) development we might see one day a fantastic (and free!) solution to play all those arcade games with 100% accuracy.

Managing to having them running flawlessly on a CRT is huge step forward, I think that the solution for LCD panels will arrive when MAME will start supporting properly adaptive sync monitors so they'll be able to run at the correct speed without any trickery.

Oh, and we're going to need much, much higher resolutions. The reason is that in the CRT era, pixels were never meant to be square.

Take for example ESPGaluda: its native resolution is 448x224 which is obviously very, very far from 4:3. This means that when you play it on a LCD, in order to achieve the correct aspect ratio you're going to need fractional scaling on one axis and this will look good only if you have a very very high resolution panel on which you can stretch that 448 pixels.

Maybe retina displays could be enough.

Anyway, I got a little bit carried away, sorry for derailing the thread!
User avatar
Despatche
Posts: 4196
Joined: Thu Dec 02, 2010 11:05 pm

Re: Guardian Force De-Lag Patch

Post by Despatche »

There are numerous times when pixel artists would assume square pixels. This happened less with arcade hardware than with consoles, but it still very much happened... NMK/Jaleco hardware comes to mind. All hardware considered, CPS1/2/3 and PGM were some really big exceptions.

NES, SNES, PC Engine, Master System, etc, they typically rely on internal resolution. The big exception is the Mega Drive's "4:3-like" mode that a lot of games used. Other than that, attempting to play most of the games made for these platforms on 4:3 screens results in a distorted picture and distorted object movement. It's exactly like attempting to play a 4:3 PC game stretched to 16:9. Unsurprisingly, many people do just that, and don't see the problem even when you point it out to them.
Rage Pro, Rage Fury, Rage MAXX!
User avatar
Shepardus
Posts: 3505
Joined: Sat Dec 13, 2014 10:01 pm
Location: Ringing the bells of fortune

Re: Guardian Force De-Lag Patch

Post by Shepardus »

donluca wrote:Take for example ESPGaluda: its native resolution is 448x224 which is obviously very, very far from 4:3. This means that when you play it on a LCD, in order to achieve the correct aspect ratio you're going to need fractional scaling on one axis and this will look good only if you have a very very high resolution panel on which you can stretch that 448 pixels.
Technically all you need is 672x896 (each of the 224x448 pixels scaled to 3x2) or some multiple thereof if all you require is that every pixel be scaled to the same size. It's easier for Espgaluda with its simple 2:1 pixel ratio than it is for something like, say, Progear with its 12:7 pixel ratio (to achieve the same with Progear you would need to scale every pixel to 7x9, or 384x224 to 2688x2016). At high enough resolution, though, a bit of uneven scaling is hardly noticeable, especially in motion.
Image
NTSC-J: You know STGs are in trouble when you have threads on how to introduce them to a wider audience and get more people playing followed by threads on how to get its hardcore fan base to play them, too.
1CCs | Twitch | YouTube
User avatar
Xyga
Posts: 7181
Joined: Tue Nov 05, 2013 8:22 pm
Location: block

Re: Guardian Force De-Lag Patch

Post by Xyga »

Oh that little tearing line in Ketsui Extra in the middle of the screen when you set the zoom to 93% which is the closest to integer...save for that fucking pixel. The rage.
And you have no choice but to use one of their crappy filters if you want it gone lol so your zoom level sien't uselful anymore anyway lol.

You know me when I have room for clean integer I don't care about anything else. :p

Hugh I'm polluting this fine thread again sorry.
Strikers1945guy wrote:"Do we....eat chicken balls?!"
User avatar
Shou
Posts: 443
Joined: Wed Jan 04, 2006 7:12 am
Location: Central Tokyo, Japan, Asia, Earth, Solar System, Milky Way
Contact:

Re: Guardian Force De-Lag Patch

Post by Shou »

Too bad the true original version on SPI is lost.
become history
User avatar
ifog
Posts: 135
Joined: Wed Oct 01, 2008 12:23 pm
Location: Nice
Contact:

Re: Guardian Force De-Lag Patch

Post by ifog »

Thank you Trap. :!:
Will test it this weekend.
User avatar
LordHypnos
Posts: 1960
Joined: Sun Mar 09, 2014 11:59 pm
Location: Mars Colony, 2309

Re: Guardian Force De-Lag Patch

Post by LordHypnos »

Xyga wrote:Oh that little tearing line in Ketsui Extra in the middle of the screen when you set the zoom to 93% which is the closest to integer...save for that fucking pixel. The rage.
And you have no choice but to use one of their crappy filters if you want it gone lol so your zoom level sien't uselful anymore anyway lol.

You know me when I have room for clean integer I don't care about anything else. :p

Hugh I'm polluting this fine thread again sorry.
You must really hate playing CPS2 games then. Nothing dumber (from a 2018 perspective) than 384x224 resolution that's supposed to be adjusted to a 4:3 aspect ratio.
YouTube | Restart Syndrome | 1cclist | Go Play Mars Matrix
Solunas wrote:How to Takumi your scoring system
1) Create Scoring System
2) Make it a multiplier for your actual score
User avatar
Xyga
Posts: 7181
Joined: Tue Nov 05, 2013 8:22 pm
Location: block

Re: Guardian Force De-Lag Patch

Post by Xyga »

No problem either on a crt (doesn't apply) or lcd, on the latter I use integers for instance on a 1080p display cps game are set to 4x5, I lose a few lines top and bottom but those are ususally empty space so I don't lose anything and it looks perfect af. IIRC marqs offers that option for his cps2 interface.
Full-HD displays are a bit limited to opt for integer-everything but most of the issues go away as long as you use settings for both yoko and tate, 320x240, 384x224, 448x224, 320x232 etc, there are good settings for most common resolutions as long as you accept a bit of overscan or underscan.
For fractional emulators/gpus or display's built-in scalers usually suck ass unless you use a select few shaders that can completely hide artifacts for the former, or a Sony TV for the latter. Otherwise an external scaler will do a better job. Unlike a lot of people who trust only the numbers I'm not so enthusiastic to see the panel's resolutions ever-increase, because I know that alone doesn't fix bad scaling.
Strikers1945guy wrote:"Do we....eat chicken balls?!"
SuperDeadite
Posts: 1000
Joined: Wed Mar 23, 2011 5:31 pm

Re: Guardian Force De-Lag Patch

Post by SuperDeadite »

WARNING! Carefully examine your cart pcb before installing this patch.
My cart had one pin that was never soldered, and possibly caused a short which made
my mobo explode.

See Here: viewtopic.php?p=1368254#p1368254
Post Reply