EmbDev.net

Forum: FPGA, VHDL & Verilog Help in vhdl


Author: William Marques (Company: IFSUL) (williammarques)
Posted on:

Rate this post
0 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;

Author: Lothar Miller (lkmiller) (Moderator)
Posted on:

Rate this post
2 useful
not useful
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(2) <= maf_reg(3);
maf_reg(1) <= maf_reg(2);
maf_reg(0) <= maf_reg(1);
You could cut down this description by a factor of 20 like this:
  for i in 0 to 63 loop
     maf_reg(i) <= maf_reg(i+1);
  end loop;
You also could write:
  for i in 63 downto 0 loop
     maf_reg(i) <= maf_reg(i+1);
  end loop;

Or take this line:
   maf_reg <= "00000000000000000000000000000000000000000000000000000000000000000";
With this slightly shorter and easier readable phrase:
   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
Author: Bitkracherl (Guest)
Posted on:

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

Depends on what is more obvious to you.

Author: William Marques (Guest)
Posted on:

Rate this post
-1 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").

Author: William Marques (Company: IFSUL) (williammarques)
Posted on:

Rate this post
0 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

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [vhdl]VHDL code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.