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

Subversion Repositories bw_tiff_compression

[/] [bw_tiff_compression/] [trunk/] [capture_manager.vhd] - Blame information for rev 6

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

Line No. Rev Author Line
1 2 amulder
----------------------------------------------------------------------------------
2
-- Company:        
3
-- Engineer:       Aart Mulder
4
-- 
5
-- Version:        V2.0
6
-- Create Date:    13:27:31 08/17/2012 
7
-- Design Name:    Tiff compression and transmission
8
-- Module Name:    capture_manager - Behavioral 
9
-- Project Name:   Tiff compression and transmission
10
----------------------------------------------------------------------------------
11
 
12
library IEEE;
13
use IEEE.STD_LOGIC_1164.ALL;
14
use IEEE.NUMERIC_STD.ALL;
15
 
16
entity capture_manager is
17
        Generic (
18
                COLUMNS_G                       : integer := 752;
19
                ROWS_G                          : integer := 480;
20
                COL_INDEX_WIDTH_G               : integer := 10; --Width to represent the column index
21
                ROW_INDEX_WIDTH_G               : integer := 9; --Width to represent the row index
22
 
23
                MAX_CODE_LEN_G                  : integer := 28;
24
                MAX_CODE_LEN_WIDTH_G            : integer := 5;
25
                SEG_OUTPUT_WIDTH_G              : integer := 8;
26
 
27
                TX_MEMORY_SIZE_G                : integer := 4;
28
                TX_MEMORY_ADDRESS_WIDTH_G       : integer := 12;
29
                TX_MEMORY_WIDTH_G               : integer := 8;
30
 
31
                --@26.6MHz
32
                BAUD_DIVIDE_G                   : integer := 15;        --115200 baud
33
                BAUD_RATE_G                     : integer := 231
34
        );
35
        Port
36
        (
37
                reset_i   : in  STD_LOGIC;
38
 
39
                fsync_i   : in  STD_LOGIC;
40
                rsync_i   : in  STD_LOGIC;
41
                pclk_i    : in  STD_LOGIC;
42
                pix_data_i: in  STD_LOGIC_VECTOR(7 downto 0);
43
 
44
                vga_fsync_o : out STD_LOGIC;
45
                vga_rsync_o : out STD_LOGIC;
46
                vgaRed      : out STD_LOGIC_VECTOR(7 downto 5);
47
                vgaGreen    : out STD_LOGIC_VECTOR(7 downto 5);
48
                vgaBlue     : out STD_LOGIC_VECTOR(7 downto 6);
49
 
50
                TX_o    : out STD_LOGIC;
51
                RX_i    : in STD_LOGIC;
52
 
53
                led0_o  : out STD_LOGIC;
54
                led1_o  : out STD_LOGIC;
55
                led2_o  : out STD_LOGIC;
56
                led3_o  : out STD_LOGIC;
57
 
58
                sw_i : in STD_LOGIC_VECTOR(6 downto 0)
59
        );
60
end capture_manager;
61
 
62
architecture Behavioral of capture_manager is
63
        type state_type is (S_Start, S_WaitForChar, S_WaitForNewFrame, S_CaptureStoreFrame,
64
                        S_SendSizeB1, S_SendSizeB1WaitRdy,
65
                        S_SendSizeB2, S_SendSizeB2WaitRdy,
66
                        S_SendStreamByte, S_SendStreamByteWaitAccepted, S_SendStreamByteWaitRdy,
67
                        S_Unknown);
68
 
69
        constant CHAR_CAP_S            : std_logic_vector(7 downto 0) := std_logic_vector(to_unsigned(83, 8));
70
--      constant CHAR_S                : std_logic_vector(7 downto 0) := std_logic_vector(to_unsigned(115, 8));
71
        constant START_NEW_FRAME_CHAR  : std_logic_vector(7 downto 0) := CHAR_CAP_S;
72
 
73
        constant ZERO_PADDING_C : std_logic_vector(15 downto 0) := (others => '0');
74
 
75
        function boolean2sl(x : boolean)
76
                        return std_logic is
77
        begin
78
                if x then
79
                        return '1';
80
                else
81
                        return '0';
82
                end if;
83
        end boolean2sl;
84
 
85
        function sl2boolean(x : std_logic)
86
                        return boolean is
87
        begin
88
                if x = '1' then
89
                        return TRUE;
90
                else
91
                        return FALSE;
92
                end if;
93
        end sl2boolean;
94
 
95
        --State machine signals
96
        signal state, state_next : state_type := S_Start;
97
 
98
        --Signals to connect the CCITT4 module
99
        signal run_len_code_CCITT4       : STD_LOGIC_VECTOR(MAX_CODE_LEN_G - 1 downto 0)       := (others => '0');
100
        signal run_len_code_width_CCITT4 : STD_LOGIC_VECTOR(MAX_CODE_LEN_WIDTH_G - 1 downto 0) := (others => '0');
101
        signal run_len_code_valid_CCITT4 : STD_LOGIC                                           := '0';
102
        signal frame_finished_CCITT4     : STD_LOGIC                                           := '0';
103
        signal pix                       : STD_LOGIC                                           := '0';
104
        signal fax4_x : unsigned(COL_INDEX_WIDTH_G-1 downto 0) := (others => '0');
105
        signal fax4_y : unsigned(ROW_INDEX_WIDTH_G-1 downto 0) := (others => '0');
106
 
107
        --Signals to connect the byte segmentation module
108
        signal seg_d1, seg_d2, seg_d3, seg_d4 : STD_LOGIC_VECTOR(SEG_OUTPUT_WIDTH_G-1 downto 0)  := (others => '0');
109
        signal seg_d_rdy1, seg_d_rdy2, seg_d_rdy3, seg_d_rdy4 : STD_LOGIC                        := '0';
110
        signal seg_frame_finished_in       : STD_LOGIC                                           := '0';
111
        signal seg_frame_finished_out      : STD_LOGIC                                           := '0';
112
        signal seg_reset                   : STD_LOGIC                                           := '0';
113
 
114
        --Signals to connect the UART module
115
        signal tx_data, rx_data                              : STD_LOGIC_VECTOR(7 downto 0) := (others => '0');
116
        signal rx_available, tx_buf_empty, tx_buf_empty_prev : STD_LOGIC                    := '0';
117
        signal rx_trigger, tx_trigger                        : STD_LOGIC                    := '0';
118
        signal uart_reset                                    : STD_LOGIC                    := '0';
119
 
120
        -- Tx memory
121
        signal tx_mem_reset         : STD_LOGIC                                              := '0';
122
        signal tx_mem_d_out         : STD_LOGIC_VECTOR(TX_MEMORY_WIDTH_G-1 downto 0)         := (others => '0');
123
        signal tx_mem_read_addr     : std_logic_vector(TX_MEMORY_ADDRESS_WIDTH_G-1 downto 0) := (others => '0');
124
        signal tx_mem_used          : unsigned(TX_MEMORY_ADDRESS_WIDTH_G-1 downto 0)         := (others => '0');
125
 
126
                --Other signals
127
        signal fsync_prev                    : STD_LOGIC             := '0';
128
        signal tx_mem_overflow               : std_logic             := '0';
129
 
130
        component CCITT4_v2
131
                port(
132
                        pclk_i               : in  STD_LOGIC;
133
                        fsync_i              : in  STD_LOGIC;
134
                        rsync_i              : in  STD_LOGIC;
135
                        pix_i                : in  STD_LOGIC;
136
                        run_len_code_o       : out STD_LOGIC_VECTOR(MAX_CODE_LEN_G - 1 downto 0);
137
                        run_len_code_width_o : out STD_LOGIC_VECTOR(MAX_CODE_LEN_WIDTH_G - 1 downto 0);
138
                        run_len_code_valid_o : out STD_LOGIC;
139
                        frame_finished_o     : out STD_LOGIC;
140
                        fax4_x               : buffer unsigned(COL_INDEX_WIDTH_G - 1 downto 0) := (others => '0');
141
                        fax4_y               : buffer unsigned(ROW_INDEX_WIDTH_G - 1 downto 0) := (others => '0')
142
                );
143
        end component CCITT4_v2;
144
 
145
begin
146
        CCITT4_ins : CCITT4_v2
147
        Port Map(
148
                pclk_i    => pclk_i,
149
                fsync_i  => fsync_i,
150
                rsync_i  => rsync_i,
151
                pix_i  => pix,
152
                run_len_code_o  => run_len_code_CCITT4,
153
                run_len_code_width_o  => run_len_code_width_CCITT4,
154
                run_len_code_valid_o  => run_len_code_valid_CCITT4,
155
                frame_finished_o  => frame_finished_CCITT4,
156
                fax4_x => fax4_x,
157
                fax4_y => fax4_y
158
        );
159
 
160
        byte_segmentation_ins_v5 : entity work.byte_segmentation_v5
161
        Generic Map(
162
                INPUT_WIDTH_G =>  MAX_CODE_LEN_G,
163
                OUTPUT_WIDTH_G => SEG_OUTPUT_WIDTH_G,
164
                INDEX_WIDTH_G =>  MAX_CODE_LEN_WIDTH_G
165
        )
166
        Port Map(
167
                reset_i => seg_reset,
168
                clk_i => pclk_i,
169
                pclk_i => pclk_i,
170
                d_i => run_len_code_CCITT4,
171
                d_width_i => run_len_code_width_CCITT4,
172
                d_rdy_i => run_len_code_valid_CCITT4,
173
                d1_o => seg_d1,
174
                d_rdy1_o => seg_d_rdy1,
175
                d2_o => seg_d2,
176
                d_rdy2_o => seg_d_rdy2,
177
                d3_o => seg_d3,
178
                d_rdy3_o => seg_d_rdy3,
179
                d4_o => seg_d4,
180
                d_rdy4_o => seg_d_rdy4,
181
                frame_finished_i => seg_frame_finished_in,
182
                frame_finished_o => seg_frame_finished_out
183
        );
184
        seg_frame_finished_in <= frame_finished_CCITT4;
185
 
186
        var_width_RAM_ins : entity work.var_width_RAM
187
        generic map(
188
                MEM_SIZE_G => TX_MEMORY_SIZE_G,
189
                MEM_INDEX_WIDTH_G => TX_MEMORY_ADDRESS_WIDTH_G,
190
                DATA_WIDTH_G  => TX_MEMORY_WIDTH_G
191
        )
192
        port map(
193
                reset_i   => tx_mem_reset,
194
                clk_i     => pclk_i,
195
                wr1_i     => seg_d_rdy1,
196
                d1_i      => seg_d1,
197
                wr2_i     => seg_d_rdy2,
198
                d2_i      => seg_d2,
199
                wr3_i     => seg_d_rdy3,
200
                d3_i      => seg_d3,
201
                wr4_i     => seg_d_rdy4,
202
                d4_i      => seg_d4,
203
                rd_addr_i => tx_mem_read_addr,
204
                d_o       => tx_mem_d_out,
205
                used_o    => tx_mem_used
206
        );
207
 
208
        UART_ins : entity work.UartComponent
209
        Generic Map(
210
                BAUD_DIVIDE_G => BAUD_DIVIDE_G,
211
                BAUD_RATE_G => BAUD_RATE_G
212
        )
213
        Port Map(
214
                TXD     => TX_o,
215
                RXD     => RX_i,
216
                CLK     => pclk_i,
217
                DBIN    => tx_data,
218
                DBOUT   => rx_data,
219
                RDA         => rx_available,
220
                TBE         => tx_buf_empty,
221
                RD              => rx_trigger,
222
                WR              => tx_trigger,
223
                PE              => open,
224
                FE              => open,
225
                OE              => open,
226
                RST         => uart_reset
227
        );
228
 
229
        decode_state_process : process(reset_i, pclk_i)
230
        begin
231
                if reset_i = '1' then
232
                        state <= S_Start;
233
                elsif pclk_i'event and pclk_i = '1' then
234
                        state <= state_next;
235
                else
236
                        state <= state;
237
                end if;
238
        end process decode_state_process;
239
 
240
        decode_next_state : process (reset_i, pclk_i, fsync_i, tx_buf_empty)
241
        begin
242
                if pclk_i'event and pclk_i = '1' then
243
                        fsync_prev <= fsync_i;
244
                        uart_reset <= '0';
245
                        rx_trigger <= '0';
246
                        tx_trigger <= '0';
247
                        tx_buf_empty_prev <= tx_buf_empty;
248
 
249
                        case (state) is
250
                                when S_Start =>
251
                                        state_next <= S_WaitForChar;
252
                                        uart_reset <= '1';
253
 
254
                                when S_WaitForChar =>
255
                                        if rx_available = '1' and rx_data = START_NEW_FRAME_CHAR then
256
                                                state_next <= S_WaitForNewFrame;
257
                                                rx_trigger <= '1';
258
                                        elsif rx_available = '1' then
259
                                                rx_trigger <= '1';
260
                                                tx_data <= rx_data;
261
                                                tx_trigger <= '1';
262
                                                state_next <= state_next;
263
                                        else
264
                                                state_next <= state_next;
265
                                        end if;
266
 
267
                                when S_WaitForNewFrame =>
268
                                        if fsync_prev = '0' and fsync_i = '1' then
269
                                                state_next <= S_CaptureStoreFrame;
270
                                        else
271
                                                state_next <= state_next;
272
                                        end if;
273
 
274
                                when S_CaptureStoreFrame =>
275
                                        if seg_frame_finished_out = '1' then
276
                                                state_next <= S_SendSizeB1;
277
                                        else
278
                                                state_next <= state_next;
279
                                        end if;
280
 
281
                                when S_SendSizeB1 =>
282
                                        state_next <= S_SendSizeB1WaitRdy;
283
                                        tx_data <= std_logic_vector(tx_mem_used(7 downto 0));
284
                                        tx_trigger <= '1';
285
                                when S_SendSizeB1WaitRdy =>
286
                                        if tx_buf_empty_prev = '0' and tx_buf_empty = '1' then
287
                                                state_next <= S_SendSizeB2;
288
                                        else
289
                                                state_next <= state_next;
290
                                        end if;
291
                                when S_SendSizeB2 =>
292
                                        state_next <= S_SendSizeB2WaitRdy;
293
                                        tx_data <= ZERO_PADDING_C(16-1 downto TX_MEMORY_ADDRESS_WIDTH_G) & std_logic_vector(tx_mem_used(TX_MEMORY_ADDRESS_WIDTH_G-1 downto 8));
294
                                        tx_trigger <= '1';
295
 
296
                                when S_SendSizeB2WaitRdy =>
297
                                        if tx_buf_empty_prev = '0' and tx_buf_empty = '1' then
298
                                                state_next <= S_SendStreamByte;
299
                                        else
300
                                                state_next <= state_next;
301
                                        end if;
302
 
303
                                when S_SendStreamByte =>
304
                                        state_next <= S_SendStreamByteWaitAccepted;
305
                                        tx_data <= tx_mem_d_out;
306
                                        tx_trigger <= '1';
307
                                when S_SendStreamByteWaitAccepted =>
308
                                        if tx_buf_empty = '0' then
309
                                                state_next <= S_SendStreamByteWaitRdy;
310
                                        end if;
311
 
312
                                when S_SendStreamByteWaitRdy =>
313
                                        if tx_buf_empty = '1' then
314
                                                if unsigned(tx_mem_read_addr) = tx_mem_used then
315
                                                        state_next <= S_Start;
316
                                                else
317
                                                        state_next <= S_SendStreamByte;
318
                                                end if;
319
                                        else
320
                                                state_next <= state_next;
321
                                        end if;
322
 
323
                                when S_Unknown =>
324
 
325
                                when others =>
326
                                        state_next <= S_Unknown;
327
                        end case;
328
                end if;
329
        end process decode_next_state;
330
 
331
        -- Detection of memory overflow and notification to the user.
332
        mem_overflow_detection_process : process(pclk_i)
333
        begin
334
                if pclk_i'event and pclk_i = '1' then
335
                        if reset_i = '1' then
336
                                tx_mem_overflow <= '0';
337
                        elsif tx_mem_used >= to_unsigned(TX_MEMORY_SIZE_G, 16) then
338
                                tx_mem_overflow <= '1';
339
                        else
340
                                tx_mem_overflow <= tx_mem_overflow;
341
                        end if;
342
                end if;
343
        end process mem_overflow_detection_process;
344
 
345
        led0_o <= boolean2sl(state = S_WaitForChar);
346
        led1_o <= boolean2sl(state = S_CaptureStoreFrame);
347
        led2_o <= boolean2sl(state = S_SendSizeB1)
348
                                or boolean2sl(state = S_SendSizeB2)
349
                                or boolean2sl(state = S_SendSizeB1WaitRdy)
350
                                or boolean2sl(state = S_SendSizeB2WaitRdy)
351
                                or boolean2sl(state = S_SendStreamByte)
352
                                or boolean2sl(state = S_SendStreamByteWaitAccepted)
353
                                or boolean2sl(state = S_SendStreamByteWaitRdy);
354
        led3_o <= tx_mem_overflow;
355
 
356
        pix <= pix_data_i(7)
357
                        when unsigned(tx_mem_used) < (to_unsigned(TX_MEMORY_SIZE_G, TX_MEMORY_ADDRESS_WIDTH_G) - to_unsigned(ROWS_G, TX_MEMORY_ADDRESS_WIDTH_G))
358
                        else '1';
359
 
360
        --Data segmentation tasks
361
        seg_reset <= '0' when state = S_CaptureStoreFrame else '1';
362
        tx_mem_reset <= '1' when state = S_WaitForNewFrame else '0';
363
 
364
        --Transmission memory read pointer incrementation
365
        tx_mem_read_write_pos_process : process(pclk_i)
366
        begin
367
                if pclk_i'event and pclk_i = '1' then
368
                        if state = S_WaitForNewFrame then
369
                                tx_mem_read_addr <= (others => '0');
370
                        elsif (tx_trigger = '1')
371
                                        and unsigned(tx_mem_read_addr) /= tx_mem_used
372
                                        and state = S_SendStreamByte then
373
                                tx_mem_read_addr <= std_logic_vector(unsigned(tx_mem_read_addr) + to_unsigned(1, TX_MEMORY_ADDRESS_WIDTH_G));
374
                        else
375
                                tx_mem_read_addr <= tx_mem_read_addr;
376
                        end if;
377
                end if;
378
        end process tx_mem_read_write_pos_process;
379
 
380
        -- Other tasks
381
        vgaRed <= pix_data_i(7 downto 5) when sw_i(0) = '0' else pix_data_i(7) & pix_data_i(7) & pix_data_i(7);
382
        vgaGreen <= pix_data_i(7 downto 5) when sw_i(0) = '0' else pix_data_i(7) & pix_data_i(7) & pix_data_i(7);
383
        vgaBlue <= pix_data_i(7 downto 6) when sw_i(0) = '0' else pix_data_i(7) & pix_data_i(7);
384
        vga_fsync_o <= fsync_i;
385
        vga_rsync_o <= rsync_i;
386
 
387
end Behavioral;

powered by: WebSVN 2.1.0

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