Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
akatsuki105 committed Apr 13, 2021
1 parent dd6e43e commit 1931e69
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
35 changes: 19 additions & 16 deletions pkg/gba/apu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math"
"mettaur/pkg/ram"
"mettaur/pkg/util"
"time"

"github.com/hajimehoshi/oto"
)
Expand All @@ -18,6 +17,14 @@ func isWaveRAM(addr uint32) bool {
return addr >= 0x04000090 && addr <= 0x0400009f
}

func isResetSoundChan(addr uint32) bool {
return addr == 0x04000065 || addr == 0x0400006d || addr == 0x04000075 || addr == 0x0400007d
}
func (g *GBA) resetSoundChan(addr uint32, b byte) {
m := map[uint32]int{0x04000065: 0, 0x0400006d: 1, 0x04000075: 2, 0x0400007d: 3}
g._resetSoundChan(m[addr], b&0x80 != 0)
}

const (
CPU_FREQ_HZ = 16777216
SND_FREQUENCY = 32768
Expand Down Expand Up @@ -57,9 +64,9 @@ type SoundChan struct {
}

func newAPU() *APU {
stream = make([]byte, BUFF_SAMPLES*2)
stream = make([]byte, BUFF_SAMPLES)

context, err := oto.NewContext(SND_FREQUENCY, 2, 2, 512)
context, err := oto.NewContext(SND_FREQUENCY, 2, 2, BUFF_SAMPLES)
if err != nil {
panic(err)
}
Expand All @@ -78,19 +85,15 @@ func (g *GBA) exitAPU() {

func (g *GBA) playSound() {
go func() {
for range time.Tick(time.Second / 60) {
g.updateSound()
g.writeSound()
for {
g.soundMix()
}
}()
go func() {
for {
g.apu.player.Write(stream)
}
}()
}

func (g *GBA) writeSound() {
g.apu.player.Write(stream)
}

func (g *GBA) updateSound() {
g.soundMix()
}

func (g *GBA) squareSample(ch int) int8 {
Expand Down Expand Up @@ -388,7 +391,7 @@ var sndBuffer [BUFF_SAMPLES]int16
var stream []byte

func (g *GBA) soundMix() {
for i := 0; i < len(sndBuffer); i += 4 {
for i := 0; i < BUFF_SAMPLES; i += 4 {
snd := sndBuffer[sndCurPlay&BUFF_SAMPLES_MSK] << 6
stream[i+0], stream[i+1] = byte(snd), byte(snd>>8)
sndCurPlay++
Expand Down Expand Up @@ -532,7 +535,7 @@ func (g *GBA) soundClock(cycles uint32) {
}
}

func (g *GBA) resetSoundChan(ch int, enable bool) {
func (g *GBA) _resetSoundChan(ch int, enable bool) {
if enable {
g.apu.chans[ch] = &SoundChan{
phase: false,
Expand Down
3 changes: 3 additions & 0 deletions pkg/gba/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (g *GBA) _setRAM8(addr uint32, b byte) {
switch {
case gpu.IsIO(addr):
g.GPU.IO[addr-0x0400_0000] = b
case isResetSoundChan(addr):
g.RAM.Set8(addr, b)
g.resetSoundChan(addr, b)
case isWaveRAM(addr):
bank := (g._getRAM(ram.SOUND3CNT_L) >> 2) & 0x10
idx := (bank ^ 0x10) | (addr & 0xf)
Expand Down

0 comments on commit 1931e69

Please sign in to comment.