From a3c3951fd09d696e7f9a61eabdb1bb25b78d94df Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 24 Jan 2024 17:29:42 -0500 Subject: [PATCH] Trying to run first GTE code --- include/PSX/GPU/gpu_types.hpp | 2 ++ include/PSX/GTE/gte.hpp | 56 +++++++++++++++++++++++++++-- include/PSX/GTE/gte_instruction.hpp | 39 ++++++++++++++++---- include/PSX/GTE/gte_types.hpp | 18 +++++++--- 4 files changed, 103 insertions(+), 12 deletions(-) diff --git a/include/PSX/GPU/gpu_types.hpp b/include/PSX/GPU/gpu_types.hpp index 966b6fd2..596d3a87 100644 --- a/include/PSX/GPU/gpu_types.hpp +++ b/include/PSX/GPU/gpu_types.hpp @@ -187,6 +187,8 @@ namespace JabyEngine { template struct Position : public internal::XYMovement, T> { + using PrimitiveType = T; + T x; T y; diff --git a/include/PSX/GTE/gte.hpp b/include/PSX/GTE/gte.hpp index ff15c048..0c9b3335 100644 --- a/include/PSX/GTE/gte.hpp +++ b/include/PSX/GTE/gte.hpp @@ -4,7 +4,59 @@ namespace JabyEngine { namespace GTE { /* - gte_SetGeomOffset(ofx,ofy) + RotTrans + + Jaby: Can we use gte_stsv instead of gte_stlvnl for writing to a SVECTOR? + Do we have to use gte_stflg?? + Look at: RotTransSV??? + + Perform coordinate transformation using a rotation matrix + input: Input vector + output: Output vector + flag: flag output + */ + static void rot_trans(const SVECTOR& input, VECTOR& output, int32_t& flag) { + ldv0(input); + rt(); + stlvnl(output); + stflg(flag); + } + + /* + SetRotMatrix + + Sets a 3x3 matrix m as a constant rotation matrix. + matrix: The rotation matrix to set + */ + static void set_rot_matrix(const MATRIX& matrix) { + __asm__ volatile("lw $12, 0(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("lw $13, 4(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("ctc2 $12, $0" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("ctc2 $13, $1" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("lw $12, 8(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("lw $13, 12(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("lw $14, 16(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("ctc2 $12, $2" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("ctc2 $13, $3" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("ctc2 $14, $4" :: "r"(&matrix) : "$12", "$13", "$14"); + } + + /* + SetTransMatrix + + Sets a constant parallel transfer vector specified by m + */ + static void set_trans_matrix(const MATRIX& matrix) { + __asm__ volatile("lw $12, 20(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("lw $13, 24(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("ctc2 $12, $5" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("lw $14, 28(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("ctc2 $13, $6" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("ctc2 $14, $7" :: "r"(&matrix) : "$12", "$13", "$14"); + } + + /* + SetGeomOffset(ofx,ofy) Load GTE-offset. */ @@ -16,7 +68,7 @@ namespace JabyEngine { } /* - gte_SetGeomScreen(h) + SetGeomScreen(h) Load distance from viewpoint to screen. */ diff --git a/include/PSX/GTE/gte_instruction.hpp b/include/PSX/GTE/gte_instruction.hpp index 0598e40a..5fd9e707 100644 --- a/include/PSX/GTE/gte_instruction.hpp +++ b/include/PSX/GTE/gte_instruction.hpp @@ -3,19 +3,46 @@ namespace JabyEngine { namespace GTE { + // Load vertex or normal to vertex register 0 static __always_inline void ldv0(const SVECTOR& vector) { - __asm__ volatile("lwc2 $0, 0(%0)":: "r"(&vector)); - __asm__ volatile("lwc2 $1, 4(%0)":: "r"(&vector)); + __asm__ volatile("lwc2 $0, 0(%0)" :: "r"(&vector)); + __asm__ volatile("lwc2 $1, 4(%0)" :: "r"(&vector)); } + // Load vertex or normal to vertex register 1 static __always_inline void ldv1(const SVECTOR& vector) { - __asm__ volatile("lwc2 $2, 0(%0)":: "r"(&vector)); - __asm__ volatile("lwc2 $3, 4(%0)":: "r"(&vector)); + __asm__ volatile("lwc2 $2, 0(%0)" :: "r"(&vector)); + __asm__ volatile("lwc2 $3, 4(%0)" :: "r"(&vector)); } + // Load vertex or normal to vertex register 2 static __always_inline void ldv2(const SVECTOR& vector) { - __asm__ volatile("lwc2 $4, 0(%0)":: "r"(&vector)); - __asm__ volatile("lwc2 $5, 4(%0)":: "r"(&vector)); + __asm__ volatile("lwc2 $4, 0(%0)" :: "r"(&vector)); + __asm__ volatile("lwc2 $5, 4(%0)" :: "r"(&vector)); + } + + // Store flag + static __always_inline void stflg(int32_t& flag) { + __asm__ volatile("cfc2 $12, $31" :: "r"(&flag) : "$12", "memory"); + __asm__ volatile("nop" :: "r"(&flag) : "$12", "memory"); + __asm__ volatile("sw $12, 0(%0)" :: "r"(&flag) : "$12", "memory"); + } + + // Store VECTOR from 32 bit universal register + static __always_inline void stlvnl(VECTOR& out_vector) { + __asm__ volatile("swc2 $25, 0(%0)" :: "r"(&out_vector) : "memory"); + __asm__ volatile("swc2 $26, 4(%0)" :: "r"(&out_vector) : "memory"); + __asm__ volatile("swc2 $27, 8(%0)" :: "r"(&out_vector) : "memory"); + } + + /* + Kernel of RotTrans + (Transfer vector)+(Rotation Matrix)*(vertex register 0) + */ + static __always_inline void rt() { + __asm__ volatile("nop"); + __asm__ volatile("nop"); + __asm__ volatile("cop2 0x0480012"); } } } \ No newline at end of file diff --git a/include/PSX/GTE/gte_types.hpp b/include/PSX/GTE/gte_types.hpp index 437c5dde..a1eb8500 100644 --- a/include/PSX/GTE/gte_types.hpp +++ b/include/PSX/GTE/gte_types.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../jabyengine_defines.h" +#include "../GPU/gpu_types.hpp" namespace JabyEngine { namespace GTE { @@ -18,6 +18,16 @@ namespace JabyEngine { static constexpr VECTOR create(T x, T y, T z) { return VECTOR{.x = x, .y = y, .z = z, .pad = 0}; } + + template + static constexpr VECTOR from(const GPU::Position& pos) { + return VECTOR::create(static_cast(pos.x), static_cast(pos.y), 0); + } + + template + constexpr S to() const { + return S::create(static_cast(this->x), static_cast(this->y)); + } }; } @@ -28,9 +38,9 @@ namespace JabyEngine { static constexpr MATRIX identity() { return MATRIX{ .rot = { - {1, 0, 0}, - {0, 1, 0}, - {0, 0, 1} + {4096, 0, 0}, + {0, 4096, 0}, + {0, 0, 4096} }, .trans = {0, 0, 0} };