Loesung des Praktikums Systementwurf - Bjarne Hoesch - Bernhard Schoeffel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rand.c 1.3KB

1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "system/task_rand.h"
  2. #include "system/hardware_task.h"
  3. #include "system/data_channel.h"
  4. #include "system/float_word.h"
  5. #include "stdio.h"
  6. #define POLYNOM ((1 << 31)|(1 << 21)|(1 << 1)|(1 << 0))
  7. int32_t shift_lsfr(int32_t *lsfr) {
  8. int feedback;
  9. feedback = *lsfr & 1;
  10. *lsfr >>= 1;
  11. if(feedback == 1)
  12. *lsfr ^= POLYNOM;
  13. return *lsfr;
  14. }
  15. int32_t pot(int32_t base, int exp) {
  16. int32_t res = 1;
  17. while(exp-- >= 0)
  18. res *= base;
  19. return res;
  20. }
  21. uint8_t clamp_value(uint8_t value) {
  22. if(value & 128) {
  23. value &= 0b10000001;
  24. //value &= ((1 << 7) | (1 << 0));
  25. } else {
  26. //value |= ~((1 << 1) | (1 << 0));
  27. value |= 0b01111100;
  28. }
  29. return value;
  30. }
  31. int task_rand_run( void * task ) {
  32. rand_config* config = ( rand_config* ) task;
  33. float_word seed = { .value = config->seed };
  34. uint32_t lfsr = seed.word;
  35. uint32_t data_channel_base = config->base.sink;
  36. data_channel_clear(data_channel_base);
  37. for(uint32_t i = 0; i < DATA_CHANNEL_DEPTH; ++i) {
  38. float_word res, value;
  39. //res.value = lfsr;
  40. //
  41. uint32_t E = (value.word >> 23) & 0xff;
  42. E = clamp_value(E);
  43. res.word = value.word;
  44. res.word &= ~(0xFF << 23);
  45. res.word |= (E << 23);
  46. value.word = shift_lsfr(&lfsr) ;
  47. data_channel_write( data_channel_base, res.word);
  48. }
  49. return 0;
  50. }