# Source code

## Portability

MEMU is written in largely portable C. A few bits are UNIX or Windows specific, particularly around the windows used to display graphics in, and fielding the keystroke events received in them. To that end, there are two seperate platform specific implementations of the windowing code, and in a few places in the code, there are a few `#ifdef` statements.

If you don't want joystick support, you can `#define NO_JOY`, thus removing the dependency on `/usr/include/linux/joystick.h` on Linux, or DirectInput headers and libraries from the DirectX SDK on Windows.

If you don't want DART support, you can `#define NO_DART`. This option is used on Windows, as the current implementation works best when used with UNIX style named pipes.

If you don't want the 80 column screen to be emulated in text mode, then you can `#define NO_TH` and not link with `th.a` or `th.lib`. This can help when building MEMU on Raspberry Pi, by removing the dependency on TH, and thus the dependency on `ncurses`.

There are various hooks in the source allowing the MEMU code to be used as a part of Bill Brendlings MEMU-Pi project.

## Tools

On Linux, I currently use `gcc`. With a bit of tweaking of the UNIX `makefile`, other compilers and XWindows directory locations can be accommodated.

On Kubuntu, I am advised that you need to

    # apt-get install build-essentials
    # apt-get install libncurses5-dev

On Windows I now use Visual Studio Build Tools 2022 and the Windows Software Development Kit - Windows 10.0. I previously used MS Visual Studio 2013, and this might still work. It is now possible to build a 64 bit version of MEMU, though there isn't a compelling reason to do so.

On MacOSX, I currently use Yosemite with XCode (clang based) and XQuartz (which doesn't seem to run very fast). This is why I describe the MacOSX version as "beta quality".

### PortAudio

On Linux you'll need the PortAudio package installed, as MEMU uses this for its sound support. As I use Fedora, I used `dnf -y portaudio portaudio-devel` to get this. On your Linux distro this may differ. On Kubuntu, I am advised that you need to

    # apt-get install libportaudio2
    # ldconfig

Also, you may need to amend the `makefile` to be more like the MacOSX case (ie: explicitly specify paths to the PortAudio headers and libraries.

On Windows you'll need to download the PortAudio source and build it. I downloaded `pa_stable_v190700_20210406.tgz`. PortAudio is difficult to build, and the instructions on the website aren't brilliant, as the MS solution and project files in the download need to be upgraded before they can be used, and to do that you need the full bloaty Visual Studio installation in place. My solution is to exploit `cmake`, which is bundled with the VS 2022 Build Tools. Assuming you've unpacked into `C:\portaudio-v19.7.0`:-``

    vcvars32
    cd /d c:\portaudio\build
    md cmake-vs2022-32
    cd cmake-vs2022-32
    copy ..\msvc\portaudio.def
    edit portaudio.def and comment out 4 PaAsio_ entrypoints
    cmake -DPA_USE_ASIO:BOOL=OFF -A Win32 ..\..
    msbuild portaudio.vcxproj -p:Configuration=Release

Similarly for a 64 bit library, in another Cmd window

    vcvars64
    cd /d c:\portaudio\build
    md cmake-vs2022-64
    cd cmake-vs2022-64
    copy ..\msvc\portaudio.def
    edit portaudio.def and comment out 4 PaAsio_ entrypoints
    cmake -DPA_USE_ASIO:BOOL=OFF ..\..
    msbuild portaudio.vcxproj -p:Configuration=Release

If you have the ASIO library from Steinberg Media Technologies GmbH, installed in `C:\ASIOSDK2`, you needn't disable ASIO support, but note that distributing an ASIO variant is subject to the ASIO license.

### Dependant modules

You also need to build the DIRT and TH modules before building MEMU. DIRT is a library of Directory Traversal code, which MEMU uses in its CP/M emulation layer when enumerating files in a directory. TH is a library of Terminal Handling code, which MEMU can use in its emulation of the 80 column card and driver (if you use the `-mon-th` option).

To build DIRT and TH :-

1. Change into their directory
2. Copy the files from the `unix` or `win32` subdirectory to the current directory
3. Run `make` or `nmake`

### MEMU

To build MEMU :-

1. Change into its directory
2. Copy the files from the `unix` or `win32` subdirectory to the current directory
3. Edit the `makefile`, so that the variables `DIRT` and `TH` reflect the directories where these modules are.
4. If running on a big-endian architecture, you'll need to change the `-DLSB_FIRST` compiler flag to `-DMSB_FIRST`.
5. On Windows, edit the `makefile` so that the `PORTAUDIO` variable reflects where PortAudio is.
6. Run `make` or `nmake`

## Editing

The source code is "folded", and was written using AE, also found at the same site that you found MEMU. If editing using a normal "flat" editor, you'll see strange "fold-crease" comments in the code.