Concatenation Operator
CoddyのVerilogジャーニー「基礎」セクションの一部。レッスン 27/90。
連結演算子 { } は、複数の信号、constants、または式を結合して、1つのより大きなベクターにします。ビットをつなぎ合わせて、より幅の広い値を形成する必要がある場合に使用します。
連結で使用できる値
連結できるもの:
- ワイヤとレジスタ:任意の信号
- 定数:
4'b1010や8'hFFのような数値
- 式:
a + bのような演算の結果 - 複製: 値を複数回繰り返すこと
連結するすべての値には、固定された既知の幅が必要です。
基本構文: {value1, value2, value3, ...} 結果の幅は、個々の幅の合計です。
例
2つの4ビット値を8ビットに結合する:
reg [3:0] high, low;
reg [7:0] word;
word = {high, low}; // high が上位 4 ビットになり、low が下位 4 ビットになる定数と結合:
data = {4'b1010, 4'b0000}; // 8'b101000002つより多くをCombineする:
full = {a, b, c, d}; // すべての幅が合計される複製
{n{value}} を使用すると、値を複数回繰り返せます:
repeated = {4{4'b1010}}; // 16'b1010101010101010 (repeat 4 times)これは符号拡張に便利です:
signed_8bit = {{4{sign_bit}}, value_4bit};コード例
module concatenation_demo;
reg [3:0] upper, lower;
reg [7:0] combined;
reg [11:0] repeated;
initial begin
upper = 4'b1010;
lower = 4'b1100;
combined = {upper, lower}; // 10101100
repeated = {3{4'b1010}}; // 101010101010
$display("{upper, lower} = %b", combined);
$display("{3{4'b1010}} = %b", repeated);
$finish;
end
endmodule出力:
{upper, lower} = 10101100
{3{4'b1010}} = 101010101010重要な注意事項
- 順序が重要です:
{a, b}は{b, a}とは異なります - すべての部分には固定幅が必要です(サイズ指定のない数値は不可)
- 連結は代入の左辺と右辺の両方で使用できます
チャレンジ
各タスクに対する正しい連結式を書いてください。
行うこと:
aとbを 8 ビットの result に Combine し、combine1に格納するc、d、eを 12 ビットの result に Combine し、combine2に格納するfとgの two copies を 12 ビットの result に Combine し、combine3に格納する
自分で試してみよう
module concatenation_challenge;
reg [3:0] a, b;
reg [3:0] c, d, e;
reg [3:0] f, g;
reg [7:0] combine1;
reg [11:0] combine2, combine3;
initial begin
a = 4'b1010;
b = 4'b0101;
c = 4'b1111;
d = 4'b0000;
e = 4'b1100;
f = 4'b1001;
g = 4'b0110;
combine1 = ______; // Combine a and b into an 8-bit result
combine2 = ______; // Combine c, d, and e into a 12-bit result
combine3 = ______; // Combine f and two copies of g into a 12-bit result
$display("{a, b} = %b", combine1);
$display("{c, d, e} = %b", combine2);
$display("{f, g, g} = %b", combine3);
$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オンラインコンパイラ