工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> Verilog

Verilog 連結

連結を使用して、マルチビット Verilog ワイヤと変数をまとめて、より大きなマルチネット ワイヤまたは変数を形成できます。 演算子 {} カンマで区切られた。連結では、ワイヤと変数に加えて、式とサイズ定数をオペランドとして使用することもできます。

連結の完全なサイズを計算するには、各オペランドのサイズを知る必要があります。

Verilog 連結の例

  
  
	wire 		a, b; 		// 1-bit wire
	wire [1:0]  res; 		// 2-bit wire to store a and b
	
	// res[1] follows a, and res[0] follows b
	assign res = {a, b}; 	
	
	
	wire [2:0]  c;
	wire [7:0] 	res1;
	
	// res[0]   follows c[2]
	// res[2:1] is always 0
	// res[4:3] follows c[1:0]
	// res[5]   follows a
	// res[6]   follows b
	assign res1 = {b, a, c[1:0], 2'b00, c[2]};

  

以下は、入力を連結して異なる出力を形成する実際の設計例です。連結された式は、単に表示したり、任意のワイヤまたは変数に割り当てたりすることができますが、必ずしも出力する必要はありません。

  
  
module des (input [1:0] 	a,
            input [2:0] 	b,
            output [4:0]	out1,
            output [3:0] 	out2            
           );
  
  assign out1 = {a, b};
  assign out2 = {a[1], 2'b01, b[2]};
  
endmodule  

module tb;
  reg [1:0] a;
  reg [2:0] b;
  wire [4:0] out1;
  wire [3:0] out2;
  
  des u0 (a, b, out1, out2);
  
  initial begin
    a <= 0;
    b <= 0;
    
    $monitor("[%0t] a=%b b=%b, out1=%b out2=%b", $time, a, b, out1, out2);
    
    #10 a <= 3;
    #5  b <= 5;
    #10 a <= 2;
    #5  b <= 1;
    
    #10 $finish;
  end
endmodule

  

out2[2:1] は常に定数 2'b01 であることに注意してください。

シミュレーションログ
xcelium> run
[0] a=00 b=000, out1=00000 out2=0010
[10] a=11 b=000, out1=11000 out2=1010
[15] a=11 b=101, out1=11101 out2=1011
[25] a=10 b=101, out1=10101 out2=1011
[30] a=10 b=001, out1=10001 out2=1010
Simulation complete via $finish(1) at time 40 NS + 0

レプリケーション オペレータ

同じ式を何度も繰り返さなければならない場合、複製定数 負でない数値である必要があり、X、Z、または任意の変数にすることはできません。この定数も、元の連結演算子とともに中かっこで囲まれ、式が繰り返される合計回数を示します。

  
  
	wire a;
	wire [6:0] res;
	
	assign res = {7{a}};
	
	{2'bz{2'b0}}         // Illegal to have Z as replication constant
	{2'bx{2'b0}}         // Illegal to have X as replication constant

  

複製式は、割り当ての左側に表示できず、output に接続できません。 または inout ポート。

  
  
module des;
  reg [1:0] a;
  reg [2:0] b;
  
  initial begin
    a <= 2;
    b <= 4;
    
    #10;
    $display("a=%b b=%b res=%b", a, b, {{2{a}}, {3{b}}});
  end
  
endmodule

  

a が 2 回繰り返され、b が 3 回繰り返されたことに注意してください。

シミュレーションログ
xcelium> run
a=10 b=100 res=1010100100100
xmsim: *W,RNQUIE: Simulation is complete.

オペランドは、定数がゼロであっても、レプリケーション式が実行されるときに 1 回だけ評価されます。

ネストされたレプリケーション

レプリケーション式は、正規の連結式の中で使用できます。上記の例をベースにすると、a と b が合計連結式に含まれています。

  
  
module des;
  reg [1:0] a;
  reg [2:0] b;
  
  initial begin
    a <= 2;
    b <= 4;
    
    #10;
    $display("a=%b b=%b res=%b", a, b, {a, b, 3'b000, {{2{a}}, {3{b}}}});
  end
  
endmodule

  
シミュレーションログ
xcelium> run
a=10 b=100 res=101000001010100100100
xmsim: *W,RNQUIE: Simulation is complete.

違法な使用

  
  
  module des;
    reg [1:0] a;
    reg [2:0] b;
    reg [3:0] _var;

    initial begin
      a <= 2;
      b <= 4;
      _var <= 3;

      // This is illegal because variables cannot be used
      // as replication constant
      $display("a=%b b=%b res=%b", a, b, {_var{a}});
    end
  endmodule

  

これにより、以下に示すようなコンパイル エラーが発生します。

シミュレーションログ
	Top level design units:
		des
      $display("a=%b b=%b res=%b", a, b, {_var{a}});
                                             |
xmelab: *E,NOTPAR (./testbench.sv,12|45): Illegal operand for constant expression [4(IEEE)].


Verilog

  1. Verilog チュートリアル
  2. Verilog 割り当て
  3. Verilog ブロッキング &ノンブロッキング
  4. Verilog 関数
  5. Verilog タスク
  6. Verilog クロック ジェネレーター
  7. Verilog 数学関数
  8. Verilog タイムフォーマット
  9. Verilog タイムスケール スコープ
  10. Verilog ファイルの IO 操作
  11. Verilog ハローワールド