@Mike Treseler
Actually, my purpose is to find 5! in each trigger CLK. I want to make
4 triggers with RUN button, so at the end we will obtain 5!.
first Run trigger : 5
second one : 5*4
third one : 5*4*3
fourth one : 5*4*3*2
Where is my mistake here? Can you have a look at it, everyone?
---------------------------------------------
-----------5! factorial calculation-----------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_signed.all;
-------------------------------------------
entity factorial is
port (clk : IN BIT;
result : OUT INTEGER);
end factorial;
-------------------------------------------
architecture calculation of factorial is
signal n: integer :=5;
signal temp: integer :=1;
begin
PROCESS (clk)
begin
if (clk'EVENT AND clk='1') then
temp <= temp * n;
n <= n-1;
result <= temp;
end if;
end PROCESS;
end calculation;
As Tricky mentioned, your loop doesn't have a proper termination
condition (test for n=1) - this means that your result will only be
cofrrect for one clock cycle, and whether that's the first or second
or fifth clock cycle will depend on what value of n you started with.
This kind of structure (where you "toss a value into the wind" and
hope you can catch it when the corresponding answer comes back ) is
usually a dangerous way to design a system - too many ways to mess up.
I added a "done" bit you can observe in the code below.
Also, while this is "neat" for a learning project, most real hardware
should do something more than once (unless you're computing the number
42), so you want a "start on new input" or a "reset" input as well as
an input that isn't constant - I'll leave this to you.
You also have to be careful of two other more things: Your calculation
outputs an INTEGER as opposed to a std_logic_vector which means that,
even though your design might simulate OK on the computer, an FPGA
synthesizer is not guaranteed to create the "normal" twos complement
bus on the FPGA pins the way you probably want.
The other thing to watch out for is that (you mention a RUN button) -
on an FPGA eval board, you need to make sure that your RUN button is
cleanly debounced - mechanical switches can jiggle enough to make
dozens of clock events happen in a matter of milliseconds on the first
button press.
One way to test if your "framework" (RUN button and output pins) is
correct is to change your calculation logic (the factorial) into the
simplest logic you can think of: for example, a counter, or just
shifting by one bit per clock, or a toggling value every clock and see
if your board toggles the outputs. Using a dead-simple computation
lets you check that your "other stuff" (like clocking, and initial
conditions) are correct before you worry about the math.
Also, use "numeric_std" instead of "std_logic_arith" - just because
some tutorial written in 1992 says to do it, don't. It's 2008 now.
There have been lost of flame wars about this - Google this group to
learn more.
If I were to take a stab, I'd write it like this (caveat: I'm just
typing this on the fly, not testing or compiling):
Good luck,
- Kenn
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
-------------------------------------------
entity factorial is
port (clk : IN BIT;
result : OUT std_logic_vector(7 downto 0);
done : out std_logic);
end factorial;
-------------------------------------------
architecture calculation of factorial is
signal n: integer :=5;
signal temp: integer :=1;
begin
PROCESS (clk)
begin
if (clk'EVENT AND clk='1') then
temp <= temp * n;
done <= '1'; -- provisionally
if (n > 1) then
n <= n-1;
done <= '0'; -- Oh, wasn't really done
end if;
result <= std_logic_vector(to_unsigned
(result,result'length));
end if;
end PROCESS;
end calculation;
------------------------------------------