/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (hide annotations)
Mon Oct 8 16:21:17 2007 UTC (16 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 5415 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1480 2007/02/19 01:34:42 debug Exp $
20061029	Changing usleep(1) calls in the debugger to usleep(10000)
20061107	Adding a new disk image option (-d o...) which sets the ISO9660
		filesystem base offset; also making some other hacks to allow
		NetBSD/dreamcast and homebrew demos/games to boot directly
		from a filesystem image.
		Moving Dreamcast-specific stuff in the documentation to its
		own page (dreamcast.html).
		Adding a border to the Dreamcast PVR framebuffer.
20061108	Adding a -T command line option (again?), for halting the
		emulator on unimplemented memory accesses.
20061109	Continuing on various SH4 and Dreamcast related things.
		The emulator should now halt on more unimplemented device
		accesses, instead of just printing a warning, forcing me to
		actually implement missing stuff :)
20061111	Continuing on SH4 and Dreamcast stuff.
		Adding a bogus Landisk (SH4) machine mode.
20061112	Implementing some parts of the Dreamcast GDROM device. With
		some ugly hacks, NetBSD can (barely) mount an ISO image.
20061113	NetBSD/dreamcast now starts booting from the Live CD image,
		but crashes randomly quite early on in the boot process.
20061122	Beginning on a skeleton interrupt.h and interrupt.c for the
		new interrupt subsystem.
20061124	Continuing on the new interrupt system; taking the first steps
		to attempt to connect CPUs (SuperH and MIPS) and devices
		(dev_cons and SH4 timer interrupts) to it. Many things will
		probably break from now on.
20061125	Converting dev_ns16550, dev_8253 to the new interrupt system.
		Attempting to begin to convert the ISA bus.
20061130	Incorporating a patch from Brian Foley for the configure
		script, which checks for X11 libs in /usr/X11R6/lib64 (which
		is used on some Linux systems).
20061227	Adding a note in the man page about booting from Dreamcast
		CDROM images (i.e. that no external kernel is needed).
20061229	Continuing on the interrupt system rewrite: beginning to
		convert more devices, adding abort() calls for legacy interrupt
		system calls so that everything now _has_ to be rewritten!
		Almost all machine modes are now completely broken.
20061230	More progress on removing old interrupt code, mostly related
		to the ISA bus + devices, the LCA bus (on AlphaBook1), and
		the Footbridge bus (for CATS). And some minor PCI stuff.
		Connecting the ARM cpu to the new interrupt system.
		The CATS, NetWinder, and QEMU_MIPS machine modes now work with
		the new interrupt system :)
20061231	Connecting PowerPC CPUs to the new interrupt system.
		Making PReP machines (IBM 6050) work again.
		Beginning to convert the GT PCI controller (for e.g. Malta
		and Cobalt emulation). Some things work, but not everything.
		Updating Copyright notices for 2007.
20070101	Converting dev_kn02 from legacy style to devinit; the 3max
		machine mode now works with the new interrupt system :-]
20070105	Beginning to convert the SGI O2 machine to the new interrupt
		system; finally converting O2 (IP32) devices to devinit, etc.
20070106	Continuing on the interrupt system redesign/rewrite; KN01
		(PMAX), KN230, and Dreamcast ASIC interrupts should work again,
		moving out stuff from machine.h and devices.h into the
		corresponding devices, beginning the rewrite of i80321
		interrupts, etc.
20070107	Beginning on the rewrite of Eagle interrupt stuff (PReP, etc).
20070117	Beginning the rewrite of Algor (V3) interrupts (finally
		changing dev_v3 into devinit style).
20070118	Removing the "bus" registry concept from machine.h, because
		it was practically meaningless.
		Continuing on the rewrite of Algor V3 ISA interrupts.
20070121	More work on Algor interrupts; they are now working again,
		well enough to run NetBSD/algor. :-)
20070122	Converting VR41xx (HPCmips) interrupts. NetBSD/hpcmips
		can be installed using the new interrupt system :-)
20070123	Making the testmips mode work with the new interrupt system.
20070127	Beginning to convert DEC5800 devices to devinit, and to the
		new interrupt system.
		Converting Playstation 2 devices to devinit, and converting
		the interrupt system. Also fixing a severe bug: the interrupt
		mask register on Playstation 2 is bitwise _toggled_ on writes.
20070128	Removing the dummy NetGear machine mode and the 8250 device
		(which was only used by the NetGear machine).
		Beginning to convert the MacPPC GC (Grand Central) interrupt
		controller to the new interrupt system.
		Converting Jazz interrupts (PICA61 etc.) to the new interrupt
		system. NetBSD/arc can be installed again :-)
		Fixing the JAZZ timer (hardcoding it at 100 Hz, works with
		NetBSD and it is better than a completely dummy timer as it
		was before).
		Converting dev_mp to the new interrupt system, although I
		haven't had time to actually test it yet.
		Completely removing src/machines/interrupts.c, cpu_interrupt
		and cpu_interrupt_ack in src/cpu.c, and
		src/include/machine_interrupts.h! Adding fatal error messages
		+ abort() in the few places that are left to fix.
		Converting dev_z8530 to the new interrupt system.
		FINALLY removing the md_int struct completely from the
		machine struct.
		SH4 fixes (adding a PADDR invalidation in the ITLB replacement
		code in memory_sh.c); the NetBSD/dreamcast LiveCD now runs
		all the way to the login prompt, and can be interacted with :-)
		Converting the CPC700 controller (PCI and interrupt controller
		for PM/PPC) to the new interrupt system.
20070129	Fixing MACE ISA interrupts (SGI IP32 emulation). Both NetBSD/
		sgimips' and OpenBSD/sgi's ramdisk kernels can now be
		interacted with again.
20070130	Moving out the MIPS multi_lw and _sw instruction combinations
		so that they are auto-generated at compile time instead.
20070131	Adding detection of amd64/x86_64 hosts in the configure script,
		for doing initial experiments (again :-) with native code
		generation.
		Adding a -k command line option to set the size of the dyntrans
		cache, and a -B command line option to disable native code
		generation, even if GXemul was compiled with support for
		native code generation for the specific host CPU architecture.
20070201	Experimenting with a skeleton for native code generation.
		Changing the default behaviour, so that native code generation
		is now disabled by default, and has to be enabled by using
		-b on the command line.
20070202	Continuing the native code generation experiments.
		Making PCI interrupts work for Footbridge again.
20070203	More native code generation experiments.
		Removing most of the native code generation experimental code,
		it does not make sense to include any quick hacks like this.
		Minor cleanup/removal of some more legacy MIPS interrupt code.
20070204	Making i80321 interrupts work again (for NetBSD/evbarm etc.),
		and fixing the timer at 100 Hz.
20070206	Experimenting with removing the wdc interrupt slowness hack.
20070207	Lowering the number of dyntrans TLB entries for MIPS from
		192 to 128, resulting in a minor speed improvement.
		Minor optimization to the code invalidation routine in
		cpu_dyntrans.c.
20070208	Increasing (experimentally) the nr of dyntrans instructions per
		loop from 60 to 120.
20070210	Commenting out (experimentally) the dyntrans_device_danger
		detection in memory_rw.c.
		Changing the testmips and baremips machines to use a revision 2
		MIPS64 CPU by default, instead of revision 1.
		Removing the dummy i960, IA64, x86, AVR32, and HP PA-RISC
		files, the PC bios emulation, and the Olivetti M700 (ARC) and
		db64360 emulation modes.
20070211	Adding an "mp" demo to the demos directory, which tests the
		SMP functionality of the testmips machine.
		Fixing PReP interrupts some more. NetBSD/prep now boots again.
20070216	Adding a "nop workaround" for booting Mach/PMAX to the
		documentation; thanks to Artur Bujdoso for the values.
		Converting more of the MacPPC interrupt stuff to the new
		system.
		Beginning to convert BeBox interrupts to the new system.
		PPC603e should NOT have the PPC_NO_DEC flag! Removing it.
		Correcting BeBox clock speed (it was set to 100 in the NetBSD
		bootinfo block, but should be 33000000/4), allowing NetBSD
		to start without using the (incorrect) PPC_NO_DEC hack.
20070217	Implementing (slow) AltiVec vector loads and stores, allowing
		NetBSD/macppc to finally boot using the GENERIC kernel :-)
		Updating the documentation with install instructions for
		NetBSD/macppc.
20070218-19	Regression testing for the release.

==============  RELEASE 0.4.4  ==============


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

  ViewVC Help
Powered by ViewVC 1.1.26