[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

random numbers (resend)




[my first send bounced so I am resending]

Here is some code to run on a SparcStaion (I or II) to generate random
numbers based on noise from the audio chip.  I have not tested the true
randomness of the out but cause I don't know how.  Thus if anyone can
please do and send me the results.  I suppose some minor tweeking can be
made for the mid range gain for a better spread. 

unfortunatly there is no way to turn off the u-law audio compression
the chip uses to outputs data if you plot the out put you will see the
problem the compression caused.



#! /bin/sh
# here be a shar file
echo x - Audio/Makefile
cat >Audio/Makefile <<'!E!O!F!'

LD= $(CC)
CC= cc

LDFLAGS=-g
CFLAGS=-g -I/usr/demo/SOUND -I/usr/local/include

LIBS= -L/usr/demo/SOUND/ -laudio

audio_rand: audio_rand.o
	$(LD) $(LDFLAGS) audio_rand.o $(LIBS) -o $@

test: audio_rand
	audio_rand | head -10000 > \#fff
	echo "plot '#fff'" | gnuplot

!E!O!F!
#! /bin/sh
echo x - Audio/audio_rand.c
cat >Audio/audio_rand.c <<'!E!O!F!'
static char *_author = "Peter Shipley\n";

#define AUDIO_CHIP
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sun/audioio.h>
#include <sbusdev/audio_79C30.h>
#include <multimedia/ulaw2linear.h>

static char *_who_did_it = "Peter M Shipley (1992) [v0.1]\n";

/* X:  0-15 
   R:  16-31
   GX: 32-33
   GR: 33-34
*/

#define MMR2_BITS 45


#define GX_GAIN 32


main()
{
struct audio_ioctl ai;
extern void bzero();
int fd;
int j, i;

    if( (fd = open("/dev/audio", O_RDONLY)) < 0) {
	perror("open:");
	exit(1);
    }

    bzero(ai, sizeof(struct audio_ioctl) );

    ai.control = AUDIO_MAP_ALL;

    if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 ) {
	perror( "AUDIOGETREG ALL:" );
	exit(1);
    }
    /* set audio input to a unconnected pin (audio input B) */
    ai.data[MMR2_BITS] = ( ai.data[MMR2_BITS] & ~AUDIO_MMR2_BITS_AINB);

    /* set gain of the GX reg to the max */
    ai.data[GX_GAIN] = 0x7F;
    ai.data[GX_GAIN+1] = 0x0;

    for(i=0;;) {
	char buf[BUFSIZ];

	read(fd, buf, BUFSIZ);

	for(j=0; j<BUFSIZ ; j++)
	    printf("%d %d\n", i++, audio_u2s(buf[j]));
    }
}
!E!O!F!
#! /bin/sh
echo x - Audio/reg.c
cat >Audio/reg.c <<'!E!O!F!'
static char *_author = "Peter Shipley\n";

#define AUDIO_CHIP
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sun/audioio.h>
#include <sbusdev/audio_79C30.h>

static char * print_byte();

static struct _map {
	char *label;
	char size;
} map[] = {
    "X filter", 16,
    "R filter", 16,
    "GX Gain", 2,
    "GR Gain", 2,
    "GER Gain", 2,
    "Sidetone", 2,
    "Frequency Tone gen", 2,
    "Amplitude Tone gen", 2,
    "MMR1", 1,
    "MMR2", 1
};

#define NMAPS  (sizeof(map) / sizeof(struct _map) )

struct audio_ioctl ai;

main()
{
extern void bzero();
int fd;
int oldmmr2;
int newmmr2;

    if( (fd = open("/dev/audio", O_RDWR)) < 0) {
	perror("open:");
	exit(1);
    }

    dump(fd);

    /*
    bzero(ai, sizeof(struct audio_ioctl) );
    ai.control = AUDIO_MAP_GX;
    if ( ioctl( fd, AUDIOGETREG, &ai ) < 0 ) {
	perror( "AUDIOGETREG MMR2:" );
	exit(1);
    }

    ai.data[0] = ai.data[1] = 0;
    if ( ioctl( fd, AUDIOSETREG, &ai ) < 0 ) {
	perror( "AUDIOSETREG MMR2:" );
	exit(1);
    }

    */

    sleep(5);
    dump(fd);

}


dump(f)
int f;
{
int i,k;

    bzero(ai, sizeof(struct audio_ioctl) );
    ai.control = AUDIO_MAP_ALL;
    if ( ioctl( f, AUDIOGETREG, &ai ) < 0 ) {
	perror( "AUDIOGETREG MMR2:" );
	exit(1);
    }


    k=0;
    for(i = 0 ; i < NMAPS ; i++) {
	int j;
	(void) printf("%s:\n", map[i].label);
	for(j=1; j <= map[i].size; j++,k++) {
	    (void) printf("%10s", print_byte(ai.data[k]));
	    if ( (j % 7) == 0)
		(void) fputc('\n', stdout);
	}
	(void) fputc('\n', stdout);
    }

    (void) fputc('\n', stdout);
}




static char *
print_byte(c)
char c;
{
static char bt[9];

    bt[0] = ( (c & 0x01) ? '1' : '0');
    bt[1] = ( (c & 0x02) ? '1' : '0');
    bt[2] = ( (c & 0x03) ? '1' : '0');
    bt[3] = ( (c & 0x04) ? '1' : '0');
    bt[4] = ( (c & 0x05) ? '1' : '0');
    bt[5] = ( (c & 0x06) ? '1' : '0');
    bt[6] = ( (c & 0x07) ? '1' : '0');
    bt[7] = ( (c & 0x08) ? '1' : '0');
    bt[8] = NULL;

    return bt;
}
!E!O!F!