Move GTE functions into library

This commit is contained in:
2024-01-28 21:38:07 -05:00
parent 48a76fd3a4
commit 6f48d1ff6c
4 changed files with 94 additions and 62 deletions

View File

@@ -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");
}

View File

@@ -0,0 +1,69 @@
#include "gte_instruction.hpp"
#include <PSX/GTE/gte.hpp>
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));
}
}
}

View File

@@ -0,0 +1,78 @@
#pragma once
#include <PSX/GTE/gte_types.hpp>
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
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
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 MATRIX to universal register
static __always_inline void ldclmv(const MATRIX& matrix, size_t col) {
__asm__ volatile("lhu $12, 0(%0)" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14");
__asm__ volatile("lhu $13, 6(%0)" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14");
__asm__ volatile("lhu $14, 12(%0)" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14");
__asm__ volatile("mtc2 $12, $9" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14");
__asm__ volatile("mtc2 $13, $10" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14");
__asm__ volatile("mtc2 $14, $11" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14");
}
// 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 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<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory");
__asm__ volatile("mfc2 $13, $10" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory");
__asm__ volatile("mfc2 $14, $11" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory");
__asm__ volatile("sh $12, 0(%0)" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory");
__asm__ volatile("sh $13, 6(%0)" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14", "memory");
__asm__ volatile("sh $14, 12(%0)" :: "r"(reinterpret_cast<uintptr_t>(&matrix) + (col << 1)) : "$12", "$13", "$14", "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");
}
/*
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");
}
}
}