Skip to content

Commit

Permalink
fixed framerate
Browse files Browse the repository at this point in the history
fixed transparent pixels in sprites
tweaks in rom bank changing
  • Loading branch information
gonccalo committed Apr 14, 2017
1 parent 8f0d5be commit 1bc3740
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions gameboygo/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func Reset() {
regs.l = 0x4D
regs.sp = 0xFFFE
regs.pc = 0x100
writeByte(0xFF04, 0xAB)
writeByte(0xFF05, 0x00)
writeByte(0xFF06, 0x00)
writeByte(0xFF07, 0x00)
Expand Down
12 changes: 6 additions & 6 deletions gameboygo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ func main() {
if *fdebug {
for {
t := time.Now()
for CicleCounter = 0; (CicleCounter < CPU_FREQ); {
for CicleCounter = 0; (CicleCounter < (CPU_FREQ/60)); {
handleInput()
ExecuteDebug()
UpdateGPU(renderer, texture, cicles_op)
}
fmt.Println(time.Second - time.Since(t))
time.Sleep(time.Second - time.Since(t))
fmt.Println((time.Second/60) - time.Since(t))
time.Sleep((time.Second/60) - time.Since(t))
}
} else{
for {
CicleCounter = 0
t := time.Now()
for ;(CicleCounter <= CPU_FREQ); {
for ;(CicleCounter <= (CPU_FREQ/60)); {
handleInput()
Execute()
UpdateGPU(renderer, texture, cicles_op)
}
fmt.Println(time.Second - time.Since(t))
time.Sleep(time.Second - time.Since(t))
//fmt.Println((time.Second/60) - time.Since(t))
time.Sleep((time.Second/60) - time.Since(t))
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions gameboygo/rom.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ func Load_rom(filename string) {

func changeLRomBank(num uint8) {
if Head.cart_type == ROM_MBC1 || Head.cart_type == ROM_MBC1_RAM_BATT || Head.cart_type == ROM_MBC1_RAM{
actualRom := romBank
romBank = romBank & 0xE0
romBank = romBank | (num & 0x1F)
if romBank == 0 {
romBank = 1
}
if actualRom == romBank {
return
}
if num := copy(ram[0x4000:0x8000], RomData[0x4000 * uint32(romBank):0x4000 * (uint32(romBank) + 1)]); num != 0x4000{
fmt.Printf("ERRO no bank, copiados %d\n", num)
return
Expand All @@ -108,11 +112,15 @@ func changeLRomBank(num uint8) {
func changeHRomOrRamBank(num uint8) {
if Head.cart_type == ROM_MBC1 || Head.cart_type == ROM_MBC1_RAM_BATT || Head.cart_type == ROM_MBC1_RAM{
if bankMode == 0{
actualRom := romBank
romBank = romBank & 0x1F
romBank = romBank | ((num&0x03)<<5)
if romBank == 0 {
romBank = 1
}
if actualRom == romBank {
return
}
if num := copy(ram[0x4000:0x8000], RomData[0x4000 * uint32(romBank):0x4000 * (uint32(romBank) + 1)]); num != 0x4000{
fmt.Printf("ERRO no bank, copiados %d\n", num)
return
Expand Down
15 changes: 12 additions & 3 deletions gameboygo/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,12 @@ func DrawLine(renderer *sdl.Renderer) {
//X flip (0=Normal, 1=Horizontally mirrored)
bit = (^Xpixel) & 0x07
}
color := getPixelColor(data0, data1, bit, palette)
if color == WHITE{
//the white color in sprites is transparent
colorCode := getSpritePixelColor(data0, data1, bit)
if colorCode == 0{
//the color 0 in sprites is transparent
continue
}
color := getColor(colorCode, palette)
if (int(spriteX) + int((^Xpixel)&0x07)) >= 160 {
continue
}
Expand All @@ -284,6 +285,14 @@ func DrawLine(renderer *sdl.Renderer) {
}
}

func getSpritePixelColor(lower, upper, bitNum uint8) uint8{
var mask uint8 = (1 << bitNum)
if bitNum < 2 {
return ((upper & mask) << (1-bitNum)) | ((lower & mask) >> bitNum)
}
return (upper & mask) >> (bitNum - 1) | ((lower & mask) >> bitNum)
}

func getPixelColor(lower, upper, bitNum uint8, palette uint8) uint8{
var mask uint8 = (1 << bitNum)
if bitNum < 2 {
Expand Down

0 comments on commit 1bc3740

Please sign in to comment.