-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathio.c
More file actions
583 lines (452 loc) · 9.81 KB
/
io.c
File metadata and controls
583 lines (452 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include "genesis.h"
#include "string.h"
#include "vdp.h"
#include "vdp_bg.h"
#include "io.h"
#include "KDebug.h"
extern char buffer[WIDTH_TILES];
extern u32 *bufftop, *curkey;
static int printCol, printRow;
static int height_tiles = HEIGHT_TILES;
static int height_input = HEIGHT_INPUT_NO_OSK;
static u16 inputPlan;
static u16 oskPlan;
static int displayingOsk = 0;
static u16 inputFlags;
static u16 oskFlags;
static int initialized = 0;
void drawOsk();
void initCursorSprite();
void initIO() {
initialized = 0;
//oskX = 0;
//oskY = 0;
inputPlan = APLAN;
inputFlags = TILE_ATTR(PAL0,0,0,0);
oskPlan = BPLAN;
oskFlags = TILE_ATTR(PAL2,0,0,0);
VDP_init();
JOY_init();
initCursorSprite();
printCol = 1;
printRow = 0;
VDP_setTextPlan(inputPlan);
}
void waitForStart() {
JOY_waitPressBtn(JOY_1, BUTTON_START);
}
void clearScreen() {
VDP_clearPlan(inputPlan, 1);
VDP_clearPlan(oskPlan, 1);
if(displayingOsk == 1) {
drawOsk();
}
VDP_waitVSync();
}
void scrollUp() {
}
void scrollDown() {
}
void incPrintRow() {
printRow++;
printCol = 1;
if(printRow >= height_input) {
clearScreen();
printRow = 0;
printCol = 1;
}
}
void incPrintCol() {
printCol++;
if(printCol >= WIDTH_TILES+1) {
incPrintRow();
}
}
static int cursorCnt = 0;
int printCursor() {
int retval = 0;
cursorCnt++;
if(cursorCnt >= 20 && cursorCnt <= 40) {
retval = 1;
}
if(cursorCnt > 40) {
cursorCnt = 0;
}
return retval;
}
char charUpcase(char c) {
if((c >= 97) & (c <= 122)) {
return c-32;
} else {
return c;
}
}
char charDowncase(char c) {
if((c >= 41) & (c <= 90)) {
return c+32;
} else {
return c;
}
}
extern u32 *loc_latest;
static u32 *cur_entry;
static int prefix_cnt, clear_cnt, continue_match;
void resetPrefixSearch() {
prefix_cnt = 0;
clear_cnt = 0;
continue_match = 0;
}
// returns a pointer to the next entry that matches the given prefix
u32* prefixMatch(char* word, u32 wordLen) {
u8 nameLen;
char* entryWord;
if(!continue_match) {
cur_entry = loc_latest;
continue_match = 1;
}
check_match:
nameLen = 0x1F & (*(((u8*)cur_entry)+4));
if(nameLen < wordLen) {
// continue onto next entry
goto get_next;
} else {
entryWord = ((u8*)cur_entry)+5;
// try to match characters
for(int i = 0; i < wordLen; i++) {
if(entryWord[i] != word[i]) {
goto get_next;
}
}
u32* res_entry = cur_entry;
cur_entry = *((u32**)cur_entry);
return res_entry;
}
get_next:
// dereference link
cur_entry = *((u32**)cur_entry);
if(cur_entry) {
goto check_match;
} else {
goto no_match;
}
no_match:
continue_match = 1;
cur_entry = *loc_latest;
return NULL;
}
static int oskX = 0;
static int oskY = 0;
// 4x4 is base tile size
const u32 spriteTiles[4*8] =
{
0x00800000,
0x00880000,
0x00888000,
0x00888800,
0x00888880,
0x00888888,
0x00888800,
0x00800000
};
void initCursorSprite() {
// load into position 1? in vram
// data, vram offset, num_tiles, use_dma
VDP_loadTileData((const u32*)spriteTiles, 1, 4, 0);
oskX = 0;
oskY = 0;
}
static u16 cursorFlags = TILE_ATTR_FULL(PAL0,1,0,0,1);
void drawOskCursor() {
int x = (1+(oskX * 2)) * 8;
int y = (HEIGHT_INPUT_OSK + (oskY * 2)) * 8;
// update position
VDP_setSprite(0, x+4, y+6, SPRITE_SIZE(2,2), cursorFlags, 0);
// draw
VDP_updateSprites();
}
void drawOsk() {
height_input = HEIGHT_INPUT_OSK;
drawOskCursor();
u8 ascii_cnt = CHAR_START;
char str[2] = {' ', '\0'};
for(int y = height_input; y < height_tiles; y+=2) {
for(int x = 1; x < WIDTH_TILES; x+=2) {
str[0] = ascii_cnt++;
VDP_drawTextBG(oskPlan, str, oskFlags, x, y);
if(ascii_cnt >= 128) { return; }
}
}
if(printRow >= height_input) {
clearScreen();
printRow = 0;
printCol = 1;
}
}
void clearOsk() {
VDP_clearPlan(oskPlan, 1);
height_input = HEIGHT_INPUT_NO_OSK;
VDP_setSprite(0, -10, -10, SPRITE_SIZE(2,2), cursorFlags, 0);
VDP_updateSprites();
VDP_waitVSync();
}
static int ptr;
static int minPtr = 0;
static char tmpBuffer[WIDTH_TILES+1];
static int defMode;
void incPtr(){
ptr++;
if (ptr >= WIDTH_TILES) {
ptr = minPtr;
}
}
void decPtr() {
ptr--;
if(ptr < minPtr) {
ptr = WIDTH_TILES-1;
}
}
void shiftBack(int start) {
for(int i = start; i < WIDTH_TILES-1; i++) {
tmpBuffer[i] = tmpBuffer[i+1];
}
}
void shiftForward(int start) {
for(int i = WIDTH_TILES-2; i >= start; i--) {
tmpBuffer[i+1] = tmpBuffer[i];
}
}
void handle_a() {
if(displayingOsk == 1) {
// place selected key in buffer
// increment cursor pointer
u8 ascii_char = CHAR_START+(oskY*19)+oskX;
tmpBuffer[ptr] = ascii_char;
incPtr();
resetPrefixSearch();
} else {
tmpBuffer[ptr] = 'A';
cursorCnt = 20;
resetPrefixSearch();
}
}
void handle_b() {
// same behavior with or without keyboard
// delete character in buffer[ptr-1]
// shift rest of characters left
shiftBack(ptr-1);
char delChar = tmpBuffer[ptr-1];
if(delChar == ':') {
defMode = 0;
} else if (delChar == ';') {
defMode = 1;
}
decPtr();
resetPrefixSearch();
}
void handle_c() {
// same behaviour with or without keyboard
// shift characters starting at buffer[ptr] right
// insert space in buffer[ptr]
shiftForward(ptr);
tmpBuffer[ptr] = ' ';
incPtr();
resetPrefixSearch();
}
void handle_x() {
int cnt = 0;
if(tmpBuffer[ptr] == ' ') {
return; //;continue;
}
// skip spaces
while(1) {
if(tmpBuffer[ptr+cnt] == ' ') {
break;
}
cnt++;
}
if(prefix_cnt == 0) {
resetPrefixSearch();
prefix_cnt = cnt;
}
u32* res = prefixMatch(tmpBuffer+ptr, prefix_cnt);
if(res) {
u8 entryWordLen = 0x1F & *(((u8*)res)+4);
char* entryWord = ((u8*)res)+5;
if(entryWordLen > clear_cnt) {
clear_cnt = entryWordLen;
}
for(int i = 0; i < clear_cnt; i++) {
tmpBuffer[ptr+i] = ' ';
}
for(int i = 0; i < entryWordLen; i++) {
//shiftForward(ptr+i+1);
tmpBuffer[ptr+i] = entryWord[i];
}
}
}
void modCursorChar(u8 dc) {
tmpBuffer[ptr] += dc;
cursorCnt = 20;
resetPrefixSearch();
}
void handle_up(u16 state) {
if(displayingOsk == 1 && !(state & BUTTON_Z)) {
oskY = (oskY-1);
if(oskY < 0) {
oskY = 4;
}
} else {
modCursorChar(1);
}
}
void handle_down(u16 state) {
if(displayingOsk == 1 && !(state & BUTTON_Z)) {
oskY = (oskY+1);
if(oskY >= 5) {
oskY = 0;
}
} else {
modCursorChar(-1);
}
}
void handle_left(u16 state) {
if(displayingOsk == 1 && !(state & BUTTON_Z)) {
oskX = (oskX-1);
if(oskX < 0) {
oskX = 18;
}
} else {
decPtr();
cursorCnt = 20;
resetPrefixSearch();
}
}
void handle_right(u16 state) {
if(displayingOsk == 1 && !(state & BUTTON_Z)) {
oskX = (oskX+1) % 19;
} else {
incPtr();
cursorCnt = 20;
resetPrefixSearch();
}
}
// when on-screen keyboard is running
// d-pad selects keys, and doesn't move the cursor
//
void get_line_of_input() {
if(initialized == 1) {
printStrn(" ok.", 4);
} else {
initialized = 1;
}
defMode = 0;
incPrintRow();
// reset buffer
for(int i = 0; i < WIDTH_TILES-1; i++) {
buffer[i] = ' ';
}
//char tmpBuffer[WIDTH_TILES+1];
for(int i = 0; i < WIDTH_TILES; i++) {
tmpBuffer[i] = ' ';
}
tmpBuffer[WIDTH_TILES] = '\0';
ptr = minPtr;
u16 lastState = JOY_readJoypad(JOY_1);
while(1) {
VDP_waitVSync();
u16 state = JOY_readJoypad(JOY_1);
u16 diff = (state ^ lastState) & state;
// needs to come first to intercept other keypresses
if(displayingOsk == 1) {
drawOsk();
}
if(diff & BUTTON_LEFT) {
handle_left(state);
} else if (diff & BUTTON_RIGHT) {
handle_right(state);
}
else if (diff & BUTTON_UP) {
handle_up(state);
}
else if (diff & BUTTON_DOWN) {
//read = 1;
handle_down(state);
}
if (diff & BUTTON_MODE) {
switch (defMode) {
case 0:
shiftForward(ptr);
tmpBuffer[ptr++] = ':';
defMode++;
break;
case 1:
tmpBuffer[ptr++] = ';';
defMode++;
break;
}
}
if (diff & BUTTON_START) {
resetPrefixSearch();
break;
}
if(diff & BUTTON_A) {
handle_a();
}
if(diff & BUTTON_B) {
handle_b();
}
if(diff & BUTTON_C) {
handle_c();
}
if(diff & BUTTON_X) {
handle_x();
}
if(diff & BUTTON_Y) {
// toggle on-screen keyboard
if(displayingOsk == 1) {
clearOsk();
displayingOsk = 0;
} else {
drawOsk();
displayingOsk = 1;
}
}
VDP_clearTextLine(printRow+1);
VDP_drawTextBG(inputPlan, &tmpBuffer[0], inputFlags, 1, printRow);
if(printCursor()) {
VDP_drawTextBG(inputPlan, "-", inputFlags, ptr+1, printRow+1);
}
lastState = state;
}
VDP_clearTextLine(printRow+1);
incPrintRow();
// skip the null at the end
for(int i = minPtr; i < WIDTH_TILES; i++) {
buffer[i-minPtr+1] = charUpcase(tmpBuffer[i]);
}
bufftop = (u32*)(buffer+WIDTH_TILES-1);
curkey = (u32*)(buffer);
}
void printChar(char c) {
if (c == '\n') {
incPrintRow();
} else {
char str[2];
str[0] = c;
str[1] = '\0';
VDP_drawTextBG(inputPlan, str, inputFlags, printCol, printRow);
incPrintCol();
}
}
void printStrn(char* str, u8 strLen) {
for(int i = 0; i < strLen; i++) {
printChar(str[i]);
}
}
void printStrNewline(char* str, u8 strLen) {
// if string will wrap-around, print newline first
if(printCol+strLen >= WIDTH_TILES+1) {
incPrintRow();
}
printStrn(str, strLen);
}