Reduction Operators
CoddyのVerilogジャーニー「基礎」セクションの一部。レッスン 25/90。
リダクション演算子は、1つのベクターのすべてのビットに対して動作し、それらを1ビットの結果に縮約します。2つの数値をビットごとに比較するビット単位演算子とは異なり、リダクション演算子は1つの数値を受け取り、そのすべてのビットに対して演算を実行し、1つの結果を生成します。
| 演算子 | 演算 | 結果 |
|---|---|---|
& | リダクション AND | all bits が 1 の場合は 1 |
| | リダクション OR | at least one bit が 1 の場合は 1 |
^ | リダクション XOR | odd number のビットが 1 の場合は 1 |
~& | リダクション NAND | all bits が 1 の場合は 0 |
~| | リダクション NOR | at least one bit が 1 の場合は 0 |
~^ | リダクション XNOR | ビットの数が偶数で 1 の場合は 1 |
動作の仕組み
リダクション AND(<strong>&</strong>):
&4'b1111 = 1 // all bits are 1
&4'b1011 = 0 // すべてのビットが1ではない
&4'b0000 = 0 // all bits are 0リダクション OR(<strong>|</strong>):
|4'b0000 = 0 // no bits are 1
|4'b0100 = 1 // 少なくとも1つのビットが1
|4'b1111 = 1 // all bits are 1リダクション XOR(<strong>^</strong>):
^4'b1010 = 0 // two 1's (even) → 0
^4'b1000 = 1 // one 1 (odd) → 1
^4'b1111 = 0 // 4つの1 (偶数) → 0コード例
module reduction_demo;
reg [3:0] a, b, c;
reg and_red, or_red, xor_red;
initial begin
a = 4'b1111;
b = 4'b1010;
c = 4'b1000;
and_red = &a; // 1111 → 1
or_red = |b; // 1010 → 1
xor_red = ^c; // 1000 → 1
$display("&4'b1111 = %d", and_red);
$display("|4'b1010 = %d", or_red);
$display("^4'b1000 = %d", xor_red);
$finish;
end
endmodule出力:
&4'b1111 = 1
|4'b1010 = 1
^4'b1000 = 1一般的な用途
すべてのビットが 1 か確認する:
all_ones = &data; // data == 8'b11111111 の場合は 1いずれかのビットが 1 か確認する:
any_one = |data; // data != 0 の場合は 1パリティをチェックする(1 の数が奇数):
odd_parity = ^data; // 1の数が奇数の場合は1すべてのビットが 0 かどうかを確認します:
all_zeros = ~|data; // data == 0 の場合は 1チャレンジ
各タスクに対する正しいリダクション式を書いてください。
行うこと:
aのすべてのビットが 1 かどうかを確認し、all_onesに格納してくださいbのいずれかのビットが 1 かどうかを確認し、any_oneに格納してくださいcに 1 が奇数個あるかどうかを確認し、odd_parityに格納してください
自分で試してみよう
module reduction_challenge;
reg [3:0] a, b, c;
reg all_ones, any_one, odd_parity;
initial begin
a = 4'b1111;
b = 4'b0100;
c = 4'b1011;
all_ones = ______; // all bits 1?
any_one = ______; // any bit 1?
odd_parity = ______; // odd number of 1's?
$display("&4'b1111 = %d", all_ones);
$display("|4'b0100 = %d", any_one);
$display("^4'b1011 = %d", odd_parity);
$finish;
end
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オンラインコンパイラ