summaryrefslogtreecommitdiff
path: root/netfpga10g/hdl/dma.vhd
blob: 4c7e4f8ef5785d8721042adc18d3e624e31fb25a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity dma is
generic (
        BITS : integer := 14
);
port (
        clk : in std_logic;
        reset : in std_logic;

        rx_read : in std_logic;
        rx_write : in std_logic;
        rx_complete : in std_logic;
        rx_data : in std_logic_vector(63 downto 0);
        rx_address : in std_logic_vector(28 downto 0);

        tx_read : out std_logic;
        tx_write : out std_logic;
        tx_complete : out std_logic;
        tx_data : out std_logic_vector(63 downto 0);
        tx_address : out std_logic_vector(28 downto 0);
        tx_length : out std_logic_vector(8 downto 0);
        tx_accept : in std_logic;
        tx_done : in std_logic;

        port0_read : out std_logic;
        port0_rd_data : in std_logic_vector(63 downto 0);
        port0_rd_empty : in std_logic;
        port0_rd_count : in std_logic_vector(BITS downto 0);

        port0_write : out std_logic;
        port0_wr_data : out std_logic_vector(63 downto 0);
        port0_wr_full : in std_logic;
        port0_wr_count : in std_logic_vector(BITS downto 0);

        port1_read : out std_logic;
        port1_rd_data : in std_logic_vector(63 downto 0);
        port1_rd_empty : in std_logic;
        port1_rd_count : in std_logic_vector(BITS downto 0);

        port1_write : out std_logic;
        port1_wr_data : out std_logic_vector(63 downto 0);
        port1_wr_full : in std_logic;
        port1_wr_count : in std_logic_vector(BITS downto 0);

        port2_read : out std_logic;
        port2_rd_data : in std_logic_vector(63 downto 0);
        port2_rd_empty : in std_logic;
        port2_rd_count : in std_logic_vector(BITS downto 0);

        port2_write : out std_logic;
        port2_wr_data : out std_logic_vector(63 downto 0);
        port2_wr_full : in std_logic;
        port2_wr_count : in std_logic_vector(BITS downto 0);

        port3_read : out std_logic;
        port3_rd_data : in std_logic_vector(63 downto 0);
        port3_rd_empty : in std_logic;
        port3_rd_count : in std_logic_vector(BITS downto 0);

        port3_write : out std_logic;
        port3_wr_data : out std_logic_vector(63 downto 0);
        port3_wr_full : in std_logic;
        port3_wr_count : in std_logic_vector(BITS downto 0);

        max_read : in std_logic_vector(2 downto 0);
        max_write : in std_logic_vector(2 downto 0);

        interrupt : out std_logic;
        interrupt_rdy : in std_logic;

        mdio_command : out std_logic_vector(0 to 27);
        mdio_data : in std_logic_vector(0 to 15);
        mdio_enable : out std_logic;
        mdio_done : in std_logic;

        leds : out std_logic_vector(2 downto 0)
);
end dma;

architecture arch of dma is
        type state_t is (state_idle, state_tx_read, state_tx_write, state_tx_complete, state_rx_complete,
                         state_dma_read, state_dma_write, state_interrupt, state_command,
                         state_test_write_head, state_test_write,
                         state_test_read_head, state_test_read, state_test_complete,
                         state_mdio);
        signal state : state_t;

        signal fifo_read : std_logic;
        signal fifo_write : std_logic;
        signal fifo_din : std_logic_vector(63 downto 0);
        signal fifo_dout : std_logic_vector(63 downto 0);
        signal fifo_full : std_logic;
        signal fifo_empty : std_logic;
        signal fifo_count : std_logic_vector(BITS downto 0);

        component simple_fifo port (
                clk : in std_logic;
                rst : in std_logic;
                rd_en : in std_logic;
                wr_en : in std_logic;
                din : in std_logic_vector(63 downto 0);
                dout : out std_logic_vector(63 downto 0);
                full : out std_logic;
                empty : out std_logic;
                data_count : out std_logic_vector(BITS downto 0)
        );
        end component;

        signal cmd_read : std_logic;
        signal cmd_write : std_logic;
        signal cmd_din : std_logic_vector(63 downto 0);
        signal cmd_dout : std_logic_vector(63 downto 0);
        signal cmd_full : std_logic;
        signal cmd_empty : std_logic;

        component cmd_fifo port (
                clk : in std_logic;
                rst : in std_logic;
                rd_en : in std_logic;
                wr_en : in std_logic;
                din : in std_logic_vector(63 downto 0);
                dout : out std_logic_vector(63 downto 0);
                full : out std_logic;
                empty : out std_logic
        );
        end component;

        signal test_completion : std_logic_vector(63 downto 0);
begin
        process (clk, reset)
                variable bar_addr : unsigned(BITS downto 0);
                variable dma_addr : unsigned(28 downto 0);
                variable length : unsigned(BITS downto 0);
                variable remain : unsigned(BITS downto 0);
                variable cycles : unsigned(BITS downto 0);
                variable backup_addr : unsigned(28 downto 0);
                variable backup_length : unsigned(BITS downto 0);
        begin
                if reset = '1' then
                        state <= state_idle;
                        tx_read <= '0';
                        tx_write <= '0';
                        tx_complete <= '0';
                        tx_data <= (others => '0');
                        tx_address <= (others => '0');
                        tx_length <= (others => '0');
                        fifo_read <= '0';
                        fifo_write <= '0';
                        fifo_din <= (others => '0');
                        interrupt <= '0';
                        cmd_read <= '0';
                        cmd_write <= '0';
                        cmd_din <= (others => '0');
                        test_completion <= (others => '0');
                        port0_read <= '0';
                        port0_write <= '0';
                        port0_wr_data <= (others => '0');
                        port1_read <= '0';
                        port1_write <= '0';
                        port1_wr_data <= (others => '0');
                        port2_read <= '0';
                        port2_write <= '0';
                        port2_wr_data <= (others => '0');
                        port3_read <= '0';
                        port3_write <= '0';
                        port3_wr_data <= (others => '0');
                        mdio_enable <= '0';
                        mdio_command <= (others => '0');

                elsif rising_edge(clk) then
                        fifo_read <= '0';
                        fifo_write <= '0';
                        fifo_din <= (others => '0');
                        cmd_read <= '0';
                        cmd_write <= '0';
                        cmd_din <= (others => '0');
                        port0_read <= '0';
                        port0_write <= '0';
                        port1_read <= '0';
                        port1_write <= '0';
                        port2_read <= '0';
                        port2_write <= '0';
                        port3_read <= '0';
                        port3_write <= '0';

                        -- bar read
                        if state = state_idle and rx_read = '1' then
                                state <= state_tx_complete;
                                tx_complete <= '1';
                                tx_data <= (others => '0');
                                tx_address <= rx_address;
                                bar_addr := unsigned(rx_address(BITS downto 0));

                                -- read fifo
                                if bar_addr = 0 then
                                        if fifo_empty = '1' then
                                                tx_data <= (others => '1');
                                        else
                                                tx_data <= fifo_dout;
                                                fifo_read <= '1';
                                        end if;

                                -- read command
                                elsif bar_addr = 1 then
                                        if cmd_empty = '1' then
                                                tx_data <= (others => '1');
                                        else
                                                tx_data <= cmd_dout;
                                                cmd_read <= '1';
                                        end if;

                                -- other
                                elsif bar_addr = 3 then
                                        tx_data(BITS downto 0) <= fifo_count;

                                elsif bar_addr = 4 then
                                        tx_data(2 downto 0) <= max_read;

                                elsif bar_addr = 5 then
                                        tx_data(2 downto 0) <= max_write;

                                elsif bar_addr = 6 then
                                        tx_data <= test_completion;

                                -- read ports
                                elsif bar_addr = 10 then
                                        if port0_rd_empty = '1' then
                                                tx_data <= (others => '1');
                                        else
                                                tx_data <= port0_rd_data;
                                                port0_read <= '1';
                                        end if;

                                elsif bar_addr = 11 then
                                        if port1_rd_empty = '1' then
                                                tx_data <= (others => '1');
                                        else
                                                tx_data <= port1_rd_data;
                                                port1_read <= '1';
                                        end if;

                                elsif bar_addr = 12 then
                                        if port2_rd_empty = '1' then
                                                tx_data <= (others => '1');
                                        else
                                                tx_data <= port2_rd_data;
                                                port2_read <= '1';
                                        end if;

                                elsif bar_addr = 13 then
                                        if port3_rd_empty = '1' then
                                                tx_data <= (others => '1');
                                        else
                                                tx_data <= port3_rd_data;
                                                port3_read <= '1';
                                        end if;

                                -- read counts
                                elsif bar_addr = 20 then
                                        tx_data(BITS downto 0) <= port0_rd_count;

                                elsif bar_addr = 21 then
                                        tx_data(BITS downto 0) <= port0_wr_count;

                                elsif bar_addr = 22 then
                                        tx_data(BITS downto 0) <= port1_rd_count;

                                elsif bar_addr = 23 then
                                        tx_data(BITS downto 0) <= port1_wr_count;

                                elsif bar_addr = 24 then
                                        tx_data(BITS downto 0) <= port2_rd_count;

                                elsif bar_addr = 25 then
                                        tx_data(BITS downto 0) <= port2_wr_count;

                                elsif bar_addr = 26 then
                                        tx_data(BITS downto 0) <= port3_rd_count;

                                elsif bar_addr = 27 then
                                        tx_data(BITS downto 0) <= port3_wr_count;

                                -- read mdio
                                elsif bar_addr = 30 then
                                        tx_data(15 downto 0) <= mdio_data;
                                end if;

                        elsif state = state_tx_complete then
                                if tx_done = '1' then
                                        state <= state_idle;
                                end if;

                        -- bar write
                        elsif state = state_idle and rx_write = '1' then
                                bar_addr := unsigned(rx_address(BITS downto 0));

                                -- write fifo
                                if bar_addr = 0 then
                                        if fifo_full = '0' then
                                                fifo_din <= rx_data;
                                                fifo_write <= '1';
                                        end if;

                                -- write command
                                elsif bar_addr = 1 then
                                        if cmd_full = '0' then
                                                cmd_din <= rx_data;
                                                cmd_write <= '1';
                                        end if;

                                        if rx_data(0) = '1' then
                                                state <= state_command;
                                        end if;

                                -- write test
                                elsif bar_addr = 2 then
                                        dma_addr := unsigned(rx_data(31 downto 3));
                                        length := unsigned(rx_data(BITS+32 downto 32));
                                        cycles := unsigned(rx_data(BITS+48 downto 48));
                                        backup_addr := dma_addr;
                                        backup_length := length;

                                        if rx_data(1) = '0' then
                                                state <= state_test_read_head;
                                        else
                                                state <= state_test_write_head;
                                        end if;

                                -- write ports
                                elsif bar_addr = 10 then
                                        if port0_wr_full = '0' then
                                                port0_wr_data <= rx_data;
                                                port0_write <= '1';
                                        end if;

                                elsif bar_addr = 11 then
                                        if port1_wr_full = '0' then
                                                port1_wr_data <= rx_data;
                                                port1_write <= '1';
                                        end if;

                                elsif bar_addr = 12 then
                                        if port2_wr_full = '0' then
                                                port2_wr_data <= rx_data;
                                                port2_write <= '1';
                                        end if;

                                elsif bar_addr = 13 then
                                        if port3_wr_full = '0' then
                                                port3_wr_data <= rx_data;
                                                port3_write <= '1';
                                        end if;

                                -- write mdio
                                elsif bar_addr = 30 then
                                        state <= state_mdio;
                                        mdio_enable <= '1';
                                        mdio_command <= rx_data(27 downto 0);
                                end if;

                        -- mdio
                        elsif state = state_mdio then
                                if mdio_done = '1' then
                                        mdio_enable <= '0';
                                        state <= state_interrupt;
                                        interrupt <= '1';
                                end if;

                        -- test read
                        elsif state = state_test_read_head then
                                remain := 16 - resize(dma_addr(3 downto 0), remain'length);

                                if max_read = "001" then
                                        remain := 32 - resize(dma_addr(4 downto 0), remain'length);
                                elsif max_read = "010" then
                                        remain := 64 - resize(dma_addr(5 downto 0), remain'length);
                                elsif max_read = "011" then
                                        remain := 128 - resize(dma_addr(6 downto 0), remain'length);
                                elsif max_read = "100" then
                                        remain := 256 - resize(dma_addr(7 downto 0), remain'length);
                                elsif max_read = "101" then
                                        remain := 512 - resize(dma_addr(8 downto 0), remain'length);
                                end if;

                                if remain > length then
                                        remain := length;
                                end if;

                                state <= state_test_read;
                                tx_read <= '1';
                                tx_address <= std_logic_vector(dma_addr);
                                tx_length <= std_logic_vector(remain(8 downto 0));
                                length := length - remain;
                                dma_addr := dma_addr + remain;

                        elsif state = state_test_read then
                                if tx_done = '1' then
                                        state <= state_test_complete;
                                end if;

                        elsif state = state_test_complete then
                                if rx_complete = '1' then
                                        test_completion <= rx_data;
                                        remain := remain - 1;

                                        if remain = 0 then
                                                if length > 0 then
                                                        state <= state_test_read_head;

                                                elsif cycles > 1 then
                                                        state <= state_test_read_head;
                                                        dma_addr := backup_addr;
                                                        length := backup_length;
                                                        cycles := cycles - 1;
                                                else
                                                        state <= state_interrupt;
                                                        interrupt <= '1';
                                                end if;
                                        end if;
                                end if;

                        -- test write
                        elsif state = state_test_write_head then
                                remain := 16 - resize(dma_addr(3 downto 0), remain'length);

                                if max_write = "001" then
                                        remain := 32 - resize(dma_addr(4 downto 0), remain'length);
                                elsif max_write = "010" then
                                        remain := 64 - resize(dma_addr(5 downto 0), remain'length);
                                elsif max_write = "011" then
                                        remain := 128 - resize(dma_addr(6 downto 0), remain'length);
                                elsif max_write = "100" then
                                        remain := 256 - resize(dma_addr(7 downto 0), remain'length);
                                elsif max_write = "101" then
                                        remain := 512 - resize(dma_addr(8 downto 0), remain'length);
                                end if;

                                if remain > length then
                                        remain := length;
                                end if;

                                state <= state_test_write;
                                tx_write <= '1';
                                tx_data <= x"FFEEDDCC00000000";
                                tx_address <= std_logic_vector(dma_addr);
                                tx_length <= std_logic_vector(remain(8 downto 0));
                                length := length - remain;
                                dma_addr := dma_addr + remain;

                        elsif state = state_test_write then
                                if tx_done = '1' then
                                        if length > 0 then
                                                state <= state_test_write_head;

                                        elsif cycles > 1 then
                                                state <= state_test_write_head;
                                                dma_addr := backup_addr;
                                                length := backup_length;
                                                cycles := cycles - 1;
                                        else
                                                state <= state_interrupt;
                                                interrupt <= '1';
                                        end if;
                                end if;

                        -- command
                        elsif state = state_command then
                                if cmd_empty = '1' then
                                        state <= state_interrupt;
                                        interrupt <= '1';
                                else
                                        cmd_read <= '1';

                                        if cmd_dout(1) = '0' then
                                                state <= state_dma_read;
                                                dma_addr := unsigned(cmd_dout(31 downto 3));
                                                length := unsigned(cmd_dout(BITS+32 downto 32));
                                                fifo_din <= std_logic_vector(resize(length, fifo_din'length));
                                                fifo_write <= '1';
                                        else
                                                state <= state_dma_write;
                                                dma_addr := unsigned(cmd_dout(31 downto 3));
                                                length := unsigned(fifo_dout(BITS downto 0)) + 1;
                                        end if;
                                end if;

                        elsif state = state_interrupt then
                                if interrupt_rdy = '1' then
                                        state <= state_idle;
                                        interrupt <= '0';
                                end if;

                        -- dma read
                        elsif state = state_dma_read then
                                remain := 16 - resize(dma_addr(3 downto 0), remain'length);

                                if max_read = "001" then
                                        remain := 32 - resize(dma_addr(4 downto 0), remain'length);
                                elsif max_read = "010" then
                                        remain := 64 - resize(dma_addr(5 downto 0), remain'length);
                                elsif max_read = "011" then
                                        remain := 128 - resize(dma_addr(6 downto 0), remain'length);
                                elsif max_read = "100" then
                                        remain := 256 - resize(dma_addr(7 downto 0), remain'length);
                                elsif max_read = "101" then
                                        remain := 512 - resize(dma_addr(8 downto 0), remain'length);
                                end if;

                                if remain > length then
                                        remain := length;
                                end if;

                                state <= state_tx_read;
                                tx_read <= '1';
                                tx_address <= std_logic_vector(dma_addr);
                                tx_length <= std_logic_vector(remain(8 downto 0));
                                length := length - remain;
                                dma_addr := dma_addr + remain;

                        elsif state = state_tx_read then
                                if tx_done = '1' then
                                        state <= state_rx_complete;
                                end if;

                        elsif state = state_rx_complete then
                                if rx_complete = '1' then
                                        fifo_din <= rx_data;
                                        fifo_write <= '1';
                                        remain := remain - 1;

                                        if remain = 0 then
                                                if length > 0 then
                                                        state <= state_dma_read;
                                                else
                                                        state <= state_command;
                                                end if;
                                        end if;
                                end if;

                        -- dma write
                        elsif state = state_dma_write then
                                remain := 16 - resize(dma_addr(3 downto 0), remain'length);

                                if max_write = "001" then
                                        remain := 32 - resize(dma_addr(4 downto 0), remain'length);
                                elsif max_write = "010" then
                                        remain := 64 - resize(dma_addr(5 downto 0), remain'length);
                                elsif max_write = "011" then
                                        remain := 128 - resize(dma_addr(6 downto 0), remain'length);
                                elsif max_write = "100" then
                                        remain := 256 - resize(dma_addr(7 downto 0), remain'length);
                                elsif max_write = "101" then
                                        remain := 512 - resize(dma_addr(8 downto 0), remain'length);
                                end if;

                                if remain > length then
                                        remain := length;
                                end if;

                                state <= state_tx_write;
                                tx_write <= '1';
                                tx_data <= fifo_dout;
                                tx_address <= std_logic_vector(dma_addr);
                                tx_length <= std_logic_vector(remain(8 downto 0));
                                fifo_read <= '1';
                                length := length - remain;
                                dma_addr := dma_addr + remain;
                                remain := remain - 1;

                        elsif state = state_tx_write then
                                if tx_done = '1' then
                                        if length > 0 then
                                                state <= state_dma_write;
                                        else
                                                state <= state_command;
                                        end if;

                                elsif tx_accept = '1' and remain > 0 then
                                        tx_data <= fifo_dout;
                                        fifo_read <= '1';
                                        remain := remain - 1;
                                end if;
                        end if;

                        if tx_done = '1' then
                                tx_read <= '0';
                                tx_write <= '0';
                                tx_complete <= '0';
                                tx_data <= (others => '0');
                                tx_address <= (others => '0');
                                tx_length <= (others => '0');
                        end if;
                end if;
        end process;

        fifo : entity work.port_fifo port map (
                reset => reset,

                rd_clk => clk,
                rd_read => fifo_read,
                rd_data => fifo_dout,
                rd_empty => fifo_empty,
                rd_count => fifo_count,

                wr_write => fifo_write,
                wr_data => fifo_din,
                wr_full => fifo_full
                --wr_count => wr_count
        );

        commands : cmd_fifo port map (
                clk => clk,
                rst => reset,
                rd_en => cmd_read,
                wr_en => cmd_write,
                din => cmd_din,
                dout => cmd_dout,
                full => cmd_full,
                empty => cmd_empty
        );

        leds <= "000" when state = state_idle else
                -- bar read
                "001" when state = state_tx_complete else
                -- dma read
                "010" when state = state_dma_read or state = state_tx_read or state = state_rx_complete else
                -- dma write
                "011" when state = state_dma_write or state = state_tx_write else
                -- mdio
                "100" when state = state_mdio else
                -- interrupt
                "101" when state = state_interrupt else
                -- other
                "111";
end arch;