EmbDev.net

Forum: FPGA, VHDL & Verilog Multiplication fixed floating-point


von Martin (Guest)


Attached files:

Rate this post
useful
not useful
Hello there, i got there another homework as final of semester.

Entering: Create and test the parameterizable implementation of the 
integer multiplication algorithm in a fixed floating point. The 
parameter must be the number of input bits
operands.

They give us something there as hints:
Let A [n-1: 0],
B [n-1: 0],
Q [n-1: 0],
C [1] and PC are registers.

PC serves as a cycle counter.
Let's put C#Q#A on the label linking registers.

Algorithm of integer multiplication:
1
BEGIN
2
C#Q :=0 || PC:=n
3
WHILE(PC>0)
4
IF A(0) = 1 THEN
5
C#Q := Q+B
6
ENDIF
7
C#Q#A :=0 # SR1(C#Q#A)||
8
PC := PC - 1
9
END
10
END

After that we got Steps of integer multiplication in binary.
How to start with it? Some solutions? I am so newbie at that 
problematics of VHDL codes, scripts.

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


Rate this post
useful
not useful
Martin wrote:
> a fixed floating point.
I do not understand what this may be.
I know either "floating point" numbers or "fixed point" numbers.
But a "fixed floting point number" is such a thing like a "flying 
submarine".
A "fixed point" number/integer is looking like a normal number/integer. 
Only you add a mental position to it which represets a point. The 
whole behaviour of the integer always stays the same.
Its a little bit like having a current of 3456 mA in a wire. That is the 
very same current like 3.456 A. It is the same current. Just divided by 
1000 and therefore getting a point.

So the integer (the current) itself doesn't even "know" that it has a 
pinot in it. You can handle it (add, sub, mul...) like a "normal" 
integer. Only you must keep an eye on the position of the point:
lets multiply two numbers: 100 * 100 = 10000
When those numbers 100 would be 2.1 fixed point numbers they would 
represent 10.0 and the result of the multiplication 10000 must will have 
the format 4.2, so you must position the point in a way you have 2 
fractional digits: 100.00

> Algorithm of integer multiplication:
Thats the usual multiplication like you do it with a pencil on a sheeet 
of paper.

> Q [n-1: 0]
That is wrong. It must be Q[2n-1 : 0] because the result of a 
multiplication is as wide as the sum of both inputs width!
Its easy to see: let n be 8, so we get an integer ranging from -128 to 
128. So we can easily calculate 100*100 = 10000. Fairly easy to see, 
that this result does not fit into -128 to 127...

> How to start with it?
Write a code for multiplying two unsigned integers without any point. 
Wrap that code with a sign-handler. Preform the multiplication of the 
fixed integer number like it would be a "normal" integer. Align the 
result to the output register in a way that the "point" (which is only a 
mental point) is at the correct position. Thats all.

You will need some sheets of paper and a few hours of thougts to get 
it...

: Edited by Moderator
von Martin (Guest)


Rate this post
useful
not useful
It isnt working.. I have tried many tutorials and no one was good. Do 
you have some example of that for projects like that is? Thanks

von Martin (Guest)


Rate this post
useful
not useful
I have read something on forum, where i found this code for 
multiplication..
1
  library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.numeric_std.ALL;
4
5
entity MultExample is
6
Port (C : OUT unsigned (31 downto 0));
7
end MultExample;
8
9
architecture Behavioral of MultExample is
10
11
constant A: integer:= 4;
12
constant B: integer:= 2;
13
14
begin
15
16
C <=TO_UNSIGNED(A*B,32);
17
18
end Behavioral;
There one advanced user told that, C is still 32-bit as result of 
multiplication of 2 integers. But how to apply something like that to my 
problem? Thanks

von Martin (Guest)


Rate this post
useful
not useful
n-1 : 0 says that from number to downto 0?

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


Rate this post
useful
not useful
That means you have n bits wide vectors.
In VHDL you would write (n-1 downto 0).

Martin wrote:
> There one advanced user told that, C is still 32-bit as result of
> multiplication of 2 integers. But how to apply something like that to my
> problem?
It's a thoroughly correct but in your case absolutely useless 
information.
Maybe you asked the wrong question...

Martin wrote:
> It isnt working.. I
What is "it"?

von Martin (Guest)


Rate this post
useful
not useful
I must user generic but it cannot compile. Does you have sketch of 
something where is  multiplication with generic form n downto 0?

von Martin (Guest)


Rate this post
useful
not useful
I must send it tomorrow to my teacher and i havent nothing.

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


Rate this post
useful
not useful
Try this:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.numeric_std.ALL;
4
5
entity MultExample is
6
Generic ( n  : natural := 16);
7
Port (a,b : in std_logic_vector(n-1 downto 0),
8
          c.  : out std_logic_vector(2*n-1 downto 0));
9
end MultExample;
10
11
architecture Behavioral of MultExample is
12
begin
13
14
    c <= std_logic_vector(unsigned(a)*unsigned(b));
15
16
end Behavioral;

von Martin (Guest)


Rate this post
useful
not useful
You had one error in your code, i fixed it. Code is working, Waveform 
simulation too.
How can i put my A[n-1 : 0] to code? Only put that to Port like:
1
A : in std_logic_vector(n-1 downto 0);
Nothing else?
How about testing with Altera..I need testbench right? Some advices to 
vhdl code at entity?
I don't have so much time for realization.

von Martin (Guest)


Rate this post
useful
not useful
How to set C as 1 it will be input i think...
Can you tell me? Thanks a lot! Compilation is unsucessful.
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.numeric_std.ALL;
4
5
entity MultExample is
6
Generic ( n  : natural := 16);
7
Port (A,B,Q : in std_logic_vector(n-1 downto 0);        
8
        C  : std_logic:='1';
9
      PC : in std_logic_vector(n-1 downto 0));
10
       
11
end MultExample;
12
13
architecture Behavioral of MultExample is
14
begin
15
16
  
17
C#Q :=0 || PC:=n
18
WHILE(PC>0)
19
IF A(0) = 1 THEN
20
C#Q := Q+B
21
ENDIF
22
C#Q#A :=0 # SR1(C#Q#A)||
23
PC := PC - 1
24
END
25
END
26
27
end Behavioral;
Is alghoritm ok in Behavioral? Or it must be in process or something 
like that? Thanks a lot for help.

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


Rate this post
useful
not useful
Martin wrote:
> Is alghoritm ok in Behavioral?
It's a pseudo language. The synthesizer will not understand it. It will 
produce lots of errors.

> Port (A,B,Q : in std_logic_vector(n-1 downto 0);
Why Q?

My best hint ist to discuss this little piece of homework with one of 
your classmates! I can't see no progress here with that topic. We are 
discussing about things on a level similar to 1+1 in arithmetics...

: Edited by Moderator
von Martin (Guest)


Rate this post
useful
not useful
Pseudo language? How now use it? Must i remake it to VHDL format? Or 
where attach it? No one from my classmates got similar homework. What is 
Q?
I got homework: A, B, Q, C, PC are registers, PC is cycle counter. 
Please help me. I can not do that alone.

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


Rate this post
useful
not useful
Martin wrote:
> Must i remake it to VHDL format?
First you must understand the algorithm in those lines. Then you can 
convert them to VHDL.

The inputs are two operands as vectors and the output is one vector of 
(curiously) the same length.

The PC (name it progress counter) and the C (name it carry) are only 
local signals.

Indeed the text of that exercise is wrong (or at least misleading), 
because due to the fact that a multiplication is thoroughly 
combinatorial there are no registers needed to get the result.

von Martin (Guest)


Rate this post
useful
not useful
I don't know how to format pseudo code to VHDL code. Yes, i understand 
to that there are 2 inputs A, B. Q is output of same lenght, because it 
got parameter n-1 downto 0.
Understand. But i don't have experiences to make code to VHDL script 
from that pseudo code. Can you help me little bit with some VHDL demo 
with my variables please?

von Martin (Guest)


Rate this post
useful
not useful
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.numeric_std.ALL;
4
5
entity MultExample is
6
Generic ( n  : natural := 16);
7
Port (A,B,Q : in std_logic_vector(n-1 downto 0);        
8
        C  : std_logic:='1';
9
      PC : in std_logic_vector(n-1 downto 0));
10
       
11
end MultExample;
12
13
architecture Behavioral of MultExample is
14
15
BEGIN
16
  C#Q := 0 || PC := n
17
  WHILE (PC > 0)
18
    IF A(0) = 1 THEN
19
      C#Q := Q + B
20
    END IF
21
    C#Q#A := 0 #SR1(C#Q#A) ||
22
      PC := PC - 1
23
    END  
24
  END
25
end Behavioral;
Can you edit it please?

von Martin (Guest)


Rate this post
useful
not useful
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.numeric_std.ALL;
4
5
entity MultExample is
6
Generic ( n  : natural := 16);
7
Port (A,B : in std_logic_vector(n-1 downto 0);        
8
      C  : std_logic:='1';
9
      Q : out std_logic_vector(n-1 downto 0);
10
      PC : in std_logic_vector(n-1 downto 0));
11
       
12
end MultExample;
13
14
architecture Behavioral of MultExample is
15
16
BEGIN
17
  Q <= std_logic_vector(unsigned(A) * unsigned(B));
18
end architecture Behavioral;
How about something like that? Can you help me?

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.