디지털회로설계/SoC,ASIC 설계
[개인스터디] Binary to BCD Code converter
rlaghrud1234
2023. 2. 13. 14:53
※시작에 앞서, 해당 내용은 서적 "VHDL과 반도체 회로설계"의 내용을 토대로 작성됐다.
※나는 Verilog HDL 코드로 작성해서 스터디를 진행했다.
1. Binary to BCD Code Converter
A. Why?
일반 프로그램 언어에서는 함수 선언을 통해 BCD 변환을 쉽게 할 수 있지만 Verilog HDL에서는 표현하기가 매우 어렵다.
해당 책이 출판될 시기에는 Logic Syntehsis에 BCD Binary의 변환 하는 함수 또는 회로 기능들이 없었기 때문에 Binary to BCD Code Converter 회로가 필요했었다고 한다.
B. 변환 방식
Binary Input을 1bit 씩 Left shift를 할 때, 1 Digit(4bit를 한 묶음으로 하는) 값이 0101, 즉, 5이상이 되면 0011 binary 값을 더해주는 방식으로 해서 BCD 변환 코드를 구할 수 있다.
처음에는 genvar문을 이용해서 구하려고 했으나, 생각보다 어려워서
verilog 파일을 두 개로 분할하여 Topmodule에 구현하는 식으로 설계했다.
우선, data flow 방식으로 구성을 했다.
C. Verilog HDL
//Top module
module BI_BCD_Converter(BIN_IN, BCD_OUT);
input[7:0] BIN_IN;
output[9:0] BCD_OUT;
wire[3:0] in1, in2, in3, in4, in5, in6, in7;
wire[3:0] out1, out2, out3, out4, out5, out6, out7;
assign in1 = {1'b0, BIN_IN[7:5]};
assign in2 = {out1[2:0], BIN_IN[4]};
assign in3 = {out2[2:0], BIN_IN[3]};
assign in4 = {out3[2:0], BIN_IN[2]};
assign in5 = {out4[2:0], BIN_IN[1]};
assign in6 = {1'b0, out1[3], out2[3], out3[3]};
assign in7 = {out6[2:0], out4[3]};
Add_11 m1(.A(in1), .B(out1));
Add_11 m2(.A(in2), .B(out2));
Add_11 m3(.A(in3), .B(out3));
Add_11 m4(.A(in4), .B(out4));
Add_11 m5(.A(in5), .B(out5));
Add_11 m6(.A(in6), .B(out6));
Add_11 m7(.A(in7), .B(out7));
assign BCD_OUT = {out6[3], out7, out5, BIN_IN[0]};
endmodule
//Adder
module Add_11(A, B);
input[3:0] A;
output[3:0] B;
reg[3:0] temp_B;
always@(*)
begin
if (A >= 4'b1001)
begin
temp_B <= (A + 4'b0011);
end
end
assign B = temp_B;
endmodule