-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f804df
commit d190785
Showing
9 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include $(PSN00BSDK)/sdk-common.mk | ||
|
||
TARGET = mask-bit.elf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <stdio.h> | ||
#include <psxgpu.h> | ||
#include "vram.h" | ||
#include "stdint.h" | ||
|
||
DISPENV disp; | ||
DRAWENV draw; | ||
|
||
#define SCR_W 320 | ||
#define SCR_H 240 | ||
|
||
void setResolution(int w, int h) { | ||
SetDefDispEnv(&disp, 0, 0, w, h); | ||
SetDefDrawEnv(&draw, 0, 0, w, h); | ||
|
||
PutDispEnv(&disp); | ||
PutDrawEnv(&draw); | ||
} | ||
|
||
void initVideo() | ||
{ | ||
ResetGraph(0); | ||
setResolution(SCR_W, SCR_H); | ||
SetDispMask(1); | ||
} | ||
|
||
void fillRect(int x, int y, int w, int h, int r, int g, int b) { | ||
FILL f; | ||
setFill(&f); | ||
setRGB0(&f, r, g, b); | ||
setXY0(&f, x, y); | ||
setWH(&f, w, h); | ||
|
||
DrawPrim(&f); | ||
} | ||
|
||
void clearScreen() { | ||
fillRect(0, 0, 512, 256, 0, 0, 0); | ||
fillRect(512, 0, 512, 256, 0, 0, 0); | ||
fillRect(0, 256, 512, 256, 0, 0, 0); | ||
fillRect(512, 256, 0x3f1, 256, 0, 0, 0); | ||
} | ||
|
||
void runTests(); | ||
|
||
int main() { | ||
initVideo(); | ||
printf("\ngpu/mask-bit\n"); | ||
|
||
setMaskBitSetting(false, false); | ||
clearScreen(); | ||
|
||
runTests(); | ||
|
||
for (;;) { | ||
VSync(0); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
gpu/mask-bit | ||
pass - testWriteAndRead | ||
pass - testSetBit | ||
pass - testCheckMaskBit | ||
pass - testCheckIsMaskBitStickyManually | ||
pass - testCheckIsMaskBitStickySetBit | ||
Done. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#ifndef __cplusplus | ||
#define true 1 | ||
#define false 0 | ||
typedef char bool; | ||
#endif | ||
|
||
typedef signed char int8_t; | ||
typedef short int int16_t; | ||
typedef int int32_t; | ||
typedef unsigned char uint8_t; | ||
typedef unsigned short int uint16_t; | ||
typedef unsigned int uint32_t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include <stdio.h> | ||
#include "vram.h" | ||
#include "stdint.h" | ||
|
||
#define assertEquals(given, expected) \ | ||
do { \ | ||
auto GIVEN = (given); \ | ||
auto EXPECTED = (expected); \ | ||
if (GIVEN == EXPECTED) { \ | ||
printf("pass - %s\n", __FUNCTION__); \ | ||
} else { \ | ||
printf("fail - %s:%d `"#given" == "#expected"`," \ | ||
" given: 0x%04x, expected: 0x%04x\n", \ | ||
__FUNCTION__, __LINE__, GIVEN, EXPECTED); \ | ||
} \ | ||
} while(0) | ||
|
||
// Check if read pixel == written pixel | ||
void testWriteAndRead() { | ||
int x = 32; | ||
int y = 32; | ||
|
||
setMaskBitSetting(false, false); | ||
|
||
vramPut(x, y, 0x1234); | ||
|
||
assertEquals(vramGet(x, y), 0x1234); | ||
} | ||
|
||
// Check if GP0(0xE6) bit0 (Set mask while drawing) works | ||
void testSetBit() { | ||
int x = 33; | ||
int y = 32; | ||
|
||
setMaskBitSetting(true, false); | ||
vramPut(x, y, 0); | ||
|
||
assertEquals(vramGet(x, y), 0x8000); | ||
} | ||
|
||
// Check if GP0(0xE6) bit1 (Check mask before draw) works | ||
void testCheckMaskBit() { | ||
int x = 34; | ||
int y = 32; | ||
|
||
// Disable mask bit set | ||
setMaskBitSetting(false, false); | ||
vramPut(x, y, 0x8000); // Write mask bit | ||
|
||
// Enable check mask bit | ||
setMaskBitSetting(false, true); | ||
vramPut(x, y, 0x1234); | ||
|
||
assertEquals(vramGet(x, y), 0x8000); | ||
} | ||
|
||
// Check mask bit (written manually) can be overwritten | ||
void testCheckIsMaskBitStickyManually() { | ||
int x = 35; | ||
int y = 32; | ||
|
||
setMaskBitSetting(false, false); | ||
vramPut(x, y, 0x8123); // Write mask bit manually | ||
vramPut(x, y, 0x0456); // Try clearing it | ||
|
||
assertEquals(vramGet(x, y), 0x0456); | ||
} | ||
|
||
// Check mask bit (written automatically) can be overwritten | ||
void testCheckIsMaskBitStickySetBit() { | ||
int x = 36; | ||
int y = 32; | ||
|
||
setMaskBitSetting(true, false); | ||
vramPut(x, y, 0x0000); // Write mask bit using HW | ||
|
||
setMaskBitSetting(false, false); | ||
vramPut(x, y, 0x0456); // Try clearing it | ||
|
||
assertEquals(vramGet(x, y), 0x0456); | ||
} | ||
|
||
extern "C" void runTests() { | ||
testWriteAndRead(); | ||
testSetBit(); | ||
testCheckMaskBit(); | ||
testCheckIsMaskBitStickyManually(); | ||
testCheckIsMaskBitStickySetBit(); | ||
|
||
printf("Done.\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "vram.h" | ||
#include <psxgpu.h> | ||
|
||
typedef struct CPU2VRAM { | ||
unsigned int tag; | ||
unsigned char p0,p1,p2,code; | ||
unsigned short x0,y0; | ||
unsigned short w,h; | ||
unsigned int data; // Single pixel | ||
} CPU2VRAM; | ||
|
||
typedef struct VRAM2CPU { | ||
unsigned int tag; | ||
unsigned char p0,p1,p2,code; | ||
unsigned short x0,y0; | ||
unsigned short w,h; | ||
} VRAM2CPU; | ||
|
||
uint32_t ReadGPUstat(); | ||
|
||
// TODO: Remove when #9 in PSn00bSDK is merged | ||
#undef setDrawMask | ||
#define setDrawMask( p, sb, mt ) \ | ||
setlen( p, 1 ), (p)->code[0] = sb|(mt<<1), setcode( p, 0xe6 ) | ||
|
||
void setMaskBitSetting(bool setBit, bool checkBit) { | ||
DR_MASK mask; | ||
setDrawMask(&mask, setBit, checkBit); | ||
DrawPrim(&mask); | ||
} | ||
|
||
void writeGP1(uint8_t cmd, uint32_t data) { | ||
uint32_t *GP1 = (uint32_t*)0x1f801814; | ||
(*GP1) = (cmd << 24) | (data&0xffffff); | ||
} | ||
|
||
uint32_t readGPU() { | ||
uint32_t* GPUREAD = (uint32_t*)0x1f801810; | ||
return *GPUREAD; | ||
} | ||
|
||
void vramPut(int x, int y, uint16_t pixel) { | ||
CPU2VRAM buf = {0}; | ||
setcode(&buf, 0xA0); // CPU -> VRAM | ||
setlen(&buf, 4); | ||
|
||
buf.x0 = x; // VRAM position | ||
buf.y0 = y; | ||
buf.w = 1; // Transfer size - 1x1 | ||
buf.h = 1; | ||
buf.data = pixel; // pixel (lower 16bit) | ||
|
||
DrawPrim(&buf); | ||
} | ||
|
||
uint32_t vramGet(int x, int y) { | ||
VRAM2CPU buf = {0}; | ||
setcode(&buf, 0xC0); // CPU -> VRAM | ||
setlen(&buf, 3); | ||
|
||
buf.x0 = x; // VRAM position | ||
buf.y0 = y; | ||
buf.w = 1; // Transfer size - 1x1 | ||
buf.h = 1; | ||
|
||
DrawPrim(&buf); | ||
|
||
writeGP1(4, 3); // DMA Direction - VRAM -> CPU | ||
|
||
// Wait for VRAM to CPU ready | ||
while ((ReadGPUstat() & (1<<27)) == 0); | ||
|
||
return readGPU(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
#include "stdint.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
void setMaskBitSetting(bool setBit, bool checkBit); | ||
void vramPut(int x, int y, uint16_t pixel); | ||
uint32_t vramGet(int x, int y) ; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |