gabonator / Education

Education projects and publications
55 stars 7 forks source link

Step by step tutorial #1

Open velteyn opened 3 years ago

velteyn commented 3 years ago

Hello, I think that this program have a LOT of potential and I could give you an hand, I watched the video but it's too fast for me (I'm an old man of 48) . Can you give us a Step By Step written tutorial that can help us to setup all the toolchain ? (Ex [1] install IDA Pro version x.y.z [2] compile the tools etc etc, how to install the tools for noobs that don't know IDA etc) Thank you Edoz

gabonator commented 3 years ago

Well, the video precisely covered all important steps to convert a game. When I was recording it for the first time, it took 40 minutes and it would be too boring to watch it, so I decided to speed up some parts and and skip some boring stuff. And if I am too fast, just pause the video and follow me frame by frame :) You will need two things:

  1. IDA disassembler - I do not know what version it was, any version should work - we are working with games made in early 90's... I was using it through virtualbox because I couldn't find any decent disassembler for mac.
  2. Then you need some programming IDE - it could be Xcode or Visual studio or Visual studio code, or even pure text editor should be sufficient. You will need to build the cicoparser code - in the video I am using a script which just calls g++ -std=c++17 ../CicParser2021/main.cpp -o cicoparser -stdlib=libc++, this should work on osx/linux, on windows you need to create new command line tool project in Visual studio and place there the source code. Cicoparser does not have any dependencies - it contains even json parser (buggy, but at least it doesn't require any external library)

And if you have IDA running and cicoparser binary, with the help of a script cico.py (in scripts folder) you can generate assembly code which in turn you use to feed cicoparser. Currently it works with games written in pure assembly, I did not have time to add support for Turbo-C or Pascal, but it is on my very long to-do list. Currently I do not plan further development in near future, hoping that the community will start contributing to this project by following the video tutorial.

After the conversion you will get one CPP source file. Just check whether there any unresolved functions at the end of this file, put there some dummy implementation (e.g. assert(0)) and place forward declarations at the top. There may be some lines which cicoparser fails to automatically convert into C, so they need to be fixed by hand. Generated code cannot be built alone, it has following dependencies (after including cpu.h):

https://github.com/gabonator/Projects/blob/master/XenonResurrection/WasmCdman/source/machine.h

That interface is very simple and clean, it is the "gateway" for the generated code to touch the real computer. You need to implement memory access methods, then emulate interrupts and port reads/writes. After load application binary into memory (this is a bit strange - but sometimes it is not possible to separate data segment from code segment, so we are loading whole blob of binary data which contains X86 instructions which we do not call) and you are ready to run the game.

After emulation of the video/filesystem/dos stuff (machine.cpp), this is the final interface which you can use to obtain the frame buffer (pixels) and to emulate keyboard input:

https://github.com/gabonator/Projects/blob/master/XenonResurrection/WasmCdman/source/interface.cpp

And then it depends on what OS you are using or what API you use for displaying the screen - in my case I have used SDL2.

I recommend using this project for reference, the code is not very clean, but the interfaces are nicely decoupled: https://github.com/gabonator/Projects/tree/master/XenonResurrection/WasmCdman/source

In this code there is one special feature done which I did not cover in the video - to run a game as WASM (webassembly) or for swift (apple ios), it must not be blocking - so you need to break the main game loop and any loops throughout the gameplay. In CDMAN it was a bit painful, because there are many loops (even for death animation). (search for 'breakup' keyword in https://github.com/gabonator/Projects/blob/master/XenonResurrection/WasmCdman/source/app/cdman.cpp) For conversion of the game into javascript I use nasty trick called "javascript generators" - this allows me to to break the infinite loops without modifying the game. If you are building the game as desktop application, you do not need to care about that.

If you have any more questions, feel free to ask...

jan-kleks commented 2 years ago

Really cool project, thank you! Have you heard about: https://github.com/M-HT/SR ? They are also trying to convert old PC games to modern operating systems.

gabonator commented 2 years ago

@jan-kleks thank you for sharing this project, I did not know about that. Old games I focused can be quite easily converted into C, but what that guy did seems really interesting. I definitely need to look to it more closely to take some ideas from him. I would like to upgrade my approach to convert more recent games, or possibly binaries for windows

jan-kleks commented 2 years ago

@jan-kleks I definitely need to look to it more closely to take some ideas from him. I would like to upgrade my approach to convert more recent games, or possibly binaries for windows

You should definitely get in touch with them. And, well, they do not support Mac (yet).

xor2003 commented 2 years ago

@gabonator Hi Gabriel, Really our projects are very close. Lately I was able to reuse DOSBOX as hardware emulation library to reduce porting time to several days. For example I already ported Test drive 3... And I can convert big games for ex. done with Microsoft Quick C and size ~300Kbytes. libdosbox I can be sure the converted source works as original because threse is debug mode when I compare each instruction results when running under DOSBOX and converted. I'm almost ready for release...

jan-kleks commented 2 years ago

@xor2003 Really cool project!

@gabonator Just wanted to let you know that there is currently a discussion going on at VOGONS regarding that SR project I posted about above. They discuss whether such an approach makes sense, performance/latency vs emulation, etc. CicoParser has been mentioned too.

BTW, did you know that there is an improved version of CD-MAN with more levels and multiplayer support? LGR talks about it in his review, unfortunately, the main character has been switched to a much worse looking. A DOS version to play on old hardware (and modern version as well) with all these improvements of version 2.0, but an old main character, could make fans of this game very happy for sure...

xor2003 commented 2 years ago

SR is good for 32-bit games. It is really cool and complex project. But for 32-bit there are decompilers available I think.