HDLBits:在線學習 Verilog (十九 · Problem 90 - 94) 首先附上傳送門 Mt2015 muxdff - HDLBits?hdlbits.01xz.netProblem 90 Mux and DFF牛刀小試考慮下圖所示的時序電路問題: 我們用3個包含觸發器和多路選擇器的子模塊來實現圖中電路。題目要求我們寫出包含一個觸發器和一個多路選擇器的子模塊。答案與解析module top_module ( input clk, input L, input r_in, input q_in, output reg Q); wire temp; assign temp = L ? r_in : q_in; //2 to 1 選擇器 always @ (posedge clk ) //觸發器 begin Q <= temp; end endmodule Problem 91 Mux and DFF牛刀小試考慮一個 n-bit 移位寄存器。 如上圖所示,我們還是實現包含選擇器和觸發器的部分。 答案與解析module top_module ( input clk, input w, R, E, L, output Q ); wire temp1, temp2; assign temp1 = E ? w:Q; assign temp2 = L ? R:temp1; //與上題類似,不做贅述 always @ (posedge clk) begin Q <= temp2; end endmodule Problem 92 DFFS and gates牛刀小試如下圖所示的狀態機,假設D觸發器在狀態機啟動之前初始化為0,實現該電路: 答案與解析本題為門電路與觸發器的結合,上圖包含三個觸發器、異或門、與門和或門。只需要注意後兩個觸發器輸出是取反即可。module top_module ( input clk, input x, output z ); reg q1 = 0; reg q2 = 0; reg q3 = 0; always @ (posedge clk) begin q1 <= x ^ q1; end always @ (posedge clk) begin q2 <= x & (~q2); end always @ (posedge clk) begin q3 <= x | (~q3); end assign z = ~(q1 | q2 | q3); endmodule Problem 93 Create circuit from truth table牛刀小試 JK觸發器的真值表如下圖所示,僅使用D觸發器和門電路來實現該JK觸發器。其中Qold是D觸發器在時鐘上升沿之前的輸出。 答案與解析module top_module ( input clk, input j, input k, output Q); always @ (posedge clk) begin case ({j, k}) 2b00: Q <= Q; 2b01: Q <= 1b0; 2b10: Q <= 1b1; 2b11: Q <= ~Q; endcase end endmodule Problem 94 Detect an Edge牛刀小試對於每個8bit的變數,檢測這些信號什麼時候從0變為1(類似檢測上升沿),輸出應該在0到1 變化之後纔有值。下圖給我們展示了輸入in[1]和輸出pedge[1]的時序關係圖: 解答與解析: 邊沿檢測的特性就是兩邊電平發生了變化,無非是0變1上升沿,1變0下降沿。(具體可參考)https://blog.csdn.net/qq_31799983/article/details/81544707?blog.csdn.net具體的設計可以採用一個寄存器Q來存儲上一個時鐘沿的輸入值D,當寄存器輸出Q與輸入D的值分別為1、0時,則檢測到下降沿。如圖: 這就像本題代碼, D就是in, Q就是temp。 module top_module ( input clk, input [7:0] in, output [7:0] pedge ); reg [7:0] temp; always @ (posedge clk) begin temp <= in; //temp始終比in晚一個週期 pedge <= ~temp & in; //當輸出為1時檢測到上升沿 //本題剛好與所示時序圖相反,其中Q就相當於temp, D就相當於in,檢測下降沿是對in取反就好。 end endmodule 推薦閱讀: 相關文章 {{#data}} {{title}} {{/data}}