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

Subversion Repositories gpib_controller

[/] [gpib_controller/] [trunk/] [vhdl/] [src/] [gpib_helper/] [Fifo8b.vhd] - Blame information for rev 3

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

Line No. Rev Author Line
1 3 Andrewski
--------------------------------------------------------------------------------
2
-- Entity: Fifo8b
3
-- Date:2011-11-28  
4
-- Author: Administrator     
5
--
6
-- Description ${cursor}
7
--------------------------------------------------------------------------------
8
library ieee;
9
use ieee.std_logic_1164.all;
10
use ieee.std_logic_unsigned.all;
11
use ieee.std_logic_arith.all;
12
 
13
use work.utilPkg.all;
14
use work.helperComponents.all;
15
 
16
 
17
entity Fifo8b is
18
        generic (
19
                MAX_ADDR_BIT_NUM : integer := 10
20
        );
21
        port (
22
                reset : in std_logic;
23
                clk : in std_logic;
24
                -------------- fifo --------------------
25
                bytesAvailable : out std_logic;
26
                availableBytesCount : out std_logic_vector(MAX_ADDR_BIT_NUM downto 0);
27
                bufferFull : out std_logic;
28
                resetFifo : in std_logic;
29
                ----------------------------------------
30
                data_in : in std_logic_vector(7 downto 0);
31
                ready_to_write :out std_logic;
32
                strobe_write : in std_logic;
33
                ----------------------------------------
34
                data_out : out std_logic_vector(7 downto 0);
35
                ready_to_read : out std_logic;
36
                strobe_read : in std_logic
37
        );
38
end Fifo8b;
39
 
40
architecture arch of Fifo8b is
41
 
42
        constant ADDR_BITS_COUNT : integer := MAX_ADDR_BIT_NUM + 1;
43
        constant MEMORY_CELLS_COUNT : integer := 2**ADDR_BITS_COUNT;
44
        constant MAX_DATA_LENGTH : integer := MEMORY_CELLS_COUNT - 1;
45
        constant MAX_ADDR : integer := MAX_DATA_LENGTH;
46
 
47
        -------------- memory ----------------
48
        signal n_clk : std_logic;
49
        signal p1_addr : std_logic_vector(MAX_ADDR_BIT_NUM downto 0);
50
        signal p1_data_in : std_logic_vector(7 downto 0);
51
        signal p1_strobe : std_logic;
52
        signal p1_data_out : std_logic_vector(7 downto 0);
53
        -------------------------------------------------
54
        signal p2_addr : std_logic_vector(MAX_ADDR_BIT_NUM downto 0);
55
        signal p2_data_in : std_logic_vector(7 downto 0);
56
        signal p2_strobe : std_logic;
57
        signal p2_data_out : std_logic_vector(7 downto 0);
58
 
59
        ------------- fifo --------------------
60
        signal writeAddr : integer range 0 to MAX_ADDR;
61
        signal readAddr : integer range 0 to MAX_ADDR;
62
        signal readAddrValid : std_logic;
63
        signal currentDataLen : integer range 0 to MAX_DATA_LENGTH;
64
 
65
        -------- control ----------------------
66
        signal ss_r, sr_r, ss_w, sr_w : std_logic;
67
 
68
 
69
begin
70
 
71
        n_clk <= not clk;
72
 
73
        p2_strobe <= '0';
74
 
75
        ready_to_write <= to_stdl((ss_w = sr_w) and currentDataLen < MAX_DATA_LENGTH);
76
        ready_to_read <= to_stdl((ss_r = sr_r) and currentDataLen > 0);
77
 
78
        bytesAvailable <= to_stdl(currentDataLen > 0);
79
        availableBytesCount <= conv_std_logic_vector(currentDataLen, ADDR_BITS_COUNT);
80
 
81
        p1_data_in <= data_in;
82
        data_out <= p2_data_out;
83
 
84
        bufferFull <= to_stdl(currentDataLen = MAX_DATA_LENGTH);
85
 
86
        p1_addr <= conv_std_logic_vector(writeAddr, ADDR_BITS_COUNT);
87
        p2_addr <= conv_std_logic_vector(readAddr, ADDR_BITS_COUNT);
88
 
89
 
90
        process (reset, clk) begin
91
                if reset = '1' then
92
                        writeAddr <= 1;
93
                        readAddr <= 0;
94
                        readAddrValid <= '0';
95
 
96
                        sr_w <= '0';
97
                        sr_r <= '0';
98
 
99
                        p1_strobe <= '0';
100
                elsif rising_edge(clk) then
101
                        if resetFifo = '1' then
102
                                writeAddr <= 1;
103
                                readAddr <= 0;
104
                                readAddrValid <= '0';
105
 
106
                                sr_w <= ss_w;
107
                                sr_r <= ss_r;
108
 
109
                                p1_strobe <= '0';
110
                        else
111
                                if sr_w /= ss_w and currentDataLen < MAX_DATA_LENGTH and
112
                                                p1_strobe = '0' then
113
                                        p1_strobe <= '1';
114
                                elsif sr_w /= ss_w and currentDataLen < MAX_DATA_LENGTH and
115
                                                p1_strobe = '1' then
116
                                        p1_strobe <= '0';
117
                                        sr_w <= ss_w;
118
 
119
                                        if writeAddr < MAX_ADDR then
120
                                                writeAddr <= writeAddr + 1;
121
                                        else
122
                                                writeAddr <= 0;
123
                                        end if;
124
 
125
                                        if readAddrValid = '0' then
126
                                                if readAddr < MAX_ADDR then
127
                                                        readAddr <= readAddr + 1;
128
                                                else
129
                                                        readAddr <= 0;
130
                                                end if;
131
 
132
                                                readAddrValid <= '1';
133
                                        end if;
134
                                end if;
135
 
136
                                if sr_r /= ss_r and currentDataLen > 0 and
137
                                                readAddrValid = '1' then
138
                                        sr_r <= ss_r;
139
 
140
                                        if currentDataLen = 1 and
141
                                                -- and last writing phase is not ongoing
142
                                                not(sr_w /= ss_w and p1_strobe = '1') then
143
                                                        -- if writing is not ongoing
144
                                                        readAddrValid <= '0';
145
                                        else
146
                                                if readAddr < MAX_ADDR then
147
                                                        readAddr <= readAddr + 1;
148
                                                else
149
                                                        readAddr <= 0;
150
                                                end if;
151
                                        end if;
152
                                end if;
153
                        end if;
154
                end if;
155
        end process;
156
 
157
        -- calculate current length
158
        process(writeAddr, readAddr, readAddrValid) begin
159
                if readAddrValid = '0' then
160
                        currentDataLen <= 0;
161
                elsif readAddr < writeAddr then
162
                        currentDataLen <= writeAddr - readAddr;
163
                else -- readAddr > writeAddr, readAddr = writeAddr shoud never happen
164
                        currentDataLen <= (MEMORY_CELLS_COUNT - readAddr) + writeAddr;
165
                end if;
166
        end process;
167
 
168
        -- subscribe write
169
        process (reset, strobe_write) begin
170
                if reset = '1' then
171
                        ss_w <= '0';
172
                elsif rising_edge(strobe_write) then
173
                        if ss_w = sr_w then
174
                                ss_w <= not sr_w;
175
                        end if;
176
                end if;
177
        end process;
178
 
179
        -- subscribe read
180
        process (reset, strobe_read) begin
181
                if reset = '1' then
182
                        ss_r <= '0';
183
                elsif rising_edge(strobe_read) then
184
                        if ss_r = sr_r then
185
                                ss_r <= not sr_r;
186
                        end if;
187
                end if;
188
        end process;
189
 
190
        -- target memory
191
        mb: MemoryBlock port map (
192
                reset => reset,
193
                clk => n_clk,
194
                -------------------------------------------------
195
                p1_addr => p1_addr,
196
                p1_data_in => p1_data_in,
197
                p1_strobe => p1_strobe,
198
                p1_data_out => p1_data_out,
199
                -------------------------------------------------
200
                p2_addr => p2_addr,
201
                p2_data_in => p2_data_in,
202
                p2_strobe => p2_strobe,
203
                p2_data_out => p2_data_out
204
        );
205
 
206
end arch;
207
 

powered by: WebSVN 2.1.0

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