Network Wizard for VHDL Test Benches
Codec library

Stateless codec

This library provides functions for a generic stateless codec.

Functionality

  • Perform data word stuffing (replace specific words with an escape sequence)
  • Encode data words from lookup table

Other libraries in Codec are:


More details in nw_sl_codec_pkg

Example use

Include the libraries:

library nw_codec;
context nw_codec.nw_codec_context;

Example 1: Bytestuffing of PPP frame - replace 0x7e and 0x7d with escape sequences. First, define the codec:

constant C_PPP_CODEC : t_codec(0 to 1)(word(7 downto 0), code(0 to 1)(7 downto 0)) := ((word => x"7e", code => (x"7d", x"5e")),
(word => x"7d", code => (x"7d", x"5d")));

Then encode a data array:

v_data := (x"19", x"7e", x"fa", x"91", x"7d", x"80", x"00"); -- data array to be encoded
v_len := f_sl_enc_len(v_data, C_PPP_CODEC); -- get length of encoded data (v_len is now 9)
v_encoded(0 to v_len - 1) := f_sl_enc(v_data, C_PPP_CODEC); -- v_encoded is now (x"19", x"7d", x"5e", x"fa", x"91", x"7d", x"5d", x"80", x"00")

Decode the encoded data:

v_dlen := f_sl_dec_len(v_encoded(0 to v_len - 1), C_PPP_CODEC); -- get length od decoded data
v_decoded(0 to v_dlen - 1) := f_sl_dec(v_encoded(0 to v_len - 1), C_PPP_CODEC); -- v_decoded is now equal to v_data

Example 2: Encode data with lookup table - here we will apply Hamming(7,4) coding to a data vector (not an elegant way to perform Hamming coding, but nonetheless).
First, define the codec (the loopup table must be complete):

constant C_HAMMING_7_4: t_codec(0 to 15)(word(3 downto 0), code(0 to 0)(6 downto 0)) := ((word => x"0", code => (others => "0000000")),
(word => x"1", code => (others => "1101001")),
(word => x"2", code => (others => "0101010")),
(word => x"3", code => (others => "1000011")),
(word => x"4", code => (others => "1001100")),
(word => x"5", code => (others => "0100101")),
(word => x"6", code => (others => "1100110")),
(word => x"7", code => (others => "0001111")),
(word => x"8", code => (others => "1110000")),
(word => x"9", code => (others => "0011001")),
(word => x"a", code => (others => "1011010")),
(word => x"b", code => (others => "0110011")),
(word => x"c", code => (others => "0111100")),
(word => x"d", code => (others => "1010101")),
(word => x"e", code => (others => "0010110")),
(word => x"f", code => (others => "1111111")));

The the codec is applied to a data vector:

v_data := (x"7", x"0", x"e", x"a"); -- data array to be encoded
v_len := f_sl_enc_len(v_data, C_HAMMING_7_4); -- get length of encoded data (in this case will be equal to v_data length)
v_ham(0 to v_len - 1) := f_sl_enc(v_data, C_HAMMING_7_4); -- v_ham is now ("0001111", "0000000", "0010110", "1011010")

See further examples in the test bench nw_codec_tb.vhd.