연산자를 사용한 할당
Coddy Verilog 여정의 기초 섹션에 포함된 레슨 — 90개 중 38번째.
연속 할당(continuous assignment)을 이해하고 나면, 이를 연산자와 결합하여 유용한 로직을 생성할 수 있습니다. assign 구문은 모든 연산자를 사용하여 wire를 구동할 수 있습니다.
기본 구문
assign wire_name = expression;식(expression)에는 다음이 포함될 수 있습니다:
- 산술 연산자 (
+,-,*,/) - 비트 연산자 (
&,|,^,~) - 논리 연산자 (
&&,||,!)
- 비교 연산자 (
>,<,==,!=) - 시프트 연산자 (
<<,>>) - 조건 연산자 (
? :)
다른 연산자를 사용한 예제
비트 AND:
assign out = a & b;덧셈:
assign sum = a + b;비교:
assign is_greater = (a > b);조건문 (multiplexer):
assign out = sel ? a : b;Shift:
assign shifted = data << 2;Concatenation:
assign bus = {high_byte, low_byte};코드 예제
module assign_operators (
input [3:0] a, b,
input sel,
output [3:0] and_out,
output [4:0] sum_out,
output is_equal,
output mux_out
);
assign and_out = a & b; // 비트 단위 AND
assign sum_out = a + b; // 덧셈
assign is_equal = (a == b); // 비교
assign mux_out = sel ? a : b; // 조건부 (멀티플렉서)
endmodule하나의 할당문에서 여러 연산자 사용하기
단일 표현식 내에서 연산자를 조합하여 사용할 수 있습니다:
assign result = (a & b) | (c ^ d);
assign final = (a + b) > (c - d);
assign parity = ^data; // Reduction XOR (1의 개수가 홀수)연산자 우선순위 (Operator Precedence)
Verilog는 표준 연산자 우선순위를 따릅니다. 의도를 명확하게 표현하기 위해 괄호 ( )를 사용하세요:
// 불명확
assign out = a & b | c;
// 명확
assign out = (a & b) | c;챌린지
과제에 따라 누락된 assign 구문을 추가하세요.
수행할 작업:
and_result가input_a AND input_b(비트 연산)와 같아지도록 작성하세요.or_result가input_a OR input_b(비트 연산)와 같아지도록 작성하세요.xor_result가input_a XOR input_b(비트 연산)와 같아지도록 작성하세요.not_result가NOT input_a(비트 연산)와 같아지도록 작성하세요.
직접 해보기
module assign_challenge (
input input_a,
input input_b,
output and_result,
output or_result,
output xor_result,
output not_result
);
// TODO: 다음에 대한 assign 문을 추가하세요:
// and_result = input_a & input_b
// or_result = input_a | input_b
// xor_result = input_a ^ input_b
// not_result = ~input_a
endmodule이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.