OpenCores
URL https://opencores.org/ocsvn/8051/8051/trunk

Subversion Repositories 8051

[/] [8051/] [trunk/] [rtl/] [verilog/] [oc8051_tc.v] - Blame information for rev 46

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 46 simont
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  8051 cores timer/counter control                            ////
4
////                                                              ////
5
////  This file is part of the 8051 cores project                 ////
6
////  http://www.opencores.org/cores/8051/                        ////
7
////                                                              ////
8
////  Description                                                 ////
9
////   timers and counters handling for 8051 core                 ////
10
////                                                              ////
11
////  To Do:                                                      ////
12
////   Nothing                                                    ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Simon Teran, simont@opencores.org                     ////
16
////                                                              ////
17
//////////////////////////////////////////////////////////////////////
18
////                                                              ////
19
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
20
////                                                              ////
21
//// This source file may be used and distributed without         ////
22
//// restriction provided that this copyright statement is not    ////
23
//// removed from the file and that any derivative work contains  ////
24
//// the original copyright notice and the associated disclaimer. ////
25
////                                                              ////
26
//// This source file is free software; you can redistribute it   ////
27
//// and/or modify it under the terms of the GNU Lesser General   ////
28
//// Public License as published by the Free Software Foundation; ////
29
//// either version 2.1 of the License, or (at your option) any   ////
30
//// later version.                                               ////
31
////                                                              ////
32
//// This source is distributed in the hope that it will be       ////
33
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
34
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
35
//// PURPOSE.  See the GNU Lesser General Public License for more ////
36
//// details.                                                     ////
37
////                                                              ////
38
//// You should have received a copy of the GNU Lesser General    ////
39
//// Public License along with this source; if not, download it   ////
40
//// from http://www.opencores.org/lgpl.shtml                     ////
41
////                                                              ////
42
//////////////////////////////////////////////////////////////////////
43
//
44
// CVS Revision History
45
//
46
// $Log: not supported by cvs2svn $
47
//
48
 
49
`include "oc8051_defines.v"
50
 
51
//synopsys translate_off
52
`include "oc8051_timescale.v"
53
//synopsys translate_on
54
 
55
 
56
 
57
module oc8051_tc (clk, rst, wr_addr, rd_addr, data_in, wr, wr_bit, ie0, ie1, tr0, tr1, t0, t1, data_out,
58
            tf0, tf1);
59
input [7:0] wr_addr, data_in, rd_addr;
60
input clk, rst, wr, wr_bit, ie0, ie1, tr0, tr1, t0, t1;
61
output [7:0] data_out;
62
output tf0, tf1;
63
reg [7:0] tmod, tl0, th0, tl1, th1, data_out;
64
reg tf0, tf1_0, tf1_1, t0_buff, t1_buff;
65
 
66
wire tc0_add, tc1_add;
67
 
68
assign tc0_add = (tr0 & (!tmod[3] | !ie0) & (!(tmod[2]) | (tmod[2] & !t0 & t0_buff)));
69
assign tc1_add = (tr1 & (!tmod[7] | !ie1) & (!(tmod[6]) | (tmod[6] & !t1 & t1_buff)));
70
assign tf1= tf1_0 | tf1_1;
71
 
72
//
73
// read or write from one of the addresses in tmod
74
//
75
always @(posedge clk or posedge rst)
76
begin
77
 if (rst) begin
78
   tmod <=#1 `OC8051_RST_TMOD;
79
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TMOD))
80
    tmod <= #1 data_in;
81
end
82
 
83
//
84
// TIMER COUNTER 0
85
//
86
always @(posedge clk or posedge rst)
87
begin
88
 if (rst) begin
89
   tl0 <=#1 `OC8051_RST_TL0;
90
   th0 <=#1 `OC8051_RST_TH0;
91
   tf0 <= #1 1'b0;
92
   tf1_0 <= #1 1'b0;
93
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TL0)) begin
94
   tl0 <= #1 data_in;
95
   tf0 <= #1 1'b0;
96
   tf1_0 <= #1 1'b0;
97
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TH0)) begin
98
   th0 <= #1 data_in;
99
   tf0 <= #1 1'b0;
100
   tf1_0 <= #1 1'b0;
101
 end else begin
102
     case (tmod[1:0])
103
      `OC8051_MODE0: begin                       // mode 0
104
        tf1_0 <= #1 1'b0;
105
        if (tc0_add)
106
          {tf0, th0,tl0[4:0]} <= #1 {1'b0, th0, tl0[4:0]}+ 1'b1;
107
      end
108
      `OC8051_MODE1: begin                       // mode 1
109
        tf1_0 <= #1 1'b0;
110
        if (tc0_add)
111
          {tf0, th0,tl0} <= #1 {1'b0, th0, tl0}+ 1'b1;
112
      end
113
 
114
      `OC8051_MODE2: begin                       // mode 2
115
        tf1_0 <= #1 1'b0;
116
        if (tc0_add) begin
117
          if (tl0 == 8'b1111_1111) begin
118
            tf0 <=#1 1'b1;
119
            tl0 <=#1 th0;
120
           end
121
          else begin
122
            tl0 <=#1 tl0 + 8'h1;
123
            tf0 <= #1 1'b0;
124
          end
125
        end
126
      end
127
      `OC8051_MODE3: begin                       // mode 3
128
 
129
         if (tc0_add)
130
           {tf0, tl0} <= #1 {1'b0, tl0} +1'b1;
131
 
132
         if (tr1)
133
           {tf1_0, th0} <= #1 {1'b0, th0} +1'b1;
134
 
135
      end
136
      default:begin
137
        tf0 <= #1 1'b0;
138
        tf1_0 <= #1 1'b0;
139
      end
140
    endcase
141
 end
142
end
143
 
144
//
145
// TIMER COUNTER 1
146
//
147
always @(posedge clk or posedge rst)
148
begin
149
 if (rst) begin
150
   tl1 <=#1 `OC8051_RST_TL1;
151
   th1 <=#1 `OC8051_RST_TH1;
152
   tf1_1 <= #1 1'b0;
153
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TL1)) begin
154
   tl1 <= #1 data_in;
155
   tf1_1 <= #1 1'b0;
156
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TH1)) begin
157
   th1 <= #1 data_in;
158
   tf1_1 <= #1 1'b0;
159
 end else begin
160
     case (tmod[5:4])
161
      `OC8051_MODE0: begin                       // mode 0
162
        if (tc1_add)
163
          {tf1_1, th1,tl1[4:0]} <= #1 {1'b0, th1, tl1[4:0]}+ 1'b1;
164
      end
165
      `OC8051_MODE1: begin                       // mode 1
166
        if (tc1_add)
167
          {tf1_1, th1,tl1} <= #1 {1'b0, th1, tl1}+ 1'b1;
168
      end
169
 
170
      `OC8051_MODE2: begin                       // mode 2
171
        if (tc1_add) begin
172
          if (tl1 == 8'b1111_1111) begin
173
            tf1_1 <=#1 1'b1;
174
            tl1 <=#1 th1;
175
           end
176
          else begin
177
            tl1 <=#1 tl1 + 8'h1;
178
            tf1_1 <= #1 1'b0;
179
          end
180
        end
181
      end
182
      default:begin
183
        tf1_1 <= #1 1'b0;
184
      end
185
    endcase
186
 end
187
end
188
 
189
always @(posedge clk or posedge rst)
190
begin
191
  if (rst) data_out <= #1 8'h0;
192
  else if (wr & !wr_bit & (wr_addr==rd_addr) & ((wr_addr==`OC8051_SFR_TH0) |
193
     (wr_addr==`OC8051_SFR_TH1)|(wr_addr==`OC8051_SFR_TL0)|(wr_addr==`OC8051_SFR_TL1)|
194
     (wr_addr==`OC8051_SFR_TMOD))) begin
195
    data_out <= #1 data_in;
196
  end else begin
197
    case (rd_addr)
198
      `OC8051_SFR_TH0: data_out <= #1 th0;
199
      `OC8051_SFR_TH1: data_out <= #1 th1;
200
      `OC8051_SFR_TL0: data_out <= #1 tl0;
201
      `OC8051_SFR_TL1: data_out <= #1 tl1;
202
      default: data_out <= #1 tmod;
203
    endcase
204
  end
205
end
206
 
207
 
208
always @(posedge clk or posedge rst)
209
  if (rst) begin
210
    t0_buff <= #1 1'b0;
211
    t1_buff <= #1 1'b0;
212
  end else begin
213
    t0_buff <= #1 t0;
214
    t1_buff <= #1 t1;
215
  end
216
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.