hdl.RAM - Single, simple dual, dual, true dual, or simple tri-port RAM for memory read/write

  access  - MATLAB ([original](https://www.mathworks.com/help/hdlcoder/ref/hdl.ram-system-object.html)) ([raw](?raw))

Single, simple dual, dual, true dual, or simple tri-port RAM for memory read/write access

Description

The hdl.RAM System object™ reads from and writes to memory locations for a single, simple dual, dual, true dual, or simple tri-port RAM. The output data is delayed one step. If your input data is:

The hdl.RAM System object can have 231 bytes of internal storage. The RAM size takes into account the address width, the number of bytes that are used to store each word, and the number of RAM banks.

To read from or write to memory locations in the RAM:

  1. Create the hdl.RAM object and set its properties.
  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects?

Creation

Syntax

Description

`ram` = hdl.RAM returns a single port RAM System object that you can write to or read from a memory location.

example

`ram` = hdl.RAM(`Name`,`Value`) returns a single, simple dual, dual, true dual, or simple tri-port RAM System object with properties set using one or more name-value pairs.

example

Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and therelease function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, seeSystem Design in MATLAB Using System Objects.

Type of RAM, specified as either:

Option to use the asynchronous read feature in your target hardware, specified as a numeric or logical 1 (true) or 0 (false). Boards that support asynchronous read allow the hardware to execute a read instruction immediately instead of waiting one cycle. This property is not available when you specify RAMType asTrue dual port.

Behavior for write output, specified as either:

Dependencies

Specify this property when you set RamType to'Single port', 'Dual port', or 'True dual port'. This property does not apply for the Simple Dual Port RAM or the Simple Tri Port RAM object. This property is not available if you set AsyncRead totrue.

Initial simulation output of the System object, specified as either:

Since R2024b

If you specify:

Dependencies

To enable this property, set AsyncRead to 0 (false).

Since R2025a

If you specify:

Dependencies

To enable this property, set ModelRAMDelay to 0 (false) or AsyncRead to 1 (true).

Usage

Syntax

Description

[dataOut](#d126e84386) = ram([wrData](#d126e83516),[rwAddress](#d126e83590),[wrEn](#d126e83673)) reads the value in memory location rwAddress whenwrEn is false and writes the valuewrData into the memory location rwAddress whenwrEn is true. dataOut is the new or old data at rwAddress. Use this syntax when you create a single port RAM System object.

[rdDataOut](#d126e84415) = ram([wrData](#d126e83516),[wrAddress](#d126e83788),[wrEn](#d126e83673),[rdAddress](#d126e83718)) writes the value wrData into memory locationwrAddress when wrEn is true.rdDataOut is the old data at the address locationrdAddress. Use this syntax when you create a simple dual-port RAM System object.

[[wrDataOut](#d126e84445),[rdDataOut](#d126e84415)] = ram([wrData](#d126e83516),[wrAddress](#d126e83788),[wrEn](#d126e83673),[rdAddress](#d126e83718)) writes the value wrData into the memory locationwrAddress when wrEn is true.wrDataOut is the new or old data at memory locationwrAddress, and rdDataOut is the old data at the address location rdAddress. Use this syntax when you create a dual-port RAM System object.

[[wrDataOutA](#mw%5F6a0a637c-0620-4804-9853-d93ea94969f5),[wrDataOutB](#mw%5F7f3fc4ad-910b-406f-8738-18e1a50e48f7)] = ram([wrDataA](#mw%5F048b267e-739c-40d0-86db-34b16c148d0b),[wrAddressA](#mw%5Fb824b792-90d5-4e1a-af4e-633e7508be4d),[wrEnA](#mw%5Fec511d31-0266-4ee1-8993-e79a921686e2),[wrDataB](#mw%5Febf645cb-a544-49cd-be67-b37aa2d9aafc),[wrAddressB](#mw%5F66560f99-f3c2-47df-97cd-4119eeafb682),[wrEnB](#mw%5Fd425a18a-da9b-4cc2-bd5f-74a83a0fe88f)) writes the value wrDataA into the memory locationwrAddressA when wrEnA is true and writes the value wrDataB into the memory locationwrAddressB when wrEnB istrue. wrDataOutA is the new or old data at memory location wrAddressA. wrDataOutB is the new or old data at the memory location wrAddressB. wrAddressA and wrAddressB cannot be the same address. Use this syntax when you create a true dual-port RAM System object.

[[rdDataOutA](#mw%5F4f93af1c-f5e4-4781-84fa-48c7265b84aa),[rdDataOutB](#mw%5Fff291bb6-55c2-4afd-8fe3-fae2b5d36c4e)] = ram([wrData](#d126e83516),[wrAddress](#d126e83788),[wrEn](#d126e83673),[rdAddressA](#mw%5F39b55b75-58af-4caf-8a99-9c42779b7499),[rdAddressB](#mw%5Fa7950b32-a2e2-4c8b-a94a-12d24f051596)) writes the value wrData into the memory locationwrAddress when wrEn is true.rdDataOutA is the old data at address locationrdAddressA, and rdDataOutB is the old data at the address location rdAddressB. Use this syntax when you create a simple tri-port RAM System object.

Input Arguments

expand all

Data to write into the RAM memory location when wrEn is true, specified as a scalar or a vector. This value can be double,single, half, integer, or afixed-point (fi) object, and can be real or complex. Thehdl.RAM System object uses fixed-point data internally for address calculations, which results in a checkout of the Fixed-Point Designer™ product license.

Bus Support:

You can use non-virtual bus at the data port for HDL code generation.

Data Types: single | double | half | int8 | int16 | int32 | uint8 | uint16 | uint32 | fi

Address to write or read, specified as a scalar or a vector. The System object writes the data from wrData to this address whenwrEn is true and reads the value in memory location rwAddress when wrEn isfalse. This value can be either fixed-point (fi) or integer, must be unsigned, and must be between2 and 31 bits long. Specify this address when you create a single-port RAM object.

Note

Data Types: uint8 | uint16 | fi

Write enable, specified as a numeric or logical 1 (true) or 0 (false). WhenwrEn is true, you write thewrData into the RAM memory location. If you create a single-port RAM, the System object reads the value in the memory location when wrEn isfalse.

Data Types: logical

Address to read the data from when you create a simple dual-port RAM or dual-port RAM System object, specified as a scalar or vector. This value can be eitherfixed-point (fi) or integer, must be unsigned, and must be between 2 and 31 bits long.

Note

Data Types: uint8 | uint16 | fi

Address to write the data into when you create a simple dual-port RAM, dual-port RAM, or simple tri-port RAM System object, specified as a scalar or vector. This value can be eitherfixed-point (fi) or integer, must be unsigned, and must be between 2 and 31 bits long.

Note

Data Types: uint8 | uint16 | fi

Data to write into the RAM memory location when wrEnA istrue, specified as a scalar or vector. This value can bedouble, single, half,integer, or a fixed-point (fi) object, and can be real or complex. This write data is used for a true dual-port RAM System object. The hdl.RAM System object uses fixed-point data type internally for address calculations when RAM banks are used, which results in a checkout of the Fixed-Point Designer product license.

Bus Support:

You can use non-virtual bus at the data port for HDL code generation.

Data Types: single | double | half | int8 | int16 | uint8 | uint16 | fi

Address to write the wrDataA into when you create a true dual-port RAM System object, specified as a scalar or vector. This value can be eitherfixed-point (fi) or integer, must be unsigned, and must be between 2 and 31 bits long.

Note

Data Types: uint8 | uint16 | fi

Write enable, specified as a numeric or logical 1 (true) or 0 (false). WhenwrEnA is true, you write thewrDataA into the RAM memory location. This enable is used for a true dual-port RAM System object.

Data Types: logical

Data that you write into the RAM memory location when wrEnB istrue, specified as a scalar or vector. This value can bedouble, single, half,integer, or a fixed-point (fi) object, and can be real or complex. This write data is used for a true dual-port RAM System object. The hdl.RAM System object uses fixed-point data type internally for address calculations when RAM banks are used, which results in a checkout of the Fixed-Point Designer product license.

Bus Support:

You can use non-virtual bus at the data port for HDL code generation.

Data Types: single | double | half | int8 | int16 | uint8 | uint16 | fi

Address to write the wrDataB into when you create a true dual-port RAM System object, specified as a scalar or vector. This value can be eitherfixed-point (fi) or integer, must be unsigned, and must be between 2 and 31 bits long.

Note

Data Types: uint8 | uint16 | fi

Write enable, specified as a numeric or logical 1 (true) or 0 (false). WhenwrEnB is true, you write thewrDataB into the RAM memory location. This enable is used for a true dual-port RAM System object.

Data Types: logical

Address to read the data from and store in rdDataOutA when you create a simple tri-port RAM System object, specified as a scalar or vector. This value can be eitherfixed-point (fi) or integer, must be unsigned, and must be between 2 and 31 bits long.

Note

Data Types: uint8 | uint16 | fi

Address to read the data from and store in rdDataOutA when you create a simple tri-port RAM System object, specified as scalar or vector. This value can be eitherfixed-point (fi) or integer, must be unsigned, and must be between 2 and 31 bits long.

Note

Data Types: uint8 | uint16 | fi

Output Arguments

expand all

Output data that the System object reads from the memory location rwAddress of a single-port RAM object when wrEn is false, returned as a scalar or vector.

Old output data that the System object reads from the memory location rdAddress of a simple dual port RAM or dual port RAM System object, returned as a scalar or vector.

New or old output data that the System object reads from the memory location wrAddress of a simple dual port RAM or dual port RAM System object, returned as a scalar or vector.

New or old output data that the System object reads from the memory location wrAddressA of a true dual port RAM System object, returned as a scalar or vector.

New or old output data that the System object reads from the memory location wrAddressB of a true dual port RAM System object, returned as a scalar or vector.

Old output data that the System object reads from the memory location rdAddressA of a simple tri port RAM System object, returned as a scalar or vector.

Old output data that the System object reads from the memory location rdAddressB of a simple tri port RAM System object, returned as a scalar or vector.

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:

expand all

step Run System object algorithm
release Release resources and allow changes to System object property values and input characteristics
reset Reset internal states of System object

Examples

collapse all

Construct System object to read from or write to a memory location in RAM. Set WriteOutputValue to Old data to return the previous value stored at the write address.

The output data port corresponds to the read/write address passed in. During a write operation, the old data at the write address is sent out as the output.

ram_1p = hdl.RAM('RAMType','Single port',... 'WriteOutputValue','Old data')

ram_1p = hdl.RAM with properties:

Main RAMType: 'Single port' AsyncRead: false WriteOutputValue: 'Old data' RAMInitialValue: 0

Show all properties

dataLength = 10; dataIn = 1:10; dataOut = zeros(1,dataLength);

Write a count pattern to the memory. Previous values on the first writes are all zero.

for ii = 1:dataLength addressIn = uint8(ii-1); writeEnable = true; dataOut(ii) = ram_1p(dataIn(ii),addressIn,writeEnable); end dataOut

dataOut = 1×10

 0     0     0     0     0     0     0     0     0     0

Read the data back.

for ii = 1:dataLength addressIn = uint8(ii-1); writeEnable = false; dataOut(ii) = ram_1p(dataIn(ii),addressIn,writeEnable); end dataOut

dataOut = 1×10

 0     1     2     3     4     5     6     7     8     9

Now, write the count in reverse order. The previous values are the original count.

for ii = 1:dataLength addressIn = uint8(ii-1); writeEnable = true; dataOut(ii) = ram_1p(dataIn(dataLength-ii+1),addressIn,writeEnable); end dataOut

dataOut = 1×10

10     1     2     3     4     5     6     7     8     9

Create System object that writes to a single port RAM and reads the newly written value.

Construct single-port RAM System object. When you write a location, the object returns the new value. The size of the RAM is inferred from the bitwidth of the address and write data on the first call to the object.

ram_1p = hdl.RAM('RAMType','Single port','WriteOutputValue','New data'); dataLength = 16; [dataIn,dataOut] = deal(uint8(zeros(1,dataLength)));

Write randomly generated data to the System object, and then read data back out again.

for ii = 1:dataLength dataIn(ii) = randi([0 63],1,1,'uint8'); addressIn = fi((ii-1),0,4,0); writeEnable = true; dataOut(ii) = ram_1p(dataIn(ii),addressIn,writeEnable); end
dataOut

dataOut = 1×16 uint8 row vector

0   52   57    8   58   40    6   17   35   61   61   10   62   61   31   51

for ii = 1:dataLength addressIn = fi((ii-1),0,4,0); writeEnable = false; dataOut(ii) = ram_1p(dataIn(ii),addressIn,writeEnable); end dataOut

dataOut = 1×16 uint8 row vector

9   52   57    8   58   40    6   17   35   61   61   10   62   61   31   51

Construct System object to read from and write to different memory locations in RAM.

The output data port corresponds to the read address. If a read operation is performed at the same address as the write operation, old data at that address is read out as the output. The size of the RAM is inferred from the bitwidth of the address and write data on the first call to the object.

ram_2p = hdl.RAM('RAMType','Simple dual port'); dataLength = 16; [dataIn,dataOut] = deal(uint8(zeros(1,dataLength)));

Write randomly generated data to the System object, and read the old data from the same address.

for ii = 1:dataLength dataIn(ii) = randi([0 63],1,1,'uint8'); wrAddr = fi((ii-1),0,4,0); writeEnable = true; dataOut(ii) = ram_2p(dataIn(ii),wrAddr,writeEnable,wrAddr); end
dataOut

dataOut = 1×16 uint8 row vector

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Write and read from different addresses. The object returns the read result after one cycle delay.

for ii = 1:dataLength wrAddr = fi((ii-1),0,4,0); rdAddr = fi(dataLength-ii+1,0,4,0); writeEnable = true; dataOut(ii) = ram_2p(dataIn(ii),wrAddr,writeEnable,rdAddr); end dataOut

dataOut = 1×16 uint8 row vector

0    9    9   51   31   61   62   10   61   61   35   17    6   40   58    8

Construct System object to read from and write to different memory locations in RAM.

There are two output ports: a write output data port and a read output data port. The write output data port sends out the new data at the write address. The read output data port sends out the old data at the read address. The size of the RAM is inferred from the bitwidth of the address and write data on the first call to the object.

ram_2p = hdl.RAM('RAMType','Dual port','WriteOutputValue','New data'); dataLength = 16; [dataIn,wrDataOut,rdDataOut] = deal(uint8(zeros(1,dataLength)));

Write randomly generated data to the System object, and read the old data from the same address.

for ii = 1:dataLength dataIn(ii) = randi([0 63],1,1,'uint8'); wrAddr = fi((ii-1),0,4,0); writeEnable = true; [wrDataOut(ii),rdDataOut(ii)] = ram_2p(dataIn(ii),wrAddr,writeEnable,wrAddr); end
wrDataOut

wrDataOut = 1×16 uint8 row vector

0   52   57    8   58   40    6   17   35   61   61   10   62   61   31   51

rdDataOut = 1×16 uint8 row vector

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Write and read from different addresses. The object returns the read result after one cycle delay.

for ii = 1:dataLength wrAddr = fi((ii-1),0,4,0); rdAddr = fi(dataLength-ii+1,0,4,0); writeEnable = true; [wrDataOut(ii),rdDataOut(ii)] = ram_2p(dataIn(ii),wrAddr,writeEnable,rdAddr); end wrDataOut

wrDataOut = 1×16 uint8 row vector

9   52   57    8   58   40    6   17   35   61   61   10   62   61   31   51

rdDataOut = 1×16 uint8 row vector

0    9    9   51   31   61   62   10   61   61   35   17    6   40   58    8

Create a System object that can write vector data to a dual-port RAM and read vector data out. Each element of the vector corresponds to a separate bank of RAM. This example creates 4 16-bit banks. Each bank has eight entries.

Construct dual-port RAM System object.

ram_2p = hdl.RAM('RAMType','Dual port','WriteOutputValue','New data');

Create vector write data and addresses. Use a 3-bit address (for 8 locations), and write 16-bit data. Read and write addresses are independent. Allocate memory for the output data.

ramDataIn = fi(randi((2^16)-1,1,4),0,16,0); ramReadAddr = fi([1,1,1,1],0,3,0); ramWriteAddr = fi([1,1,1,1],0,3,0); [wrOut,rdOut] = deal(fi(zeros(1,4),0,16,0));

First, write locations in bank 1 and 4, then read all banks. The write data is echoed in the wrOut output argument. The object returns read results after one cycle delay.

[wrOut,rdOut] = ram_2p(ramDataIn,ramWriteAddr,[true,false,false,true],ramReadAddr); [wrOut,rdOut] = ram_2p(ramDataIn,ramWriteAddr,[false,false,false,false],ramReadAddr); [wrOut,rdOut] = ram_2p(ramDataIn,ramWriteAddr,[false,false,false,false],ramReadAddr)

wrOut = 53393 0 0 59859

      DataTypeMode: Fixed-point: binary point scaling
        Signedness: Unsigned
        WordLength: 16
    FractionLength: 0

rdOut = 53393 0 0 59859

      DataTypeMode: Fixed-point: binary point scaling
        Signedness: Unsigned
        WordLength: 16
    FractionLength: 0

Algorithms

expand all

In a Simulink® model, you can use the hdl.RAM System object inside a MATLAB System or a MATLAB Function block. If you log the output of a MATLAB System block, the output data has at least three dimensions because the MATLAB System block has at least two dimensions, and the time data adds a third dimension. For example, if you input scalar data to the block, the logged output data has the dimension 1 by 1 by N, whereN is the number of time steps. To obtain an output dimension that is same as the input dimension, add a Reshape block at the output with theOutput dimensionality parameter set to Derive from reference input port.

If your data is scalar, the RAM size, or number of locations, is inferred from the data type of the address variable.

Data type of address variable RAM address size (bits)
single or double 16
uint_N_ N
embedded.fi WordLength

The maximum RAM address size is 32 bits.

If your data is a vector, HDL Coder generates an array of parallel RAM banks. The number of elements in the vector determines the number of RAM banks. The size of each RAM bank is inferred from the data type of the address variable.

Data type of address variable RAM address size (bits)
single or double 16
uint_N_ N
embedded.fi WordLength

The maximum RAM bank address size is 32 bits.

Extended Capabilities

Version History

Introduced in R2015a

expand all

You can use RAM System object inside a data rate feedback loop and use clock-rate pipelining optimization. This model design is helpful for applications that require programmable or tunable lookup tables without having to regenerate bitstreams.

Use the new property ModelRAMDelay to model delay in your simulation. ModelRAMDelay istrue by default and is disabled when AsyncRead istrue.

See Also

Blocks

Topics