Help in vhdl

OP (Company: IFSUL) #3429847
Rate this post
• ▲ useful
▼ not useful
Hi!
I'm trying to build a moving average filter and an error is occuring. 
Basicaly, I'm using an accumulator to add the data present in 
maf_reg(64) and subtract the data present in maf_reg(0). The addition is 
perfectly working, but the subtraction isn't. The operation is between 
lines 94 and 98.

CODE:

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity MAF6bits is
  port(
            clk : in  std_logic;
            rst : in  std_logic;
          sigIN : in  std_logic; 
-- bit stream
         sigOUT : out std_logic_vector (5 downto 0) 
-- average
  );
end entity;

architecture behaviour of MAF6bits is

  signal maf_reg : std_logic_vector (64 downto 0);      -- registrador
  signal     acc : std_logic_vector (5 downto 0);
  signal    temp : std_logic_vector (1 downto 0);

begin

  process( rst , clk )
  begin
    if ( rst='1' ) then
      maf_reg <= 
"00000000000000000000000000000000000000000000000000000000000000000";
      acc     <= "000000";
    elsif ( clk'event and clk='1' ) then
      maf_reg(64)  <= sigIN;
      maf_reg(63)  <= maf_reg(64);
      maf_reg(62)  <= maf_reg(63);
      maf_reg(61)  <= maf_reg(62);
      maf_reg(60)  <= maf_reg(61);
      maf_reg(59)  <= maf_reg(60);
      maf_reg(58)  <= maf_reg(59);
      maf_reg(57)  <= maf_reg(58);
      maf_reg(56)  <= maf_reg(57);
      maf_reg(55)  <= maf_reg(56);
      maf_reg(54)  <= maf_reg(55);
      maf_reg(53)  <= maf_reg(54);
      maf_reg(52)  <= maf_reg(53);
      maf_reg(51)  <= maf_reg(52);
      maf_reg(50)  <= maf_reg(51);
      maf_reg(49)  <= maf_reg(50);
      maf_reg(48)  <= maf_reg(49);
      maf_reg(47)  <= maf_reg(48);
      maf_reg(46)  <= maf_reg(47);
      maf_reg(45)  <= maf_reg(46);
      maf_reg(44)  <= maf_reg(45);
      maf_reg(43)  <= maf_reg(44);
      maf_reg(42)  <= maf_reg(43);
      maf_reg(41)  <= maf_reg(42);
      maf_reg(40)  <= maf_reg(41);
      maf_reg(39)  <= maf_reg(40);
      maf_reg(38)  <= maf_reg(39);
      maf_reg(37)  <= maf_reg(38);
      maf_reg(36)  <= maf_reg(37);
      maf_reg(35)  <= maf_reg(36);
      maf_reg(34)  <= maf_reg(35);
      maf_reg(33)  <= maf_reg(34);
      maf_reg(32)  <= maf_reg(33);
      maf_reg(31)  <= maf_reg(32);
      maf_reg(30)  <= maf_reg(31);
      maf_reg(29)  <= maf_reg(30);
      maf_reg(28)  <= maf_reg(29);
      maf_reg(27)  <= maf_reg(28);
      maf_reg(26)  <= maf_reg(27);
      maf_reg(25)  <= maf_reg(26);
      maf_reg(24)  <= maf_reg(25);
      maf_reg(23)  <= maf_reg(24);
      maf_reg(22)  <= maf_reg(23);
      maf_reg(21)  <= maf_reg(22);
      maf_reg(20)  <= maf_reg(21);
      maf_reg(19)  <= maf_reg(20);
      maf_reg(18)  <= maf_reg(19);
      maf_reg(17)  <= maf_reg(18);
      maf_reg(16)  <= maf_reg(17);
      maf_reg(15)  <= maf_reg(16);
      maf_reg(14)  <= maf_reg(15);
      maf_reg(13)  <= maf_reg(14);
      maf_reg(12)  <= maf_reg(13);
      maf_reg(11)  <= maf_reg(12);
      maf_reg(10)  <= maf_reg(11);
      maf_reg(9)   <= maf_reg(10);
      maf_reg(8)   <= maf_reg(9);
      maf_reg(7)   <= maf_reg(8);
      maf_reg(6)   <= maf_reg(7);
      maf_reg(5)   <= maf_reg(6);
      maf_reg(4)   <= maf_reg(5);
      maf_reg(3)   <= maf_reg(4);
      maf_reg(2)   <= maf_reg(3);
      maf_reg(1)   <= maf_reg(2);
      maf_reg(0)   <= maf_reg(1);

      if    ( temp = "01" ) then
        acc <= acc - "000001";
      elsif ( temp = "10" ) then
        acc <= acc + "000001";
      end if;

    end if;

    temp <= maf_reg(64)&maf_reg(0);

  end process;

  sigOUT(5) <= acc(5);
  sigOUT(4) <= acc(4);
  sigOUT(3) <= acc(3);
  sigOUT(2) <= acc(2);
  sigOUT(1) <= acc(1);
  sigOUT(0) <= acc(0);

end architecture;
Moderator (Company: Titel) #3430036
Rate this post
• ▲ useful
▼ not useful
1
maf_reg(63) <= maf_reg(64);
2
maf_reg(62) <= maf_reg(63);
3
maf_reg(61) <= maf_reg(62);
4
maf_reg(60) <= maf_reg(61);
5
maf_reg(59) <= maf_reg(60);
6
:
7
:
8
maf_reg(2) <= maf_reg(3);
9
maf_reg(1) <= maf_reg(2);
10
maf_reg(0) <= maf_reg(1);
You could cut down this description by a factor of 20 like this:
1
  for i in 0 to 63 loop
2
     maf_reg(i) <= maf_reg(i+1);
3
  end loop;
You also could write:
1
  for i in 63 downto 0 loop
2
     maf_reg(i) <= maf_reg(i+1);
3
  end loop;

Or take this line:
1
   maf_reg <= "00000000000000000000000000000000000000000000000000000000000000000";
With this slightly shorter and easier readable phrase:
1
   maf_reg <= (others=>'0');


William Marques wrote:
> The addition is perfectly working, but the subtraction isn't.
What does not work? What do you expect? And what happens instead?
Maybe you have a little problem here:
> use ieee.std_logic_unsigned.all;
Guest #3430325
Rate this post
• ▲ useful
▼ not useful
In the if statement:

 if    ( temp = "01" ) then
        acc <= acc - "000001";
 elsif ( temp = "10" ) then
        acc <= acc + "000001";
 end if;

I intend to add '1' to the accumulator acc if there is '1' in 
maf_reg(64) and '0' in maf_reg(0) and subtract '1' if there is a '0' in 
maf_reg(64) and '1' in maf_reg(0). The problem is that the summation is 
working and the accumulator increases its value by 1 but when the second 
condition occurs the accumulator doesn't subtract 1. Instead, it remains 
with its value. So it's just increasing (when temp = "01") and never 
decreasing (when temp = "10").
OP (Company: IFSUL) #3430329
Rate this post
• ▲ useful
▼ not useful
Firstly, thanks.

In the if statement:

 if    ( temp = "01" ) then
        acc <= acc - "000001";
 elsif ( temp = "10" ) then
        acc <= acc + "000001";
 end if;

I intend to add '1' to the accumulator acc if there is '1' in
maf_reg(64) and '0' in maf_reg(0) and subtract '1' if there is a '0' in
maf_reg(64) and '1' in maf_reg(0). The problem is that the summation is
working and the accumulator increases its value by 1 but when the second
condition occurs the accumulator doesn't subtract 1. Instead, it remains
with its value.

So it's just increasing (when temp = "10") and never decreasing (when 
temp = "01").

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.