Skip to content

Commit 1fe299f

Browse files
committed
fix bugs
1 parent 3a42d68 commit 1fe299f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2431
-927
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
go.sum linguist-generated=true
2+
**/wasm_exec.js linguist-vendored=false

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
build/
22
dawngb
33
dawngb.exe
4-
rom/
5-
src/godot/**/*.gb
6-
!src/godot/**/hello.gb
4+
rom
5+
**/*.gb
6+
**/*.gbc

core/gb/apu/apu.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ type APU struct {
1313
*psg.PSG
1414
sampleWriter io.Writer
1515

16-
samples [547 * 2]int16 // [[left, right]...], 547 = 32768 / 60
16+
samples [549 * 2]int16 // [[left, right]...], 549 = 32768 / 59.7275
1717
sampleCount uint16
18+
Mask uint8
1819
}
1920

2021
func New(audioBuffer io.Writer) *APU {
@@ -25,6 +26,7 @@ func New(audioBuffer io.Writer) *APU {
2526
return &APU{
2627
PSG: psg.New(psg.MODEL_GB),
2728
sampleWriter: audioBuffer,
29+
Mask: 0b1111, // (CH4, CH3, CH2, CH1)
2830
}
2931
}
3032

@@ -43,7 +45,7 @@ func (a *APU) Run(cycles8MHz int64) {
4345
}
4446
if a.cycles%256 == 0 { // 32768Hzにダウンサンプリングしたい = 32768Hzごとにサンプルを生成したい = 256マスターサイクルごとにサンプルを生成する (8MHz / 32768Hz = 256)
4547
if int(a.sampleCount) < len(a.samples)/2 {
46-
left, right := a.PSG.Sample()
48+
left, right := a.PSG.Sample(a.Mask)
4749
lvolume, rvolume := a.PSG.Volume()
4850
lsample, rsample := (int(left)*512)-16384, (int(right)*512)-16384
4951
lsample, rsample = (lsample*int(lvolume+1))/8, (rsample*int(rvolume+1))/8
@@ -59,3 +61,23 @@ func (a *APU) FlushSamples() {
5961
binary.Write(a.sampleWriter, binary.LittleEndian, a.samples[:a.sampleCount*2])
6062
a.sampleCount = 0
6163
}
64+
65+
type Snapshot struct {
66+
Header uint64
67+
Cycles int64
68+
PSG psg.Snapshot
69+
Reserved [16]uint8
70+
}
71+
72+
func (a *APU) CreateSnapshot() Snapshot {
73+
return Snapshot{
74+
Cycles: a.cycles,
75+
PSG: a.PSG.CreateSnapshot(),
76+
}
77+
}
78+
79+
func (a *APU) RestoreSnapshot(snap Snapshot) bool {
80+
a.cycles = snap.Cycles
81+
ok := a.PSG.RestoreSnapshot(snap.PSG)
82+
return ok
83+
}

core/gb/apu/psg/ch_noise.go

+64-48
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package psg
22

3-
import (
4-
"encoding/binary"
5-
"io"
6-
)
7-
8-
type noise struct {
3+
type Noise struct {
94
enabled bool // NR52.3
105

116
length uint8 // 音の残り再生時間
127
stop bool // NR44.6
138

14-
envelope *envelope
9+
envelope *Envelope
1510

16-
lfsr uint16 // Noiseの疑似乱数(lfsr: Linear Feedback Shift Register = 疑似乱数生成アルゴリズム)
11+
LFSR uint16 // Noiseの疑似乱数(lfsr: Linear Feedback Shift Register = 疑似乱数生成アルゴリズム)
1712

1813
// この2つでノイズの周波数(疑似乱数の生成頻度)を決める
1914
divisor uint8 // NR43.0-2; ノイズ周波数1(カウント指定)
@@ -25,40 +20,45 @@ type noise struct {
2520
output uint8 // 0..15
2621
}
2722

28-
func newNoiseChannel() *noise {
29-
return &noise{
23+
func newNoiseChannel() *Noise {
24+
return &Noise{
3025
envelope: newEnvelope(),
31-
lfsr: 0,
26+
LFSR: 0,
3227
}
3328
}
3429

35-
func (ch *noise) reset() {
36-
ch.enabled = false
37-
ch.length, ch.stop = 0, false
30+
func (ch *Noise) Reset() {
31+
ch.TurnOff()
3832
ch.envelope.reset()
39-
ch.lfsr = 0
33+
ch.LFSR = 0
4034
ch.divisor, ch.octave = 0, 0
4135
ch.period = 0
4236
ch.narrow = false
4337
ch.output = 0
4438
}
4539

46-
func (ch *noise) reload() {
40+
func (ch *Noise) TurnOff() {
41+
ch.enabled = false
42+
ch.length, ch.stop, ch.divisor, ch.narrow, ch.octave = 0, false, 0, false, 0
43+
ch.envelope.TurnOff()
44+
}
45+
46+
func (ch *Noise) reload() {
4747
ch.enabled = ch.dacEnable()
4848
ch.envelope.reload()
49-
ch.lfsr = 0x7FFF
49+
ch.LFSR = 0x7FFF
5050
if ch.length == 0 {
5151
ch.length = 64
5252
}
5353
}
5454

55-
func (ch *noise) clock64Hz() {
55+
func (ch *Noise) clock64Hz() {
5656
if ch.enabled {
5757
ch.envelope.update()
5858
}
5959
}
6060

61-
func (ch *noise) clock256Hz() {
61+
func (ch *Noise) clock256Hz() {
6262
if ch.stop && ch.length > 0 {
6363
ch.length--
6464
if ch.length == 0 {
@@ -67,7 +67,7 @@ func (ch *noise) clock256Hz() {
6767
}
6868
}
6969

70-
func (ch *noise) clockTimer() {
70+
func (ch *Noise) clockTimer() {
7171
// ch.enabledに関わらず、乱数は生成される
7272
ch.period--
7373
if ch.period == 0 {
@@ -76,7 +76,7 @@ func (ch *noise) clockTimer() {
7676
}
7777

7878
result := uint8(0)
79-
if (ch.lfsr & 1) == 0 {
79+
if (ch.LFSR & 1) == 0 {
8080
result = ch.envelope.volume
8181
}
8282

@@ -87,54 +87,70 @@ func (ch *noise) clockTimer() {
8787
ch.output = result
8888
}
8989

90-
func (ch *noise) update() {
90+
func (ch *Noise) update() {
9191
if ch.octave < 14 {
92-
bit := ((ch.lfsr ^ (ch.lfsr >> 1)) & 1)
92+
bit := ((ch.LFSR ^ (ch.LFSR >> 1)) & 1)
9393
if ch.narrow {
94-
ch.lfsr = (ch.lfsr >> 1) ^ (bit << 6)
94+
ch.LFSR = (ch.LFSR >> 1) ^ (bit << 6)
9595
} else {
96-
ch.lfsr = (ch.lfsr >> 1) ^ (bit << 14)
96+
ch.LFSR = (ch.LFSR >> 1) ^ (bit << 14)
9797
}
9898
}
9999
}
100100

101101
var noisePeriodTable = []uint8{4, 8, 16, 24, 32, 40, 48, 56}
102102

103-
func (ch *noise) calcFreqency() uint32 {
103+
func (ch *Noise) calcFreqency() uint32 {
104104
return uint32(noisePeriodTable[ch.divisor]) << ch.octave
105105
}
106106

107-
func (ch *noise) getOutput() uint8 {
107+
// GetOutput gets 4bit sample (0..15)
108+
func (ch *Noise) GetOutput() uint8 {
108109
if ch.enabled {
109110
return ch.output
110111
}
111112
return 0
112113
}
113114

114-
func (ch *noise) dacEnable() bool {
115+
func (ch *Noise) dacEnable() bool {
115116
return ((ch.envelope.initialVolume != 0) || ch.envelope.direction)
116117
}
117118

118-
func (ch *noise) serialize(s io.Writer) {
119-
binary.Write(s, binary.LittleEndian, ch.enabled)
120-
binary.Write(s, binary.LittleEndian, ch.length)
121-
binary.Write(s, binary.LittleEndian, ch.stop)
122-
ch.envelope.serialize(s)
123-
binary.Write(s, binary.LittleEndian, ch.lfsr)
124-
binary.Write(s, binary.LittleEndian, ch.octave)
125-
binary.Write(s, binary.LittleEndian, ch.divisor)
126-
binary.Write(s, binary.LittleEndian, ch.period)
127-
binary.Write(s, binary.LittleEndian, ch.narrow)
119+
type NoiseSnapshot struct {
120+
Header uint64
121+
Enabled bool
122+
Length uint8
123+
Stop bool
124+
Envelope EnvelopeSnapshot
125+
LFSR uint16
126+
Divisor, Octave uint8
127+
Period uint32
128+
Narrow bool
129+
Output uint8
130+
Reserved [15]uint8
131+
}
132+
133+
func (ch *Noise) CreateSnapshot() NoiseSnapshot {
134+
return NoiseSnapshot{
135+
Enabled: ch.enabled,
136+
Length: ch.length,
137+
Stop: ch.stop,
138+
Envelope: ch.envelope.CreateSnapshot(),
139+
LFSR: ch.LFSR,
140+
Divisor: ch.divisor,
141+
Octave: ch.octave,
142+
Period: ch.period,
143+
Narrow: ch.narrow,
144+
Output: ch.output,
145+
}
128146
}
129147

130-
func (ch *noise) deserialize(s io.Reader) {
131-
binary.Read(s, binary.LittleEndian, &ch.enabled)
132-
binary.Read(s, binary.LittleEndian, &ch.length)
133-
binary.Read(s, binary.LittleEndian, &ch.stop)
134-
ch.envelope.deserialize(s)
135-
binary.Read(s, binary.LittleEndian, &ch.lfsr)
136-
binary.Read(s, binary.LittleEndian, &ch.octave)
137-
binary.Read(s, binary.LittleEndian, &ch.divisor)
138-
binary.Read(s, binary.LittleEndian, &ch.period)
139-
binary.Read(s, binary.LittleEndian, &ch.narrow)
148+
func (ch *Noise) RestoreSnapshot(snap NoiseSnapshot) {
149+
ch.enabled = snap.Enabled
150+
ch.length, ch.stop = snap.Length, snap.Stop
151+
ch.envelope.RestoreSnapshot(snap.Envelope)
152+
ch.LFSR = snap.LFSR
153+
ch.divisor, ch.octave, ch.period = snap.Divisor, snap.Octave, snap.Period
154+
ch.narrow = snap.Narrow
155+
ch.output = snap.Output
140156
}

0 commit comments

Comments
 (0)