RAM-Based Shift Register (ALTSHIFT_TAPS) IP Core-实现3X3像素阵列存储

最近想要实现CNN的FPGA加速处理,首先明确在CNN计算的过程中,因为卷积运算是最耗时间的,因此只要将卷积运算在FPGA上并行实现,即可完成部分运算的加速

那么对于卷积的FPGA实现首先要考虑的是卷积子模板具体如何实现,我们在matlab或者c实现比如3X3的子模板的时候,只要用一个数组即可将模板的数据存储起来,而在FPGA的话有以下三种方法:

  1. 用2个或3个RAM存储3X3像素阵列
  2. 用2个或3个FIFO存储3X3像素阵列
  3. 用shift_RAM移位存储3X3像素阵列

而shift_RAM好像就是为了阵列的实现量身定做的一般。

shift_RAM的配置参数主要有以下几个:

手册中可以参考理解的一个非常形象的图如下:

进一步的进行单独一个IP核的仿真后得到:

其中上述参数设置分别为8,2,3,上述仿真图中,相当于把一个矩阵A通过移位寄存的方法通过row3_data送入到RAM,然后分三行输出,在游标所示处就可以开始输出3X3矩阵

0,56,-122

92,50,-57

-58,-13,-61

以下部分是加入了对视频信号处理控制后的代码实现过程:

/*-----------------------------------------------------------------------

CONFIDENTIAL IN CONFIDENCE
This confidential and proprietary software may be only used as authorized
by a licensing agreement from CrazyBingo (Thereturnofbingo).
In the event of publication, the following notice is applicable:
Copyright (C) 2011-20xx CrazyBingo Corporation
The entire notice above must be reproduced on all authorized copies.
Author                :        CrazyBingo
Technology blogs     :         http://blog.chinaaet.com/crazybingo
Email Address         :         [email protected]
Filename            :        VIP_Matrix_Generate_3X3_8Bit.v
Data                :        2014-03-19
Description            :        Generate 8Bit 3X3 Matrix for Video Image Processor.
                            Give up the 1th and 2th row edge data caculate for simple process
                            Give up the 1th and 2th point of 1 line for simple process
Modification History    :
Data            By            Version            Change Description
=========================================================================
13/05/26        CrazyBingo    1.0                Original
14/03/16        CrazyBingo    2.0                Modification
-*/ 

`timescale 1ns/1ns
module VIP_Matrix_Generate_3X3_8Bit
#(
    parameter    [9:0]    IMG_HDISP = 10‘d640,    //640*480
    parameter    [9:0]    IMG_VDISP = 10‘d480
)
(
    //global clock
    input                clk,                  //cmos video pixel clock
    input                rst_n,                //global reset

    //Image data prepred to be processd
    input                per_frame_vsync,    //Prepared Image data vsync valid signal
    input                per_frame_href,        //Prepared Image data href vaild  signal
    input                per_frame_clken,    //Prepared Image data output/capture enable clock
    input        [7:0]    per_img_Y,            //Prepared Image brightness input

    //Image data has been processd
    output                matrix_frame_vsync,    //Prepared Image data vsync valid signal
    output                matrix_frame_href,    //Prepared Image data href vaild  signal
    output                matrix_frame_clken,    //Prepared Image data output/capture enable clock
    output    reg    [7:0]    matrix_p11, matrix_p12, matrix_p13,    //3X3 Matrix output
    output    reg    [7:0]    matrix_p21, matrix_p22, matrix_p23,
    output    reg    [7:0]    matrix_p31, matrix_p32, matrix_p33
);

//Generate 3*3 matrix
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//sync row3_data with per_frame_clken & row1_data & raw2_data
wire    [7:0]    row1_data;    //frame data of the 1th row
wire    [7:0]    row2_data;    //frame data of the 2th row
reg    [7:0]    row3_data;    //frame data of the 3th row
always@(posedge clk or negedge rst_n)
begin
    if(!rst_n)
        row3_data <= 0;
    else
        begin
        if(per_frame_clken)
            row3_data <= per_img_Y;
        else
            row3_data <= row3_data;
        end
end

//---------------------------------------
//module of shift ram for raw data
wire    shift_clk_en = per_frame_clken;
Line_Shift_RAM_8Bit
#(
    .RAM_Length    (IMG_HDISP)
)
u_Line_Shift_RAM_8Bit
(
    .clock        (clk),
    .clken        (shift_clk_en),    //pixel enable clock
//    .aclr        (1‘b0),

    .shiftin    (row3_data),    //Current data input
    .taps0x        (row2_data),    //Last row data
    .taps1x        (row1_data),    //Up a row data
    .shiftout    ()
);

//------------------------------------------
//lag 2 clocks signal sync  因为数据存储耗费了一个时钟,因此3*3阵列读取使能和时钟要偏移一个时钟
reg    [1:0]    per_frame_vsync_r;
reg    [1:0]    per_frame_href_r;
reg    [1:0]    per_frame_clken_r;
always@(posedge clk or negedge rst_n)
begin
    if(!rst_n)
        begin
        per_frame_vsync_r <= 0;
        per_frame_href_r <= 0;
        per_frame_clken_r <= 0;
        end
    else
        begin
        per_frame_vsync_r     <=     {per_frame_vsync_r[0],     per_frame_vsync};
        per_frame_href_r     <=     {per_frame_href_r[0],     per_frame_href};
        per_frame_clken_r     <=     {per_frame_clken_r[0],     per_frame_clken};
        end
end
//Give up the 1th and 2th row edge data caculate for simple process
//Give up the 1th and 2th point of 1 line for simple process
wire    read_frame_href        =    per_frame_href_r[0];    //RAM read href sync signal
wire    read_frame_clken    =    per_frame_clken_r[0];    //RAM read enable
//将存储RAM以及阵列生成两个步骤需要的时钟都去掉
assign    matrix_frame_vsync     =     per_frame_vsync_r[1];
assign    matrix_frame_href     =     per_frame_href_r[1];
assign    matrix_frame_clken     =     per_frame_clken_r[1];

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
/******************************************************************************
                    ----------    Convert Matrix    ----------
                [ P31 -> P32 -> P33 -> ]    --->    [ P11 P12 P13 ]
                [ P21 -> P22 -> P23 -> ]    --->    [ P21 P22 P23 ]
                [ P11 -> P12 -> P11 -> ]    --->    [ P31 P32 P33 ]
******************************************************************************/
//---------------------------------------------------------------------------
//---------------------------------------------------
/***********************************************
    (1) Read data from Shift_RAM
    (2) Caculate the Sobel
    (3) Steady data after Sobel generate
************************************************/
//wire    [23:0]    matrix_row1 = {matrix_p11, matrix_p12, matrix_p13};    //Just for test
//wire    [23:0]    matrix_row2 = {matrix_p21, matrix_p22, matrix_p23};
//wire    [23:0]    matrix_row3 = {matrix_p31, matrix_p32, matrix_p33};
always@(posedge clk or negedge rst_n)
begin
    if(!rst_n)
        begin
        {matrix_p11, matrix_p12, matrix_p13} <= 24‘h0;
        {matrix_p21, matrix_p22, matrix_p23} <= 24‘h0;
        {matrix_p31, matrix_p32, matrix_p33} <= 24‘h0;
        end
    else if(read_frame_href)
        begin
        if(read_frame_clken)    //Shift_RAM data read clock enable
            begin
            {matrix_p11, matrix_p12, matrix_p13} <= {matrix_p12, matrix_p13, row1_data};    //1th shift input
            {matrix_p21, matrix_p22, matrix_p23} <= {matrix_p22, matrix_p23, row2_data};    //2th shift input
            {matrix_p31, matrix_p32, matrix_p33} <= {matrix_p32, matrix_p33, row3_data};    //3th shift input
            end
        else
            begin
            {matrix_p11, matrix_p12, matrix_p13} <= {matrix_p11, matrix_p12, matrix_p13};
            {matrix_p21, matrix_p22, matrix_p23} <= {matrix_p21, matrix_p22, matrix_p23};
            {matrix_p31, matrix_p32, matrix_p33} <= {matrix_p31, matrix_p32, matrix_p33};
            end
        end
    else
        begin
        {matrix_p11, matrix_p12, matrix_p13} <= 24‘h0;
        {matrix_p21, matrix_p22, matrix_p23} <= 24‘h0;
        {matrix_p31, matrix_p32, matrix_p33} <= 24‘h0;
        end
end

endmodule
//注意这里得到的每一行得第一第二的像素都没有用到,而且最后一行的像素没有被运算。

原文地址:https://www.cnblogs.com/Dinging006/p/9972150.html

时间: 2024-08-29 21:28:23

RAM-Based Shift Register (ALTSHIFT_TAPS) IP Core-实现3X3像素阵列存储的相关文章

USB 2.0 Hub IP Core

来自: http://arasan.com/products/usb/usb-2-0/usb-2-0-hub/ The Arasan USB 2.0 Hub IP core is an USB 2.0 specification compliant hub core that supports 480 Mbit/s in High Speed (HS) mode, 12 Mbit/s in Full Speed (FS) mode, and 1.5 Mbit/s in Low Speed (LS

Shift Register(Using Submodule)

/*************************************************** /  Shift Register module by Submodule /  Programing by seongki ***************************************************/ module Shift_Register_4_str(output [3:0] A_par,input [3:0] I_par,input s1,s0,MSB_

Shift Register

/*************************************************** /  Shift Register module /  Programing by seongki ***************************************************/ module Shift_Register_4_beh(output reg [3:0] A_par,input [3:0] I_par, input s1,s0, MSB_in,LSB_

H.265 Video Encoder IP Core

复制: 开源H.265硬件视频编码器H.265 Video Encoder IP Core是开源的H.265硬件视频编码器,实现了H.265(或叫HEVC)的大部分功能. 它由复旦大学专用集成电路与系统国家重点实验室(State Key Lab of ASIC & System,Fudan University)视频图像处理实验室(VIP Lab)范益波教授研究团队开发完成,并开放源代码.任何组织个人可以无偿使用上述代码用于研究和生产目的,VIP Lab将会持续更新并维护H.265硬件视频编码器

ModelSim Simulation of RapidIO II IP Core Demonstration Testbench May Require ld_debug Command

Solution ID: fb83262Last Modified: May 17, 2013Product Category: Intellectual PropertyProduct Area: Comm, Interface & PeripheralsProduct Sub-area: IP Spec and ProtocolVersion Found In: v12.1Version Fixed In: v13.0 Title ModelSim Simulation of RapidIO

Xilinx 7系列例化MIG IP core DDR3读写

昨晚找了一下,发现DDR3读写在工程上多是通过例化MIG,调用生成IPcore的HDL Functional Model.我说嘛,自己哪能写出那么繁琐的,不过DDR读写数据可以用到状态机,后期再添砖加瓦吧,当下先对比一下网上找的一段程序和自己例化后的程序. 另外,仿真了十余分钟,最后的是什么鬼?一头雾水T.T.想着每一次要分析信号要等那么久就难受. 更重要的是分享一波关于"Xilinx平台下DDR3设计教程"的资料.就其中的"仿真篇"而言,亲测可行,还是中文版 da

IP Core 分类

IP(Intelligent Property)核是具有知识产权核的集成电路芯核总称,是经过反复验证过的.具有特定功能的宏模块,与芯片制造工艺无关,可以移植到不同的半导体工艺中.到了SOC阶段,IP核设计已经成为ASIC电路设计公司和FPGA提供商的重要任务,也是其实力体现.对于FPGA开发软件,其提供的IP核越丰富,用户的设计就越方便,其市场占用率就越高. IP(Intellectual Property)就是常说的知识产权.美国Dataquest咨询公司将半导体产业的IP定义为用于ASIC.

XDMA ip core的使用

XDMA核的使用 一.   XDMA相关知识 绝对地址就是物理地址=段地址*16+偏移地址,也就是段地址<<4+偏移地址 主机host通过PCIe接口访问DMA,DMA即外部设备不通过CPU而直接与系统内存(DDR)交换数据. PIO模式下硬盘和内存之间的数据传输是通过CPU来控制的,而在DMA模式下,CPU只需向DMA控制下达命令,让DMA来控制数据的发送,数据传送完毕后再把数据反馈给CPU,这样很大程度上减轻了 CPU的资源占有率. DMA和PIO模式的区别就在于,DMA模式不过分依赖CP

Swift - 使用Core Data进行数据持久化存储

一,Core Data介绍 1,Core Data是iOS5之后才出现的一个数据持久化存储框架,它提供了对象-关系映射(ORM)的功能,即能够将对象转化成数据,也能够将保存在数据库中的数据还原成对象. 2,虽然其底层也是由类似于SQL的技术来实现,但我们不需要编写任何SQL语句,有点像Java开发中的Hibernate持久化框架 3,Core Data数据最终的存储类型可以是:SQLite数据库,XML,二进制,内存里,或自定义数据类型. 4,与SQLite区别:只能取出整个实体记录,然后分解,