/[gxemul]/trunk/src/promemul/dreamcast_scramble.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/src/promemul/dreamcast_scramble.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (show annotations)
Mon Oct 8 16:22:56 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 5477 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1632 2007/09/11 21:46:35 debug Exp $
20070616	Implementing the MIPS32/64 revision 2 "ror" instruction.
20070617	Adding a struct for each physpage which keeps track of which
		ranges within that page (base offset, length) that are
		continuously translatable. When running with native code
		generation enabled (-b), a range is added after each read-
		ahead loop.
		Experimenting with using the physical program counter sample
		data (implemented 20070608) together with the "translatable
		range" information, to figure out which physical address ranges
		would be worth translating to native code (if the number of
		samples falling within a range is above a certain threshold).
20070618	Adding automagic building of .index comment files for
		src/file/, src/promemul/, src src/useremul/ as well.
		Adding a "has been translated" bit to the ranges, so that only
		not-yet-translated ranges will be sampled.
20070619	Moving src/cpu.c and src/memory_rw.c into src/cpus/,
		src/device.c into src/devices/, and src/machine.c into
		src/machines/.
		Creating a skeleton cc/ld native backend module; beginning on
		the function which will detect cc command line, etc.
20070620	Continuing on the native code generation infrastructure.
20070621	Moving src/x11.c and src/console.c into a new src/console/
		subdir (for everything that is console or framebuffer related).
		Moving src/symbol*.c into a new src/symbol/, which should
		contain anything that is symbol handling related.
20070624	Making the program counter sampling threshold a "settings
		variable" (sampling_threshold), i.e. it can now be changed
		during runtime.
		Switching the RELEASE notes format from plain text to HTML.
		If the TMPDIR environment variable is set, it is used instead
		of "/tmp" for temporary files.
		Continuing on the cc/ld backend: simple .c code is generated,
		the compiler and linker are called, etc.
		Adding detection of host architecture to the configure script
		(again), and adding icache invalidation support (only
		implemented for Alpha hosts so far).
20070625	Simplifying the program counter sampling mechanism.
20070626	Removing the cc/ld native code generation stuff, program
		counter sampling, etc; it would not have worked well in the
		general case.
20070627	Removing everything related to native code generation.
20070629	Removing the (practically unusable) support for multiple
		emulations. (The single emulation allowed now still supports
		multiple simultaneous machines, as before.)
		Beginning on PCCTWO and M88K interrupts.
20070723	Adding a dummy skeleton for emulation of M32R processors.
20070901	Fixing a warning found by "gcc version 4.3.0 20070817
		(experimental)" on amd64.
20070905	Removing some more traces of the old "multiple emulations"
		code.
		Also looking in /usr/local/include and /usr/local/lib for
		X11 libs, when running configure.
20070909	Minor updates to the guest OS install instructions, in
		preparation for the NetBSD 4.0 release.
20070918	More testing of NetBSD 4.0 RC1.

1 /*
2 * GXemul: $Id: dreamcast_scramble.c,v 1.4 2007/06/19 03:52:07 debug Exp $
3 *
4 * COMMENT: Dreamcast emulation (helper module)
5 *
6 * This is from Marcus Comstedt's Dreamcast development files
7 * (http://mc.pp.se/dc/files/scramble.c). Public Domain, according to
8 * Marcus software page.
9 *
10 * The only modification done to make it fit into the emulator is that
11 * it is executed internally from src/file.c.
12 */
13
14 /* #define STAND_ALONE */
15
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #define MAXCHUNK (2048*1024)
21
22 static unsigned int seed;
23
24 void my_srand(unsigned int n)
25 {
26 seed = n & 0xffff;
27 }
28
29 unsigned int my_rand(void)
30 {
31 seed = (seed * 2109 + 9273) & 0x7fff;
32 return (seed + 0xc000) & 0xffff;
33 }
34
35 void load(FILE *fh, unsigned char *ptr, unsigned long sz)
36 {
37 if(fread(ptr, 1, sz, fh) != sz)
38 {
39 fprintf(stderr, "Read error!\n");
40 exit(1);
41 }
42 }
43
44 void load_chunk(FILE *fh, unsigned char *ptr, unsigned long sz)
45 {
46 static int idx[MAXCHUNK/32];
47 int i;
48
49 /* Convert chunk size to number of slices */
50 sz /= 32;
51
52 /* Initialize index table with unity,
53 so that each slice gets loaded exactly once */
54 for(i = 0; i < (int)sz; i++)
55 idx[i] = i;
56
57 for(i = sz-1; i >= 0; --i)
58 {
59 /* Select a replacement index */
60 int x = (my_rand() * i) >> 16;
61
62 /* Swap */
63 int tmp = idx[i];
64 idx[i] = idx[x];
65 idx[x] = tmp;
66
67 /* Load resulting slice */
68 load(fh, ptr+32*idx[i], 32);
69 }
70 }
71
72 void load_file(FILE *fh, unsigned char *ptr, unsigned long filesz)
73 {
74 unsigned long chunksz;
75
76 my_srand(filesz);
77
78 /* Descramble 2 meg blocks for as long as possible, then
79 gradually reduce the window down to 32 bytes (1 slice) */
80 for(chunksz = MAXCHUNK; chunksz >= 32; chunksz >>= 1)
81 while(filesz >= chunksz)
82 {
83 load_chunk(fh, ptr, chunksz);
84 filesz -= chunksz;
85 ptr += chunksz;
86 }
87
88 /* Load final incomplete slice */
89 if(filesz)
90 load(fh, ptr, filesz);
91 }
92
93 void read_file(char *filename, unsigned char **ptr, unsigned long *sz)
94 {
95 FILE *fh = fopen(filename, "rb");
96 if(fh == NULL)
97 {
98 fprintf(stderr, "Can't open \"%s\".\n", filename);
99 exit(1);
100 }
101 if(fseek(fh, 0, SEEK_END)<0)
102 {
103 fprintf(stderr, "Seek error.\n");
104 exit(1);
105 }
106 *sz = ftell(fh);
107 *ptr = malloc(*sz);
108 if( *ptr == NULL )
109 {
110 fprintf(stderr, "Out of memory.\n");
111 exit(1);
112 }
113 if(fseek(fh, 0, SEEK_SET)<0)
114 {
115 fprintf(stderr, "Seek error.\n");
116 exit(1);
117 }
118 load_file(fh, *ptr, *sz);
119 fclose(fh);
120 }
121
122 void save(FILE *fh, unsigned char *ptr, unsigned long sz)
123 {
124 if(fwrite(ptr, 1, sz, fh) != sz)
125 {
126 fprintf(stderr, "Write error!\n");
127 exit(1);
128 }
129 }
130
131 void save_chunk(FILE *fh, unsigned char *ptr, unsigned long sz)
132 {
133 static int idx[MAXCHUNK/32];
134 int i;
135
136 /* Convert chunk size to number of slices */
137 sz /= 32;
138
139 /* Initialize index table with unity,
140 so that each slice gets saved exactly once */
141 for(i = 0; i < (int)sz; i++)
142 idx[i] = i;
143
144 for(i = sz-1; i >= 0; --i)
145 {
146 /* Select a replacement index */
147 int x = (my_rand() * i) >> 16;
148
149 /* Swap */
150 int tmp = idx[i];
151 idx[i] = idx[x];
152 idx[x] = tmp;
153
154 /* Save resulting slice */
155 save(fh, ptr+32*idx[i], 32);
156 }
157 }
158
159 void save_file(FILE *fh, unsigned char *ptr, unsigned long filesz)
160 {
161 unsigned long chunksz;
162
163 my_srand(filesz);
164
165 /* Descramble 2 meg blocks for as long as possible, then
166 gradually reduce the window down to 32 bytes (1 slice) */
167 for(chunksz = MAXCHUNK; chunksz >= 32; chunksz >>= 1)
168 while(filesz >= chunksz)
169 {
170 save_chunk(fh, ptr, chunksz);
171 filesz -= chunksz;
172 ptr += chunksz;
173 }
174
175 /* Save final incomplete slice */
176 if(filesz)
177 save(fh, ptr, filesz);
178 }
179
180 void write_file(char *filename, unsigned char *ptr, unsigned long sz)
181 {
182 FILE *fh = fopen(filename, "wb");
183 if(fh == NULL)
184 {
185 fprintf(stderr, "Can't open \"%s\".\n", filename);
186 exit(1);
187 }
188 save_file(fh, ptr, sz);
189 fclose(fh);
190 }
191
192 void dreamcast_descramble(char *src, char *dst)
193 {
194 unsigned char *ptr = NULL;
195 unsigned long sz = 0;
196 FILE *fh;
197
198 read_file(src, &ptr, &sz);
199
200 fh = fopen(dst, "wb");
201 if(fh == NULL)
202 {
203 fprintf(stderr, "Can't open \"%s\".\n", dst);
204 exit(1);
205 }
206 if( fwrite(ptr, 1, sz, fh) != sz )
207 {
208 fprintf(stderr, "Write error.\n");
209 exit(1);
210 }
211 fclose(fh);
212 free(ptr);
213 }
214
215 void scramble(char *src, char *dst)
216 {
217 unsigned char *ptr = NULL;
218 unsigned long sz = 0;
219 FILE *fh;
220
221 fh = fopen(src, "rb");
222 if(fh == NULL)
223 {
224 fprintf(stderr, "Can't open \"%s\".\n", src);
225 exit(1);
226 }
227 if(fseek(fh, 0, SEEK_END)<0)
228 {
229 fprintf(stderr, "Seek error.\n");
230 exit(1);
231 }
232 sz = ftell(fh);
233 ptr = malloc(sz);
234 if( ptr == NULL )
235 {
236 fprintf(stderr, "Out of memory.\n");
237 exit(1);
238 }
239 if(fseek(fh, 0, SEEK_SET)<0)
240 {
241 fprintf(stderr, "Seek error.\n");
242 exit(1);
243 }
244 if( fread(ptr, 1, sz, fh) != sz )
245 {
246 fprintf(stderr, "Read error.\n");
247 exit(1);
248 }
249 fclose(fh);
250
251 write_file(dst, ptr, sz);
252
253 free(ptr);
254 }
255
256
257 #ifdef STAND_ALONE
258
259 int main(int argc, char *argv[])
260 {
261 int opt = 0;
262
263 if(argc > 1 && !strcmp(argv[1], "-d"))
264 opt ++;
265
266 if(argc != 3+opt)
267 {
268 fprintf(stderr, "Usage: %s [-d] from to\n", argv[0]);
269 exit(1);
270 }
271
272 if(opt)
273 dreamcast_descramble(argv[2], argv[3]);
274 else
275 scramble(argv[1], argv[2]);
276
277 return 0;
278 }
279
280 #endif

  ViewVC Help
Powered by ViewVC 1.1.26