zoukankan      html  css  js  c++  java
  • 数据通路

    摘要:

    数据通路。

    y = func(a,b)

    func可以是加法,减法,乘法,比较,移位,舍入,饱和等。

    分别针对有符号数,无符号数的情况。

    代码实现:

    //无符号数的数据通路
    module arithmetic(
                        clk,
                        a,
                        b,
                        res_sum,
                        res_sub,
                        res_pro,
                        right_shift,
                        right_shift_a
                        );
        input [3:0]a;
        input [3:0]b;
        input clk;
        output [4:0]res_sum;//
        output [3:0]res_sub;//
        output [7:0]res_pro;//
        output [3:0]right_shift;//a逻辑右移3位
        output [3:0]right_shift_a;//a算术右移3位
        assign res_sum=a+b;
        assign res_sub=a-b;
        assign res_pro=a*b;
        assign right_shift=a>>3;//逻辑右移
        assign right_shift_a=a>>>3;//算术右移
    endmodule

    tb:

    `timescale 1ns/1ns
    module arithmetic_tb();
        reg [3:0]a;
        reg [3:0]b;
        wire [4:0]res_sum;//
        wire [3:0]res_sub;//
        wire [7:0]res_pro;//
        wire [3:0]right_shift;//a逻辑右移3位
        wire [3:0]right_shift_a;//a算术右移3位
        reg clk;
        arithmetic arithmetic(
                        .clk(clk),
                        .a(a),
                        .b(b),
                        .res_sum(res_sum),
                        .res_sub(res_sub),
                        .res_pro(res_pro),
                        .right_shift(right_shift),
                        .right_shift_a(right_shift_a)
                        );
        initial clk=0;
        always #4 clk=~clk;
        
        initial begin
            a=4'b1011;//11
            b=4'b1001;//9
            #10;
            a=4'b1001;
            b=4'b0111;
        end
    endmodule 

    仿真波形:

     ------------------------------------------------------------------------------------------------------------------------------------------

    对于有符号数来说:

    代码实现:

    //有符号数的数据通路
    module arithmetic(
                        clk,
                        a,
                        b,
                        res_sum,
                        res_sub,
                        res_pro,
                        right_shift,
                        right_shift_a
                        );
        input  clk;
        input  signed[3:0]a;
        input  signed[3:0]b;
        output signed[4:0]res_sum;//
        output signed[3:0]res_sub;//
        output signed[7:0]res_pro;//
        output signed[3:0]right_shift;//a逻辑右移3位
        output signed[3:0]right_shift_a;//a算术右移3位
        assign res_sum=a+b;
        assign res_sub=a-b;
        assign res_pro=a*b;
        assign right_shift=a>>3;//逻辑右移
        assign right_shift_a=a>>>3;//算术右移
    endmodule
    View Code

    tb部分代码和之前一样。

    这时请注意仿真波形,

    逻辑右移和算术右移结果不一样了,逻辑右移是左边补零,而算术右移左边是补符号位1.

    同时应该注意到,有符号数输入的是二进制补码,而不是原码,所以1011和1001对应的是-5和-7而不是-3和-1哦,同理1001和0111对应的是-7和+7而不是-1和+7.这样波形就可以得到解释了。

     

    将radix切换为 use global radix,

    YKJIAO
  • 相关阅读:
    关于MATLAB处理大数据坐标文件2017628
    回溯算法
    [leetcode] 046. Permutations 解题报告
    [leetcode] 226. Invert Binary Tree 解题报告
    [leetcode] 121. Best Time to Buy and Sell Stock 解题报告
    [leetcode] 112. Path Sum 解题报告
    [leetcode] 190. Reverse Bits 解题报告
    [leetcode] 189. Rotate Array 解题报告
    [leetcode] 100. Same Tree 解题报告
    [leetcode] 88. Merge Sorted Array 解题报告
  • 原文地址:https://www.cnblogs.com/ajiaoa/p/12785852.html
Copyright © 2011-2022 走看看