2’s compliment & Absolute Unit
[code]
`timescale 1ns / 1ps
//////////////////////////////////////////////
// 2’s Complement, Absolute Unit Design
//////////////////////////////////////////////
//2’s Complement Unit
module twos_com(ref_data_8bit,com_out);
input [7:0] ref_data_8bit; // 8bit input unsigned number
output [15:0] com_out; // 16bit output signed number
// 8bit input is 1’s complement -> +1 => 2’s complement
// and then 8bit is extend to 16bit
assign com_out = {11111111, ~ref_data_8bit+1};
endmodule
// Absolute Unit
module abs_mode(abs_in, abs_out);
input [9:0] abs_in; // absolute unit input 10bit
output [9:0] abs_out; // absolute unit output 10bit
//if abs_in MSB is ‘0’ then abs_in = positive
// abs_in MSB is ‘1’ then abs_in = negative
assign abs_out = abs_in[9]?~(abs_in-1):abs_in;
endmodule
[/code]
2의 보수를 만드는 Unit의 경우 부호가 없는 8bit의 데이터를 입력받은 후
1의 보수를 취하기 위해 ~ 연산을 수행하였다.
그 뒤에 1을 더하여 2의 보수를 구하고 그 결과를 16bit로 확장하기 위해 { 비트1, 비트2 }로 묶었다.
절대값을 취하는 Unit의 경우 입력받은 데이터는 10bit의 signed number이다.
따라서 입력한 bit가 양수인지 음수인지 파악하기 위해 최상위 비트(MSB)값에 대한 조건문을 추가한다.
그 값이 참일 경우 음수임을 의미하므로, 1을 빼고 1의 보수를 취해서 양수로 만들어준다.
MSB가 0인 경우 양수이므로, 입력받은 데이터를 그대로 출력한다.

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