0

How do you multiply a binary number by ten in verilog? Is it as simple as

reg [7:0] a,b;
reg[31:0] p;
b= 8'b00001001;
p=a*b;

Upgraded to windows 8 and my xilinx is working atm. Need to know asap for planning stage.

Marty
  • 6,494
  • 3
  • 37
  • 40
David Flanagan
  • 373
  • 2
  • 7
  • 19

2 Answers2

2

Nearly - 10 in binary is 1010! For multiplying two 8-bit numbers, you'll only need 16 bits for the result, so reg [15:0] p would do. What about:

 always @( a )
     p = a * 8'd10; 

or

wire [13:0] p;
assign p = a * 4'd10;
Marty
  • 6,494
  • 3
  • 37
  • 40
0

I'm not sure how good the HDL synthesis tools are at converting multiplies to bit shifts. I wouldn't want to synthesise a multiplier for this code by accident (even your CPU is unlikely to use a multiply for this).

wire [7:0] a;
wire [11:0] shift2;
wire [11:0] shift1;
wire [11:0] result;

assign shift2 = a << 3; // a * 8
assign shift1 = a << 1; // a * 2
assign result = shift2 + shift1;
Grandswiss
  • 93
  • 1
  • 4
dave
  • 4,812
  • 4
  • 25
  • 38
  • 1
    Modern synthesis tools tend to be very efficient when an operand is fixed. Using a `* constant` also allows power optimizations to be made by the tools, and when pushed for timing they are free to choose faster architectures. – Morgan Nov 15 '12 at 17:24