<BR>`timescale 1ns / 1ps<BR>//////////////////////////////////////////////<BR>// 2's Complement, Absolute Unit Design<BR>//////////////////////////////////////////////<BR><BR>//2's Complement Unit<BR>module twos_com(ref_data_8bit,com_out);<BR>&nbsp;&nbsp; input [7:0] ref_data_8bit; // 8bit input unsigned number<BR>&nbsp;&nbsp; output [15:0] com_out; &nbsp;// 16bit output signed number<BR><BR>&nbsp;&nbsp; // 8bit input is 1's complement -&gt; +1 =&gt; 2's complement<BR>&nbsp; &nbsp;// and then 8bit is extend to 16bit<BR>&nbsp; &nbsp;assign com_out = {11111111, ~ref_data_8bit+1};<BR>endmodule<BR><BR>// Absolute Unit<BR>module abs_mode(abs_in, abs_out);<BR>&nbsp;&nbsp; input [9:0] abs_in; &nbsp; // absolute unit input 10bit<BR>&nbsp; &nbsp;output [9:0] abs_out; &nbsp;// absolute unit output 10bit<BR><BR>&nbsp;&nbsp; //if abs_in MSB is '0' then abs_in = positive<BR>&nbsp; &nbsp;// &nbsp; abs_in MSB is '1' then abs_in = negative<BR>&nbsp; &nbsp;assign abs_out = abs_in[9]?~(abs_in-1):abs_in;<BR>endmodule<BR>


2의 보수를 만드는 Unit의 경우 부호가 없는 8bit의 데이터를 입력받은 후
1의 보수를 취하기 위해 ~ 연산을 수행하였다.
그 뒤에 1을 더하여 2의 보수를 구하고 그 결과를 16bit로 확장하기 위해 { 비트1, 비트2 }로 묶었다.

절대값을 취하는 Unit의 경우 입력받은 데이터는 10bit의 signed number이다.
따라서 입력한 bit가 양수인지 음수인지 파악하기 위해 최상위 비트(MSB)값에 대한 조건문을 추가한다.
그 값이 참일 경우 음수임을 의미하므로, 1을 빼고 1의 보수를 취해서 양수로 만들어준다.
MSB가 0인 경우 양수이므로, 입력받은 데이터를 그대로 출력한다.

사용자 삽입 이미지


< 2’s Complement Unit Simulation Result >

사용자 삽입 이미지< Absolute Unit >
< Test Bench >
[#M_ more.. | less.. | 

<BR>`timescale 1ns / 1ps /////////////////////////////////////////////////////<BR>// 2's Complement, Absolute Unit Design Test Bench<BR>// 200420174 Jeon Chang Kyu<BR>/////////////////////////////////////////////////////<BR>module twos_com_tb_v;<BR>&nbsp; &nbsp;// Inputs<BR>&nbsp; &nbsp;reg [7:0] ref_data_8bit;<BR>&nbsp; &nbsp;// Outputs<BR>&nbsp; &nbsp;wire [15:0] com_out;<BR><BR>&nbsp; &nbsp;// 2's complement unit: U1<BR>&nbsp; &nbsp;twos_com U1 (.ref_data_8bit(ref_data_8bit), .com_out(com_out));<BR><BR>&nbsp; &nbsp;initial begin<BR>&nbsp; &nbsp; ref_data_8bit = 8'b00001111; &nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; ref_data_8bit = 8'b01001101; &nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; ref_data_8bit = 8'b01101000; &nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; ref_data_8bit = 8'b00010111; &nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; ref_data_8bit = 8'b11001100; &nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; ref_data_8bit = 8'b01011011; &nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; ref_data_8bit = 8'b10011111;<BR>&nbsp; &nbsp;end<BR>endmodule<BR><BR>module abs_mode_tb_v;<BR>&nbsp; &nbsp;// Inputs<BR>&nbsp; &nbsp;reg [9:0] abs_in;<BR>&nbsp; &nbsp;// Outputs<BR>&nbsp; &nbsp;wire [9:0] abs_out;<BR><BR>&nbsp; &nbsp;// Absolute unit: U2<BR>&nbsp; &nbsp;abs_mode U2 (.abs_in(abs_in),.abs_out(abs_out));<BR><BR>&nbsp; &nbsp;initial begin<BR>&nbsp; &nbsp; abs_in = 10'b0000000011;&nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; abs_in = 10'b1000000110;&nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; abs_in = 10'b1000101111; &nbsp; &nbsp;#50; //50ns delay<BR>&nbsp; &nbsp; abs_in = 10'b0010101101;&nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; abs_in = 10'b1010100100;&nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; abs_in = 10'b0010101101;&nbsp; &nbsp; #50; //50ns delay<BR>&nbsp; &nbsp; abs_in = 10'b0100101001;<BR>&nbsp; &nbsp;end<BR>endmodule<BR>

_M#]