From 725241d80b5efcffcf644af69a3576a0263cafc0 Mon Sep 17 00:00:00 2001 From: jaby Date: Thu, 1 Feb 2024 18:54:45 -0500 Subject: [PATCH] Support getting and setting of matrix --- include/PSX/GTE/gte.hpp | 76 ++++++++++++++++++----- src/Library/src/BootLoader/start_boot.cpp | 35 +++++++++++ src/Library/src/GTE/gte.cpp | 26 ++++++++ 3 files changed, 120 insertions(+), 17 deletions(-) diff --git a/include/PSX/GTE/gte.hpp b/include/PSX/GTE/gte.hpp index f275d6b6..0b434bd3 100644 --- a/include/PSX/GTE/gte.hpp +++ b/include/PSX/GTE/gte.hpp @@ -41,24 +41,23 @@ namespace JabyEngine { __asm__ volatile("ctc2 $14, $4" :: "r"(&matrix) : "$12", "$13", "$14"); } - static MATRIX get_rot_matrix() { - MATRIX matrix = {0}; + /* + GetRotMatrix - // TODO: v why is this needed? - // TODO: v what is this? v - // TODO: v v what exactly is this register? - __asm__ volatile("cfc2 $12, $0" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("cfc2 $13, $1" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("sw $12, 0(%0)" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("sw $13, 4(%0)" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("cfc2 $12, $2" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("cfc2 $13, $3" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("cfc2 $14, $4" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("sw $12, 8(%0)" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("sw $13, 12(%0)" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - __asm__ volatile("sw $14, 16(%0)" :: "r"(&matrix) : "$12", "$13", "$14", "memory"); - - return matrix; + Writes the current 3x3 constant rotation matrix to matrix + (This doesn't require us to use memory clobber) + */ + static void get_rot_matrix(MATRIX &matrix) { + __asm__ volatile("cfc2 $12, $0" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("cfc2 $13, $1" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("sw $12, 0(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("sw $13, 4(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("cfc2 $12, $2" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("cfc2 $13, $3" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("cfc2 $14, $4" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("sw $12, 8(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("sw $13, 12(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("sw $14, 16(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); } /* @@ -75,6 +74,21 @@ namespace JabyEngine { __asm__ volatile("ctc2 $14, $7" :: "r"(&matrix) : "$12", "$13", "$14"); } + /* + GetTransMatrix + + Writes the current constant parallel transfer vector to matrix + (This doesn't require us to use memory clobber) + */ + static void get_trans_matrix(MATRIX& matrix) { + __asm__ volatile("cfc2 $14, $7" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("cfc2 $13, $6" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("sw $14, 28(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("cfc2 $12, $5" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("sw $13, 24(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + __asm__ volatile("sw $12, 20(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); + } + /* MulMatrix0 @@ -88,6 +102,34 @@ namespace JabyEngine { */ MATRIX& multiply_matrix(const MATRIX& m0, const MATRIX& m1, MATRIX& result); + /* + matrix: first input + + Sets the 3x3 constant rotation matrix and the parallel transfer vector from input + */ + void set_matrix(const MATRIX& matrix); + + /* + returns: current matrix + + Gets the current 3x3 constant rotation matrix and the parallel transfer vector + */ + MATRIX get_matrix(); + + /* + matrix: optional input + + Pushes the current matrix (rotation and parallel) to an internal stack + Optional: replaces current matrix (rotation and parallel) with input + */ + void push_matrix(); + void push_matrix(const MATRIX& matrix); + + /* + Restores the previous stored matrix (rotation and parallel) + */ + void pop_matrix(); + /* SetGeomOffset(ofx,ofy) diff --git a/src/Library/src/BootLoader/start_boot.cpp b/src/Library/src/BootLoader/start_boot.cpp index 5eb5465f..802f8fd3 100644 --- a/src/Library/src/BootLoader/start_boot.cpp +++ b/src/Library/src/BootLoader/start_boot.cpp @@ -104,10 +104,45 @@ namespace JabyEngine { printf("Cos of %i = %i (%i)\n", angle, cos_value, wanna_be_float(cos_value)); }; + static const auto print_matrix = [](const GTE::MATRIX& matrix) { + printf("{%i, %i, %i}\n", matrix.rot[0][0], matrix.rot[0][1], matrix.rot[0][2]); + printf("{%i, %i, %i}\n", matrix.rot[1][0], matrix.rot[1][1], matrix.rot[1][2]); + printf("{%i, %i, %i}\n", matrix.rot[2][0], matrix.rot[2][1], matrix.rot[2][2]); + printf("{%i, %i, %i}\n", matrix.trans[0], matrix.trans[1], matrix.trans[2]); + }; + display_stuff(25); display_stuff(45); display_stuff(75); display_stuff(90); + + auto rot_mat = []() -> GTE::MATRIX { + auto rot = GTE::MATRIX::identity(); + + rot.rot[0][0] = 123; + rot.rot[0][1] = 456; + rot.rot[0][2] = 789; + + rot.rot[1][0] = 1123; + rot.rot[1][1] = 1456; + rot.rot[1][2] = 1789; + + rot.rot[2][0] = 2123; + rot.rot[2][1] = 2456; + rot.rot[2][2] = 2789; + + rot.trans[0] = 9; + rot.trans[1] = 8; + rot.trans[2] = 7; + return rot; + }(); + + print_matrix(rot_mat); + printf("---\n"); + GTE::set_matrix(rot_mat); + rot_mat = GTE::get_matrix(); + print_matrix(rot_mat); + printf("---\n"); } void start() { diff --git a/src/Library/src/GTE/gte.cpp b/src/Library/src/GTE/gte.cpp index f477a26b..44aebada 100644 --- a/src/Library/src/GTE/gte.cpp +++ b/src/Library/src/GTE/gte.cpp @@ -43,5 +43,31 @@ namespace JabyEngine { return result; } + + void set_matrix(const MATRIX& matrix) { + set_rot_matrix(matrix); + set_trans_matrix(matrix); + } + + MATRIX get_matrix() { + MATRIX matrix; + + get_rot_matrix(matrix); + get_trans_matrix(matrix); + return matrix; + } + + void push_matrix() { + // FIXME: Implement this + } + + void push_matrix(const MATRIX& matrix) { + push_matrix(); + set_matrix(matrix); + } + + void pop_matrix() { + // FIXME: Implement this + } } } \ No newline at end of file