News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

SPEED-UP patch FOR Pac-Man emulator for CPC by SyX & TotO

Started by 40Crisis, 23:16, 03 June 14

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

40Crisis

When this Pacman emulator was released on the 1st of April 2012,
Many people thought it's was an april fool.

The unbelievable was real !

Some people were complaining that it's was a little bit slow and to address
this problem, they suggest to use the modified rom with hack speed.

Two months after, Syx released the source code which is a fantastic piece of software.

After seeing the Sinclair ZX Spectrum +2A emulator by Simon Owen, I was a little suprised by
the speed difference between the 2 versions.
I had the feeling that the speed could be better on the CPC.

So I began to analyse the code behaviour and timings of the CPC version.
Something that intrigates me was the fact that in game 37% of the
CPU time was wasted in the main loop to wait for the task list to complete.

Here's one profile example by address location:

$238d = 17.05 %
$2390 = 6.82 % 
$2391 = 3.41 % 
$2392 = 10.23 %

After numerous checks and measuring timings of what was done in the
interrupt routine ( graphics display, sound rendering, input controls),
I realised that this was done in less than 1/50th of second and the problem was
not in the rendering speed.

It takes me time to understand that the problem was the 300 Hz frequency of interrupts
compared to 50/60Hz frequency of the arcade game. The game rendering timing needed an average
of about three 300 Hz interrupts to be completed.

The consequence was that interrupts were often not triggered because interrupts were disable
inside interrupt routine for more than 1/300 s.
This lead to miss some VBL Vblank interrupt.

So I concentrated on the difficult problem: how not to miss VBL Vblank interrupt.
I first tried to tweak interrupt routine but I gave up because it's wasn't the solution.

Finally, I found a very simple solution:
modifing the rom main loop by disabling interrupts,
waiting for vblank, calling original interrupt,disabling interrupts, and then waiting
for task list to complete was much more efficient with little loss in waiting time.

I measured an average of 12.8% CPU time wasted in the waiting part of the main loop.

Now the emulator appears to be much more quicker and smoother.

The only side effect I found is during the display of the main screen were sprites appear for a
brief moment in the middle of the screen, though the modified emulator hasn't been tested much.

In the included archive you will find the pacman. file to put instead of the original one
and the modified source code.

Original source code is at SyX Storage Unit

Enjoy.

SyX

Jejeje, FANTASTIC 40Crisis!!! :D :D :D

I was hacking the code again, because after all the hard work of porting it, we had a bitter aftertaste for the lack of constant speed using the original arcade roms. I was working in two ideas for fixing it, adding a frameskip or fixing the arcade code.

You have made the last and i can only thank you, it's another thing less in the todo list, and one of the things  were burning me more, jejeje, because the state-of-the-art tile and sprite code that i developed for pac-man only were looking at it best in projects of friends (Teodoro, Invasion of the Zombie Monsters V2, 4mhz, ...).

Are you using any special tool for making the profiling or simply counting cycles and making the math?

PS1: I will check if the hack can be used with all the rom hacks too and then i will publish a Pac-Man v1.1 :)

PS2: The fix works in the same pacman hacks than the original (the FAST versions are even funnier now, jejeje).

40Crisis

Quote from: SyX on 00:23, 04 June 14
Jejeje, FANTASTIC 40Crisis!!! :D :D :D

I was hacking the code again, because after all the hard work of porting it, we had a bitter aftertaste for the lack of constant speed using the original arcade roms. I was working in two ideas for fixing it, adding a frameskip or fixing the arcade code.

You have made the last and i can only thank you, it's another thing less in the todo list, and one of the things  were burning me more, jejeje, because the state-of-the-art tile and sprite code that i developed for pac-man only were looking at it best in projects of friends (Teodoro, Invasion of the Zombie Monsters V2, 4mhz, ...).

Are you using any special tool for making the profiling or simply counting cycles and making the math?

I modified the arnold emulator to do  statistics on CPU cycles, track I/O locations in memory, track read/write to memory/screen memory,etc.. The problem is that my code is consuming a lot of CPU and need to be optimised.
So the emulator doesn't run at full speed. Another problem is that I'm using the windows version and even without profiling code, directx sound rendering is jerky and comes late.


PS1: I will check if the hack can be used with all the rom hacks too and then i will publish a Pac-Man v1.1 :)

It will depend if the main loop code is at location $238D in all versions


PS2: The fix works in the same pacman hacks than the original (the FAST versions are even funnier now, jejeje).

TotO


Great patch Guy!  :-\

As we already said into the past, the next step to improve the speed will be to fix the ROM itself...
But, we don't expect that a few patch should allow a 30% speed-up!!!  :o

It run great now (amazing on a real CPC) as the random frame-drop is now fixed.
That allow it to run always at the speed that we got "sometimes". Thank you!

Now, I have to ask to SyX to swap the score and ghosts texts colours in real time.  :D
"You make one mistake in your life and the internet will never let you live it down" (Keith Goodyer)

mr_lou

This is awesome.

I tried it just now on my CPC+. Runs way too fast now of course, because I have to patched ROMs.  :)
Will have to dig up the original ROMs somewhere.

redbox

Quote from: mr_lou on 16:45, 04 June 14
I tried it just now on my CPC+. Runs way too fast now of course, because I have to patched ROMs.  :)

Patched how?

mr_lou

Quote from: redbox on 18:42, 04 June 14
Patched how?

I dunno.
The story goes, that the first version of the Pac-Man emulator was a bit too slow, so someone patched the ROM files somehow, making the game run faster, although not with a stable speed. It would slow down at times and run faster at other times.

It seems that now the optimal solution is to run with the original ROM files and this speed patch for the binary instead.

SyX

Quote from: redbox on 18:42, 04 June 14
Patched how?
The emulator supports original pacman roms  and bootlegs, one of this bootlegs has a hack that makes pacman goes hyperfast compared with the original arcade.

MacDeath


Token

Extremely nice.  8)
Congrats to all of you to make that classic of the classics of arcade to run for real on a CPC.

CraigsBar

Pacman emulation on an Amstrad CPC was good before, Now it is amazing. I am hooked all over again.


The extra speed makes this the must have retro game.


I just LOVE it. Great work to all involved. :D


Craig
IRC:  #Retro4All on Freenode

SyX

Thanks everybody for all the nice words. At the end work and life has not let me finish the v1.1 yesterday (today for me yet), but along the day and after everything is tested, it will be available, there is a few extra fixes aside the amazing patch of 40Crisis. For example, the shaking when pacman goes up/down.

Quote from: 40Crisis on 07:17, 04 June 14
I modified the arnold emulator to do  statistics on CPU cycles, track I/O locations in memory, track read/write to memory/screen memory,etc.. The problem is that my code is consuming a lot of CPU and need to be optimised.
So the emulator doesn't run at full speed. Another problem is that I'm using the windows version and even without profiling code, directx sound rendering is jerky and comes late.

I know that arnoldemu has made a lot of improvements since the last release, and i'm sure that when the new version is available, your developing system will improve.

Ah, 40Crisis i have reworked your patch and now the game runs without interrupts at all (only the initialization interrupt, that is executed once). This has let me to save a few more scanlines because i don't need to save/restore any register in the stack in both old interrupt routines, arcade and cpc.

Everything looks more clear now and for example the old cpc interrupt routine is now called cpc_emulation_code. And the only extra optimizations that i can think now is align a few tables to $xxx0 address for change a few 16 bits INCs to 8 bits, but i don't know if i will do or not, because destroy the clarity a little and doesn't save much more cpu time.

SyX

As i promised, Pac-Man v1.1 (codename Arcade Edition) is here.

I would like to thank 40Crisis again, because his speed-up hack makes the game totally enjoyable now and because during the process of adding it, let me to add a few extras optimizations and fixes (always great for a hacker mind) and making good the reason because i publish the sources of my projects.

PS: Remember that you need to add the rom files to the DSK.

Carnivius

Quote from: SyX on 19:08, 05 June 14

PS: Remember that you need to add the rom files to the DSK.

How do I do this?   I have the arcade rom (from my MAME roms directory) and I can see what I assume to be the four files mentioned in your pacman's readme file but I don't know how to use Winape to put the roms in the dsk.
Favorite CPC games: Count Duckula 3, Oh Mummy Returns, RoboCop Resurrection, Tankbusters Afterlife

SyX

Quote from: Carnivac on 19:33, 05 June 14
How do I do this?   I have the arcade rom (from my MAME roms directory) and I can see what I assume to be the four files mentioned in your pacman's readme file but I don't know how to use Winape to put the roms in the dsk.
Go to menu File -> Drive A -> Edit Disc or Shift + Ctrl + F1, and drag and drop the rom files to the dsk.

Carnivius

ok I did that and instead of the pacman picture flashing reds it now fades to black but then nothing. 
Favorite CPC games: Count Duckula 3, Oh Mummy Returns, RoboCop Resurrection, Tankbusters Afterlife

SyX

Quote from: Carnivac on 19:44, 05 June 14
ok I did that and instead of the pacman picture flashing reds it now fades to black but then nothing. 
Mark the checkbox that put Add/Remove AMSDOS headers.
Must be only that.

Carnivius

Quote from: SyX on 19:59, 05 June 14
Mark the checkbox that put Add/Remove AMSDOS headers.
Must be only that.

That seems to have done it.  Thanks! 
Is all working now.  I loves Pac-Man!  :)
Favorite CPC games: Count Duckula 3, Oh Mummy Returns, RoboCop Resurrection, Tankbusters Afterlife

40Crisis

Quote from: SyX on 19:08, 05 June 14
As i promised, Pac-Man v1.1 (codename Arcade Edition) is here.

I would like to thank 40Crisis again, because his speed-up hack makes the game totally enjoyable now and because during the process of adding it, let me to add a few extras optimizations and fixes (always great for a hacker mind) and making good the reason because i publish the sources of my projects.


PS: Remember that you need to add the rom files to the DSK.

Many thanks for this new release

Just a question about something I have seen in the game since version 1.0

the ghost sprites when overlapping other ghost or pacman seem to be unmasked (see screen snasphots)
Black color appears not to transparent. But you hardly notice it.

Is it a bug or feature ? ;)


SyX

Quote from: 40Crisis on 21:10, 05 June 14
Many thanks for this new release
You are guilty of making this release possible :D

Quote from: 40Crisis on 21:10, 05 June 14Just a question about something I have seen in the game since version 1.0

the ghost sprites when overlapping other ghost or pacman seem to be unmasked (see screen snasphots)
Black color appears not to transparent. But you hardly notice it.

Is it a bug or feature ? ;)
Feature!!! :)

My first implementation printed everything as masked sprites (ghost, pacman and fruits), but the framerate was so low that i changed a lot of things, fruits are printed like tiles, ghosts are non-mask sprites and preshifted and only pacman is the only one masked, because our goal was getting the maximum speed possible.

And as you said is difficult to notice it, because your eyes are mainly centered in pacman... and now is even more difficult to see with the extra and constant speed, jejeje.

Another optimization trick related to those efforts in get enough speed, it's that one of the pre-shifted frames of the ghosts lost one pixel column in the right, but during the game is hard to see too, jejeje.

When you can not get enough cpu speed, then is time of doing illusion tricks, jajaja.

40Crisis

Quote from: SyX on 21:29, 05 June 14
You are guilty of making this release possible :D
It's was a pleasure to bring you a sort of proof of concept.

It's always an interesting challenge to understand
how things work and to improve them.

I learned a lot about pacman internals.

By the way, will you publish the updated source code ?

MacDeath

I guess it may also be easy to speed up PacMania as well...
it managed a decent speed when you take the speed up... and it can get rid of those numerous useless rasters and "1bpp mask+1bpp graphics" into real Mode1. :P
::)

Token

Love the new version so much, so thanks a lot. Smooth, fast, feel like the arcade machine, that's amazing.
:-* :-* :-* :-* :-* :-*

SyX

Quote from: 40Crisis on 21:56, 05 June 14
By the way, will you publish the updated source code ?
You can find them in the downloads section of the SyX Storage Unit (i need to update that place or pass everything to github or bitbucket, jejeje).

Kris

Just test it on CPC+ and it is simply amazing !!! What a big difference between original and last version !
Congrats to all people involved in this nice improvement :)

Powered by SMFPacks Menu Editor Mod