forked from bogini/Pong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m100_counter.v
51 lines (47 loc) · 1.1 KB
/
m100_counter.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Listing 14.8
module m100_counter
(
input wire clk, reset,
input wire d_inc, d_clr,
output wire [3:0] dig0, dig1
);
// signal declaration
reg [3:0] dig0_reg, dig1_reg, dig0_next, dig1_next;
// registers
always @(posedge clk, posedge reset)
if (reset)
begin
dig1_reg <= 0;
dig0_reg <= 0;
end
else
begin
dig1_reg <= dig1_next;
dig0_reg <= dig0_next;
end
// next-state logic
always @*
begin
dig0_next = dig0_reg;
dig1_next = dig1_reg;
if (d_clr)
begin
dig0_next = 0;
dig1_next = 0;
end
else if (d_inc)
if (dig0_reg==9)
begin
dig0_next = 0;
if (dig1_reg==9)
dig1_next = 0;
else
dig1_next = dig1_reg + 1;
end
else // dig0 not 9
dig0_next = dig0_reg + 1;
end
// output
assign dig0 = dig0_reg;
assign dig1 = dig1_reg;
endmodule