Build a 4-bit binary counter that counts from 0 through 15, inclusive, with a period of 16. The reset input is synchronous, and should reset the counter to 0.
1 2 3 4 5 6 7 8 9 10 11 12 13
module top_module ( input clk, input reset, // Synchronous active-high reset output [3:0] q); always @(posedge clk) begin if (reset) begin q <= 0; end elsebegin q <= q + 1; end end endmodule
2 Counter10
Build a decade counter that counts from 0 through 9, inclusive, with a period of 10. The reset input is synchronous, and should reset the counter to 0.
1 2 3 4 5 6 7 8 9 10 11 12 13
module top_module ( input clk, input reset, // Synchronous active-high reset output [3:0] q); always @(posedge clk) begin if (reset || q == 4'd9) begin q <= 0; end elsebegin q <= q + 1; end end endmodule
3 Counter10-1
Make a decade counter that counts 1 through 10, inclusive. The reset input is synchronous, and should reset the counter to 1.
1 2 3 4 5 6 7 8 9 10 11 12 13
module top_module ( input clk, input reset, output [3:0] q); always @(posedge clk) begin if (reset || q == 4'd10) begin q <= 1'b1; end elsebegin q <= q + 1'b1; end end endmodule
4 CounterSlow
Build a decade counter that counts from 0 through 9, inclusive, with a period of 10. The reset input is synchronous, and should reset the counter to 0. We want to be able to pause the counter rather than always incrementing every clock cycle, so the slowena input indicates when the counter should increment.
module top_module ( input clk, input slowena, input reset, output [3:0] q); always @(posedge clk) begin // ··· and slowena means when q == 9 and slowena == 0, the result should be keeped instead of be added. if (reset || q == 4'd9 && slowena == 1) begin q <= 0; end elseif (slowena == 1) begin q <= q + 1; end /* // here is a wiser solution as below always @(posedge clk) begin if (reset == 1'b1) q <= 4'd0; else if(slowena == 1'b1) begin if (q == 4'd9) q <= 4'd0; else q <= q + 1'b1; end else q <= q; end */ end endmodule
5
Design a 1-12 counter with the following inputs and outputs:
Reset Synchronous active-high reset that forces the counter to 1
Enable Set high for the counter to run
Clk Positive edge-triggered clock input
Q[3:0] The output of the counter
c_enable, c_load, c_d[3:0] Control signals going to the provided 4-bit counter, so correct operation can be verified.
You have the following components available:
the 4-bit binary counter (count4 ) below, which has Enable and synchronous parallel-load inputs (load has higher priority than enable). The count4 module is provided to you. Instantiate it in your circuit.
The c_enable , c_load , and c_d outputs are the signals that go to the internal counter’s enable , load , and d inputs, respectively. Their purpose is to allow these signals to be checked for correctness.
From a 1000 Hz clock, derive a 1 Hz signal, called OneHertz , that could be used to drive an Enable signal for a set of hour/minute/second counters to create a digital wall clock. Since we want the clock to count once per second, the OneHertz signal must be asserted for exactly one cycle each second. Build the frequency divider using modulo-10 (BCD) counters and as few other gates as possible. Also output the enable signals from each of the BCD counters you use (c_enable[0] for the fastest counter, c_enable[2] for the slowest).
The following BCD counter is provided for you. Enable must be high for the counter to run. Reset is synchronous and set high to force the counter to zero. All counters in your circuit must directly use the same 1000 Hz signal.
Build a 4-digit BCD (binary-coded decimal) counter. Each decimal digit is encoded using 4 bits: q[3:0] is the ones digit, q[7:4] is the tens digit, etc. For digits [3:1], also output an enable signal indicating when each of the upper three digits should be incremented.
You may want to instantiate or modify some one-digit decade counters.
// version - 0.0 - This version is based ont the idea on this site : https://blog.csdn.net/anbncn1234/article/details/106115746 module bcd_counter( input clk, input rst, input ena, output [3:0] q ); always @(posedge clk) begin if (rst) begin q <= 0; end elseif (ena == 1'b1) begin // back to 0 if (q == 4'd9) begin q <= 0; end // output carryflag elseif ( q == 4'd8) begin q <= q + 1'b1; end // +1 count elsebegin q <= q + 1'b1; end end end endmodule module top_module ( input clk, input reset, // Synchronous active-high reset output [3:1] ena, output [15:0] q ); bcd_counter bcd_counter_inst_unit(.clk(clk),.rst(reset),.ena(1'b1),.q(q[3:0])); bcd_counter bcd_counter_inst_decade(.clk(clk),.rst(reset),.ena(q[3:0] == 4'd9),.q(q[7:4])); bcd_counter bcd_counter_inst_hundred(.clk(clk),.rst(reset),.ena(q[3:0] == 4'd9 & q[7:4] == 4'd9),.q(q[11:8])); bcd_counter bcd_counter_inst_thousand(.clk(clk),.rst(reset),.ena(q[3:0] == 4'd9 & q[7:4] == 4'd9 & q[11:8] == 4'd9),.q(q[15:12])); assign ena = {(q[3:0] == 4'd9 & q[7:4] == 4'd9 & q[11:8] == 4'd9),(q[3:0] == 4'd9 & q[7:4] == 4'd9),q[3:0] == 4'd9}; endmodule
// version - 0.1 - wrong verion bug comes from the ena_next signal, which is logical wrong. Can't find a solution now. module bcd_counter( input clk, input rst, input ena, output [3:0] q, output ena_next ); always @(posedge clk) begin if (rst) begin q <= 0; ena_next <= 0; end elseif (ena == 1'b1) begin // back to 0 if (q == 4'd9) begin q <= 0; ena_next <= 0; end // output carryflag elseif ( q == 4'd8) begin ena_next <= 1'b1; q <= q + 1'b1; end // +1 count elsebegin q <= q + 1'b1; end end end endmodule module top_module ( input clk, input reset, // Synchronous active-high reset output [3:1] ena, output [15:0] q ); bcd_counter bcd_counter_inst_unit(.clk(clk),.rst(reset),.ena(1'b1),.q(q[3:0]),.ena_next(ena[1])); bcd_counter bcd_counter_inst_decade(.clk(clk),.rst(reset),.ena(ena[1]),.q(q[7:4]),.ena_next(ena[2])); bcd_counter bcd_counter_inst_hundred(.clk(clk),.rst(reset),.ena(ena[2]),.q(q[11:8]),.ena_next(ena[3])); bcd_counter bcd_counter_inst_thousand(.clk(clk),.rst(reset),.ena(ena[3]),.q(q[15:12])); endmodule
Debug
注意定义的时候,是 q [3:0] 还是 [3:0]q。否则可能会出现bug,q has an aggregate value
这个ena信号,需要在q值为9(低位都为9)的时候,进行置高。
8Count clock
Create a set of counters suitable for use as a 12-hour clock (with am/pm indicator). Your counters are clocked by a fast-running clk, with a pulse on ena whenever your clock should increment (i.e., once per second).
reset resets the clock to 12:00 AM. pm is 0 for AM and 1 for PM. hh, mm, and ss are two BCD (Binary-Coded Decimal) digits each for hours (01-12), minutes (00-59), and seconds (00-59). Reset has higher priority than enable, and can occur even when not enabled.
The following timing diagram shows the rollover behaviour from 11:59:59 AM to 12:00:00 PM and the synchronous reset and enable behaviour.
module sec_counter( input clk, input reset, input ena, output [7:0] ss ); always @(posedge clk) begin if (reset) begin ss <= 0; end elsebegin if (ena == 1'b1) begin if (ss == 8'd59) begin ss <= 0; end else ss <= ss + 1'b1; end elsebegin ss <= ss; end end end endmodule
module min_counter( input clk, input reset, input ss, input ena, output [7:0] mm ); always @(posedge clk) begin if (reset) begin mm <= 0; end elsebegin if (ena == 1'b1 && ss == 8'd59) begin if (mm == 5'd59) begin mm <= 0; end else mm <= mm + 1'b1; end else mm <= mm; end end endmodule
module hour_counter( input clk, input reset, input ss, input mm, input ena, output [7:0] hh ); always @(posedge clk) begin if (reset) begin hh <= 8'd12; end elsebegin if (ena == 1'b1 && ss == 8'd59 && mm == 8'd59) begin if (hh == 8'd12) begin hh <= 1'b1; end else hh <= hh + 1'b1; end else hh <= hh; end end endmodule
module pm_counter( input clk, input reset, input ss, input mm, input hh, input ena, output pm ); always @(posedge clk) begin if (reset) begin pm <= 0; end elsebegin if (hh == 8'd11 && mm == 8'd59 && ss == 8'd59) begin pm <= ~pm; end else pm <= pm; end end endmodule
module bcd_counter( input clk, input reset, input ena, output [3:0] q ); always @(posedge clk) begin if (reset) begin q <= 0; end elsebegin if (ena == 1'b1) begin if (q == 4'd9) begin q <= 0; end elsebegin q <= q + 1'b1; end end elsebegin q <= q; end end end endmodule
module bcd_counter_mod_6( input clk, input reset, input ena, output [3:0] q ); always @(posedge clk) begin if (reset) begin q <= 0; end elsebegin if (ena == 1'b1) begin if (q == 4'd5) begin q <= 0; end elsebegin q <= q + 1'b1; end end elsebegin q <= q; end end end endmodule
module bcd_counter_mod_12( input clk, input reset, input ena, output [7:0] q ); always @(posedge clk) begin if (reset) begin q <= 8'h12; end elsebegin if (ena == 1'b1) begin if (q == 8'h12) begin q <= 8'h01; end elseif (q == 8'h09) begin q <= 8'h10; end elsebegin q <= q + 1'b1; end end elsebegin q <= q; end end end endmodule
module bcd_counter_pm( input clk, input reset, input ena, output q ); always @(posedge clk) begin if (reset) begin q <= 0; end elsebegin if (ena == 1'b1) begin q <= ~q; end elsebegin q <= q; end end end endmodule
Debug
例化模块时,需要在信号前面加上.,否则出现报错cannot connect instance ports both by order and by name。