2021年6月25日 星期五

HDLBits Left/right arithmetic shift by 1 or 8(Shift18)

 HDLBits Left/right arithmetic shift by 1 or 8(Shift18)

Build a 64-bit arithmetic shift register, with synchronous load. The shifter can shift both left and right, and by 1 or 8 bit positions, selected by amount.

An arithmetic right shift shifts in the sign bit of the number in the shift register (q[63] in this case) instead of zero as done by a logical right shift. Another way of thinking about an arithmetic right shift is that it assumes the number being shifted is signed and preserves the sign, so that arithmetic right shift divides a signed number by a power of two.

There is no difference between logical and arithmetic left shifts.

  • load: Loads shift register with data[63:0] instead of shifting.
  • ena: Chooses whether to shift.
  • amount: Chooses which direction and how much to shift.
    • 2'b00: shift left by 1 bit.
    • 2'b01: shift left by 8 bits.
    • 2'b10: shift right by 1 bit.
    • 2'b11: shift right by 8 bits.
  • q: The contents of the shifter.

建立一個具有同步置數的64位算術移位Register。 Shifter可以向左和向右移位1或8位的位置(按不同的數字進行選擇)。
算術右移是將移位寄存器中數字(q[63])的符號位移位,而不是邏輯右移所做的零。另一種考慮算術右移的方法是,它假設移動的數是有符號的,並且保留了符號,所以算術右移將有符號的數除以2的冪。
邏輯左移和算術左移之間沒有區別。
load:data[63:0]置入移位寄存器而不是移位。
ena:選擇是否移位。
amount:選擇移位方向和移位改變多少:
amount操作
2’b00左移1位 
2’b01左移8位
2’b10右移1位 
2’b11右移8位


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)
                q <= data;
            else if (ena)
                begin
                    case (amount)
                        2'b00 : q <= {q[62:0],1'b0};  //邏輯左移1bit
                        2'b01 : q <= {q[55:0],8'b0};  //邏輯左移8bits
                        2'b10 : q <= {q[63],q[63:1]};    //算術右移1bit
                        2'b11 : q <= {{8{q[63]}},q[63:8]};//算術右移8bits
                    endcase
                end
         end   
endmodule


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...