2020-03-08 07:07:17 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 Blue Liang, liangkangnan@163.com
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-03-29 15:19:14 +00:00
|
|
|
`include "defines.v"
|
|
|
|
|
2020-03-08 07:07:17 +00:00
|
|
|
|
|
|
|
// 32 bits count up timer module
|
2020-03-29 15:19:14 +00:00
|
|
|
module timer(
|
2020-03-08 07:07:17 +00:00
|
|
|
|
|
|
|
input wire clk,
|
|
|
|
input wire rst,
|
|
|
|
|
2020-03-29 15:19:14 +00:00
|
|
|
input wire[31:0] data_i,
|
|
|
|
input wire[31:0] addr_i,
|
|
|
|
input wire we_i,
|
|
|
|
input wire req_i,
|
2020-03-08 07:07:17 +00:00
|
|
|
|
2020-03-29 15:19:14 +00:00
|
|
|
output reg[31:0] data_o,
|
2020-04-18 03:37:22 +00:00
|
|
|
output reg int_sig_o,
|
2020-03-29 15:19:14 +00:00
|
|
|
output reg ack_o
|
2020-03-08 07:07:17 +00:00
|
|
|
|
|
|
|
);
|
|
|
|
|
2020-04-11 11:03:49 +00:00
|
|
|
localparam REG_CTRL = 4'h0;
|
|
|
|
localparam REG_COUNT = 4'h4;
|
|
|
|
localparam REG_VALUE = 4'h8;
|
2020-03-08 07:07:17 +00:00
|
|
|
|
|
|
|
// [0]: timer enable
|
|
|
|
// [1]: timer int enable
|
|
|
|
// [2]: timer int pending, write 1 to clear it
|
2020-03-29 15:19:14 +00:00
|
|
|
// addr offset: 0x00
|
2020-03-08 07:07:17 +00:00
|
|
|
reg[31:0] timer_ctrl;
|
|
|
|
|
2020-03-29 15:19:14 +00:00
|
|
|
// timer current count, read only
|
|
|
|
// addr offset: 0x04
|
|
|
|
reg[31:0] timer_count;
|
|
|
|
|
|
|
|
// timer expired value
|
|
|
|
// addr offset: 0x08
|
|
|
|
reg[31:0] timer_value;
|
|
|
|
|
|
|
|
|
2020-04-18 03:37:22 +00:00
|
|
|
// counter
|
2020-03-08 07:07:17 +00:00
|
|
|
always @ (posedge clk) begin
|
2020-03-29 15:19:14 +00:00
|
|
|
if (rst == `RstEnable) begin
|
2020-04-18 03:37:22 +00:00
|
|
|
timer_count <= `ZeroWord;
|
2020-03-08 07:07:17 +00:00
|
|
|
end else begin
|
2020-04-18 03:37:22 +00:00
|
|
|
if (timer_ctrl[0] == 1'b1 && timer_value > 32'h0) begin
|
|
|
|
timer_count <= timer_count + 1'b1;
|
2020-04-11 11:03:49 +00:00
|
|
|
end else begin
|
2020-04-18 03:37:22 +00:00
|
|
|
timer_count <= `ZeroWord;
|
2020-04-11 11:03:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-18 03:37:22 +00:00
|
|
|
// int signal
|
2020-04-11 11:03:49 +00:00
|
|
|
always @ (posedge clk) begin
|
|
|
|
if (rst == `RstEnable) begin
|
2020-04-18 03:37:22 +00:00
|
|
|
int_sig_o <= `INT_DEASSERT;
|
2020-04-11 11:03:49 +00:00
|
|
|
end else begin
|
2020-04-18 03:37:22 +00:00
|
|
|
if (timer_count >= timer_value && timer_value > 32'h0) begin
|
|
|
|
int_sig_o <= `INT_ASSERT;
|
|
|
|
end else if (we_i == `WriteEnable && addr_i[3:0] == REG_CTRL && timer_ctrl[2] == 1'b1) begin
|
|
|
|
int_sig_o <= `INT_DEASSERT;
|
2020-04-11 11:03:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-18 03:37:22 +00:00
|
|
|
// write regs
|
2020-04-11 11:03:49 +00:00
|
|
|
always @ (posedge clk) begin
|
|
|
|
if (rst == `RstEnable) begin
|
2020-04-18 03:37:22 +00:00
|
|
|
timer_ctrl <= `ZeroWord;
|
|
|
|
timer_value <= `ZeroWord;
|
2020-04-11 11:03:49 +00:00
|
|
|
end else begin
|
2020-04-18 03:37:22 +00:00
|
|
|
if (we_i == `WriteEnable) begin
|
|
|
|
case (addr_i[3:0])
|
|
|
|
REG_CTRL: begin
|
|
|
|
timer_ctrl <= data_i;
|
|
|
|
end
|
|
|
|
REG_VALUE: begin
|
|
|
|
timer_value <= data_i;
|
|
|
|
end
|
|
|
|
endcase
|
2020-04-11 11:03:49 +00:00
|
|
|
end else begin
|
2020-04-18 03:37:22 +00:00
|
|
|
if (timer_count >= timer_value && timer_value > 32'h0) begin
|
|
|
|
timer_ctrl[0] <= 1'b0;
|
|
|
|
end
|
2020-03-08 07:07:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-18 03:37:22 +00:00
|
|
|
// read regs
|
2020-03-08 07:07:17 +00:00
|
|
|
always @ (*) begin
|
2020-03-29 15:19:14 +00:00
|
|
|
if (rst == `RstEnable) begin
|
|
|
|
data_o <= `ZeroWord;
|
2020-03-08 07:07:17 +00:00
|
|
|
end else begin
|
2020-04-11 11:03:49 +00:00
|
|
|
case (addr_i[3:0])
|
|
|
|
REG_VALUE: begin
|
2020-03-29 15:19:14 +00:00
|
|
|
data_o <= timer_value;
|
|
|
|
end
|
2020-04-11 11:03:49 +00:00
|
|
|
REG_CTRL: begin
|
2020-03-29 15:19:14 +00:00
|
|
|
data_o <= timer_ctrl;
|
|
|
|
end
|
2020-04-11 11:03:49 +00:00
|
|
|
REG_COUNT: begin
|
2020-03-29 15:19:14 +00:00
|
|
|
data_o <= timer_count;
|
|
|
|
end
|
|
|
|
default: begin
|
|
|
|
data_o <= `ZeroWord;
|
|
|
|
end
|
|
|
|
endcase
|
2020-03-08 07:07:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|