Write the code of 16to32-bit Sign Extension Unit with the testbench. “a” is 16-bit input…
- User
Write the code of 16to32-bit Sign Extension Unit with the testbench. “a” is 16-bit input and “result” is 32-bit output.Write the testbench write code in verilog
- Assistant
Here is a verilog code for a 16-to-32 bit sign extension unit along with its testbench:
module sign_extension_unit #(parameter MSB = 15) ( input signed [15:0] a, output signed [31:0] result ); assign result = {{32-{MSB+1}}{a[MSB]}, a}; endmodule module sign_extension_unit_tb; reg signed [15:0] a; wire signed [31:0] result; sign_extension_unit #(15) sign_ext_unit( .a(a), .result(result) ); initial begin $display("Testing Sign Extension Unit"); $display("--------------------------------------"); // Test case 1: a is positive #10 a = 16'h1234; #10 $display("a = %h, result = %h", a, result); // result should be 32'h00001234 // Test case 2: a is negative #10 a = 16'hFFFF; #10 $display("a = %h, result = %h", a, result); // result should be 32'hFFFFFFFF // Test case 3: a is zero #10 a = 16'h0000; #10 $display("a = %h, result = %h", a, result); // result should be 32'h00000000 // Add more test cases if needed $finish; end endmoduleIn this code, the
sign_extension_unitmodule takes a 16-bit inputaand extends it to a 32-bit outputresultby sign-extending the most significant bit. TheMSBparameter is set to 15, indicating that the most significant bit ofais bit 15.The
assignstatement in thesign_extension_unitmodule performs the sign extension by concatenatinga[MSB](the most significant bit ofa) witha. The concatenation is achieved using the{}operator, with the number of bits in the concatenated value being 32-(MSB+1), ensuring that the final result is a 32-bit value.The
sign_extension_unit_tbmodule is the testbench for thesign_extension_unitmodule. It instantiates thesign_extension_unitmodule and provides test cases in theinitialblock. Each test case sets the value ofaand displays the resulting value ofresultusing the$displaysystem function. The expected results are also provided as comments.You can simulate this code using a Verilog simulator, such as ModelSim or VCS, to observe the result of the sign extension unit for different input values.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.