EmbDev.net

Forum: FPGA, VHDL & Verilog Help in vhdl


von William M. (Company: IFSUL) (williammarques)


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;

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


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;

: Edited by Moderator
von Bitkracherl (Guest)


Rate this post
useful
not useful
I think you could even so write the shift like:
1
maf_reg(63 downto 0) <= maf_reg(64 downto 1);

Depends on what is more obvious to you.

von William Marques (Guest)


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").

von William M. (Company: IFSUL) (williammarques)


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").

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.