Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit f0856bb
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 26 22:09:15 2024 +0900

    fix

commit 1a5083c
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 26 04:04:09 2024 +0900

    fix

commit 929d588
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 14:55:09 2024 +0900

    fix

commit e637ec8
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 14:29:56 2024 +0900

    fix

commit 8feca17
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 14:26:52 2024 +0900

    wip

commit b05dfe3
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 14:21:12 2024 +0900

    wip

commit ddb02e6
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 14:15:53 2024 +0900

    wip

commit 923f5ad
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 13:48:44 2024 +0900

    wip

commit 9a0c950
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 13:43:29 2024 +0900

    wip

commit f5ead09
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 13:38:38 2024 +0900

    fix

commit 013e0c4
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 13:30:01 2024 +0900

    wip

commit e8e9338
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 13:23:50 2024 +0900

    fix

commit 5e764a0
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Wed Nov 20 13:17:05 2024 +0900

    fix

commit 5e28825
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 18:57:59 2024 +0900

    fix

commit 7a13969
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 16:40:34 2024 +0900

    wip

commit c37ad48
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 16:03:55 2024 +0900

    wip

commit 78f5448
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 15:48:35 2024 +0900

    fix

commit 629aa66
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 15:48:11 2024 +0900

    wip

commit 5f41e0e
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 15:41:30 2024 +0900

    wip

commit 6244d26
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 15:32:43 2024 +0900

    wip

commit 3691bbc
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 15:29:14 2024 +0900

    wip

commit b489855
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 15:19:48 2024 +0900

    refactor palette

commit 48f91c9
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 15:08:07 2024 +0900

    wip

commit ba7d3b8
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 14:23:19 2024 +0900

    wip

commit 8d73726
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 14:19:42 2024 +0900

    fix

commit 4160e88
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 14:17:28 2024 +0900

    fix mbc2

commit 99b9aae
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 13:58:09 2024 +0900

    wip

commit 20a3e8d
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 13:39:34 2024 +0900

    fix

commit a62ab00
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 13:28:05 2024 +0900

    wip

commit b9b126a
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 13:25:47 2024 +0900

    wip

commit 17b9f56
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:28:06 2024 +0900

    wip

commit 16dcbf6
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:24:58 2024 +0900

    fix

commit 0bf2fa2
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:22:08 2024 +0900

    fix

commit 1accc90
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:16:19 2024 +0900

    wip

commit 039577e
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:14:50 2024 +0900

    wip

commit e2baebd
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:11:53 2024 +0900

    sp

commit 01fd9c9
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:09:51 2024 +0900

    rename

commit 595c2af
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:08:23 2024 +0900

    wip

commit 1490d56
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Tue Nov 19 12:05:01 2024 +0900

    wip

commit ade39dd
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 18:08:35 2024 +0900

    wip

commit ebbe87b
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 17:59:01 2024 +0900

    wip memory

commit c289fb0
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 17:09:49 2024 +0900

    wip

commit 024393c
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 17:06:08 2024 +0900

    wip

commit 6330e8a
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 17:01:18 2024 +0900

    compatible palette

commit 8e27dfe
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 15:56:51 2024 +0900

    wip

commit 0fa21ab
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 14:22:07 2024 +0900

    wip

commit 58f3103
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 12:32:18 2024 +0900

    mbc2

commit eeaee45
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 01:47:29 2024 +0900

    fix

commit ddf6422
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Mon Nov 18 01:17:53 2024 +0900

    wip

commit a76e67b
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Sun Nov 17 23:27:43 2024 +0900

    cgb mode

commit 734c186
Author: akatsuki105 <bluejapan73+dev@gmail.com>
Date:   Sun Nov 17 23:05:27 2024 +0900

    mode
  • Loading branch information
akatsuki105 committed Nov 29, 2024
1 parent dc50be5 commit 3a42d68
Show file tree
Hide file tree
Showing 38 changed files with 1,376 additions and 1,273 deletions.
29 changes: 23 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
{
"[go]": {
"editor.tabSize": 2,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
"[go]": {
"editor.tabSize": 2,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
},
"[cpp]": {
"editor.formatOnSave": true,
"editor.tabSize": 2,
"editor.foldingStrategy": "indentation",
},
"[c]": {
"editor.foldingStrategy": "indentation",
},
"gopls": {
"codelenses": { // CodeLens はエディタ画面から直接コマンドを実行できる機能 (packageの上にクリックすると静的解析を行うボタンが出てくる)
"gc_details": true
},
"ui.diagnostic.annotations": {
"escape": true,
// "inline": true,
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can play on [web](https://dawngb.vercel.app/)!
## Features

- GB(DMG) and GBC(CGB) support
- MBC1, MBC3, MBC5, MBC30 support
- MBC1, MBC2, MBC3, MBC5, MBC30 support
- Sound(APU) support
- Libretro support(run `make libretro`)
- Multiplatform support
Expand Down
5 changes: 3 additions & 2 deletions core/gb/apu/apu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/akatsuki105/dawngb/core/gb/apu/psg"
)

// SoCに組み込まれているため、`/cpu`にある方が正確ではある
type APU struct {
cycles int64 // 8MHzのマスターサイクル単位
*psg.PSG
Expand All @@ -27,8 +28,8 @@ func New(audioBuffer io.Writer) *APU {
}
}

func (a *APU) Reset(hasBIOS bool) {
a.PSG.Reset(hasBIOS)
func (a *APU) Reset() {
a.PSG.Reset()
a.cycles = 0
clear(a.samples[:])
a.sampleCount = 0
Expand Down
7 changes: 2 additions & 5 deletions core/gb/apu/psg/psg.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func New(model uint8) *PSG {
}
}

func (a *PSG) Reset(hasBIOS bool) {
func (a *PSG) Reset() {
a.enabled = false
a.ch1.reset()
a.ch2.reset()
Expand All @@ -49,12 +49,9 @@ func (a *PSG) Reset(hasBIOS bool) {
clear(a.ioreg[:])
a.leftVolume, a.rightVolume = 7, 7
a.leftEnables, a.rightEnables = [4]bool{}, [4]bool{}
if !hasBIOS {
a.skipBIOS()
}
}

func (a *PSG) skipBIOS() {
func (a *PSG) SkipBIOS() {
a.Write(NR10, 0x80)
a.Write(NR11, 0xBF)
a.Write(NR12, 0xF3)
Expand Down
21 changes: 12 additions & 9 deletions core/gb/cartridge/cartridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type mbc interface {

type Cartridge struct {
title string
rom []uint8
ROM []uint8
ram []uint8 // SRAM
mbc
mbc // mapper
}

func New(rom []uint8) (*Cartridge, error) {
Expand All @@ -40,10 +40,10 @@ func New(rom []uint8) (*Cartridge, error) {
ram: make([]uint8, 0),
}

c.rom = make([]uint8, (32*KB)<<rom[0x148])
copy(c.rom, rom)
c.ROM = make([]uint8, (32*KB)<<rom[0x148])
copy(c.ROM, rom)

ramSize, ok := RAM_SIZES[rom[0x149]]
ramSize, ok := RAM_SIZES[rom[0x149]] // これはSRAMチップのサイズであって、MBC2のようなMBCチップにRAMが内蔵されている場合は0になるっぽい
if ok {
c.ram = make([]uint8, ramSize)
}
Expand All @@ -53,17 +53,20 @@ func New(rom []uint8) (*Cartridge, error) {
return nil, err
}
c.mbc = mbc
fmt.Println("MapperID:", c.rom[0x147])
// fmt.Println("MapperID:", c.ROM[0x147])
return c, nil
}

func createMBC(c *Cartridge) (mbc, error) {
mbcType := c.rom[0x147]
mbcType := c.ROM[0x147]
switch mbcType {
case 0:
return newMBC0(c), nil
case 1, 2, 3:
return newMBC1(c), nil
case 5, 6:
c.ram = make([]uint8, 512)
return newMBC2(c), nil
case 16, 19:
return newMBC3(c), nil
case 25, 26, 27:
Expand All @@ -85,8 +88,8 @@ func (c *Cartridge) Write(addr uint16, val uint8) {
c.mbc.write(addr, val)
}

func (c *Cartridge) IsCGB() bool {
return (c.rom[0x143] & (1 << 7)) != 0
func (c *Cartridge) CGBFlag() uint8 {
return c.ROM[0x143]
}

func (c *Cartridge) LoadSRAM(data []uint8) error {
Expand Down
19 changes: 14 additions & 5 deletions core/gb/cartridge/mbc0.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package cartridge

type mbc0 struct {
c *Cartridge
c *Cartridge
hasRam bool
}

func newMBC0(c *Cartridge) mbc {
return &mbc0{c: c}
hasRam := c.ROM[0x147] != 0
return &mbc0{
c: c,
hasRam: hasRam,
}
}

func (m *mbc0) read(addr uint16) uint8 {
switch addr >> 12 {
case 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7:
return m.c.rom[addr]
return m.c.ROM[addr]
case 0xA, 0xB:
return m.c.ram[addr&0x1FFF]
if m.hasRam {
return m.c.ram[addr&0x1FFF]
}
}
return 0xFF
}

func (m *mbc0) write(addr uint16, val uint8) {
switch addr >> 12 {
case 0xA, 0xB:
m.c.ram[addr&0x1FFF] = val
if m.hasRam {
m.c.ram[addr&0x1FFF] = val
}
}
}
17 changes: 8 additions & 9 deletions core/gb/cartridge/mbc1.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cartridge

type mbc1 struct {
c *Cartridge
ramEnabled bool
romBank uint8
ramBank uint8
mode uint8
c *Cartridge
ramEnabled bool
romBank, ramBank uint8
mode uint8
}

func newMBC1(c *Cartridge) mbc {
func newMBC1(c *Cartridge) *mbc1 {
return &mbc1{
c: c,
romBank: 1,
Expand All @@ -18,15 +17,15 @@ func newMBC1(c *Cartridge) mbc {
func (m *mbc1) read(addr uint16) uint8 {
switch addr >> 12 {
case 0x0, 0x1, 0x2, 0x3:
return m.c.rom[addr&0x3FFF]
return m.c.ROM[addr&0x3FFF]
case 0x4, 0x5, 0x6, 0x7:
romBank := uint(m.romBank)
if m.mode == 0 {
if len(m.c.rom) >= int(1*MB) {
if len(m.c.ROM) >= int(1*MB) {
romBank |= (uint(m.ramBank) << 5)
}
}
return m.c.rom[(romBank<<14)|uint(addr&0x3FFF)]
return m.c.ROM[(romBank<<14)|uint(addr&0x3FFF)]
case 0xA, 0xB:
if m.ramEnabled {
ramBank := uint(0)
Expand Down
52 changes: 52 additions & 0 deletions core/gb/cartridge/mbc2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cartridge

type mbc2 struct {
c *Cartridge
ramEnabled bool
romBank uint8 // 0..15
}

func newMBC2(c *Cartridge) *mbc2 {
return &mbc2{
c: c,
romBank: 1,
}
}

func (m *mbc2) reset() {
m.ramEnabled = false
m.romBank = 1
}

func (m *mbc2) read(addr uint16) uint8 {
switch addr >> 12 {
case 0x0, 0x1, 0x2, 0x3: // ROMバンク0
return m.c.ROM[addr&0x3FFF]
case 0x4, 0x5, 0x6, 0x7: // ROMバンク1..15
return m.c.ROM[(uint(m.romBank)<<14)|uint(addr&0x3FFF)]
case 0xA, 0xB: // RAM
if m.ramEnabled {
return 0xF0 | (m.c.ram[addr&0x1FF] & 0x0F)
}
}
return 0xFF
}

func (m *mbc2) write(addr uint16, val uint8) {
switch addr >> 12 {
case 0x0, 0x1, 0x2, 0x3: // RAM有効化 or ROMバンク切り替え
mode := (addr >> 8) & 0x1
if mode == 0 {
m.ramEnabled = val == 0x0A
} else {
m.romBank = val & 0x0F
if m.romBank == 0 {
m.romBank = 1
}
}
case 0xA, 0xB: // RAM書き込み
if m.ramEnabled {
m.c.ram[addr&0x1FF] = val & 0x0F
}
}
}
27 changes: 13 additions & 14 deletions core/gb/cartridge/mbc3.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ type rtc struct {
}

type mbc3 struct {
c *Cartridge
ramEnabled bool
romBank uint
ramBank uint
rtc rtc
ramBankMax uint
c *Cartridge
ramEnabled bool
romBank, ramBank uint8
rtc rtc
ramBankMax uint8
}

func newMBC3(c *Cartridge) mbc {
Expand All @@ -39,19 +38,19 @@ func newMBC3(c *Cartridge) mbc {
// ポケモンクリスタルなどは、MBC30と呼ばれる特殊なMBC3を使っている
// これを見分ける方法は今のところ、カートリッジヘッダのROMサイズとRAMサイズを見るしかない
func (m *mbc3) isMBC30() bool {
return (len(m.c.rom) > int(2*MB)) || (len(m.c.ram) > int(32*KB))
return (len(m.c.ROM) > int(2*MB)) || (len(m.c.ram) > int(32*KB))
}

func (m *mbc3) read(addr uint16) uint8 {
switch addr >> 12 {
case 0x0, 0x1, 0x2, 0x3:
return m.c.rom[addr&0x3FFF]
return m.c.ROM[addr&0x3FFF]
case 0x4, 0x5, 0x6, 0x7:
return m.c.rom[(m.romBank<<14)|uint(addr&0x3FFF)]
return m.c.ROM[(uint(m.romBank)<<14)|uint(addr&0x3FFF)]
case 0xA, 0xB:
if m.ramEnabled {
if m.ramBank < m.ramBankMax {
return m.c.ram[(m.ramBank<<13)|uint(addr&0x1FFF)]
return m.c.ram[(uint(m.ramBank)<<13)|uint(addr&0x1FFF)]
}

// RTC
Expand Down Expand Up @@ -81,16 +80,16 @@ func (m *mbc3) write(addr uint16, val uint8) {
case 0x0, 0x1:
m.ramEnabled = (val&0x0F == 0x0A)
case 0x2, 0x3:
m.romBank = uint(val & 0b111_1111)
m.romBank = (val & 0b111_1111)
if m.isMBC30() {
m.romBank = uint(val)
m.romBank = val
}
if m.romBank == 0 {
m.romBank = 1
}
case 0x4, 0x5:
if val <= 0x0C {
m.ramBank = uint(val)
m.ramBank = val
}
case 0x6, 0x7:
// 現在のRTCの値をlatch(保存), これで特定の瞬間のRTCの値を取得できる
Expand All @@ -99,7 +98,7 @@ func (m *mbc3) write(addr uint16, val uint8) {
case 0xA, 0xB:
if m.ramEnabled {
if m.ramBank < m.ramBankMax {
m.c.ram[(m.ramBank<<13)|uint(addr&0x1FFF)] = val
m.c.ram[(uint(m.ramBank)<<13)|uint(addr&0x1FFF)] = val
} else {
switch m.ramBank {
case 0xC:
Expand Down
Loading

0 comments on commit 3a42d68

Please sign in to comment.