| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| module scanner ( |
| input clk, |
| input [7:0] eth_in, |
| input eth_in_s, |
| input eth_in_e, |
| |
| |
| |
| |
| |
| |
| |
| |
| input enable_rx, |
| |
| output [3:0] ip_a, |
| input [7:0] ip_d, |
| |
| output [3:0] pno_a, |
| input [7:0] pno_d, |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| output busy, |
| output keep, |
| output [3:0] debug, |
| |
| |
| |
| output [7:0] odata, |
| output odata_s, |
| output odata_f, |
| output status_valid, |
| output [7:0] status_vec, |
| output [10:0] pack_len |
| ); |
| |
| parameter handle_arp = 1; |
| parameter handle_icmp = 1; |
|
|
| |
| wire enable_rx_r; |
| reg_tech_cdc enable_rx_cdc(.I(enable_rx), .C(clk), .O(enable_rx_r)); |
|
|
| |
| wire [7:0] eth_octet = eth_in; |
| wire eth_strobe = eth_in_s; |
| |
| reg h_idle=1, h_preamble=0, h_data=0, h_drop=0; |
| |
| |
| |
| |
| |
| wire drop_packet; |
| reg [3:0] ifg_count=0; |
| wire ifg_inc = ~(&ifg_count[3:2]); |
| wire ifg_ok = ifg_count >= 10; |
| |
| always @(posedge clk) begin |
| if (h_idle | h_preamble) ifg_count <= ifg_count + ifg_inc; |
| else ifg_count <= 0; |
| if (h_idle & eth_strobe) begin |
| h_idle <= 0; |
| if (eth_octet==8'h55 && enable_rx_r) h_preamble <= 1; |
| else h_drop <= 1; |
| end |
| if (h_preamble) begin |
| if (eth_strobe & (eth_octet==8'hd5)) begin |
| h_preamble <= 0; |
| if (ifg_ok) h_data <= 1; |
| else h_drop <= 1; |
| end else if (eth_strobe & (eth_octet!=8'h55)) begin |
| h_preamble <= 0; h_drop <= 1; |
| end else if (~eth_strobe) begin |
| h_preamble <= 0; h_idle <= 1; |
| end |
| end |
| if (h_data) begin |
| if (~eth_strobe) begin |
| h_data <= 0; h_idle <= 1; |
| end else if (drop_packet) begin |
| h_data <= 0; h_drop <= 1; |
| end |
| end |
| if (h_drop & ~eth_strobe) begin |
| h_drop <= 0; h_idle <= 1; |
| end |
| end |
|
|
| |
| reg [1:0] debug1_r=0; |
| always @(posedge clk) begin |
| if (h_idle) debug1_r <= 0; |
| if (h_preamble) debug1_r <= 1; |
| if (h_data) debug1_r <= 3; |
| if (h_drop) debug1_r <= 2; |
| end |
|
|
| |
| |
| reg h_data_d1=0, h_data_d2=0, data_first=0; |
| reg [7:0] data_d1=0, data_d2=0; |
| always @(posedge clk) begin |
| h_data_d1 <= h_data; |
| h_data_d2 <= h_data & h_data_d1; |
| |
| data_first <= h_data & ~h_data_d1; |
| data_d1 <= eth_strobe ? eth_octet : 8'b0; |
| data_d2 <= data_d1; |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| reg [10:0] pack_cnt=0; |
| always @(posedge clk) begin |
| pack_cnt <= h_data ? pack_cnt+1 : 0; |
| end |
| assign drop_packet = pack_cnt >= 1536; |
| assign ip_a = pack_cnt > 36 || pack_cnt < 16 ? pack_cnt[3:0] : pack_cnt[3:0]+8; |
| wire ip_m = ip_d == data_d1; |
| |
| |
| reg want_c_eth=0, want_c_arp=0, want_c_ip=0; |
| always @(posedge clk) begin |
| want_c_eth <= pack_cnt < 6; |
| want_c_arp <= pack_cnt >= 38 && pack_cnt < 42; |
| want_c_ip <= pack_cnt >= 30 && pack_cnt < 34; |
| end |
| |
| wire pz = pack_cnt == 0; |
| reg pass_ethmac=0; always @(posedge clk) begin if (want_c_eth & ~ip_m) pass_ethmac <= 0; if (pz) pass_ethmac <= 1; end |
| reg pass_arpip=0; always @(posedge clk) begin if (want_c_arp & ~ip_m) pass_arpip <= 0; if (pz) pass_arpip <= 1; end |
| reg pass_ipdst=0; always @(posedge clk) begin if (want_c_ip & ~ip_m) pass_ipdst <= 0; if (pz) pass_ipdst <= 1; end |
|
|
| |
| wire [15:0] ip_length, udp_length; |
|
|
| |
| wire pass_arp0; |
| generate if (handle_arp) begin : find_arp |
| arp_patt arp_p (.clk(clk), .cnt(pack_cnt), .data(data_d1), .pass(pass_arp0)); |
| end else begin : no_find_arp |
| assign pass_arp0 = 0; |
| end endgenerate |
|
|
| |
| wire pass_icmp0; |
| generate if (handle_icmp) begin : find_icmp |
| icmp_patt icmp_p(.clk(clk), .cnt(pack_cnt), .data(data_d1), .pass(pass_icmp0)); |
| end else begin : no_find_icmp |
| assign pass_icmp0 = 0; |
| end endgenerate |
|
|
| |
| wire pass_ip0; ip_patt ip_p (.clk(clk), .cnt(pack_cnt), .data(data_d1), .pass(pass_ip0), .length(ip_length)); |
| wire pass_udp0; udp_patt udp_p (.clk(clk), .cnt(pack_cnt), .data(data_d1), .pass(pass_udp0), .length(udp_length)); |
| wire pass_sum; cksum_chk chk_p (.clk(clk), .cnt(pack_cnt), .data(data_d1), .pass(pass_sum), .length(ip_length)); |
|
|
| |
| wire crc_zero; |
| crc8e_guts crc8e(.clk(clk), .gate(h_data_d1), .first(data_first), |
| .d_in(data_d1), .zero(crc_zero)); |
| wire final_octet = h_data_d1 & ~h_data; |
|
|
| |
| reg udp_port_stb=0; |
| always @(posedge clk) udp_port_stb <= pack_cnt == 36; |
| wire [2:0] port_p0; wire port_h, port_v; |
| udp_port_cam #(.naw(3)) cam(.clk(clk), |
| .port_s(udp_port_stb), .data(data_d1), |
| .pno_a(pno_a), .pno_d(pno_d), |
| .port_p(port_p0), .port_h(port_h), .port_v(port_v) |
| ); |
|
|
| |
| reg [10:0] pack_len_r=0; |
| always @(posedge clk) if (h_data) pack_len_r <= pack_cnt; |
|
|
| |
| reg ip_len_check=0, udp_len_check=0; |
| always @(posedge clk) if (h_data) begin |
| ip_len_check <= pack_cnt >= ip_length + 18; |
| udp_len_check <= ip_length >= udp_length + 20; |
| end |
|
|
| |
| reg unicast_src_mac=0; |
| always @(posedge clk) begin |
| if (pack_cnt==1) unicast_src_mac <= 1; |
| if (pack_cnt==7) unicast_src_mac <= ~data_d1[0]; |
| end |
|
|
| |
| wire pass_arp = unicast_src_mac & crc_zero & pass_arp0 & pass_arpip; |
| wire pass_ip = unicast_src_mac & crc_zero & pass_ethmac & pass_ip0 & pass_ipdst & ip_len_check; |
| wire pass_icmp = pass_ip & pass_icmp0 & pass_sum; |
| wire pass_udp = pass_ip & pass_udp0 & udp_len_check & port_h; |
| wire [1:0] category = pass_udp ? 3 : pass_icmp ? 2 : pass_arp ? 1 : 0; |
| wire [2:0] port_p = pass_udp ? port_p0 : 3'd0; |
|
|
| |
| assign status_vec = {port_p, pass_ip, pass_ethmac, crc_zero, category}; |
| assign status_valid = final_octet; |
| assign pack_len = pack_len_r; |
|
|
| |
| wire busy_with_arp = unicast_src_mac & pass_arp0; |
| wire busy_with_udp = unicast_src_mac & pass_ethmac & pass_ip0 & pass_udp0; |
| wire busy_with_icmp = unicast_src_mac & pass_ethmac & pass_ip0 & pass_icmp0; |
| reg busy_r=0, keep_r=0; |
| always @(posedge clk) begin |
| busy_r <= odata_s & (busy_with_arp | busy_with_udp | busy_with_icmp); |
| keep_r <= odata_s & (category != 0); |
| end |
| assign busy = busy_r; |
| assign keep = keep_r; |
|
|
| |
| reg [1:0] debug2_r=0; |
| always @(posedge clk) begin |
| if (status_valid) debug2_r <= category; |
| end |
| assign debug = {debug2_r, debug1_r}; |
|
|
| |
| assign odata = data_d2; |
| assign odata_s = h_data_d2; |
| assign odata_f = final_octet; |
|
|
| endmodule |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| module cksum_chk( |
| input clk, |
| input [10:0] cnt, |
| input [7:0] data, |
| input [15:0] length, |
| output pass |
| ); |
|
|
| wire chksum_zero = cnt == 22; |
| wire icmp_gate = cnt >= 35; |
| |
| wire chksum_gate = icmp_gate; |
|
|
| |
| wire end_of_ip = cnt>32 && cnt == (length+14); |
|
|
| |
| wire ones; |
| ones_chksum ck(.clk(clk), .clear(chksum_zero), .gate(chksum_gate), |
| .din(data), .all_ones(ones)); |
|
|
| |
| reg eof=0, state=0; |
| always @(posedge clk) begin |
| if (chksum_zero) state <= 0; |
| if (end_of_ip) state <= ones; |
| eof <= end_of_ip; |
| if (eof) state <= state & ones; |
| end |
| assign pass = state; |
|
|
| endmodule |
|
|
| |
| |
| module arp_patt( |
| input clk, |
| input [10:0] cnt, |
| input [7:0] data, |
| output pass |
| ); |
|
|
| reg [7:0] template=0; |
| always @(posedge clk) case(cnt[3:0]) |
| |
| |
| 4'd12: template <= 8'h08; |
| 4'd13: template <= 8'h06; |
| 4'd14: template <= 8'h00; |
| 4'd15: template <= 8'h01; |
| 4'd00: template <= 8'h08; |
| 4'd01: template <= 8'h00; |
| 4'd02: template <= 8'h06; |
| 4'd03: template <= 8'h04; |
| 4'd04: template <= 8'h00; |
| 4'd05: template <= 8'h01; |
| default: template <= 8'h00; |
| endcase |
|
|
| reg want=0, pass_r=0; |
| wire match = data == template; |
| always @(posedge clk) begin |
| want <= cnt >= 12 && cnt < 22; |
| if (cnt == 0) pass_r <= 1; |
| if (want & ~match) pass_r <= 0; |
| end |
| assign pass = pass_r; |
|
|
| endmodule |
|
|
| |
| |
| module ip_patt( |
| input clk, |
| input [10:0] cnt, |
| input [7:0] data, |
| output pass, |
| output [15:0] length |
| ); |
|
|
| reg [7:0] template=0; |
| always @(posedge clk) case(cnt[4:0]) |
| |
| |
| 5'd12: template <= 8'h08; |
| 5'd13: template <= 8'h00; |
|
|
| |
| 5'd14: template <= 8'h45; |
| |
| |
| |
| |
| |
| 5'd20: template <= 8'h00; |
| 5'd21: template <= 8'h00; |
| |
| |
| |
| default: template <= 8'h00; |
| endcase |
|
|
| |
| reg ttl_flag=0; |
| wire zero_ttl = ttl_flag & ~(|data); |
|
|
| reg want=0, mask_df_bit=0, pass_r=0; |
| wire [7:0] df_mask = {1'b1, ~mask_df_bit, 6'h3f}; |
| wire match = (data&df_mask) == template; |
| always @(posedge clk) begin |
| want <= cnt >= 12 && cnt < 15 || cnt >= 20 && cnt < 22; |
| mask_df_bit <= cnt == 20; |
| ttl_flag <= cnt == 22; |
| if (cnt == 0) pass_r <= 1; |
| if (want & ~match) pass_r <= 0; |
| if (zero_ttl) pass_r <= 0; |
| end |
|
|
| |
| |
| |
| reg [7:0] data_d=0; |
| reg [15:0] length_r=0; |
| always @(posedge clk) begin |
| data_d <= data; |
| if (cnt==18) length_r <= {data_d, data}; |
| end |
| assign length = length_r; |
|
|
| |
| reg out_chksum_gate=0, out_chksum_zero=0; |
| always @(posedge clk) begin |
| out_chksum_gate <= cnt >= 14 && cnt < 34; |
| out_chksum_zero <= cnt == 0; |
| end |
| wire chksum_all_ones; |
| ones_chksum ck(.clk(clk), .clear(out_chksum_zero), .gate(out_chksum_gate), |
| .din(data), .all_ones(chksum_all_ones)); |
| reg chksum_all_ones_d=0; |
| reg chksum_fail=0; |
| always @(posedge clk) begin |
| chksum_all_ones_d <= chksum_all_ones; |
| if (cnt==0) chksum_fail <= 0; |
| if (cnt==35) chksum_fail <= ~chksum_all_ones | ~chksum_all_ones_d; |
| end |
| assign pass = pass_r & ~chksum_fail; |
|
|
| endmodule |
|
|
| |
| |
| |
| module icmp_patt( |
| input clk, |
| input [10:0] cnt, |
| input [7:0] data, |
| output pass |
| ); |
|
|
| reg [7:0] template=0; |
| always @(posedge clk) case(cnt[2:0]) |
| |
| |
| |
| 3'd7: template <= 8'h01; |
| 3'd2: template <= 8'h08; |
| 3'd3: template <= 8'h00; |
| default: template <= 8'h00; |
| endcase |
|
|
| reg want=0, pass_r=0; |
| wire match = data == template; |
| always @(posedge clk) begin |
| want <= cnt == 23 || cnt==34 || cnt==35; |
| if (cnt == 0) pass_r <= 1; |
| if (want & ~match) pass_r <= 0; |
| end |
| assign pass = pass_r; |
|
|
| endmodule |
|
|
| |
| |
| module udp_patt( |
| input clk, |
| input [10:0] cnt, |
| input [7:0] data, |
| output pass, |
| output [15:0] length |
| ); |
|
|
| |
| |
| wire [7:0] template = 8'h11; |
|
|
| |
| |
| |
| wire reject_low = cnt==35 & ~|data[7:2]; |
|
|
| |
| reg pzero_d=0, pzero_d1=0, pzero_t=0; |
| always @(posedge clk) begin |
| pzero_d <= data == 8'h00; |
| pzero_d1 <= pzero_d; |
| pzero_t <= cnt == 36; |
| end |
| wire discard_port0 = pzero_t & pzero_d & pzero_d1; |
|
|
| reg want=0, pass_r=0; |
| wire match = data == template; |
| always @(posedge clk) begin |
| want <= cnt == 23; |
| if (cnt == 0) pass_r <= 1; |
| if (want & ~match) pass_r <= 0; |
| if (reject_low) pass_r <= 0; |
| if (discard_port0) pass_r <= 0; |
| end |
| assign pass = pass_r; |
|
|
| |
| |
| |
| reg [7:0] data_d=0; |
| reg [15:0] length_r=0; |
| always @(posedge clk) begin |
| data_d <= data; |
| if (cnt==40) length_r <= {data_d, data}; |
| end |
| assign length = length_r; |
|
|
| |
| |
|
|
| endmodule |
|
|