Data comes in from a module where the valid bits are going to be between 1 and 32. A word comes along with data that indicates how many bits are valid starting from the least significant bit. This data stream needs to be combined into a register. When the register reaches 128 bits of data, the data is shifted to external memory (FIFO in this case) and the process starts again.
Here is a diagram:
A simple way to do this is to create a mux. The mux has 256 outputs. Each input is the variable size input shifted by one extra bit. So with sel 0 we have data shifted by 0 bits, with sel 1 we have data shifted by 1 bit and so on. We then OR the new data with the data in the register. The register is always created to zero. The solution works but creates a very big multiplexer and fails timing!
This means that this process must be pipelined. But I am not completely sure how it should be pipelined. One way is that there be a large number of pipeline stages (where each stage shifts data away from the least significant bit) like this: first stage can shift data by 128 bits, next stage can shift data by 64 bits, next stage can shift data by 32 bits, next stage can shift data by 16 bits, next stage can shift data by 8 bits, next stage can shift data by 4 bits, next stage can shift data by 2 bits and last stage can shift data by 1 bit. However, a mechanism must still be worked out to decide if the shift is applied or the data is passed to next stage unchanged.
Is this a reasonable approach or there is a better way to design this?