Skip to content

Commit

Permalink
Merge pull request #7 from ichirin2501/impl-apu
Browse files Browse the repository at this point in the history
Implementation APU
  • Loading branch information
ichirin2501 authored Jan 8, 2024
2 parents 0fe8fd6 + f1a7b6e commit 26ffaa8
Show file tree
Hide file tree
Showing 52 changed files with 2,226 additions and 881 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/scheduled-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Scheduled-test

on:
schedule:
- cron: "0 0 * * 1"

jobs:
scheduled-test:
strategy:
matrix:
go-version: [1.18.x, 1.19.x, 1.20.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: '${{ matrix.go-version }}'
- name: Checkout code
uses: actions/checkout@v4
- name: Test
run: make test
8 changes: 4 additions & 4 deletions .github/workflows/go.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ jobs:
test:
strategy:
matrix:
go-version: [1.18.x]
go-version: [1.18.x, 1.19.x, 1.20.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
go-version: '${{ matrix.go-version }}'
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Test
run: make test
2 changes: 1 addition & 1 deletion Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:1.18-buster

RUN apt-get update && \
apt-get install -y --no-install-recommends --no-install-suggests \
gcc libgl1-mesa-dev xorg-dev
gcc libgl1-mesa-dev xorg-dev libasound2-dev

WORKDIR /work
COPY . .
Expand Down
162 changes: 90 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ It's great because it's written so simple.
## TODOs

- refactoring
- decay ppu openbus
- apu emulation
- (I'm not interested in sound so I probably won't implement it...)

Expand All @@ -34,77 +33,96 @@ It's great because it's written so simple.

| Test | SingleRom | Result |
| - | - | - |
| blargg_ppu_tests_2005.09.15b | palette_ram.nes | OK |
| blargg_ppu_tests_2005.09.15b | sprite_ram.nes | OK |
| blargg_ppu_tests_2005.09.15b | vbl_clear_time.nes | OK |
| blargg_ppu_tests_2005.09.15b | vram_access.nes | OK |
| branch_timing_tests | 1.Branch_Basics.nes | OK |
| branch_timing_tests | 2.Backward_Branch.nes | OK |
| branch_timing_tests | 3.Forward_Branch.nes | OK |
| cpu_dummy_reads | cpu_dummy_reads.nes | OK |
| cpu_dummy_writes | cpu_dummy_writes_oam.nes | OK |
| cpu_dummy_writes | cpu_dummy_writes_ppumem.nes | OK |
| cpu_exec_space | test_cpu_exec_space_apu.nes | Failed |
| cpu_exec_space | test_cpu_exec_space_ppuio.nes | OK |
| cpu_timing_test6 | cpu_timing_test.nes | OK |
| instr_misc | 01-abs_x_wrap.nes | OK |
| instr_misc | 02-branch_wrap.nes | OK |
| instr_misc | 03-dummy_reads.nes | OK |
| instr_misc | 04-dummy_reads_apu.nes | Failed |
| instr_test-v5 | 01-basics.nes | OK |
| instr_test-v5 | 02-implied.nes | OK |
| instr_test-v5 | 03-immediate.nes | OK |
| instr_test-v5 | 04-zero_page.nes | OK |
| instr_test-v5 | 05-zp_xy.nes | OK |
| instr_test-v5 | 06-absolute.nes | OK |
| instr_test-v5 | 07-abs_xy.nes | OK |
| instr_test-v5 | 08-ind_x.nes | OK |
| instr_test-v5 | 09-ind_y.nes | OK |
| instr_test-v5 | 10-branches.nes | OK |
| instr_test-v5 | 11-stack.nes | OK |
| instr_test-v5 | 12-jmp_jsr.nes | OK |
| instr_test-v5 | 13-rts.nes | OK |
| instr_test-v5 | 14-rti.nes | OK |
| instr_test-v5 | 15-brk.nes | OK |
| instr_test-v5 | 16-special.nes | OK |
| nestest | nestest.nes | OK |
| oam_read | oam_read.nes | OK |
| oam_stress | oam_stress.nes | OK |
| ppu_open_bus | ppu_open_bus.nes | OK |
| ppu_read_buffer | test_ppu_read_buffer.nes | OK |
| ppu_vbl_nmi | 01-vbl_basics.nes | OK |
| ppu_vbl_nmi | 02-vbl_set_time.nes | OK |
| ppu_vbl_nmi | 03-vbl_clear_time.nes | OK |
| ppu_vbl_nmi | 04-nmi_control.nes | OK |
| ppu_vbl_nmi | 05-nmi_timing.nes | OK |
| ppu_vbl_nmi | 06-suppression.nes | OK |
| ppu_vbl_nmi | 07-nmi_on_timing.nes | OK |
| ppu_vbl_nmi | 08-nmi_off_timing.nes | OK |
| ppu_vbl_nmi | 09-even_odd_frames.nes | OK |
| ppu_vbl_nmi | 10-even_odd_timing.nes | Failed |
| sprite_hit_tests_2005.10.05 | 01.basics.nes | OK |
| sprite_hit_tests_2005.10.05 | 02.alignment.nes | OK |
| sprite_hit_tests_2005.10.05 | 03.corners.nes | OK |
| sprite_hit_tests_2005.10.05 | 04.flip.nes | OK |
| sprite_hit_tests_2005.10.05 | 05.left_clip.nes | OK |
| sprite_hit_tests_2005.10.05 | 06.right_edge.nes | OK |
| sprite_hit_tests_2005.10.05 | 07.screen_bottom.nes | OK |
| sprite_hit_tests_2005.10.05 | 08.double_height.nes | OK |
| sprite_hit_tests_2005.10.05 | 09.timing_basics.nes | OK |
| sprite_hit_tests_2005.10.05 | 10.timing_order.nes | OK |
| sprite_hit_tests_2005.10.05 | 11.edge_timing.nes | OK |
| sprite_overflow_tests | 1.Basics.nes | OK |
| sprite_overflow_tests | 2.Details.nes | OK |
| sprite_overflow_tests | 3.Timing.nes | Failed |
| sprite_overflow_tests | 4.Obscure.nes | Failed |
| sprite_overflow_tests | 5.Emulator.nes | OK |
| vbl_nmi_timing | 1.frame_basics.nes | OK |
| vbl_nmi_timing | 2.vbl_timing.nes | OK |
| vbl_nmi_timing | 3.even_odd_frames.nes | OK |
| vbl_nmi_timing | 4.vbl_clear_timing.nes | OK |
| vbl_nmi_timing | 5.nmi_suppression.nes | OK |
| vbl_nmi_timing | 6.nmi_disable.nes | OK |
| vbl_nmi_timing | 7.nmi_timing.nes | OK |
| apu_test | 1-len_ctr.nes ||
| apu_test | 2-len_table.nes ||
| apu_test | 3-irq_flag.nes ||
| apu_test | 4-jitter.nes ||
| apu_test | 5-len_timing.nes ||
| apu_test | 6-irq_flag_timing.nes ||
| apu_test | 7-dmc_basics.nes ||
| apu_test | 8-dmc_rates.nes ||
| blargg_apu_2005.07.30 | 01.len_ctr.nes ||
| blargg_apu_2005.07.30 | 02.len_table.nes ||
| blargg_apu_2005.07.30 | 03.irq_flag.nes ||
| blargg_apu_2005.07.30 | 04.clock_jitter.nes ||
| blargg_apu_2005.07.30 | 05.len_timing_mode0.nes ||
| blargg_apu_2005.07.30 | 06.len_timing_mode1.nes ||
| blargg_apu_2005.07.30 | 07.irq_flag_timing.nes ||
| blargg_apu_2005.07.30 | 08.irq_timing.nes ||
| blargg_apu_2005.07.30 | 09.reset_timing.nes ||
| blargg_apu_2005.07.30 | 10.len_halt_timing.nes ||
| blargg_apu_2005.07.30 | 11.len_reload_timing.nes ||
| blargg_ppu_tests_2005.09.15b | palette_ram.nes ||
| blargg_ppu_tests_2005.09.15b | sprite_ram.nes ||
| blargg_ppu_tests_2005.09.15b | vbl_clear_time.nes ||
| blargg_ppu_tests_2005.09.15b | vram_access.nes ||
| branch_timing_tests | 1.Branch_Basics.nes ||
| branch_timing_tests | 2.Backward_Branch.nes ||
| branch_timing_tests | 3.Forward_Branch.nes ||
| cpu_dummy_reads | cpu_dummy_reads.nes ||
| cpu_dummy_writes | cpu_dummy_writes_oam.nes ||
| cpu_dummy_writes | cpu_dummy_writes_ppumem.nes ||
| cpu_exec_space | test_cpu_exec_space_apu.nes ||
| cpu_exec_space | test_cpu_exec_space_ppuio.nes ||
| cpu_timing_test6 | cpu_timing_test.nes ||
| instr_misc | 01-abs_x_wrap.nes ||
| instr_misc | 02-branch_wrap.nes ||
| instr_misc | 03-dummy_reads.nes ||
| instr_misc | 04-dummy_reads_apu.nes ||
| instr_test-v5 | 01-basics.nes ||
| instr_test-v5 | 02-implied.nes ||
| instr_test-v5 | 03-immediate.nes ||
| instr_test-v5 | 04-zero_page.nes ||
| instr_test-v5 | 05-zp_xy.nes ||
| instr_test-v5 | 06-absolute.nes ||
| instr_test-v5 | 07-abs_xy.nes ||
| instr_test-v5 | 08-ind_x.nes ||
| instr_test-v5 | 09-ind_y.nes ||
| instr_test-v5 | 10-branches.nes ||
| instr_test-v5 | 11-stack.nes ||
| instr_test-v5 | 12-jmp_jsr.nes ||
| instr_test-v5 | 13-rts.nes ||
| instr_test-v5 | 14-rti.nes ||
| instr_test-v5 | 15-brk.nes ||
| instr_test-v5 | 16-special.nes ||
| nestest | nestest.nes ||
| oam_read | oam_read.nes ||
| oam_stress | oam_stress.nes ||
| ppu_open_bus | ppu_open_bus.nes ||
| ppu_read_buffer | test_ppu_read_buffer.nes ||
| ppu_vbl_nmi | 01-vbl_basics.nes ||
| ppu_vbl_nmi | 02-vbl_set_time.nes ||
| ppu_vbl_nmi | 03-vbl_clear_time.nes ||
| ppu_vbl_nmi | 04-nmi_control.nes ||
| ppu_vbl_nmi | 05-nmi_timing.nes ||
| ppu_vbl_nmi | 06-suppression.nes ||
| ppu_vbl_nmi | 07-nmi_on_timing.nes ||
| ppu_vbl_nmi | 08-nmi_off_timing.nes ||
| ppu_vbl_nmi | 09-even_odd_frames.nes ||
| ppu_vbl_nmi | 10-even_odd_timing.nes ||
| sprite_hit_tests_2005.10.05 | 01.basics.nes ||
| sprite_hit_tests_2005.10.05 | 02.alignment.nes ||
| sprite_hit_tests_2005.10.05 | 03.corners.nes ||
| sprite_hit_tests_2005.10.05 | 04.flip.nes ||
| sprite_hit_tests_2005.10.05 | 05.left_clip.nes ||
| sprite_hit_tests_2005.10.05 | 06.right_edge.nes ||
| sprite_hit_tests_2005.10.05 | 07.screen_bottom.nes ||
| sprite_hit_tests_2005.10.05 | 08.double_height.nes ||
| sprite_hit_tests_2005.10.05 | 09.timing_basics.nes ||
| sprite_hit_tests_2005.10.05 | 10.timing_order.nes ||
| sprite_hit_tests_2005.10.05 | 11.edge_timing.nes ||
| sprite_overflow_tests | 1.Basics.nes ||
| sprite_overflow_tests | 2.Details.nes ||
| sprite_overflow_tests | 3.Timing.nes ||
| sprite_overflow_tests | 4.Obscure.nes ||
| sprite_overflow_tests | 5.Emulator.nes ||
| vbl_nmi_timing | 1.frame_basics.nes ||
| vbl_nmi_timing | 2.vbl_timing.nes ||
| vbl_nmi_timing | 3.even_odd_frames.nes ||
| vbl_nmi_timing | 4.vbl_clear_timing.nes ||
| vbl_nmi_timing | 5.nmi_suppression.nes ||
| vbl_nmi_timing | 6.nmi_disable.nes ||
| vbl_nmi_timing | 7.nmi_timing.nes ||

https://github.com/christopherpow/nes-test-roms/blob/master/other/RasterDemo.NES
![RasterDemo.nes](/images/RasterDemo.gif)
Expand Down
86 changes: 63 additions & 23 deletions cmd/rgnes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import (
"image"
"image/color"
"os"
"runtime"
"time"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"github.com/ichirin2501/rgnes/nes/apu"
"github.com/ichirin2501/rgnes/nes/cassette"
"github.com/ichirin2501/rgnes/nes/cpu"
"github.com/ichirin2501/rgnes/nes/joypad"
"github.com/ichirin2501/rgnes/nes/ppu"
"github.com/hajimehoshi/oto"
"github.com/ichirin2501/rgnes/nes"
)

func init() {
runtime.LockOSThread()
}

func main() {
if err := realMain(); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down Expand Up @@ -54,6 +56,30 @@ func (r *renderer) Refresh() {
r.currImg.Refresh()
}

type player struct {
p *oto.Player
buf []byte
}

func newPlayer() (*player, error) {
c, err := oto.NewContext(44100, 1, 1, 1)
if err != nil {
return nil, err
}
p := c.NewPlayer()
return &player{
p: p,
buf: make([]byte, 1),
}, nil
}

func (p *player) Sample(v float32) {
p.buf[0] = byte(v * 0xFF)
if _, err := p.p.Write(p.buf); err != nil {
fmt.Println("why: ", err)
}
}

func realMain() error {
var (
rom string
Expand Down Expand Up @@ -84,21 +110,27 @@ func realMain() error {
}
defer f.Close()

mapper, err := cassette.NewMapper(f)
mapper, err := nes.NewMapper(f)
if err != nil {
return err
}

player, err := newPlayer()
if err != nil {
return err
}

trace := &cpu.Trace{}
irp := &cpu.Interrupter{}
trace := &nes.Trace{}
irp := &nes.Interrupter{}

m := mapper.MirroingType()
ppu := ppu.New(renderer, mapper, &m, irp)
joypad := joypad.New()
apu := apu.New()
cpuBus := cpu.NewBus(ppu, apu, mapper, joypad)
ppu := nes.NewPPU(renderer, mapper, m, irp)
joypad := nes.NewJoypad()
apu := nes.NewAPU(irp, player)
cpuBus := nes.NewBus(ppu, apu, mapper, joypad)

cpu := cpu.New(cpuBus, irp, cpu.WithTracer(trace))
cpu := nes.NewCPU(cpuBus, irp, nes.WithTracer(trace))
apu.PowerUp()
cpu.PowerUp()

if deskCanvas, ok := win.Canvas().(desktop.Canvas); ok {
Expand All @@ -123,7 +155,15 @@ func realMain() error {
//beforeScanline := ppu.Scanline
cpu.Step()

//fmt.Printf("%s\n", trace.NESTestString())
// fmt.Printf("%s apuSteps:%d\tapuFrameMode:%d\tapuFrameSeqStep:%d\tapuPulse1LC:%d\tframeIRQFlag:%v\tnewfval:%v\twriteDelayFC:%v\n", trace.NESTestString(),
// apu.FetchFrameStep(),
// apu.FetchFrameMode(),
// apu.FetchFrameSeqStep(),
// apu.FetchPulse1LC(),
// apu.FetchFrameIRQFlag(),
// apu.FetchNewFrameCounterVal(),
// apu.FetchWriteDelayFC(),
// )
//fmt.Printf("%s ppu.v:0x%04X ppu.buf:0x%02X mapper[0]:0x%02X\n", trace.NESTestString(), v, ppuBuf, mp0)
//fmt.Printf("0x6000 = 0x%02X\n", cpuBus.ReadForTest(0x6000))

Expand All @@ -138,27 +178,27 @@ func realMain() error {
return nil
}

func updateKey(win fyne.Window, cpu *cpu.CPU, j *joypad.Joypad, k fyne.KeyName, pressed bool) {
func updateKey(win fyne.Window, cpu *nes.CPU, j *nes.Joypad, k fyne.KeyName, pressed bool) {
switch k {
case fyne.KeyEscape:
win.Close()
case fyne.KeyR:
cpu.Reset()
case fyne.KeySpace:
j.SetButtonStatus(joypad.ButtonSelect, pressed)
j.SetButtonStatus(nes.ButtonSelect, pressed)
case fyne.KeyReturn:
j.SetButtonStatus(joypad.ButtonStart, pressed)
j.SetButtonStatus(nes.ButtonStart, pressed)
case fyne.KeyUp:
j.SetButtonStatus(joypad.ButtonUP, pressed)
j.SetButtonStatus(nes.ButtonUP, pressed)
case fyne.KeyDown:
j.SetButtonStatus(joypad.ButtonDown, pressed)
j.SetButtonStatus(nes.ButtonDown, pressed)
case fyne.KeyLeft:
j.SetButtonStatus(joypad.ButtonLeft, pressed)
j.SetButtonStatus(nes.ButtonLeft, pressed)
case fyne.KeyRight:
j.SetButtonStatus(joypad.ButtonRight, pressed)
j.SetButtonStatus(nes.ButtonRight, pressed)
case fyne.KeyZ:
j.SetButtonStatus(joypad.ButtonA, pressed)
j.SetButtonStatus(nes.ButtonA, pressed)
case fyne.KeyX:
j.SetButtonStatus(joypad.ButtonB, pressed)
j.SetButtonStatus(nes.ButtonB, pressed)
}
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
fyne.io/fyne/v2 v2.1.4
github.com/hajimehoshi/oto v1.0.1
github.com/stretchr/testify v1.6.1
)

Expand All @@ -19,9 +20,11 @@ require (
github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 // indirect
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect
github.com/yuin/goldmark v1.3.8 // indirect
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 // indirect
golang.org/x/image v0.0.0-20200430140353-33d19683fad8 // indirect
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
golang.org/x/text v0.3.3 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
Loading

0 comments on commit 26ffaa8

Please sign in to comment.