Pemodelan Tingkat Gerbang: Menghubungkan Skema Hardware ke Kode Verilog
Sebagian besar desain digital dilakukan pada tingkat abstrak yang lebih tinggi seperti RTL, meskipun terkadang menjadi intuitif untuk membangun rangkaian deterministik yang lebih kecil pada tingkat yang lebih rendah dengan menggunakan elemen kombinatorial seperti and dan or. Pemodelan pada tingkat ini biasanya disebut pemodelan tingkat gerbang karena melibatkan gerbang dan memiliki hubungan satu ke satu antara skema hardware dan kode Verilog.
Verilog mendukung beberapa gerbang logika dasar yang dikenal sebagai primitives karena dapat diinstantiasi seperti modul karena sudah ditetapkan sebelumnya.
Gerbang And/Or/Xor
Primitive ini mengimplementasikan gerbang AND dan OR yang menerima banyak input skalar dan memberikan satu output skalar. Terminal pertama dalam daftar argumen ke primitive ini adalah output yang diperbarui setiap kali salah satu input berubah.
module gates (
input a, b,
output c, d, e
);
and (c, a, b); // c is the output, a and b are inputs
or (d, a, b); // d is the output, a and b are inputs
xor (e, a, b); // e is the output, a and b are inputs
endmodule
module tb;
reg a, b;
wire c, d, e;
integer i;
gates u0 (
.a(a), .b(b), .c(c), .d(d), .e(e)
);
initial begin
{a, b} = 0;
$monitor ("[T=%0t a=%0b b=%0b c(and)=%0b d(or)=%0b e(xor)=%0b", $time, a, b, c, d, e);
for (i = 0; i < 10; i = i+1) begin
#1 a <= $random; b <= $random;
end
end
endmodule
Log Simulasi
ncsim> run
[T=0 a=0 b=0 c(and)=0 d(or)=0 e(xor)=0
[T=1 a=0 b=1 c(and)=0 d(or)=1 e(xor)=1
[T=2 a=1 b=1 c(and)=1 d(or)=1 e(xor)=0
[T=4 a=1 b=0 c(and)=0 d(or)=1 e(xor)=1
[T=5 a=1 b=1 c(and)=1 d(or)=1 e(xor)=0
[T=6 a=0 b=1 c(and)=0 d(or)=1 e(xor)=1
[T=7 a=1 b=0 c(and)=0 d(or)=1 e(xor)=1
[T=10 a=1 b=1 c(and)=1 d(or)=1 e(xor)=0
ncsim: *W,RNQUIE: Simulation is complete.
Gerbang Nand/Nor/Xnor
Invers dari semua gerbang di atas juga tersedia dalam bentuk nand, nor dan xnor. Desain yang sama dari di atas digunakan kembali dengan pengecualian bahwa primitives diganti dengan versi inversnya.
module gates (
input a, b,
output c, d, e
);
// Use nand, nor, xnor instead of and, or and xor
// in this example
nand (c, a, b); // c is the output, a and b are inputs
nor (d, a, b); // d is the output, a and b are inputs
xnor (e, a, b); // e is the output, a and b are inputs
endmodule
module tb;
reg a, b;
wire c, d, e;
integer i;
gates u0 (
.a(a), .b(b), .c(c), .d(d), .e(e)
);
initial begin
{a, b} = 0;
$monitor ("[T=%0t a=%0b b=%0b c(nand)=%0b d(nor)=%0b e(xnor)=%0b", $time, a, b, c, d, e);
for (i = 0; i < 10; i = i+1) begin
#1 a <= $random; b <= $random;
end
end
endmodule
Log Simulasi
ncsim> run
[T=0 a=0 b=0 c(nand)=1 d(nor)=1 e(xnor)=1
[T=1 a=0 b=1 c(nand)=1 d(nor)=0 e(xnor)=0
[T=2 a=1 b=1 c(nand)=0 d(nor)=0 e(xnor)=1
[T=4 a=1 b=0 c(nand)=1 d(nor)=0 e(xnor)=0
[T=5 a=1 b=1 c(nand)=0 d(nor)=0 e(xnor)=1
[T=6 a=0 b=1 c(nand)=1 d(nor)=0 e(xnor)=0
[T=7 a=1 b=0 c(nand)=1 d(nor)=0 e(xnor)=0
[T=10 a=1 b=1 c(nand)=0 d(nor)=0 e(xnor)=1
ncsim: *W,RNQUIE: Simulation is complete.
Gerbang ini dapat memiliki lebih dari dua input.
module gates (
input a, b, c, d,
output x, y, z
);
and (x, a, b, c, d); // x is the output, a, b, c, d are inputs
or (y, a, b, c, d); // y is the output, a, b, c, d are inputs
nor (z, a, b, c, d); // z is the output, a, b, c, d are inputs
endmodule
module tb;
reg a, b, c, d;
wire x, y, z;
integer i;
gates u0 (
.a(a), .b(b), .c(c), .d(d), .x(x), .y(y), .z(z)
);
initial begin
{a, b, c, d} = 0;
$monitor ("[T=%0t a=%0b b=%0b c=%0b d=%0b x=%0b y=%0b x=%0b", $time, a, b, c, d, x, y, z);
for (i = 0; i < 10; i = i+1) begin
#1 a <= $random; b <= $random; c <= $random; d <= $random;
end
end
endmodule
Log Simulasi
ncsim> run
[T=0 a=0 b=0 c=0 d=0 x=0 y=0 x=1
[T=1 a=0 b=1 c=1 d=1 x=0 y=1 x=0
[T=2 a=1 b=1 c=1 d=0 x=0 y=1 x=0
[T=3 a=1 b=1 c=0 d=1 x=0 y=1 x=0
[T=4 a=1 b=0 c=1 d=0 x=0 y=1 x=0
[T=5 a=1 b=0 c=1 d=1 x=0 y=1 x=0
[T=6 a=0 b=1 c=0 d=0 x=0 y=1 x=0
[T=7 a=0 b=1 c=0 d=1 x=0 y=1 x=0
[T=8 a=1 b=1 c=1 d=0 x=0 y=1 x=0
[T=9 a=0 b=0 c=0 d=1 x=0 y=1 x=0
[T=10 a=0 b=1 c=1 d=1 x=0 y=1 x=0
ncsim: *W,RNQUIE: Simulation is complete.
Gerbang Buf/Not
Gerbang ini hanya memiliki satu input skalar dan satu atau lebih output. buf adalah singkatan dari buffer yang cukup mentransfer nilai dari input ke output tanpa perubahan polaritas. not adalah singkatan dari inverter yang membalik polaritas sinyal di inputnya. Jadi 0 di inputnya akan menghasilkan 1 dan sebaliknya.
module gates (
input a,
output c, d
);
buf (c, a); // c is the output, a is input
not (d, a); // d is the output, a is input
endmodule
module tb;
reg a;
wire c, d;
integer i;
gates u0 (
.a(a), .c(c), .d(d)
);
initial begin
a = 0;
$monitor ("[T=%0t a=%0b c(buf)=%0b d(not)=%0b", $time, a, c, d);
for (i = 0; i < 10; i = i+1) begin
#1 a <= $random;
end
end
endmodule
Log Simulasi
xcelium> run
[T=0 a=0 c(buf)=0 d(not)=1
[T=2 a=1 c(buf)=1 d(not)=0
[T=8 a=0 c(buf)=0 d(not)=1
[T=9 a=1 c(buf)=1 d(not)=0
xmsim: *W,RNQUIE: Simulation is complete.
Terminal terakhir dalam daftar port terhubung ke input gerbang dan semua terminal lain terhubung ke port output gerbang. Berikut adalah contoh buffer multi-output, meskipun jarang digunakan.
module gates (
input a,
output c, d
);
not (c, d, a); // c,d is the output, a is input
endmodule
Log Simulasi
xcelium> run
[T=0 a=0 c=1 d=1
[T=2 a=1 c=0 d=0
[T=8 a=0 c=1 d=1
[T=9 a=1 c=0 d=0
xmsim: *W,RNQUIE: Simulation is complete.
Gerbang Bufif/Notif
Buffer dan Inverter dengan sinyal kontrol tambahan untuk mengaktifkan output tersedia melalui primitive bufif dan notif. Gerbang ini memiliki output yang valid hanya jika sinyal kontrol diaktifkan, jika tidak output akan berada dalam impedansi tinggi. Terdapat dua versi ini, satu dengan polaritas kontrol normal yang ditunjukkan oleh 1 seperti bufif1 dan notif1 dan yang kedua dengan polaritas kontrol terbalik yang ditunjukkan oleh 0 seperti bufif0 dan notif0.