Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
17 years ago

Writing to memory

I'm a beginner in FPGA, working on Stratix III DE3 340.

I got a set of serial data, converted it into parallel form, and now I need to write the output of a deserialiser into memory (for processing later). How do I begin? Is there some kind of megafunction for this?

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The exact answer depends on the details of your system, but assuming you're getting one bit at a time and just want to store them in blockram, then by far the simplest is to use the fact that you can use different bit widths for the two different ports. Eg, you can have one be 1 bit wide and read it out 16-bit at a time on the other.

    In the more general case, you can accumulate the bits in a shift register and every time you have collected a word worth of data, you write it to memory.

    Untested example code (not production code, but written for illustration):

    module memory(
       input clock,
       input serial_in,
       input serial_valid,
       input  read_address,
       output reg  read_data);
      reg  word;
      reg  bits_collected = 0;
      reg  bits = 0;
      reg  write_address = 0;
      always @(posedge clock) begin
        read_data <= word;
        if (serial_valid) begin
           bits <= {serial_in, bits}; // lowest bit first
           bits_collected <= bits_collected + 1;
           if (bits_collected == 32) begin
              word <= bits;
              write_address <= write_address + 1;
              bits_collected <= 1;
           end
      end
    endmodule