RGBR누르면 열리는 도어락을 베릴로그로 짜면 되는데
이론과 실습의 갭은 너무 크다..
우선 코드는 밑에와 같이 만들었는데 문제는 TestBench야 도통 클락사이를 어떻게 짜야될지 모르겠어 맨 밑에 테스트벤치 한데까지 써볼게
`timescale 1 ns/1 ns
module Code_Detector(Start, Red, Green, Blue, Clk, Rst, U);
input Start, Red, Green, Blue;
output reg U;
input Clk, Rst;
parameter Wait_state = 000, Start_state = 001, Red1_state = 010, Blue_state = 011,
Green_state = 100, Red2_state = 101; //don't need to 110, 111
reg [1:0] State, StateNext;
// StateReg
always @(posedge Clk) begin
if (Rst == 1 )
State <= Wait_state;
else
State <= StateNext;
end // StateReg end
// CombLogic
always @(State,Start, Red, Green, Blue, Clk, Rst)
begin
U <= 0;
case(State)
Wait_state: begin
if (Start == 0)
StateNext <= Wait_state;
else
StateNext <= Start_state;
end
Start_state: begin
if (Red == 1 && Green == 0 && Blue == 0)
StateNext <= Red1_state;
else if (Red == 1 && Green == 1 && Blue == 1)
StateNext <= Start_state;
else //b+g
StateNext <= Wait_state;
end
Red1_state: begin
if (Red == 0 && Green == 0 && Blue == 1)
StateNext <= Blue_state;
else if (Red == 1 && Green == 1 && Blue == 1)
StateNext <= Red1_state;
else
StateNext <= Wait_state;
end
Blue_state: begin
if (Red == 0 && Green == 1 && Blue == 0)
StateNext <= Blue_state;
else if (Red == 1 && Green == 1 && Blue == 1)
StateNext <= Blue_state;
else
StateNext <= Wait_state;
end
Green_state: begin
if (Red == 0 && Green == 1 && Blue == 0)
StateNext <= Red2_state;
else if (Red == 1 && Green == 1 && Blue == 1)
StateNext <= Green_state;
else
StateNext <= Wait_state;
end
Red2_state: begin
U <= 1;
StateNext <= Wait_state;
end
default: begin //don't use 110, 111
U <= 0;
StateNext <= Wait_state;
end
endcase //case end
end// CombLogic end
endmodule
중요한건 위에 코드 디텍터도 안맞을지 모름ㅋㅋ
테스트코드는 밑에.. 보다시피 for문이 뻥 뚫려있어
state를 하나씩 끝나고 하나씩 넘겨야 할텐데 0~2/3~5/6~8/9~11을 하나씩 넣어서 뭘 비교하면 될텐데
도통 감이 안와 ㅠㅠ 아이디어라도 던져줘..
//code_detector_TB.v
`timescale 1 ns/1 ns
module Testbench();
reg Start_s, Red_s, Green_s, Blue_s, Clk_s, Rst_s;
reg [11:0] Anser_Index;
reg [11:0] Index; //12bits 4096 test input
wire U_s;
Code_Detector CompToTest(Start_s, Red_s, Green_s, Blue_s, Clk_s, Rst_s, U_s);
initial begin
Rst_s <= 0;
Start_s <= 1; //now start state
Index <= 12'b0000_0000_0000;
for(Index=0; Index<12'b111_111_111_111; Index=Index+12'b000_000_000_001)
begin
//
@(posedge Clk_s);
#5
//
@(posedge Clk_s);
#5
//
@(posedge Clk_s);
#5
if (Index != 12'b100_001_010_100)
begin
$display("# %d: %b is incorrect", Index, Index);
end
else
begin
$display("# %d: %b is correct", Index, Index);
Anser_Index <= Index;
end
end // end for loop
if (U_s==1)
$display("# %d: %b is correct", Anser_Index, Anser_Index);
$stop;
end
endmodule
테스트벤치에서 인덱스는 하나씩 증가하면 될테고.. 겨스님말로는 3개를 넣고 그다음 포스엣지로 클럭하나 끝내고 , 두번째고 넣고 끝내고, 세번째고 넣고 끝내고, 네번째도 넣고 끝내고 => 그리고 이게 하나의 case 다 그리고 그게 4096번이 될 테고, 도어락은 그중에 RGBR(12bits) 순서일때만 한 번 열린다. 라는데 뭘 어떻게 넣으라는거지..?
state에 3비트씩 저장하라는건가?ㅠㅠ나는 state는 reg선언했는데 구러면 reg [2:0] 선언해야 하는거? 각각? 그리고, R누르고 G말고 R또 누르면 다시 start로 돌아가는건 테스트벤치에 어떻게 구현하라는거.. 그냥 모두 incorrect인 상태로, 확인만 되면 되는건감
필요한건 다 써져있네. 성공케이스랑 실패케이스 한번씩 돌려 본 다음 잘 들여다보면 할 수 있게 될꺼임. 그래도 못하겠으면 술이나 퍼
테스트벤치에서 reg Start_s, Red_s, Green_s, Blue_s, Clk_s, Rst_s; 이거말고 reg [2:0]Start_s, Red_s, Green_s, Blue_s, Clk_s, Rst_s; 이렇게 선언해주는게 맞는거야..? 그리고 세개씩 집어넣으면 됨 포스엣지 사이마다..?
내가 아직 웨이브폼 보는 법을 잘 몰라서 코드짜지도 못하지만 코드를 짜도 제대로 된건지 아닌지 몰라 ㅠㅠ 가지말고 좀 봐봐 ㅠㅠ 그렇게 각 state마다 숫자를 바꿔줘서 넣기만 하면 돼? 테스트코드 첨짜본다늉 좀 봐봐 가지말고..ㅜㅜ!!!!!!
Index12비트짜리에서 3개씩은 어떻게 떼가지구 옴...?... ㅇ..아...눙물난다 진짜 나 너무 모르는것같아 빡친다 검색해도 안나오고