1
+ -- Some random comments
2
+ -- are here!
3
+
4
+ library ieee;
5
+ use ieee.std_logic_1164.all ;
6
+ use ieee.numeric_std.all ;
7
+
8
+ library work;
9
+ use work.hamm_pkg.all ;
10
+
11
+ --- Generic hamming-code encoder that takes a message `message` and packages
12
+ --- it with corresponding parity bits into an `encoding` for extended
13
+ --- hamming code (SECDED).
14
+ ---
15
+ --- Implemented in purely combinational logic. Parity bits are set in the
16
+ --- indices corresponding to powers of 2 (0, 1, 2, 4, 8, ...).
17
+ entity hamm_enc is
18
+ generic (
19
+ --- Number of parity bits to encode (excluding 0th DED bit)
20
+ PARITY_BITS : positive range 2 to positive 'high
21
+ );
22
+ port (
23
+ --- Incoming message
24
+ message : in logics(data_size(PARITY_BITS)- 1 downto 0 );
25
+ --- Outgoing encoding
26
+ encoding : out logics(block_size(PARITY_BITS)- 1 downto 0 )
27
+ );
28
+ end entity hamm_enc;
29
+
30
+ --- # RTL
31
+ --- My basic architecture.
32
+ architecture rtl of hamm_enc is
33
+ constant EVEN_PARITY : boolean := true ;
34
+
35
+ constant TOTAL_BITS_SIZE : positive := block_size(PARITY_BITS);
36
+ constant PARITY_LINE_SIZE : positive := TOTAL_BITS_SIZE/ 2 ;
37
+
38
+ type hamm_block is array (0 to PARITY_BITS- 1 ) of logics(PARITY_LINE_SIZE- 1 downto 0 );
39
+
40
+ signal enc_block : hamm_block;
41
+
42
+ -- +1 parity for the zero-th check bit
43
+ signal check_bits : logics(PARITY_BITS- 1 + 1 downto 0 );
44
+
45
+ signal empty_block : logics(TOTAL_BITS_SIZE- 1 downto 0 );
46
+ signal full_block : logics(TOTAL_BITS_SIZE- 1 downto 0 );
47
+
48
+ begin
49
+ --! Formats the incoming message into a clean hamming-code block with parity
50
+ --! bits cleared.
51
+ process (message)
52
+ variable ctr : natural ;
53
+ begin
54
+ empty_block <= (others => '0' );
55
+ ctr := 0 ;
56
+ for ii in 0 to TOTAL_BITS_SIZE- 1 loop
57
+ -- use information bit otherwise reserve for parity bit
58
+ if is_pow_2(ii) = false then
59
+ empty_block(ii) <= message(ctr);
60
+ ctr := ctr + 1 ;
61
+ end if ;
62
+ end loop ;
63
+ end process ;
64
+
65
+ --! divide the entire hamming-code block into parity subset groups
66
+ process (empty_block)
67
+ variable temp_line : logics(PARITY_LINE_SIZE- 1 downto 0 );
68
+ variable index : logics(PARITY_BITS- 1 downto 0 );
69
+ begin
70
+ for ii in PARITY_BITS- 1 downto 0 loop
71
+ temp_line := (others => '0' );
72
+ for jj in TOTAL_BITS_SIZE- 1 downto 0 loop
73
+ -- decode the parity bit index
74
+ index := (others => '0' );
75
+ index := logics(to_unsigned (jj, PARITY_BITS));
76
+
77
+ if index (ii) = '1' then
78
+ -- insert new bit
79
+ temp_line := temp_line(PARITY_LINE_SIZE- 2 downto 0 ) & empty_block(jj);
80
+ end if ;
81
+ end loop ;
82
+ -- drive the ii'th vector in the block as this parity's subset of bits
83
+ enc_block(ii) <= temp_line;
84
+ end loop ;
85
+ end process ;
86
+
87
+ --! instantiate parity checkers for the subset of bits to evaluate
88
+ gen_check_bits: for ii in 0 to PARITY_BITS- 1 generate
89
+ u_par : entity work.parity
90
+ generic map (
91
+ SIZE => TOTAL_BITS_SIZE/ 2 ,
92
+ EVEN_PARITY => EVEN_PARITY
93
+ ) port map (
94
+ data => enc_block(ii),
95
+ check_bit => check_bits(ii)
96
+ );
97
+ end generate gen_check_bits;
98
+
99
+ --! fill the hamming-code block with computed parity bits
100
+ process (empty_block, check_bits)
101
+ variable ctr : natural ;
102
+ begin
103
+ full_block <= empty_block;
104
+ ctr := 0 ;
105
+
106
+ for ii in 1 to TOTAL_BITS_SIZE- 1 loop
107
+ -- use information bit otherwise reserve for parity bit
108
+ if is_pow_2(ii) = true then
109
+ full_block(ii) <= check_bits(ctr);
110
+ ctr := ctr + 1 ;
111
+ end if ;
112
+ end loop ;
113
+ end process ;
114
+
115
+ --! computes the extra parity bit (0th bit) for double-error detection
116
+ u_ded : entity work.parity
117
+ generic map (
118
+ SIZE => TOTAL_BITS_SIZE- 1 ,
119
+ EVEN_PARITY => EVEN_PARITY
120
+ ) port map (
121
+ data => full_block(TOTAL_BITS_SIZE- 1 downto 1 ),
122
+ check_bit => check_bits(PARITY_BITS)
123
+ );
124
+
125
+ -- drive the output with the hamming-code block and the 0th parity bit
126
+ encoding <= full_block(TOTAL_BITS_SIZE- 1 downto 1 ) & check_bits(PARITY_BITS);
127
+
128
+ end architecture rtl;
0 commit comments