File: sramstream.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 (889 lines) | stat: -rw-r--r-- 16,276 bytes parent folder | download | duplicates (3)
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/*
 * 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:        sramstream.cpp ( POWDER Library, C++ )
 *
 * COMMENTS:
 *	The SRAMSTREAM class provides the routines to push bits
 *	into the saved memory space.  It also does implicit zero
 *	compression, so the more zeros present, the less space
 *	required.
 */

#include "sramstream.h"
#include <stdio.h>

#include "sramstream.h"
#include "msg.h"
#include "assert.h"

#include "mygba.h"

// Crappy flashers require one to specify your save type.
// Since we aren't using the official HDK, we have to roll our own
// signifier:
const char *glbSaveType = "SRAM_V113";

static void
delay()
{
    static volatile int delay = 0;

    delay *= delay + 1;
}

SRAMSTREAM::SRAMSTREAM()
{
    mySRAMPtr = 0;
    myPos = 0;
    myBit = 0;
    myNumBlocks = 0;
}

SRAMSTREAM::~SRAMSTREAM()
{
}

void
SRAMSTREAM::rewind()
{
    mySRAMPtr = 0;
    myPos = 0;
    myBit = 0;
    myNumBlocks = 0;
}

bool
SRAMSTREAM::writeRaw(const char *data, int length)
{
    char		*startsram = hamfake_writeLockSRAM(), *sram;
    int			 i;

    if (mySRAMPtr + length > 63000)
    {
	// Fail to write!
	return false;
    }
    
    sram = startsram;
    sram += mySRAMPtr;
    for (i = 0; i < length; i++)
    {
	*sram++ = *data++;
	delay();
    }
    mySRAMPtr += length;

    hamfake_writeUnlockSRAM(startsram);

    return true;
}

bool
SRAMSTREAM::readRaw(char *data, int len)
{
    char		*sram, *startsram = hamfake_readLockSRAM();
    int			 i;

    sram = startsram;
    sram += mySRAMPtr;
    
    for (i = 0; i < len; i++)
    {
	*data++ = *sram++;
	delay();
    }

    mySRAMPtr += len;
    
    hamfake_readUnlockSRAM(startsram);
    return true;
}

bool
SRAMSTREAM::write(int val, int bits)
{
    
    // Only support full int writes for now.
    while (bits > 0)
    {
	myBuffer[myPos++] = val & 0xff;
	val >>= 8;
	bits -= 8;
	
	if (myPos >= 1024)
	{
	    writeToSRAM(myBuffer, 1024);
	    myPos = 0;
	}
    }
    // Does not yet handle fractional.
    UT_ASSERT(!bits);
    return true;
}

bool
SRAMSTREAM::uread(int &val, int bits)
{
    int		offset = 0;
    int		newval;
    
    val = 0;
    while (bits > 0)
    {
	if (!myPos && !myBit)
	{
	    // myBuffer not yet read...
	    readFromSRAM((char *)myBuffer, 256 * sizeof(int));
	}
	newval = myBuffer[myPos++];
	newval &= 0xff;
	val |= newval << offset;
	offset += 8;
	bits -= 8;
	
	if (myPos >= 1024)
	    myPos = 0;
    }
    UT_ASSERT(!bits);		// Should be even bytes.
    return true;
}

bool
SRAMSTREAM::read(int &val, int bits)
{
    uread(val, bits);
    if (bits < 32)
    {
	if (val & (1 << (bits-1)))
	{
	    // Sign extend...
	    val |= ~(int)((1 << bits) - 1);
	}
    }

    return true;
}

bool
SRAMSTREAM::readString(char *dst, int maxlen)
{
    int		i;
    int		val;

    for (i = 0; i < maxlen-1; i++)
    {
	if (!uread(val, 8))
	{
	    dst[i] = 0;
	    return false;
	}
	dst[i] = val;
	if (!dst[i])
	    return true;		// success.
    }
    // Clipped, likely corrupt now.
    dst[i] = 0;
    return false;
}

bool
SRAMSTREAM::writeString(const char *src, int maxlen)
{
    int		i;
    int		val;

    if (!src)
	src = "";

    for (i = 0; i < maxlen-1; i++)
    {
	val = src[i];
	if (!write(val, 8))
	{
	    return false;
	}
	if (!src[i])
	    return true;		// success.
    }
    // Clip the string.
    val = 0;
    if (!write(val, 8))
	return false;
    return true;
}

bool
SRAMSTREAM::flush()
{
    BUF		buf;

    if (myPos || myBit)
    {
	int		i;
	// We want to zero out the end parts to reduce the saved size...
	for (i = myPos+1; i < 256; i++)
	{
	    myBuffer[i] = 0;
	}
	writeToSRAM((char *)myBuffer, 256 * sizeof(int));
    }
    buf.sprintf("Completed save of %d bytes, %d blocks.",
	    mySRAMPtr,
	    myNumBlocks);
    msg_report(buf);
    return true;
}

void
SRAMSTREAM::writeToSRAM(char *data, int len)
{
    char	*sram = hamfake_writeLockSRAM();

    if (mySRAMPtr > 63000)
    {
	// Fail to write!
	return;
    }

    sram += mySRAMPtr;
    UT_ASSERT(len == 1024);
    mySRAMPtr += compress(sram, data, len);
    myNumBlocks++;

    hamfake_writeUnlockSRAM(sram);
}

void 
SRAMSTREAM::readFromSRAM(char *data, int len)
{
    char	*sram = hamfake_readLockSRAM();

    sram += mySRAMPtr;
    UT_ASSERT(len == 1024);
    mySRAMPtr += decompress(data, sram, len);
    myNumBlocks++;

    hamfake_readUnlockSRAM(sram);
}

// This will compress 1k block into dst.
int
SRAMSTREAM::compress(char *dst, char *src, int blocklen)
{
    // First, we determine the best method...
    int			zero = 0;		// Number zeros..
    int			rlesave = 0;		// Amount saved by RLE
    int			zeroprebyte = 0;	// Zeros with byte preproc
    int			zeropreshort = 0;	// Zeros with short preproc
    int			zeropreint = 0;		// Zeros with int preproc
    int			currunlen, minzero;
    int			isconstant = 1;
    int			zerotablesize;
    int			i;
    PREPROCESS_TYPE	preprocess;
    bool		istopblock = false;

#if 1
    if (blocklen == 1024)
	istopblock = true;
#endif

    // Number of bytes needed for a zerotable of this blocklen.
    zerotablesize = (blocklen + 7) >> 3;

    currunlen = 0;
    for (i = 0; i < blocklen; i++)
    {
	if (src[i] == 0)
	    zero++;
	if (i && src[i] == src[i-1])
	{
	    currunlen++;
	}
	else
	    currunlen = 1;		// Difference, run of 1.
	if (currunlen > 3)
	    rlesave++;			// After three codes we save...
	if ((!i && !src[i]) || (src[i] == src[i-1]))
	    zeroprebyte++;
	if ((i < 2 && !src[i]) || (src[i] == src[i-2]))
	    zeropreshort++;
	if ((i < 4 && !src[i]) || (src[i] == src[i-4]))
	    zeropreint++;
	else
	    isconstant = 0;
    }
    if (0)
    {
	BUF		buf;
	buf.sprintf("Z:%d,B:%d,S:%d,I:%d",
		zero, zeroprebyte, zeropreshort, zeropreint);
	msg_report(buf);
    }

    // Find best preprocess...
    if (zero >= zeroprebyte)
    {
	minzero = zero;
	preprocess = PREPROCESS_NONE;
    }
    else
    {
	minzero = zeroprebyte;
	preprocess = PREPROCESS_BYTE;
    }
    if (minzero < zeropreshort)
    {
	preprocess = PREPROCESS_SHORT;
	minzero = zeropreshort;
    }
    if (minzero < zeropreint)
    {
	preprocess = PREPROCESS_INT;
	minzero = zeropreint;
    }

    // Determine best method...
    if (isconstant)
    {
	//msg_report("Constant...");
	if (istopblock)
	    msg_report("_");
	return compressConstant(dst, src);
    }

    // May have to punt for uncompressed...
    if ((blocklen - minzero + zerotablesize) > blocklen) // &&
	// (blocklen + 8 - rlesave) > blocklen)
    {
	// msg_report("None...");
	if (istopblock)
	    msg_report("x");
	return compressNone(dst, src, blocklen);
    }
    
    //if ((blocklen - minzero + zerotablesize) < (blocklen - rlesave))
    if (1)
    {
	//msg_report("ZeroTable...");
	if (istopblock)
	    msg_report(".");
	return compressZeroTable(dst, src, preprocess, blocklen);
    }
    else
    {
	if (istopblock)
	    msg_report("r");
	return compressRLE(dst, src, blocklen);
    }
}

// This will decompress into a 1k block.
int
SRAMSTREAM::decompress(char *dst, char *src, int blocklen)
{
    PREPROCESS_TYPE		preprocess;
    COMPRESS_TYPE		compress;
    char			c;
    bool			istopblock = false;

#if 1
    if (blocklen == 1024)
	istopblock = true;
#endif

    // Get type code...
    c = *src++;
    delay();
    
    compress = (COMPRESS_TYPE) (c & 0xf);
    preprocess = (PREPROCESS_TYPE) ((c >> 4) & 0xf);

    switch (compress)
    {
	case COMPRESS_NONE:
	    //msg_report("None...");
	    if (istopblock)
		msg_report("x");
	    return decompressNone(dst, src, blocklen) + 1;
	case COMPRESS_CONSTANT:
	    //msg_report("Constant...");
	    if (istopblock)
		msg_report("_");
	    return decompressConstant(dst, src, blocklen) + 1;
	case COMPRESS_ZEROTABLE:
	    //msg_report("ZeroTable...");
	    if (istopblock)
		msg_report(".");
	    return decompressZeroTable(dst, src, preprocess, blocklen) + 1;
	case COMPRESS_RLE:
	    //msg_report("RLE...");
	    if (istopblock)
		msg_report("r");
	    return decompressRLE(dst, src, blocklen);
	default:
	    UT_ASSERT(!"Unknown compression type!");
	    return 0;
    }
}

int
SRAMSTREAM::compressConstant(char *dst, char *src)
{
    // Write out the constant code, than the four bytes needed...
    *dst++ = COMPRESS_CONSTANT;
    delay();
    *dst++ = *src++;
    delay();
    *dst++ = *src++;
    delay();
    *dst++ = *src++;
    delay();
    *dst++ = *src++;
    delay();

    return 5;		// Fixed 5 bytes..
}

int
SRAMSTREAM::decompressConstant(char *dst, char *src, int len)
{
    int		i;

    UT_ASSERT(! (len & 3));
    for (i = 0; i < (len >> 2); i++)
    {
	*dst++ = src[0];
	delay();
	*dst++ = src[1];
	delay();
	*dst++ = src[2];
	delay();
	*dst++ = src[3];
	delay();
    }
    return 4;
}

int
SRAMSTREAM::compressNone(char *dst, char *src, int len, bool header)
{
    int		i;
    
    if (header)
    {
        *dst++ = COMPRESS_NONE;
	delay();
    }

    for (i = 0; i < len; i++)
    {
	*dst++ = *src++;
	delay();
    }
    return len + (header ? 1 : 0);	// Fixed size..
}

int
SRAMSTREAM::decompressNone(char *dst, char *src, int len)
{
    int		i;

    for (i = 0; i < len; i++)
    {
	*dst++ = *src++;
	delay();
    }
    return len;
}

int
SRAMSTREAM::compressZeroTable(char *dst, char *src, PREPROCESS_TYPE preprocess, int blocklen)
{
    char		zerotable[128];
    int			zerotablelen;
    int			i, j, pos, prelen;
    char		*dststart = dst;

    zerotablelen = (blocklen + 7) >> 3;

    UT_ASSERT(! (blocklen & 7));
    
    *dst++ = COMPRESS_ZEROTABLE | (preprocess << 4);
    delay();

    switch (preprocess)
    {
	case PREPROCESS_NONE:
	    //msg_report("prenone");
	    prelen = 0;
	    break;
	case PREPROCESS_BYTE:
	    //msg_report("prebyte");
	    prelen = 1;
	    break;
	case PREPROCESS_SHORT:
	    //msg_report("preshort");
	    prelen = 2;
	    break;
	case PREPROCESS_INT:
	    //msg_report("preint");
	    prelen = 4;
	    break;
	default:
	    UT_ASSERT(0);
	    prelen = 1024;
	    break;
    }

    // First, we build the zero table...
    pos = 0;
    for (i = 0; i < zerotablelen; i++)
    {
	zerotable[i] = 0;
	for (j = 0; j < 8; j++)
	{
	    // If before prelen, act as if pre was zero...
	    if (pos < prelen)
	    {
		if (src[pos])
		    zerotable[i] |= 1 << j;
	    }
	    else
	    {
		switch (preprocess)
		{
		    case PREPROCESS_NONE:
			if (src[pos])
			    zerotable[i] |= 1 << j;
			break;
		    case PREPROCESS_BYTE:
		    case PREPROCESS_SHORT:
		    case PREPROCESS_INT:
			if (src[pos] != src[pos - prelen])
			    zerotable[i] |= 1 << j;
			break;
		}
	    }
	    pos++;
	}
    }

    // Now save it using simple RLE..
    //dst += compressRLE(dst, zerotable, 128, false);

    // If zerotablelen is short enough, go to no compression.
    if (zerotablelen < 20)
        dst += compressNone(dst, zerotable, zerotablelen, false);
    else
	dst += compress(dst, zerotable, zerotablelen);

    // And now save the non-zero entries...
    pos = 0;
    for (i = 0; i < zerotablelen; i++)
    {
	for (j = 0; j < 8; j++)
	{
	    if (pos < prelen || (preprocess == PREPROCESS_NONE))
	    {
		if (zerotable[i] & (1 << j))
		{
		    *dst++ = src[pos];
		    delay();
		}
	    }
	    else
	    {
		// Modulate our result by the prelen previous one..
		if (zerotable[i] & (1 << j))
		{
		    *dst++ = src[pos] ^ src[pos-prelen];
		    delay();
		}
	    }
	    pos++;
	}
    }

    return (dst - dststart);
}

int
SRAMSTREAM::decompressZeroTable(char *dst, char *src, PREPROCESS_TYPE preprocess, int blocklen)
{
    char	zerotable[128];
    int		zerotablelen;
    char	*srcstart = src;
    int		prelen;
    int		i, j, pos;
    char	c;

    zerotablelen = (blocklen + 7) >> 3;
    UT_ASSERT(! (blocklen & 7) );

    // Fetch the zero table...
    //src += decompressRLE(zerotable, src, 128);
    if (zerotablelen < 20)
	src += decompressNone(zerotable, src, zerotablelen);
    else
	src += decompress(zerotable, src, zerotablelen);

    switch (preprocess)
    {
	case PREPROCESS_NONE:
	    //msg_report("prenone");
	    prelen = 0;
	    break;
	case PREPROCESS_BYTE:
	    //msg_report("prebyte");
	    prelen = 1;
	    break;
	case PREPROCESS_SHORT:
	    //msg_report("preshort");
	    prelen = 2;
	    break;
	case PREPROCESS_INT:
	    //msg_report("preint");
	    prelen = 4;
	    break;
	default:
	    UT_ASSERT(0);
	    prelen = 1024;
	    break;
    }

    pos = 0;
    for (i = 0; i < zerotablelen; i++)
    {
	for (j = 0; j < 8; j++)
	{
	    if (pos < prelen || (preprocess == PREPROCESS_NONE))
	    {
		c = 0;
	    }
	    else
	    {
		c = dst[-prelen];
	    }
	    if (zerotable[i] & (1 << j))
	    {
		c ^= *src++;
	    }
	    *dst++ = c;
	    delay();
	    pos++;
	}
    }
    
    return (src - srcstart);
}

int
SRAMSTREAM::compressRLE(char *dst, char *src, int len, bool header)
{
    char		*dststart = dst;
    int			 currun, i;
    int			 runsame;
    int			 n, j;

    UT_ASSERT(!"RLE IS BROKEN!");
    
    if (header)
    {
	*dst++ = COMPRESS_RLE;
	delay();
    }
    
    // The rule is if we see something 3 long, we do a run.  This doesn't
    // save anything, except it might collapse one of the 8 mandatory
    // restarts in an 8k table.

    currun = 0;
    runsame = 0;
    i = 0;
    while (i < 1024)
    {
	if (currun && (src[i-1] == src[i]))
	    runsame++;
	else
	{
	    // We have hit the end of a same run..  Properly write out
	    // if 3 or greater.
	    if (runsame > 3)
	    {
		// TODO: Is this off by one?
		j = i - currun;		// Get back to the start...
		n = currun - runsame;	// number different prior to same.
		if (n)
		{
		    *dst++ = n-1;	// No high bit, raw data...
		    delay();
		    while (n--)
		    {
			*dst++ = src[j++];
			delay();
		    }
		}
		// Now, dump out the run proper...
		*dst++ = (runsame-1) | 0x80;	// high bit for rle
		delay();
		*dst++ = src[i-1];		// Last valid...
		delay();
		
		// We now want i to be on the stack, so a currun of 1
		// and runsame of 1.
		runsame = currun = 1;
		i++;
		continue;
	    }
	    // Otherwise, we keep trucking resetting runsame to 1.
	    runsame = 1;
	}
	currun++;
	if (currun == 128)
	{
	    // We need to dump out this sequence and reset...
	    // If runsame == currun, we had 128 in a row the same.
	    // Otherwise, it is raw data time...
	    if (runsame == currun)
	    {
		// NOTE: This errors out on MSVC.
		// Good thing we don't use runlength due to some
		// other error
		*dst++ = (char) (unsigned char)0xFF; // (128 - 1) | 0x80
		delay();
		*dst++ = src[i];
		delay();
	    }
	    else
	    {
		j = i - currun;
		n = 128;
		*dst++ = n-1;	// No high bit, raw data...
		delay();
		while (n--)
		{
		    *dst++ = src[j++];
		    delay();
		}
	    }

	    // Now we have dumped out a full block, reset everything..
	    runsame = currun = 0;
	    i++;
	    continue;
	}

	i++;
    }

    // i hit 1024.  Need to dump what is left...
    if (currun)
    {
	if (runsame == currun)
	{
	    *dst++ = 0x80 | (currun - 1); 
	    delay();
	    *dst++ = src[i];
	    delay();
	}
	else
	{
	    j = i - currun;
	    n = currun;
	    *dst++ = n-1;	// No high bit, raw data...
	    delay();
	    while (n--)
	    {
		*dst++ = src[j++];
		delay();
	    }
	}
    }

    return (dst - dststart);
}

int
SRAMSTREAM::decompressRLE(char *dst, char *src, int len)
{
    int			n;
    char		c;
    char		*srcstart = src;
    
    while (len > 0)
    {
	c = *src++;
	delay();
	if (c & 0x80)
	{
	    // RLE...
	    n = c & 0x7f;
	    n++;
	    c = *src++;
	    delay();
	    while (n--)
	    {
		*dst++ = c;
		len--;
	    }
	}
	else
	{
	    // Raw data...
	    n = c & 0x7f;
	    n++;
	    while (n--)
	    {
		*dst++ = *src++;
		len--;
		delay();
	    }
	}
    }

    return (src - srcstart);
}

//
// Hamfake methods defined in the non-SDL world.
//
#if !defined(USING_SDL) && !defined(USING_DS)

char *
hamfake_readLockSRAM()
{
    return (char *) 0xE000000;
}

void
hamfake_readUnlockSRAM(char *)
{
}

char *
hamfake_writeLockSRAM()
{
    return (char *) 0xE000000;
}

void
hamfake_writeUnlockSRAM(char *)
{
}

// This is being written whilst travelling at a great land speed.
// I'm on the Nozomi Super Express Shinkasen, speeding past Mt. Fuji.
void
hamfake_endWritingSession()
{
}

#endif