-
Notifications
You must be signed in to change notification settings - Fork 0
/
opl2_test.go
115 lines (96 loc) · 2.61 KB
/
opl2_test.go
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
package opl2_test
import (
"flag"
"math"
"os"
"testing"
"time"
"github.com/gotracker/opl2"
"github.com/pkg/errors"
)
var (
ym3812 *opl2.Chip
ymf262 *opl2.Chip
sampleRate uint
errIrqFailure = errors.New("irq failure")
errOPL2ChipNotDetected = errors.New("opl2 chip not detected")
errOPL3ChipNotDetected = errors.New("opl3 chip not detected")
)
func detectChip(c *opl2.Chip) error {
// Reset both timers
c.WriteReg(0x04, 0x60)
// Enable the interrupts
c.WriteReg(0x04, 0x80)
// Read status value
status1 := c.ReadStatus()
testStatus1 := status1 & 0xE0
if testStatus1 != 0x00 {
return errors.Wrapf(errIrqFailure, "expected status of 00 was not found, got %0.2X instead", testStatus1)
}
// Write FF to Timer 1
c.WriteReg(0x02, 0xFF)
// Start Timer 1
c.WriteReg(0x04, 0x21)
// Delay for at least 80 microseconds, simulated by generating 80us of data
dataLen := math.Round((time.Microsecond * 80).Seconds() * float64(sampleRate))
c.GenerateBlock2(uint(dataLen), nil)
// Read status value
status2 := c.ReadStatus()
testStatus2 := status2 & 0xE0
if testStatus2 != 0xC0 {
return errors.Wrapf(errIrqFailure, "expected status of C0 was not found, got %0.2X instead", testStatus2)
}
// Reset both timers
c.WriteReg(0x04, 0x60)
// Enable the interrupts
c.WriteReg(0x04, 0x80)
return nil
}
func detectOPLVersion(c *opl2.Chip, opl3Expected bool) error {
if err := detectChip(c); err != nil {
return err
}
status3 := c.ReadStatus()
testStatus3 := status3 & 0x06
if opl3Expected {
if testStatus3 != 0x00 {
return errors.Wrapf(errOPL3ChipNotDetected, "expected status of 00 was not found, got %0.2X instead", testStatus3)
}
} else {
if testStatus3 == 0x00 {
return errors.Wrapf(errOPL2ChipNotDetected, "expected status of non-00 was not found, got %0.2X instead", testStatus3)
}
}
return nil
}
func TestDetectOPL3WithOPL2(t *testing.T) {
if err := detectOPLVersion(ym3812, true); err != nil {
if !errors.Is(err, errOPL3ChipNotDetected) {
t.Error(err)
}
}
}
func TestDetectOPL2WithOPL3(t *testing.T) {
if err := detectOPLVersion(ymf262, false); err != nil {
if !errors.Is(err, errOPL2ChipNotDetected) {
t.Error(err)
}
}
}
func TestResetOPL2(t *testing.T) {
if err := detectChip(ym3812); err != nil {
t.Error(err)
}
}
func TestResetOPL3(t *testing.T) {
if err := detectChip(ymf262); err != nil {
t.Error(err)
}
}
func TestMain(m *testing.M) {
flag.UintVar(&sampleRate, "s", uint(math.Round(opl2.OPLRATE)), "sample rate for OPL2/3 devices")
flag.Parse()
ym3812 = opl2.NewChip(uint32(sampleRate), false)
ymf262 = opl2.NewChip(uint32(sampleRate), true)
os.Exit(m.Run())
}