HDLBits答案(12)_Verilog移位寄存器
Verilog移位寄存器
HDLBits鏈接
前言
今天更新一節寄存器相關內容,其中涉及CRC校驗的內容是用線性反饋移位寄存器搭建而成的。
題庫
題目描述1:
構建一個4bit的移位寄存器(右移),含異步復位、同步加載和使能
- areset:讓寄存器復位為0
- load:加載4bit數據到移位寄存器中,不移位
- ena:使能右移
- q:移位寄存器中的內容
Solution1:
module top_module(input clk,input areset, // async active-high reset to zeroinput load,input ena,input [3:0] data,output reg [3:0] q); always @(posedge clk or posedge areset)beginif(areset)beginq <= 4'b0;endelse if(load) beginq <= data;endelse if(ena)beginq <= {1'b0,q[3:1]};endelse beginq <= q;endendendmodule題目描述2:
構建一個100位的左右旋轉器,同步load,左右旋轉需使能。旋轉器從另一端輸入移位的位元,不像移位器那樣丟棄移位的位元而以零位移位。如果啟用,旋轉器就會旋轉這些位,而不會修改或丟棄它們。
- load:加載100位的移位寄存器數據
- ena[1:0]:2’b01 右轉1bit; 2’b10 左轉1bit;其他情況不轉
- q:旋轉器內容
Solution2:
module top_module(input clk,input load,input [1:0] ena,input [99:0] data,output reg [99:0] q);always @(posedge clk) beginif(load) beginq <= data;endelse begincase (ena)2'b01:q <= {q[0],q[99:1]};2'b10:q <= {q[98:0],q[99]};default:q <= q;endcaseendendendmodule題目描述3:
建立一個64位算術移位寄存器,同步加載。移位器可以左右移位,并按數量選擇1位或8位的移位。
- load:加載數據
- ena:決定是否移位
- amount:決定移位方向與數量:2’b00:左移1位;2’b01:左移8位;2’b10:右移1位;2’b11:右移8位
- q:寄存器內容(輸出)
Solution3:
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)beginif(load)beginq <= data;endelse beginif(ena)begincase(amount)2'b00: q <= {q[62:0],1'b0};2'b01: q <= {q[55:0],8'b0};2'b10: q <= {q[63],q[63:1]};2'b11: q <= {{8{q[63]}},q[63:8]};endcaseendelse beginq <= q;endendendendmodule題目描述4:
構造線性移位寄存器,reset應當使LFSR歸1。
Solution4:
module top_module(input clk,input reset, // Active-high synchronous reset to 5'h1output [4:0] q ); always @(posedge clk)beginif(reset)beginq <= 5'h1;endelse beginq[4] <= 1'b0 ^ q[0];q[3] <= q[4];q[2] <= q[3] ^ q[0];q[1] <= q[2];q[0] <= q[1];endendendmodule題目描述5:
為這個序列電路編寫Verilog代碼。假設你要在DE1-SoC板上實現這個電路。將R輸入連接到SW開關,將時鐘連接到密鑰[0],將L連接到密鑰[1],將Q輸出連接到紅燈LEDR上。
Solution5:
module top_module (input [2:0] SW, // Rinput [1:0] KEY, // L and clkoutput [2:0] LEDR); // Qwire clk;assign clk = KEY[0];always @(posedge clk)beginif(KEY[1])beginLEDR[0] <= SW[0];LEDR[1] <= SW[1];LEDR[2] <= SW[2];endelse beginLEDR[0] <= LEDR[2];LEDR[1] <= LEDR[0];LEDR[2] <= LEDR[2] ^ LEDR[1];endendendmodule題目描述5:
構建一個32位的Galois LFSR,其taps位置為32、22、2和1。
Solution5:
module top_module(input clk,input reset, // Active-high synchronous reset to 32'h1output [31:0] q ); integer i;always @(posedge clk)beginif(reset)beginq <= 32'h1;endelse beginfor(i=0;i<32;i++)beginif((i==21)||(i==1)||(i==0))beginq[i] <= q[i+1] ^ q[0];endelse if(i==31)beginq[31] <= 1'b0 ^ q[0];endelse beginq[i] <= q[i+1];end endendendendmodule**題目描述6:**實現如下電路圖
Solution6:
module top_module (input clk,input resetn, // synchronous resetinput in,output out);reg [3:0] tmp;assign out = tmp[3];always @(posedge clk)beginif(!resetn)begintmp <= 4'h0;endelse begintmp <= {tmp[3:1],in};endendendmodule**題目描述7:**實現如下電路圖
- Connect the R inputs to the SW switches,
- clk to KEY[0],
- E to KEY[1],
- L to KEY[2], and
- w to KEY[3].
- Connect the outputs to the red lights LEDR[3:0].
Solution7:
module top_module (input [3:0] SW,input [3:0] KEY,output [3:0] LEDR ); MUXDFF u1(.clk(KEY[0]),.w(KEY[3]),.R(SW[3]),.E(KEY[1]),.L(KEY[2]),.Q(LEDR[3]));MUXDFF u2(.clk(KEY[0]),.w(LEDR[3]),.R(SW[2]),.E(KEY[1]),.L(KEY[2]),.Q(LEDR[2]));MUXDFF u3(.clk(KEY[0]),.w(LEDR[2]),.R(SW[1]),.E(KEY[1]),.L(KEY[2]),.Q(LEDR[1]));MUXDFF u4(.clk(KEY[0]),.w(LEDR[1]),.R(SW[0]),.E(KEY[1]),.L(KEY[2]),.Q(LEDR[0]));endmodulemodule MUXDFF (input clk,input w,R,E,L,output Q );wire tmp;assign tmp = E ? w : Q;always @(posedge clk)beginQ <= L? R : tmp;endendmodule題目描述8:
在這個問題中,你將為一個8x1存儲器設計一個電路,在這個電路中,寫入到存儲器是通過移位來完成的,而讀取是“隨機訪問”,就像在一個典型的RAM中一樣。然后您將使用該電路實現一個3輸入邏輯功能。
首先,用8個d類型觸發器創建一個8位移位寄存器。標記為Q[0]到Q[7]。移位寄存器輸入稱為S,輸入Q[0] (MSB先移位)。使能輸入enable控制是否移位,擴展電路使其有3個額外的輸入A,B,C和一個輸出Z。電路的行為應該如下:當ABC為000時,Z=Q[0],當ABC為001時,Z=Q[1],以此類推。你的電路應該只包含8位移位寄存器和多路復用器。(這個電路稱為3輸入查找表(LUT))。
Solution8:
module top_module (input clk,input enable,input S,input A, B, C,output Z ); reg [7:0] Q;always @(posedge clk)beginif(enable)beginQ <= {Q[6:0],S};endelse beginQ <= Q;endendassign Z = Q[{A,B,C}];endmodule小結
今天更新了部分移位寄存器部分的答案,注意最后一題用了一些技巧來簡化代碼書寫,但實現時電路并無差異,體現了HDL中Describe的特性。之后一段時間還要忙比賽,有空再繼續更,希望隊伍比賽順利。
總結
以上是生活随笔為你收集整理的HDLBits答案(12)_Verilog移位寄存器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDLBits答案(11)_Verilo
- 下一篇: HDLBits答案(13)_Verilo