Menguasai Fungsi Matematika Verilog untuk Desain Perangkat Keras yang Efisien
Fungsi matematika Verilog dapat digunakan sebagai ganti ekspresi konstan dan mendukung matematika integer dan real.
Fungsi Matematika Integer
Fungsi $clog2 mengembalikan ceiling dari log2 dari argumen yang diberikan. Biasanya digunakan untuk menghitung lebar minimum yang diperlukan untuk mengaddress memori dengan ukuran tertentu.
Misalnya, jika desain memiliki 7 penambal paralel, maka jumlah bit minimum yang diperlukan untuk mewakili semua 7 penambal adalah $clog2 dari 7 yang menghasilkan 3.
module des #(parameter NUM_UNITS = 7)
// Use of this system function helps to reduce the
// number of input wires to this module
(input [$clog2(NUM_UNITS)-1:0] active_unit);
initial $monitor("active_unit = %d", active_unit);
endmodule
`define NUM_UNITS 5
module tb;
integer i;
reg [`NUM_UNITS-1:0] active_unit;
des #(.NUM_UNITS(`NUM_UNITS)) u0(active_unit);
initial begin
active_unit = 1; #10 active_unit = 7; #10 active_unit = 8;
end
endmodule
Perhatikan bahwa sinyal active_unit memiliki 3-bit untuk menyimpan total 5 unit.
Log Simulasi
xcelium> run
active_unit = 001
active_unit = 111
active_unit = 000
xmsim: *W,RNQUIE: Simulation is complete.
Fungsi Matematika Real
Fungsi sistem ini menerima argumen real dan mengembalikan angka real.
| Function | Description |
|---|
| $ln(x) | Natural logarithm log(x) |
| $log10(x) | Decimal Logarithm log10(x) |
| exp(x) | Exponential of x (ex) where e=2.718281828... |
| sqrt(x) | Square root of x |
| $pow(x, y) | xy |
| $floor(x) | Floor x |
| $ceil(x) | Ceiling x |
| $sin(x) | Sine of x where x is in radians |
| $cos(x) | Cosine of x where x is in radians |
| $tan(x) | Tangent of x where x is in radians |
| $asin(x) | Arc-Sine of x |
| $acos(x) | Arc-Cosine of x |
| $atan(x) | Arc-tangent of x |
| $atan2(x, y) | Arc-tangent of x/y |
| $hypot(x, y) | Hypotenuse of x and y : sqrt(xx + yy) |
| $sinh(x) | Hyperbolic Sine of x |
| $cosh(x) | Hyperbolic-Cosine of x |
| $tanh(x) | Hyperbolic-Tangent of x |
| $asinh(x) | Arc-hyperbolic Sine of x |
| $acosh(x) | Arc-hyperbolic Cosine of x |
| $atanh(x) | Arc-hyperbolic tangent of x |
module tb;
real x, y;
initial begin
x = 10000;
$display("$log10(%0.3f) = %0.3f", x, $log10(x));
x = 1;
$display("$ln(%0.3f) = %0.3f", x, $ln(x));
x = 2;
$display("$exp(%0.3f) = %0.3f", x, $exp(x));
x = 25;
$display("$sqrt(%0.3f) = %0.3f", x, $sqrt(x));
x = 5; y = 3;
$display("$pow(%0.3f, %0.3f) = %0.3f", x, y, $pow(x, y));
x = 2.7813;
$display("$floor(%0.3f) = %0.3f", x, $floor(x));
x = 7.1111;
$display("$ceil(%0.3f) = %0.3f", x, $ceil(x));
x = 30 * (22.0/7.0) / 180; // convert 30 degrees to radians
$display("$sin(%0.3f) = %0.3f", x, $sin(x));
x = 90 * (22.0/7.0) / 180;
$display("$cos(%0.3f) = %0.3f", x, $cos(x));
x = 45 * (22.0/7.0) / 180;
$display("$tan(%0.3f) = %0.3f", x, $tan(x));
x = 0.5;
$display("$asin(%0.3f) = %0.3f rad, %0.3f deg", x, $asin(x), $asin(x) * 7.0/22.0 * 180);
x = 0;
$display("$acos(%0.3f) = %0.3f rad, %0.3f deg", x, $acos(x), $acos(x) * 7.0/22.0 * 180);
x = 1;
$display("$atan(%0.3f) = %0.3f rad, %f deg", x, $atan(x), $atan(x) * 7.0/22.0 * 180);
end
endmodule
Log Simulasi
xcelium> run
$log10(10000.000) = 4.000
$ln(1.000) = 0.000
$exp(2.000) = 7.389
$sqrt(25.000) = 5.000
$pow(5.000, 3.000) = 125.000
$floor(2.781) = 2.000
$ceil(7.111) = 8.000
$sin(0.524) = 0.500
$cos(1.571) = -0.001
$tan(0.786) = 1.001
$asin(0.500) = 0.524 rad, 29.988 deg
$acos(0.000) = 1.571 rad, 89.964 deg
$atan(1.000) = 0.785 rad, 44.981895 deg
xmsim: *W,RNQUIE: Simulation is complete.