summaryrefslogtreecommitdiff
path: root/netfpga10g/hdl/pcie_tx.vhd
blob: dc892b4101949a4a7023d9434a5c5a8ae05b6102 (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

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

entity pcie_tx is port (
        clk : in std_logic;

        frame : out std_logic_vector(63 downto 0) := (others => '0');
        sof : out std_logic := '0';
        eof : out std_logic := '0';
        half : out std_logic := '0';
        valid : out std_logic := '0';
        ready : in std_logic;

        read : in std_logic;
        write : in std_logic;
        complete : in std_logic;
        data : in std_logic_vector(63 downto 0);
        address : in std_logic_vector(63 downto 3);
        length : in std_logic_vector(8 downto 0);
        tag : in std_logic_vector(4 downto 0);
        local : in std_logic_vector(15 downto 0);
        remote : in std_logic_vector(23 downto 0);
        accept : out std_logic;
        done : out std_logic
);
end pcie_tx;

architecture arch of pcie_tx is
        type state_t is (state_idle, state_read_32, state_read_64,
                         state_write_32, state_write_64,
                         state_write_end_32, state_write_head_64,
                         state_complete, state_complete_head, state_done);
        signal state : state_t := state_idle;
begin
        accept <= ready when state = state_write_32 or state = state_write_64 else '0';
        done <= ready when state = state_done else '0';

        process (clk)
                variable extra : std_logic_vector(31 downto 0) := (others => '0');
                variable remain : unsigned(8 downto 0) := (others => '0');
        begin
                if rising_edge(clk) and (ready = '1' or state = state_idle) then
                        frame <= (others => '0');
                        sof <= '0';
                        eof <= '0';
                        half <= '0';
                        valid <= '0';

                        -- read
                        if state = state_idle and read = '1' then
                                if unsigned(length) = 0 then
                                        state <= state_done;
                                else
                                        if address(63 downto 32) = x"00000000" then
                                                state <= state_read_32;
                                                frame <= "0" & "00" & "00000" & x"00" &
                                                         "000000" &
                                                         length & "0" &
                                                         local &
                                                         "000" & tag & x"FF";
                                        else
                                                state <= state_read_64;
                                                frame <= "0" & "01" & "00000" & x"00" &
                                                         "000000" &
                                                         length & "0" &
                                                         local &
                                                         "000" & tag & x"FF";
                                        end if;
                                        sof <= '1';
                                        valid <= '1';
                                end if;

                        elsif state = state_read_32 then
                                state <= state_done;
                                frame <= address(31 downto 3) & "000" & x"00000000";
                                eof <= '1';
                                half <= '1';
                                valid <= '1';

                        elsif state = state_read_64 then
                                state <= state_done;
                                frame <= address & "000";
                                eof <= '1';
                                valid <= '1';

                        -- write
                        elsif state = state_idle and write = '1' then
                                if unsigned(length) = 0 then
                                        state <= state_done;
                                else
                                        if address(63 downto 32) = x"00000000" then
                                                state <= state_write_32;
                                                frame <= "0" & "10" & "00000" & x"00" &
                                                         "000000" &
                                                         length & "0" &
                                                         local &
                                                         x"00" & x"FF";
                                                extra := address(31 downto 3) & "000";
                                        else
                                                state <= state_write_head_64;
                                                frame <= "0" & "11" & "00000" & x"00" &
                                                         "000000" &
                                                         length & "0" &
                                                         local &
                                                         x"00" & x"FF";
                                        end if;
                                        sof <= '1';
                                        valid <= '1';
                                        remain := unsigned(length);
                                end if;

                        elsif state = state_write_32 then
                                frame <= extra & data(63 downto 32);
                                extra := data(31 downto 0);
                                valid <= '1';

                                remain := remain - 1;

                                if remain = 0 then
                                        state <= state_write_end_32;
                                end if;

                        elsif state = state_write_end_32 then
                                state <= state_done;
                                frame <= extra & x"00000000";
                                eof <= '1';
                                half <= '1';
                                valid <= '1';

                        elsif state = state_write_head_64 then
                                state <= state_write_64;
                                frame <= address & "000";
                                valid <= '1';

                        elsif state = state_write_64 then
                                frame <= data;
                                valid <= '1';

                                remain := remain - 1;

                                if remain = 0 then
                                        state <= state_done;
                                        eof <= '1';
                                end if;

                        -- complete
                        elsif state = state_idle and complete = '1' then
                                state <= state_complete_head;
                                frame <= "0" & "10" & "01010" & x"00" &
                                         x"0002" &
                                         local &
                                         x"0008";
                                sof <= '1';
                                valid <= '1';

                        elsif state = state_complete_head then
                                state <= state_complete;
                                frame <= remote & "0" & address(6 downto 3) & "000" &
                                         data(63 downto 32);
                                valid <= '1';

                        elsif state = state_complete then
                                state <= state_done;
                                frame <= data(31 downto 0) & x"00000000";
                                eof <= '1';
                                half <= '1';
                                valid <= '1';

                        -- done
                        elsif state = state_done then
                                state <= state_idle;
                        end if;
                end if;
        end process;
end arch;