%bochsdefs; ]> Bochs Developers Guide KevinLawton BryceDenney MichaelCalabrese Vasudeva Resources for developers The development guide describes resources that are intended for developers in particular. Many Bochs resources are also covered in the User Guide, including compile instructions, bochsrc options, how to find the mailing lists, etc.
Setting up CVS write access If you are an official SourceForge developer, then you can use CVS with write access. The CVS contains the most recent copy of the source code, and with write access you can upload any changes you make to the CVS server for others to use. A few extra steps are required the first time you use CVS write access.
Install ssh and cvs First you need to install both cvs (Concurrent Version System) and ssh (Secure Shell). These are already installed on many UNIX systems and also Cygwin (win32 platform). If not, you can install binaries or compile cvs and ssh yourself. The links below should help you get going. CVS software and instructions are available at www.cvshome.org. A free version of secure shell called OpenSSH is at www.openssh.org. OpenSSH requires a library called OpenSSL from www.openssl.org. Be sure to install OpenSSL before trying to compile OpenSSH.
ssh to cvs.sf.net Next, you need to use secure shell to connect to cvs.sf.net. This step is a little strange, because you can't actually log in and get a shell prompt. All that will happen, when you get the username and password right, is that it will create a home directory on that machine for you. That's it! If you try it again, it will say "This is a restricted Shell Account. You cannot execute anything here." At this point, you've succeeded and you never have to do this step again, ever. ssh sfusername@cvs.sf.net Replace sfusername with your Source Forge username, of course. The first time, you will probably get a message like The authenticity of host 'cvs.sf.net' can't be established. Are you sure you want to continue? Just type yes. When it asks for a password, be sure to type your source forge password. If you have trouble logging in, be sure to use your SOURCE FORGE username and password in the ssh line, which isn't necessarily the same as your local username and password. Add the "-v" option to ssh to see more information about what is failing. If you have ssh version 2, it is possible that you might need to add "-1" to the ssh command to force it to use the version 1 protocol.
Set CVS_RSH environment variable to ssh Every time you connect to the Source Forge CVS server (including cvs update, stat, commit, etc.), you must set the CVS_RSH environment variable to ssh. So just take the time now to add one of these lines to your .bashrc/.cshrc, so that the CVS_RSH setting will be there every time you log in. export CVS_RSH=ssh (bash syntax) setenv CVS_RSH ssh (csh syntax)
cvs checkout Finally, you should be able to do the checkout! If you already have a bochs subdirectory directory, move it out of the way because the checkout will overwrite it. export CVSROOT=":ext:sfusername@cvs.bochs.sourceforge.net:/cvsroot/bochs" cvs -z3 checkout bochs sfusername@cvs.sf.net's password: <--type your password In the CVSROOT variable, replace sfusername with your SF username. There's no need to add CVSROOT to your rc files because CVS will remember it after the checkout. The -z3 (optional) just adds some compression to make the checkout go faster. Once all the files have been downloaded, you will have a bochs directory which is checked out with write access!
Using CVS write access
Checking in files Once you have a bochs directory with cvs write access, you can compile the files, edit them, test them, etc. See the documentation section, "Tracking the source code with CVS" for more info on CVS, in the User Manual. (FIXME: add cross reference) But what's new and different is that you can now do cvs commits. When a file is all fixed and ready to share with the rest of the world, you run a commit command to upload your version to the server. First, it's good to do a cvs update to make sure nobody else has changed it since you downloaded it last. $ cvs update file.cc sfusername@cvs.sf.net's password: <--type your password $ cvs commit file.cc sfusername@cvs.sf.net's password: <--type your password [editor opens. type log message, save, and exit.] When CVS starts an editor, The default is usually vi. If you want a different editor, set the EDITOR environment variable to the name of your preferred editor. When you're done, just save the file and quit the editor. Unless there's some problem, you will see a message that says what the new revision number for the file is, and then "done". If while you're editing the log message, you decide that you don't want to commit after all, don't save the file. Quit the editor, and when it asks where the log message went, tell it to abort. Here is an example of a successful checkin: $ cvs commit misc.txt sfusername@cvs.sf.net's password: <--type your password [edit log msg] Checking in misc.txt; /cvsroot/bochs/bochs/doc/docbook/misc.txt,v <-- misc.txt new revision: 1.6; previous revision: 1.5 done And here is an aborted one: $ cvs commit misc.txt sfusername@cvs.sf.net's password: <--type your password [quit editor without saving] Log message unchanged or not specified a)bort, c)ontinue, e)dit, !)reuse this message unchanged for remaining dirs Action: a cvs [commit aborted]: aborted by user
SourceForge bug, feature, and patch trackers &FIXME;
Ideas for other sections Ideas: - how to browse code with cvsweb - how to find an identifier, variable, or specific text in the code - write access CVS (must be an official developer on SF) - how to make patches with CVS
About the code
How to add keymapping in a GUI client Christophe Bothamy, wrote the keymapping code for Bochs, provided these instructions to help developers to add keymapping to a GUI. Bochs creates a bx_keymap_c object named bx_keymap. This object allows you to : - load the configuration specified keymap file - get the translated BX_KEY_* from your GUI key You have to provide a translation function from string to your Bit32u key constant. Casting will be necessary if your key constants are not Bit32u typed. The function must be "static Bit32u (*)(const char *)" typed, and must return BX_KEYMAP_UNKNOWN if it can not translate the parameter string. What you have to do is : - call once "void loadKeymap(Bit32u (*)(const char*))", providing your translation function, to load the keymap - call "Bit32u getBXKey(Bit32u)" that returns the BX_KEY_* constant, for each key you want to map. The file gui/x.cc implements this architecture, so you can refer to it as an example.
Directory Structure &FIXME; cpu directory does this, iodev does that, gui does that
Configure Scripting &FIXME; configure script, makefiles, header files
Log Functions &FIXME; log functions: what is a panic, what is an error, etc.
Emulator Objects &FIXME; objects that do all the work (cpu, mem)
timers &FIXME;
Sound Blaster 16 Emulation Sound Blaster 16 (SB16) emulation for Bochs was written and donated by Josef Drexler, who has a web page on the topic. The entire set of his SB16 patches have been integrated into Bochs, however, so you can find everything you need here. SB16 Emulation has been tested with several soundcards and versions of Linux. Please give Josef feedback on whether is does or doesn't work on your combination of software and hardware.
How well does it work? Right now, MPU401 emulation is next to perfect. It supports UART and SBMIDI mode, because the SB16's MPU401 ports can't do anything else as well. The digital audio basically works, but the emulation is too slow for fluent output unless the application doesn't do much in the background (or the foreground, really). The sound tends to looping or crackle on slower computer, but the emulation appears to be correct. Even a MOD player works, although only for lower sampling speeds. Also, the MIDI data running through the MPU401 ports can be written into a SMF, that is the standard midi file. The wave output can be written into a VOC file, which has a format defined by Creative Labs. This file format can be converted to WAV by sox for example.
Output to a sound card Output is supported on Linux and Windows 95 at the moment. On Linux, the output goes to any file or device. If you have a wavetable synthesizer, midi can go to /dev/midi00, otherwise you may need a midi interpreter. For example, the midid program from the DosEmu project would work. Wave output should go to /dev/dsp. These devices are assumed to be OSS devices, if they're not some of the ioctl's might fail. On Windows, midi and output goes to the midi mapper and the wave mapper, respectively. A future version might have selectable output devices.
Installation on Linux Prerequisites: A wavetable synthesizer on /dev/midi00 and a working /dev/dsp if you want real time music and sound, otherwise output to midi and wave files is also possible. Optionally, you can use a software midi interpreter, such as the midid program from the DosEmu project instead of /dev/midi00.
Configuring bochs There are a few values in config.h that are relevant to the sound functions. Edit config.h after running configure, but before compiling. BX_USE_SB16_SMF should be 1 unless you intend to have several sound cards running at the same time. BX_USE_SOUND_VIRTUAL can be 0 or 1, and determines whether the output class uses virtual functions or not. The former is more versatile and allows to select the class at runtime (not supported at the moment), while the latter is slightly faster. BX_SOUND_OUTPUT_C is the name of the class used for output. The default is to have no output functions, so you need to change this if you want any sound. The following are supported at the moment: bx_sound_linux_c for output to /dev/dsp and /dev/midi00 on Linux (and maybe other OSes that use the OSS driver) bx_sound_windows_c for output to the midi and wave mapper of Windows 3.1 and higher. bx_sound_output_c for no output at all. Setup the SB16 emulation in your .bochsrc, according to instructions in that file.
Runtime configuration The source for the SB16CTRL program that is used to modify the runtime behaviour of the SB16 emulator is included in misc/sb16. You can compile it or download the executable. misc/sb16/ contains a C program that can be run inside the emulator, and the executable for DOS. It currently supports the following commands: &FIXME; number, six numbers, some numbers, and filename below should be in tags -i number: shows the selected emulator info string, e.g. sb16ctrl -i 3 to show how many patch translations are active -t six numbers: loads a translation into the translation table. The numbers are: OldBankMSB,OldBankLSB,OldProgram,NewBankMSB,NewBankLSB,NewProgram All values can be 0..127 or 255. 255 for Old values means match any and for New values means don't change, e.g. sb16ctrl -t 255,255,0,255,255,32 to change patch 0 (Piano) to patch 32 (Acoustic Bass) -r: Reset the patch translation table e.g. sb16ctrl -r -m some numbers: Upload the given numbers to the midi output device. Note that it should be a complete midi message, and also that it is subject to patch translation. e.g. sb16ctrl -m 0x80,64,0 to send a note-off message to channel 0. -f filename: Reads in a file and executes the commands in it. These have the same format as the above commands, except that they don't have the dash "-" in front of them. Comment lines are supported and start with a hash sign "#". -h: Show a brief summary of the commands. All numbers can be valid parameters to the strtol() function, so hex and octal notation is fine. They have to be delimited by either commas "," or slashes "/", spaces are not allowed. The command line can have any number of commands. However, if none are given, "-f -" is assumed, which means commands are taken from stdin.
Features planned for the future Ports to more OS's, but I can't do this myself Finishing the OPL3 FM emulation by translating the music to midi data
Description of the sound output classes This file is intended for programmers who would like to port the sound output routines to their platform. It gives a short outline what services have to be provided. You should also have a look at the exisiting files, SOUNDLNX.CC for Linux and SOUNDWIN.CC for Windows and their respective header files to get an idea about how these things really work.
Files The main include file is bochs.h. It has all definitions for the system-independent functions that the SB16 emulation uses, which are defined in sb16.h. Additionally, every output driver will have an include file, which should be included at the end of sb16.h to allow the emulator to use that driver. To actually make the emulator use any specific driver, BX_SOUND_OUTPUT_C has to be set to the name of the respective output class. Note that if your class contains any system-specific statements, include-files and so on, you should enclose both the include-file and the CC-file in an #if defined (OS-define) construct. Also don't forget to add your file to the object list in iodev/Makefile and iodev/Makefile.in.
Classes The following classes are involved with the SB16 emulation: bx_sb16_c is the class containing the emulator itself, that is the part acting on port accesses by the application, handling the DMA transfers and so on. It also prepares the data for the output classes. bx_sound_output_c is the base output class. It has all the methods used by the emulator, but only as stubs and does not actually produce any output. These methods are then called by the emulator whenever output is necessary. bx_sound_OS_c is derived from bx_sound_output_c. It contains the code to generate output for the OS operating system. It is necessary to override all the methods defined in the base class, unless virtual functions are used. Note that this should remain an option, so try to override all methods, even if only as stubs. They should be declared virtual if and only if BX_USE_SOUND_VIRTUAL is defined, just as in the examples. The constructor should call the inherited constructor as usual, even though the current constructor does not do anything yet.
Methods The following are the methods that the output class has to override. All but constructor and destructor have to return either BX_SOUND_OUTPUT_OK (0) if the function was successful, or BX_SOUND_OUTPUT_ERR (1) if not. If any of the initialization functions fail, output to that device is disabled until the emulator is restarted.
bx_sound_OS_c(bx_sb16_c*sb16) The emulator instantiates the class at the initialization of Bochs. Description of the parameter: sb16 is a pointer to the emulator class. This pointer can then be used to access for example the writelog function to generate sound-related log messages. Apart from that, no access to the emulator should be necessary. The constructor should not allocate the output devices. This shouldn't be done until the actual output occurs; in either initmidioutput() or initwaveoutput(). Otherwise it would be impossible to have two copies of Bochs running concurrently (if anybody ever wants to do this).
~bx_sound_OS_c() The instance is destroyed just before Bochs ends.
int openmidioutput(char *device) openmidioutput() is called when the first midi output starts. It is only called if the midi output mode is 1 (midimode 1). It should prepare the given MIDI hardware for receiving midi commands. openmidioutput() will always be called before openwaveoutput(), and closemidioutput()will always be called before closewaveoutput(), but not in all cases will both functions be called.
Description of the parameters: device is a system-dependent variable. It contains the value of the MIDI=device configuration option. Note that only one midi output device will be used at any one time. device may not have the same value throughout one session, but it will be closed before it is changed.
int midiready() midiready() is called whenever the applications asks if the midi queue can accept more data. Return values: BX_SOUND_OUTPUT_OK if the midi output device is ready. BX_SOUND_OUTPUT_ERR if it isn't ready. Note: midiready() will be called a few times before the device is opened. If this is the case, it should always report that it is ready, otherwise the application (not Bochs) will hang.
int sendmidicommand(int delta, int command, int length, Bit8u data[]) sendmidicommand()is called whenever a complete midi command has been written to the emulator. It should then send the given midi command to the midi hardware. It will only be called after the midi output has been opened. Note that if at all possible it should not wait for the completion of the command and instead indicate that the device is not ready during the execution of the command. This is to avoid delays in the program while it is generating midi output. Description of the parameters: delta is the number of delta ticks that have passed since the last command has been issued. It is always zero for the first command. There are 24 delta ticks per quarter, and 120 quarters per minute, thus 48 delta ticks per second. command is the midi command byte (sometimes called status byte), in the usual range of 0x80..0xff. For more information please see the midi standard specification. length is the number of data bytes that are contained in the data structure. This does not include the status byte which is not replicated in the data array. It can only be greater than 3 for SysEx messages (commands 0xF0 and 0xF7) data[] is the array of these data bytes, in the order they have in the standard MIDI specification. Note, it might be NULL if length==0.
int closemidioutput() closemidioutput() is called before shutting down Bochs or when the emulator gets the stop_output command through the emulator port. After this, no more output will be necessary until openmidioutput() is called again, but midiready() might still be called. It should do the following: Wait for all remaining messages to be completed Reset and close the midi output device
int openwaveoutput(char *device) openwaveoutput() is called when the first wave output occurs, and only if the selected wavemode is 1. It should do the following: Open the given device, and prepare it for wave output or Store the device name so that the device can be opened in startplayback(). openmidioutput() will always be called before openwaveoutput(), and closemidioutput()will always be called before closewaveoutput(), but not in all cases will both functions be called. openwaveoutput() will typically be called once, whereas startplayback() is called for every new DMA transfer to the SB16 emulation. If feasible, it could be useful to open and/or lock the output device in startplayback() as opposed to openwaveoutput() to ensure that it can be used by other applications while Bochs doesn't need it. However, many older applications don't use the auto-init DMA mode, which means that they start a new DMA transfer for every single block of output, which means usually for every 2048 bytes or so. Unfortunately there is no way of knowing whether the application will restart an expired DMA transfer soon, so that in these cases the startwaveplayback function will be called very often, and it isn't a good idea to have it reopen the device every time. The buffer when writing to the device should not be overly large. Usually about four buffers of 4096 bytes produce best results. Smaller buffers could mean too much overhead, while larger buffers contribute to the fact that the actual output will always be late when the application tries to synchronize it with for example graphics. The parameters are the following: device is the wave device selected by the user. It is strictly system-dependent. The value is that of the WAVE=device configuration option. Note that only one wave output device will be used at any one time. device may not have the same value throughout one session, but it will be closed before it is changed.
int startwaveplayback(int frequency, int bits, int stereo, int format) This function is called whenever the application starts a new DMA transfer. It should do the following: Open the wave output device, unless openwaveoutput() did that already Prepare the device for data and set the device parameters to those given in the function call The parameters are the following: frequency is the desired frequency of the output. Because of the capabities of the SB16, it can have any value between 5000 and 44,100. bits is either 8 or 16, denoting the resolution of one sample. stereo is either 1 for stereo output, or 0 for mono output. format is a bit-coded value (see below).
Format Bits &FIXME; Insert FORMAT BITS table here
int waveready() This is called whenever the emulator has another output buffer ready and would like to pass it to the output class. This happens every BX_SOUND_OUTPUT_WAVEPACKETSIZE bytes, or whenever a DMA transfer is done or aborted. It should return whether the output device is ready for another buffer of BX_SOUND_OUTPUT_WAVEPACKETSIZE bytes. If BX_SOUND_OUTPUT_ERR is returned, the emulator waits about 1/(frequency * bytes per sample) seconds and then asks again. The DMA transfer is stalled during that time, but the application keeps running, until the output device becomes ready. As opposed to midiready(), waveready() will not be called unless the device is open.
int sendwavepacket(int length, Bit8u data[]) This function is called whenever a data packet of at most BX_SB16_WAVEPACKETSIZE is ready at the SB16 emulator. It should then do the following: Send this wave packet to the wave hardware This function has to be synchronous, meaning that it has to return immediately, and not wait until the output is done. Also, this function might be called before the previous output is done. If your hardware can't append the new output to the old one, you will have to implement this yourself, or the output will be very chunky, with as much silence between the blocks as the blocks take to play. This is not what you want. Instead, waveready() should return BX_SOUND_OUTPUT_ERR until the device accepts another block of data. Parameters: length is the number of data bytes in the data stream. It will never be larger than BX_SB16_WAVEPACKETSIZE. data is the array of data bytes. The order of bytes in the data stream is the same as that in the Wave file format:
Sequences of Databytes Table &FIXME; Insert Sequences of Databytes table here Typically 8 bit data will be unsigned with values from 0 to 255, and 16 bit data will be signed with values from -32768 to 32767, although the SB16 is not limited to this. For further information on the codecs and the use of reference bytes please refer to the Creative Labs Sound Blaster Programmer's Manual, which can be downloaded from the Creative Labs web site.
int stopwaveplayback() This function is called at the end of a DMA transfer. It should do the following: Close the output device if it was opened by startwaveplayback(). and it's not going to be opened soon. Which is almost impossible to tell.
int closewaveoutput() This function is called just before Bochs exits. It should do the following: Close the output device, if this hasn't been done by stopwaveplayback(). Typically, stopwaveplayback() will be called several times, whenever a DMA transfer is done, where closewaveoutput() will only be called once. However, in the future it might be possible that openwaveoutput() is called again, for example if the user chose to switch devices while Bochs was running. This is not supported at the moment, but might be in the future.
Debugger
compile with debugger support &FIXME;
get started in debugger &FIXME;
command reference &FIXME;
techniques &FIXME;
Coding
coding conventions &FIXME;
patches: &FIXME; how to make, where to submit, what happens then?
life cycle of a CVS release &FIXME;