File: hamfake.cpp

package info (click to toggle)
powder 117-2
  • links: PTS
  • area: non-free
  • in suites: stretch
  • size: 10,576 kB
  • ctags: 3,545
  • sloc: cpp: 55,002; makefile: 541; sh: 258; objc: 245; ansic: 107; csh: 54
file content (746 lines) | stat: -rw-r--r-- 14,251 bytes parent folder | download | duplicates (6)
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*
 * PROPRIETARY INFORMATION.  This software is proprietary to POWDER
 * Development, and is not to be reproduced, transmitted, or disclosed
 * in any way without written permission.
 *
 * Produced by:	Jeff Lait
 *
 *      	POWDER Development
 *
 * NAME:        hamfake.cpp ( POWDER Library, C++ )
 *
 * COMMENTS:
 *	This file implements all the fake ham functions.
 *	It also stores the global state of the hardware.
 */


#include "mygba.h"
#include "hamfake.h"

#include "../../gfxengine.h"
#include "../../bmp.h"
#include "../../control.h"

#include <nds.h>
#include <fat.h>

#include <sys/stat.h>

#define SRAMSIZE 65536

#define KEY_REPEAT_INITIAL 15
#define KEY_REPEAT_AFTER 7

//
// Global GBA state:
//

bg_info			ham_bg[4];

int			ham_vramtop = 0;
u8			ham_winin[2], ham_winout[2];
bool			ham_fatvalid = false;

bool			ham_extratileset = false;

short			glbStylusX, glbStylusY;
bool			glbStylusState;

// It is important to use this rather than the SpriteEntry
// defined in sprite.h because it uses a union of enums and
// we don't allow short enums, so it will have the entirely
// wrong size.
struct POWDER_SpriteEntry
{
    u16 attribute[4];
};

static const int POWDER_SPRITES = 128;
POWDER_SpriteEntry		glbSpriteList[POWDER_SPRITES];
bool			glbSpriteDirty = false;

// This is the path, including trailing slash, where we will
// save all of our data such as .SAV files and character dumps.
// If it doesn't exist, we create it on start up.  If creation
// fails, we dump a warning and use /
const char		*glbAbsoluteDataPath;

// Keyboard state:

void
hamfake_rebuildScreen()
{
}

void
updateOAM()
{
    if (!glbSpriteDirty)
	return;

    // Compiling without short-enums can cause problems with DS
    // data structures.
    DC_FlushRange(glbSpriteList,POWDER_SPRITES*sizeof(POWDER_SpriteEntry));
    dmaCopy(glbSpriteList, OAM, POWDER_SPRITES*sizeof(POWDER_SpriteEntry));
    
    glbSpriteDirty = false;
}

// Wait for an event to occur.
void
hamfake_awaitEvent()
{
    // Check for the lid going down, in which case we power down until
    // it goes up...
    if (ctrl_rawpressed(BUTTON_LID))
    {
	u16		powerstatus;

	powerstatus = REG_POWERCNT;
	REG_POWERCNT = 0;
	while (ctrl_rawpressed(BUTTON_LID))
	{
	    // Can't await frame here as we are already doing so :>
	    // Note that they await a VGL,
	    // perhaps that would halt the porcessor
	    // for more savings?
	    // Apparently it does, which is why we added it to
	    // the main loop but foolishly forgot it in the powerdown loop,
	    // ironically thus consuming less CPU power when the lid
	    // is open as opposed to closed.
	    swiWaitForVBlank();
	}
	REG_POWERCNT = powerstatus;
    }
    updateOAM();
    swiWaitForVBlank();
}

// Return our internal screen.
u16 *
hamfake_lockScreen()
{
    // Because we are the very epitome of laziness,
    // we give the proper offset to center this GBA style
    // rather than fixing everything else.
    return VRAM_A + 8 + 16 * 256;
}

void
hamfake_unlockScreen(u16 *)
{
}

char *glbSRAM = 0;

// Deal with our SRAM buffer.
char *
hamfake_writeLockSRAM()
{
    // TODO: determine if we are proper PASSME or not before
    // writing to SRAM.
    if (!glbSRAM)
    {
	glbSRAM = (char *)malloc(SRAMSIZE);

	// Read our .sav file from fat if possible.
	if (ham_fatvalid)
	{
	    FILE	*fp;

	    fp = hamfake_fopen("powder.sav", "rb");
	    if (!fp)
	    {
		iprintf("No save file found.\n");
	    }
	    else
	    {
		fread(glbSRAM, SRAMSIZE, 1, fp);
		fclose(fp);
	    }
	}
    }
    return glbSRAM;
}

void
hamfake_writeUnlockSRAM(char *)
{
}

char *
hamfake_readLockSRAM()
{
    return hamfake_writeLockSRAM();
}

void
hamfake_readUnlockSRAM(char *)
{
}

void
hamfake_endWritingSession()
{
    // Why the hell do I have a write unlock if there is an endWritingSession?
    FILE		*fp;

    fp = hamfake_fopen("powder.sav", "wb");

    if (!fp)
    {
	iprintf("Failure to open powder.sav for writing!\n");
	return;
    }

    fwrite(glbSRAM, SRAMSIZE, 1, fp);

    fclose(fp);
}

bool
hamfake_fatvalid()
{
    return ham_fatvalid;
}

// Called to run our event poll
void
hamfake_pollEvents()
{
    updateOAM();
    scanKeys();
    touchPosition	pos = touchReadXY();
    glbStylusX = pos.px;
    glbStylusY = pos.py;
    glbStylusState = (keysHeld() & KEY_TOUCH) ? true : false;
}

bool
hamfake_isPressed(BUTTONS button)
{
    hamfake_pollEvents();
    switch (button)
    {
	case BUTTON_UP:
	    return keysHeld() & KEY_UP;
	    
	case BUTTON_DOWN:
	    return keysHeld() & KEY_DOWN;

	case BUTTON_LEFT:
	    return keysHeld() & KEY_LEFT;

	case BUTTON_RIGHT:
	    return keysHeld() & KEY_RIGHT;

	case BUTTON_SELECT:
	    return keysHeld() & KEY_SELECT;
	case BUTTON_START:
	    return keysHeld() & KEY_START;

	case BUTTON_A:
	    return keysHeld() & KEY_A;
	case BUTTON_B:
	    return keysHeld() & KEY_B;
	case BUTTON_R:
	    return keysHeld() & KEY_R;
	case BUTTON_L:
	    return keysHeld() & KEY_L;

	case BUTTON_X:
	    return keysHeld() & KEY_X;
	case BUTTON_Y:
	    return keysHeld() & KEY_Y;

	case BUTTON_TOUCH:
	    return keysHeld() & KEY_TOUCH;

	case BUTTON_LID:
	    return keysHeld() & KEY_LID;

	case NUM_BUTTONS:
	    return false;
    }

    return false;
}

int
hamfake_isAnyPressed()
{
    hamfake_pollEvents();
    if (keysHeld() == 0)
    {
	// For backwards compatability with R_CTRLINPUT the sense of this
	// return is a bit odd...
	return 0x3FF;
    }
    return 0;
}

int
hamfake_peekKeyPress()
{
    return 0;
}

int
hamfake_getKeyPress(bool onlyascii)
{
    return 0;
}

void
hamfake_insertKeyPress(int key)
{
    return;
}

void
hamfake_clearKeyboardBuffer()
{
}

void
hamfake_setScrollX(int layer, int scroll)
{
    BG_OFFSET[layer].x = scroll;
}

void
hamfake_setScrollY(int layer, int scroll)
{
    BG_OFFSET[layer].y = scroll;
}

void
ham_Init()
{
    if (fatInitDefault())
    {
	iprintf("FAT system initialized\n");
	ham_fatvalid = true;

	// Determine our save directory
	struct stat		buffer;
	int			status;

	status = stat("/data", &buffer);
	if (status < 0)
	{
	    // An error, try to make the directory.
	    mkdir("/data", S_IRWXU | S_IRWXG | S_IRWXO);
	}
	status = stat("/data/powder", &buffer);
	if (status < 0)
	{
	    // Again, an error, so try another make.  I don't
	    // think mkdir will make two...
	    mkdir("/data/powder", S_IRWXU | S_IRWXG | S_IRWXO);
	}

	// And stat again..
	status = stat("/data/powder", &buffer);

	if (!status && S_ISDIR(buffer.st_mode))
	{
	    // Everything is cool!
	    glbAbsoluteDataPath = "/data/powder/";
	}
	else
	{
	    // Failed to create directory...
	    iprintf("Failed to find/create /data/powder.  POWDER's data will be saved to the root of your flash card.\n");
	}
	
	// Attempt to load foreign images.
	if (bmp_loadExtraTileset())
	{
	    ham_extratileset = true;
	    iprintf("Extra tileset loaded.\n");
	}
    }
    else
    {
	iprintf("No FAT system found - save disabled\n");
	ham_fatvalid = false;
    }

    int		i;
    for (i = 0; i < POWDER_SPRITES; i++)
    {
	glbSpriteList[i].attribute[0] = ATTR0_DISABLED;
	glbSpriteList[i].attribute[1] = ATTR1_SIZE_16;
	glbSpriteList[i].attribute[2] = 0;
	glbSpriteList[i].attribute[3] = 0;
    }
}

void
ham_StartIntHandler(u8 intno, void (*fp)())
{
    // assert(intno == INT_TYPE_VBL);
    irqSet(IRQ_VBLANK, fp);
    irqEnable(IRQ_VBLANK);
}

void
ham_SetBgMode(u8 mode)
{
    switch (mode)
    {
	case 3:
#if 1
	    vramSetBankA(VRAM_A_LCD);
	    videoSetMode(MODE_FB0 |
		    DISPLAY_SPR_ACTIVE |
		    DISPLAY_SPR_1D |
		    DISPLAY_SPR_1D_BMP);
	    // Desire a flat memory model.
#else
	    videoSetMode(MODE_3_2D |
			DISPLAY_BG0_ACTIVE |
			DISPLAY_BG1_ACTIVE |
			DISPLAY_BG2_ACTIVE |
			DISPLAY_BG3_ACTIVE);
#endif
	    break;

	case 0:
	    // Desire tiled mode.
	    vramSetBankA(VRAM_A_MAIN_BG);
	    vramSetBankE(VRAM_E_MAIN_SPRITE);
	    videoSetMode(MODE_0_2D |
			DISPLAY_SPR_ACTIVE |
			DISPLAY_BG0_ACTIVE |
			DISPLAY_BG1_ACTIVE |
			DISPLAY_BG2_ACTIVE |
			DISPLAY_BG3_ACTIVE |
			DISPLAY_SPR_1D |	// I am too old school.
			DISPLAY_SPR_1D_BMP |
			DISPLAY_WIN0_ON);

	    // Reset the top of our vram.
	    ham_vramtop = 0;
	    ham_DeleteWin(0);
	    ham_DeleteWin(1);
	    break;
    }
}


void
ham_LoadBGPal(void *vpal, int bytes)
{
    u16		*pal = (u16 *) vpal;
    bytes /= 2;
    int		i;

    for (i = 0; i < bytes; i++)
    {
	BG_PALETTE[i] = pal[i];
    }
}

void
hamfake_LoadSpritePal(void *vpal, int bytes)
{
    u16		*pal = (u16 *) vpal;
    bytes /= 2;
    int		i;

    for (i = 0; i < bytes; i++)
    {
	// The sprite palette could theoritically be entirely independent,
	// but since we'll likely just be grabbing tiles from the shared
	// graphics list...
	SPRITE_PALETTE[i] = pal[i];
    }
}

u8
ham_AllocateVramBlock(int bytes, int blocksize)
{
    int		blockid;

    // Round current vram to nearest block.
    blockid = (ham_vramtop + blocksize-1) / blocksize;
    ham_vramtop = blockid * blocksize;
    
    // Round up bytes to allocate...
    bytes = (bytes + blocksize -1) / blocksize;
    bytes *= blocksize;

    ham_vramtop += bytes;

    return blockid;
}

map_info_ptr
ham_InitMapEmptySet(u8 size, u8 rot)
{
    map_info_ptr		mapinfo;
    int				vramsize;

    mapinfo = new map_info;

    // Find size in bytes of map.
    // if 0 size, is 32x32 so hence 2k.
    // If 3 size, is 64x64 so hence 8k.
    vramsize = 0;
    switch (size)
    {
	case 0:
	    vramsize = 2 * 32 * 32;
	    mapinfo->largemap = false;
	    break;
	case 3:
	    vramsize = 2 * 64 * 64;
	    mapinfo->largemap = true;
	    break;
    }

    mapinfo->blockid = ham_AllocateVramBlock(vramsize, 0x800);
    
    return mapinfo;
}

tile_info_ptr
ham_InitTileEmptySet(u16 entries, u8 colormode, u8 onlybaseblock)
{
    tile_info_ptr	tileinfo;
    int			vramsize;

    tileinfo = new tile_info;

    vramsize = entries;
    if (colormode == 0)
	vramsize *= 8 * 8 / 2;			// 16 colours
    else
	vramsize *= 8 * 8;

    tileinfo->blockid = ham_AllocateVramBlock(vramsize, 0x2000);
    
    return tileinfo;
}

void
ham_InitBg(int layer, int active, int priority, int mosaic)
{
    int		mapsize;
    if (ham_bg[layer].mi->largemap)
	mapsize = BG_64x64;
    else
	mapsize = BG_32x32;
    
    BGCTRL[layer] = BG_256_COLOR |
		    BG_PRIORITY(priority) |
		    BG_TILE_BASE(ham_bg[layer].ti->blockid) |
		    BG_MAP_BASE(ham_bg[layer].mi->blockid) |
		    mapsize;
}

void
ham_CreateWin(int wid, int x1, int y1, int x2, int y2, int inside, int outside, int fx)
{
    switch (wid)
    {
	case 0:
	    WIN0_X0 = x1;
	    WIN0_X1 = x2;
	    WIN0_Y0 = y1;
	    WIN0_Y1 = y2;
	    break;
	case 1:
	    WIN1_X0 = x1;
	    WIN1_X1 = x2;
	    WIN1_Y0 = y1;
	    WIN1_Y1 = y2;
	    break;
    }
    ham_winin[wid] = inside;
    ham_winout[wid] = outside;
    // Update control registers.
    WIN_IN = ham_winin[0] | (ham_winin[1] << 8);
    // This is wrong.
    WIN_OUT = ham_winout[0] | (ham_winout[1] << 8);
}

void
ham_DeleteWin(int wid)
{
    // Allow everything inside & outside.
    ham_winin[wid] = WIN_BG0 | WIN_BG1 | WIN_BG2 | WIN_BG3 | WIN_OBJ;
    ham_winout[wid] = WIN_BG0 | WIN_BG1 | WIN_BG2 | WIN_BG3 | WIN_OBJ;
    WIN_IN = ham_winin[0] | (ham_winin[1] << 8);
    WIN_OUT = ham_winout[0] | (ham_winout[1] << 8);
}

void
ham_SetMapTile(int layer, int mx, int my, int tileidx)
{
    int		blockid = ham_bg[layer].mi->blockid;

    u16		*map = (u16 *) SCREEN_BASE_BLOCK(blockid);

    // We are interleaved in a funny way...
    if (mx >= 32)
    {
	mx -= 32;
	map += 32 * 32;
    }
    if (my >= 32)
    {
	my -= 32;
	map += 2 * 32 * 32;
    }
    map[my*32 + mx] = tileidx;
}

void
ham_ReloadTileGfx(tile_info_ptr tiledata, const u16 *data, int destidx, 
		  int numtile)
{
    int		i;
    int		 blockid = tiledata->blockid;
    u16		*dest = (u16 *) CHAR_BASE_BLOCK(blockid);
    dest += 4 * 8 * destidx;
    numtile *= 4 * 8;

    for (i = 0; i < numtile; i++)
    {
	dest[i] = data[i];
    }
}

void
hamfake_ReloadSpriteGfx(const u16 *data, int destidx, int numtile)
{
    u16		*dest = &SPRITE_GFX[0];
    int		 i;
    
    dest += destidx * 4 * 8;
    numtile *= 4 * 8;

    for (i = 0; i < numtile; i++)
    {
	dest[i] = data[i];
    }
}

void
hamfake_softReset()
{
}

void
hamfake_setFullScreen(bool fullscreen)
{
}

bool
hamfake_isFullScreen()
{
    return true;		// Always full :>
}

bool
hamfake_extratileset()
{
    return ham_extratileset;
}

void
hamfake_getstyluspos(int &x, int &y)
{
    x = glbStylusX;
    y = glbStylusY;

    // Stylus positions are returned in the original GBA window,
    // not the extended window of the DS!
    x -= 8;
    y -= 16;
}

bool
hamfake_getstylusstate()
{
    return glbStylusState;
}

void
hamfake_movesprite(int spriteno, int x, int y)
{
    // The caller is specifying in GBA space.
    x += 8;
    y += 16;

    // Clear out our old coord.
    glbSpriteList[spriteno].attribute[0] &= ~255;
    glbSpriteList[spriteno].attribute[1] &= ~511;

    // Merge in new coord.
    y &= 255;
    x &= 511;
    glbSpriteList[spriteno].attribute[0] |= y;
    glbSpriteList[spriteno].attribute[1] |= x;

    glbSpriteDirty = true;
}

void
hamfake_enablesprite(int spriteno, bool enabled)
{
    // Change the flag to reflect the new value.
    // Track if dirty to save cycles.
    if (enabled)
    {
	int		y;
	y = glbSpriteList[spriteno].attribute[0] & 255;
	glbSpriteList[spriteno].attribute[0] = ATTR0_COLOR_256 | ATTR0_SQUARE;
	glbSpriteList[spriteno].attribute[0] |= y;
	// We want sprites to always be under the text.
	// Exception is the cursor.
	if (spriteno)
	    glbSpriteList[spriteno].attribute[2] = 8 * spriteno | ATTR2_PRIORITY(1);
	else
	    glbSpriteList[spriteno].attribute[2] = 8 * spriteno;
	glbSpriteDirty = true;
    }
    else
    {
	glbSpriteList[spriteno].attribute[0] = ATTR0_DISABLED;
	glbSpriteDirty = true;
    }
}

FILE *
hamfake_fopen(const char *path, const char *mode)
{
    FILE		*result;

    // We want to use an absolute path.
    // Prefix /data/powder.
    char		*buf;
    buf = new char[strlen(path) + strlen(glbAbsoluteDataPath) + 5];
    sprintf(buf, "%s%s", glbAbsoluteDataPath, path);

    result = fopen(buf, mode);

    delete [] buf;

    return result;
}

void
hamfake_setTileSize(int width, int height)
{
}

int
hamfake_getKeyModifiers()
{
    return 0;
}