Alchemy
Loading...
Searching...
No Matches

text.c

Go to the documentation of this file.
1/// @file
2/// Number text, the debug text screen and the window graphics.
3/// Part of runtime.c, the runtime object, compiled only through it.
4
6
7// Debug text: a 32x20-character screen of window font tiles in BG0, written
8// from gDebugTextCursor onwards; characters are tile numbers in palette 15.
9
10/// Writes `value` as eight hexadecimal digits to the number buffer. Called by
11/// DebugText_PrintHex.
13 u8 *buffer = gNumberTextBuffer;
14 const u8 *digits = gText_HexDigits;
15 s32 index = 7;
16
17 do {
18 buffer[index] = digits[value & 0xF];
19 value >>= 4;
20 index--;
21 } while (index >= 0);
22
23 {
24 u8 *terminator = gNumberTextBuffer;
25 terminator[8] = 0;
26 }
27}
28
29/// Writes `number` to the number buffer as eleven characters: spaces, a sign
30/// (a space or '-') and its decimal digits, right-aligned. Called by
31/// DebugText_PrintDecimal and the Djinn preview's number text.
33 u32 *placeValues;
34 s8 *output;
35 s32 placesLeft;
36 u32 placeValue;
37 s32 digit;
38#ifdef UBFIX
39 u32 magnitude;
40#else
41 s32 magnitude;
42#endif
43 s8 sign;
44
45 digit = number;
46 magnitude = digit;
47 placeValues = (u32 *)gText_PowersOfTen;
48 sign = ' ';
49 output = gNumberTextBuffer;
50 if (number < 0) {
51#ifdef UBFIX
52 magnitude = 0U - magnitude;
53#else
54 // UB: negating INT_MIN cannot be represented as a signed s32.
55 magnitude = -magnitude;
56#endif
57 sign = '-';
58 }
59 // Pad with a space for each power of ten above the number
60 placeValue = *placeValues++;
61 placesLeft = 9;
62 if (magnitude < placeValue) {
63 do {
64 placesLeft -= 1;
65 *output++ = ' ';
66 if (placesLeft == 0)
67 break;
68 placeValue = *placeValues++;
69 } while (magnitude < placeValue);
70 }
71 *output++ = sign;
72 // Then one digit for each remaining power of ten
73 placeValues -= 1;
74 if (placesLeft != 0) {
75 do {
76 placeValue = *placeValues++;
77 digit = magnitude / placeValue;
78 *output++ = digit + '0';
79 magnitude -= digit * placeValue;
80 placesLeft -= 1;
81 } while (placesLeft != 0);
82 }
83 output[0] = magnitude + '0';
84 output[1] = 0;
85}
86
87/// Writes `count` blank characters at the debug text cursor and moves it on.
88/// Reached through the far-call table.
90 u16 *dst = gDebugTextCursor;
91 u32 index;
92
93 for (index = 0; index < count; index++)
94 *dst++ = 0xf000;
95 gDebugTextCursor = dst;
96}
97
98#ifdef MODERN
99/// Copies size bytes from source to destination, for the modern build.
100// Camelot's compiler copied the task inline; a modern one calls memcpy, and the
101// game links no C library.
102void *memcpy(void *destination, const void *source, unsigned long size) {
103 u8 *to = destination;
104 const u8 *from = source;
105
106 while (size-- != 0)
107 *to++ = *from++;
108 return destination;
109}
110#endif
111
112/// Prints the string `src` (at most 32 characters) at the debug text cursor,
113/// wrapping from the end of the screen to its start, when debug text is on;
114/// the cursor moves past it. Called by the hex and decimal printers and
115/// through the far-call table.
116void DebugText_Print(const u8 *src) {
117 if (gDebugTextEnabled != 0) {
118 u32 addr = ((u32)&gDebugTextCursor);
119 u32 c = *src;
120 u16 *dst = *(u16 **)addr;
121 u32 cnt = 0;
122 src++;
123
124 if (c != 0) {
125 u32 mask = 0xf000;
126 addr = (u32)(BG_SCREEN_BLOCK(4) + 0x500);
127 do {
128 *dst++ = c | mask;
129 if (dst == (u16 *)addr)
130 dst = (u16 *)BG_SCREEN_BLOCK(4);
131 cnt++;
132 if (cnt > 31)
133 break;
134 c = *src++;
135 } while (c != 0);
136 addr = ((u32)&gDebugTextCursor);
137 }
138 *(u16 **)addr = dst;
139 }
140}
141
142/// Prints the last `digits` hexadecimal digits of `value` (1 to 8; anything
143/// else prints all 8). Reached through the far-call table.
144void DebugText_PrintHex(u32 value, s32 digits) {
145 s32 n;
146
147 n = digits;
148 if ((n - 1) > 7U) {
149 n = 8;
150 }
152 DebugText_Print((const u8 *)(((u32)&gNumberTextHexEnd) - n));
153}
154
155/// Prints the last `digits` characters of `value` in signed decimal (1 to 10;
156/// anything else prints 10). Reached through the far-call table.
157void DebugText_PrintDecimal(s32 value, s32 digits) {
158 s32 n;
159
160 n = digits;
161 if ((n - 1) > 9U) {
162 n = 10;
163 }
165 DebugText_Print((const u8 *)(((u32)&gNumberTextDecimalEnd) - n));
166}
167
168/// Blanks the debug text screen, homes the cursor and points BG0 at it.
169/// Called by System_Initialize, Game_ResetForNewGame and the debug modes.
170void DebugText_Clear(void) {
171 Dma_Fill32(0xf000f000, BG_SCREEN_BLOCK(4), 0x500);
174}
175
176/// Loads the window font and frame tiles into BG character block 0, the window
177/// palette into BG palette 15 with its gradient colours, and the basic sprite
178/// palettes. Called by System_Initialize and by the modes that replace those
179/// graphics and must put them back (the debug battle, the palette editor).
183 Dma_Set(gUi_WindowPalette, (void *)&BG_PLTT_COLOR(15, 0),
184 DMA_ENABLE << 16 | 0x10, REG_DMA3);
186 Io_Write16(RGB(0, 12, 16), &BG_PLTT_COLOR(15, 4));
187 Io_Write16(RGB(0, 11, 14), &BG_PLTT_COLOR(15, 5));
188 Io_Write16(RGB(0, 10, 12), &BG_PLTT_COLOR(15, 6));
189 Io_Write16(RGB(0, 9, 10), &BG_PLTT_COLOR(15, 7));
190 Io_Write16(RGB(0, 13, 18), &BG_PLTT_COLOR(15, 8));
191 Io_Write16(RGB(0, 14, 20), &BG_PLTT_COLOR(15, 9));
192 Io_Write16(RGB(0, 15, 22), &BG_PLTT_COLOR(15, 10));
194 REG_DMA3);
195}
196
197/// Loads the basic sprite palettes (palettes 0 to 13). Called by
198/// Ui_LoadWindowGraphics and the field object setup.
201 REG_DMA3);
202}
static __inline__ void Dma_Set(const volatile void *source, volatile void *destination, u32 control, volatile u32 *channel)
Starts a DMA transfer: stores the source, destination and control words into channel's registers with...
Definition dma.h:62
#define Dma_Fill32(value, destination, size)
Fills size bytes at destination with the word value by DMA3.
Definition dma.h:30
#define OBJ_PLTT
Definition io_reg.h:159
#define BG_SCREEN_BLOCK(n)
Definition io_reg.h:167
#define RGB(r, g, b)
A 15-bit colour from its red, green and blue levels (0 to 31 each), as the palette stores it: blue in...
Definition io_reg.h:164
#define BG_CHAR_BLOCK(n)
Definition io_reg.h:166
#define PLTT_BACKDROP
Definition io_reg.h:155
#define REG_DMA3
Definition io_reg.h:153
#define BGCNT_SCREEN_BASE(block)
Definition io_reg.h:28
#define DMA_COPY_WORDS(n)
Definition io_reg.h:226
#define BG_PLTT_COLOR(palette, color)
Definition io_reg.h:158
#define DMA_ENABLE
Definition io_reg.h:217
#define REG_BG0CNT
Definition io_reg.h:261
#define Io_Write16(v, reg)
Writes a value to a 16-bit register or memory location.
Definition iowrite.h:14
u8 gNumberTextBuffer[]
const u8 gText_HexDigits[]
Definition numtext.c:5
const s32 gText_PowersOfTen[9]
Place values for decimal display.
Definition numtext.c:9
char ResourceId_WindowTiles
What every part of the runtime object shares: its includes and the library call two parts make.
void * Resource_GetTableEntry(s32 index)
Returns the ROM address of resource index from the resource directory.
Definition system.c:150
const u8 gSystem_BasicColorPalette[]
const u16 gUi_WindowPalette[]
u8 gDebugTextEnabled
u8 gNumberTextHexEnd[]
u16 * gDebugTextCursor
u8 gNumberTextDecimalEnd[]
void DebugText_PrintDecimal(s32 value, s32 digits)
Prints the last digits characters of value in signed decimal (1 to 10; anything else prints 10).
Definition text.c:157
void DebugText_PrintBlanks(u32 count)
Writes count blank characters at the debug text cursor and moves it on.
Definition text.c:89
void Palette_LoadBasicObjColors(void)
Loads the basic sprite palettes (palettes 0 to 13).
Definition text.c:199
void DebugText_PrintHex(u32 value, s32 digits)
Prints the last digits hexadecimal digits of value (1 to 8; anything else prints all 8).
Definition text.c:144
void NumberText_FormatHex(u32 value)
Writes value as eight hexadecimal digits to the number buffer.
Definition text.c:12
void DebugText_Print(const u8 *src)
Prints the string src (at most 32 characters) at the debug text cursor, wrapping from the end of the ...
Definition text.c:116
void Ui_LoadWindowGraphics(void)
Loads the window font and frame tiles into BG character block 0, the window palette into BG palette 1...
Definition text.c:180
void DebugText_Clear(void)
Blanks the debug text screen, homes the cursor and points BG0 at it.
Definition text.c:170
void NumberText_FormatDecimal(s32 number)
Writes number to the number buffer as eleven characters: spaces, a sign (a space or '-') and its deci...
Definition text.c:32
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
signed char s8
Definition types.h:10
unsigned short u16
Definition types.h:13
unsigned char u8
Definition types.h:11
void * memcpy(void *destination, const void *source, unsigned long size)