Skip to content

Commit

Permalink
Additional safety check for the regression of game freezing when exit…
Browse files Browse the repository at this point in the history
…ing NTR menu
  • Loading branch information
xzn committed Jan 16, 2024
1 parent 1af54df commit 3b0a6f4
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 51 deletions.
11 changes: 10 additions & 1 deletion include/2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@

#define BOTTOM_HEIGHT 240
#define BOTTOM_WIDTH 320
#define BOTTOM_UI_BPP 2
#define BOTTOM_UI_PITCH (BOTTOM_HEIGHT * BOTTOM_UI_BPP)
#define BOTTOM_UI_FORMAT 3

#define BOTTOM_VID_BPP 3
#define BOTTOM_VID_PITCH (BOTTOM_HEIGHT * BOTTOM_VID_BPP)
#define BOTTOM_VID_FORMAT 1

// #define BOTTOM_FRAME1 (getPhysAddr(bottomFrameBuffer) | 0x80000000)
#define BOTTOM_FRAME1 (bottomRenderingFrameBuffer)
// #define BOTTOM_FRAME2 BOTTOM_FRAME1
#define BOTTOM_FRAME_SIZE (320 * 240 * 3)
#define BOTTOM_FRAME_VID_SIZE (BOTTOM_WIDTH * BOTTOM_VID_PITCH)



Expand All @@ -37,5 +44,7 @@ void square(int x, int y, int xs, int ys);

u32 getPhysAddr(u32 vaddr);
extern u32 bottomFrameBuffer;
extern u32 bottomFrameBufferPitch;
extern u32 bottomRenderingFrameBuffer;
extern u32 bottomFrameIsVid;
#endif
3 changes: 3 additions & 0 deletions include/func.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ typedef struct _GAME_PLUGIN_MENU {
#define REG16(x) (*(volatile u16*)(x))
#define SW(addr, data) *(u32*)(addr) = data

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))



#endif
1 change: 1 addition & 0 deletions include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
void write_byte(u32 address, u8 byte);
void write_word(u32 address, u32 word);
void write_color(u32 address, u8 r, u8 g, u8 b);
void write_color_vid(u32 address, u8 r, u8 g, u8 b);
u32 read_word(u32 address);
char nibble_to_readable(u8 nibble);
int byte_to_string(u8 byte, char* ret, int max_len);
Expand Down
50 changes: 28 additions & 22 deletions source/2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@


void paint_square(int x, int y, u8 r, u8 g, u8 b, int w, int h, int screen){
int x1, y1;
int x1, y1;

for (x1 = x; x1 < x+w; x1++){
for (y1 = y; y1 < y+h; y1++){
paint_pixel(x1,y1,r,g,b,screen);
}
}
for (x1 = x; x1 < x+w; x1++){
for (y1 = y; y1 < y+h; y1++){
paint_pixel(x1,y1,r,g,b,screen);
}
}
}

void paint_pixel(u32 x, u32 y, u8 r, u8 g, u8 b, int screen){
Expand All @@ -22,13 +22,19 @@ void paint_pixel(u32 x, u32 y, u8 r, u8 g, u8 b, int screen){
if (y >= BOTTOM_HEIGHT) {
return;
}
int coord = 720*x+720-(y*3);
write_color(coord+screen,r,g,b);

if (bottomFrameIsVid) {
int coord = BOTTOM_VID_PITCH*x+BOTTOM_VID_PITCH-(y*BOTTOM_VID_BPP)-BOTTOM_VID_BPP;
write_color_vid(coord+screen,r,g,b);
} else {
int coord = bottomFrameBufferPitch*x+BOTTOM_UI_PITCH-(y*BOTTOM_UI_BPP)-BOTTOM_UI_BPP;
write_color(coord+screen,r,g,b);
}
}

void blank(int x, int y, int xs, int ys){
paint_square(x,y,255,255,255,xs,ys,BOTTOM_FRAME1);
// paint_square(x,y,255,255,255,xs,ys,BOTTOM_FRAME2);
paint_square(x,y,255,255,255,xs,ys,BOTTOM_FRAME1);
// paint_square(x,y,255,255,255,xs,ys,BOTTOM_FRAME2);
}

void paint_letter(char letter, int x, int y, u8 r, u8 g, u8 b, int screen) {
Expand Down Expand Up @@ -57,20 +63,20 @@ void paint_letter(char letter, int x, int y, u8 r, u8 g, u8 b, int screen) {
}
}
void paint_word(char* word, int x,int y, u8 r, u8 g, u8 b, int screen){
int tmp_x =x;
unsigned int i;
int line = 0;
int tmp_x =x;
unsigned int i;
int line = 0;

for (i = 0; i <strlen(word); i++){

if (tmp_x+8 > BOTTOM_WIDTH) {
line++;
tmp_x = x;
}
paint_letter(word[i],tmp_x,y+(line*8),r,g,b, screen);
for (i = 0; i <strlen(word); i++){

tmp_x = tmp_x+8;
}
if (tmp_x+8 > BOTTOM_WIDTH) {
line++;
tmp_x = x;
}
paint_letter(word[i],tmp_x,y+(line*8),r,g,b, screen);

tmp_x = tmp_x+8;
}

}

2 changes: 1 addition & 1 deletion source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void threadStart() {
if (isFileExist("/debug.flag")) {
nsInit();
}
svc_sleepThread(1000000000);
// svc_sleepThread(1000000000);
plgInitFromInjectHOME();
screenshotMain();
//magicKillProcess(0x27);
Expand Down
5 changes: 5 additions & 0 deletions source/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
}*/

void write_color(u32 address, u8 r, u8 g, u8 b){
u16 color = (((u16)r >> 3) << 11) | (((u16)g >> 3) << 6) | (((u16)b >> 3) << 1) | 1;
*(u16 *)address = color;
}

void write_color_vid(u32 address, u8 r, u8 g, u8 b){
write_byte(address, b);
write_byte(address+1, g);
write_byte(address+2, r);
Expand Down
1 change: 0 additions & 1 deletion source/sharedfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


void initSharedFunc(void) {

INIT_SHARED_FUNC(showDbgShared, 0);
INIT_SHARED_FUNC(nsDbgPrintShared, 1);
INIT_SHARED_FUNC(plgRegisterMenuEntry, 2);
Expand Down
83 changes: 57 additions & 26 deletions source/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ u32 allowDirectScreenAccess = 0;
u32 bottomFrameBuffer = 0x1F000000;
u32 bottomRenderingFrameBuffer = 0x1F000000;
u32 bottomAllocFrameBuffer = 0;
u32 bottomFrameBufferPitch = BOTTOM_UI_PITCH;
u32 hGSPProcess = 0;



u32 bottomFrameIsVid = 0;
u32 bottomFrameSavedVid = 0;

int builtinDrawString(char* str, int x, int y, char r, char g, char b, int newLine) {
int len = strlen(str);
Expand Down Expand Up @@ -66,18 +67,19 @@ u32 initDirectScreenAccess(void) {
if (ret != 0) {
return ret;
}
// bottomAllocFrameBuffer = plgRequestMemory(BOTTOM_FRAME_SIZE);
// if (bottomAllocFrameBuffer == 0) {
// return -1;
// }
bottomAllocFrameBuffer = plgRequestMemory(BOTTOM_FRAME_VID_SIZE);
if (bottomAllocFrameBuffer == 0) {
return -1;
}
allowDirectScreenAccess = 1;
bottomRenderingFrameBuffer = bottomAllocFrameBuffer;

return 0;

}

u32 controlVideo(u32 cmd, u32 arg1, u32 /*arg2*/, u32 /*rg3*/) {
bottomFrameIsVid = 1;

if (cmd == CONTROLVIDEO_ACQUIREVIDEO) {
acquireVideo();
return 0;
Expand Down Expand Up @@ -128,14 +130,20 @@ void mdelay(u32 m) {
void memcpy_ctr(void* dst, void* src, size_t size);
void updateScreen(void) {
// if (bottomFrameBuffer != BOTTOM_FRAME1)
memcpy_ctr((void *)((getPhysAddr(bottomFrameBuffer) | 0x80000000)), (void *)BOTTOM_FRAME1, BOTTOM_FRAME_SIZE);
if (bottomFrameIsVid)
memcpy_ctr((void *)(getPhysAddr(bottomFrameBuffer) | 0x80000000), (void *)BOTTOM_FRAME1, BOTTOM_FRAME_VID_SIZE);
else
for (int j = 0; j < BOTTOM_WIDTH; ++j)
memcpy_ctr(
(u8 *)(getPhysAddr(bottomFrameBuffer) | 0x80000000) + j * bottomFrameBufferPitch,
(u8 *)BOTTOM_FRAME1 + j * bottomFrameBufferPitch,
BOTTOM_UI_PITCH
);
*(vu32*)(IoBasePdc + 0x568) = getPhysAddr(bottomFrameBuffer);
*(vu32*)(IoBasePdc + 0x56C) = getPhysAddr(bottomFrameBuffer);
*(vu32*)(IoBasePdc + 0x570) = 0x00080301;
*(vu32*)(IoBasePdc + 0x570) = 0x00080300 | (bottomFrameIsVid ? BOTTOM_VID_FORMAT : BOTTOM_UI_FORMAT);
*(vu32*)(IoBasePdc + 0x55c) = 0x014000f0;
*(vu32*)(IoBasePdc + 0x590) = 0x000002d0;
// if (bottomFrameBuffer == BOTTOM_FRAME1)
// svc_flushProcessDataCache(0xffff8001, BOTTOM_FRAME1, BOTTOM_FRAME_SIZE);
*(vu32*)(IoBasePdc + 0x590) = bottomFrameIsVid ? BOTTOM_VID_PITCH : bottomFrameBufferPitch;
}

s32 showMenu(char* title, u32 entryCount, char* captions[]) {
Expand Down Expand Up @@ -288,7 +296,7 @@ void showDbgShared(char* fmt, u32 v1, u32 v2) {

extern PLGLOADER_INFO *g_plgInfo;
u32 decideBottomFrameBufferAddr() {

bottomFrameBufferPitch = BOTTOM_UI_PITCH;
if (g_plgInfo) {
u32 tidLow = g_plgInfo->tid[0];
if ((tidLow == 0x00125500) || (tidLow == 0x000D6E00) || (tidLow == 0x00125600)) {
Expand Down Expand Up @@ -318,9 +326,10 @@ u32 decideBottomFrameBufferAddr() {
if (
isInVRAM(bl_fbaddr[0]) ||
isInVRAM(bl_fbaddr[1])
)
return (bl_fbaddr[0] < bl_fbaddr[1] ? bl_fbaddr[0] : bl_fbaddr[1])
+ 0x07000000;
) {
bottomFrameBufferPitch = REG(IoBasePdc + 0x590);
return MIN(MIN(bl_fbaddr[0], bl_fbaddr[1]), 0x18600000 - BOTTOM_FRAME_VID_SIZE) + 0x07000000;
}
u32 tl_fbaddr[2];
tl_fbaddr[0] = REG(IoBasePdc + 0x468);
tl_fbaddr[1] = REG(IoBasePdc + 0x46c);
Expand All @@ -335,24 +344,31 @@ u32 decideBottomFrameBufferAddr() {
void acquireVideo(void) {
if (videoRef == 0) {
bottomFrameBuffer = decideBottomFrameBufferAddr();
if (bottomAllocFrameBuffer != 0)
bottomRenderingFrameBuffer = bottomAllocFrameBuffer;
else
bottomRenderingFrameBuffer = bottomFrameBuffer;
bottomRenderingFrameBuffer = bottomFrameBuffer;
*(vu32*)(IoBaseLcd + 0x204) = 0;
*(vu32*)(IoBaseLcd + 0xA04) = 0;
if (!bottomFrameIsVid && bottomAllocFrameBuffer)
for (int j = 0; j < BOTTOM_WIDTH; ++j)
memcpy_ctr(
(u8 *)bottomAllocFrameBuffer + j * BOTTOM_UI_PITCH,
(u8 *)bottomFrameBuffer + j * bottomFrameBufferPitch,
BOTTOM_UI_PITCH
);
else if (bottomFrameIsVid && bottomAllocFrameBuffer) {
bottomFrameSavedVid = 1;
memcpy_ctr(
(void *)bottomAllocFrameBuffer,
(void *)bottomFrameBuffer,
BOTTOM_FRAME_VID_SIZE
);
}
savedVideoState[0] = *(vu32*)(IoBasePdc + 0x568);
savedVideoState[1] = *(vu32*)(IoBasePdc + 0x56C);
savedVideoState[2] = *(vu32*)(IoBasePdc + 0x570);
savedVideoState[3] = *(vu32*)(IoBasePdc + 0x55c);
savedVideoState[4] = *(vu32*)(IoBasePdc + 0x590);
*(vu32*)(IoBasePdc + 0x568) = getPhysAddr(bottomFrameBuffer);
*(vu32*)(IoBasePdc + 0x56C) = getPhysAddr(bottomFrameBuffer);
*(vu32*)(IoBasePdc + 0x570) = 0x00080301;
*(vu32*)(IoBasePdc + 0x55c) = 0x014000f0;
*(vu32*)(IoBasePdc + 0x590) = 0x000002d0;

blank(0, 0, 320, 240);
updateScreen();
}
videoRef ++;
}
Expand All @@ -365,6 +381,21 @@ void releaseVideo(void) {
*(vu32*)(IoBasePdc + 0x570) = savedVideoState[2];
*(vu32*)(IoBasePdc + 0x55c) = savedVideoState[3];
*(vu32*)(IoBasePdc + 0x590) = savedVideoState[4];
if (!bottomFrameIsVid && bottomAllocFrameBuffer)
for (int j = 0; j < BOTTOM_WIDTH; ++j)
memcpy_ctr(
(u8 *)bottomFrameBuffer + j * bottomFrameBufferPitch,
(u8 *)bottomAllocFrameBuffer + j * BOTTOM_UI_PITCH,
BOTTOM_UI_PITCH
);
else if (bottomFrameSavedVid) {
bottomFrameSavedVid = 0;
memcpy_ctr(
(void *)bottomFrameBuffer,
(void *)bottomAllocFrameBuffer,
BOTTOM_FRAME_VID_SIZE
);
}
}
}

Expand Down

0 comments on commit 3b0a6f4

Please sign in to comment.