This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprng.c
86 lines (69 loc) · 2.59 KB
/
prng.c
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
/**
* @file prng.c
* @author Michael Foukarakis
* @date Created: Wed Mar 12, 2014 14:03 GMT
* Last Update: Thu Oct 06, 2016 14:43 CEST
* @description PRNG using linear feedback shift registers.
* One shift register is used to provide data, while another register is used
* to "clock" data output; meaning a data bit is output when the clock
* register outputs a one bit, otherwise the data bit is dropped.
*------------------------------------------------------------------------
* -*- coding: utf-8 -*-
*------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include "lfsr.h"
lfsr_t glfsr_d0;
lfsr_t glfsr_c0;
int main(int argc, char **argv)
{
unsigned char bit0, bitc0, bitr = 0;
char byte = 0, bitpos = 7;
unsigned long long bitcounter = 0, ones = 0, zeros = 0, dropped = 0;
lfsr_data_t polynom_d, init_value_d,
polynom_c, init_value_c;
if (argc < 5) {
fprintf(stderr, "Usage: %s <data_polynomial> <data_seed> <clock_polynomial> <clock_seed>\n", argv[0]);
exit(1);
}
FILE *fp = fopen("bitstream.bin", "w");
if(!fp) {
fprintf(stderr, "Error opening output file.\n");
exit(-1);
}
sscanf(argv[1], "%llx", &polynom_d);
sscanf(argv[2], "%llx", &init_value_d);
GLFSR_init(&glfsr_d0, polynom_d, init_value_d);
sscanf(argv[3], "%llx", &polynom_c);
sscanf(argv[4], "%llx", &init_value_c);
GLFSR_init(&glfsr_c0, polynom_c, init_value_c);
do {
bit0 = GLFSR_next(&glfsr_d0);
bitc0 = GLFSR_next(&glfsr_c0);
if (glfsr_d0.data == init_value_d)
printf("GLFSR D0 overflow at %llu.\n", bitcounter);
if (glfsr_c0.data == init_value_c)
printf("GLFSR C0 overflow at %llu.\n", bitcounter);
printf("Next bits: %d, %d\t= ", bit0, bitc0);
if (bitc0) {
bitr = bit0;
printf("%d\n", bitr);
if (bitpos < 0) {
fprintf(fp, "%c", byte);
bitpos = 7;
byte = 0;
}
byte |= bitr << bitpos;
bitpos--;
bitr ? ones++ : zeros++;
} else {
printf("dropped\n");
dropped++;
}
bitcounter++;
} while (!((glfsr_d0.data == init_value_d) && (glfsr_c0.data == init_value_c)));
fclose(fp);
printf("\nNumber of steps: %llu (%llu bytes)\nOnes: %llu, zeros: %llu, dropped: %llu\n", bitcounter, (zeros + ones) / 8, ones, zeros, dropped);
return 0;
}