Verilog Concatenation meistern: Praktischer Leitfaden
Multi-Bit Verilog Wires/Variables können zu größeren Multi-Net Wires/Variables vereint werden mittels Concatenation Operatoren { und }, durch Kommas getrennt. Erlaubt auch Expressions und Sized Constants als Operanden.
Größe jedes Operanden muss bekannt sein, um Gesamtgröße zu berechnen.
Concatenation Beispiel
wire a, b; // 1-Bit
wire [1:0] res; // 2-Bit für a,b
assign res = {a, b}; // res[1]=a, res[0]=b
wire [2:0] c;
wire [7:0] res1;
assign res1 = {b, a, c[1:0], 2'b00, c[2]};
// res1[7]=b, [6]=a, [5:4]=c[1:0], [3:2]=0, [1]=c[2], [0]=?
Funktionierendes Design-Beispiel
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
Simulationslog: out2[2:1] immer konstant 2'b01.
Replication Operator
Wiederholung desselben Expressions: {N{expr}}. N = Replication Constant (nicht-negativ, nicht X/Z/Variable). In Braces mit Concatenation-Operator.
wire a; wire [6:0] res;
assign res = {7{a}}; // a 7-mal wiederholt
{2'bz{2'b0}} // Illegal: Z als Replication Constant
{2'bx{2'b0}} // Illegal: X als Replication Constant
Replication Expressions nicht auf LHS von Assignments, nicht an output/inout Ports anschließbar.
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
Operanden werden nur einmal evaluiert (auch bei Constant 0).
Verschachtelte Replication (Nested Replication)
Replication Expression innerhalb regulärer Concatenation erlaubt.
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
Illegale Nutzung
Variable als Replication Constant → Compile Error.
module des;
reg [1:0] a; reg [2:0] b; reg [3:0] _var;
initial begin a <= 2; b <= 4; _var <= 3;
$display("a=%b b=%b res=%b", a, b, {_var{a}}); // Error: Illegal operand for constant expression
end
endmodule
Verilog
- Verilog meistern: Komplettes Tutorial für digitales Schaltungsdesign
- Verilog Assignments verstehen: Typen, Syntax und Best Practices
- Verilog Blocking vs. Non-Blocking: Wichtige Unterschiede erklärt
- Verilog Functions meistern für effizientes RTL-Design
- Verilog Tasks verstehen: Functions vs. Tasks, Syntax und Verwendung
- Verilog Clock Generator: Design & Implementierungsleitfaden
- Verilog Math Functions meistern für effizientes Hardware-Design
- Verilog $timeformat meistern: Präzision und Anzeigesteuerung
- Verilog Timescales an jedem Scope verstehen und ausgeben
- Datei-E/A in Verilog meistern: Öffnen, Lesen, Schreiben und Schließen