# Thread: Code for Pseudo Random Bit Sequence Generator Using buffer Concept

1. ## Code for Pseudo Random Bit Sequence Generator Using buffer Concept

I am working with Vivado HLS for function "Pseudo Random Bit Sequence Generator". I need C code for PRBS using buffer concept.
Can anyone help in writing and executing this program  using storage concept.

I tried this code, But Not working

Code:
```bool pseudo_random() {

static ap_uint<3> lfsr=5;

//x^3 + x^2 + 1
bool b_3 = lfsr.get_bit(3-3);
bool b_2 = lfsr.get_bit(3-2);

bool new_bit = b_3 ^ b_2 ;
lfsr = lfsr &gt;&gt; 1;
lfsr.set_bit(2, new_bit);

return lfsr.get_bit(0);

}```
2.
Code:
``` int16_t prbs(int16_t);

int main(void)
{
xil_printf("%c[2J",27);
uint16_t start_state = 0x7D0;  /* Any nonzero start state will work. */
uint16_t lfsr = start_state;
uint16_t count = 0;
do
{
lfsr = prbs(lfsr) ;
//printf("value= 0x%02X\n", lfsr);
count++;
} while (count < 50);
return 0;
}

int16_t prbs(int16_t lfsr)
{
bool bit;                    /* Must be 16bit to allow bit<<15 later in the code */
/* taps: 16 14 13 11; feedback polynomial: x^16 + x^14 + x^13 + x^11 + 1 */
// bit  = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
// lfsr =  (lfsr >> 1) | (bit << 15);
bit  = ((lfsr >> 0) ^ (lfsr >> 2) ) & 1;
lfsr =  (lfsr >> 1) | (bit << 10);
printf("%d",bit);
return lfsr;
}```

Reference link :AR# 59228: 2013.4 Vivado HLS - Example showing how to use logic debug to test an AXI Lite Slave and AXI Master interface, and then verify it in SDK. 2. [QUOTE=SarasMuthu;1267973]I am working with Vivado HLS for function "Pseudo Random Bit Sequence Generator". I need C code for PRBS using buffer concept.
Can anyone help in writing and executing this program  using storage concept.

I tried this code, But Not working

Code:
```bool pseudo_random() {

static ap_uint<3> lfsr=5;

//x^3 + x^2 + 1
bool b_3 = lfsr.get_bit(3-3);
bool b_2 = lfsr.get_bit(3-2);

bool new_bit = b_3 ^ b_2 ;
lfsr = lfsr &gt;&gt; 1;
lfsr.set_bit(2, new_bit);

return lfsr.get_bit(0);

}```
2.
Code:
``` int16_t prbs(int16_t);

int main(void)
{
xil_printf("%c[2J",27);
uint16_t start_state = 0x7D0;  /* Any nonzero start state will work. */
uint16_t lfsr = start_state;
uint16_t count = 0;
do
{
lfsr = prbs(lfsr) ;
//printf("value= 0x%02X\n", lfsr);
count++;
} while (count < 50);
return 0;
}

int16_t prbs(int16_t lfsr)
{
bool bit;                    /* Must be 16bit to allow bit<<15 later in the code */
/* taps: 16 14 13 11; feedback polynomial: x^16 + x^14 + x^13 + x^11 + 1 */
// bit  = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
// lfsr =  (lfsr >> 1) | (bit << 15);
bit  = ((lfsr >> 0) ^ (lfsr >> 2) ) & 1;
lfsr =  (lfsr >> 1) | (bit << 10);
printf("%d",bit);
return lfsr;
}```
I want to execute PRBS using buffer concept. Through basic knowledge i have written the Codes for Pseudo_random binary sequence using buffer concept but showing error.

Code:
```int main() {
{
uint32 buff[];
static unsigned lfsr  = 0xCD;
int bit, i, count;
for ( i = 0; i > 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4) ) & 1;
lfsr = (lfsr >> 1) | (bit << 7);
buff[] =bit;
}
memcpy(buff+i, bit, bit* sizeof(int));
buff +=1;
for (i = 0; i < 50; i++) {
buff[i+1]= buff[i]+ 1;
}
printf("%d", &buff);
}
}``` 3. Originally Posted by SarasMuthu
I tried this code, But Not working
Code:
```bool pseudo_random() {

static ap_uint<3> lfsr=5;

//x^3 + x^2 + 1
bool b_3 = lfsr.get_bit(3-3);
bool b_2 = lfsr.get_bit(3-2);

bool new_bit = b_3 ^ b_2 ;
lfsr = lfsr &gt;&gt; 1;
lfsr.set_bit(2, new_bit);

return lfsr.get_bit(0);

}```
You do realise that it is unwise to just randomly try code without understanding them at least in part? What you have posted looks like C++ rather than C, and furthermore contains an obvious text formatting error (the presence of HTML entities).

If I were you, I would investigate two things:
• A pseudorandom integer generator.
• This "buffer concept" that you mentioned, presumably involving turning a pseudorandom integer into a buffer of bits.

You shouldn't bother trying to implement the pseudorandom integer generator yourself: there are many libraries that already exist and have been tested by others, e.g., you could look up "Mersenne Twister" for ready-to-use source code for that algorithm, likely with a license suitable for your purposes. For testing, you could even just use rand() from <stdlib.h>!

Actually yes. Use rand(). You can always swap it out for something better/more certain later. From RAND_MAX you can make an assumption/estimation about how many random bits there are per integer returned by rand(), and therefore work out how many times your pseudorandom bit function can be called before it exhausts its buffer and you have to call rand() again. 4. I am trying PRBS for my project work (FPGA Hardware). Using buffer, i need to store bit`s values 0`s and 1`s. Without buffer, i cant retrieve my result into hardware.

Code:
```int main()
{
unsigned lfsr  = 0xCD;
int bit, i;
for ( i = 0; i < 50; i++)
{
bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4) ) & 1;
lfsr = (lfsr >> 1) | (bit << 7);
}
printf("%d", bit);
return 0;
}```
if i put printf inside the loop its showing random values but i dont want that. I need buffer to store all values and return the bits. Please suggest me. How to use buffer concept ? Originally Posted by laserlight You do realise that it is unwise to just randomly try code without understanding them at least in part? What you have posted looks like C++ rather than C, and furthermore contains an obvious text formatting error (the presence of HTML entities).

If I were you, I would investigate two things:
• A pseudorandom integer generator.
• This "buffer concept" that you mentioned, presumably involving turning a pseudorandom integer into a buffer of bits.

You shouldn't bother trying to implement the pseudorandom integer generator yourself: there are many libraries that already exist and have been tested by others, e.g., you could look up "Mersenne Twister" for ready-to-use source code for that algorithm, likely with a license suitable for your purposes. For testing, you could even just use rand() from <stdlib.h>!

Actually yes. Use rand(). You can always swap it out for something better/more certain later. From RAND_MAX you can make an assumption/estimation about how many random bits there are per integer returned by rand(), and therefore work out how many times your pseudorandom bit function can be called before it exhausts its buffer and you have to call rand() again. 5. Originally Posted by SarasMuthu
I need buffer to store all values and return the bits. Please suggest me. How to use buffer concept ?
By generating a pseudorandom integer, then extracting its bits into a buffer, either in advance (e.g., into a buffer of unsigned char that you then access on subsequent calls until the buffer is exhausted), or when needed (e.g., by using bitwise operations on the integer itself on each call, until the number of pseudorandom bits of the integer is exhausted). When the buffer is exhausted, generate a new pseudorandom integer.

If you don't understand this, then stop thinking about pseudorandom bit generators using a "buffer concept": the concept is way too advanced for you right now, and trying to tackle it head on means that you will fail. Instead, think about how you could turn an integer like 123 into a buffer of bits. If you get stuck on this, then think about how you would separately print the bits of an integer like 123 (this is exceedingly trivial; if you cannot do this, revise your material on bitwise operations). Popular pages Recent additions bit, bool, c code, lfsr, uint16_t 