// IPv4 Header Parser - Reference Solution
// Extracts key fields from a 160-bit (20-byte) IPv4 header
// Purely combinational logic (wire slicing + comparison)

module ipv4_parser (
    input  wire [159:0] header,   // Raw IPv4 header (MSB first, network byte order)
    output wire         valid,    // High if Version=4 and IHL=5
    output wire [15:0]  total_length,
    output wire [7:0]   ttl,
    output wire [7:0]   protocol,
    output wire [31:0]  src_ip,
    output wire [31:0]  dst_ip
);

    // IPv4 Header Layout (160 bits = 20 bytes, MSB first):
    // Bits 159-156: Version (4 bits)      - should be 4
    // Bits 155-152: IHL (4 bits)          - should be 5 (no options)
    // Bits 151-144: Type of Service       - ignored
    // Bits 143-128: Total Length (16 bits)
    // Bits 127-112: Identification        - ignored
    // Bits 111-96:  Flags + Fragment      - ignored
    // Bits 95-88:   TTL (8 bits)
    // Bits 87-80:   Protocol (8 bits)
    // Bits 79-64:   Header Checksum       - ignored
    // Bits 63-32:   Source IP (32 bits)
    // Bits 31-0:    Destination IP (32 bits)
    
    // Extract Version and IHL
    wire [3:0] version = header[159:156];
    wire [3:0] ihl     = header[155:152];
    
    // Validity check: Version must be 4, IHL must be 5
    assign valid = (version == 4'd4) && (ihl == 4'd5);
    
    // Extract Total Length (bytes 2-3, bits 143-128)
    assign total_length = header[143:128];
    
    // Extract TTL (byte 8, bits 95-88)
    assign ttl = header[95:88];
    
    // Extract Protocol (byte 9, bits 87-80)
    assign protocol = header[87:80];
    
    // Extract Source IP (bytes 12-15, bits 63-32)
    assign src_ip = header[63:32];
    
    // Extract Destination IP (bytes 16-19, bits 31-0)
    assign dst_ip = header[31:0];

endmodule
