Bläddra i källkod

Multiplication Module

Oliver Jaison 5 år sedan
förälder
incheckning
21566a94fc
1 ändrade filer med 58 tillägg och 2 borttagningar
  1. 58 2
      src/FPA_module_test.sv

+ 58 - 2
src/FPA_module_test.sv

@@ -86,7 +86,43 @@ endmodule : floating_add
 module floating_product #(parameter N=16, M=4)(a, b, c);
 	input logic [N-1:0] a, b;
 	output logic [N-1:0] c;
-	
+
+	// sign_x = x[N-1]
+	// exponent_x = x[N-2:N-2-M]
+	// mantissa_x = x[N-3-M:0]
+
+	logic sum;
+	logic underflow;
+	logic [N-3-M:0] product;
+
+	// We have assigned an {M+1} bit exponent so we must have a 2^{M} offset
+	assign sum = a[N-2:N-2-M] + b[N-2:N-2-M];
+	assign c[N-2:N-2-M] = sum - (1 << M);
+
+	always_comb
+		begin
+			// If the sums of the exponents is less than 2^M then the exponent will underflow and the product is zero
+			if (sum < (1 << M))
+				begin
+					c[N-1:0] = 0;
+					underflow = 1;
+				end
+			// If either input number has a high-order bit of zero, then that input is zero and the product is zero
+			else if (!a[N-3-M] || !b[N-3-M])
+				c[N-1:0] = 0;
+			else
+				begin
+					// Setting the mantissa of the output
+					product = a[N-3-M:0] * b[N-3-M];
+					if (product[N-3-M])
+						c[N-3-M:0] = product[N-3-M:0];
+					else
+						c[N-3-M:0] = product[N-3-M:0] << 1;
+					// Setting the sign of the output
+					c[N-1] = a[N-1]^b[N-1];
+				end
+		end
+
 endmodule : floating_product
 
 
@@ -109,4 +145,24 @@ module floating_add_tb;
 		test_inputs(16'b0, 16'b0_01111_0000000000, 16'b0_01111_0000000000);
 		$finish();
 	end
-endmodule : floating_add_tb
+endmodule : floating_add_tb
+
+module floating_product_tb;
+	logic [15:0] a, b, c;
+	floating_product multiplier1(a, b, c);
+
+	task test_inputs;
+		input [15:0] in_a, in_b, expected_c;
+		assign a = in_a;
+		assign b = in_b;
+		#2ps;
+		if(c == expected_c) $display("PASS: a=%b b=%b c=%b", a,b,c);
+		else $error("FAIL: a=%b b=%b c=%b, expected c=%b", a,b,c,expected_c);
+		#2ps;
+	endtask : test_inputs
+
+	initial begin
+		test_inputs(16'b0, 16'b0_01111_0000000000, 16'b0);
+		$finish();
+	end
+endmodule : floating_product_tb