結合演算子
CoddyのVerilogジャーニー「基礎」セクションの一部 — レッスン 27/90。
結合演算子 { } は、複数の信号、定数、または式を1つのより大きなベクトルに結合します。ビットをつなぎ合わせてより広い値を形成する必要がある場合は常にこれが使用されます。
結合に使用できる値
以下を結合できます:
- Wireおよびreg — すべての信号
- 定数 —
4'b1010や8'hFFなどの数値
- 式 (Expressions) —
a + bのような演算の結果 - 複製 (Replications) — 値を複数回繰り返すこと
結合されるすべての値は、固定された既知の幅を持つ必要があります。
基本構文: {value1, value2, value3, ...} 結果の幅は、個々の幅の合計になります。
例
2つの 4-bit の値を 8 bits に結合(Combine)する:
reg [3:0] high, low;
reg [7:0] word;
word = {high, low}; // high が上位4ビットになり、low が下位4ビットになる定数との結合:
data = {4'b1010, 4'b0000}; // 8'b101000003つ以上の結合:
full = {a, b, c, d}; // すべての幅が合計されるReplication
{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}とは異なります - すべての部分の幅が固定されている必要があります(サイズ未指定の数値は使用できません)
- 結合(Concatenation)は代入の左辺と右辺の両方で使用できます
チャレンジ
各タスクに対して正しい結合式を記述してください。
作業内容:
aとbを結合して8-bitの結果にし、combine1に格納しますc、d、eを結合して12-bitの結果にし、combine2に格納しますfと2つのgのコピーを結合して12-bitの結果にし、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このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。