libctru/arm11u/source/main.c

176 lines
3.4 KiB
C
Raw Normal View History

2014-01-18 23:18:03 +01:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctr/types.h>
#include <ctr/srv.h>
#include <ctr/APT.h>
#include <ctr/GSP.h>
2014-01-19 21:49:01 +01:00
#include <ctr/HID.h>
2014-01-18 23:18:03 +01:00
#include <ctr/svc.h>
2014-01-19 21:49:01 +01:00
#include "costable.h"
2014-01-18 23:18:03 +01:00
2014-01-19 21:49:01 +01:00
u8* gspHeap;
u32* gxCmdBuf;
u8 currentBuffer;
u8* topLeftFramebuffers[2];
Handle gspEvent, gspSharedMemHandle;
2014-01-19 21:49:01 +01:00
void gspGpuInit()
{
gspInit();
2014-01-18 23:18:03 +01:00
GSPGPU_AcquireRight(NULL, 0x0);
GSPGPU_SetLcdForceBlack(NULL, 0x0);
2014-01-18 23:18:03 +01:00
//set subscreen to blue
u32 regData=0x01FF0000;
GSPGPU_WriteHWRegs(NULL, 0x202A04, (u8*)&regData, 4);
2014-01-18 23:18:03 +01:00
//grab main left screen framebuffer addresses
GSPGPU_ReadHWRegs(NULL, 0x400468, (u8*)&topLeftFramebuffers, 8);
2014-01-18 23:18:03 +01:00
//convert PA to VA (assuming FB in VRAM)
topLeftFramebuffers[0]+=0x7000000;
topLeftFramebuffers[1]+=0x7000000;
//setup our gsp shared mem section
u8 threadID;
svc_createEvent(&gspEvent, 0x0);
GSPGPU_RegisterInterruptRelayQueue(NULL, gspEvent, 0x1, &gspSharedMemHandle, &threadID);
2014-01-18 23:18:03 +01:00
svc_mapMemoryBlock(gspSharedMemHandle, 0x10002000, 0x3, 0x10000000);
//map GSP heap
svc_controlMemory((u32*)&gspHeap, 0x0, 0x0, 0x2000000, 0x10003, 0x3);
//wait until we can write stuff to it
svc_waitSynchronization1(gspEvent, 0x55bcb0);
2014-01-19 21:49:01 +01:00
//GSP shared mem : 0x2779F000
gxCmdBuf=(u32*)(0x10002000+0x800+threadID*0x200);
2014-01-18 23:18:03 +01:00
2014-01-19 21:49:01 +01:00
currentBuffer=0;
}
void gspGpuExit()
{
GSPGPU_UnregisterInterruptRelayQueue(NULL);
//unmap GSP shared mem
svc_unmapMemoryBlock(gspSharedMemHandle, 0x10002000);
svc_closeHandle(gspSharedMemHandle);
svc_closeHandle(gspEvent);
gspExit();
//free GSP heap
svc_controlMemory((u32*)&gspHeap, (u32)gspHeap, 0x0, 0x2000000, MEMOP_FREE, 0x0);
}
2014-01-19 21:49:01 +01:00
void swapBuffers()
{
u32 regData;
GSPGPU_ReadHWRegs(NULL, 0x400478, (u8*)&regData, 4);
2014-01-19 21:49:01 +01:00
regData^=1;
currentBuffer=regData&1;
GSPGPU_WriteHWRegs(NULL, 0x400478, (u8*)&regData, 4);
2014-01-19 21:49:01 +01:00
}
void copyBuffer()
{
//copy topleft FB
u8 copiedBuffer=currentBuffer^1;
u8* bufAdr=&gspHeap[0x46500*copiedBuffer];
GSPGPU_FlushDataCache(NULL, bufAdr, 0x46500);
2014-01-18 23:18:03 +01:00
//GX RequestDma
2014-01-19 21:49:01 +01:00
u32 gxCommand[0x8];
2014-01-18 23:18:03 +01:00
gxCommand[0]=0x00; //CommandID
2014-01-19 21:49:01 +01:00
gxCommand[1]=(u32)bufAdr; //source address
gxCommand[2]=(u32)topLeftFramebuffers[copiedBuffer]; //destination address
gxCommand[3]=0x46500; //size
2014-01-18 23:18:03 +01:00
gxCommand[4]=gxCommand[5]=gxCommand[6]=gxCommand[7]=0x0;
GSPGPU_submitGxCommand(gxCmdBuf, gxCommand, NULL);
2014-01-19 21:49:01 +01:00
}
2014-01-18 23:18:03 +01:00
2014-01-19 21:49:01 +01:00
s32 pcCos(u16 v)
{
return costable[v&0x1FF];
}
2014-01-18 23:18:03 +01:00
u32 cnt;
2014-01-19 21:49:01 +01:00
void renderEffect()
{
u8* bufAdr=&gspHeap[0x46500*currentBuffer];
2014-01-18 23:18:03 +01:00
2014-01-19 21:49:01 +01:00
int i, j;
for(i=1;i<400;i++)
{
for(j=1;j<240;j++)
{
u32 v=(j+i*240)*3;
bufAdr[v]=(pcCos(i+cnt)+4096)/32;
bufAdr[v+1]=(pcCos(j-256+cnt)+4096)/64;
bufAdr[v+2]=(pcCos(i+128-cnt)+4096)/32;
2014-01-19 21:49:01 +01:00
}
}
cnt++;
}
Handle hidHandle;
Handle hidMemHandle;
void hidInit()
{
srv_getServiceHandle(NULL, &hidHandle, "hid:USER");
HIDUSER_GetInfo(hidHandle, &hidMemHandle);
svc_mapMemoryBlock(hidMemHandle, 0x10000000, 0x1, 0x10000000);
HIDUSER_Init(hidHandle);
}
void hidExit()
{
svc_unmapMemoryBlock(hidMemHandle, 0x10000000);
svc_closeHandle(hidMemHandle);
svc_closeHandle(hidHandle);
2014-01-19 21:49:01 +01:00
}
2014-01-18 23:18:03 +01:00
2014-01-19 21:49:01 +01:00
int main()
{
initSrv();
2014-01-19 21:49:01 +01:00
aptInit(APPID_APPLICATION);
2014-01-19 21:49:01 +01:00
gspGpuInit();
hidInit();
2014-01-19 21:49:01 +01:00
aptSetupEventHandler();
APP_STATUS status;
while((status=aptGetStatus())!=APP_EXITING)
2014-01-19 21:49:01 +01:00
{
if(status==APP_RUNNING)
{
u32 PAD=((u32*)0x10000000)[7];
u32 regData=PAD|0x01000000;
GSPGPU_WriteHWRegs(NULL, 0x202A04, (u8*)&regData, 4);
renderEffect();
swapBuffers();
copyBuffer();
}
svc_sleepThread(16666666);
2014-01-19 21:49:01 +01:00
}
2014-01-18 23:18:03 +01:00
hidExit();
gspGpuExit();
aptExit();
2014-01-19 21:49:01 +01:00
svc_exitProcess();
2014-01-18 23:18:03 +01:00
return 0;
}