forked from SJTU-YONGFU-RESEARCH-GRP/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurable_mult.v
More file actions
executable file
·84 lines (82 loc) · 3.67 KB
/
configurable_mult.v
File metadata and controls
executable file
·84 lines (82 loc) · 3.67 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Configurable Multiplier
*
* This module implements a configurable multiplier that can perform both signed
* and unsigned multiplication. It supports both compile-time and runtime
* configuration of the sign mode.
*
* Key Features:
* - Signed/unsigned mode: Can switch between signed and unsigned multiplication
* - Compile-time configurable: SIGNED_MODE parameter
* - Runtime configurable: sign_mode input signal
* - Two's complement: Properly handles signed two's complement arithmetic
* - Configurable width: Any bit width via WIDTH parameter
*
* Sign Mode:
* - SIGNED_MODE = 0, sign_mode = 0: Unsigned multiplication
* - SIGNED_MODE = 1, sign_mode = 1: Signed multiplication
* - Either parameter or signal can enable signed mode
*
* Signed Multiplication:
* - Inputs are sign-extended before multiplication
* - Result is properly sign-extended to 2×WIDTH bits
* - Handles two's complement correctly
*
* Unsigned Multiplication:
* - Standard unsigned multiplication
* - Result is zero-extended to 2×WIDTH bits
*
* Use Cases:
* - Arithmetic units in processors
* - Digital signal processing
* - Graphics processing
* - Mathematical computations
*
* @param WIDTH Width of input operands (default: 8 bits)
* Product width is 2×WIDTH bits
* @param SIGNED_MODE Compile-time sign mode: 0=unsigned, 1=signed (default: 0)
*
* @input a[WIDTH-1:0] First operand
* @input b[WIDTH-1:0] Second operand
* @input sign_mode Runtime sign mode control: 0=unsigned, 1=signed
* @output product[2*WIDTH-1:0] Multiplication result
*/
module configurable_mult #(
parameter WIDTH = 8, // Width of each input operand
parameter SIGNED_MODE = 0 // 0 for unsigned, 1 for signed multiplication
) (
input wire [WIDTH-1:0] a, // Input operand A
input wire [WIDTH-1:0] b, // Input operand B
input wire sign_mode, // Dynamic sign mode control (0=unsigned, 1=signed)
output reg [2*WIDTH-1:0] product // Result of multiplication
);
// ============================================================================
// Sign Mode Determination
// ============================================================================
// Signed mode is enabled if either compile-time parameter or runtime signal is set
// This allows flexibility: can be configured at compile time or runtime
wire is_signed = SIGNED_MODE || sign_mode;
// ============================================================================
// Multiplication Logic
// ============================================================================
// Performs multiplication based on sign mode
always @(*) begin
if (is_signed) begin
// ====================================================================
// Signed Multiplication
// ====================================================================
// Sign-extend inputs before multiplication
// Sign extension: replicate MSB to extend to wider width
// $signed() cast ensures proper two's complement interpretation
// Result is automatically sign-extended to 2×WIDTH bits
product = $signed({{1{a[WIDTH-1]}}, a}) * $signed({{1{b[WIDTH-1]}}, b});
end else begin
// ====================================================================
// Unsigned Multiplication
// ====================================================================
// Standard unsigned multiplication
// Result is zero-extended to 2×WIDTH bits
product = a * b;
end
end
endmodule