Menu
CoddyTech

연산자를 사용한 할당

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

연속 할당을 이해하고 나면 연산자와 결합하여 유용한 논리를 만들 수 있습니다. assign 문은 어떤 연산자든 사용하여 와이어를 구동할 수 있습니다.

기본 구문

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 [3:0] 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_result가 input_a AND input_b와 같도록 하세요(비트 단위).
  2. or_result가 input_a OR input_b와 같도록 하세요(비트 단위).
  3. xor_result가 input_a XOR input_b와 같도록 하세요(비트 단위).
  4. 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
quiz icon실력 점검

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

기초의 모든 레슨

직접 연습해 보세요: 온라인 Verilog 컴파일러