File: TestEnvelopeGenerator.cpp

package info (click to toggle)
libsidplayfp 2.16.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 4,872 kB
  • sloc: cpp: 29,874; sh: 4,476; ansic: 986; makefile: 480; perl: 35
file content (145 lines) | stat: -rw-r--r-- 3,914 bytes parent folder | download | duplicates (4)
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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * This file is part of libsidplayfp, a SID player engine.
 *
 *  Copyright (C) 2014-2020 Leandro Nini
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "utpp/utpp.h"

#include "../src/builders/residfp-builder/residfp/Dac.cpp"

#define private public
#define protected public
#define class struct

#include "../src/builders/residfp-builder/residfp/EnvelopeGenerator.h"
#include "../src/builders/residfp-builder/residfp/EnvelopeGenerator.cpp"

using namespace UnitTest;

SUITE(EnvelopeGenerator)
{

struct TestFixture
{
    // Test setup
    TestFixture()
    {
        generator.reset();
        generator.envelope_counter = 0;
    }

    reSIDfp::EnvelopeGenerator generator;
};

TEST_FIXTURE(TestFixture, TestADSRDelayBug)
{
    // If the rate counter comparison value is set below the current value of the
    // rate counter, the counter will continue counting up until it wraps around
    // to zero at 2^15 = 0x8000, and then count rate_period - 1 before the
    // envelope can constly be stepped.

    generator.writeATTACK_DECAY(0x70);

    generator.writeCONTROL_REG(0x01);

    // wait 200 cycles
    for (int i=0; i<200; i++)
    {
        generator.clock();
    }

    CHECK_EQUAL(1, (int)generator.readENV());

    // set lower Attack time
    // should theoretically clock after 63 cycles
    generator.writeATTACK_DECAY(0x20);

    // wait another 200 cycles
    for (int i=0; i<200; i++)
    {
        generator.clock();
    }

    CHECK_EQUAL(1, (int)generator.readENV());
}

TEST_FIXTURE(TestFixture, TestFlipFFto00)
{
    // The envelope counter can flip from 0xff to 0x00 by changing state to
    // release, then to attack. The envelope counter is then frozen at
    // zero; to unlock this situation the state must be changed to release,
    // then to attack.

    generator.writeATTACK_DECAY(0x77);
    generator.writeSUSTAIN_RELEASE(0x77);

    generator.writeCONTROL_REG(0x01);

    do
    {
        generator.clock();
    } while ((int)generator.readENV() != 0xff);

    generator.writeCONTROL_REG(0x00);
    // run for three clocks, accounting for state pipeline
    generator.clock();
    generator.clock();
    generator.clock();
    generator.writeCONTROL_REG(0x01);

    // wait at least 313 cycles
    // so the counter is clocked once
    for (int i=0; i<315; i++)
    {
        generator.clock();
    }

    CHECK_EQUAL(0, (int)generator.readENV());
}

TEST_FIXTURE(TestFixture, TestFlip00toFF)
{
    // The envelope counter can flip from 0x00 to 0xff by changing state to
    // attack, then to release. The envelope counter will then continue
    // counting down in the release state.

    generator.counter_enabled = false;

    generator.writeATTACK_DECAY(0x77);
    generator.writeSUSTAIN_RELEASE(0x77);
    generator.clock();
    CHECK_EQUAL(0, (int)generator.readENV());

    generator.writeCONTROL_REG(0x01);
    // run for three clocks, accounting for state pipeline
    generator.clock();
    generator.clock();
    generator.clock();
    generator.writeCONTROL_REG(0x00);

    // wait at least 313 cycles
    // so the counter is clocked once
    for (int i=0; i<315; i++)
    {
        generator.clock();
    }

    CHECK_EQUAL(0xff, (int)generator.readENV());
}

}