From 43910d1c96ff3692880b02669f0943bca9ecb7a7 Mon Sep 17 00:00:00 2001
From: Reinier van der Walle <walle@astron.nl>
Date: Thu, 24 Feb 2022 16:20:04 +0100
Subject: [PATCH] added icmp checksum calculation

---
 libraries/io/eth/src/vhdl/eth_control.vhd | 2 ++
 libraries/io/eth/tb/vhdl/tb_eth.vhd       | 6 +++++-
 libraries/io/eth1g/tb/vhdl/tb_eth1g.vhd   | 6 +++++-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/libraries/io/eth/src/vhdl/eth_control.vhd b/libraries/io/eth/src/vhdl/eth_control.vhd
index c9f05abf52..7efb2fa35a 100644
--- a/libraries/io/eth/src/vhdl/eth_control.vhd
+++ b/libraries/io/eth/src/vhdl/eth_control.vhd
@@ -187,6 +187,8 @@ BEGIN
       nxt_hdr_response_arr <= func_network_total_header_response_arp( rcv_hdr_words_arr, reg_config.mac_address, reg_config.ip_address);
     ELSIF rcv_hdr_status.is_icmp='1' THEN
       nxt_hdr_response_arr <= func_network_total_header_response_icmp(rcv_hdr_words_arr, reg_config.mac_address);
+      -- Calculate icmp checksum = original checksum + 0x0800.
+      nxt_hdr_response_arr(9)(c_halfword_w-1 DOWNTO 0) <= TO_UVEC( 2048 + TO_UINT(rcv_hdr_words_arr(9)(c_halfword_w-1 DOWNTO 0)), c_halfword_w);
     ELSIF rcv_hdr_status.is_udp='1' THEN
       nxt_hdr_response_arr <= func_network_total_header_response_udp( rcv_hdr_words_arr, reg_config.mac_address);
     ELSIF rcv_hdr_status.is_ip='1' THEN
diff --git a/libraries/io/eth/tb/vhdl/tb_eth.vhd b/libraries/io/eth/tb/vhdl/tb_eth.vhd
index 22387c22d9..8274212a95 100644
--- a/libraries/io/eth/tb/vhdl/tb_eth.vhd
+++ b/libraries/io/eth/tb/vhdl/tb_eth.vhd
@@ -181,9 +181,13 @@ ARCHITECTURE tb OF tb_eth IS
                                                              checksum => TO_UVEC(c_network_icmp_checksum,         c_network_icmp_checksum_w),  -- init value
                                                              id       => TO_UVEC(c_network_icmp_id,               c_network_icmp_id_w),
                                                              sequence => TO_UVEC(c_network_icmp_sequence,         c_network_icmp_sequence_w));
+
+  -- checksum is 0x0800 + original checksum
+  CONSTANT c_exp_icmp_checksum   : STD_LOGIC_VECTOR(c_network_icmp_checksum_w-1 DOWNTO 0) := TO_UVEC( 2048 + TO_UINT(c_tx_icmp_header.checksum), c_network_icmp_checksum_w);
+
   CONSTANT c_exp_icmp_header     : t_network_icmp_header := (msg_type => TO_UVEC(c_network_icmp_msg_type_reply,   c_network_icmp_msg_type_w),  -- ping reply
                                                              code     => c_tx_icmp_header.code,
-                                                             checksum => c_tx_icmp_header.checksum,          -- init value
+                                                             checksum => c_exp_icmp_checksum,
                                                              id       => c_tx_icmp_header.id,
                                                              sequence => c_tx_icmp_header.sequence);
   
diff --git a/libraries/io/eth1g/tb/vhdl/tb_eth1g.vhd b/libraries/io/eth1g/tb/vhdl/tb_eth1g.vhd
index 956e61a07e..ad31f6993c 100644
--- a/libraries/io/eth1g/tb/vhdl/tb_eth1g.vhd
+++ b/libraries/io/eth1g/tb/vhdl/tb_eth1g.vhd
@@ -181,9 +181,13 @@ ARCHITECTURE tb OF tb_eth1g IS
                                                              checksum => TO_UVEC(c_network_icmp_checksum,         c_network_icmp_checksum_w),  -- init value
                                                              id       => TO_UVEC(c_network_icmp_id,               c_network_icmp_id_w),
                                                              sequence => TO_UVEC(c_network_icmp_sequence,         c_network_icmp_sequence_w));
+
+  -- checksum is 0x0800 + original checksum
+  CONSTANT c_exp_icmp_checksum   : STD_LOGIC_VECTOR(c_network_icmp_checksum_w-1 DOWNTO 0) := TO_UVEC( 2048 + TO_UINT(c_tx_icmp_header.checksum), c_network_icmp_checksum_w);
+
   CONSTANT c_exp_icmp_header     : t_network_icmp_header := (msg_type => TO_UVEC(c_network_icmp_msg_type_reply,   c_network_icmp_msg_type_w),  -- ping reply
                                                              code     => c_tx_icmp_header.code,
-                                                             checksum => c_tx_icmp_header.checksum,          -- init value
+                                                             checksum => c_exp_icmp_checksum,
                                                              id       => c_tx_icmp_header.id,
                                                              sequence => c_tx_icmp_header.sequence);
   
-- 
GitLab