算术右移不使用移位运算符

时间:2017-05-12 13:54:15

标签: verilog bit-shift

我想在16 Bit Arithmetic right shift function中写verilog而不使用shift operators

到目前为止

我的代码:

module my_shift(Number, Range, Shifted);
  input [15:0] Number;
  input [3:0] Range;
  output[15:0] Shifted;
  reg Shifted;

always @(Number)
  Shifted = shifted_number(Number, Range);

    function[15:0] shifted_number;
     input [15:0] number;
     input [3:0] Range;         //4 bit shift range
     integer i;
     integer j;

     begin
         for (i = 0; i < Range; i = i + 1) begin
             for (j = 0; j < 15; j = j + 1) begin
                shifted_number[j] = number[j+1];
             end
         end
         shifted_number[15] = number[15];
     end

    endfunction

endmodule

我写了一个简单的Testbench来测试我的function。 我只想shift三个不同的数字加1 bit

我的测试平台:

module test;
reg [15:0] Number;
reg [3:0] Range;
wire[15:0]  shifted;

my_shift shift_number(Number, Range,shifted);

initial
$monitor($time," -->Number = %b, shifted = %b, ",Number,shifted);

initial begin
Range = 1;
Number = 3; 

#10;
Number = 4; 

#10;
Number = 5; 

#100 $finish;
end

endmodule

这给了我以下output

  

0 - &gt; Number = 0000000000000011,shift = 0000000000000001,

     

10 - &gt; Number = 0000000000000100,shift = 0000000000000000,

     

20 - &gt; Number = 0000000000000101,shift = 0000000000000000,

它编译但是有一个警告:

my_shift shift_number(Number, Range,shifted);

警告:

warning: Port sizes don't match in port #3

为什么编译器会给我这个警告,为什么输出不正确?

1 个答案:

答案 0 :(得分:2)

您错误地将Shifted声明为1位reg

变化:

  output [15:0] Shifted;
  reg Shifted;

为:

  output reg [15:0] Shifted;

我现在得到这个输出:

           0 -->Number = 0000000000000011, shifted = 1000000000000001, 
          10 -->Number = 0000000000000100, shifted = 1000000000000010, 
          20 -->Number = 0000000000000101, shifted = 1000000000000010, 

我在2个不同的模拟器上运行您的代码,并获得了更多有用的警告和错误消息。您可以在 edaplayground 上试试运气。