forked from hoglet67/verilog-6502
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTIMER.v
82 lines (69 loc) · 1.68 KB
/
TIMER.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
`default_nettype none
/*
* HuC6280 timer
*/
module TIMER(input wire clk, reset,
input wire re, we,
input wire clk_en,
input wire CET_n,
input wire addr,
input wire TIQ_ack,
input wire [7:0] dIn,
output wire [7:0] dOut,
output reg TIQ_n);
wire restart;
reg timer_en;
reg [9:0] div;
reg [6:0] counter, reset_val;
//HIGH when timer should be restarted
assign restart = (~CET_n & we & addr & dIn[0] & ~timer_en);
//MMIO read
assign dOut = (~CET_n & re & ~addr) ? counter : 0;
//MMIO write
always @(posedge clk) begin
if(reset)
timer_en <= 0;
else if(clk_en) begin
if(~CET_n & we) begin
if(~addr) //counter
reset_val <= dIn[6:0];
else //control
timer_en <= dIn[0];
end
end
end
always @(posedge clk) begin
if(reset)
div <= 1023;
else if(clk_en) begin
if(restart) //timer is being started
div <= 1023;
else if(timer_en)
div <= div - 1;
end
end
always @(posedge clk) begin
if(reset)
counter <= 0;
else if(clk_en) begin
if(timer_en && div == 0) begin
if(counter != 0)
counter <= counter - 1;
else
counter <= reset_val;
end
else if(restart)
counter <= reset_val;
end
end
always @(posedge clk) begin
if(reset)
TIQ_n <= 1;
else if(clk_en) begin
if(timer_en && div == 0 && counter == 0)
TIQ_n <= 0;
else if(TIQ_ack)
TIQ_n <= 1;
end
end
endmodule