From 17ef3e91e121c331d18c75a334197a1ed21418a1 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 28 Jan 2024 21:38:07 -0500 Subject: [PATCH] Move GTE functions into library --- include/PSX/GTE/gte.hpp | 63 ++--------------- src/Library/src/BootLoader/start_boot.cpp | 22 ++++-- src/Library/src/GTE/gte.cpp | 69 +++++++++++++++++++ .../Library/src}/GTE/gte_instruction.hpp | 2 +- 4 files changed, 94 insertions(+), 62 deletions(-) create mode 100644 src/Library/src/GTE/gte.cpp rename {include/PSX => src/Library/src}/GTE/gte_instruction.hpp (97%) diff --git a/include/PSX/GTE/gte.hpp b/include/PSX/GTE/gte.hpp index 979fc6b7..1e0a5539 100644 --- a/include/PSX/GTE/gte.hpp +++ b/include/PSX/GTE/gte.hpp @@ -1,5 +1,5 @@ #pragma once -#include "gte_instruction.hpp" +#include "gte_types.hpp" namespace JabyEngine { namespace GTE { @@ -15,12 +15,7 @@ namespace JabyEngine { 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); - } + void rot_trans(const SVECTOR& input, VECTOR& output, int32_t& flag); /* SetRotMatrix @@ -28,32 +23,14 @@ namespace JabyEngine { 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"); - } + void set_rot_matrix(const MATRIX& matrix); /* 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"); - } + void set_trans_matrix(const MATRIX& matrix); /* MulMatrix0 @@ -66,46 +43,20 @@ namespace JabyEngine { Multiplies two matrices m0 and m1. The function destroys the constant rotation matrix */ - static MATRIX& mult_matrix(const MATRIX& m0, const MATRIX& m1, MATRIX& result) { - /* - Jaby: Somehow this code creates stack usage.... Investigate!! - */ - set_rot_matrix(m0); - - ldclmv(m1, 0); - rtir(); - stclmv(result, 0); - - ldclmv(m1, 1); - rtir(); - stclmv(result, 1); - - ldclmv(m1, 2); - rtir(); - stclmv(result, 2); - - return result; - } + MATRIX& mult_matrix(const MATRIX& m0, const MATRIX& m1, MATRIX& result); /* SetGeomOffset(ofx,ofy) Load GTE-offset. */ - static 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_offset(int32_t off_x, int32_t off_y); /* SetGeomScreen(h) Load distance from viewpoint to screen. */ - static void set_geom_screen(int32_t h) { - __asm__ volatile("ctc2 %0, $26" :: "r"(h)); - } + void set_geom_screen(int32_t h); } } \ 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 1c584a56..2f12d2ee 100644 --- a/src/Library/src/BootLoader/start_boot.cpp +++ b/src/Library/src/BootLoader/start_boot.cpp @@ -69,14 +69,26 @@ namespace JabyEngine { } static void test_gte() { - const auto data = GTE::SVECTOR::create(); +#define gte_MulMatrix0(r1,r2,r3) \ + { gte_SetRotMatrix(r1); \ + gte_ldclmv(r2); \ + gte_rtir(); \ + gte_stclmv(r3); \ + gte_ldclmv((char*)r2+2);\ + gte_rtir(); \ + gte_stclmv((char*)r3+2);\ + gte_ldclmv((char*)r2+4);\ + gte_rtir(); \ + gte_stclmv((char*)r3+4); } + + const auto m0 = GTE::MATRIX::identity(); + const auto m1 = GTE::MATRIX::identity(); + auto m2 = GTE::MATRIX::identity(); - asm("# MY PLANSCHI START"); - GTE::ldv0(data); - asm("# MY PLANSCHI END"); + GTE::mult_matrix(m0, m1, m2); asm("# THEIR PLANSCHI START"); - gte_ldv0(&data); + gte_MulMatrix0(&m0, &m1, &m2); asm("# THEIR PLANSCHI END"); } diff --git a/src/Library/src/GTE/gte.cpp b/src/Library/src/GTE/gte.cpp new file mode 100644 index 00000000..49b50e9b --- /dev/null +++ b/src/Library/src/GTE/gte.cpp @@ -0,0 +1,69 @@ +#include "gte_instruction.hpp" +#include + +namespace JabyEngine { + namespace GTE { + void rot_trans(const SVECTOR& input, VECTOR& output, int32_t& flag) { + ldv0(input); + rt(); + stlvnl(output); + 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!! + */ + asm("# MY PLANSCHI START"); + set_rot_matrix(m0); + + ldclmv(m1, 0); + rtir(); + stclmv(result, 0); + + ldclmv(m1, 1); + rtir(); + stclmv(result, 1); + + ldclmv(m1, 2); + rtir(); + 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 diff --git a/include/PSX/GTE/gte_instruction.hpp b/src/Library/src/GTE/gte_instruction.hpp similarity index 97% rename from include/PSX/GTE/gte_instruction.hpp rename to src/Library/src/GTE/gte_instruction.hpp index d21dc3df..eeaafbd6 100644 --- a/include/PSX/GTE/gte_instruction.hpp +++ b/src/Library/src/GTE/gte_instruction.hpp @@ -1,5 +1,5 @@ #pragma once -#include "gte_types.hpp" +#include namespace JabyEngine { namespace GTE {