-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cpu table optmization #21
Conversation
…or changes to the cb_table function which unecessarily converted indexes to bytes
Codecov Report
@@ Coverage Diff @@
## master #21 +/- ##
==========================================
+ Coverage 65.07% 69.11% +4.04%
==========================================
Files 20 20
Lines 2265 2461 +196
==========================================
+ Hits 1474 1701 +227
+ Misses 762 730 -32
- Partials 29 30 +1
Continue to review full report at Codecov.
|
This is great, thanks @ear7h - I'm going to have a review of it soon, but I thought it would be good to benchmark it first just to check. I added in the new instruction table alongside the old one and then ran this benchmark to run some random instructions: package gb
import (
"math/rand"
"testing"
"time"
"github.com/Humpheh/goboy/pkg/cart"
)
func randomBytes(num int) (out []byte) {
rand.Seed(time.Now().Unix())
for i := 0; i < num; i++ {
out = append(out, byte(rand.Intn(0x100)))
}
out[0x147] = 0
return
}
var randomRom = randomBytes(0x10000)
func testGB() *Gameboy {
gameboy := Gameboy{}
gameboy.mainInst = gameboy.mainInstructions()
gameboy.setup()
gameboy.Memory.Cart = cart.NewCart(randomRom, "")
return &gameboy
}
func BenchmarkGameboy_ExecuteNextOpcode(b *testing.B) {
gameboy := testGB()
for n := 0; n < b.N; n++ {
gameboy.ExecuteOpcode(byte(rand.Intn(0x100)))
}
}
func BenchmarkGameboy_ExecuteNextOpcode2(b *testing.B) {
gameboy := testGB()
for n := 0; n < b.N; n++ {
gameboy.mainInst[byte(rand.Intn(0x100))]()
}
} The results were:
So the new version is definitely faster! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks for doing this. Just some code/comment style points :)
pkg/gb/gameboy.go
Outdated
InputMask byte | ||
|
||
mainInst [0x100]func() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put the mainInst
above the cbInst
so that they are grouped together?
@@ -320,6 +322,8 @@ func (gb *Gameboy) setup() error { | |||
|
|||
gb.cbInst = gb.cbInstructions() | |||
|
|||
gb.mainInst = gb.mainInstructions() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, could you put this line above the gb.cbInst
line?
pkg/gb/instructions.go
Outdated
log.Printf("Unimplemented opcode: %#2x", opcode) | ||
WaitForInput() | ||
} | ||
gb.mainInst[opcode]() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer is the contents of mainInstructions
were in this file rather than a separate one. Also it looks like ExecuteOpcode
is only called in one place, so it may be better just to replace that line with gb.mainInst[opcode]()
and remove this function.
pkg/gb/instructions_table.go
Outdated
0x06: func() { | ||
// LD B, n | ||
gb.CPU.BC.SetHi(gb.popPC()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove these blank lines before the end of each function?
pkg/gb/instructions_table.go
Outdated
val := gb.Memory.ReadHighRam(0xFF00 + uint16(gb.popPC())) | ||
gb.CPU.AF.SetHi(val) | ||
|
||
// ========== 16-Bit Loads =========== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is in the wrong place now and should be outside the function.
pkg/gb/instructions_table.go
Outdated
// POP HL | ||
gb.CPU.HL.Set(gb.popStack()) | ||
|
||
// ========== 8-Bit ALU =========== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with this one.
pkg/gb/instructions_table.go
Outdated
addr := gb.CPU.HL.HiLo() | ||
gb.instDec(func(val byte) { gb.Memory.Write(addr, val) }, gb.Memory.Read(addr)) | ||
|
||
// ========== 16-Bit ALU =========== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another misplaced comment.
pkg/gb/instructions_table.go
Outdated
gb.instDec16(gb.CPU.SP.Set, gb.CPU.SP.HiLo()) | ||
|
||
}, | ||
0x27: func() { /* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment seems to have formatted badly. Could you replace the /* ... */
with just //
lines? I think that makes it more consistent with the rest of the code base.
pkg/gb/instructions_table.go
Outdated
gb.halted = true | ||
|
||
}, | ||
0x10: func() { //gb.halted = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment can be moved below the // STOP
comment
… instructions.go, and minor style changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, thanks! :)
See #20
Implemented a static array for storing operations where the functions are indexed by their opcode. Also removed
byte
casts from thecbInstruction
function as the compiler will already remove the bounds checks from the array. At least based on the assembly output from a similar function:https://godbolt.org/z/t7uSt1