45 lines
1.1 KiB
C
Raw Normal View History

2023-10-31 07:47:27 +01:00
#include "system/task_rand.h"
#include "system/hardware_task.h"
#include "system/data_channel.h"
#include "system/float_word.h"
2024-12-04 08:58:18 +01:00
#include <stdio.h>
#include <system.h>
2023-10-31 07:47:27 +01:00
int task_rand_run( void * task ) {
2024-12-04 08:58:18 +01:00
// Nachfolgende Anweisungen Schreiben 1024 Mal den seed Wert in den FIFO für Rand
rand_config * config = ( rand_config * ) task;
float_word seed = {.value = config->seed};
2023-10-31 07:47:27 +01:00
2024-12-04 08:58:18 +01:00
uint32_t lfsr = seed.word;
uint32_t bit = 0; // Must be 32-bit to allow bit << 31 later in the code
for( uint32_t i = 0; i < DATA_CHANNEL_DEPTH; i++ ) {
float_word res;
uint32_t sign = (lfsr >> 31) & 1;
uint32_t exponent;
uint32_t mantisse = lfsr & 0x7FFFFF;
if((lfsr >> 30) & 1) // MSB exponent
{
exponent = 128 + ((lfsr >> 23) & 1); // 128 to 129
}
else
{
exponent = 124 + ((lfsr >> 23) & 3); // 124 to 127
}
2023-10-31 07:47:27 +01:00
2024-12-04 08:58:18 +01:00
uint32_t ieee754 = (sign << 31) | (exponent << 23) | mantisse;
res.value = ieee754;
data_channel_write( config->base.sink, ieee754);
// fibonacci feedback polynomial: x^31 + x^21 + x^1 + 1
bit = ((lfsr >> 31) ^ (lfsr >> 21) ^ (lfsr >> 1)) & 1;
lfsr = (lfsr << 1) | bit;
}
return 0;
}