Main Content

Random Access of External Memory

This example shows how to model external memory accesses from FPGA for rotating an ASCII art image. Many applications require FPGA to access memory in random fashion as per the requirements of algorithm. You will learn how to design memory address generation along with other AXI4 master signals to read and write specific regions of memory using SoC Blockset. You will simulate, implement and verify your design on hardware.

Supported hardware platforms:

  • Artix® 7 35T Arty FPGA evaluation kit

  • Xilinx® Kintex® 7 KC705 development board

  • Xilinx Zynq® ZC706 evaluation kit

  • Xilinx Zynq UltraScale+™ MPSoC ZCU102 Evaluation Kit

  • Xilinx Zynq UltraScale+ RFSoC ZCU111 Evaluation Kit

  • Altera® Cyclone® V SoC development kit

  • Altera Arria® 10 SoC development kit

Design Task

The ASCII art image is encoded as 24-by-64 matrix of uint8 characters. The design task is to rotate the image by modeling AXI4 Master interfaces in FPGA logic for external memory access. By simulating the design with external memory model and the AXI4 protocol, you verify the behavior at application design time. This saves time otherwise spent in debugging the design on hardware during the implementation phase.

The overall dataflow is as described in figure below. The image is stored in the external memory at the memory region from address 0x00000000 to 0x000017FF. FPGA algorithm reads the image from this region and rotates it by writing it in the reverse order into the memory region from 0x00001800 to 0x00002FFF. Finally, the data is read back from the memory.

Model Structure

The models are structured using Model references. Top model ' soc_image_rotation ' includes the FPGA model ' soc_image_rotation_fpga ' using Model block as model reference.

The top model covers the following areas:

  • Memory Region 1: The Input Read Memory block models memory region 1 using an AXI4 Random Access Memory block. The Input Read Memory stores the input image and initializes the memory before the simulation starts. To see how model parameters and input data are initialized, open the preload simulation function <matlab:edit('soc_image_rotation_init').

  • FPGA: This area instantiates the FPGA model reference which models the logic for AXI4 Master interfaces and data rotation.

  • Memory Region 2: The Output Write Memory block models memory region 2 using an AXI4 Random Access Memory blocks. The Output Write Memory stores the rotated image and logs it at the end of the simulation. To see how input data and output data are plotted, open the stop simulation function soc_image_rotation_post.m.

  • Control: The Algorithm Start block sends a Start signal to the FPGA algorithm via Register Channel block.

FPGA model implements the algorithm in three subsystems, AXI4MasterRead, ReverseDataOrder and AXI4MasterWrite. Open FPGA subsystem for image rotation:

As the positive edge of start signal is detected, AXIMasterRead reads one line of image data and delivers it to ReverseDataOrder to reverse the order of data. The reversed data is then written to external memory by AXIMasterWrite subsystem. Once the data for one line is written, it sends a signal request_next_line to trigger reading of next line by AXIMasterRead. This cycle continues until all lines of the image are processed.

Open AXI4MasterReadController and AXI4MasterWriteController blocks to inspect the MATLAB® code for AXI4 Master interfaces. These blocks design the addressing logic for read and write operations as per AXI4 protocol. SoC Blockset™ supports AXI4 Master protocol and for timing diagrams of AXI4 signals, please refer to Model Design for AXI4 Master Interface Generation (HDL Coder).

Simulation

Run the model and open the Logic analyzer from the FPGA model. Notice the following key points:

  • One line of data is written/read by masters in one burst. Since each line is 64 characters long; the burst length is 64 (0x40). Note this value on signals rd_len and wr_len.

  • Each character has 4 bytes as it is extended to uint32 data type, which makes the length of line 64x4 = 256 (0x100) bytes. Therefore, addresses increment/decrement by 0x100. Note this on rd_addr and wr_addr signals.

  • One read burst is followed by one write burst. Observe how rd_dvalid and wr_dvalid toggle alternatively.

  • request_next_line asserts after each write burst, which trigger the next read burst.

The input and output images are plotted at the end of simulation:

Implementation

Following products are required for this section:

  • HDL Coder™

  • SoC Blockset Support Package for AMD® FPGA and SoC Devices, or

  • SoC Blockset Support Package for Intel® Devices

To implement the model on a supported FPGA board, use the SoC Builder application. Make sure you have installed required products and FPGA vendor software before implementation.

Open SoC Builder by clicking 'Configure, Build, & Deploy' button in the toolstrip and follow these steps:

  • Select 'Build Model' on 'Setup' screen. Click 'Next'.

  • Click 'View/Edit Memory Map' to view the memory map on 'Review Memory Map' screen. Notice that the base address 0x80000000 is assigned to Input Read Memory block, and base address 0x80002000 is assigned to Output Write Memory block. The AXI4 address is the sum of the base address and the address from FPGA algorithm. For example the output data is written to the external memory from address 0x80002000 to address 0x80002000 + 0x17FF = 0x800037FF. Refer to Model Design for AXI4 Master Interface Generation (HDL Coder) for more information about base address register calculation. Click 'Next'.

  • Specify project folder on 'Select Project Folder' screen. Click 'Next'.

  • Select 'Build, load and run' on 'Select Build Action' screen. Click 'Next'.

  • Click 'Validate' to check the compatibility of model for implementation on 'Validate Model' screen. Click 'Next'.

  • Click 'Build' to begin building of the model on 'Build Model' screen. An external shell will open when FPGA synthesis begins. Click 'Next' to 'Load Bitstream' screen.

The FPGA synthesis may take more than 30 minutes to complete. To save time, you may want to use the provided pre-generated bitstream by following these steps:

  • Close the external shell to terminate synthesis.

  • Copy pre-generated bitstream to your project folder by running the command below and then,

  • Click 'Load' button to load pre-generated bitstream.

copyfile(fullfile(matlabshared.supportpkg.getSupportPackageRoot,'toolbox','soc','supportpackages','xilinxsoc','xilinxsocexamples','bitstreams','soc_image_rotation-zc706.bit'), './soc_prj');

To run this example, copy the example test bench to your project folder.

copyfile(fullfile(matlabroot,'toolbox','soc','socexamples','soc_image_rotation_aximanager.m'), './soc_prj','f');

Enter the following command to run the test bench:

soc_image_rotation_aximanager

The test bench performs the following operations:

  • Initializes image rotation IP

  • Writes input image data to external memory

  • Starts the image rotation operation

  • Reads back and display output image data from external memory

If your FPGA board is not Xilinx Zynq ZC706 evaluation kit you need to do the following settings in the configuration parameters of the top model before launching the SoC Builder.

  • Select the 'Hardware board' under 'Hardware Implementation' panel to match your board.

  • Uncheck 'Include processing system' under 'Hardware Implementation -> Target hardware resources -> FPGA design (top-level)' panel.

Available pre-generated bitstreams are:

  • 'soc_image_rotation-zc706.bit'

  • 'soc_image_rotation-arty.bit'

  • 'soc_image_rotation-zcu102.bit'

  • 'soc_image_rotation-XilinxZynqUltraScale_RFSoCZCU111EvaluationKit.bit'

  • 'soc_image_rotation-kc705.bit'

  • 'soc_image_rotation-a10soc.sof'

Modify the copyfile command and example test bench to match your board and selected project folder as appropriate. In case of Altera Arria® 10 SoC development kit and Altera Cyclone® V SoC development kit use below copyfile command corresponding to your board.

copyfile(fullfile(matlabshared.supportpkg.getSupportPackageRoot,'toolbox','soc','supportpackages','intelsoc','intelsocexamples','bitstreams','soc_image_rotation-a10soc.sof'), './soc_prj');

Note that pre-generated bitstream may not work if you customized the memory map.

Conclusion

This example shows modeling of AXI4 Master interfaces for accessing external memory in random fashion using SoC Blockset by rotating an ASCII art image. You can use this as a guide to design your own algorithm to access memory directly using AXI4 Master protocol.

See Also