How to wait a second?

General Programming help in C, C++ or ASM, Compiling / Debugging, and R3000A Central Processing Unit (CPU) information
Post Reply
User avatar
earlyProg
Interested PSXDEV User
Interested PSXDEV User
Posts: 6
Joined: May 16th, 2013, 3:44 am
Skype: alexcorporation3
Location: Italy

How to wait a second?

Post by earlyProg » June 6th, 2013, 1:44 am

PLEASE HELP ME! :crying
Yes, I am.

User avatar
t0rxe
C Programming Expert
C Programming Expert
Posts: 140
Joined: December 19th, 2012, 8:08 pm
IRC: t0rxe
Motto: /\OX[]
PlayStation Model: SCPH-5502
Location: Australia

Re: How to wait a second?

Post by t0rxe » June 6th, 2013, 1:23 pm

You can make a for loop and use VSync(0) for example to make a timer.
VSync(x) is defined in the Psy-Q manual, where 'x' is the systems definitive.

You will need to do some basic math to figure out the amount of time needed to make a second.
In my example below, 10 is just a number I randomly chose, and so may be to high or to little...

EG:

Code: Select all

void Timer()
{
  int i;

  for (i=0; i<10; i++) VSync(0);
}
"Nostalgia isn't a big enough word to describe the PlayStation from my eyes"

DEBRO
Curious PSXDEV User
Curious PSXDEV User
Posts: 17
Joined: February 5th, 2013, 3:08 am

Re: How to wait a second?

Post by DEBRO » June 6th, 2013, 10:49 pm

Hi there,

The Kernel section of the Run-Time Library 4.4 document makes reference to a System Clock. Read the section on Root Counter Control. This may help you.

You could also write a VSyncCallback that increments a frame counter every VSync. You could then check the value to see if 60 frames (for NTSC) or 50 frames (for PAL) have passed.

The question doesn't give a lot of information about what you are really trying to do so I'm not sure which technique would work better for you. I haven't done either on the PlayStation so I can't supply a sample. I have done the latter for other platforms (i.e. Atari2600, Atari5200, Atari7800, Atari 8-bits computers) and it works.

For instance, if you were trying to check when a second has passed after pressing a button, you could set a counter to 0 when the button is pressed (also keeping track of when the button is released so the counter gets reset) then for each VsyncCallback increment the counter. In your loop or program you would then check if the appropriate time has elapsed (i.e. (counter % 60) == 0 for NTSC or (counter % 50) == 0 for PAL).

I hope this helps in solving your question. Please share your final solution with the forum. I'm sure it will help others as well.

User avatar
inc^lightforce
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 145
Joined: March 7th, 2013, 7:34 am
I am a: Programmer, Artist, Gamer
PlayStation Model: Black
Skype: a5recordings
Location: Germany
Contact:

Re: How to wait a second?

Post by inc^lightforce » June 7th, 2013, 1:50 am

AGREE :) thats more than enough .
use this code
t0rxe wrote:

Code: Select all

void Timer()
{
  int i;

  for (i=0; i<10; i++) VSync(0); 
}
if you wanna wait more than 1 sec. you can change the value "i<10;" to 60 or higher
my coding page/ forum at:
http://aftermath-creations.net/

DEBRO
Curious PSXDEV User
Curious PSXDEV User
Posts: 17
Joined: February 5th, 2013, 3:08 am

Re: How to wait a second?

Post by DEBRO » June 7th, 2013, 12:16 pm

Hi there,
inc^lightforce wrote:AGREE :) thats more than enough .
use this code
t0rxe wrote:

Code: Select all

void Timer()
{
  int i;

  for (i=0; i<10; i++) VSync(0); 
}
if you wanna wait more than 1 sec. you can change the value "i<10;" to 60 or higher
I'm still learning this system and would like to understand how this code waits a second.

If my understanding of this code is correct it will call VSync 11 times. VSync(0) waits for the next VSync to execute. For NTSC machines this is every 1/60 of a second and for PAL systems every 1/50 of a second. If VSync is called 11 times then wouldn't that be 0.183 seconds for NTSC and 0.22 seconds for PAL? Is that correct?

Also, this assumes no other callbacks are happening during this tight loop. If anything (though not likely) is done while the the loop is executing then isn't it possible for a VSync to happen before the VSync(0) call? This may not matter...I don't know the context of what the original asker is planning. Maybe an exact second pause is not needed. Maybe an approximation is okay.

Again, I'm learning this system and trying to better understand. Thanks.

User avatar
t0rxe
C Programming Expert
C Programming Expert
Posts: 140
Joined: December 19th, 2012, 8:08 pm
IRC: t0rxe
Motto: /\OX[]
PlayStation Model: SCPH-5502
Location: Australia

Re: How to wait a second?

Post by t0rxe » June 7th, 2013, 8:47 pm

You have the right idea :ugeek:
"Nostalgia isn't a big enough word to describe the PlayStation from my eyes"

User avatar
bizarro2011
Serious PSXDEV User
Serious PSXDEV User
Posts: 118
Joined: March 27th, 2012, 9:09 pm
Skype: abernardo2011
Location: Brazil

Re: How to wait a second?

Post by bizarro2011 » June 24th, 2013, 11:10 pm

I thought of this idea. (not tested)
try this.

void Timer(int n)
{
int i;

for (i=0; i<n; i++) VSync(0);
}

with it.

int segundos;

double n = ((double)segundos)/60.;

User avatar
Shadow
Admin / PSXDEV
Admin / PSXDEV
Posts: 2233
Joined: December 31st, 2012, 5:37 pm
PlayStation Model: H2000/5502

Re: How to wait a second?

Post by Shadow » January 4th, 2016, 11:19 pm

Another way is to just do 'VSync(60)'. Depending on your setup video mode and PSX (since VSync will either use 50Hz or 60Hz), this is one way to do a delay.

The most accurate way is to use the free-running root counter and use some math to generate a seconds counter. This is how Bust A Groove does it with their dance system so it's always in sync.
Development Console: SCPH-5502 with 8MB RAM, MM3 Modchip, PAL 60 Colour Modification (for NTSC), DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.

Development Computer: Windows 98, Pentium 3 [400MHz], 128MB SDRAM, DTL-H2000, DTL-H201A, 21" Sony Trinitron CRT, CD-ROM burner, 3.25" and 5.25" Floppy Diskette Drives and a ZIP 100 Diskette Drive.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests