From 79dfb131b5c3694a75cbecb64a7b8f2ef29a9ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Czekan=CC=81ski?= Date: Mon, 2 Dec 2019 17:53:04 +0100 Subject: [PATCH] Moved common code to static library, refactored mask-bit test --- .gitignore | 1 + Makefile | 3 +- common-test.mk | 13 ++ common.mk | 38 ++++++ common/Makefile | 12 ++ common/common.h | 4 + gpu/mask-bit/vram.c => common/gpu.c | 60 ++++++--- common/gpu.h | 41 ++++++ {gpu/mask-bit => common}/stdint.h | 0 common/test.h | 14 ++ gpu/mask-bit/Makefile | 2 +- gpu/mask-bit/main.c | 59 --------- gpu/mask-bit/{tests.cpp => main.cpp} | 183 ++++++++++++++------------- gpu/mask-bit/vram.h | 14 -- 14 files changed, 263 insertions(+), 181 deletions(-) create mode 100644 common-test.mk create mode 100644 common.mk create mode 100644 common/Makefile create mode 100644 common/common.h rename gpu/mask-bit/vram.c => common/gpu.c (58%) create mode 100644 common/gpu.h rename {gpu/mask-bit => common}/stdint.h (100%) create mode 100644 common/test.h delete mode 100644 gpu/mask-bit/main.c rename gpu/mask-bit/{tests.cpp => main.cpp} (68%) delete mode 100644 gpu/mask-bit/vram.h diff --git a/.gitignore b/.gitignore index ef83c04..b5a4c79 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ build/ .vscode/ .idea/ +*.a *.elf *.exe *.zip diff --git a/Makefile b/Makefile index fdf2a6b..2fcb3f9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ TOPTARGETS = build clean -IMAGES = gpu/bandwidth \ +IMAGES = common \ + gpu/bandwidth \ gpu/benchmark \ gpu/quad \ gpu/transparency \ diff --git a/common-test.mk b/common-test.mk new file mode 100644 index 0000000..09fa34e --- /dev/null +++ b/common-test.mk @@ -0,0 +1,13 @@ +BASEDIR = $(dir $(lastword $(MAKEFILE_LIST))) +include $(BASEDIR)/common.mk + +INCLUDE += -I$(BASEDIR)/common +LIBDIRS += -L$(BASEDIR)/common +LIBS += -lc -lcommon -lpsxetc -lpsxgpu -lpsxapi + +all: $(OFILES) + $(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET) + elf2x -q $(TARGET) + +clean: + rm -rf build $(TARGET) $(TARGET:.elf=.exe) diff --git a/common.mk b/common.mk new file mode 100644 index 0000000..58fde9f --- /dev/null +++ b/common.mk @@ -0,0 +1,38 @@ +PSN00BSDK ?= /opt/psn00bsdk/ +PREFIX = mipsel-unknown-elf- +INCLUDE := -I$(PSN00BSDK)/libpsn00b/include +LIBDIRS := -L$(PSN00BSDK)/libpsn00b +GCC_VERSION = 7.4.0 +GCC_BASE = /usr/local/mipsel-unknown-elf + +CFLAGS = -g -msoft-float -O2 -fno-builtin -fdata-sections -ffunction-sections +CPPFLAGS = $(CFLAGS) -fno-exceptions +AFLAGS = -g -msoft-float +LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x + +# Toolchain programs +CC = $(PREFIX)gcc +CXX = $(PREFIX)g++ +AS = $(PREFIX)as +AR = $(PREFIX)ar +LD = $(PREFIX)ld +RANLIB = $(PREFIX)ranlib + +LIBS = + +CFILES = $(notdir $(wildcard *.c)) +CPPFILES = $(notdir $(wildcard *.cpp)) +AFILES = $(notdir $(wildcard *.s)) +OFILES = $(addprefix build/,$(CFILES:.c=.o) $(CPPFILES:.cpp=.o) $(AFILES:.s=.o)) + +build/%.o: %.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +build/%.o: %.cpp + @mkdir -p $(dir $@) + $(CXX) $(AFLAGS) $(INCLUDE) -c $< -o $@ + +build/%.o: %.s + @mkdir -p $(dir $@) + $(CC) $(AFLAGS) $(INCLUDE) -c $< -o $@ diff --git a/common/Makefile b/common/Makefile new file mode 100644 index 0000000..ac4d119 --- /dev/null +++ b/common/Makefile @@ -0,0 +1,12 @@ +include ../common.mk + +TARGET = libcommon.a + +all: $(TARGET) + +$(TARGET): $(OFILES) + $(AR) cr $(TARGET) $(OFILES) + $(RANLIB) $(TARGET) + +clean: + rm -Rf build $(TARGET) diff --git a/common/common.h b/common/common.h new file mode 100644 index 0000000..03161e8 --- /dev/null +++ b/common/common.h @@ -0,0 +1,4 @@ +#pragma once +#include +#include +#include "gpu.h" \ No newline at end of file diff --git a/gpu/mask-bit/vram.c b/common/gpu.c similarity index 58% rename from gpu/mask-bit/vram.c rename to common/gpu.c index 8d1429c..d8c209a 100644 --- a/gpu/mask-bit/vram.c +++ b/common/gpu.c @@ -1,22 +1,40 @@ -#include "vram.h" +#include "gpu.h" #include -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; +DISPENV disp; +DRAWENV draw; -typedef struct VRAM2CPU { - unsigned int tag; - unsigned char p0,p1,p2,code; - unsigned short x0,y0; - unsigned short w,h; -} VRAM2CPU; +void setResolution(int w, int h) { + SetDefDispEnv(&disp, 0, 0, w, h); + SetDefDrawEnv(&draw, 0, 0, w, h); -uint32_t ReadGPUstat(); + PutDispEnv(&disp); + PutDrawEnv(&draw); +} + +void initVideo(int width, int height) +{ + ResetGraph(0); + setResolution(width, height); + 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); +} // TODO: Remove when #9 in PSn00bSDK is merged #undef setDrawMask @@ -29,6 +47,18 @@ void setMaskBitSetting(bool setBit, bool checkBit) { DrawPrim(&mask); } +void gpuNop() { + writeGP0(0, 0); +} + +void writeGP0(uint8_t cmd, uint32_t value) { + DR_TPAGE p; + p.code[0] = value; + setlen( &p, 1 ); + setcode( &p, cmd ); + DrawPrim(&p); +} + void writeGP1(uint8_t cmd, uint32_t data) { uint32_t *GP1 = (uint32_t*)0x1f801814; (*GP1) = (cmd << 24) | (data&0xffffff); diff --git a/common/gpu.h b/common/gpu.h new file mode 100644 index 0000000..300b5a1 --- /dev/null +++ b/common/gpu.h @@ -0,0 +1,41 @@ +#pragma once +#include "stdint.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + +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; + +void setResolution(int w, int h); +void initVideo(int width, int height); +void fillRect(int x, int y, int w, int h, int r, int g, int b); +void clearScreen(); + +void setMaskBitSetting(bool setBit, bool checkBit); +void gpuNop(); +uint32_t ReadGPUstat(); +void writeGP0(uint8_t cmd, uint32_t value); +void writeGP1(uint8_t cmd, uint32_t data); + +uint32_t readGPU(); +void vramPut(int x, int y, uint16_t pixel); +uint32_t vramGet(int x, int y); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/gpu/mask-bit/stdint.h b/common/stdint.h similarity index 100% rename from gpu/mask-bit/stdint.h rename to common/stdint.h diff --git a/common/test.h b/common/test.h new file mode 100644 index 0000000..8ec5420 --- /dev/null +++ b/common/test.h @@ -0,0 +1,14 @@ +#pragma once + +#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%x, expected: 0x%x\n", \ + __FUNCTION__, __LINE__, GIVEN, EXPECTED); \ + } \ +} while(0) diff --git a/gpu/mask-bit/Makefile b/gpu/mask-bit/Makefile index 7e10a58..635bcc3 100644 --- a/gpu/mask-bit/Makefile +++ b/gpu/mask-bit/Makefile @@ -1,3 +1,3 @@ -include $(PSN00BSDK)/sdk-common.mk +include ../../common-test.mk TARGET = mask-bit.elf \ No newline at end of file diff --git a/gpu/mask-bit/main.c b/gpu/mask-bit/main.c deleted file mode 100644 index 9fe2962..0000000 --- a/gpu/mask-bit/main.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#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; -} diff --git a/gpu/mask-bit/tests.cpp b/gpu/mask-bit/main.cpp similarity index 68% rename from gpu/mask-bit/tests.cpp rename to gpu/mask-bit/main.cpp index 934a8e9..ae0cca4 100644 --- a/gpu/mask-bit/tests.cpp +++ b/gpu/mask-bit/main.cpp @@ -1,91 +1,92 @@ -#include -#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"); -} \ No newline at end of file +#include +#include + +// 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); +} + +void runTests() { + testWriteAndRead(); + testSetBit(); + testCheckMaskBit(); + testCheckIsMaskBitStickyManually(); + testCheckIsMaskBitStickySetBit(); + + printf("Done.\n"); +} + +int main() { + initVideo(320, 240); + printf("\ngpu/mask-bit\n"); + + setMaskBitSetting(false, false); + clearScreen(); + + runTests(); + + for (;;) { + VSync(0); + } + return 0; +} diff --git a/gpu/mask-bit/vram.h b/gpu/mask-bit/vram.h deleted file mode 100644 index 6e7b5e9..0000000 --- a/gpu/mask-bit/vram.h +++ /dev/null @@ -1,14 +0,0 @@ -#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 \ No newline at end of file