verilog中的算术移位运算

verilog中的算术移位运算,verilog,system-verilog,Verilog,System Verilog,我有一个Verilog模块,一次只能移动一位。 请帮我完成这个模块 module shift_right1 ( output logic [15:0] shifted, input wire [15:0] unshifted, input wire [3:0] amt ); localparam int width = 16; always @* begin automatic int limit = width - amt; for ( i

我有一个Verilog模块,一次只能移动一位。
请帮我完成这个模块

module shift_right1 (
  output logic [15:0] shifted,
  input  wire  [15:0] unshifted,
  input  wire   [3:0] amt );

  localparam int width = 16;

  always @* begin 
    automatic int limit = width - amt;

    for ( int i=0; i<limit; i++ ) begin
      shifted[i] = unshifted[i+amt];
    end
  end
endmodule
模块右移1(
输出逻辑[15:0]移位,
输入线[15:0]未移动,
输入线[3:0]金额);
localparam int width=16;
总是开始
自动整数限制=宽度-金额;

for(int i=0;i算术移位在移位时保留符号。在for循环之后,您需要设置移位值的符号。类似的操作可能会起作用:

for ( int i=limit; i<width-1; i++ ) shifted[i] = unshifted[width-1];

我认为关键是每次取一位作为输入。这意味着输入是1位,将旧数据右移。这是串行到并行转换器的实现

module shifter(
   input               dat_rx, //1bit data input    
   input               clk,
   output logic [15:0] dat_tx  //Parallel output
);
   always @(posedge clk) begin
     dat_tx <= {dat_rx, dat_tx[15:1]}; //Next input and right shift
   end
endmodule

localparam int
automatic int
我不知道有任何指南显示如何编写这样的verilog。通过标记一个变量automatic,您想做什么?一次移位1位,通常意味着每个时钟周期1位,并且您的示例中没有时钟。您能尝试澄清这个问题吗?在中做得很好包含一些代码,虽然很好看。实际上,这是我家庭作业项目的一部分,教授让我将代码更改为一次输入一位。模块shift_right1应该对16位量执行逻辑右移。您的解决方案应该一次分配一位移位,就像现有代码一样。(换句话说,不要只使用右移运算符。)测试台输出可能会提供问题所在的线索。提示:问题可以通过一行或两行code@user3159419:看起来您只是想完成当前代码,它只缺少位[width-1:limit]的赋值。对于逻辑移位,只需在下面的回答中将我的代码更改为
shift[i]=1'b0;
。注释中提到的“逻辑右移”不是算术移位,但仅从问题中并不清楚。Morgan,谢谢你的回答。但在上述代码中,我需要使用与我的教授所述相同的变量,即amt[3:0]。我必须添加一个附加逻辑,但不能更改变量。@user3159419很高兴您发现它有用,问题和答案更有用(首选)在这个网站上,这对很多人都有帮助,而且它不是一个代码编写服务。你没有说明问题中的amt是什么,也没有回答我在你的问题上留下的问题。如果端口列表是固定的,问题应该说明这一点,并清楚地解释端口是什么。
module shifter(
   input               dat_rx, //1bit data input    
   input               clk,
   output logic [15:0] dat_tx  //Parallel output
);
   always @(posedge clk) begin
     dat_tx <= {dat_rx, dat_tx[15:1]}; //Next input and right shift
   end
endmodule
module shifter_comb(
   input dat_rx,
   input  logic [15:0] dat_old,
   output logic [15:0] dat_tx
);
   always @* begin
     dat_tx = {dat_rx, dat_old[15:1]}; //Next input and right shift
   end
endmodule