Verilog study notes - Signed number of multiplications and additions

     There calculate the number of symbols is a very important issue in Verilog (also easily be overlooked), the use writing FIR filter Verilog language , the need to involve a signed number addition and multiplication , I was in the previous program all the input, output and intermediate signals are defined as signed numbers, this problem does not occur in the calculation (actually encountered problems signed and no symbol number before the program, not the final filtered result, the blog program is already correction off), the following practical to test Verilog multiplication problem;

1. unsigned multiplication test program and have written a number of symbols

     Programming follows, wherein two multipliers are unsigned multiplication, there are four combinations of symbols, the product output is divided into unsigned and signed, a total of eight possible;

module signed_test(
    input           [7:0]   data_in_unsigned_1,
    input           [7:0]   data_in_unsigned_2,

    input   signed  [7:0]   data_in_signed_1,
    input   signed  [7:0]   data_in_signed_2,
    
    output          [15:0]  data_out_000,
    output          [15:0]  data_out_001,
    output          [15:0]  data_out_010,
    output          [15:0]  data_out_011,
    
    output  signed  [15:0]  data_out_100,
    output  signed  [15:0]  data_out_101,
    output  signed  [15:0]  data_out_110,
    output  signed  [15:0]  data_out_111
);

//无符号 = 无符号 * 无符号 
assign data_out_000 = data_in_unsigned_1 * data_in_unsigned_2;
//无符号 = 无符号 * 有符号 
assign data_out_001 = data_in_unsigned_1 * data_in_signed_2;
//无符号 = 有符号 * 无符号 
assign data_out_010 = data_in_signed_1   * data_in_unsigned_2;
//无符号 = 有符号 * 有符号 
assign data_out_011 = data_in_signed_1   * data_in_signed_2; 

//有符号 = 无符号 * 无符号 
assign data_out_100 = data_in_unsigned_1 * data_in_unsigned_2;
//有符号 = 无符号 * 有符号 
assign data_out_101 = data_in_unsigned_1 * data_in_signed_2;
//有符号 = 有符号 * 无符号 
assign data_out_110 = data_in_signed_1   * data_in_unsigned_2;
//有符号 = 有符号 * 有符号 
assign data_out_111 = data_in_signed_1   * data_in_signed_2; 
    
endmodule

     FIG RTL generated as follows:
Here Insert Picture Description
     It can be seen irrespective of the product and output symbols, symbols and have in fact the same unsigned number, look how we define it , such as a 16-bit binary product 16'b1100_0000_0000_0011, when we think it is unsigned number is the highest position of the sign bit is not 1, but 2 ^ 15 (2 to the power 15), so the decimal number represents 2 ^ 15 + 2 ^ 14 + 2 ^ 1 + 2 ^ 0 = 49155;

     The original binary data is represented inverted plus 1 if the 16'b1100_0000_0000_0011 as 16-bit binary number is a symbol of view, the most significant bit is the sign bit, and the remaining data (complement representation), to calculate its corresponding decimal numbers
(1) to remove the sign bit, the remaining 15-bit retention of 100_0000_0000_0011;
(2) the 100_0000_0000_0011 negated give 011_1111_1111_1100;
(3) the lowest bit 011_1111_1111_1100 + 1, to give 011_1111_1111_1101;
(4 ) 011_1111_1111_1101 converter according unsigned decimal 16381.;
(5) the maximum bit plus sign bit, 0 represents a positive number, 1 for negative numbers, the final conversion is -16831;

     The default Windows Calculator highest bit is the sign bit;

Test data are as follows:
Here Insert Picture Description

initial begin 
        data_in_unsigned_1 = 8'hff; //255
        data_in_unsigned_2 = 8'hf0; //240
    
        data_in_signed_1 = 8'hff;   //-1
        data_in_signed_2 = 8'hf0;   //-16
    
    #200;
        data_in_unsigned_1 = 8'hff; //255
        data_in_unsigned_2 = 8'h0f; //15
        data_in_signed_1 = 8'hff;   //-1
        data_in_signed_2 = 8'h0f;   //15
    
    #200;
        data_in_unsigned_1 = 8'd127; //127
        data_in_unsigned_2 = 8'd15;  //15
        
        data_in_signed_1 = -8'sd127;  //-127,十进制有符号数赋值,必须要用 sd 表示
        data_in_signed_2 = -8'sd15;   //-15
    
    #200;     
        data_in_unsigned_1 = 8'd128; //128
        data_in_unsigned_2 = 8'd15;  //15       
        data_in_signed_1 = -8'sd128;  //-128
        data_in_signed_2 = -8'sd15;   //-15
    
    #200;
        data_in_unsigned_1 = 8'd127; //127
        data_in_unsigned_2 = 8'd15;  //15           
        data_in_signed_1 = -8'sd127;  //-127
        data_in_signed_2 = 8'sd15;    //15
        
    #200;     
        data_in_unsigned_1 = 8'd128; //128
        data_in_unsigned_2 = 8'd15;  //15               
        data_in_signed_1 = -8'sd128;  //-128
        data_in_signed_2 = 8'sd15;   //15
    
    #200;
        data_in_unsigned_1 = 8'd127; //127
        data_in_unsigned_2 = 8'd15;  //15          
        data_in_signed_1 = 8'sd127;  //127
        data_in_signed_2 = -8'sd15;   //-15
            
    #200;          
        data_in_unsigned_1 = 8'd127; //127
        data_in_unsigned_2 = 8'd15;  //15
        data_in_signed_1 = 8'sd127;  //127
        data_in_signed_2 = 8'sd15;   //15        
    
    #200;
    $stop;
end

2. Simulation

     The simulation result of the calculation is as follows:
Here Insert Picture Description
     the upper chart analysis:
     (1) using the same hex hex data assignment in 0 ~ 400 ns, the simulation to the multiplier, respectively, so that the multiplier signed and unsigned read number, to see 8'hff (corresponding to binary 8'b1111_1111) to read an unsigned number is read in accordance with the original code, corresponding to 255 in decimal, when reading a signed number is read in accordance with complement according to the above-mentioned strip-symbol bit inversion, and then calculate the decimal add 1 to obtain 1;
     (2) direct assignment decimal data, unsigned multiplier to always read according to the read original code, it corresponds to 127 8-bit binary 8'b0111_1111, corresponds to the decimal 128 8-bit binary 8'b1000_0000; the time to read a signed number is converted directly complement form, such as -127, first remove 127 the sign bit, the corresponding 7 bit binary number 7'b111_1111, is the inverse 7'b000_0000, plus 1 7'b000_0001, back to the sign bit is the most significant bit 8'b1000_0001; -128 for special representation, the most significant bit binary number of 8-bit is the sign bit indicating the sign, the remaining 7-bit can be represented by Number range from 0 to 127, preceded by ± -127 to 127 can be represented, of which 2 is the number of very special and 8'b0000_0000 8'b1000_0000, according to the above will be +0 and -0, to distinguish it number two, the former is defined 8'b0000_0000 represents 0, while 8'b1000_0000 represent -128, so not only can distinguish two numbers represents a number more than -128 (universal entire computer system, said in other similar-digit a negative number);
Here Insert Picture Description
     (3) actually observed following data can be found, all of computing the correct time and data_out_111 data_out_000 only data which is consistent with common sense:
     unsigned * = unsigned unsigned (only 0 and positive number);
     * Signed Signed Signed = (0, either positive or negative);
other computing Why wrong? Here actually follow a principle:
     if there is an unsigned number expression, all forced operands will be converted to an unsigned number;
     this also explains calculation and data_out_010 data_out_001 when 0 ~ 400 ns and the results data_out_000 exactly the same, they are 8 bits as hexadecimal number assigned unsigned calculations (problem does not exist here original decimal to binary code conversion of complement, as a hexadecimal);
     when later when designing input and output, if it is signed, then the input / output and the intermediate amount of the correlation calculation are explicitly defined by signed;
Here Insert Picture Description

3. Another signed number of multiplication

     Calculating an amount related to said earlier will involve all of the number of symbols defined as a calculation method, in addition, usually unsigned may be defined, but the actual number of symbols is passed, such as the following input and outputs are not signed to signed numbers specified, the default is calculated according to an unsigned number calculation (I actually felt is to read the 8-bit binary numbers as the original code to count), then if the incoming external data actually signed number (such as the FIR filter to be passed in both positive and negative signal filtering), the need to extend the sign bit multiplication and addition to calculate;

module signed_test_2(
input [7:0] data_in_1,
input [7:0] data_in_2,
output [15:0] data_out_1,
output [15:0] data_out_2
);
     对于乘法,需要扩展符号位 到 和积的位数相等,比如乘数a为 N-bit,乘数 b 为M-bit,两个相乘得到 N+M 位数据,此时需要对 a 扩展 M-bit 到 N+M 位,对 b 扩展 N-bit 到 N+M 位;
     下面,使用 位拼接符 { } 来做演示,位拼接符可以按照二进制的位来进行高低位的拼接,假设 data_in_1= 8’b1000_0011,对于 {{8{data_in_1[7]}},data_in_1} 可以这样理解:
     (1)先看 8{data_in_1[7]},表示取出 8-bit 数据 data_in_1 的最高位 data_in_1[7],重复 8 次,相当于
{ data_in_1[7], data_in_1[7], data_in_1[7], data_in_1[7], data_in_1[7], data_in_1[7], data_in_1[7], data_in_1[7] },
即高位扩展 8-bit 的 1;
     (2){{8{data_in_1[7]}},data_in_1} 相当于在 data_in_1 的前面补上 8 个 data_in_1[7],即 结果为 16-bit 的 16’b1111_1111_1000_0011;

//不做符号位扩展,直接相乘
assign data_out_1 = data_in_1 * data_in_2;
//做符号位扩展,再相乘
assign data_out_2 = {{8{data_in_1[7]}},data_in_1} * {{8{data_in_2[7]}},data_in_2};

     Simulation test data are as follows, at a given hexadecimal data, at 2 decimal assignment signed, at 3 to 2 and contrast at, to see whether the same as last assignment (see blog says is wrong assignment 3 , so the test);
Here Insert Picture Description
     simulation results below, the assignment can be seen at figure 2 and 3 at the same data in the simulation, all data are displayed (Radix right data in a signed decimal number -> signed decimal );
Here Insert Picture Description
     see, data_out_1 result is wrong (not complement sign bit), the result is a data_out_2 (complement sign bit);
Here Insert Picture Description
     for addition of signed numbers, the same, or all of the operations associated with a symbol defined as number, or extended sign bits, for the addition operation, each summand only extended to a sign bit;

In addition, calls can also be used instead of a multiplier of a multiplication sign * IP, or IP adder instead of the addition sign +, the input-output configuration as signed to the IP core.

MATLAB wireless communication with the FPGA, image processing, digital signal processing series Summary

Published 30 original articles · won praise 56 · views 60000 +

Guess you like

Origin blog.csdn.net/DengFengLai123/article/details/104072423