Assign With Operators
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このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
基礎のすべてのレッスン
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic自分で練習してみよう: Verilogオンラインコンパイラ