Skip to content

Commit

Permalink
Moved common code to static library, refactored mask-bit test
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Dec 2, 2019
1 parent 0626d0b commit 79dfb13
Show file tree
Hide file tree
Showing 14 changed files with 263 additions and 181 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ build/
.vscode/
.idea/

*.a
*.elf
*.exe
*.zip
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
TOPTARGETS = build clean

IMAGES = gpu/bandwidth \
IMAGES = common \
gpu/bandwidth \
gpu/benchmark \
gpu/quad \
gpu/transparency \
Expand Down
13 changes: 13 additions & 0 deletions common-test.mk
Original file line number Diff line number Diff line change
@@ -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)
38 changes: 38 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
@@ -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 $@
12 changes: 12 additions & 0 deletions common/Makefile
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <stdint.h>
#include <stdio.h>
#include "gpu.h"
60 changes: 45 additions & 15 deletions gpu/mask-bit/vram.c → common/gpu.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
#include "vram.h"
#include "gpu.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;
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
Expand All @@ -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);
Expand Down
41 changes: 41 additions & 0 deletions common/gpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include "stdint.h"
#include <psxgpu.h>

#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
File renamed without changes.
14 changes: 14 additions & 0 deletions common/test.h
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion gpu/mask-bit/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include $(PSN00BSDK)/sdk-common.mk
include ../../common-test.mk

TARGET = mask-bit.elf
59 changes: 0 additions & 59 deletions gpu/mask-bit/main.c

This file was deleted.

Loading

0 comments on commit 79dfb13

Please sign in to comment.