Media Engine in USER mode.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
And
Posts: 4
Joined: Thu Aug 17, 2006 9:54 pm

Media Engine in USER mode.

Post by And »

Hi everyone... just joined the forums. For the last week I've started to programme the psp. I've been messing around with the media engine and have got it to work in user mode... Can anybody tell me if there is a better way to do this? Or is this the only way?

Next on my list is to get the main core interrupt picking up the media engine interrupt requests.

Enjoy :)

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <pspdisplay.h>
#include <stdlib.h>
#include <string.h>


PSP_MODULE_INFO&#40;"meccode", 0x1000, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;PSP_THREAD_ATTR_USER&#41;;

#define printf	pspDebugScreenPrintf

void me_stub&#40;void&#41;;
void me_stub_end&#40;void&#41;;

void me_startproc&#40;u32 func, u32 param&#41;
&#123;
      memcpy&#40;&#40;void *&#41;0xbfc00040, me_stub, &#40;int&#41;&#40;me_stub_end - me_stub&#41;&#41;;
      _sw&#40;func, 0xbfc00600&#41;;
      _sw&#40;param, 0xbfc00604&#41;;
      sceKernelDcacheWritebackAll&#40;&#41;;
      sceSysregMeResetEnable&#40;&#41;;
      sceSysregMeBusClockEnable&#40;&#41;;
      sceSysregMeResetDisable&#40;&#41;;
&#125;
   
unsigned int g_counter;

void my_function&#40;int param&#41;
&#123;
	volatile unsigned int *pctr = &#40;volatile unsigned int *&#41;&#40;&#40;&#40;int&#41;&g_counter&#41;|0x40000000&#41;;
	while&#40;1&#41;
	&#123;
		int x;
		for &#40;x = 0; x < 1024*1024; x ++&#41;;
                &#40;*pctr&#41; += param;
	&#125;
&#125;

int start_me&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
	me_startproc&#40;&#40;u32&#41;my_function,0x10000&#41;;	
	sceKernelExitDeleteThread&#40;0&#41;;
	return 0;
&#125;

__attribute__&#40;&#40;constructor&#41;&#41;
void start_up&#40;void&#41;
&#123;
	void &#40;*_start_me_func&#41;&#40;SceSize args, void *argp&#41; = &#40;void *&#41;start_me;
	_start_me_func = &#40;void *&#41; &#40;&#40;u32&#41; _start_me_func | 0x80000000&#41;;
	SceUID thid;
	thid = sceKernelCreateThread&#40;"Start ME", &#40;void *&#41; _start_me_func, 1, 512, 0, 0&#41;;
	sceKernelStartThread&#40;thid, 0, 0&#41;;
&#125;

int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
    sceKernelExitGame&#40;&#41;;

	return 0;
&#125;

int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
    int cbid;
    cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
    sceKernelRegisterExitCallback&#40;cbid&#41;;
    sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

int SetupCallbacks&#40;void&#41;
&#123;
    int thid = 0;
    thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
    if &#40;thid >= 0&#41;
	sceKernelStartThread&#40;thid, 0, 0&#41;;
    return thid;
&#125;

int main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
	pspDebugScreenInit&#40;&#41;;
    SetupCallbacks&#40;&#41;;
	while&#40;1&#41;
	&#123;
		unsigned int *pctr = &#40;unsigned int *&#41;&#40;&#40;&#40;int&#41;&g_counter&#41;|0x40000000&#41;; // uncached read pointer

		pspDebugScreenSetXY&#40;0, 0&#41;;
		pspDebugScreenPrintf&#40;"ME Basic Example, press Home to exit\n"&#41;;
		pspDebugScreenPrintf&#40;"ME Counter&#58; %08x\n", *pctr&#41;;
		sceDisplayWaitVblankStart&#40;&#41;;
	&#125;
	return 0;
&#125;
Also you need to complile the mestub.s file with the above code.
siberianstar
Posts: 70
Joined: Thu Jun 22, 2006 9:24 pm

Post by siberianstar »

The media engine cannot be started in USERMODE, what your code does is to start the function start_up in kernel mode, and the main thread in user mode.

In the start_up function (that is running in kernel mode) the media engine is started.

I've used this trick a long time ago to have the 'home' button working, nothing of special.

Cya
And
Posts: 4
Joined: Thu Aug 17, 2006 9:54 pm

Post by And »

siberianstar wrote:The media engine cannot be started in USERMODE, what your code does is to start the function start_up in kernel mode, and the main thread in user mode.

In the start_up function (that is running in kernel mode) the media engine is started.

I've used this trick a long time ago to have the 'home' button working, nothing of special.

Cya
I know what the code does. I didn't say it was special. It just switches into user mode. The reason why I posted it was because all the examples I found of media engine code did not tell you how to switch into user mode. Now you state you did this a long time ago - wouldn't it be nice if you updated the examples in the SDK to show this. The whole point of homebrew is sharing...
Post Reply