-
Notifications
You must be signed in to change notification settings - Fork 2
/
clockwork.v
174 lines (162 loc) · 4.51 KB
/
clockwork.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* ------------------------------------------------ *
* Title : Clockwork *
* Project : Digital Clock *
* ------------------------------------------------ *
* File : clockwork.v *
* Author : Yigit Suoglu *
* Last Edit : 21/04/2021 *
* Licence : CERN-OHL-W *
* ------------------------------------------------ *
* Description : Time keeping module for a clock *
* ------------------------------------------------ */
module clockWorkHex(clk_1hz, time_in, time_out, time_ow);
input clk_1hz, time_ow; //1 Hz clock (clock), Time overwrite (asynchronous reset)
//Time signal format: hhhhh:mmmmmm:ssssss
input [16:0] time_in; //time input
output [16:0] time_out; //main output
//separated time signals to respective meaning
wire [5:0] sec_in, min_in;
wire [4:0] hour_in;
reg [5:0] sec_reg, min_reg;
reg [4:0] hour_reg;
//separation and combination of time signals
assign {hour_in, min_in, sec_in} = time_in;
assign time_out = {hour_reg, min_reg, sec_reg};
//handle seconds
always@(posedge clk_1hz or posedge time_ow)
begin
if(time_ow)
begin
sec_reg <= sec_in;
end
else
begin
sec_reg <= (sec_reg == 6'd59) ? 6'd0 : (sec_reg + 6'd1);
end
end
//handle minutes
always@(posedge clk_1hz or posedge time_ow)
begin
if(time_ow)
begin
min_reg <= min_in;
end
else
begin
if(sec_reg == 6'd59)
begin
min_reg <= (min_reg == 6'd59) ? 6'd0 : (min_reg + 6'd1);
end
end
end
//handle hours
always@(posedge clk_1hz or posedge time_ow)
begin
if(time_ow)
begin
hour_reg <= hour_in;
end
else
begin
if((sec_reg == 6'd59)&(min_reg == 6'd59))
begin
hour_reg <= (hour_reg == 5'd23) ? 5'd0 : (hour_reg + 5'd1);
end
end
end
endmodule
module clockWorkDec(clk_1hz, time_in, time_out, time_ow);
input clk_1hz, time_ow; //1 Hz clock (clock), Time overwrite (asynchronous reset)
//Time signal format: hh_hhhh:mmm_mmmm:sss_ssss only decimal values
input [19:0] time_in; //time input
output [19:0] time_out; //main output
//separated time signals to respective meaning
wire [6:0] sec_in, min_in;
wire [5:0] hour_in;
reg [6:0] sec_reg, min_reg;
reg [5:0] hour_reg;
//separation and combination of time signals
assign {hour_in, min_in, sec_in} = time_in;
assign time_out = {hour_reg, min_reg, sec_reg};
//handle seconds
always@(posedge clk_1hz or posedge time_ow)
begin
if(time_ow)
begin
sec_reg <= sec_in;
end
else
begin
casex(sec_reg)
7'h59:
begin
sec_reg <= 7'd0;
end
7'h?9:
begin
sec_reg <= {(sec_reg[6:4]+3'd1),4'h0};
end
default:
begin
sec_reg <= sec_reg + 7'd1;
end
endcase
end
end
//handle minutes
always@(posedge clk_1hz or posedge time_ow)
begin
if(time_ow)
begin
min_reg <= min_in;
end
else
begin
if(sec_reg == 7'h59)
begin
casex(min_reg)
7'h59:
begin
min_reg <= 7'h0;
end
7'h?9:
begin
min_reg <= {(min_reg[6:4]+3'd1),4'h0};
end
default:
begin
min_reg <= min_reg + 7'd1;
end
endcase
end
end
end
//handle hours
always@(posedge clk_1hz or posedge time_ow)
begin
if(time_ow)
begin
hour_reg <= hour_in;
end
else
begin
if((sec_reg == 7'h59)&(min_reg == 7'h59))
begin
casex(hour_reg)
6'h23:
begin
hour_reg <= 6'd0;
end
6'b0?1001: //09 & 19
begin
hour_reg <= {(hour_reg[5:4]+3'd1),4'd0};
end
default:
begin
hour_reg <= hour_reg + 6'd1;
end
endcase
end
end
end
endmodule