Kraklog
Day.78 Advanced Verilog #1-원치 않는 Latch 제거 본문
프로그램 : Quaturs Prime lite edition 18.1
사용문법 : Verilog 2001
보드 : DE1-SOC
Verilog 를 이용해서 디지털 회로를 디지털할 때 always @ 구문을 이용한다면 항상 원하지 않는 Latch에 대해서 주의를 해야한다.
(*항상 always @ 구문에 의해서만 생기는 것은 아니다. 주로 always @ 구문을 이용하면 자주 발생하는 것.)
Verilog에서는 combination logic을 기술할 때 모든 조건에 대하여 입력하지 않으면 자동으로 logic을 형성하게 되는데 이전 값을 유지시키기 위해서 latch가 생긴다.
module my_latch (
input [2:0] sel,
input a,
input b,
input c,
output reg mux_out
);
always @ (*) begin
if(sel==3'b001)
mux_out = a;
else if (sel == 3'b010)
mux_out=b;
else if (sel == 3'b100)
mux_out = c;
end
endmodule
if ~ else if
구문을 사용했기때문에 logic은 구동되는 순서가 정해지게 된다.
따라서 현재 상태는 if 구문에 대한 모든 조건이 closed 되어지지 않은 상태이기 때문에 후반부에 latch가 존재하게 된다.
module my_latch (
input [2:0] sel,
input a,
input b,
input c,
output reg mux_out
);
always @ (*) begin
if(sel==3'b001)
mux_out = a;
else if (sel == 3'b010)
mux_out=b;
else if (sel == 3'b100)
mux_out = c;
else
out = 1'bx;
end
endmodule
sel에 그 외의 경우를 할당을 해주어 모든 조건에 close 시켜주면
원하지 않는 latch가 사라짐을 알 수 있다.
-불필요한 의존성으로 인한 latch
module my_latch(
input [0:2] sel,
input a,
input b,
input c,
output reg x,
output reg y,
output reg z
);
always@(*) begin
if (sel == 3'b010)
x=a;
else if (sel == 3'b100)
y=b;
else if (sel == 3'b001)
z= c;
else begin
x = 0;
y = 0;
z = 0;
end
end
endmodule
Result : Combinational Processes에서 모든 조건이 작성되어지지 않았을 때 원치 않는 latch가 발생된다.
모든 경우가 할당되어진 모습처럼 보이지만, 불필요하게 a를 확인하고 b를 확인하는 등의 절차가(if else if 구문에 의해) 발생하게 되므로 latch가 발생하게 된다.
다음은 latch를 제거하는 3가지 방식에 대한 방법이다.
module my_latch(
input [0:2] sel,
input a,
input b,
input c,
output reg x,
output reg y,
output reg z
);
always@(*) begin
if (sel == 3'b010)
x=a;
else
x=1'b0;
if (sel == 3'b100)
y=b;
else
y=1'b0;
if(sel ==3'b001)
z=c;
else
z=1'b0;
end
endmodule
always @ (*) begin
if (sel == 3'b010)
x = a;
else
x = 1'b0;
end
always @ (*) begin
if (sel == 3'b100)
y=b;
else
y= 1'b0;
end
always @ (*) begin
if (sel == 3'b001)
z=c;
else
z=1'b0;
end
always @ (*) begin
x = 1'b0;
y = 1'b0;
z = 1'b0
if (sel == 3'b010)
x=a;
if (sel==3'b100)
y=b;
if(sel == 3'b001)
z=c
end
각각의 if 문을 통해서 latch를 제거할 수 있다.
전처리기를 활용한 latch 제거 방법
`define TEST_latch3
module my_latch(
input [0:2] sel,
input a,
input b,
input c,
`ifdef TEST_latch3
output x,
output y,
output z
`else
output reg x,
output reg y,
output reg z
`endif
);
`ifdef TEST_latch
always @(*) begin
if (sel == 3'b010)
x = a;
else
x = 1'b0;
if (sel == 3'b100)
y = b;
else
y = 1'b0;
if (sel == 3'b001)
z = c;
else
z = 1'b0;
end
`elsif TEST_latch3
assign x = (sel == 3'b010) ? a : 1'b0;
assign y = (sel == 3'b100) ? b : 1'b0;
assign z = (sel == 3'b001) ? c : 1'b0;
`elsif TEST_latch0
always @(*) begin
if (sel == 3'b001)
mux_out = a;
else if (sel == 3'b010)
mux_out = b;
else if (sel == 3'b100)
mux_out = c;
end
always @(*) begin
if (sel == 3'b001)
outp = a;
else if (sel == 3'b010)
outp = b;
else if (sel == 3'b100)
outp = c;
else
outp = 3'bx;
end
`elsif TEST_latch1
always @(*) begin
x = 1'b0;
y = 1'b0;
z = 1'b0;
if (sel == 3'b010)
x = a;
if (sel == 3'b100)
y = b;
if (sel == 3'b001)
z = c;
end
`elsif TEST_latch2
always @(*) begin
if (sel == 3'b010)
x = a;
else
x = 1'b0;
end
always @(*) begin
if (sel == 3'b100)
y = b;
else
y = 1'b0;
end
always @(*) begin
if (sel == 3'b001)
z = c;
else
z = 1'b0;
end
`endif
endmodule
- ifdef 참조 : (https://www.chipverify.com/verilog/verilog-ifdef-conditional-compilation)
마찬가지로 latch가 제거되었음.
IF_ELSE
'[Harman] 하만 반도체 설계 > VerilogHDL' 카테고리의 다른 글
Day.80 Advavnced Verilog #3 (0) | 2023.11.13 |
---|---|
Day.79 Advanced Verilog #2 (0) | 2023.11.10 |
Day24. Simple UART RX (0) | 2023.07.28 |
Day23. Simple UART TX,7-seg button&clock (0) | 2023.07.28 |
Day22. mult control, multiplexer 8x8 (0) | 2023.07.28 |