count.c 1.95 KB
Newer Older
1
//-------------------------------------------------------------------------
Roberto Hexsel's avatar
Roberto Hexsel committed
2
// test if COUNT register counts up monotonically as it should
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// returns error if the time to compute every 11th element of the Fibonacci
//    sequence, as measured by COUNT is not monotonically increasing
//-------------------------------------------------------------------------

#include "cMIPS.h"

#define TRUE  (1==1)
#define FALSE (1==0)

//---------------------------------------------------------------------
int fibonacci(int n) {
  int i;
  int f1 = 0;
  int f2 = 1;
  int fi = 0;;
  
  if (n == 0)
    return 0;
  if(n == 1)
    return 1;
  
  for(i = 2 ; i <= n ; i++ ) {
    fi = f1 + f2;
    f1 = f2;
    f2 = fi;
  }
  return fi;
}

//=====================================================================
int main() {
  int i, new, old, monotonic;

  print( startCount() );         // start COUNT
  monotonic = TRUE;

  for (i=0; i < 44; i += 11) {
    old = readCount();           // COUNT before computing fib(i)
    print( fibonacci(i) );
    new = readCount();           // COUNT after  computing fib(i)
    monotonic = monotonic && ( (new - old) > 0 );
    if ( monotonic == FALSE ) {
      to_stdout('e'); to_stdout('r'); to_stdout('r'); to_stdout('\n');
      print(new);
      exit(new);
    }
    // print(new);
  }
  // print(new);
  to_stdout('o'); to_stdout('k'); to_stdout('\n');


  // now disable COUNT and make sure that it has stopped
  print( stopCount() );          // stop COUNT
  old = readCount();             // COUNT before computing fib(i)
  print( fibonacci(5) );
  new = readCount();           // COUNT after  computing fib(i)
  monotonic = monotonic && ( (new - old) > 0 );
  if ( monotonic == TRUE ) {
    to_stdout('e'); to_stdout('r'); to_stdout('r'); to_stdout('\n');
    print(new);
    exit(new);
  } else {
    // print(new);
    to_stdout('o'); to_stdout('k'); to_stdout('\n');
  }
  exit(new);
}
//=====================================================================