OpenCores
URL https://opencores.org/ocsvn/tv80/tv80/trunk

Subversion Repositories tv80

[/] [tv80/] [trunk/] [tests/] [fib.c] - Blame information for rev 98

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ghutchis
// Recursively compute fibonnaci sequence, using a 
2
// really inefficient algorithm.
3
// (Stack exercise test)
4
 
5
#include "tv80_env.h"
6
 
7
int answers[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
8
                  233, 377, 610, 987, 1597, 2584, 4181 };
9
 
10
int fib (int n)
11
{
12
  int rv;
13
  timeout_port = 0x02;
14
 
15
  if (n < 2) rv = n;
16
  else rv = fib(n-1) + fib(n-2);
17
 
18
  timeout_port = 0x01;
19
  return rv;
20
}
21
 
22
int main ()
23
{
24
  int fn, fr;
25
  char pass;
26
 
27
  set_timeout (60000);
28
  pass = 1;
29
 
30
  for (fn = 1; fn < 20; fn++) {
31
    print ("Computing Fibonacci number ");
32
    print_num (fn);
33
    print ("\n");
34
 
35
    fr = fib(fn);
36
    print ("Number is: ");
37
    print_num (fr);
38
 
39
    if (fr == answers[fn-1]) {
40
      print (" (correct)\n");
41
    } else {
42
      print (" (incorrect)\n");
43
      print ("Correct result: ");
44
      print_num (answers[fn-1]);
45
      pass = 0;
46
      print ("\n");
47
    }
48
  }
49
 
50
  if (pass)
51
    sim_ctl (SC_TEST_PASSED);
52
  else
53
    sim_ctl (SC_TEST_FAILED);
54
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.