Code Refactor

This commit is contained in:
Samuel Walker 2025-03-28 10:50:13 -06:00
parent e7ae07ae13
commit 8605221316
5 changed files with 72 additions and 57 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"tamtypes.h": "c"
}
}

View File

@ -2,8 +2,10 @@ cmake_minimum_required(VERSION 3.10)
set(MAKE_TOOLCHAIN_FILE $PS2SDK/ps2dev.cmake)
set(CMAKE_C_FLAGS_DEBUG -DDEBUG)
project(helloworld)
add_executable(helloworld.elf main.c)
FILE(GLOB c_src src/**.c)
add_executable(helloworld.elf ${c_src})
target_link_libraries(helloworld.elf debug -ldebug -lgraph -ldma -ldraw)
target_include_directories(helloworld.elf PUBLIC include/)
add_custom_command(
TARGET helloworld.elf
POST_BUILD

14
include/gs.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <draw.h>
struct draw_state {
int width;
int height;
framebuffer_t fb;
zbuffer_t zb;
int vmode;
int gmode;
};
int gs_init(struct draw_state *ds, int psm, int psmz);

29
src/gs.c Normal file
View File

@ -0,0 +1,29 @@
#include "gs.h"
#include <graph.h>
#include <dma.h>
int gs_init(struct draw_state *ds, int psm, int psmz) {
ds->fb.address = graph_vram_allocate(ds->width, ds->height, psm, GRAPH_ALIGN_PAGE);
ds->fb.width = ds->width;
ds->fb.height = ds->height;
ds->fb.psm = psm;
ds->fb.mask = 0;
ds->zb.address = graph_vram_allocate(ds->width, ds->height, psmz, GRAPH_ALIGN_PAGE);
ds->zb.enable = 0;
ds->zb.method = 0;
ds->zb.zsm = 0;
ds->zb.mask = 0;
graph_set_mode(ds->gmode, ds->vmode, GRAPH_MODE_FIELD, GRAPH_DISABLE);
graph_set_screen(0, 0, ds->width, ds->height);
graph_set_bgcolor(0, 0, 0);
graph_set_framebuffer_filtered(ds->fb.address, ds->width, psm, 0, 0);
graph_enable_output();
qword_t buf[100];
qword_t *q = buf;
q = draw_setup_environment(q, 0, &ds->fb, &ds->zb);
q = draw_primitive_xyoffset(q, 0, 2048-(ds->width/2), 2048-(ds->height/2));
q = draw_finish(q);
dma_channel_send_normal(DMA_CHANNEL_GIF, buf, q-buf, 0, 0);
dma_wait_fast();
return 0;
}

View File

@ -13,6 +13,7 @@
#include <inttypes.h>
#include <gs_gp.h>
#include <stdlib.h>
#include "gs.h"
#define myftoi4(x) ((x)<<4)
@ -28,6 +29,7 @@
#define VID_W 640
#define VID_H 448
#define vid_buf_size (100 * 16)
int print_buffer(qword_t *b, int len) {
DEBUG_MSG("-- buffer\n");
@ -39,43 +41,6 @@ int print_buffer(qword_t *b, int len) {
return 0;
}
int gs_finish() {
qword_t buff[50];
qword_t *q = buff;
//TODO: Remove!
q = draw_finish(q);
dma_channel_send_normal(DMA_CHANNEL_GIF, buff, q-buff, 0, 0);
dma_wait_fast();
return 0;
}
zbuffer_t *z;
int gs_init(int width, int height, int psm, int psmz, int vmode, int gmode) {
framebuffer_t fb;
fb.address = graph_vram_allocate(width, height, psm, GRAPH_ALIGN_PAGE);
fb.width = width;
fb.height = height;
fb.psm = psm;
fb.mask = 0;
z->address = graph_vram_allocate(width, height, psmz, GRAPH_ALIGN_PAGE);
z->enable = 0;
z->method = 0;
z->zsm = 0;
z->mask = 0;
graph_set_mode(gmode, vmode, GRAPH_MODE_FIELD, GRAPH_DISABLE);
graph_set_screen(0, 0, width, height);
graph_set_bgcolor(0, 0, 0);
graph_set_framebuffer_filtered(fb.address, width, psm, 0, 0);
graph_enable_output();
qword_t buf[100];
qword_t *q = buf;
q = draw_setup_environment(q, 0, &fb, z);
q = draw_primitive_xyoffset(q, 0, 2048-(VID_W/2), 2048-(VID_H/2));
q = draw_finish(q);
dma_channel_send_normal(DMA_CHANNEL_GIF, buf, q-buf, 0, 0);
dma_wait_fast();
return 0;
}
static int verts[] = {
100, 100, 0,
300, 100, 0,
@ -90,15 +55,7 @@ static int verts[] = {
uint64_t red = 0xf0;
uint64_t blue = 0x0f;
uint64_t green = 0xf0;
int draw() {
qword_t buf[50];
qword_t *q = buf;
q = draw_disable_tests(q, 0, z);
q = draw_clear(q, 0, 2048.0f - 320, 2048.0f - 244, VID_W, VID_H, 20, 20, 20);
//q = draw_enable_tests(q, 0, z);
// GIFTag header - 3x(col, pos)
qword_t *draw(qword_t *q) {
q->dw[0] = 0xD000000000000001;
q->dw[1] = 0x0005151515151510;
q++;
@ -123,12 +80,7 @@ int draw() {
q->dw[1] = z;
q++;
}
q = draw_finish(q);
print_buffer(buf, q-buf);
dma_channel_send_normal(DMA_CHANNEL_GIF, buf, q-buf, 0, 0);
dma_wait_fast();
//gs_finish();
return 0;
return q;
}
int main() {
@ -142,20 +94,33 @@ int main() {
dma_channel_fast_waits(DMA_CHANNEL_GIF);
DEBUG_MSG("DMA Initialized...\n");
// initialize graphics mode
gs_init(VID_W, VID_H, GS_PSM_32, GS_PSMZ_24, GRAPH_MODE_NTSC, GRAPH_MODE_INTERLACED);
struct draw_state state = {0};
state.width = VID_W;
state.height = VID_H;
state.gmode = GRAPH_MODE_INTERLACED;
state.vmode = GRAPH_MODE_NTSC;
gs_init(&state, GS_PSM_32, GS_PSMZ_24);
DEBUG_MSG("GS Initialized...\n");
// clear screen
// build buffer with triangle data
z = malloc(sizeof(zbuffer_t));
qword_t *buf = malloc(vid_buf_size);
graph_wait_vsync();
while(1) {
dma_wait_fast();
red++;
if(red > 0xFF) {
red = 0;
}
draw();
qword_t *q = buf;
q = draw_disable_tests(q, 0, &state.zb);
q = draw_clear(q, 0, 2048.0f - 320, 2048.0f - 244, VID_W, VID_H, 20, 20, 20);
q = draw(q);
q = draw_finish(q);
dma_channel_send_normal(DMA_CHANNEL_GIF, buf, q-buf, 0, 0);
draw_wait_finish();
graph_wait_vsync();
}
}