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

Subversion Repositories bw_tiff_compression

[/] [bw_tiff_compression/] [trunk/] [TB_capture_manager.vhd] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 amulder
--------------------------------------------------------------------------------
2
-- Company:        
3
-- Engineer:       Aart Mulder
4
--
5
-- Create Date:    18:12:25 08/26/2012
6
-- Design Name:    Tiff compression and transmission
7
-- Module Name:    /home/aart/Documents/aaMIUN_master/2nd_15HP_Project_FPGA_CCITT_implementation/vhdl/xilinix_fax4_work_model/TB_capture_manager.vhd
8
-- Project Name:   Tiff compression and transmission
9
-- VHDL Test Bench Created by ISE for module: capture_manager
10
-- Note:           Run the simulation for 13600us when using a 752x480 image
11
--                 or at least ~54ms to simulate RS232 communication as well. 
12
--                 I.e. the byte segmentation and RS232 communication are
13
--                 bypassed for OUT_FILENAME_C to save time in some cases.
14
--------------------------------------------------------------------------------
15
 
16
library ieee;
17
use ieee.std_logic_1164.ALL;
18
use ieee.numeric_std.ALL;
19
use IEEE.MATH_REAL.ALL;
20
library STD;
21
use STD.TEXTIO.ALL;
22
 
23
ENTITY TB_capture_manager IS
24
        Generic (
25
                COLUMNS_G         : integer := 752;
26
                ROWS_G            : integer := 480;
27
                COL_INDEX_WIDTH_G : integer := 10;
28
                ROW_INDEX_WIDTH_G : integer := 9;
29
 
30
                MAX_CODE_LEN_G    : integer := 28;
31
                MAX_CODE_LEN_WIDTH_G:integer:= 5;
32
                SEG_OUTPUT_WIDTH_G : integer:= 8;
33
 
34
                TX_MEMORY_SIZE_G            : integer := 4000;
35
                TX_MEMORY_ADDRESS_WIDTH_G   : integer := 12;
36
 
37
                RSYNC_DEATH_LEN_G   : integer := 10;
38
                INTEGER_RANGE_G   : integer := 200000;
39
 
40
                --@Simulation
41
                BAUD_DIVIDE_G        : integer := 2;
42
                BAUD_RATE_G          : integer := 32
43
        );
44
        Port(
45
                cols_o   : buffer unsigned(16-1 downto 0);
46
                rows_o   : buffer unsigned(16-1 downto 0);
47
                col_o    : buffer unsigned(16-1 downto 0);
48
                row_o    : buffer unsigned(16-1 downto 0)
49
        );
50
END TB_capture_manager;
51
 
52
ARCHITECTURE behavior OF TB_capture_manager IS
53
        type ByteT is (
54
                c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,
55
                c19,c20,c21,c22,c23,c24,c25,c26,c27,c28,c29,c30,c31,c32,c33,c34,
56
                c35,c36,c37,c38,c39,c40,c41,c42,c43,c44,c45,c46,c47,c48,c49,c50,
57
                c51,c52,c53,c54,c55,c56,c57,c58,c59,c60,c61,c62,c63,c64,c65,c66,
58
                c67,c68,c69,c70,c71,c72,c73,c74,c75,c76,c77,c78,c79,c80,c81,c82,
59
                c83,c84,c85,c86,c87,c88,c89,c90,c91,c92,c93,c94,c95,c96,c97,c98,
60
                c99,c100,c101,c102,c103,c104,c105,c106,c107,c108,c109,c110,c111,
61
                c112,c113,c114,c115,c116,c117,c118,c119,c120,c121,c122,c123,c124,
62
                c125,c126,c127,c128,c129,c130,c131,c132,c133,c134,c135,c136,c137,
63
                c138,c139,c140,c141,c142,c143,c144,c145,c146,c147,c148,c149,c150,
64
                c151,c152,c153,c154,c155,c156,c157,c158,c159,c160,c161,c162,c163,
65
                c164,c165,c166,c167,c168,c169,c170,c171,c172,c173,c174,c175,c176,
66
                c177,c178,c179,c180,c181,c182,c183,c184,c185,c186,c187,c188,c189,
67
                c190,c191,c192,c193,c194,c195,c196,c197,c198,c199,c200,c201,c202,
68
                c203,c204,c205,c206,c207,c208,c209,c210,c211,c212,c213,c214,c215,
69
                c216,c217,c218,c219,c220,c221,c222,c223,c224,c225,c226,c227,c228,
70
                c229,c230,c231,c232,c233,c234,c235,c236,c237,c238,c239,c240,c241,
71
           c242,c243,c244,c245,c246,c247,c248,c249,c250,c251,c252,c253,c254,c255);
72
        subtype Byte is ByteT;
73
        type ByteFileType is file of Byte;
74
        type MATRIX is array (integer range <>) of std_logic_vector(7 downto 0);
75
 
76
        constant PCLK_PERIOD_C : time := 37ns;  -- ~27MHz, Aptina WVGA Camera Module
77
 
78
        constant OUT_FILENAME_C  : String := "../images/tif/Segmentation752x480.tif";
79
        constant OUT_FILENAME_2_C: String := "../images/tif/Segmentation752x480(UART).tif";
80
 
81
        constant IN_FILENAME1_C : String := "../images/bmp/Segmentation752x480(real)bw_24bit.bmp";
82
        constant IN_FILENAME2_C : String := "../images/bmp/Segmentation752x480(real2)_24bit.bmp";
83
        constant IN_FILENAME3_C : String := "../images/bmp/Segmentation752x480(real4)bw_24bit.bmp";
84
        constant IN_FILENAME4_C : String := "../images/bmp/Segmentation752x480(real5)_24bit.bmp";
85
        constant IN_FILENAME_C  : String := IN_FILENAME1_C;
86
 
87
        constant CHAR_CAP_S           : std_logic_vector(7 downto 0) := std_logic_vector(to_unsigned(83, 8));
88
        constant START_NEW_FRAME_CHAR_C : std_logic_vector(7 downto 0) := CHAR_CAP_S;
89
 
90
        function int2bit_vec(A: integer; SIZE: integer) return BIT_VECTOR is
91
                variable RESULT : BIT_VECTOR(SIZE-1 DOWNTO 0);
92
                variable TMP  : integer;
93
        begin
94
                TMP := A;
95
                for i in 0 to SIZE - 1 loop
96
                        if TMP mod 2 = 1 then
97
                                RESULT(i) := '1';
98
                        else
99
                                RESULT(i) := '0';
100
                        end if;
101
                        TMP := TMP / 2;
102
                end loop;
103
                return RESULT;
104
        end;
105
 
106
        --Files
107
        file infile  : ByteFileType;
108
        file tif_file : ByteFileType;
109
 
110
   --Inputs
111
   signal reset_i : std_logic := '0';
112
   signal fsync_i : std_logic := '0';
113
   signal rsync_i : std_logic := '0';
114
   signal pclk_i : std_logic := '0';
115
   signal pix_data_i : std_logic_vector(7 downto 0) := (others => '0');
116
   signal RX_i : std_logic := '0';
117
        signal sw_i : STD_LOGIC_VECTOR(7 downto 0) := (others => '0');
118
 
119
        --Outputs
120
   signal vga_fsync_o : std_logic;
121
   signal vga_rsync_o : std_logic;
122
   signal vgaRed : std_logic_vector(7 downto 5);
123
   signal vgaGreen : std_logic_vector(7 downto 5);
124
   signal vgaBlue : std_logic_vector(7 downto 6);
125
   signal TX_o : std_logic;
126
   signal led0_o : std_logic;
127
   signal led1_o : std_logic;
128
   signal led2_o : std_logic;
129
   signal led3_o : std_logic;
130
        signal CCITT4_run_len_code_o : STD_LOGIC_VECTOR (MAX_CODE_LEN_G-1 downto 0);
131
        signal CCITT4_run_len_code_width_o : STD_LOGIC_VECTOR (MAX_CODE_LEN_WIDTH_G-1 downto 0);
132
        signal CCITT4_run_len_code_valid_o : STD_LOGIC;
133
        signal CCITT4_frame_finished_o : STD_LOGIC;
134
 
135
        --Other signals
136
        signal bitsperpixel : std_logic_vector(15 downto 0);
137
        signal colorplanes : std_logic_vector(15 downto 0);
138
        signal CCITTOutBuf : std_logic_vector (INTEGER_RANGE_G downto 0);
139
        signal RxByteCnt : integer range 0 to INTEGER_RANGE_G := 0;
140
        signal RS232_RX_DATA: MATRIX(0 to TX_MEMORY_SIZE_G);
141
 
142
        --RS232 tx module signals
143
        signal pc_tx_trigger, pc_rx_trigger, pc_tx_buf_empty, pc_rx_available, pc_rx_available_prev, pc_uart_reset : std_logic;
144
        signal pc_tx_data, pc_rx_data : std_logic_vector(7 downto 0);
145
 
146
        signal rxStreamSizeSignal : integer range 0 to 65535 := 0;
147
 
148
BEGIN
149
        -- Instantiate the Unit Under Test (UUT)
150
        uut : entity work.capture_manager
151
        Generic Map(
152
                COLUMNS_G         => COLUMNS_G,
153
                ROWS_G            => ROWS_G,
154
                COL_INDEX_WIDTH_G => COL_INDEX_WIDTH_G,
155
                ROW_INDEX_WIDTH_G => ROW_INDEX_WIDTH_G,
156
 
157
                MAX_CODE_LEN_WIDTH_G => MAX_CODE_LEN_WIDTH_G,
158
                MAX_CODE_LEN_G    => MAX_CODE_LEN_G,
159
 
160
                SEG_OUTPUT_WIDTH_G => SEG_OUTPUT_WIDTH_G,
161
 
162
                TX_MEMORY_SIZE_G => TX_MEMORY_SIZE_G,
163
                TX_MEMORY_ADDRESS_WIDTH_G => TX_MEMORY_ADDRESS_WIDTH_G,
164
 
165
                BAUD_DIVIDE_G => BAUD_DIVIDE_G,
166
                BAUD_RATE_G => BAUD_RATE_G
167
        )
168
        Port Map
169
        (
170
                reset_i => reset_i,
171
                fsync_i => fsync_i,
172
                rsync_i => rsync_i,
173
                pclk_i => pclk_i,
174
                pix_data_i => pix_data_i,
175
                vga_fsync_o => vga_fsync_o,
176
                vga_rsync_o => vga_rsync_o,
177
                vgaRed => vgaRed,
178
                vgaGreen => vgaGreen,
179
                vgaBlue => vgaBlue,
180
                TX_o => TX_o,
181
                RX_i => RX_i,
182
                led0_o => led0_o,
183
                led1_o => led1_o,
184
                led2_o => led2_o,
185
                led3_o => led3_o,
186
                sw_i => sw_i(6 downto 0),
187
                CCITT4_run_len_code_o => CCITT4_run_len_code_o,
188
                CCITT4_run_len_code_width_o => CCITT4_run_len_code_width_o,
189
                CCITT4_run_len_code_valid_o => CCITT4_run_len_code_valid_o,
190
                CCITT4_frame_finished_o => CCITT4_frame_finished_o
191
        );
192
 
193
        UART_pc_ins : entity work.UartComponent
194
        Generic Map(
195
                BAUD_DIVIDE_G => BAUD_DIVIDE_G,
196
                BAUD_RATE_G => BAUD_RATE_G
197
        )
198
        Port Map(
199
                TXD     => RX_i,
200
                RXD     => TX_o,
201
                CLK     => pclk_i,
202
                DBIN    => pc_tx_data,
203
                DBOUT => pc_rx_data,
204
                RDA     => pc_rx_available,
205
                TBE     => pc_tx_buf_empty,
206
                RD              => pc_rx_trigger,
207
                WR              => pc_tx_trigger,
208
                PE              => open,
209
                FE              => open,
210
                OE              => open,
211
                RST     => pc_uart_reset
212
        );
213
        pc_uart_reset <= reset_i;
214
 
215
        -- hold reset state for 3 clk periods.
216
        reset_i <= '1', '0' after PCLK_PERIOD_C * 3;
217
 
218
        -- Clock process definitions
219
        pclk_process : process
220
        begin
221
                pclk_i <= '0';
222
                wait for PCLK_PERIOD_C/2;
223
                pclk_i <= '1';
224
                wait for PCLK_PERIOD_C/2;
225
        end process;
226
 
227
        --Simulation of the master/PC on the other side of the RS232 connection.
228
        master_sim_process : process
229
        begin
230
                pc_tx_trigger <= '0';
231
                while reset_i = '1' loop
232
 
233
                end loop;
234
 
235
                wait for PCLK_PERIOD_C * 6;
236
                pc_tx_data <= START_NEW_FRAME_CHAR_C;
237
                pc_tx_trigger <= '1';
238
                wait for PCLK_PERIOD_C * 1;
239
                pc_tx_trigger <= '0';
240
 
241
                wait for 50ms;
242
                pc_tx_data <= START_NEW_FRAME_CHAR_C;
243
                pc_tx_trigger <= '1';
244
                wait for PCLK_PERIOD_C * 1;
245
                pc_tx_trigger <= '0';
246
 
247
                wait;
248
        end process master_sim_process;
249
 
250
        --Simulation of the input from the camera.
251
        img_read : process (pclk_i)
252
                variable pixelB : Byte;
253
                variable pixelG : Byte;
254
                variable pixelR : Byte;
255
                variable pixel : Byte;
256
                variable pixel1 : real := 0.0;
257
                variable pixel2 : std_logic_vector(7 downto 0);
258
                variable cols : unsigned(16-1 downto 0);
259
                variable rows : unsigned(16-1 downto 0);
260
                variable col : unsigned(16-1 downto 0);
261
                variable row : unsigned(16-1 downto 0);
262
                variable cnt : integer;
263
                variable rsync : std_logic;
264
                variable stop : std_logic;
265
                variable is_header_read : std_logic := '0';
266
                variable fileClosed : boolean := true;
267
                variable bitPos : integer range 0 to 8 := 0;
268
                variable dataOffset : std_logic_vector(31 downto 0);
269
                variable dataOffset2 : integer;
270
        begin  -- process img_read
271
                if (reset_i = '1') then
272
 
273
                elsif (pclk_i'event and pclk_i = '1') then
274
                        if fileClosed = true then
275
                                fileClosed := false;
276
                                file_open(infile, IN_FILENAME_C, READ_MODE);
277
 
278
                                is_header_read := '1';
279
                                pix_data_i <= (others => '0');
280
                                col             := (others => '0');
281
                                row             :=      (others => '0');
282
                                for i in 0 to 13 loop -- read header infos
283
                                        read(infile, pixel);
284
 
285
                                        case i is
286
                                                when 0 =>      -- First BMP indicator byte: 0x42
287
                                                when 1 =>      -- Second BMP indicator byte: 0x4D
288
                                                when 2 =>               -- 4 bytes with the file size
289
                                                when 10 =>              -- 1st byte with the data offset
290
                                                        dataOffset(7 downto 0) := to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8));
291
                                                when 11 =>              -- 2st byte with the data offset
292
                                                        dataOffset(15 downto 8) := to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8));
293
                                                when 12 =>              -- 3st byte with the data offset
294
                                                        dataOffset(23 downto 16) := to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8));
295
                                                when 13 =>              -- 4st byte with the data offset
296
                                                        dataOffset(31 downto 24) := to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8));
297
                                                        dataOffset2 := to_integer(unsigned(dataOffset));
298
                                                when others =>
299
                                                        null;
300
                                        end case;
301
                                end loop; -- i
302
                                for i in 14 to dataOffset2-1 loop -- read header infos
303
                                        read(infile, pixel);
304
 
305
                                        case i is
306
                                                when 18 =>              -- 1st byte of cols
307
                                                        cols(7 downto 0 ) := unsigned(to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8)));
308
                                                when 19 =>              -- 2nd byte of cols
309
                                                        cols(15 downto 8) := unsigned(to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8)));
310
                                                when 22 =>              -- 1st byte of rows
311
                                                        rows(7 downto 0 ) := unsigned(to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8)));
312
                                                when 23 =>              -- 2nd byte of  rows
313
                                                        rows(15 downto 8) := unsigned(to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8)));
314
                                                when 24 =>              -- do important things
315
                                                        cols_o  <= cols;
316
                                                        rows_o  <= rows;
317
                                                        cols            := cols - 1;
318
                                                        rows            := rows - 1;
319
                                                when 26 =>              --2 bytes with number of color planes
320
                                                        colorplanes(7 downto 0) <= to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8));
321
                                                when 27 =>
322
                                                        colorplanes(15 downto 8) <= to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8));
323
                                                when 28 =>
324
                                                        bitsperpixel(7 downto 0) <= to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8));
325
                                                when 29 =>              --2 bytes with number of bits per pixel
326
                                                        bitsperpixel(15 downto 8) <= to_Stdlogicvector(int2bit_vec(ByteT'pos(pixel), 8));
327
                                                when 53 =>
328
                                                        --report "\nFinished reading reading header\n";
329
                                                when others =>
330
                                                        null;
331
                                        end case;
332
                                end loop; -- i
333
                                rsync := '0';
334
                                cnt     := RSYNC_DEATH_LEN_G;
335
                                stop    := '0';
336
                                bitPos := 7;
337
 
338
                        end if; --fileClosed = true
339
 
340
                        rsync_i <= rsync;
341
                        if rsync = '1' then
342
                                if row = 0 and col = 0 then
343
                                        fsync_i <= '1';
344
                                else
345
                                        fsync_i <= '0';
346
                                end if;
347
 
348
                                if stop = '0' then
349
                                        if bitsperpixel = std_logic_vector(to_unsigned(1, 16)) then
350
                                                if bitPos = 7 then
351
                                                        read(infile, pixelB); -- B
352
                                                end if;
353
                                                pixel2 := to_stdlogicvector(int2bit_vec(ByteT'pos(pixelB), 8));
354
                                                pixel2 := "0000000" & pixel2(bitPos downto bitPos);
355
                                                pixel1 := real(to_integer(unsigned(pixel2))*255);
356
                                                if bitPos = 0 then
357
                                                        bitPos := 7;
358
                                                else
359
                                                        bitPos := bitPos - 1;
360
                                                end if;
361
                                        elsif bitsperpixel = std_logic_vector(to_unsigned(8, 16)) then
362
                                                read(infile, pixelB); -- B
363
                                                pixel1  := (ByteT'pos(pixelB)*1.0);
364
                                        elsif bitsperpixel = std_logic_vector(to_unsigned(24, 16)) then
365
                                                read(infile, pixelB); -- B
366
                                                read(infile, pixelG); -- G
367
                                                read(infile, pixelR); -- R
368
                                                pixel1  := (ByteT'pos(pixelB)*0.11) + (ByteT'pos(pixelR)*0.3) + (ByteT'pos(pixelG)*0.59);
369
                                        elsif bitsperpixel = std_logic_vector(to_unsigned(32, 16)) then
370
                                                read(infile, pixelB); -- X do a dummy read to get rid of the first byte.
371
                                                read(infile, pixelB); -- B
372
                                                read(infile, pixelG); -- G
373
                                                read(infile, pixelR); -- R
374
                                                pixel1  := (ByteT'pos(pixelB)*0.11) + (ByteT'pos(pixelR)*0.3) + (ByteT'pos(pixelG)*0.59);
375
                                        end if;
376
                                        pix_data_i <= std_logic_vector(to_unsigned(integer(pixel1), 8));
377
 
378
                                        col_o           <= col;
379
                                        row_o           <= row;
380
                                end if;
381
 
382
                                if col = cols then
383
                                        col     := (others => '0');
384
                                        rsync   := '0';
385
                                        if row = rows then
386
                                                File_Close(infile);
387
                                                fileClosed := true;
388
                                                stop := '1';
389
                                        else
390
                                                row := row + 1;
391
                                        end if;         -- row
392
                                else
393
                                        col := col + 1;
394
                                end if;                 -- col
395
                        else                                    -- rsync
396
                                if cnt > 0 then
397
                                        cnt     := cnt -1;
398
                                else
399
                                        cnt     := RSYNC_DEATH_LEN_G;
400
                                        rsync := '1';
401
                                end if;
402
                                pix_data_i <= (others => 'X');
403
                        end if; -- rsync
404
                end if;   -- clk
405
        end process img_read;
406
 
407
        write_CCITT4_output_to_img_process : process(pclk_i)
408
                variable HEADER : MATRIX(0 to 5)         := (x"4D",x"4D",x"00",x"2A",x"00",x"00");--first 6 bytes of header fix
409
                variable HEADER_offset : MATRIX(0 to 1)  := (x"00",x"0F");                 --offset for number of directories 
410
                variable DATA: MATRIX(0 to INTEGER_RANGE_G);
411
                variable NO_DIR : MATRIX(0 to 1)           := (x"00",x"08");                        -- no of directories fix\
412
                --Types: 1=BYTE
413
                --       2=ASCII
414
                --       3=SHORT
415
                --       4=LONG
416
                --       5=RATIONAL
417
                --Tif IFD(header) entry                       |  ID tag   | Field type|   Number of values    |     Actual value      |
418
                variable HEADER_W: MATRIX(0 to 11)         := (x"01",x"00",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"05",x"00",x"00");   -- img width
419
                variable HEADER_L: MATRIX(0 to 11)         := (x"01",x"01",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"05",x"00",x"00");   -- img length
420
                variable HEADER_BITS: MATRIX(0 to 11)      := (x"01",x"02",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"01",x"00",x"00");   -- bits per sample fix
421
                variable HEADER_COMPRESS: MATRIX(0 to 11)  := (x"01",x"03",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"04",x"00",x"00");   -- COMPRESSION fix
422
                variable HEADER_PHOTO: MATRIX(0 to 11)     := (x"01",x"06",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"00",x"00",x"00");   -- photomotric interpretation fix
423
                variable STRIP_OFFSET: MATRIX(0 to 11)     := (x"01",x"11",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"08",x"00",x"00");   -- strip offset 
424
                variable ROWS_PER_STRIP: MATRIX(0 to 11)   := (x"01",x"16",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"00",x"00",x"00");   -- strip offset 
425
                variable STRIP_BYTE_COUNT: MATRIX(0 to 11) := (x"01",x"17",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"00",x"00",x"00");   -- strip offset 
426
                variable HEADER_EOB: MATRIX(0 to 3)        := (x"00",x"00",x"00",x"00");   -- END of block
427
                variable CCITTOutIndex : integer range 0 to INTEGER_RANGE_G+1 := 0;
428
                variable ind1 : integer range 0 to INTEGER_RANGE_G+2 := 0;
429
                variable ind2 : integer range 0 to INTEGER_RANGE_G+4 := 0;
430
                variable ind3 : integer range 0 to INTEGER_RANGE_G+5 := 0;
431
                variable rem_index2 : std_logic_vector(15 downto 0);
432
                variable s : line;
433
                variable fileClosed : boolean := true;
434
                variable frame_finished_prev : std_logic := '0';
435
        begin
436
                if pclk_i'event and pclk_i = '1' then
437
                        if CCITT4_run_len_code_valid_o = '1' then
438
                                ind1 := INTEGER_RANGE_G - CCITTOutIndex;
439
                                ind2 := INTEGER_RANGE_G - CCITTOutIndex - to_integer(unsigned(CCITT4_run_len_code_width_o)) + 1;
440
                                ind3 := to_integer(unsigned(CCITT4_run_len_code_width_o))-1;
441
                                CCITTOutBuf(ind1 downto ind2) <= CCITT4_run_len_code_o(ind3 downto 0);
442
                                CCITTOutIndex := CCITTOutIndex + to_integer(unsigned(CCITT4_run_len_code_width_o));
443
                        elsif frame_finished_prev = '0' and CCITT4_frame_finished_o = '1' then
444
                                ----------------------------------------------------File writing--------------------------
445
                                if fileClosed then
446
                                        fileClosed := false;
447
                                        file_open(tif_file, OUT_FILENAME_C, WRITE_MODE);
448
                                end if;
449
 
450
                                report "Frame finished. Starting to copy to DATA()";
451
 
452
                                write(s, string'("CCITTOutIndex = "));
453
                                write(s, CCITTOutIndex);
454
                                writeline(output, s);
455
 
456
                                --Fill up the last byte with zeros if left over
457
                                ind1 := CCITTOutIndex mod 8;
458
                                if ind1 > 0 then
459
                                        ind1 := INTEGER_RANGE_G - (CCITTOutIndex - (CCITTOutIndex mod 8) + 8);
460
                                        CCITTOutBuf(INTEGER_RANGE_G - CCITTOutIndex downto ind1) <= (others => '0');
461
                                        CCITTOutIndex := INTEGER_RANGE_G - ind1;
462
                                end if;
463
                                CCITTOutIndex := CCITTOutIndex / 8;
464
 
465
                                write(s, string'("CCITTOutIndex = "));
466
                                write(s, CCITTOutIndex);
467
                                writeline(output, s);
468
 
469
                                ind1 := INTEGER_RANGE_G;
470
                                ind2 := INTEGER_RANGE_G - 7;
471
                                for i in 0 to CCITTOutIndex loop --
472
                                        if ind2 > INTEGER_RANGE_G then
473
                                                report "Invalid ind2";
474
                                        end if;
475
                                        if ind1 > INTEGER_RANGE_G then
476
                                                report "Invalid ind1";
477
                                        end if;
478
                                        DATA(i) := CCITTOutBuf(ind1 downto ind2);
479
                                        ind1 := ind1 - 8;
480
                                        ind2 := ind2 - 8;
481
                                end loop;
482
                                report "Finished copying to DATA()";
483
 
484
                                write(s, string'("CCITTOutIndex = "));
485
                                write(s, CCITTOutIndex);
486
                                writeline(output, s);
487
 
488
                                --Align the data in the DATA() on word size
489
                                if CCITTOutIndex mod 2 = 1 then
490
                                        DATA(CCITTOutIndex) := (others => '0');
491
                                        CCITTOutIndex := CCITTOutIndex + 1;
492
                                end if;
493
 
494
                                report "Finished copying to DATA()";
495
                                HEADER_W(8):=std_logic_vector(cols_o(15 downto 8));
496
                                HEADER_W(9):=std_logic_vector(cols_o(7 downto 0));
497
 
498
                                HEADER_L(8):=std_logic_vector(rows_o(15 downto 8));
499
                                HEADER_L(9):=std_logic_vector(rows_o(7 downto 0));
500
 
501
                                rem_index2 :=STD_LOGIC_VECTOR(to_unsigned(CCITTOutIndex + 8, 16));
502
                                HEADER_offset(1):=rem_index2(7 downto 0);
503
                                HEADER_offset(0):=rem_index2(15 downto 8);
504
 
505
                                ROWS_PER_STRIP(8):=std_logic_vector(rows_o(15 downto 8));
506
                                ROWS_PER_STRIP(9):=std_logic_vector(rows_o(7 downto 0));
507
 
508
                                STRIP_BYTE_COUNT(8) := STD_LOGIC_VECTOR(to_unsigned(CCITTOutIndex/256, 8));
509
                                STRIP_BYTE_COUNT(9) := STD_LOGIC_VECTOR(to_unsigned(CCITTOutIndex, 8));
510
 
511
                                --------------------------------------
512
                                ---- start writing
513
                                --------------------------------------  
514
                                for i in 0 to 5 loop -- read header infos
515
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER(i))))); --first 6 bytes of header        
516
                                end loop;
517
                                for i in 0 to 1 loop
518
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_offset(i))))); --, offset for number of directories 
519
                                end loop;
520
                                for i in 0 to CCITTOutIndex-1 loop
521
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(DATA(i))))); --, data
522
                                end loop;
523
                                for i in 0 to 1 loop
524
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(NO_DIR(i))))); --, number of directories
525
                                end loop;
526
                                for i in 0 to 11 loop
527
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_W(i))))); --, img width
528
                                end loop;
529
                                for i in 0 to 11 loop
530
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_L(i))))); --, img length
531
                                end loop;
532
                                for i in 0 to 11 loop
533
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_BITS(i))))); --, bits per sample
534
                                end loop;
535
                                for i in 0 to 11 loop
536
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_COMPRESS(i))))); --, COMPRESSION
537
                                end loop;
538
                                for i in 0 to 11 loop
539
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_PHOTO(i))))); --, Photometric interprattion
540
                                end loop;
541
                                for i in 0 to 11 loop
542
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(STRIP_OFFSET(i))))); --, STRIP offset
543
                                end loop;
544
                                for i in 0 to 11 loop
545
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(ROWS_PER_STRIP(i))))); --, Rows per strip
546
                                end loop;
547
                                for i in 0 to 11 loop
548
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(STRIP_BYTE_COUNT(i))))); --, Strip byte count
549
                                end loop;
550
                                for i in 0 to 3 loop
551
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_EOB(i))))); --, EOB
552
                                end loop;
553
                                --------------------------------------
554
                                ---- end writing
555
                                --------------------------------------          
556
 
557
                                File_Close(tif_file);
558
                                fileClosed := true;
559
                                CCITTOutIndex := 0;
560
                        end if;
561
 
562
                        rxStreamSizeSignal <= CCITTOutIndex;
563
                        frame_finished_prev := CCITT4_frame_finished_o;
564
                end if;
565
        end process write_CCITT4_output_to_img_process;
566
 
567
        rs232_rx_sim_process : process(pclk_i)
568
                variable HEADER : MATRIX(0 to 5)           := (x"4D",x"4D",x"00",x"2A",x"00",x"00");--first 6 bytes of header fix
569
                variable HEADER_offset : MATRIX(0 to 1)    := (x"00",x"0F");                 --offset for number of directories 
570
                variable DATA: MATRIX(0 to 10000);
571
                variable NO_DIR : MATRIX(0 to 1)           := (x"00",x"08");                        -- no of directories fix\
572
                --Types: 1=BYTE
573
                --       2=ASCII
574
                --       3=SHORT
575
                --       4=LONG
576
                --       5=RATIONAL
577
                --Tif IFD(header) entry                       |  ID tag   | Field type|   Number of values    |     Actual value      |
578
                variable HEADER_W: MATRIX(0 to 11)         := (x"01",x"00",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"05",x"00",x"00");   -- img width
579
                variable HEADER_L: MATRIX(0 to 11)         := (x"01",x"01",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"05",x"00",x"00");   -- img length
580
                variable HEADER_BITS: MATRIX(0 to 11)      := (x"01",x"02",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"01",x"00",x"00");   -- bits per sample fix
581
                variable HEADER_COMPRESS: MATRIX(0 to 11)  := (x"01",x"03",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"04",x"00",x"00");   -- COMPRESSION fix
582
                variable HEADER_PHOTO: MATRIX(0 to 11)     := (x"01",x"06",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"00",x"00",x"00");   -- photomotric interpretation fix
583
                variable STRIP_OFFSET: MATRIX(0 to 11)     := (x"01",x"11",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"08",x"00",x"00");   -- strip offset 
584
                variable ROWS_PER_STRIP: MATRIX(0 to 11)   := (x"01",x"16",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"00",x"00",x"00");   -- strip offset 
585
                variable STRIP_BYTE_COUNT: MATRIX(0 to 11) := (x"01",x"17",x"00",x"03",x"00",x"00",x"00",x"01",x"00",x"00",x"00",x"00");   -- strip offset 
586
                variable HEADER_EOB: MATRIX(0 to 3)        := (x"00",x"00",x"00",x"00");   -- END of block
587
                variable ind1 : integer range 0 to INTEGER_RANGE_G := 0;
588
                variable rem_index2 : std_logic_vector(15 downto 0);
589
                variable fileClosed : boolean := true;
590
                variable rxStreamSize : integer range 0 to 65535 := 0;
591
        begin
592
                if pclk_i'event and pclk_i = '1' then
593
                        pc_rx_trigger <= '0';
594
                        pc_rx_available_prev <= pc_rx_available;
595
 
596
                        if ind1 = 2 then
597
                                rxStreamSize := to_integer(unsigned(DATA(0))) + to_integer(unsigned(DATA(1))) * 256;
598
                        end if;
599
 
600
                        if pc_rx_available_prev = '0' and pc_rx_available = '1' then
601
                                DATA(ind1) := pc_rx_data;
602
                                ind1 := ind1 + 1;
603
                                pc_rx_trigger <= '1';
604
                        elsif ind1 = rxStreamSize+2 and rxStreamSize /= 0 then
605
                                ----------------------------------------------------File writing--------------------------
606
                                if fileClosed then
607
                                        fileClosed := false;
608
                                        file_open(tif_file, OUT_FILENAME_2_C, WRITE_MODE);
609
                                end if;
610
 
611
                                --Align the data in the DATA() on word size
612
                                if ind1 mod 2 = 1 then
613
                                        DATA(ind1) := (others => '0');
614
                                        ind1 := ind1 + 1;
615
                                end if;
616
 
617
                                HEADER_W(8):=std_logic_vector(cols_o(15 downto 8));
618
                                HEADER_W(9):=std_logic_vector(cols_o(7 downto 0));
619
 
620
                                HEADER_L(8):=std_logic_vector(rows_o(15 downto 8));
621
                                HEADER_L(9):=std_logic_vector(rows_o(7 downto 0));--- "00000010";
622
 
623
                                rem_index2 :=STD_LOGIC_VECTOR(to_unsigned(ind1 + 8 - 2, 16));
624
                                HEADER_offset(1):=rem_index2(7 downto 0);
625
                                HEADER_offset(0):=rem_index2(15 downto 8);
626
 
627
                                ROWS_PER_STRIP(8):=std_logic_vector(rows_o(15 downto 8));
628
                                ROWS_PER_STRIP(9):=std_logic_vector(rows_o(7 downto 0));
629
 
630
                                STRIP_BYTE_COUNT(8) := STD_LOGIC_VECTOR(to_unsigned(ind1/256, 8));
631
                                STRIP_BYTE_COUNT(9) := STD_LOGIC_VECTOR(to_unsigned(ind1, 8));
632
 
633
                                --------------------------------------
634
                                ---- start writing
635
                                --------------------------------------  
636
                                for i in 0 to 5 loop -- read header infos
637
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER(i))))); --first 6 bytes of header        
638
                                end loop;
639
                                for i in 0 to 1 loop
640
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_offset(i))))); --, offset for number of directories 
641
                                end loop;
642
                                for i in 2 to ind1-1 loop
643
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(DATA(i))))); --, data
644
                                end loop;
645
                                for i in 0 to 1 loop
646
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(NO_DIR(i))))); --, number of directories
647
                                end loop;
648
                                for i in 0 to 11 loop
649
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_W(i))))); --, img width
650
                                end loop;
651
                                for i in 0 to 11 loop
652
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_L(i))))); --, img length
653
                                end loop;
654
                                for i in 0 to 11 loop
655
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_BITS(i))))); --, bits per sample
656
                                end loop;
657
                                for i in 0 to 11 loop
658
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_COMPRESS(i))))); --, COMPRESSION
659
                                end loop;
660
                                for i in 0 to 11 loop
661
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_PHOTO(i))))); --, Photometric interprattion
662
                                end loop;
663
                                for i in 0 to 11 loop
664
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(STRIP_OFFSET(i))))); --, STRIP offset
665
                                end loop;
666
                                for i in 0 to 11 loop
667
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(ROWS_PER_STRIP(i))))); --, Rows per strip
668
                                end loop;
669
                                for i in 0 to 11 loop
670
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(STRIP_BYTE_COUNT(i))))); --, Strip byte count
671
                                end loop;
672
                                for i in 0 to 3 loop
673
                                        write(tif_file , ByteT'val(ieee.numeric_std.To_Integer(unsigned(HEADER_EOB(i))))); --, EOB
674
                                end loop;
675
                                --------------------------------------
676
                                ---- end writing
677
                                --------------------------------------          
678
 
679
                                ind1 := 0;
680
                                rxStreamSize := 0;
681
                                File_Close(tif_file);
682
                                fileClosed := true;
683
                        end if;
684
 
685
                        RxByteCnt <= ind1;
686
                end if;
687
        end process rs232_rx_sim_process;
688
 
689
end;

powered by: WebSVN 2.1.0

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