From 1f43aeadb1e540f603bec23564c4edc70286ee55 Mon Sep 17 00:00:00 2001 From: Jaby Date: Mon, 29 Jan 2024 21:24:14 -0500 Subject: [PATCH] Revert back GTE usage and finalize code approach --- include/PSX/GTE/gte.hpp | 42 +++++- include/PSX/GTE/gte_instruction.hpp | 148 ++++++++++------------ src/Library/src/BootLoader/start_boot.cpp | 8 +- src/Library/src/GTE/gte.cpp | 66 ++-------- 4 files changed, 115 insertions(+), 149 deletions(-) diff --git a/include/PSX/GTE/gte.hpp b/include/PSX/GTE/gte.hpp index 42a5999c..0735795f 100644 --- a/include/PSX/GTE/gte.hpp +++ b/include/PSX/GTE/gte.hpp @@ -15,7 +15,12 @@ namespace JabyEngine { output: Output vector flag: flag output */ - void rot_trans(const SVECTOR& input, VECTOR& output, int32_t& flag); + void rot_trans(const SVECTOR& input, VECTOR& output, int32_t& flag) { + ldv0(input); + rt(); + stlvnl(output); + stflg(flag); + } /* SetRotMatrix @@ -23,14 +28,32 @@ namespace JabyEngine { Sets a 3x3 matrix m as a constant rotation matrix. matrix: The rotation matrix to set */ - void set_rot_matrix(const MATRIX& matrix); + 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 */ - void set_trans_matrix(const MATRIX& matrix); + 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"); + } /* MulMatrix0 @@ -43,20 +66,27 @@ namespace JabyEngine { Multiplies two matrices m0 and m1. The function destroys the constant rotation matrix */ - MATRIX& mult_matrix(const MATRIX& m0, const MATRIX& m1, MATRIX& result); + MATRIX& multiply_matrix(const MATRIX& m0, const MATRIX& m1, MATRIX& result); /* SetGeomOffset(ofx,ofy) Load GTE-offset. */ - void set_geom_offset(int32_t off_x, int32_t off_y); + void set_geom_offset(int32_t off_x, int32_t off_y) { + __asm__ volatile("sll $12, %0, 16" :: "r"(off_x), "r"(off_y) : "$12", "$13"); + __asm__ volatile("sll $13, %1, 16" :: "r"(off_x), "r"(off_y) : "$12", "$13"); + __asm__ volatile("ctc2 $12, $24" :: "r"(off_x), "r"(off_y) : "$12", "$13"); + __asm__ volatile("ctc2 $13, $25" :: "r"(off_x), "r"(off_y) : "$12", "$13"); + } /* SetGeomScreen(h) Load distance from viewpoint to screen. */ - void set_geom_screen(int32_t h); + void set_geom_screen(int32_t h) { + __asm__ volatile("ctc2 %0, $26" :: "r"(h)); + } } } \ No newline at end of file diff --git a/include/PSX/GTE/gte_instruction.hpp b/include/PSX/GTE/gte_instruction.hpp index 852d8f99..d21dc3df 100644 --- a/include/PSX/GTE/gte_instruction.hpp +++ b/include/PSX/GTE/gte_instruction.hpp @@ -1,94 +1,78 @@ #pragma once #include "gte_types.hpp" -#ifdef __INTELLISENSE__ - // Load vertex or normal to vertex register 0 - void __jaby_engine_gte_ldv0(const JabyEngine::GTE::SVECTOR& vector); +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)); + } - // Load vertex or normal to vertex register 1 - void __jaby_engine_gte_ldv1(const JabyEngine::GTE::SVECTOR& 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)); + } - // Load vertex or normal to vertex register 2 - void __jaby_engine_gte_ldv2(const JabyEngine::GTE::SVECTOR& 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)); + } - // Load column vector of JabyEngine::GTE::MATRIX to universal register - void __jaby_engine_gte_ldclmv(const JabyEngine::GTE::MATRIX& matrix, size_t col); + // Load column vector of MATRIX to universal register + static __always_inline void ldclmv(const MATRIX& matrix, size_t col) { + __asm__ volatile("lhu $12, 0(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); + __asm__ volatile("lhu $13, 6(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); + __asm__ volatile("lhu $14, 12(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); + __asm__ volatile("mtc2 $12, $9" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); + __asm__ volatile("mtc2 $13, $10" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); + __asm__ volatile("mtc2 $14, $11" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); + } - // Store flag - void __jaby_engine_gte_stflg(int32_t& flag); + // 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 JabyEngine::GTE::MATRIX column from 16 bit universal register - void __jaby_engine_gte_stclmv(JabyEngine::GTE::MATRIX& matrix, size_t col); + // Store MATRIX column from 16 bit universal register + static __always_inline void stclmv(MATRIX& matrix, size_t col) { + __asm__ volatile("mfc2 $12, $9" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); + __asm__ volatile("mfc2 $13, $10" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); + __asm__ volatile("mfc2 $14, $11" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); + __asm__ volatile("sh $12, 0(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); + __asm__ volatile("sh $13, 6(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); + __asm__ volatile("sh $14, 12(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); + } - // Store VECTOR from 32 bit universal register - void __jaby_engine_gte_stlvnl(JabyEngine::GTE::VECTOR& out_vector); + // 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) - */ - void __jaby_engine_gte_rt(); + /* + 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"); + } - /* - Variation of gte_rt - (Rotation Matrix)*(16 bit universal vector) - */ - void __jaby_engine_gte_rtir(); -#else - #define __jaby_engine_gte_ldv0(vector) { \ - __asm__ volatile("lwc2 $0, 0(%0)" :: "r"(&vector)); \ - __asm__ volatile("lwc2 $1, 4(%0)" :: "r"(&vector)); \ + /* + Variation of gte_rt + (Rotation Matrix)*(16 bit universal vector) + */ + static __always_inline void rtir() { + __asm__ volatile("nop"); + __asm__ volatile("nop"); + __asm__ volatile("cop2 0x049E012"); + } } - - #define __jaby_engine_gte_ldv1(vector) { \ - __asm__ volatile("lwc2 $2, 0(%0)" :: "r"(&vector)); \ - __asm__ volatile("lwc2 $3, 4(%0)" :: "r"(&vector)); \ - } - - #define __jaby_engine_gte_ldv2(vector) { \ - __asm__ volatile("lwc2 $4, 0(%0)" :: "r"(&vector)); \ - __asm__ volatile("lwc2 $5, 4(%0)" :: "r"(&vector)); \ - } - - #define __jaby_engine_gte_ldclmv(matrix, col) { \ - __asm__ volatile("lhu $12, 0(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); \ - __asm__ volatile("lhu $13, 6(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); \ - __asm__ volatile("lhu $14, 12(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); \ - __asm__ volatile("mtc2 $12, $9" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); \ - __asm__ volatile("mtc2 $13, $10" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); \ - __asm__ volatile("mtc2 $14, $11" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14"); \ - } - - #define __jaby_engine_gte_stflg(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"); \ - } - - #define __jaby_engine_gte_stclmv(matrix, col) { \ - __asm__ volatile("mfc2 $12, $9" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); \ - __asm__ volatile("mfc2 $13, $10" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); \ - __asm__ volatile("mfc2 $14, $11" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); \ - __asm__ volatile("sh $12, 0(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); \ - __asm__ volatile("sh $13, 6(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); \ - __asm__ volatile("sh $14, 12(%0)" :: "r"(reinterpret_cast(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory"); \ - } - - #define __jaby_engine_gte_stlvnl(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"); \ - } - - #define __jaby_engine_gte_rt() { \ - __asm__ volatile("nop"); \ - __asm__ volatile("nop"); \ - __asm__ volatile("cop2 0x0480012"); \ - } - - #define __jaby_engine_gte_rtir() { \ - __asm__ volatile("nop"); \ - __asm__ volatile("nop"); \ - __asm__ volatile("cop2 0x049E012"); \ - } -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Library/src/BootLoader/start_boot.cpp b/src/Library/src/BootLoader/start_boot.cpp index 2f12d2ee..3e45cd22 100644 --- a/src/Library/src/BootLoader/start_boot.cpp +++ b/src/Library/src/BootLoader/start_boot.cpp @@ -85,11 +85,9 @@ namespace JabyEngine { const auto m1 = GTE::MATRIX::identity(); auto m2 = GTE::MATRIX::identity(); - - GTE::mult_matrix(m0, m1, m2); - asm("# THEIR PLANSCHI START"); - gte_MulMatrix0(&m0, &m1, &m2); - asm("# THEIR PLANSCHI END"); + asm("# PLANSCHI START"); + JabyEngine::GTE::multiply_matrix(m0, m1, m2); + asm("# PLANSCHI END"); } void start() { diff --git a/src/Library/src/GTE/gte.cpp b/src/Library/src/GTE/gte.cpp index 3c1cf4f8..1ceab138 100644 --- a/src/Library/src/GTE/gte.cpp +++ b/src/Library/src/GTE/gte.cpp @@ -2,68 +2,22 @@ namespace JabyEngine { namespace GTE { - void rot_trans(const SVECTOR& input, VECTOR& output, int32_t& flag) { - __jaby_engine_gte_ldv0(input); - __jaby_engine_gte_rt(); - __jaby_engine_gte_stlvnl(output); - __jaby_engine_gte_stflg(flag); - } - - 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"); - } - - 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"); - } - - MATRIX& mult_matrix(const MATRIX& m0, const MATRIX& m1, MATRIX& result) { - /* - Jaby: Somehow this code creates stack usage.... Investigate!! - Jaby: Reimplement all of this with the original code and see how it goes?! - */ - asm("# MY PLANSCHI START"); + MATRIX& multiply_matrix(const MATRIX& m0, const MATRIX& m1, MATRIX& result) { set_rot_matrix(m0); - __jaby_engine_gte_ldclmv(m1, 0); - __jaby_engine_gte_rtir(); - __jaby_engine_gte_stclmv(result, 0); + JabyEngine::GTE::ldclmv(m1, 0); + JabyEngine::GTE::rtir(); + JabyEngine::GTE::stclmv(result, 0); - __jaby_engine_gte_ldclmv(m1, 1); - __jaby_engine_gte_rtir(); - __jaby_engine_gte_stclmv(result, 1); + JabyEngine::GTE::ldclmv(m1, 1); + JabyEngine::GTE::rtir(); + JabyEngine::GTE::stclmv(result, 1); - __jaby_engine_gte_ldclmv(m1, 2); - __jaby_engine_gte_rtir(); - __jaby_engine_gte_stclmv(result, 2); + JabyEngine::GTE::ldclmv(m1, 2); + JabyEngine::GTE::rtir(); + JabyEngine::GTE::stclmv(result, 2); return result; - asm("# MY PLANSCHI END"); - } - - void set_geom_offset(int32_t off_x, int32_t off_y) { - __asm__ volatile("sll $12, %0, 16" :: "r"(off_x), "r"(off_y) : "$12", "$13"); - __asm__ volatile("sll $13, %1, 16" :: "r"(off_x), "r"(off_y) : "$12", "$13"); - __asm__ volatile("ctc2 $12, $24" :: "r"(off_x), "r"(off_y) : "$12", "$13"); - __asm__ volatile("ctc2 $13, $25" :: "r"(off_x), "r"(off_y) : "$12", "$13"); - } - - void set_geom_screen(int32_t h) { - __asm__ volatile("ctc2 %0, $26" :: "r"(h)); } } } \ No newline at end of file