HDLBits答案(12)_Verilog移位寄存器
Verilog移位寄存器
HDLBits鏈接
前言
今天更新一節(jié)寄存器相關(guān)內(nèi)容,其中涉及CRC校驗(yàn)的內(nèi)容是用線性反饋移位寄存器搭建而成的。
題庫(kù)
題目描述1:
構(gòu)建一個(gè)4bit的移位寄存器(右移),含異步復(fù)位、同步加載和使能
- areset:讓寄存器復(fù)位為0
 - load:加載4bit數(shù)據(jù)到移位寄存器中,不移位
 - ena:使能右移
 - q:移位寄存器中的內(nèi)容
 
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:
構(gòu)建一個(gè)100位的左右旋轉(zhuǎn)器,同步load,左右旋轉(zhuǎn)需使能。旋轉(zhuǎn)器從另一端輸入移位的位元,不像移位器那樣丟棄移位的位元而以零位移位。如果啟用,旋轉(zhuǎn)器就會(huì)旋轉(zhuǎn)這些位,而不會(huì)修改或丟棄它們。
- load:加載100位的移位寄存器數(shù)據(jù)
 - ena[1:0]:2’b01 右轉(zhuǎn)1bit; 2’b10 左轉(zhuǎn)1bit;其他情況不轉(zhuǎn)
 - q:旋轉(zhuǎn)器內(nèi)容
 
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:
建立一個(gè)64位算術(shù)移位寄存器,同步加載。移位器可以左右移位,并按數(shù)量選擇1位或8位的移位。
- load:加載數(shù)據(jù)
 - ena:決定是否移位
 - amount:決定移位方向與數(shù)量:2’b00:左移1位;2’b01:左移8位;2’b10:右移1位;2’b11:右移8位
 - q:寄存器內(nèi)容(輸出)
 
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:
構(gòu)造線性移位寄存器,reset應(yīng)當(dāng)使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:
為這個(gè)序列電路編寫Verilog代碼。假設(shè)你要在DE1-SoC板上實(shí)現(xiàn)這個(gè)電路。將R輸入連接到SW開關(guān),將時(shí)鐘連接到密鑰[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:
構(gòu)建一個(gè)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:**實(shí)現(xiàn)如下電路圖
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:**實(shí)現(xiàn)如下電路圖
- 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:
在這個(gè)問題中,你將為一個(gè)8x1存儲(chǔ)器設(shè)計(jì)一個(gè)電路,在這個(gè)電路中,寫入到存儲(chǔ)器是通過移位來完成的,而讀取是“隨機(jī)訪問”,就像在一個(gè)典型的RAM中一樣。然后您將使用該電路實(shí)現(xiàn)一個(gè)3輸入邏輯功能。
首先,用8個(gè)d類型觸發(fā)器創(chuàng)建一個(gè)8位移位寄存器。標(biāo)記為Q[0]到Q[7]。移位寄存器輸入稱為S,輸入Q[0] (MSB先移位)。使能輸入enable控制是否移位,擴(kuò)展電路使其有3個(gè)額外的輸入A,B,C和一個(gè)輸出Z。電路的行為應(yīng)該如下:當(dāng)ABC為000時(shí),Z=Q[0],當(dāng)ABC為001時(shí),Z=Q[1],以此類推。你的電路應(yīng)該只包含8位移位寄存器和多路復(fù)用器。(這個(gè)電路稱為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小結(jié)
今天更新了部分移位寄存器部分的答案,注意最后一題用了一些技巧來簡(jiǎn)化代碼書寫,但實(shí)現(xiàn)時(shí)電路并無差異,體現(xiàn)了HDL中Describe的特性。之后一段時(shí)間還要忙比賽,有空再繼續(xù)更,希望隊(duì)伍比賽順利。
總結(jié)
以上是生活随笔為你收集整理的HDLBits答案(12)_Verilog移位寄存器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: HDLBits答案(11)_Verilo
 - 下一篇: HDLBits答案(13)_Verilo