Saturday, September 27, 2014

The Parallel Port Problem

Recently I was spending some time programming for Nintendo64 using a gameshark as a means to inject "homebrew" code on the system and have it execute. There exists a version of gameshark (v3.3) that has a parallel port built in that allows you to essentially read and write memory from a PC. The functionality is mainly there to help people create codes by searching for values and modifying them to fit their needs. Because I don't have a parallel port built into my laptop, and because USB to parallel port adapters are 99% of the time made specifically with printer functionality in mind, as well as the fact that even in these cases the software assumes you have a built in parallel port on your motherboard mapped to 0x378, I needed to find another way to use the utilities.

In the past I've made a few hacks for the gameshark "official utilities" that would allow you to use a port outside of the normal 0x378 range, and I've used this method in order to be able to use a PCI parallel port card with the gameshark software. The problem is this doesn't help out much for people running laptops with no built in parallel port. 

The gameshark uses a custom protocol through bit-banging, and it has been reverse engineered by HCS. He reverse engineered the protocol for the gameshark communications and wrote some software that would let him upload an NES emulator (NEON64) he wrote for N64 by using the parallel port on the gameshark. At some point I forked the project and added support in to allow it to work on most any game, during any time during the game. 

Recently HCS re-forked the project, so to speak, and turned it into something quite amazing. He found a USB to parallel port adapter that is actually capable of doing raw bit-banging, and he wrote a library for it using libusb and rewrote the gameshark homebrew loader (gsuploader) so that it could now use the USB to parallel port adapter, as well as adding a lot more functionality to the loader.  

At this point I had an idea to get the normal gameshark hacking tools to work with the library that HCS wrote and started on it. The original idea was overly complex and involved a bunch of patching, debugging calls, interprocess communication, code injection... I started to realize that I was going to have to do all of this stuff twice, because there are 2 "official gameshark tools." 

While writing the debugging functionality for the patcher though, I had an idea. I know that on newer versions of windows there's a permissions bitmap for each port, and it requires a higher CPU privilege in order to make use of some of the instructions. Namely the instructions I was interested in were "IN" and "OUT" respectively.  If you attempt to access the port with these instructions the CPU with throw a privileged instruction exception that gets passed to whatever exception handler is in place. By starting a debugging session on another program I was able to trap these privileged instruction exceptions remotely, and use the trap in order to remap where the data went that should have went to/from the parallel port.

By using this method it's insanely slow, but it does work. The overhead of USB for doing a 1-byte transfer makes the setup not quite as ideal as one would hope. One of the interesting aspects is that you could actually remap legacy software that usually uses the 0x378 range to ANY device (arduino, etc) by just rewriting the READ_PORT and WRITE_PORT functions that I've set up, but for now the simple program I wrote just uses the stuff that HCS wrote. 

It's just a demo for now since it's so slow, but if you're desperate and looking for a way to use legacy software through USB then there's a possibility that this might work for you. Keep in mind you'll need a MosChip 7705 USB to parallel port adapter for this software to work out of the box, but like I said before, you could adapt it to work with anything. 

*HCS: Website: (http://hcs64.com) | github: (https://github.com/hcs64)

*ppcasm: Website: (you are here...) | github: (https://github.com/ppcasm)

mappar: github: (https://github.com/ppcasm/mappar)

DEMO:  https://dl.dropboxusercontent.com/u/6458262/mappar_n64.7z

The demo works with Nintendo64 gameshark "n64utils" program. Just unzip, install libusb drivers through zadig, insert MosChip7705 based adapter, and double click the RUN_N64UTILS.bat file.

No comments:

Post a Comment