Menu
Coddy logo textTech

Concatenation Operator

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

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

連結で使用できる値

連結できるもの:

  • ワイヤとレジスタ:任意の信号
  • 定数4'b10108'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'b10100000

2つより多くを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} とは異なります
  • すべての部分には固定幅が必要です(サイズ指定のない数値は不可)
  • 連結は代入の左辺と右辺の両方で使用できます
challenge icon

チャレンジ

各タスクに対する正しい連結式を書いてください。

行うこと:

  1. ab を 8 ビットの result に Combine し、combine1 に格納する
  2. cde を 12 ビットの result に Combine し、combine2 に格納する
  3. fg の 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
quiz icon腕試し

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

基礎のすべてのレッスン

自分で練習してみよう: Verilogオンラインコンパイラ