File: reg_pseudo.c

package info (click to toggle)
gcpegg 5.1-7
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 324 kB
  • ctags: 437
  • sloc: ansic: 3,695; makefile: 94; sh: 27; csh: 21
file content (135 lines) | stat: -rw-r--r-- 3,212 bytes parent folder | download | duplicates (7)
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
/* PROGRAM:     eggsh
 * FILE:        $Header: /home/egg/src/RCS/reg_pseudo.c,v 1.1 1998/12/31 22:04:39 ghn Exp $
 * PURPOSE:     PEAR (Bradish box/micro-REG) hardware interface
 * AUTHOR:      Greg Nelson
 * DATE:        98-04-12
 *
 * REVISED:     $Log: reg_pseudo.c,v $
 * REVISED:     Revision 1.1  1998/12/31 22:04:39  ghn
 * REVISED:     Initial revision
 * REVISED:
 * REVISED:     Revision 1.3  1998/08/01 18:50:39  ghn
 * REVISED:     Added John's byte-order-independence changes and PSEUDO support.
 * REVISED:
 * REVISED:     Revision 1.2  1998/08/01 17:13:51  ghn
 * REVISED:     Added John's Solaris support and DUMPREG option.
 * REVISED:
 * REVISED:     Revision 1.1  1998/07/21 11:37:41  ghn
 * REVISED:     Initial revision
 * REVISED:
 * Copyright 1998 - Greg Nelson
 * Redistributable under the terms of the GNU Public Licence (GPL)
 */

/* Define this to dump samples from the REG which we actually use into
   a file named dumpreg.dat.  */
/* #define DUMPREG */

#include <stdio.h>
#include <unistd.h>
#include <time.h>

#include "global.h"
#include "genlib.h"
#include "reg.h"
#include "lecuyer.h"

#define MAXDEV  20

static int32 oldbits[MAXDEV], bitsleft[MAXDEV];

#ifdef DUMPREG
static FILE *dumpfile;                /* REG dump file handle */
static unsigned char dumpbuf[1024];   /* REG dump buffer */
static int dumpptr = 0;               /* Pointer into dump buffer */
#endif

static int32 OpenDev(DevOpts *opts) {
    int32 seed;
    int i;

    seed = time(NULL);
    LEsetSeed(seed);

    for (i = 0; i < 37; i++) {
        seed = (seed << 3) ^ LEnextByte();
    }
    LEsetSeed(seed);
    seed = LEnextByte();
    for (i = 0; i < (seed & 0x37); i++) {
        (void) LEnextByte();
    }
    return 0;
}

static int32 Sample(int32 dd, uint16 bits) {
  int32 bc, sum;
  uint8 c1;

  sum = bc = 0;
  while (bc < bits) {
    if (bitsleft[dd]) {
      sum += (oldbits[dd] & 0x01);
      oldbits[dd] >>= 1;
      bitsleft[dd]--;
      bc++;
    } else {
      c1 = LEnextByte();
#ifdef DUMPREG
      dumpbuf[dumpptr++] = c1;
      if (dumpptr >= sizeof(dumpbuf)) {
        fwrite(dumpbuf, sizeof(dumpbuf), 1, dumpfile);
        dumpptr = 0;
      }
#endif
      oldbits[dd] = c1;
      bitsleft[dd] = 8;
    }
  }

  return sum;
}

#define SAMP_PERIOD     1000    /* msec */
#define MARGIN          .95     /* how much to headroom to allow in
                                   speed measurement */

static int32 EvalSpeed(int32 dd) {
  struct timeval start, end;
  int32 bitct, samp;

  gettimeofday(&start, NULL);
  bitct = 0;
  while (1) {
    gettimeofday(&end, NULL);
    if (deltams(&end, &start) >= SAMP_PERIOD) break;
    samp = Sample(dd, 1);
    bitct++;
  }

  return (int32)(bitct * MARGIN);
}

static int32 Discard(int32 dd) {
  int32 disc;
  
  disc = bitsleft[dd];
  bitsleft[dd] = 0;
  return disc;
}

static int32 CloseDev(int32 dd) {
  return 0;
}

/*  Driver description table.  */

REG_driver REG_pseudo = {
                        "PSEUDO", 
                        2000,
                        OpenDev,
                        EvalSpeed,
                        Sample,
                        Discard,
                        CloseDev
                      };