Skip to content

Instantly share code, notes, and snippets.

@Relys
Last active August 29, 2015 14:05
Show Gist options
  • Save Relys/14ae1db245637e130633 to your computer and use it in GitHub Desktop.
Save Relys/14ae1db245637e130633 to your computer and use it in GitHub Desktop.
#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>
#include <ctr/GX.h>
#include <ctr/HID.h>
#include <ctr/svc.h>
#include "costable.h"
u8* gspHeap;
u32* gxCmdBuf;
u8 currentBuffer;
u8* topLeftFramebuffers[2];
u8* subFramebuffers[2];
Handle gspEvent, gspSharedMemHandle;
void gfxInit()
{
gspInit();
GSPGPU_AcquireRight(NULL, 0x0);
GSPGPU_SetLcdForceBlack(NULL, 0x0);
//setup our gsp shared mem section
u8 threadID;
svc_createEvent(&gspEvent, 0x0);
GSPGPU_RegisterInterruptRelayQueue(NULL, gspEvent, 0x1, &gspSharedMemHandle, &threadID);
svc_mapMemoryBlock(gspSharedMemHandle, 0x10002000, 0x3, 0x10000000);
//map GSP heap
svc_controlMemory((u32*)&gspHeap, 0x0, 0x0, 0x02000000, 0x10003, 0x3);
//setup framebuffers on the GSP heap
topLeftFramebuffers[0]=(u8*)gspHeap+0xC000000;
topLeftFramebuffers[1]=topLeftFramebuffers[0]+0x46500;
subFramebuffers[0]=topLeftFramebuffers[1]+0x46500;
subFramebuffers[1]=subFramebuffers[0]+0x38400;
GSPGPU_WriteHWRegs(NULL, 0x400468, (u32*)&topLeftFramebuffers, 8);
GSPGPU_WriteHWRegs(NULL, 0x400568, (u32*)&subFramebuffers, 8);
topLeftFramebuffers[0]-=0xC000000;
topLeftFramebuffers[1]-=0xC000000;
subFramebuffers[0]-=0xC000000;
subFramebuffers[1]-=0xC000000;
//wait until we can write stuff to it
svc_waitSynchronization1(gspEvent, 0x55bcb0);
//GSP shared mem : 0x2779F000
gxCmdBuf=(u32*)(0x10002000+0x800+threadID*0x200);
currentBuffer=0;
}
void gfxExit()
{
//free GSP heap
svc_controlMemory((u32*)&gspHeap, (u32)gspHeap, 0x0, 0x02000000, MEMOP_FREE, 0x0);
//unmap GSP shared mem
svc_unmapMemoryBlock(gspSharedMemHandle, 0x10002000);
GSPGPU_UnregisterInterruptRelayQueue(NULL);
svc_closeHandle(gspSharedMemHandle);
svc_closeHandle(gspEvent);
GSPGPU_ReleaseRight(NULL);
gspExit();
}
u8* gfxGetFramebuffer(bool top, u16* width, u16* height)
{
if(width)*width=240;
if(top)
{
if(height)*height=400;
return topLeftFramebuffers[currentBuffer^1];
}else{
if(height)*height=320;
return subFramebuffers[currentBuffer^1];
}
}
void gfxFlushBuffers()
{
GSPGPU_FlushDataCache(NULL, gfxGetFramebuffer(true, NULL, NULL), 0x46500);
GSPGPU_FlushDataCache(NULL, gfxGetFramebuffer(false, NULL, NULL), 0x38400);
}
void gfxSwapBuffers()
{
u32 regData;
GSPGPU_ReadHWRegs(NULL, 0x400478, (u32*)&regData, 4);
regData^=1;
currentBuffer=regData&1;
GSPGPU_WriteHWRegs(NULL, 0x400478, (u32*)&regData, 4);
GSPGPU_WriteHWRegs(NULL, 0x400578, (u32*)&regData, 4);
}
void copyBuffer()
{
//copy topleft FB
u8 copiedBuffer=currentBuffer^1;
u8* bufAdr=&gspHeap[0x46500*copiedBuffer];
GSPGPU_FlushDataCache(NULL, bufAdr, 0x46500);
GX_RequestDma(gxCmdBuf, (u32*)bufAdr, (u32*)topLeftFramebuffers[copiedBuffer], 0x46500);
}
s32 pcCos(u16 v)
{
return costable[v&0x1FF];
}
u32 cnt;
void renderEffect()
{
u8* bufAdr=&gspHeap[0x46500*currentBuffer];
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;
}
}
cnt++;
}
int main()
{
initSrv();
aptInit(APPID_APPLICATION);
gfxInit();
hidInit(NULL);
aptSetupEventHandler();
APP_STATUS status;
while((status=aptGetStatus())!=APP_EXITING)
{
if(status==APP_RUNNING)
{
u32 PAD=hidSharedMem[7];
u32 regData=PAD|0x01000000;
GSPGPU_WriteHWRegs(NULL, 0x202A04, (u32*)&regData, 4);
renderEffect();
gfxSwapBuffers();
copyBuffer();
}
else if(status == APP_SUSPENDING)
{
aptReturnToMenu();
}
else if(status == APP_SLEEPMODE)
{
aptWaitStatusEvent();
}
svc_sleepThread(16666666);
}
hidExit();
gfxExit();
aptExit();
svc_exitProcess();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment