RAM is a useful thing to have when you need to store bits of information for later, the code below is for a simple, single port write first, RAM module and test bench. This may seem like a great bit of code to have lying around, but in reality it’s more for educational purposes. If you were actually going to use a RAM module like the one below you should really use a RAM generator from the manufacture of what ever device you are using ie: Xilinx, Altera, Lattice… These generated module are designed to take advantage of special RAM blocks built into the FPGA which allows them to be fast and efficient.
The function of this module is simple, by changing the address bits Address you can then read, on the next clock cycle, the data that is present at that memory location on the DataOut pins. In order to write data to a specific memory location you simply supply the data and address to the DataIn and Address lines respectively and then toggle the write enable WriteEn high for one clock cycle. The Enable bit is a global enable that prevents any actions from happening if the pin is low.
To change the width and depth of this module you only need to change the two generics at the top of the module. The DATA_WIDTH controls how many bits are in the input and out data. To adjust how many memory locations are in the RAM module you need to adjust the address width ADDRESS_WIDTH, this controls how many bits are in the address input and the number of RAM location will be spots. For example if you have an address width of 8, like below, you would be able to read 256 different memory locations.
7/6/2013: Removed need for ADDR_COUNT generic. Updated port and generic names for consistency.