Menu
Coddy logo textTech

結合演算子

CoddyのVerilogジャーニー「基礎」セクションの一部 — レッスン 27/90。

結合演算子 { } は、複数の信号、定数、または式を1つのより大きなベクトルに結合します。ビットをつなぎ合わせてより広い値を形成する必要がある場合は常にこれが使用されます。

結合に使用できる値

以下を結合できます:

  • Wireおよびreg — すべての信号
  • 定数4'b10108'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'b10100000

3つ以上の結合:

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)は代入の左辺と右辺の両方で使用できます
challenge icon

チャレンジ

各タスクに対して正しい結合式を記述してください。

作業内容:

  1. ab を結合して8-bitの結果にし、combine1 に格納します
  2. cde を結合して12-bitの結果にし、combine2 に格納します
  3. 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
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

基礎のすべてのレッスン