Skip to content

Commit 2bac65f

Browse files
committed
added sprintf
1 parent b7de6bf commit 2bac65f

File tree

5 files changed

+172
-19
lines changed

5 files changed

+172
-19
lines changed

src/native_functions.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,21 @@
7272
/** uint32_t strtoul(const char* str, char** endptr, int base) */
7373
#define STR_TO_UL_FPTR VOID_PTR(0x80325b24)
7474

75-
/** char* itoa(int32_t value, char* str, int base) */
76-
#define ITOA_FPTR VOID_PTR(0x803250c0)
75+
/************************** print.h ***************************/
76+
77+
/** int sprintf(char* str, const char* format, ...) */
78+
#define SPRINTF_FPTR VOID_PTR(0x80323cf4)
7779

7880
/************************** math.h ****************************/
7981

8082
/** float sin(float x) */
81-
#define SIN_FPTR VOID_PTR(0x80326220)
83+
#define SIN_FPTR VOID_PTR(0x80326220)
8284

8385
/** float cos(float x) */
84-
#define COS_FPTR VOID_PTR(0x80326200)
86+
#define COS_FPTR VOID_PTR(0x80326200)
8587

8688
/** float tan(float x) */
87-
#define TAN_FPTR VOID_PTR(0x803261bc)
89+
#define TAN_FPTR VOID_PTR(0x803261bc)
8890

8991
/** float acos(float x) */
9092
#define ACOS_FPTR VOID_PTR(0x80022d1c)
@@ -110,5 +112,19 @@
110112
/** float recipSqrt(float x) */
111113
#define RECIP_SQRT_FPTR VOID_PTR(0x80022df8);
112114

115+
/************************** profile.h *************************/
116+
117+
/** u32 OSGetTime() */
118+
#define OS_GET_TIME_FPTR VOID_PTR(0x8034c3f0)
119+
120+
/** u32 OSGetTick() */
121+
#define OS_GET_TICK_FPTR VOID_PTR(0x8034c408)
122+
123+
/** u32 OSGetSystemTime() */
124+
#define OS_GET_SYSTEM_TIME_FPTR VOID_PTR(0x8034c410)
125+
126+
/** u32 OSGetTick() */
127+
#define OS_TIME_TO_SYSTEM_TIME_FPTR VOID_PTR(0x8034c474)
128+
113129
#endif // #ifdef PAL
114130
#endif // #ifndef MML_NATIVE_FUNCTIONS_H

src/print.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#include "math.h"
55
#include "gctypes.h"
66
#include "error.h"
7+
#include "native_functions.h"
8+
9+
//#include <stdarg.h>
10+
11+
int (*sprintf)(char* str, const char* format, ...) = SPRINTF_FPTR;
712

813
#define LINE_SIZE 60
914
#define MAX_LINES 27
@@ -105,19 +110,20 @@ void print(const char* str)
105110
memcpy(menu + numLines, &tempSlot, sizeof(DebugMenuSlot));
106111
}
107112

108-
void printInt(const char* str, u32 n)
109-
{
110-
char buffer[128];
111-
char numString[16];
112-
113-
memset(buffer, 0, sizeof(buffer));
114-
memset(numString, 0, sizeof(numString));
113+
/*
114+
int (*vsprintf)(char * s, const char * format, va_list arg)
115+
= (void*) 0x80323dc8;
115116
116-
strcat(buffer, str);
117-
itoa(n, numString);
118-
strcat(buffer, numString);
117+
int printf(const char* format, ...)
118+
{
119+
char buffer[512];
120+
va_list argptr;
121+
va_start(argptr, format);
122+
int result = vsprintf(buffer, format, argptr);
123+
va_end(argptr);
119124
print(buffer);
120-
}
125+
return result;
126+
}*/
121127

122128
//TODO: solve the array out of bounds error (happens with -O3)
123129
#pragma GCC push_options

src/print.h

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,51 @@
99

1010
#include "gctypes.h"
1111

12+
/** @cond */
13+
extern int (*sprintf)(char* str, const char* format, ...);
14+
15+
/**
16+
* @brief print to the output stream
17+
* @ingroup CallsMalloc
18+
*
19+
* Writes the C string pointed by @p format to the output stream. If
20+
* @p format includes format specifiers (subsequences beginning with %),
21+
* the additional arguments following @p format are formatted and inserted
22+
* in the resulting string replacing their respective specifiers.
23+
*
24+
* @param format - C string
25+
* @return On success, the total number of character written. On
26+
* failure, a negative number is returned
27+
*
28+
* @see http://www.cplusplus.com/reference/cstdio/printf/
29+
*/
30+
//int printf(const char* format, ...);
31+
32+
#if 0
33+
/** @endcond */
34+
35+
/**
36+
* @brief write formatted data to string
37+
*
38+
* Composes a string with the same text that would be printed if @p format
39+
* was used on @c printf, but instead of being printed, the content is
40+
* stored as a C string in the buffer pointed by @p str.
41+
*
42+
* The size of the buffer should be large enough to contain the entire
43+
* resulting string
44+
*
45+
* @param str - Pointer to a buffer where the resulting C string is stored
46+
* @param format - C string
47+
*
48+
* @return On success, the total number of character written. On
49+
* failure, a negative number is returned
50+
*
51+
* @see http://www.cplusplus.com/reference/cstdio/sprintf/
52+
*/
53+
int sprintf(char* str, const char* format, ...);
54+
55+
#endif
56+
1257
/**
1358
* @brief print a string to the output stream
1459
* @ingroup CallsMalloc
@@ -21,7 +66,6 @@
2166
* @return none
2267
*/
2368
void print(const char* str);
24-
void printInt(const char* str, u32 n);
2569

2670
/**
2771
* @brief clear the output stream
@@ -33,13 +77,16 @@ void printInt(const char* str, u32 n);
3377
void clear();
3478

3579
/**
36-
* @brief "throw" an error
80+
* @brief print error message
3781
* @ingroup CallsMalloc
3882
*
39-
* Print an error message and set _errorState to true
83+
* Print an error message
4084
*
4185
* @param errMessage - error message to print
4286
* @return none
87+
*
88+
* @note this will still print the error if the print stream has failed
89+
* to allocate memory
4390
*/
4491
void error(const char* errMessage);
4592

tests/testString.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <math.h>
2+
#include <string.h>
3+
#include <system.h>
4+
#include <print.h>
5+
#include <gctypes.h>
6+
#include <error.h>
7+
8+
static char heap[15000];
9+
static bool initRun = false, testRun = false;
10+
static char str[100] = {0};
11+
12+
static void init()
13+
{
14+
initHeap(heap, heap + sizeof(heap));
15+
}
16+
17+
static void test()
18+
{
19+
sprintf(str, "test of sprintf, int: %d and float: %f", 5, 4.506);
20+
print(str);
21+
}
22+
23+
void _main()
24+
{
25+
if (!initRun) { init(); initRun = true; }
26+
if (!testRun) { test(); testRun = true; }
27+
}
28+

tests/testString.ini

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[Memory Regions]
2+
3+
; regions in memory that are available to inject code into
4+
5+
REGIONS =
6+
7+
8022887c-80228920 ; unused code
8+
8032c848-8032c87c ; unused code
9+
8032dcb0-8032ddb8 ; unused code
10+
8032ed8c-8032ee8c ; unused code
11+
80393a5c-80393c0c ; unused code
12+
803fa3e8-803fc2e8 ; debug menu tables/strings
13+
803fc420-803fdc18 ; debug menu tables/strings
14+
803001dc-80301e40 ; debug menu functions
15+
801910e0-8019af4c ; tournament mode
16+
8040a950-8040bf4c ; unknown
17+
18+
[Static Overwrites]
19+
20+
; debug menu replaces tournament mode - needed for test output
21+
22+
8022d638 = 38000006
23+
24+
[AI]
25+
26+
; all .c source files
27+
28+
SOURCES = tests/testString.c
29+
30+
; any libraries to link against
31+
32+
LIBRARIES = libmml_O1.a
33+
34+
; search directories for .h files (mostly used for libraries)
35+
36+
INCLUDE_PATHS = src
37+
38+
; flags for the compiler
39+
; -fmerge-constants useful when using string literals
40+
41+
COMPILER_FLAGS = -std=c99 -fmerge-constants -fno-builtin -Werror -Wall -Wextra
42+
43+
; flags for the linker
44+
45+
LINKER_FLAGS =
46+
47+
; symbols (functions) that are injected to specific addresses
48+
; specified addresses can not overlap with available memory regions
49+
; format: symbol_name inject_address original_instruction
50+
51+
FIXED_SYMBOLS =
52+
53+
_main 80377998 7ee3bb78
54+
display 801a633c 7c7f1b78
55+
56+

0 commit comments

Comments
 (0)