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

Subversion Repositories RISCMCU

[/] [RISCMCU/] [trunk/] [vhdl/] [v_timer.vhd] - Blame information for rev 8

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

Line No. Rev Author Line
1 8 yapzihe
----------------------------------------------------------------------------
2
----                                                                    ----
3
---- WISHBONE RISCMCU IP Core                                           ----
4
----                                                                    ----
5
---- This file is part of the RISCMCU project                           ----
6
---- http://www.opencores.org/projects/riscmcu/                         ----
7
----                                                                    ----
8
---- Description                                                        ----
9
---- Implementation of a RISC Microcontroller based on Atmel AVR        ----
10
---- AT90S1200 instruction set and features with Altera Flex10k20 FPGA. ----
11
----                                                                    ----
12
---- Author(s):                                                         ----
13
----    - Yap Zi He, yapzihe@hotmail.com                                ----
14
----                                                                    ----
15
----------------------------------------------------------------------------
16
----                                                                    ----
17
---- Copyright (C) 2001 Authors and OPENCORES.ORG                       ----
18
----                                                                    ----
19
---- This source file may be used and distributed without               ----
20
---- restriction provided that this copyright statement is not          ----
21
---- removed from the file and that any derivative work contains        ----
22
---- the original copyright notice and the associated disclaimer.       ----
23
----                                                                    ----
24
---- This source file is free software; you can redistribute it         ----
25
---- and/or modify it under the terms of the GNU Lesser General         ----
26
---- Public License as published by the Free Software Foundation;       ----
27
---- either version 2.1 of the License, or (at your option) any         ----
28
---- later version.                                                     ----
29
----                                                                    ----
30
---- This source is distributed in the hope that it will be             ----
31
---- useful, but WITHOUT ANY WARRANTY; without even the implied         ----
32
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ----
33
---- PURPOSE. See the GNU Lesser General Public License for more        ----
34
---- details.                                                           ----
35
----                                                                    ----
36
---- You should have received a copy of the GNU Lesser General          ----
37
---- Public License along with this source; if not, download it         ----
38
---- from http://www.opencores.org/lgpl.shtml                           ----
39
----                                                                    ----
40
----------------------------------------------------------------------------
41
 
42
library ieee;
43
use ieee.std_logic_1164.all;
44
use ieee.std_logic_arith.all;
45
use ieee.std_logic_unsigned.all;
46
 
47
entity v_timer is
48
 port(  extpin, clr_tov0 : in std_logic;
49
                rd_timsk, wr_timsk, rd_tifr, wr_tifr : in std_logic;
50
                rd_tccr0, wr_tccr0, rd_tcnt0, wr_tcnt0 : in std_logic;
51
                clk, clrn : in std_logic;
52
                c : inout std_logic_vector(7 downto 0);
53
                timerirq : out std_logic
54
 );
55
end v_timer;
56
 
57
architecture timer of v_timer is
58
 
59
signal toie0, tov0 : std_logic;
60
signal cs : integer range 0 to 7;
61
signal tcnt0 : std_logic_vector(7 downto 0);
62
signal div1, div2, div4, div8, div16, div32, div64, div128, div256, div512, div1024 : std_logic;
63
signal timerclk, inc_tcnt0, currentstate, laststate : std_logic;
64
 
65
begin
66
 
67
-- Timer Interrupt Request
68
timerirq <= toie0 and tov0;
69
 
70
-- Read 4 Registers
71
c <=    "000000" & toie0 & "0"           when rd_timsk = '1' else
72
                "000000" & tov0 & "0"            when rd_tifr  = '1' else
73
                conv_std_logic_vector(cs,8) when rd_tccr0 = '1' else
74
                tcnt0                                           when rd_tcnt0 = '1' else
75
                "ZZZZZZZZ";
76
 
77
-- Select Clock Source
78
with cs select
79
        timerclk <=     '0'                      when 0,
80
                                clk                     when 1,
81
                                div8            when 2,
82
                                div64           when 3,
83
                                div256          when 4,
84
                                div1024         when 5,
85
                                not extpin      when 6,
86
                                extpin          when 7;
87
 
88
-- Timer : clear/write 4 registers, increment timer, set overflow flag, sample clock source
89
process(clrn, clr_tov0, wr_tifr, c, clk)
90
begin
91
        if clrn = '0' then
92
                toie0 <= '0';
93
                cs <= 0;
94
                tcnt0 <= "00000000";
95
                tov0 <= '0';
96
                currentstate <= '0';
97
                laststate <= '0';
98
 
99
        elsif clr_tov0 = '1' or (wr_tifr = '1' and c(1) = '1') then
100
                tov0 <= '0';
101
 
102
        elsif clk'event and clk = '1' then
103
 
104
                if wr_tcnt0 = '1' then
105
                        tcnt0 <= c;
106
                elsif inc_tcnt0 = '1' then
107
                        tcnt0 <= tcnt0 + 1;
108
                        if tcnt0 = "11111111" then
109
                                tov0 <= '1';
110
                        end if;
111
                end if;
112
 
113
                if wr_timsk = '1' then
114
                        toie0 <= c(1);
115
                end if;
116
                if wr_tccr0 = '1' then
117
                        cs <= conv_integer(c(2 downto 0));
118
                end if;
119
 
120
                currentstate <= timerclk;
121
                laststate <= currentstate;
122
 
123
        end if;
124
end process;
125
 
126
-- Detect rising edge
127
inc_tcnt0 <=    '1' when (laststate ='0' and currentstate = '1') or cs = 1 else
128
                                '0';
129
 
130
-- 10 bit prescaler
131
process(clk, clrn)
132
begin
133
        if clrn = '0' then
134
                div2 <= '0';
135
                div4 <= '0';
136
                div8 <= '0';
137
                div16 <= '0';
138
                div32 <= '0';
139
                div64 <= '0';
140
                div128 <= '0';
141
                div256 <= '0';
142
                div512 <= '0';
143
                div1024 <= '0';
144
 
145
        elsif clk'event and clk = '1' then
146
                div2 <= not div2;
147
                if div2 = '1' then
148
                        div4 <= not div4;
149
                        if div4 = '1' then
150
                                div8 <= not div8;
151
                                if div8 = '1' then
152
                                        div16 <= not div16;
153
                                        if div16 = '1' then
154
                                                div32 <= not div32;
155
                                                if div32 = '1' then
156
                                                        div64 <= not div64;
157
                                                        if div64 = '1' then
158
                                                                div128 <= not div128;
159
                                                                if div128 = '1' then
160
                                                                        div256 <= not div256;
161
                                                                        if div256 = '1' then
162
                                                                                div512 <= not div512;
163
                                                                                if div512 = '1' then
164
                                                                                        div1024 <= not div1024;
165
                                                                                end if;
166
                                                                        end if;
167
                                                                end if;
168
                                                        end if;
169
                                                end if;
170
                                        end if;
171
                                end if;
172
                        end if;
173
                end if;
174
        end if;
175
end process;
176
 
177
end timer;

powered by: WebSVN 2.1.0

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