verilator/test_regress/t/t_recursive_method.v

65 lines
1.5 KiB
Systemverilog
Raw Normal View History

2023-03-02 03:07:37 +01:00
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2023 Antmicro Ltd
2023-03-02 03:07:37 +01:00
// SPDX-License-Identifier: CC0-1.0
class Fib;
2026-03-10 02:38:29 +01:00
function int get_fib(int n);
if (n == 0 || n == 1) return n;
else return get_fib(n - 1) + get_fib(n - 2);
endfunction
2023-03-02 03:07:37 +01:00
endclass
class FibStatic;
2026-03-10 02:38:29 +01:00
static function int get_fib(int n);
if (n == 0 || n == 1) return n;
else return get_fib(n - 1) + get_fib(n - 2);
endfunction
2023-03-02 03:07:37 +01:00
endclass
class Factorial;
2026-03-10 02:38:29 +01:00
static function int factorial(int n);
return fact(n, 1);
endfunction
static function int fact(int n, int acc);
if (n < 2) fact = acc;
else fact = fact(n - 1, acc * n);
endfunction
2023-03-02 03:07:37 +01:00
endclass
2026-03-10 02:38:29 +01:00
class Getter3 #(
int T = 5
);
static function int get_3();
if (T == 3) return 3;
else return Getter3#(3)::get_3();
endfunction
2023-03-02 03:07:37 +01:00
endclass
module t;
2023-03-02 03:07:37 +01:00
2026-03-10 02:38:29 +01:00
initial begin
automatic Fib fib = new;
automatic Getter3 getter3 = new;
2023-03-02 03:07:37 +01:00
2026-03-10 02:38:29 +01:00
if (fib.get_fib(0) != 0) $stop;
if (fib.get_fib(1) != 1) $stop;
if (fib.get_fib(8) != 21) $stop;
2023-03-02 03:07:37 +01:00
2026-03-10 02:38:29 +01:00
if (FibStatic::get_fib(0) != 0) $stop;
if (FibStatic::get_fib(1) != 1) $stop;
if (FibStatic::get_fib(8) != 21) $stop;
2023-03-02 03:07:37 +01:00
2026-03-10 02:38:29 +01:00
if (Factorial::factorial(0) != 1) $stop;
if (Factorial::factorial(1) != 1) $stop;
if (Factorial::factorial(6) != 720) $stop;
2023-03-02 03:07:37 +01:00
2026-03-10 02:38:29 +01:00
if (getter3.get_3() != 3) $stop;
if (Getter3#(3)::get_3() != 3) $stop;
2023-03-02 03:07:37 +01:00
2026-03-10 02:38:29 +01:00
$write("*-* All Finished *-*\n");
$finish;
end
2023-03-02 03:07:37 +01:00
endmodule