Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Signed multiplier in Verilog. "signed" doesn't work

Status
Not open for further replies.

oak_tree

Newbie level 5
Newbie level 5
Joined
Nov 12, 2009
Messages
10
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Location
San Diego
Activity points
1,351
Hi,

I'm trying to code a signed multiplier, and I used 'signed' for the ports and wire, but when I run (ModelSim) simulation to check it, it doesn't work for me.
Below is the code and the simple testbench. The numbers are the most positive (0xFF) * most negative (0x2000). In simulation, I get 0x1FE000, which is not the correct answer. Any idea?

The code:
========

Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module mult (one_a, dccfltro, multout);
 
input         [7:0]    one_a; 
input  signed [13:0]   dccfltro;
output signed [22:0]   multout;
 
wire signed  [22:0]   multout;
 
// one_a is unsigned, so treat is as positive
assign multout = dccfltro * {1'b0, one_a};
 
endmodule
 
Testbench:
=======
module mult_tb1 ();
 
wire signed [22:0] multout;
 
mult mult (
        .dccfltro (14'h2000),
        .one_a    (8'hff),
        .multout  (multout)
    ); 
 
endmodule

 
Last edited by a moderator:

Hi,

I'm trying to code a signed multiplier, and I used 'signed' for the ports and wire, but when I run (ModelSim) simulation to check it, it doesn't work for me.
Below is the code and the simple testbench. The numbers are the most positive (0xFF) * most negative (0x2000). In simulation, I get 0x1FE000, which is not the correct answer. Any idea?

The code:
========

Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module mult (one_a, dccfltro, multout);
 
input         [7:0]    one_a; 
input  signed [13:0]   dccfltro;
output signed [22:0]   multout;
 
wire signed  [22:0]   multout;
 
// one_a is unsigned, so treat is as positive
assign multout = dccfltro * {1'b0, one_a};
 
endmodule
 
Testbench:
=======
module mult_tb1 ();
 
wire signed [22:0] multout;
 
mult mult (
        .dccfltro (14'h2000),
        .one_a    (8'hff),
        .multout  (multout)
    ); 
 
endmodule

Hi

A bit rusty but I think there is something missing-you have an error in the logic design? Maybe this helps
**broken link removed**
 

Hi,

I'm trying to code a signed multiplier, and I used 'signed' for the ports and wire, but when I run (ModelSim) simulation to check it, it doesn't work for me.
Below is the code and the simple testbench. The numbers are the most positive (0xFF) * most negative (0x2000). In simulation, I get 0x1FE000, which is not the correct answer. Any idea?

Code Verilog - [expand]
1
2
3
4
5
6
7
8
input         [7:0]    one_a; 
input  signed [13:0]   dccfltro;
output signed [22:0]   multout;
 
wire signed  [22:0]   multout;
 
// one_a is unsigned, so treat is as positive
assign multout = dccfltro * {1'b0, one_a};

All inputs to the * have to be signed for Verilog to produce signed values for a result. Define your one_a as input signed [8:0] one_a;

https://billauer.co.il/blog/2012/10/signed-arithmetics-verilog/
 
Last edited:
0x1FE000 is the correct answer for what you have done.

In Verilog, the sign of an assignment is determined solely by the right hand side of the equation. In your case you have a signed value multiplied with an unsigned value. The result of (signed * unsigned) is unsigned. All operands must be signed to get a signed result.

r.b.

Ooops, missed the previous replies!
 
Last edited:
Thanks for the replies. I had 'signed' also for one_a. However, what prevented it from working is {1'b0, one_a} (the 0 in front).

What I did after reading the replies is define one_a as "signed [8:0]", and then drove 0 for the MSB in the instantiation. That works.

Thanks again for all the replies!
 

Hi,

I'm trying to code a signed multiplier, and I used 'signed' for the ports and wire, but when I run (ModelSim) simulation to check it, it doesn't work for me.
Below is the code and the simple testbench. The numbers are the most positive (0xFF) * most negative (0x2000). In simulation, I get 0x1FE000, which is not the correct answer. Any idea?

The code:
========

Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module mult (one_a, dccfltro, multout);
 
input         [7:0]    one_a; 
input  signed [13:0]   dccfltro;
output signed [22:0]   multout;
 
wire signed  [22:0]   multout;
 
// one_a is unsigned, so treat is as positive
assign multout = dccfltro * $signed{1'b0, one_a};
 
endmodule
 
Testbench:
=======
module mult_tb1 ();
 
wire signed [22:0] multout;
 
mult mult (
        .dccfltro (14'h2000),
        .one_a    (8'hff),
        .multout  (multout)
    ); 
 
endmodule



I think this will also work,
in this case you can keep the one_a as unsigned.
 
Re: Signed multiplier in Verilog. "signed" doesn't work

Thanks Shibin.

It worked, but I needed to add () in order not to get a compliation error:

Code:
assign multout = dccfltro * $signed ({1'b0, one_a});
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top