-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPT4_Entropy.cpp
132 lines (112 loc) · 3.05 KB
/
GPT4_Entropy.cpp
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
//GPT-4 refactoring Walter Anderson's Entropy.cpp library.
//Please note that you need to handle any exceptions thrown in the code, such as `std::invalid_argument`. Since Arduino doesn't support exceptions by //default due to the memory footprint, you might need to find an alternate way to handle the error. If you decide to use exceptions, you'll need to enable //them in your compiler settings.
#include <Arduino.h>
#include <Entropy.h>
constexpr uint8_t WDT_MAX_8INT = 0xFF;
constexpr uint16_t WDT_MAX_16INT = 0xFFFF;
constexpr uint32_t WDT_MAX_32INT = 0xFFFFFFFF;
#ifndef ARDUINO_SAM_DUE
constexpr uint8_t gWDT_buffer_SIZE = 32;
constexpr uint8_t WDT_POOL_SIZE = 8;
uint8_t gWDT_buffer[gWDT_buffer_SIZE];
uint8_t gWDT_buffer_position = 0;
uint8_t gWDT_loop_counter = 0;
volatile uint8_t gWDT_pool_start = 0;
volatile uint8_t gWDT_pool_end = 0;
volatile uint8_t gWDT_pool_count = 0;
volatile uint32_t gWDT_entropy_pool[WDT_POOL_SIZE];
#endif
// This function initializes the global variables needed to implement the circular entropy pool and
// the buffer that holds the raw Timer 1 values that are used to create the entropy pool.
void EntropyClass::Initialize(void)
{
// Code removed for brevity
}
uint32_t EntropyClass::random(void)
{
#ifdef ARDUINO_SAM_DUE
while (! (TRNG->TRNG_ISR & TRNG_ISR_DATRDY));
retVal = TRNG->TRNG_ODATA;
#else
while (gWDT_pool_count < 1);
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
retVal = gWDT_entropy_pool[gWDT_pool_start];
gWDT_pool_start = (gWDT_pool_start + 1) % WDT_POOL_SIZE;
--gWDT_pool_count;
}
#endif
return retVal;
}
uint8_t EntropyClass::random8(void)
{
static uint8_t byte_position = 0;
uint8_t retVal8;
if (byte_position == 0)
share_entropy.int32 = random();
retVal8 = share_entropy.int8[byte_position++];
byte_position %= 4;
return retVal8;
}
uint16_t EntropyClass::random16(void)
{
static uint8_t word_position = 0;
uint16_t retVal16;
if (word_position == 0)
share_entropy.int32 = random();
retVal16 = share_entropy.int16[word_position++];
word_position %= 2;
return retVal16;
}
uint32_t EntropyClass::random(uint32_t max)
{
uint32_t slice;
if (max < 2)
retVal = 0;
else
{
retVal = WDT_MAX_32INT;
// Code removed for brevity
}
return retVal;
}
uint32_t EntropyClass::random(uint32_t min, uint32_t max)
{
uint32_t tmp_random, tmax;
tmax = max - min;
if (tmax < 1)
retVal = min;
else
{
tmp_random = random(tmax);
retVal = min + tmp_random;
}
return retVal;
}
// Remaining functions left out for brevity...
#ifndef ARDUINO_SAM_DUE
static void isr_hardware_neutral(uint8_t val)
{
// Code removed for brevity...
}
#endif
#if defined( __AVR_ATtiny25__ ) || defined( __AVR_ATtiny45__ ) || defined( __AVR_ATtiny85__ )
ISR(WDT_vect)
{
isr_hardware_neutral(TCNT0);
}
#elif defined(__AV
R__)
ISR(WDT_vect)
{
isr_hardware_neutral(TCNT1L);
}
#endif
#ifndef ARDUINO_SAM_DUE
void wdt_init(void) attribute((naked)) attribute((section(".init3")));
void wdt_init(void)
{
// Code removed for brevity...
}
#endif
EntropyClass Entropy;