forked from Stephane-D/SGDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsg.c
82 lines (61 loc) · 1.36 KB
/
psg.c
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
#include "config.h"
#include "types.h"
#include "psg.h"
#include "vdp.h"
void PSG_reset()
{
vu8 *pb;
u16 i;
pb = (u8*) PSG_PORT;
for (i = 0; i < 4; i++)
{
// set tone to 0
*pb = 0x80 | (i << 5) | 0x00;
*pb = 0x00;
// set envelope to silent
*pb = 0x90 | (i << 5) | 0x0F;
}
}
void PSG_write(u8 data)
{
vu8 *pb;
pb = (u8*) PSG_PORT;
*pb = data;
}
void PSG_setEnvelope(u8 channel, u8 value)
{
vu8 *pb;
pb = (u8*) PSG_PORT;
*pb = 0x90 | ((channel & 3) << 5) | (value & 0xF);
}
void PSG_setTone(u8 channel, u16 value)
{
vu8 *pb;
pb = (u8*) PSG_PORT;
*pb = 0x80 | ((channel & 3) << 5) | (value & 0xF);
*pb = (value >> 4) & 0x3F;
}
void PSG_setToneLow(u8 channel, u8 value)
{
vu8 *pb;
pb = (u8*) PSG_PORT;
*pb = 0x80 | ((channel & 3) << 5) | (value & 0xF);
}
void PSG_setFrequency(u8 channel, u16 value)
{
u16 data;
if (value)
{
// frequency to tone conversion
if (IS_PAL_SYSTEM) data = 3546893 / (value * 32);
else data = 3579545 / (value * 32);
}
else data = 0;
PSG_setTone(channel, data);
}
void PSG_setNoise(u8 type, u8 frequency)
{
vu8 *pb;
pb = (u8 *) PSG_PORT;
*pb = 0xE0 | ((type & 1) << 2) | (frequency & 0x3);
}