3
3
#include "link.h"
4
4
#include "protocol.h"
5
5
6
- #ifdef REGION_JAP
6
+ // Enable this to simulate failed scans with ⮜ and successful scans with ➤
7
+ #define DEBUG_MODE 0
8
+
9
+ // Enable this to display error codes
10
+ #define DISPLAY_ERROR_CODES 1
11
+
7
12
// Japanese strings are encoded as Shift-JIS byte arrays.
13
+ #ifdef REGION_JAP
14
+ #if DISPLAY_ERROR_CODES == 1
15
+ /* "0123456789" */
16
+ const u8 MSG_NUMBERS [] = {0x82 , 0x4f , 0x82 , 0x50 , 0x82 , 0x51 , 0x82 ,
17
+ 0x52 , 0x82 , 0x53 , 0x82 , 0x54 , 0x82 , 0x55 ,
18
+ 0x82 , 0x56 , 0x82 , 0x57 , 0x82 , 0x58 , 0x00 };
19
+ #endif
20
+
8
21
#ifdef LANGUAGE_ENG
9
22
const u8 MSG_WAITING_GAME [] = {
10
23
0x82 , 0x76 , 0x82 , 0x60 , 0x82 , 0x68 , 0x82 , 0x73 , 0x82 , 0x68 , 0x82 ,
@@ -62,8 +75,42 @@ extern int __end[];
62
75
ERAPI_HANDLE_REGION region ;
63
76
u8 card [CARD_BUFFER_SIZE ];
64
77
const u16 palette [] = {0x0000 , 0xFFFF };
78
+ u32 previousKeys = 0 ;
79
+
80
+ #if DISPLAY_ERROR_CODES == 1
81
+ void codeToString (char * buf , int num ) {
82
+ #ifdef REGION_JAP
83
+ const u8 * digits = MSG_NUMBERS ;
84
+ int temp [5 ];
85
+ int len = 0 ;
86
+ do {
87
+ temp [len ++ ] = num % 10 ;
88
+ num /= 10 ;
89
+ } while (num && len < 5 );
90
+
91
+ u8 * p = (u8 * )buf ;
92
+ for (int i = len - 1 ; i >= 0 ; -- i ) {
93
+ int idx = temp [i ] * 2 ;
94
+ * p ++ = digits [idx ];
95
+ * p ++ = digits [idx + 1 ];
96
+ }
97
+ * p = 0 ;
98
+ #else
99
+ char temp [6 ];
100
+ int pos = 0 ;
101
+ do {
102
+ temp [pos ++ ] = '0' + (num % 10 );
103
+ num /= 10 ;
104
+ } while (num && pos < 5 );
105
+ int j = 0 ;
106
+ while (pos )
107
+ buf [j ++ ] = temp [-- pos ];
108
+ buf [j ] = '\0' ;
109
+ #endif
110
+ }
111
+ #endif
65
112
66
- void print (const char * text );
113
+ void print (const char * text , bool canCancel );
67
114
bool cancel ();
68
115
void reset ();
69
116
@@ -85,14 +132,13 @@ int main() {
85
132
86
133
// loop
87
134
while (1 ) {
88
- if (cancel ())
89
- break ;
135
+ u32 errorCode = 0 ;
90
136
91
137
// init loader
92
138
reset ();
93
139
94
140
// "Waiting for game..."
95
- print (MSG_WAITING_GAME );
141
+ print (MSG_WAITING_GAME , false );
96
142
97
143
// handshake with game
98
144
if (!sendAndExpect (HANDSHAKE_1 , HANDSHAKE_1 , cancel ))
@@ -104,54 +150,91 @@ int main() {
104
150
105
151
// wait for card request
106
152
u16 cardRequest = sendAndReceiveExcept (HANDSHAKE_3 , HANDSHAKE_3 , cancel );
107
- if (cardRequest != GAME_REQUEST )
153
+ if (cardRequest != GAME_REQUEST ) {
154
+ errorCode = 1 ;
108
155
goto error ;
156
+ }
109
157
110
158
// confirm card request
111
159
if (!sendAndExpect (GAME_ANIMATING , EREADER_ANIMATING , cancel ))
112
160
continue ;
113
161
if (!send (EREADER_ANIMATING , cancel ))
114
162
continue ;
115
163
116
- // "Scan a card!"
117
- print (MSG_SCAN_CARD );
118
-
119
164
// scan card
120
- if (cancel ())
121
- goto abort ;
122
- if (! sendAndExpect ( EREADER_READY , GAME_READY , cancel ))
123
- goto abort ;
165
+ if (! sendAndExpect ( EREADER_READY , GAME_READY , cancel )) {
166
+ errorCode = 2 ;
167
+ goto error ;
168
+ }
124
169
170
+ // "Scan a card!"
171
+ print (MSG_SCAN_CARD , false);
172
+
173
+ #if DEBUG_MODE == 1
174
+ u32 resultCode = 0 ;
175
+ while (true) {
176
+ u32 debugKeys = ERAPI_GetKeyStateRaw ();
177
+ if ((debugKeys & ERAPI_KEY_LEFT ) != 0 ) {
178
+ resultCode = SCAN_SUCCESS - 1 ;
179
+ break ;
180
+ }
181
+ if ((debugKeys & ERAPI_KEY_RIGHT ) != 0 ) {
182
+ resultCode = SCAN_SUCCESS ;
183
+ const char msg [] = "HelloWorld" ;
184
+ const u32 msgLen = sizeof (msg ) - 1 ;
185
+ const u32 byteCount = CARD_BUFFER_SIZE - CARD_OFFSET ;
186
+ for (u32 i = 0 ; i < byteCount ; i ++ )
187
+ card [CARD_OFFSET + i ] = i == byteCount - 1 ? '!' : msg [i % msgLen ];
188
+ break ;
189
+ }
190
+ }
191
+ #else
125
192
u32 resultCode = ERAPI_ScanDotCode ((u32 )card );
126
- if (resultCode != SCAN_SUCCESS )
193
+ #endif
194
+
195
+ if (resultCode != SCAN_SUCCESS ) {
196
+ errorCode = 3 ;
127
197
goto error ;
198
+ }
128
199
129
200
// "Transferring..."
130
- print (MSG_TRANSFERRING );
201
+ print (MSG_TRANSFERRING , true );
131
202
132
203
// transfer start
133
- if (!sendAndExpect (EREADER_SEND_READY , GAME_RECEIVE_READY , cancel ))
204
+ if (!sendAndExpect (EREADER_SEND_READY , GAME_RECEIVE_READY , cancel )) {
205
+ errorCode = 4 ;
134
206
goto error ;
135
- if (!send (EREADER_SEND_START , cancel ))
207
+ }
208
+ if (!send (EREADER_SEND_START , cancel )) {
209
+ errorCode = 5 ;
136
210
goto error ;
211
+ }
137
212
138
213
// transfer
139
214
u32 checksum = 0 ;
140
215
for (u32 o = CARD_OFFSET ; o < CARD_SIZE ; o += 2 ) {
141
216
u16 block = * (u16 * )(card + o );
142
- if (!send (block , cancel ))
217
+ if (!send (block , cancel )) {
218
+ errorCode = 6 ;
143
219
goto error ;
220
+ }
144
221
checksum += block ;
145
222
}
146
- if (!send (checksum & 0xffff , cancel ))
223
+ if (!send (checksum & 0xffff , cancel )) {
224
+ errorCode = 7 ;
147
225
goto error ;
148
- if (!send (checksum >> 16 , cancel ))
226
+ }
227
+ if (!send (checksum >> 16 , cancel )) {
228
+ errorCode = 8 ;
149
229
goto error ;
150
- if (!send (EREADER_SEND_END , cancel ))
230
+ }
231
+ if (!send (EREADER_SEND_END , cancel )) {
232
+ errorCode = 9 ;
151
233
goto error ;
234
+ }
152
235
153
236
// "Card sent!"
154
- print (MSG_CARD_SENT );
237
+ print (MSG_CARD_SENT , false );
155
238
for (u32 i = 0 ; i < POST_TRANSFER_WAIT ; i ++ )
156
239
ERAPI_RenderFrame (1 );
157
240
@@ -168,7 +251,13 @@ int main() {
168
251
// "Error!"
169
252
ERAPI_ClearRegion (region );
170
253
ERAPI_DrawText (region , 0 , 0 , MSG_ERROR );
254
+ #if DISPLAY_ERROR_CODES == 1
255
+ char errorCodeStr [11 ];
256
+ codeToString (errorCodeStr , errorCode );
257
+ ERAPI_DrawText (region , 0 , 16 , errorCodeStr );
258
+ #else
171
259
ERAPI_DrawText (region , 0 , 16 , MSG_WAITING_GAME );
260
+ #endif
172
261
ERAPI_RenderFrame (1 );
173
262
174
263
send (EREADER_CANCEL , cancel );
@@ -183,16 +272,20 @@ int main() {
183
272
return ERAPI_EXIT_TO_MENU ;
184
273
}
185
274
186
- void print (const char * text ) {
275
+ void print (const char * text , bool canCancel ) {
187
276
ERAPI_ClearRegion (region );
188
277
ERAPI_DrawText (region , 0 , 0 , text );
189
- ERAPI_DrawText (region , 0 , 16 , MSG_PRESS_B_CANCEL );
278
+ if (canCancel )
279
+ ERAPI_DrawText (region , 0 , 16 , MSG_PRESS_B_CANCEL );
190
280
ERAPI_RenderFrame (1 );
191
281
}
192
282
193
283
bool cancel () {
194
284
u32 keys = ERAPI_GetKeyStateRaw ();
195
- return (keys & ERAPI_KEY_B ) != 0 ;
285
+ bool isPressed =
286
+ (previousKeys & ERAPI_KEY_B ) == 0 && (keys & ERAPI_KEY_B ) != 0 ;
287
+ previousKeys = keys ;
288
+ return isPressed ;
196
289
}
197
290
198
291
void reset () {
0 commit comments