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

Subversion Repositories gpib_controller

[/] [gpib_controller/] [trunk/] [prototype_1/] [fpga/] [xilinx_prj/] [src/] [main.vhd] - Blame information for rev 11

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 Andrewski
--------------------------------------------------------------------------------
2
--This file is part of fpga_gpib_controller.
3
--
4
-- Fpga_gpib_controller is free software: you can redistribute it and/or modify
5
-- it under the terms of the GNU General Public License as published by
6
-- the Free Software Foundation, either version 3 of the License, or
7
-- (at your option) any later version.
8
--
9
-- Fpga_gpib_controller is distributed in the hope that it will be useful,
10
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
-- GNU General Public License for more details.
13
 
14
-- You should have received a copy of the GNU General Public License
15
-- along with Fpga_gpib_controller.  If not, see <http://www.gnu.org/licenses/>.
16 3 Andrewski
----------------------------------------------------------------------------------
17 11 Andrewski
-- Author: Andrzej Paluch
18 3 Andrewski
-- 
19
-- Create Date:    22:17:28 01/24/2012 
20
-- Design Name: 
21
-- Module Name:    main - Behavioral 
22
-- Project Name: 
23
-- Target Devices: 
24
-- Tool versions: 
25
-- Description: 
26
--
27
-- Dependencies: 
28
--
29
-- Revision: 
30
-- Revision 0.01 - File Created
31
-- Additional Comments: 
32
--
33
----------------------------------------------------------------------------------
34
library IEEE;
35
use IEEE.STD_LOGIC_1164.ALL;
36
use IEEE.STD_LOGIC_ARITH.ALL;
37
use IEEE.STD_LOGIC_UNSIGNED.ALL;
38
 
39
-- Uncomment the following library declaration if using
40
-- arithmetic functions with Signed or Unsigned values
41
--use IEEE.NUMERIC_STD.ALL;
42
 
43
-- Uncomment the following library declaration if instantiating
44
-- any Xilinx primitives in this code.
45
--library UNISIM;
46
--use UNISIM.VComponents.all;
47
 
48
use work.communication.ALL;
49
use work.wrapperComponents.ALL;
50
use work.helperComponents.ALL;
51
 
52
 
53
entity main is
54
        port(
55
                --reset : in std_logic;
56
                clk : in std_logic;
57
 
58
                ------ UART ---------
59
                RX : in std_logic;
60
                TX : out std_logic;
61
 
62
                ------ GPIB ---------
63
                DI : in std_logic_vector (7 downto 0);
64
                DO : out std_logic_vector (7 downto 0);
65
                ATN_in : in std_logic;
66
                ATN_out : out std_logic;
67
                DAV_in : in std_logic;
68
                DAV_out : out std_logic;
69
                NRFD_in : in std_logic;
70
                NRFD_out : out std_logic;
71
                NDAC_in : in std_logic;
72
                NDAC_out : out std_logic;
73
                EOI_in : in std_logic;
74
                EOI_out : out std_logic;
75
                SRQ_in : in std_logic;
76
                SRQ_out : out std_logic;
77
                IFC_in : in std_logic;
78
                IFC_out : out std_logic;
79
                REN_in : in std_logic;
80
                REN_out : out std_logic;
81
 
82
                ------ LEDS ---------
83
                led1 : out std_logic;
84
                led2 : out std_logic
85
                ------ DEBUG --------
86
                ;debug1 : out std_logic
87
                ;debug2 : out std_logic
88
        );
89
end main;
90
 
91
architecture Behavioral of main is
92
 
93
        constant BUF_LEN_MAX_BIT_NUM : integer := 11;
94
 
95
        type UART_REC_STATES is (
96
                ST_UART_REC_READ_ADDR,
97
                ST_UART_REC_READ_B1,
98
                ST_UART_REC_READ_B2,
99
 
100
                ST_UART_REC_READ_BURST_W_LEN_B1,
101
                ST_UART_REC_READ_BURST_W_LEN_B2,
102
 
103
                ST_UART_REC_BURST_READ
104
        );
105
 
106
        type UART_TR_STATES is (
107
                ST_UART_TR_IDLE,
108
                ST_UART_TR_WAIT_NOT_READY_TO_SEND_1,
109
                ST_UART_TR_WRITE_B1,
110
                ST_UART_TR_WAIT_NOT_READY_TO_SEND_2,
111
                ST_UART_TR_WRITE_B2,
112
 
113
                ST_UART_TR_WRITE_BURST,
114
                ST_UART_TR_WAIT_BURST_NOT_READY_TO_SEND,
115
                ST_UART_TR_WAIT_BURST_READY_TO_SEND
116
        );
117
 
118
        ---------- global -------------
119
        signal reset : std_logic;
120
        signal reset_timer : integer range 0 to 50000000;
121
 
122
        ---------- UART ---------------
123
        signal uart_reset, uart_clk, uart_RX, uart_TX, uart_data_out_ready,
124
                uart_data_in_ready, uart_ready_to_send : std_logic;
125
        signal uart_data_out, uart_data_in : std_logic_vector(7 downto 0);
126
 
127
        ---------- GPIB ------------------------
128
        signal gpib_reset, gpib_clk : std_logic;
129
        ---------- GPIB interface signals ------
130
        signal gpib_DI, gpib_DO : std_logic_vector (7 downto 0);
131
        signal gpib_output_valid : std_logic;
132
        signal gpib_ATN_in, gpib_ATN_out, gpib_DAV_in, gpib_DAV_out, gpib_NRFD_in,
133
                gpib_NRFD_out, gpib_NDAC_in, gpib_NDAC_out, gpib_EOI_in, gpib_EOI_out,
134
                gpib_SRQ_in, gpib_SRQ_out, gpib_IFC_in, gpib_IFC_out, gpib_REN_in,
135
                gpib_REN_out : std_logic;
136
        ---------- registers access -------------
137
        signal gpib_data_in, gpib_data_out : std_logic_vector(15 downto 0);
138
        signal gpib_reg_addr : std_logic_vector(14 downto 0);
139
        signal gpib_strobe_read, gpib_strobe_write : std_logic;
140
 
141
        ----------- writer strobe pulse generator
142
        signal t_in_strobe_write, t_out_strobe_write : std_logic;
143
        ----------- reader strobe pulse generator
144
        signal t_in_strobe_read, t_out_strobe_read : std_logic;
145
 
146
        ---------- interrupt line ---------------
147
        signal gpib_interrupt_line : std_logic;
148
 
149
        ---------- UART transceiver -------------
150
        signal uart_rec_state : UART_REC_STATES;
151
        signal uart_strobe_write_s1, uart_strobe_write_s2 : std_logic;
152
        signal uart_tr_state : UART_TR_STATES;
153
 
154
        signal burstLen : std_logic_vector (BUF_LEN_MAX_BIT_NUM downto 0);
155
        signal isBurstRead : std_logic;
156
        signal subscribeBurstRead_1, subscribeBurstRead_2 : std_logic;
157
        signal currentBurstReadLen : integer range 0 to 2**BUF_LEN_MAX_BIT_NUM;
158
 
159
        -- GPIB synchronizer
160
        signal gSync_clk : std_logic;
161
        signal gSync_DI : std_logic_vector(7 downto 0);
162
        signal gSync_DO : std_logic_vector(7 downto 0);
163
        signal gSync_ATN_in : std_logic;
164
        signal gSync_ATN_Out : std_logic;
165
        signal gSync_DAV_in : std_logic;
166
        signal gSync_DAV_out : std_logic;
167
        signal gSync_NRFD_in : std_logic;
168
        signal gSync_NRFD_out : std_logic;
169
        signal gSync_NDAC_in : std_logic;
170
        signal gSync_NDAC_out : std_logic;
171
        signal gSync_EOI_in : std_logic;
172
        signal gSync_EOI_out : std_logic;
173
        signal gSync_SRQ_in : std_logic;
174
        signal gSync_SRQ_out : std_logic;
175
        signal gSync_IFC_in : std_logic;
176
        signal gSync_IFC_out : std_logic;
177
        signal gSync_REN_in : std_logic;
178
        signal gSync_REN_out : std_logic;
179
 
180
begin
181
 
182
        -- UART
183
        uart_reset <= reset;
184
        uart_clk <= clk;
185
        uart_RX <= RX;
186
        TX <= uart_TX;
187
 
188
        -- GPIB sync
189
        gSync_clk <= clk;
190
        gSync_DI <= DI;
191
        gSync_ATN_in <= ATN_in;
192
        gSync_DAV_in <= DAV_in;
193
        gSync_NRFD_in <= NRFD_in;
194
        gSync_NDAC_in <= NDAC_in;
195
        gSync_EOI_in <= EOI_in;
196
        gSync_SRQ_in <= SRQ_in;
197
        gSync_IFC_in <= IFC_in;
198
        gSync_REN_in <= REN_in;
199
 
200
 
201
        -- GPIB
202
        gpib_reset <= reset;
203
        gpib_clk <= clk;
204
        gpib_DI <= not gSync_DO;
205
        DO <= gpib_DO when gpib_output_valid = '1' else "00000000";
206
        gpib_ATN_in <= not gSync_ATN_Out;
207
        ATN_out <= gpib_ATN_out;
208
        gpib_DAV_in <= not gSync_DAV_out;
209
        DAV_out <= gpib_DAV_out;
210
        gpib_NRFD_in <= not gSync_NRFD_out;
211
        NRFD_out <= gpib_NRFD_out;
212
        gpib_NDAC_in <= not gSync_NDAC_out;
213
        NDAC_out <= gpib_NDAC_out;
214
        gpib_EOI_in <= not gSync_EOI_out;
215
        EOI_out <= gpib_EOI_out;
216
        gpib_SRQ_in <= not gSync_SRQ_out;
217
        SRQ_out <= gpib_SRQ_out;
218
        gpib_IFC_in <= not gSync_IFC_out;
219
        IFC_out <= gpib_IFC_out;
220
        gpib_REN_in <= not gSync_REN_out;
221
        REN_out <= gpib_REN_out;
222
 
223
        -- DEBUG
224
        --led1 <= reset;
225
        led2 <= uart_data_out_ready;
226
        --debug1 <= gpib_output_valid;
227
 
228
        ---------- receive from UART -----------
229
        process (reset, uart_data_out_ready, uart_strobe_write_s2) begin
230
                if reset = '1' then
231
                        uart_strobe_write_s1 <= uart_strobe_write_s2;
232
                        t_in_strobe_write <= '0';
233
 
234
                        subscribeBurstRead_1 <= '0';
235
 
236
                        uart_rec_state <= ST_UART_REC_READ_ADDR;
237
                elsif rising_edge(uart_data_out_ready) then
238
                        case uart_rec_state is
239
                                when ST_UART_REC_READ_ADDR =>
240
                                        gpib_reg_addr(14 downto 6) <= "000000000";
241
                                        gpib_reg_addr(5 downto 0) <= uart_data_out(5 downto 0);
242
 
243
                                        if uart_data_out(7) = '1' then
244
                                                if uart_data_out(6) = '1' then
245
                                                        isBurstRead <= '1';
246
                                                        uart_rec_state <= ST_UART_REC_READ_BURST_W_LEN_B1;
247
                                                else
248
                                                        uart_strobe_write_s1 <= not uart_strobe_write_s2;
249
                                                        uart_rec_state <= ST_UART_REC_READ_ADDR;
250
                                                end if;
251
                                        else
252
                                                if uart_data_out(6) = '1' then
253
                                                        isBurstRead <= '0';
254
                                                        uart_rec_state <= ST_UART_REC_READ_BURST_W_LEN_B1;
255
                                                else
256
                                                        uart_rec_state <= ST_UART_REC_READ_B1;
257
                                                end if;
258
                                        end if;
259
 
260
                                when ST_UART_REC_READ_B1 =>
261
                                        gpib_data_in(7 downto 0) <= uart_data_out;
262
                                        uart_rec_state <= ST_UART_REC_READ_B2;
263
 
264
                                when ST_UART_REC_READ_B2 =>
265
                                        gpib_data_in(15 downto 8) <= uart_data_out;
266
                                        t_in_strobe_write <= not t_out_strobe_write;
267
                                        uart_rec_state <= ST_UART_REC_READ_ADDR;
268
 
269
                                -- burst length
270
                                when ST_UART_REC_READ_BURST_W_LEN_B1 =>
271
                                        burstLen(7 downto 0) <= uart_data_out;
272
                                        uart_rec_state <= ST_UART_REC_READ_BURST_W_LEN_B2;
273
 
274
                                when ST_UART_REC_READ_BURST_W_LEN_B2 =>
275
                                        burstLen(11 downto 8) <= uart_data_out(3 downto 0);
276
                                        if isBurstRead = '1' then
277
                                                subscribeBurstRead_1 <= not subscribeBurstRead_2;
278
                                                uart_rec_state <= ST_UART_REC_READ_ADDR;
279
                                        else
280
                                                uart_rec_state <= ST_UART_REC_BURST_READ;
281
                                        end if;
282
 
283
                                when ST_UART_REC_BURST_READ =>
284
                                        gpib_data_in(7 downto 0) <= uart_data_out;
285
                                        t_in_strobe_write <= not t_out_strobe_write;
286
                                        burstLen <= burstLen - 1;
287
 
288
                                        if burstLen = "000000000001" then
289
                                                uart_rec_state <= ST_UART_REC_READ_ADDR;
290
                                        end if;
291
 
292
                                when others =>
293
                                        uart_rec_state <= ST_UART_REC_READ_ADDR;
294
                        end case;
295
                end if;
296
        end process;
297
 
298
        ---------- write to UART ---------------------
299
        process (reset, clk, uart_strobe_write_s1) begin
300
                if reset = '1' then
301
                        uart_strobe_write_s2 <= uart_strobe_write_s1;
302
                        uart_data_in_ready <= '0';
303
                        t_in_strobe_read <= '0';
304
 
305
                        subscribeBurstRead_2 <= '0';
306
 
307
                        led1 <= '0';
308
 
309
                        uart_tr_state <= ST_UART_TR_IDLE;
310
                elsif rising_edge(clk) then
311
                        case uart_tr_state is
312
                                when ST_UART_TR_IDLE =>
313
                                        if uart_strobe_write_s2 /= uart_strobe_write_s1 and
314
                                                        uart_ready_to_send = '1' then
315
                                                uart_strobe_write_s2 <= uart_strobe_write_s1;
316
                                                uart_data_in <= gpib_data_out(7 downto 0);
317
                                                uart_data_in_ready <= '1';
318
                                                uart_tr_state <= ST_UART_TR_WAIT_NOT_READY_TO_SEND_1;
319
                                        elsif subscribeBurstRead_1 /= subscribeBurstRead_2 and
320
                                                        uart_ready_to_send = '1' then
321
                                                subscribeBurstRead_2 <= subscribeBurstRead_1;
322
                                                currentBurstReadLen <= conv_integer(UNSIGNED(burstLen));
323
 
324
                                                uart_tr_state <= ST_UART_TR_WRITE_BURST;
325
                                        end if;
326
 
327
                                when ST_UART_TR_WAIT_NOT_READY_TO_SEND_1 =>
328
                                        if uart_ready_to_send = '0' then
329
                                                uart_data_in_ready <= '0';
330
                                                uart_tr_state <= ST_UART_TR_WRITE_B1;
331
                                        end if;
332
 
333
                                when ST_UART_TR_WRITE_B1 =>
334
                                        if uart_ready_to_send = '1' then
335
                                                uart_data_in <= gpib_data_out(15 downto 8);
336
                                                uart_data_in_ready <= '1';
337
                                                uart_tr_state <= ST_UART_TR_WAIT_NOT_READY_TO_SEND_2;
338
                                        end if;
339
 
340
                                when ST_UART_TR_WAIT_NOT_READY_TO_SEND_2 =>
341
                                        if uart_ready_to_send = '0' then
342
                                                uart_data_in_ready <= '0';
343
                                                uart_tr_state <= ST_UART_TR_WRITE_B2;
344
                                        end if;
345
 
346
                                when ST_UART_TR_WRITE_B2 =>
347
                                        if uart_ready_to_send = '1' then
348
                                                t_in_strobe_read <= not t_out_strobe_read;
349
 
350
                                                uart_tr_state <= ST_UART_TR_IDLE;
351
                                        end if;
352
 
353
                                -- burst read
354
                                when ST_UART_TR_WRITE_BURST =>
355
                                        if uart_ready_to_send = '1' then
356
                                                uart_data_in <= gpib_data_out(7 downto 0);
357
                                                uart_data_in_ready <= '1';
358
                                                currentBurstReadLen <= currentBurstReadLen - 1;
359
 
360
                                                led1 <= '1';
361
 
362
                                                uart_tr_state <= ST_UART_TR_WAIT_BURST_NOT_READY_TO_SEND;
363
                                        end if;
364
 
365
                                when ST_UART_TR_WAIT_BURST_NOT_READY_TO_SEND =>
366
                                        if uart_ready_to_send = '0' then
367
                                                uart_data_in_ready <= '0';
368
                                                t_in_strobe_read <= not t_out_strobe_read;
369
 
370
                                                uart_tr_state <= ST_UART_TR_WAIT_BURST_READY_TO_SEND;
371
                                        end if;
372
 
373
                                when ST_UART_TR_WAIT_BURST_READY_TO_SEND =>
374
                                        if uart_ready_to_send = '1' then
375
 
376
                                                if currentBurstReadLen > 0 then
377
                                                        uart_tr_state <= ST_UART_TR_WRITE_BURST;
378
                                                else
379
                                                        led1 <= '0';
380
                                                        uart_tr_state <= ST_UART_TR_IDLE;
381
                                                end if;
382
                                        end if;
383
 
384
 
385
                                when others =>
386
                                        uart_tr_state <= ST_UART_TR_IDLE;
387
                        end case;
388
                end if;
389
        end process;
390
 
391
        process (clk) begin
392
                if rising_edge(clk) then
393
                        if reset_timer < 50000000 then
394
                                reset_timer <= reset_timer + 1;
395
                                reset <= '1';
396
                        else
397
                                reset <= '0';
398
                        end if;
399
                end if;
400
        end process;
401
 
402
        uart0: Uart port map(
403
                reset => uart_reset, clk => uart_clk, RX => uart_RX, TX => uart_TX,
404
                data_out => uart_data_out, data_out_ready => uart_data_out_ready,
405
                data_in => uart_data_in, data_in_ready => uart_data_in_ready,
406
                ready_to_send => uart_ready_to_send
407
        );
408
 
409
        spg_strobe_write: SinglePulseGenerator generic map (WIDTH => 1) port map(
410
                reset => reset, clk => clk,
411
                t_in => t_in_strobe_write, t_out => t_out_strobe_write,
412
                pulse => gpib_strobe_write
413
        );
414
 
415
        spg_strobe_read: SinglePulseGenerator generic map (WIDTH => 1) port map(
416
                reset => reset, clk => clk,
417
                t_in => t_in_strobe_read, t_out => t_out_strobe_read,
418
                pulse => gpib_strobe_read
419
        );
420
 
421
        gpibSync: GpibSynchronizer port map (
422
                clk => gSync_clk,
423
                DI => gSync_DI,
424
                DO => gSync_DO,
425
                ATN_in => gSync_ATN_in,
426
                ATN_out => gSync_ATN_Out,
427
                DAV_in => gSync_DAV_in,
428
                DAV_out => gSync_DAV_out,
429
                NRFD_in => gSync_NRFD_in,
430
                NRFD_out => gSync_NRFD_out,
431
                NDAC_in => gSync_NDAC_in,
432
                NDAC_out => gSync_NDAC_out,
433
                EOI_in => gSync_EOI_in,
434
                EOI_out => gSync_EOI_out,
435
                SRQ_in => gSync_SRQ_in,
436
                SRQ_out => gSync_SRQ_out,
437
                IFC_in => gSync_IFC_in,
438
                IFC_out => gSync_IFC_out,
439
                REN_in => gSync_REN_in,
440
                REN_out => gSync_REN_out
441
        );
442
 
443
        gpib0: RegsGpibFasade port map(
444
                reset => gpib_reset, clk => gpib_clk,
445
                DI => gpib_DI, DO => gpib_DO, output_valid => gpib_output_valid,
446
                ATN_in => gpib_ATN_in, ATN_out => gpib_ATN_out,
447
                DAV_in => gpib_DAV_in, DAV_out => gpib_DAV_out,
448
                NRFD_in => gpib_NRFD_in, NRFD_out => gpib_NRFD_out,
449
                NDAC_in => gpib_NDAC_in, NDAC_out => gpib_NDAC_out,
450
                EOI_in => gpib_EOI_in, EOI_out => gpib_EOI_out,
451
                SRQ_in => gpib_SRQ_in, SRQ_out => gpib_SRQ_out,
452
                IFC_in => gpib_IFC_in, IFC_out => gpib_IFC_out,
453
                REN_in => gpib_REN_in, REN_out => gpib_REN_out,
454
                data_in => gpib_data_in, data_out => gpib_data_out,
455
                reg_addr => gpib_reg_addr,
456
                strobe_read => gpib_strobe_read,
457
                strobe_write => gpib_strobe_write,
458
                interrupt_line => gpib_interrupt_line,
459
                debug1 => debug1, debug2 => debug2
460
        );
461
 
462
end Behavioral;
463
 

powered by: WebSVN 2.1.0

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