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

Subversion Repositories 8051

[/] [8051/] [tags/] [rel_12/] [rtl/] [verilog/] [oc8051_tc.v] - Blame information for rev 112

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

Line No. Rev Author Line
1 82 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 112 simont
// Revision 1.5  2003/01/13 14:14:41  simont
48
// replace some modules
49
//
50 82 simont
// Revision 1.4  2002/09/30 17:33:59  simont
51
// prepared header
52
//
53
//
54
 
55
`include "oc8051_defines.v"
56
 
57
//synopsys translate_off
58
`include "oc8051_timescale.v"
59
//synopsys translate_on
60
 
61
 
62
 
63 112 simont
module oc8051_tc (clk, rst,
64
            wr_addr, rd_addr,
65
            data_in, data_out,
66
            wr, wr_bit,
67
            ie0, ie1,
68
            tr0, tr1,
69
            t0, t1,
70
            tf0, tf1,
71
            pres_ow);
72 82 simont
 
73 112 simont
input [7:0]  wr_addr,
74
             data_in,
75
             rd_addr;
76
input        clk,
77
             rst,
78
             wr,
79
             wr_bit,
80
             ie0,
81
             ie1,
82
             tr0,
83
             tr1,
84
             t0,
85
             t1;
86 82 simont
output [7:0] data_out;
87 112 simont
output       tf0,
88
             tf1,
89
             pres_ow;
90 82 simont
 
91 112 simont
 
92 82 simont
reg [7:0] tmod, tl0, th0, tl1, th1, data_out;
93
reg tf0, tf1_0, tf1_1, t0_buff, t1_buff;
94
 
95 112 simont
reg pres_ow;
96
reg [3:0] prescaler;
97
 
98 82 simont
wire tc0_add, tc1_add;
99
 
100 112 simont
assign tc0_add = (tr0 & (!tmod[3] | !ie0) & ((!tmod[2] & pres_ow) | (tmod[2] & !t0 & t0_buff)));
101
assign tc1_add = (tr1 & (!tmod[7] | !ie1) & ((!tmod[6] & pres_ow) | (tmod[6] & !t1 & t1_buff)));
102 82 simont
assign tf1= tf1_0 | tf1_1;
103
 
104
//
105
// read or write from one of the addresses in tmod
106
//
107
always @(posedge clk or posedge rst)
108
begin
109
 if (rst) begin
110
   tmod <=#1 `OC8051_RST_TMOD;
111
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TMOD))
112
    tmod <= #1 data_in;
113
end
114
 
115
//
116
// TIMER COUNTER 0
117
//
118
always @(posedge clk or posedge rst)
119
begin
120
 if (rst) begin
121
   tl0 <=#1 `OC8051_RST_TL0;
122
   th0 <=#1 `OC8051_RST_TH0;
123
   tf0 <= #1 1'b0;
124
   tf1_0 <= #1 1'b0;
125
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TL0)) begin
126
   tl0 <= #1 data_in;
127
   tf0 <= #1 1'b0;
128
   tf1_0 <= #1 1'b0;
129
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TH0)) begin
130
   th0 <= #1 data_in;
131
   tf0 <= #1 1'b0;
132
   tf1_0 <= #1 1'b0;
133
 end else begin
134
     case (tmod[1:0])
135
      `OC8051_MODE0: begin                       // mode 0
136
        tf1_0 <= #1 1'b0;
137
        if (tc0_add)
138
          {tf0, th0,tl0[4:0]} <= #1 {1'b0, th0, tl0[4:0]}+ 1'b1;
139
      end
140
      `OC8051_MODE1: begin                       // mode 1
141
        tf1_0 <= #1 1'b0;
142
        if (tc0_add)
143
          {tf0, th0,tl0} <= #1 {1'b0, th0, tl0}+ 1'b1;
144
      end
145
 
146
      `OC8051_MODE2: begin                       // mode 2
147
        tf1_0 <= #1 1'b0;
148
        if (tc0_add) begin
149
          if (tl0 == 8'b1111_1111) begin
150
            tf0 <=#1 1'b1;
151
            tl0 <=#1 th0;
152
           end
153
          else begin
154
            tl0 <=#1 tl0 + 8'h1;
155
            tf0 <= #1 1'b0;
156
          end
157
        end
158
      end
159
      `OC8051_MODE3: begin                       // mode 3
160
 
161
         if (tc0_add)
162
           {tf0, tl0} <= #1 {1'b0, tl0} +1'b1;
163
 
164 112 simont
         if (tr1 & pres_ow)
165 82 simont
           {tf1_0, th0} <= #1 {1'b0, th0} +1'b1;
166
 
167
      end
168
      default:begin
169
        tf0 <= #1 1'b0;
170
        tf1_0 <= #1 1'b0;
171
      end
172
    endcase
173
 end
174
end
175
 
176
//
177
// TIMER COUNTER 1
178
//
179
always @(posedge clk or posedge rst)
180
begin
181
 if (rst) begin
182
   tl1 <=#1 `OC8051_RST_TL1;
183
   th1 <=#1 `OC8051_RST_TH1;
184
   tf1_1 <= #1 1'b0;
185
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TL1)) begin
186
   tl1 <= #1 data_in;
187
   tf1_1 <= #1 1'b0;
188
 end else if ((wr) & !(wr_bit) & (wr_addr==`OC8051_SFR_TH1)) begin
189
   th1 <= #1 data_in;
190
   tf1_1 <= #1 1'b0;
191
 end else begin
192
     case (tmod[5:4])
193
      `OC8051_MODE0: begin                       // mode 0
194
        if (tc1_add)
195
          {tf1_1, th1,tl1[4:0]} <= #1 {1'b0, th1, tl1[4:0]}+ 1'b1;
196
      end
197
      `OC8051_MODE1: begin                       // mode 1
198
        if (tc1_add)
199
          {tf1_1, th1,tl1} <= #1 {1'b0, th1, tl1}+ 1'b1;
200
      end
201
 
202
      `OC8051_MODE2: begin                       // mode 2
203
        if (tc1_add) begin
204
          if (tl1 == 8'b1111_1111) begin
205
            tf1_1 <=#1 1'b1;
206
            tl1 <=#1 th1;
207
           end
208
          else begin
209
            tl1 <=#1 tl1 + 8'h1;
210
            tf1_1 <= #1 1'b0;
211
          end
212
        end
213
      end
214
      default:begin
215
        tf1_1 <= #1 1'b0;
216
      end
217
    endcase
218
 end
219
end
220
 
221
always @(posedge clk or posedge rst)
222
begin
223
  if (rst) data_out <= #1 8'h0;
224
  else if (wr & !wr_bit & (wr_addr==rd_addr) & ((wr_addr==`OC8051_SFR_TH0) |
225
     (wr_addr==`OC8051_SFR_TH1)|(wr_addr==`OC8051_SFR_TL0)|(wr_addr==`OC8051_SFR_TL1)|
226
     (wr_addr==`OC8051_SFR_TMOD))) begin
227
    data_out <= #1 data_in;
228
  end else begin
229
    case (rd_addr)
230
      `OC8051_SFR_TH0: data_out <= #1 th0;
231
      `OC8051_SFR_TH1: data_out <= #1 th1;
232
      `OC8051_SFR_TL0: data_out <= #1 tl0;
233
      `OC8051_SFR_TL1: data_out <= #1 tl1;
234
      default: data_out <= #1 tmod;
235
    endcase
236
  end
237
end
238
 
239 112 simont
always @(posedge clk or posedge rst)
240
begin
241
  if (rst) begin
242
    prescaler <= #1 4'h0;
243
    pres_ow <= #1 1'b0;
244
  end else if (prescaler==4'b1011) begin
245
    prescaler <= #1 4'h0;
246
    pres_ow <= #1 1'b1;
247
  end else begin
248
    prescaler <= #1 prescaler + 4'h1;
249
    pres_ow <= #1 1'b0;
250
  end
251
end
252 82 simont
 
253
always @(posedge clk or posedge rst)
254
  if (rst) begin
255
    t0_buff <= #1 1'b0;
256
    t1_buff <= #1 1'b0;
257
  end else begin
258
    t0_buff <= #1 t0;
259
    t1_buff <= #1 t1;
260
  end
261
endmodule

powered by: WebSVN 2.1.0

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