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 

Threads / Kernel mode error

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



Joined: 10 May 2006
Posts: 376

PostPosted: Mon Aug 28, 2006 3:56 pm    Post subject: Threads / Kernel mode error Reply with quote

Hi.

I'm trying to get WiFi code working in my game. Before this the game was running pure usermode and worked perfectly.

Now I'm trying to load the net modules, the game is having errors.

The LoadMainMenu() function is being run correctly, except that if I try and create a new player, or load a previously saved player, the PSP crashes - it's as though file io has stopped working.

Thanks in advance for looking at the code.

Code:
// INCLUDES
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <stdio.h>
#include <pspsdk.h>
#include "graphics.h"
#include <psppower.h>
#include "mainmenu.h"
#include "functions.h"
#include "sound.h"
#include "wifi.h"

// DEFINES
#define RGB(r,g,b) (((r) << 16) | ((g) << 8) | (b))

// GLOBAL VARIABLES
char BornOn[255]; //ADOPTED DATE & TIME
int GreenBarLength[5]; //ARRAY FOR LENGTH OF GREEN BARS
int PlayerFileLines[10]; //NUMBER OF LINES IN THE SAVE FILE
char PlayerName[8]; //PLAYER NAME
char GameSavesName[100][255]; //GAME SAVES NAMES
char TrackName[100][255]; //TRACK NAMES FOR DANCE OFF
Color WhiteColor = RGB(255, 255, 255); //COLOR WHITE
SceCtrlData pad, lastpad; //CONTROLS & OLD CONTROLS
int AddToEnergy = 0; //MODIFIER FOR ENERGY
int AddToHunger = 0; //MODIFIER FOR HUNGER
int AddToFun = 0; //MODIFIER FOR FUN
int AddToHygiene = 0; //MODIFIER FOR HYGIENE
Image* LetterImg[27]; //IMAGES FOR LETTERS
Image* PetIdle; //IMAGE FOR PET NOT BLINKING
Image* PetBlink; //IMAGE FOR PET BLINKING
SceKernelUtilsMt19937Context ctx; //MERSENNE TWISTER FOR RANDOM NUMBERS
int NumPerfectScores[3]; //HOLDER FOR NUMBER OF PERFECT SCORES
int GameTime = 0; //GAME TIME (3600 TO A MINUTE)

// MODULE INFO
PSP_MODULE_INFO("AniMate", 0x1000, 1, 1);
PSP_MAIN_THREAD_ATTR(0);
PSP_MAIN_THREAD_STACK_SIZE_KB(32); /* smaller stack for kernel thread */

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

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
          int cbid;

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

          sceKernelSleepThreadCB();

          return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
          int thid = 0;

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

          return thid;
}

// START MAIN
int user_main(SceSize argc, void* argv)
{
          SetupCallbacks();
          scePowerSetClockFrequency(333, 333, 166); //SOFTWARE DECODING OF MUSIC SLOWS DOWN CPU, UGH!
          initGraphics();

          //pspAudioInit();
          WAV_Init();

          int i;  //FOR 'FOR' LOOP
          char buffer[255]; //CHARACTER BUFFER

          //MAIN MENU--NEW GAME LETTERS
          extern Image* LetterImg[27];
          for (i = 0; i < 27; i++) {
          sprintf(buffer, "./Images/Letters/%i.png", i);
          LetterImg[i] = Load_Image(buffer);
          }
         
          //PET IDLE
          extern Image* PetIdle;
          PetIdle = Load_Image("./Images/Pet/PetIdle.png");

          //PET BLINK
          extern Image* PetBlink;
          PetBlink = Load_Image("./Images/Pet/PetBlink.png");

          while(1)
                  {
                  LoadMainMenu();
                  }
                 
          //CLEAN UP ON EXIT
          for (i = 0; i < 27; i++)
          freeImage(LetterImg[i]);

          freeImage(PetIdle);
          freeImage(PetBlink);

   return 0;
}

int main(void)
{
   pspDebugScreenInit();
   int err = pspSdkLoadInetModules();

   if (err != 0) {
      pspDebugScreenPrintf("pspSdkLoadInetModules failed with %x\n", err);
      sceKernelDelayThread(5*1000000); // 5 sec to read error
      //return 1;
   }

   // create user thread, tweek stack size here if necessary
   SceUID thid = sceKernelCreateThread("User Mode Thread", user_main,
      0x11, // default priority
      256 * 1024, // stack size (256KB is regular default)
      PSP_THREAD_ATTR_USER, NULL);

   // start user thread, then wait for it to do everything else
   sceKernelStartThread(thid, 0, NULL);
   sceKernelWaitThreadEnd(thid, NULL);

   sceKernelExitGame();

   return 0;

}
Back to top
View user's profile Send private message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Mon Aug 28, 2006 7:12 pm    Post subject: Reply with quote

have you tried this for your initial kernel thread:
Code:
    sceKernelStartThread(thid, 0, NULL);
    sceKernelExitDeleteThread(0);
    return 0;

instead of:
Code:
   sceKernelStartThread(thid, 0, NULL);
   sceKernelWaitThreadEnd(thid, NULL);

   sceKernelExitGame();

   return 0;
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Tue Aug 29, 2006 5:14 pm    Post subject: Reply with quote

Thanks for the reply mbf, unfortunately I'm still getting the same errors.

I've narrowed the error down somewhat.

I'm loading all my external files (images/sounds etc) using relative paths ie. "./Sounds/mysound.wav".

The sounds load fine, as do the images, it's when I'm trying to do something like create a file or list the files in a directory when I'm getting errors.

It's like my relative path is getting messed up somehow.

Anybody have any ideas please?

PS. When I move all my code from 'user_main' into 'main', it works flawlessly.
Back to top
View user's profile Send private message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Tue Aug 29, 2006 7:02 pm    Post subject: Reply with quote

Mhh... weird. You can check the current directory with the cwd() function, from main() and from user_main(). I don't see why it would be different though.

Also check your heap size (shouldn't be an issue either).

What kind of errors do you get?
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Wed Aug 30, 2006 1:00 am    Post subject: Reply with quote

Insert_witty_name wrote:
The sounds load fine, as do the images, it's when I'm trying to do something like create a file or list the files in a directory when I'm getting errors.

It's like my relative path is getting messed up somehow.
Are you using sceIo* or POSIX calls? The sceIo functions are broken in many ways regarding relative paths. We worked around many known issues when we implemented the standard calls in newlib.
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Wed Aug 30, 2006 2:03 am    Post subject: Reply with quote

jimparis wrote:
Are you using sceIo* or POSIX calls? The sceIo functions are broken in many ways regarding relative paths. We worked around many known issues when we implemented the standard calls in newlib.


I use both in different functions, neither works.

mbf: Basically my game crashes when trying to list the files in a directory, or create a new file.

I briefly messed with getcwd() and chdir() this morning to try and isolate where the error is coming from.

I'll mess about with it again when I come back from work.

Thank you both for you assistance.
Back to top
View user's profile Send private message
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Wed Aug 30, 2006 3:55 am    Post subject: Reply with quote

Insert_witty_name wrote:
Basically my game crashes when trying to list the files in a directory, or create a new file.


If you're using the native sceIo* function for reading directories, make sure that you have read the other threads here about it. You can expect odd problems if you pass the necessary structure on the stack, or don't properly initialise it to 0s.
_________________
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Wed Aug 30, 2006 4:29 am    Post subject: Reply with quote

I'll convert all my sceIo* calls to POSIX and see how it goes.

This is my GetGameSaves() function, can't see anything wrong with it:

Code:
int GetGameSaves()
{

int dfd;
SceIoDirent GameSavesDir;
memset(&GameSavesDir, 0, sizeof(GameSavesDir));
extern char GameSavesName[100][255];
int GameSaveCounter = 0;
int MaxGameSaves = 0;

dfd = sceIoDopen("./GameSaves/");

while(sceIoDread(dfd, &GameSavesDir) > 0)
{
sprintf(GameSavesName[GameSaveCounter],"%s",GameSavesDir.d_name);
MaxGameSaves = GameSaveCounter;
GameSaveCounter++;
}
sceIoDclose(dfd);

return MaxGameSaves;
}


[EDIT] Actually on a second glance through my code, it seems that the POSIX stuff is working (image/sound etc loading) it's just the sceIO calls that aren't. I'll convert them over to POSIX and report back.

Thanks for the point in the right direction.
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Wed Aug 30, 2006 9:58 pm    Post subject: Reply with quote

Ok, I've changed all my file IO to newlib calls and all is well on the file io front.

But now for some reason my music/sound is choppy in places when it wasn't before (in user mode)

Any suggestions as to what could be causing it?
Back to top
View user's profile Send private message
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Thu Aug 31, 2006 5:04 am    Post subject: Reply with quote

Checked the relative thread priorities? Don't forget that the scheduler on the PSP is also not as pre-emptive as you would like...
_________________
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Thu Aug 31, 2006 5:26 am    Post subject: Reply with quote

The only threads I'm using are the ones that are in my original post.

I'm going to try and optimise my code some more and see if that solves the issue (the sound only goes choppy when the CPU is under load).

Thanks for the help on this, It's all been pretty smooth until this whole threading fiasco.
Back to top
View user's profile Send private message
jockyw2001



Joined: 29 Sep 2005
Posts: 339

PostPosted: Thu Aug 31, 2006 5:58 am    Post subject: Reply with quote

try this:
Code:

// create user thread, tweek stack size here if necessary
   SceUID thid = sceKernelCreateThread("User Mode Thread", user_main,
      8, // default priority
      256 * 1024, // stack size (256KB is regular default)
      PSP_THREAD_ATTR_USER, NULL);
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Sat Sep 02, 2006 3:57 am    Post subject: Reply with quote

Thanks for that jockyw2001 - using that, the other ideas in this thread and optimising my code has removed the stuttering in my sounds.

Thanks to all.
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