演算子を用いた Assign
CoddyのVerilogジャーニー「基礎」セクションの一部 — レッスン 38/90。
連続代入(continuous assignment)を理解すれば、演算子(operators)と組み合わせて役立つ論理回路を作成できます。assign ステートメントでは、任意の演算子を使用してワイヤをドライブできます。
基本構文
assign wire_name = expression;式には以下を含めることができます:
- 算術演算子 (
+,-,*,/) - ビット演算子 (
&,|,^,~) - 論理演算子 (
&&,||,!)
- 比較演算子 (
>,<,==,!=) - シフト演算子 (
<<,>>) - 条件演算子 (
? :)
さまざまな演算子の例
ビット単位AND:
assign out = a & b;加算:
assign sum = a + b;比較(Comparison):
assign is_greater = (a > b);条件(マルチプレクサ / Conditional):
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; // 条件付き(マルチプレクサ)
endmodule1つの代入文における複数の演算子
単一の式の中で演算子を組み合わせることができます:
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;チャレンジ
タスクに基づいて不足している 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: 代入文を追加:
// and_result = input_a & input_b
// or_result = input_a | input_b
// xor_result = input_a ^ input_b
// not_result = ~input_a
endmoduleこのレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。