/* First for PSP */

#include <pspkernel.h>
#include <pspdebug.h>

#include <pspdisplay.h>
#include <pspctrl.h>

PSP_MODULE_INFO("Count Mofo!",0,1,1);

#define printf pspDebugScreenPrintf


int exit_callback(int arg1, int arg2, void *common) {
  sceKernelExitGame();
  return 0;
}


int CallbackThread(SceSize args, void *argp) {

  int cbid;

  cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  sceKernelRegisterExitCallback(cbid);

  sceKernelSleepThreadCB();

  return 0;
}

int SetupCallbacks(void) {
  int thid = 0;
  
  thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
  if (thid >= 0) {
    sceKernelStartThread(thid, 0, 0);
  }
  return thid;
}


void l2() {
  int i = 0;
  int c = 0;
  SceCtrlData pad;

  printf("Press 'X' to start t3h timer\n");

  while(1) {
    sceCtrlReadBufferPositive(&pad, 1);
    if (pad.Buttons & PSP_CTRL_CROSS) {
      break;
    }
  }

  pspDebugScreenClear();

  printf("Press 'O' to stop t3h timer\n");

  while(1) {
    sceCtrlReadBufferPositive(&pad, 1);
    if(pad.Buttons & PSP_CTRL_CIRCLE) {
      break;
    }
    printf("motherfucker!: %d\n", c);
    c++;

    for(i=0;i<5;i++)
      sceDisplayWaitVblankStart();
    

  }
  

  pspDebugScreenClear();
  printf("Finshed, counted to %d!!\n", c);


}

int main () {
  
  pspDebugScreenInit();
  SetupCallbacks();

  
  l2();


  sceKernelSleepThread();

  return 0;
  
}

