summaryrefslogtreecommitdiff
path: root/cpu/beta_cpu/sat_counter.cc
blob: da095c3e1d8397f9e2342f90650b9fd4a2e4196f (plain)
1
2
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
#include "base/misc.hh"
#include "cpu/beta_cpu/sat_counter.hh"

SatCounter::SatCounter()
    : maxVal(0), counter(0)
{
}

SatCounter::SatCounter(unsigned bits)
    : maxVal((1 << bits) - 1), counter(0)
{
}

SatCounter::SatCounter(unsigned bits, unsigned initial_val)
    : maxVal((1 << bits) - 1), counter(initial_val)
{
    // Check to make sure initial value doesn't exceed the max counter value.
    if (initial_val > maxVal) {
        panic("BP: Initial counter value exceeds max size.");
    }
}

void
SatCounter::setBits(unsigned bits)
{
    maxVal = (1 << bits) - 1;
}

void
SatCounter::increment()
{
    if(counter < maxVal) {
        ++counter;
    }
}

void
SatCounter::decrement()
{
    if(counter > 0) {
        --counter;
    }
}