Skip to content

Commit

Permalink
Updated for the recent changes on math unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane-D committed Feb 27, 2025
1 parent aa066a1 commit 8b33303
Show file tree
Hide file tree
Showing 24 changed files with 149 additions and 129 deletions.
2 changes: 1 addition & 1 deletion inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
* Set it to 1 if you want to enable the TTY text console module (written by Andreas Dietrich).<br>
* It consume about 34 bytes of memory when enabled.
*/
#define MODULE_CONSOLE 0
#define MODULE_CONSOLE 1


#endif // _CONFIG_
28 changes: 20 additions & 8 deletions inc/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,32 +252,44 @@ u16 intToHex(u32 value, char *str, u16 minsize);

/**
* \brief
* Convert a fix32 value to string.
* Convert a fix16 value to string.
*
* \param value
* The fix32 value to convert to string.
* The fix16 value to convert to string.
* \param str
* Destination string (it must be large enough to receive result).
* \param numdec
* Number of wanted decimal.
*
* Converts the specified fix32 value to string.<br>
* Converts the specified fix16 value to string.<br>
*/
void fix32ToStr(fix32 value, char *str, u16 numdec);
void fix16ToStr(fix16 value, char *str, u16 numdec);
/**
* \brief
* Convert a fix16 value to string.
* Convert a fix32 value to string.
*
* \param value
* The fix16 value to convert to string.
* The fix32 value to convert to string.
* \param str
* Destination string (it must be large enough to receive result).
* \param numdec
* Number of wanted decimal.
*
* Converts the specified fix16 value to string.<br>
* Converts the specified fix32 value to string.<br>
*/
void fix16ToStr(fix16 value, char *str, u16 numdec);
void fix32ToStr(fix32 value, char *str, u16 numdec);
/**
* \brief
* Same as fix16ToStr(..)
* \see fix16ToStr
*/
void F16_toStr(fix16 value, char *str, u16 numdec);
/**
* \brief
* Same as fix32ToStr(..)
* \see fix32ToStr
*/
void F32_toStr(fix32 value, char *str, u16 numdec);

#endif // _STRING_H_

6 changes: 3 additions & 3 deletions sample/benchmark/src/bg_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,10 @@ static u32 displayResult(u32 op, fix32 time, u16 y)
u32 speed;

fix32ToStr(time, timeStr, 2);
speedOp = intToFix32(op >> 4);
speedOp = FIX32(op >> 4);
// get number of points computed per second
speedOp = fix32Div(speedOp, time);
speed = fix32ToRoundedInt(speedOp << 4);
speedOp = F32_div(speedOp, time);
speed = F32_toRoundedInt(speedOp << 4);
// put it in speedStr
intToStr(speed, speedStr, 1);

Expand Down
24 changes: 12 additions & 12 deletions sample/benchmark/src/bmp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,11 +821,11 @@ static u16 displayResult(u32 op, fix32 time, u16 y)
fix32 speed;

fix32ToStr(time, timeStr, 2);
speed = intToFix32(op);
speed = FIX32(op);
// get speed in op/s
speed = fix32Div(speed, time);
speed = F32_div(speed, time);
// put it in speedStr
intToStr(fix32ToRoundedInt(speed), speedStr, 1);
intToStr(F32_toRoundedInt(speed), speedStr, 1);

strcpy(str, "Elapsed time = ");
strcat(str, timeStr);
Expand All @@ -836,7 +836,7 @@ static u16 displayResult(u32 op, fix32 time, u16 y)
// display test string
VDP_drawText(str, 3, y);

return fix32ToInt(speed);
return F32_toInt(speed);
}

static u16 displayResult2(u32 op, fix32 time, u16 y)
Expand All @@ -847,11 +847,11 @@ static u16 displayResult2(u32 op, fix32 time, u16 y)
fix32 speed;

fix32ToStr(time, timeStr, 2);
speed = intToFix32(op / 100);
speed = FIX32(op / 100);
// get speed in op/s
speed = fix32Div(speed, time);
speed = F32_div(speed, time);
// put it in speedStr
intToStr(fix32ToRoundedInt(speed) * 100, speedStr, 1);
intToStr(F32_toRoundedInt(speed) * 100, speedStr, 1);

strcpy(str, "Elapsed time = ");
strcat(str, timeStr);
Expand All @@ -862,7 +862,7 @@ static u16 displayResult2(u32 op, fix32 time, u16 y)
// display test string
VDP_drawText(str, 3, y);

return fix32ToInt(speed);
return F32_toInt(speed);
}

static u16 displayResult3(u32 op, fix32 time, u16 y)
Expand All @@ -873,11 +873,11 @@ static u16 displayResult3(u32 op, fix32 time, u16 y)
fix32 speed;

fix32ToStr(time, timeStr, 2);
speed = intToFix32(op / 10);
speed = FIX32(op / 10);
// get speed in op/s
speed = fix32Div(speed, time);
speed = F32_div(speed, time);
// put it in speedStr
intToStr(fix32ToRoundedInt(speed) * 10, speedStr, 1);
intToStr(F32_toRoundedInt(speed) * 10, speedStr, 1);

strcpy(str, "Elapsed time = ");
strcat(str, timeStr);
Expand All @@ -888,5 +888,5 @@ static u16 displayResult3(u32 op, fix32 time, u16 y)
// display test string
VDP_drawText(str, 3, y);

return fix32ToInt(speed);
return F32_toInt(speed);
}
16 changes: 8 additions & 8 deletions sample/benchmark/src/math_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ u16 executeMathsAdvTest(u16 *scores)
M3D_setTransform(&transformation, &translation, &rotation);
M3D_resetTransform(&transformation);
M3D_setTranslation(&transformation, FIX16(1.5), FIX16(-2.5), FIX16(20));
M3D_setRotation(&transformation, FIX16(1.5), FIX16(2.4), FIX16(3.2));
M3D_setRotation(&transformation, 15, 65, 230);

VDP_drawText("3D Transform for 1024 points (x50)", 2, y++);
i = 50;
Expand Down Expand Up @@ -650,9 +650,9 @@ static u32 displayResult(u32 op, fix32 time, u16 y, u32 dirty)
fix32 speedKop;

fix32ToStr(time, timeStr, 2);
speedKop = intToFix32(op / 4096);
speedKop = FIX32(op / 4096);
// get speed in Kb/s
speedKop = fix32Div(speedKop, time);
speedKop = F32_div(speedKop, time);
// put it in speedStr
fix32ToStr(speedKop * 4, speedStr, 2);

Expand All @@ -665,7 +665,7 @@ static u32 displayResult(u32 op, fix32 time, u16 y, u32 dirty)
// display test string
VDP_drawText(str, 3, y);

return fix32ToInt(speedKop);
return F32_toInt(speedKop);
}

static u32 displayResult3D(u32 op, fix32 time, u16 y, u32 dirty)
Expand All @@ -676,11 +676,11 @@ static u32 displayResult3D(u32 op, fix32 time, u16 y, u32 dirty)
fix32 speedKop;

fix32ToStr(time, timeStr, 2);
speedKop = intToFix32(op / 100);
speedKop = FIX32(op / 100);
// get number of points computed per second
speedKop = fix32Div(speedKop, time);
speedKop = F32_div(speedKop, time);
// put it in speedStr
intToStr(fix32ToRoundedInt(speedKop) * 100, speedStr, 1);
intToStr(F32_toRoundedInt(speedKop) * 100, speedStr, 1);

strcpy(str, "Elapsed time = ");
strcat(str, timeStr);
Expand All @@ -691,5 +691,5 @@ static u32 displayResult3D(u32 op, fix32 time, u16 y, u32 dirty)
// display test string
VDP_drawText(str, 3, y);

return fix32ToInt(speedKop);
return F32_toInt(speedKop);
}
14 changes: 7 additions & 7 deletions sample/benchmark/src/mem_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,9 @@ static u32 displayResult(u32 bytes, fix32 time, u16 y)
fix32 speedKb;

fix32ToStr(time, timeStr, 2);
speedKb = intToFix32(bytes / 4096);
speedKb = FIX32(bytes / 4096);
// get speed in Kb/s
speedKb = fix32Div(speedKb, time);
speedKb = F32_div(speedKb, time);
// put it in speedStr
fix32ToStr(speedKb * 4, speedStr, 2);

Expand All @@ -822,7 +822,7 @@ static u32 displayResult(u32 bytes, fix32 time, u16 y)
// display test string
VDP_drawText(str, 3, y);

return fix32ToInt(speedKb);
return F32_toInt(speedKb);
}

static u32 displayResultAlloc(u32 nb, fix32 time, u16 y)
Expand All @@ -833,11 +833,11 @@ static u32 displayResultAlloc(u32 nb, fix32 time, u16 y)
fix32 speed;

fix32ToStr(time, timeStr, 2);
speed = intToFix32(nb / 100);
speed = FIX32(nb / 100);
// get speed in op/s
speed = fix32Div(speed, time);
speed = F32_div(speed, time);
// put it in speedStr
intToStr(fix32ToInt(speed) * 100, speedStr, 1);
intToStr(F32_toInt(speed) * 100, speedStr, 1);

strcpy(str, "Elapsed time = ");
strcat(str, timeStr);
Expand All @@ -848,5 +848,5 @@ static u32 displayResultAlloc(u32 nb, fix32 time, u16 y)
// display test string
VDP_drawText(str, 3, y);

return fix32ToInt(speed);
return F32_toInt(speed);
}
6 changes: 3 additions & 3 deletions sample/benchmark/src/spr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ static void updatePartic(u16 num, u16 preloadedTiles, u16 realloc)
if (o->timer >= 10) o->timer = 0;

// update sprite position
SPR_setPosition(s, fix16ToInt(o->pos.x), 224 - fix16ToInt(o->pos.y));
SPR_setPosition(s, F16_toInt(o->pos.x), 224 - F16_toInt(o->pos.y));
// animate (30 FPS)
if (o->timer & 1)
{
Expand Down Expand Up @@ -1259,9 +1259,9 @@ static void updatePos(u16 num)
#endif

// set sprite position
SPR_setPosition(s, fix16ToInt(o->pos.x), fix16ToInt(o->pos.y));
SPR_setPosition(s, F16_toInt(o->pos.x), F16_toInt(o->pos.y));
// set sprite depth
SPR_setDepth(s, (300 - fix16ToInt(o->pos.y)) >> 4);
SPR_setDepth(s, (300 - F16_toInt(o->pos.y)) >> 4);

sprite++;
}
Expand Down
34 changes: 16 additions & 18 deletions sample/bitmap/cube 3D/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

#define MAX_POINTS 256


extern Mat3D_f16 MatInv;
extern Mat3D_f16 Mat;

//Vect3D_f16 pts_3D[MAX_POINTS];
//Vect2D_s16 pts_2D[MAX_POINTS];
//Vect3D_f16 vtab_3D[MAX_POINTS];
//Vect2D_s16 vtab_2D[MAX_POINTS];

Vect3D_f16* pts_3D;
Vect2D_s16* pts_2D;
Vect3D_f16* vtab_3D;
Expand All @@ -23,9 +14,7 @@ Translation3D translation;
Transformation3D transformation;

Vect3D_f16 rotstep;

fix16 camdist;

u16 flatDrawing;


Expand Down Expand Up @@ -67,6 +56,8 @@ int main()
M3D_setLightEnabled(1);
M3D_setLightXYZ(FIX16(0.9), FIX16(0.9), FIX16(-0.9));

memset(&transformation, 0, sizeof(transformation));
memset(&rotstep, 0, sizeof(rotstep));
// allocate translation and rotation structure
M3D_setTransform(&transformation, &translation, &rotation);
M3D_setTranslation(&transformation, FIX16(0), FIX16(0), FIX16(20));
Expand All @@ -87,6 +78,13 @@ int main()
rotation.x += rotstep.x;
rotation.y += rotstep.y;
rotation.z += rotstep.z;
// clamp angle to 0-360
if (rotation.x >= FIX16(360.0)) rotation.x -= FIX16(360.0);
if (rotation.y >= FIX16(360.0)) rotation.y -= FIX16(360.0);
if (rotation.z >= FIX16(360.0)) rotation.z -= FIX16(360.0);
if (rotation.x < 0) rotation.x += FIX16(360.0);
if (rotation.y < 0) rotation.y += FIX16(360.0);
if (rotation.z < 0) rotation.z += FIX16(360.0);
transformation.rebuildMat = 1;

updatePointsPos();
Expand Down Expand Up @@ -143,9 +141,9 @@ void drawPoints(u8 col)
*pt_dst++ = pts_2D[*poly_ind++];
*pt_dst = pts_2D[*poly_ind++];

dp = fix16Mul(transformation.lightInv.x, norm->x) +
fix16Mul(transformation.lightInv.y, norm->y) +
fix16Mul(transformation.lightInv.z, norm->z);
dp = F16_mul(transformation.lightInv.x, norm->x) +
F16_mul(transformation.lightInv.y, norm->y) +
F16_mul(transformation.lightInv.z, norm->z);
norm++;

if (dp > 0) col += (dp >> (FIX16_FRAC_BITS - 2));
Expand Down Expand Up @@ -183,25 +181,25 @@ void doActionJoy(u8 numjoy, u16 value)
if (value & BUTTON_UP)
{
if (value & BUTTON_A) translation.y += FIX16(0.2);
else rotstep.x += FIX16(0.05);
else rotstep.x += FIX16(0.2);
}

if (value & BUTTON_DOWN)
{
if (value & BUTTON_A) translation.y -= FIX16(0.2);
else rotstep.x -= FIX16(0.05);
else rotstep.x -= FIX16(0.2);
}

if (value & BUTTON_LEFT)
{
if (value & BUTTON_A) translation.x -= FIX16(0.2);
else rotstep.y += FIX16(0.05);
else rotstep.y += FIX16(0.2);
}

if (value & BUTTON_RIGHT)
{
if (value & BUTTON_A) translation.x += FIX16(0.2);
else rotstep.y -= FIX16(0.05);
else rotstep.y -= FIX16(0.2);
}

if (value & BUTTON_Y)
Expand Down
Loading

0 comments on commit 8b33303

Please sign in to comment.