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 16, 2013
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: 139
Joined: Dec 19, 2012
Motto: /\OX[]
PlayStation Model: SCPH-5502
Location: Australia

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: Feb 05, 2013

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
Verified
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 248
Joined: Mar 07, 2013
I am a: Programmer Windows+PS1, GFX Artist
PlayStation Model: Black
Location: Germany

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

DEBRO
Curious PSXDEV User
Curious PSXDEV User
Posts: 17
Joined: Feb 05, 2013

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: 139
Joined: Dec 19, 2012
Motto: /\OX[]
PlayStation Model: SCPH-5502
Location: Australia

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: Mar 27, 2012
Location: Brazil

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
Verified
Admin / PSXDEV
Admin / PSXDEV
Posts: 2670
Joined: Dec 31, 2012
PlayStation Model: H2000/5502
Discord: Shadow^PSXDEV

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), PSIO Switch Board, DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.

PlayStation Development PC: Windows 98 SE, Pentium 3 at 400MHz, 128MB SDRAM, DTL-H2000, DTL-H2010, DTL-H201A, DTL-S2020 (with 4GB SCSI-2 HDD), 21" Sony G420, CD-R burner, 3.25" and 5.25" Floppy Diskette Drives, ZIP 100 Diskette Drive and an IBM Model M keyboard.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests