Verilog典型题目学习01:算数左右移

5. 算数左右移

题目要求

构建一个64位算术移位寄存器(shifter),具有同步load数据功能.这个shifter根据amount信号选择,可左右移1位或8位.

在算术右移时,若符号位(q[63])为1则需要复制位,而并非简单的逻辑右移.

算术左移与逻辑左移没有区别.

  • load: 寄存器载入数据(优先级最高)

  • ena: 决定是否发生位移

  • amount: 决定位移的方向

    • 2'b00: 算术左移1位

    • 2'b01: 算术左移8位

    • 2'b10: 算术右移1位

    • 2'b11: 算术右移8位

  • q: shifter的内容

端口描述

module top_module(
    input clk,
    input load,
    input ena,
    input [1:0] amount,
    input [63:0] data,
    output reg [63:0] q);


 

答案

module top_module(
    input clk,
    input load,
    input ena,
    input [1:0] amount,
    input [63:0] data,
    output reg [63:0] q);

always @(posedge clk)  begin
    if (load) begin
        q<= data;
    end 
    
    else if (ena) begin
        case (amount)
            2'b00: q<= (q<<1);
            2'b01: q<= (q<<8);
            2'b10: if(q[63])begin
                q<= {1'b1,q[63:1]};
                   end     
                   else begin
                       q<=(q>>1);
                   end
            2'b11: if(q[63])begin
                   q<={8'b11111111,q[63:8]};
                   end  
                   else begin
                       q<=(q>>8);
                   end
        endcase
    end
    
end



endmodule


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值