Gameboy emulator written in FreeBasic

User projects written in or related to FreeBASIC.
IchMagBier
Posts: 43
Joined: Jan 13, 2018 8:47

Gameboy emulator written in FreeBasic

Postby IchMagBier » Oct 19, 2018 15:58

Hello

I wrote a Gameboy emulator with FreeBasic and GTK. It should support most games. I successfully tested:
Sound isn't supported at the moment.
Games are saved and loaded automatically when you start/stop the emulator. The savegames are stored in the "saves"-folder.

Download link: https://github.com/IchMagBier/FB-Gameboy
Sourcecode and a Linux binary are included.
ImageImageImageImage

Usage
Simply run "fbgb" from command-line and pass the rom-file as the parameter, like this:

Code: Select all

./fbgb rom.gb

Controls
  • [WASD] Moving
  • [Q / E] A / B
  • [SPACE] Start
  • [CTRL] Select
  • [F1] Changes the color palette
  • [F2] Changes the speed
  • [F3] Displays the memory
  • [F4] Displays a disassembly of the code, aswell as the current state of the CPU-registers
  • [F5] Displays a log of the last "interesting" actions, aswell as some misc infos
  • [F6] Reset
Resizing the window also resizes the game screen.

Please look at the output of [F3]. It will show the whole 64kB address space of the Gameboy. I think it's really amazing that they were able to write great games with this tiny amount of memory, even with memory banking.

I don't have Windows to test it and I don't trust WINE, would be awesome if someone could tell me, if it works.

Changelog:
21. Oct
- Added support for savegames.
- Added support for MBC3
- Added [F6]-reset
- Fixed the "flickering" when starting certain games
20. Oct
- Added a GTK-dialog for opening the rom-file, when it wasn't passed as the parameter
- Fixed segfault on exit
- Fixed sprite not displaying when SpriteX < 8
- Fixed weird joypad behavior
19. Oct
- Initial upload
Last edited by IchMagBier on Oct 21, 2018 6:22, edited 2 times in total.
D.J.Peters
Posts: 7859
Joined: May 28, 2005 3:28

Re: Gameboy emulator written in FreeBasic

Postby D.J.Peters » Oct 19, 2018 17:13

Not bad at all
but for the screen output and key input you can use FreeBASIC
there is no need for GTK+ for this simple task.

By the way I know you used my implementation of a opcode table ;-)

Joshy
grindstone
Posts: 659
Joined: May 05, 2015 5:35
Location: Germany

Re: Gameboy emulator written in FreeBasic

Postby grindstone » Oct 19, 2018 17:16

WinXP/32:

    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\fbc -s console "fbgb.bas"
    fbgb.bas(54) warning 4(1): Suspicious pointer assignment
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lgtk-win32-2.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lgdk-win32-2.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lgio-2.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lgobject-2.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lglib-2.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lgthread-2.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lgmodule-2.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lcairo
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lpango-1.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lgdk_pixbuf-2.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -lpangocairo-1.0
    C:\Programme\Programmiersprachen\FreeBASIC-1_05_0\bin\win32\ld.exe: cannot find -latk-1.0

    Build error(s)
IchMagBier
Posts: 43
Joined: Jan 13, 2018 8:47

Re: Gameboy emulator written in FreeBasic

Postby IchMagBier » Oct 19, 2018 17:21

D.J. wrote:Not bad at all
but for the screen output and key input you can use FreeBASIC
there is no need for GTK+ for this simple task.

By the way I know you used my implementation of a opcode table ;-)

I am using GTK for being able to resize the window. I think FreeBasic can't do that on its own. I am using FB-commands to draw the screen. Look at the sub "frame_zeichnen()" in "gpu.bas". ;-)

I actually got the idea of the opcode table from here: https://cturt.github.io/cinoop.html
I guess a "select case as const" would be more performant, but the opcode table was way easier to debug.
Where can I find your implementation?

grindstone wrote:Build error(s)

You need to install GTK 2.0.
Lachie Dazdarian
Posts: 2338
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Re: Gameboy emulator written in FreeBasic

Postby Lachie Dazdarian » Oct 20, 2018 19:33

I need help compiling a Windows executable. Anyone managed to do it?
dafhi
Posts: 1276
Joined: Jun 04, 2005 9:51

Re: Gameboy emulator written in FreeBasic

Postby dafhi » Oct 21, 2018 0:22

could u b more specific?
IchMagBier
Posts: 43
Joined: Jan 13, 2018 8:47

[Update] Gameboy emulator

Postby IchMagBier » Oct 21, 2018 6:29

See changelog in my first post.
The emulator now supports savegames. They are loaded and saved automatically when you start/stop the emulator.
I also added support for some Gameboy Color features. Nothing is finished, but i was able to start Pokemon Gold:
Image
It's mostly the graphics-part that isn't finished, as you can see.

Lachie wrote:I need help compiling a Windows executable. Anyone managed to do it?

Do you have GTK and it's dependencies installed? GTK is the only library I am using. Can you post the compiler-output?
St_W
Posts: 1479
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Gameboy emulator written in FreeBasic

Postby St_W » Oct 28, 2018 17:16

You're obviously missing gtk libraries.
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Re: Gameboy emulator written in FreeBasic

Postby Pritchard » Oct 28, 2018 18:20

If anyone builds for Windows, having a Windows distribution (with binaries) would be best. In my experience, that's usually how Windows applications are deployed: https://www.gtk.org/download/windows.php

I might give it a go. Will GTK+3 work? Or is this GTK 2, or...?
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: Gameboy emulator written in FreeBasic

Postby Roland Chastain » Oct 28, 2018 18:35

Hello!

Very interesting. I have just downloaded the source code from Github. I cannot compile it.

C:\Atelier\Basic\gameboy>build

C:\Atelier\Basic\gameboy>C:\FBC\1.05.0\fbc.exe src\fbgb.bas -x fbgb.exe
C:\Atelier\Basic\gameboy\src\ui.bas(38) warning 3(1): Passing different pointer types, at parameter 1 of GTK_WINDOW_GET_
SIZE()
C:\Atelier\Basic\gameboy\src\gpu.bas(189) error 41: Variable not declared, MapDaten in 'local uint8 TileID = speicher(Ma
pDaten + (TileY * 32) + TileX) 'Index des Tiles im VRAM'
C:\Atelier\Basic\gameboy\src\gpu.bas(190) error 41: Variable not declared, TileDaten in 'local int32 TileDataPtr = iif(T
ileDaten=&h8000,TileDaten + TileID * 16,TileDaten + cast(byte,TileID) * 16) + (TileYOffset * 2)'
C:\Atelier\Basic\gameboy\src\gpu.bas(192) error 41: Variable not declared, i in 'local uint8 BitNummer = 7 - (speicher(M
_SCROLLX) + i) mod 8'
C:\Atelier\Basic\gameboy\src\gpu.bas(194) error 9: Expected expression, found 'i' in 'buffer_bg((speicher(M_SCANLINE) *
160) + i) = bg_pal(Farbe)'
C:\Atelier\Basic\gameboy\src\gpu.bas(195) error 106: NEXT without FOR in 'next'
C:\Atelier\Basic\gameboy\src\gpu.bas(196) error 109: END IF without IF, found 'end' in 'end if'
C:\Atelier\Basic\gameboy\src\gpu.bas(216) error 9: Expected expression, found 'bg_pal' in 'buffer_bg(speicher(M_WX) - 7
+ (speicher(M_SCANLINE) * 160) + i) = bg_pal(Farbe)'
C:\Atelier\Basic\gameboy\src\gpu.bas(243) error 41: Variable not declared, obp1_pal in 'if Farbe <> 0 then buffer_bg(Spr
iteX + i + speicher(M_SCANLINE) * 160) = iif(bit(Flags,4) = true, obp1_pal(Farbe),obp0_pal(Farbe))'
C:\Atelier\Basic\gameboy\src\gpu.bas(248) error 111: END SUB or FUNCTION without SUB or FUNCTION, found 'end' in 'end su
b'
C:\Atelier\Basic\gameboy\src\gpu.bas(250) error 4: Duplicated definition in 'sub frame_zeichnen()'
C:\Atelier\Basic\gameboy\src\gpu.bas(250) error 132: Too many errors, exiting

C:\Atelier\Basic\gameboy>


Please find a solution. I already gave you a star on your Github page. :)

Best regards.

Roland

P.-S. There are missing lines in gpu.bas.
IchMagBier
Posts: 43
Joined: Jan 13, 2018 8:47

Re: Gameboy emulator written in FreeBasic

Postby IchMagBier » Oct 28, 2018 19:23

Pirtchard wrote:I might give it a go. Will GTK+3 work? Or is this GTK 2, or...?

It's GTK 2. ;)
The newest Windows OS I have is NT4 in a VM, sorry.
I would love to hear if someone managed to compile it on Windows. I guess it would be an idea to use the WinAPI instead of GTK on Windows.

Roland wrote:Very interesting. I have just downloaded the source code from Github. I cannot compile it.
P.-S. There are missing lines in gpu.bas.

Wow, you were right. I have no idea how that happened, I fixed it. Updated gpu.bas is on Github.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: Gameboy emulator written in FreeBasic

Postby Roland Chastain » Oct 29, 2018 7:41

IchMagBier wrote:Updated gpu.bas is on Github.


OK, thank you.

I downloaded gtk import libraries and binaries provided by TJF in this post:
viewtopic.php?p=164715#p164715

Now I get this error :

C:\Atelier\Basic\gameboy>C:\FBC\1.05.0\fbc.exe src\fbgb.bas -x fbgb.exe -p "C:\Users\Roland\Downloads\TJF\gtk\lib\win32"
C:\Atelier\Basic\gameboy\src\ui.bas(39) warning 3(1): Passing different pointer types, at parameter 1 of GTK_WINDOW_GET_
SIZE()
src\fbgb.bas(37) warning 4(1): Suspicious pointer assignment
src\fbgb.bas(39) warning 4(1): Suspicious pointer assignment
src\fbgb.o:fake:(.text+0xff07): undefined reference to `g_mutex_unlock'

C:\Atelier\Basic\gameboy>


I saw this discussion:
viewtopic.php?f=14&t=26931

I tried the solution given by TJF in this post:
viewtopic.php?p=250257#p250257

But after that there was another problem, because of the macros used in your program, which are incompatible with "Gir/Gtk-2.0.bi".

So, I have no more ideas. :)

Regards.

Roland
Last edited by Roland Chastain on Oct 30, 2018 10:19, edited 1 time in total.
IchMagBier
Posts: 43
Joined: Jan 13, 2018 8:47

Re: Gameboy emulator written in FreeBasic

Postby IchMagBier » Oct 30, 2018 8:12

Which macros are incompatible?
I might upload a version without the GTK-features for Windows or DOS this week. I just need it for scaling the window and the file-dialog.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: Gameboy emulator written in FreeBasic

Postby Roland Chastain » Oct 30, 2018 10:13

IchMagBier wrote:Which macros are incompatible?


Here is the modification I made into ui.bas (see explanation in my previous post):

Code: Select all

'#include once "gtk/gtk.bi"
#include once "gnome/gir/gtk-2.0.bi"


And here is compiler output:

C:\Atelier\Basic\gameboy>build
C:\Atelier\Basic\gameboy>C:\FBC\1.05.0\fbc.exe -version
FreeBASIC Compiler - Version 1.05.0 (01-31-2016), built for win32 (32bit)
Copyright (C) 2004-2016 The FreeBASIC development team.
standalone
C:\Atelier\Basic\gameboy>C:\FBC\1.05.0\fbc.exe src\fbgb.bas -x fbgb.exe -p gtk\lib\win32
C:\Atelier\Basic\gameboy\src\alex-macros.bas(16) error 4: Duplicated definition, char in '#define char as ubyte'
C:\Atelier\Basic\gameboy\src\ui.bas(40) warning 3(1): Passing different pointer types, at parameter 1 of GTK_WINDOW_GET_SIZE()
C:\Atelier\Basic\gameboy\src\ui.bas(47) warning 3(1): Passing different pointer types, at parameter 1 of GDK_DRAW_RGB_32_IMAGE()
C:\Atelier\Basic\gameboy\src\gpu.bas(148) warning 3(1): Passing different pointer types, at parameter 1 of GDK_DRAW_RGB_32_IMAGE()
src\fbgb.bas(40) warning 4(1): Suspicious pointer assignment
src\fbgb.bas(40) error 41: Variable not declared, gtk_signal_connect in 'screenres_gtk(160 * zoom, 144 * zoom, "GB")'
src\fbgb.bas(40) error 41: Variable not declared, g_signal_connect in 'screenres_gtk(160 * zoom, 144 * zoom, "GB")'
src\fbgb.bas(40) error 3: Expected End-of-Line, found 'g_signal_connect' in 'screenres_gtk(160 * zoom, 144 * zoom, "GB")'
src\fbgb.bas(40) error 3: Expected End-of-Line, found 'g_signal_connect' in 'screenres_gtk(160 * zoom, 144 * zoom, "GB")'
src\fbgb.bas(42) warning 4(1): Suspicious pointer assignment
src\fbgb.bas(42) error 3: Expected End-of-Line, found 'gtk_signal_connect' in 'screenres_gtk(160 * zoom, 144 * zoom, "GB")'
src\fbgb.bas(42) error 3: Expected End-of-Line, found 'g_signal_connect' in 'screenres_gtk(160 * zoom, 144 * zoom, "GB")'
src\fbgb.bas(42) error 3: Expected End-of-Line, found 'g_signal_connect' in 'screenres_gtk(160 * zoom, 144 * zoom, "GB")'
src\fbgb.bas(42) error 3: Expected End-of-Line, found 'g_signal_connect' in 'screenres_gtk(160 * zoom, 144 * zoom, "GB")'
src\fbgb.bas(131) warning 3(1): Passing different pointer types, at parameter 1 of GDK_DRAW_RGB_32_IMAGE()
src\fbgb.bas(134) warning 3(1): Passing different pointer types, at parameter 1 of GDK_DRAW_RGB_32_IMAGE()
src\fbgb.bas(137) warning 3(1): Passing different pointer types, at parameter 1 of GDK_DRAW_RGB_32_IMAGE()
C:\Atelier\Basic\gameboy>


As you can see, char is already defined somewhere.

IchMagBier wrote:I might upload a version without the GTK-features for Windows or DOS this week. I just need it for scaling the window and the file-dialog.


OK, I will wait for the next version. ;)

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 1 guest