-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHCP.v
146 lines (115 loc) · 4.06 KB
/
DHCP.v
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
//
// HPSDR - High Performance Software Defined Radio
//
// Metis code.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// DHCP - copyright 2010, 2011, 2012 Phil Harman VK6APH
/*
change log:
*/
// sequencer for DHCP - state machine to sequence DHCP discover, offer, request and ACK/NAK
/*
+--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+
clock --+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+
+-----+
DHCP_start-------+ +---------------------------------------------------------------
+-----+
DHCP_discover -------+ +--------------------------------------------------------------- sent to Tx_MAC
+---------------------
DHCP_offer -----------------//-----+ from Rx_MAC
+-----+
DHCP_request --------------------------+ +----------------------------------------- sent to Tx_MAC
+---------------
DHCP_ACK ----------------------------------//-----+ from Rx_MAC
OR ---------+ +--------------------
time_out +-----------------------------------------//------------------------+
The code loops waiting for DHCP_start from Metis. It then sets DHCP_discover high which causes
the TX_MAC send a discovery request. Then loops waiting for DHCP_offer, from Rx_MAC, or a time out.
It then sets DHCP_request high which causes TX_MAC to send a request. Then loops waiting for an
DHCP_ACK from Rx_MAC or a time out.
Code then loops back to the start.
*/
module DHCP (clock, DHCP_start, DHCP_renew, DHCP_discover, DHCP_offer, time_out, DHCP_request, DHCP_ACK);
input clock;
input DHCP_start;
input DHCP_renew;
input DHCP_offer;
input DHCP_ACK;
output DHCP_discover;
output DHCP_request;
output time_out;
reg [3:0]DHCP_state;
reg DHCP_discover;
reg DHCP_request;
reg [24:0]time_count;
reg time_out; // set when DHCP times out
// NOTE: If set, then leave time_out flag set until we get the next discover or renew request
always @ (posedge clock) // Tx is on negedge Tx_clock_2
begin
case(DHCP_state)
0: begin
time_count <= 0; // loop until we see a DHCP start signal
if (DHCP_start) begin
DHCP_discover <= 1'b1;
time_out <= 0; // reset time out flag
DHCP_state <= 1;
end
else if (DHCP_renew) begin // send a DHCP request
time_out <= 0;
DHCP_state <= 2;
end
else DHCP_state <= 0;
end
1: begin // loop until we see a DHCP offer, time out of not found
DHCP_discover <= 0;
if (DHCP_offer)
DHCP_state <= 2;
else begin
if (time_count == 25000000) begin
time_out <= 1'b1;
time_count <= 0;
DHCP_state <= 0;
end
else begin
time_count <= time_count + 1'b1;
DHCP_state <= 1;
end
end
end
2: begin
DHCP_request <= 1'b1; // accept offered IP address
DHCP_state <= 3;
end
3: begin
DHCP_request <= 0; // loop until we see a DHCP ACK or NAK, time out if not found
if (DHCP_ACK)
DHCP_state <= 0;
else begin
if (time_count == 25000000) begin
time_count <= 0;
time_out <= 1'b1;
DHCP_state <= 0;
end
else begin
time_count <= time_count + 1'b1;
DHCP_state <= 3;
end
end
end
default: DHCP_state <= 0;
endcase
end
endmodule