Thursday, January 21, 2010

Building for DOS, part 2: Setting up DJGPP under DOSEMU

First edit your /etc/dosemu/dosemu.conf: bump up the size of the available DPMI memory from the default 0x5000 KiB to something like 0x10000 or even 0x20000, as you might otherwise run out of memory on more complicated source files.
Now you can proceed with DJGPP install. -- I suggest you use the Zip File Picker, as I already mentioned in the previous post.

The Zip File Picker suggests you make the zip extracting from inside DOSEMU. But in my experience extracting from linux works ok and it's more comfortable. Do read the readme. But the first part of the installation can be as simple as this (you could pick a different target directory name, as long as it's not dev or dev/something):

get all the needed ZIP files in a directory
$ cd ~/.dosemu/drive_c
$ mkdir djgpp
$ for i in /path/to/*.zip; do unzip $i -d djgpp; done

Next step is setting the DJGPP environment variable and adding c:\djgpp\bin into the PATH. The easiest way to do this with dosemu is by editing autoexec.bat. A word of warning -- if the file is a symlink and you can't edit the file the symlink points to (as is the case with default Ubuntu install), you have to change the symlink into a (editable) copy first, e.g.:

$ cd ~/.dosemu/drive_c
$ cp autoexec.bat autoexec.bat.copy; mv autoexec.bat.copy autoexec.bat

As the file uses DOS line end characters (CR LF), if might look odd in some linux text editors (like mcedit). The easiest way is to edit the file from within dosemu -- fire up DOSEMU, then type:

edit autoexec.bat

The default dosemu autoexec.bat file sets up some environment variables and then plays some DOSEMU-specific tricks. So before the unix and lredir tricks insert these two lines:

set DJGPP=C:\DJGPP\DJGPP.ENV
set PATH=C:\DJGPP\BIN;%PATH%

The last step is adapting config.sys (use edit config.sys). Zip File Picker suggests config.sys is to contain these three lines:

files=40
fcbs=40,0
shell=c:\dos\command.com c:\dos /e:2048 /p

On my ubuntu install, the "files=40" line is already present. So just "fcbs=40,0" needs to be added. The file also contains:

shellhigh=z:\command.com /e:1024 /p

It worked ok for me if I left it at "shellhigh" and just enlarged the environment size from 1024 to DJGPP-suggested 2048:

shellhigh=z:\command.com /e:2048 /p

Restart DOSEMU (exitemu in dosemu, then fire up DOSEMU again). If everything worked ok you should be able to build and run C (and optionally other languages, if you installed those too) programs in DOSEMU:

C:\test>gcc test1.c -o test1.exe
C:\test>test1
Hello World!

Note: On one of my boxes (running Ubuntu 9.10, DOSEMU 1.4.0+svn.1828-2ubuntu2) I was unable to get g++ versions 4.4.1 and 4.4.2 working. -- But 4.4.2 is working fine for me on Gentoo with dosemu-1.4.1_pre20091009. Here is the bug report.

It seems to be possible to work around the issue for the time being: Set $_cpu_emu = "vm86sim" in /etc/dosemu/dosemu.conf and use GCC version 4.3.2.

Monday, January 11, 2010

Building for DOS, part 1

And now for something completely different. :)

The unthinkable happened -- I had to build some C++ program of mine for FreeDOS. I understand that DOS is by all means a dead platform, but if you happen to have to target DOS, then read on. :)

The good news is that there is a port of GCC for DOS. It's called DJGPP. The website pretty much looks like it looked 10 years ago, which might imply it's dead. But in fact DJGPP is still alive, as the newsgroups and mailing lists prove.

There are more ways how you could use DJGPP: You could install it on a real DOS machine (yuck!). Or you could install linux to DJGPP cross-compiler (haven't tried, but doc implies it's tricky). Or you could install DJGGP inside DOSEMU. That is the way I chose.

Determining what files you need to download is a bit daunting, but DJGPP offers a handy tool, the Zip File Picker.

For DOSEMU you want to select "Build and run programs with DJGPP", then "DOSEMU". Select the languages you need. Unless you're feeling adventurous, you don't need any DOS GUI, you can stick to editing the files from linux, and just build in DOSEMU.

There are in fact more things than what the Zip File Picker offers that you can install. This includes various utilities and libraries. You can even get Perl for DOS. To see what's available, have a look into Getting djgpp - pick a mirror, then go to the "current" directory.

In second part I describe the configuration of DOSEMU for DJGPP.