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.
module top_module (
input clk,
input reset, // Synchronous active-high reset
output [3:0] q);
always @(posedge clk) begin
if(reset)
q<=4'b0;
else
q<=q+1'b1;
end
endmodule
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.
module top_module (
input clk,
input reset, // Synchronous active-high reset
output [3:0] q);
always @(posedge clk) begin
if(reset||q>=4'd9)
q<=4'b0;
else
q<=q+1'b1;
end
endmodule
Make a decade counter that counts 1 through 10, inclusive. The reset input is synchronous, and should reset the counter to 1
module top_module (
input clk,
input reset,
output [3:0] q);
always @(posedge clk) begin
if(reset||q>=4'd10)
q<=1'b1;
else
q<=q+1'b1;
end
endmodule
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
if(reset)
q<=4'b0;
else if(slowena) begin
if(q==4'd9) begin
q<=4'b0;
end
else begin
q<=q+1'b1;
end
end
end
endmodule
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.
- logic gates
module count4( input clk, input enable, input load, input [3:0] d, output reg [3:0] Q );
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.
module top_module (
input clk,
input reset,
input enable,
output [3:0] Q,
output c_enable,
output c_load,
output [3:0] c_d
);
assign c_enable = enable;
assign c_load = reset | ((Q == 4'd12) && (enable == 1'b1));
assign c_d = c_load ? 4'd1 : 4'd0;
count4 the_counter (clk, c_enable, c_load, c_d , Q);
endmodule
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.
module bcdcount ( input clk, input reset, input enable, output reg [3:0] Q );
module top_module (
input clk,
input reset,
output OneHertz,
output [2:0] c_enable
); //
wire[3:0] one, ten, hundred;
assign c_enable = {one == 4'd9 && ten == 4'd9, one == 4'd9, 1'b1};
assign OneHertz = (one == 4'd9 && ten == 4'd9 && hundred == 4'd9);
bcdcount counter0 (clk, reset, c_enable[0], one);
bcdcount counter1 (clk, reset, c_enable[1], ten);
bcdcount counter2 (clk, reset, c_enable[2], hundred);
endmodule
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
module top_module (
input clk,
input reset, // Synchronous active-high reset
output [3:1] ena,
output [15:0] q);
reg [3:0] one;
reg [7:4] ten;
reg [11:8] hundred;
reg [15:9] thousand;
assign q = {thousand,hundred,ten,one};
assign ena[1]=(one==4'd9)?1'b1:1'b0;
assign ena[2]=((one==4'd9)&&(ten==4'd9))?1'b1:1'b0;
assign ena[3]=((one==4'd9)&&(ten==4'd9)&&(hundred== 4'd9))?1'b1:1'b0;
always @(posedge clk) begin
if(reset)
one<=4'b0;
else if(one==4'd9)
one<=4'b0;
else begin
one<=one+1'b1;
end
end
always @(posedge clk) begin
if(reset)
ten<=4'b0;
else if(ten==4'd9&&one==4'd9)
ten<=4'b0;
else if(one==4'd9)
ten<=ten+1'b1;
end
always @(posedge clk) begin
if(reset)
hundred<=4'b0;
else if(hundred==4'd9&&ten==4'd9&&one==4'd9)
hundred<=4'b0;
else if(ten==4'd9&&one==4'd9)
hundred<=hundred+1'b1;
end
always @(posedge clk) begin
if(reset)
thousand<=4'b0;
else if(thousand==4'd9&&hundred==4'd9&&ten==4'd9&&one==4'd9)
thousand<=4'b0;
else if(hundred==4'd9&&ten==4'd9&&one==4'd9)
thousand<=thousand+1'b1;
end
endmodule
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 top_module(
input clk,
input reset,
input ena,
output pm,
output [7:0] hh,
output [7:0] mm,
output [7:0] ss);
reg [3:0] ss_one;
reg [3:0] ss_ten;
reg [3:0] mm_one;
reg [3:0] mm_ten;
reg [3:0] hh_one;
reg [3:0] hh_ten;
wire pm_ding;
assign ss={ss_ten,ss_one};
assign mm={mm_ten,mm_one};
assign hh={hh_ten,hh_one};
always @(posedge clk) begin
if(reset)
ss_one<=4'b0;
else if(ena) begin
if(ss_one==4'd9) begin
ss_one<=4'b0;
end
else begin
ss_one<=ss_one+1'b1;
end
end
end
always @(posedge clk) begin
if(reset)
ss_ten<=4'b0;
else if(ena) begin
if(ss_ten==4'd5&&ss_one==4'd9) begin
ss_ten<=4'b0;
end
else if(ss_one==9) begin
ss_ten<=ss_ten+1'b1;
end
end
end
always @(posedge clk) begin
if(reset)
mm_one<=4'b0;
else if(ena) begin
if(mm_one==4'd9&&ss_ten==4'd5&&ss_one==4'd9) begin
mm_one<=4'b0;
end
else if(ss_ten==4'd5&&ss_one==9) begin
mm_one<=mm_one+1'b1;
end
end
end
always @(posedge clk) begin
if(reset)
mm_ten<=4'b0;
else if(ena) begin
if(mm_ten==4'd5&&mm_one==4'd9&&ss_ten==4'd5&&ss_one==4'd9) begin
mm_ten<=4'b0;
end
else if(mm_one==4'd9&&ss_ten==4'd5&&ss_one==9) begin
mm_ten<=mm_ten+1'b1;
end
end
end
always @(posedge clk) begin
if(reset)
hh_one<=4'd2;
else if(ena) begin
if(hh_one==4'd9&&mm_ten==4'd5&&mm_one==4'd9&&ss_ten==4'd5&&ss_one==4'd9) begin
hh_one<=4'b0;
end
else if(hh_ten==1&&hh_one==4'd2&&mm_ten==4'd5&&mm_one==4'd9&&ss_ten==4'd5&&ss_one==4'd9) begin
hh_one<=4'd1;
end
else if(mm_ten==4'd5&&mm_one==4'd9&&ss_ten==4'd5&&ss_one==9) begin
hh_one<=hh_one+1'b1;
end
end
end
always @(posedge clk) begin
if(reset)
hh_ten<=4'd1;
else if(ena) begin
if(hh_ten==1&&hh_one==4'd2&&mm_ten==4'd5&&mm_one==4'd9&&ss_ten==4'd5&&ss_one==4'd9) begin
hh_ten<=4'd0;
end
else if(hh_one==4'd9&&mm_ten==4'd5&&mm_one==4'd9&&ss_ten==4'd5&&ss_one==9) begin
hh_ten<=hh_ten+1'b1;
end
end
end
assign pm_ding=hh_ten==4'd1&&hh_one==4'd1&&mm_ten==4'd5&&mm_one==4'd9&&ss_ten==4'd5&&ss_one==4'd9;
always@(posedge clk)begin
if(reset)begin
pm<=1'b0;
end
else if(pm_ding)begin
pm<=~pm;
end
end
endmodule