endmodule To use the above module, you would instantiate it in your top-level Verilog file or in a testbench. Here’s a simple testbench example:
// State machine for multiplication always @(posedge clk) begin if (reset) begin state <= 0; product <= 16'd0; multiplicand <= a; multiplier <= b; end else if (start) begin case (state) 0: begin product <= 16'd0; multiplicand <= a; multiplier <= b; state <= 1; end 1: begin if (multiplier != 8'd0) begin if (multiplier[0]) begin product <= product + {8'd0, multiplicand}; end multiplicand <= multiplicand << 1; multiplier <= {multiplier[7:1], 1'd0}; state <= 1; end else begin state <= 2; end end 2: begin state <= 2; // Stay in this state to hold the result end default: state <= 0; endcase end end 8-bit multiplier verilog code github
initial $monitor("a = %d, b = %d, product = %d", a, b, product); endmodule To use the above module, you would
// Output the product assign product;
multiplier_8bit_manual uut (.a(a), .b(b), .product(product), .start(start), .clk(clk), .reset(reset)); endmodule To use the above module
reg [15:0] product; reg [7:0] multiplicand; reg [7:0] multiplier; reg [3:0] state;
module multiplier_8bit(a, b, product); input [7:0] a, b; output [15:0] product; assign product = a * b; endmodule However, if you want to implement it more manually without using the built-in multiplication operator ( * ), you can do it by shifting and adding, similar to how multiplication is done manually. Manual 8-bit Multiplier module multiplier_8bit_manual(a, b, product, start, clk, reset); input [7:0] a, b; output [15:0] product; input start, clk, reset;