forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sceSystimer usage (hardware stopwatch)

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
adrahil



Joined: 16 Mar 2006
Posts: 277

PostPosted: Mon Jan 22, 2007 9:35 am    Post subject: sceSystimer usage (hardware stopwatch) Reply with quote

Here are header+stubs alongside with a stupid example of how to use it. The timing is a little strange, as it doesn't seem to be exactly microseconds. Anyway, here it is:

pspsystimer.h:
Code:
/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * pspsystimer.h - Prototypes for the sceSystimer library.
 *
 * Copyright (c) 2007 Iaroslav Gaponenko <adrahil@gmail.com>
 *
 * $Id: pspctrl_kernel.h 2015 2006-10-05 20:23:07Z tyranid $
 */

#ifndef __SYSTIMER_H__
#define __SYSTIMER_H__

#ifdef __cplusplus
extern "C" {
#endif

typedef int SceSysTimerId;

/**
 * Allocate a new SysTimer timer instance.
 *
 * @return SysTimerId on success, < 0 on error
 */
SceSysTimerId sceSTimerAlloc(void);

/**
 * Free an instance of a SysTimer timer.
 *
 * @param timer - The timer id.
 *
 */
void sceSTimerFree(SceSysTimerId timer);

/**
 * Start the SysTimer timer count.
 *
 * @param timer - The timer id.
 *
 */
void sceSTimerStartCount(SceSysTimerId timer);

/**
 * Stop the current SysTimer timer count.
 *
 * @param timer - The timer id.
 *
 */
void sceSTimerStopCount(SceSysTimerId timer);

/**
 * Reset the current SysTimer timer count.
 *
 * @param timer - The timer id.
 *
 */
void sceSTimerResetCount(SceSysTimerId timer);

/**
 * Get the current SysTimer timer count.
 *
 * @param timer - The timer id.
 * @param count - The pointer to an integer into which the count will be written.
 *
 */
void sceSTimerGetCount(SceSysTimerId timer, int* count);

/**
 * Setup a SysTimer handler
 *
 * @param timer - The timer id.
 * @param cycle - The timer cycle in microseconds (???). Maximum: 4194303 which represents ~1/10 seconds.
 * @param handler - The handler function. Has to return -1.
 * @param unk1 - Unknown. Pass 0.
 *
 */
void sceSTimerSetHandler(SceSysTimerId timer, int cycle, int (*handler)(void), int unk1);

/* Unknown functions. */
//probably something to set the cycle of an active timer.
void SysTimerForKernel_53231A15(SceSysTimerId timer, int unk1);
//more complex. computes some ratio (unk2/unk1) and saves both parameters into the hardware registers. Might be some sort of scaling factor?
void SysTimerForKernel_B53534B4(SceSysTimerId timer, int unk1, int unk2);



#ifdef __cplusplus
}
#endif

#endif


SysTimerForKernel.S:
Code:
        .set noreorder
 
#include "pspstub.s"
 
        STUB_START      "SysTimerForKernel",0x00010011,0x00090005
        STUB_FUNC       0x228EDAE4,sceSTimerGetCount
        STUB_FUNC       0x4A01F9D3,sceSTimerStopCount
        STUB_FUNC       0x53231A15,SysTimerForKernel_53231A15
        STUB_FUNC       0x54BB5DB4,sceSTimerResetCount
        STUB_FUNC       0x975D8E84,sceSTimerSetHandler
        STUB_FUNC       0xA95143E2,sceSTimerStartCount
        STUB_FUNC       0xB53534B4,SysTimerForKernel_B53534B4
        STUB_FUNC       0xC105CF38,sceSTimerFree
        STUB_FUNC       0xC99073E3,sceSTimerAlloc
        STUB_END


Now, the sample. Use the standard makefile and link to the stub file.

main.c:
Code:
/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * main.c - Simple Systimer example
 *
 * Copyright (c) 2007 Iaroslav Gaponenko <adrahil@gmail.com>
 *
 * $Id: main.c 1888 2006-05-01 08:47:04Z tyranid $
 * $HeadURL$
 */
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include "pspsystimer.h"

#define printf pspDebugScreenPrintf

/* Define the module info section */
PSP_MODULE_INFO("systimer", 0x1000, 1, 1);

/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU);

SceSysTimerId timer;
int tic = 0;

int my_tick_handler(void){
  static int div;
  div++;
  //arrange to get more or less seconds...
  if(div > 12){
    div = 0;
    if(tic){
      pspDebugScreenPrintf("Tac!\n");
    }else{
      pspDebugScreenPrintf("Tic!\n");
    }
    tic = ~tic;
  }
  return -1;
}

int main(int argc, char *argv[]) {

  pspDebugScreenInit();
  pspDebugScreenPrintf("SysTimer sample by Adrahil.\n");

  timer = sceSTimerAlloc();
  sceSTimerStartCount(timer);
  sceSTimerSetHandler(timer, 4194303, my_tick_handler, 0);

  sceKernelSleepThreadCB();
  return 0;

}


What else can I say :3 Your PSP is now transformed into a 300$ stopwatch! Enjoy it! (Seriously, the only advantage of this method is that it uses some hardware timer at 0xbc50XXXX...) I shouldn't even have to mention that because of this reason it requires kernel mode :P
Back to top
View user's profile Send private message
harleyg



Joined: 05 Oct 2005
Posts: 123

PostPosted: Mon Jan 22, 2007 6:10 pm    Post subject: Reply with quote

Nice stuff adrahil. :)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group