Menu
Coddy logo textTech

연산자를 사용한 할당

Coddy Verilog 여정의 기초 섹션에 포함된 레슨 — 90개 중 38번째.

연속 할당(continuous assignment)을 이해하고 나면, 이를 연산자(operators)와 결합하여 유용한 로직을 만들 수 있습니다. assign 문은 어떤 연산자든 사용하여 와이어(wire)를 구동할 수 있습니다.

기본 구문

assign wire_name = expression;

표현식에는 다음이 포함될 수 있습니다:

  • 산술 연산자 (+, -, *, /)
  • 비트 연산자 (&, |, ^, ~)
  • 논리 연산자 (&&, ||, !)
  • 비교 연산자 (>, <, ==, !=)
  • 시프트 연산자 (<<, >>)
  • 조건 연산자 (? :)

다양한 연산자를 사용한 예제

비트 단위 AND:

assign out = a & b;

덧셈:

assign sum = a + b;

비교:

assign is_greater = (a > b);

조건문 (멀티플렉서):

assign out = sel ? a : b;

시프트:

assign shifted = data << 2;

결합:

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;   // 리덕션 XOR (1의 개수가 홀수임)

연산자 우선순위

Verilog는 표준 연산자 우선순위를 따릅니다. 의도를 명확하게 하기 위해 괄호 ( )를 사용하세요:

// 불분명함
assign out = a & b | c;

// 명확함
assign out = (a & b) | c;
challenge icon

챌린지

작업 내용에 따라 누락된 assign 문을 추가하세요.

수행할 작업:

  1. and_resultinput_a AND input_b (비트 연산)와 같게 만듭니다.
  2. or_resultinput_a OR input_b (비트 연산)와 같게 만듭니다.
  3. xor_resultinput_a XOR input_b (비트 연산)와 같게 만듭니다.
  4. not_resultNOT input_a (비트 연산)와 같게 만듭니다.

치트 시트

assign 문은 조합 논리(combinational logic)를 위한 다양한 연산자를 지원합니다:

assign wire_name = expression;

연산자 유형:

  • 비트 연산(Bitwise): &, |, ^, ~
  • 산술 연산(Arithmetic): +, -, *, /
  • 논리 연산(Logical): &&, ||, !
  • 비교 연산(Comparison): >, <, ==, !=
  • 시프트 연산(Shift): <<, >>
  • 조건 연산(Conditional): ? :
  • 결합 연산(Concatenation): { }
assign and_out  = a & b;          // Bitwise AND
assign sum_out  = a + b;          // Addition
assign is_equal = (a == b);       // Comparison
assign mux_out  = sel ? a : b;    // Multiplexer
assign bus      = {high, low};    // Concatenation
assign parity   = ^data;          // Reduction XOR

우선순위를 명확하게 하려면 괄호를 사용하세요:

assign out = (a & b) | c;

직접 해보기

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
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

기초의 모든 레슨