<commit_before>
#include <stdio.h>
#include <conio.h> /* this is where Open Watcom hides the outp() etc. functions */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <assert.h>
#include <fcntl.h>
#include <dos.h>

#include <hw/dos/dos.h>
#include <hw/dos/dosbox.h>
#include <hw/8237/8237.h>		/* 8237 DMA */
#include <hw/8254/8254.h>		/* 8254 timer */
#include <hw/8259/8259.h>		/* 8259 PIC interrupts */
#include <hw/sndsb/sndsb.h>
#include <hw/dos/doswin.h>
#include <hw/dos/tgusmega.h>
#include <hw/dos/tgussbos.h>

/* Windows 9x VxD enumeration */
#include <windows/w9xvmm/vxd_enum.h>

/* uncomment this to enable debugging messages */
//#define DBG

#if defined(DBG)
# define DEBUG(x) (x)
#else
# define DEBUG(x)
#endif

int sndsb_read_dsp(struct sndsb_ctx * const cx) {
    /* TODO: convert this to use cx->dspio_adj_base so we don't compute baseio+READ_STATUS+alias every time */
    const unsigned int status_port = cx->baseio+SNDSB_BIO_DSP_READ_STATUS+(cx->dsp_alias_port?1:0);
    const unsigned int read_data_port = cx->baseio+SNDSB_BIO_DSP_READ_DATA+(cx->dsp_alias_port?1:0);
    register unsigned int pa_hi = (unsigned int)(SNDSB_IO_COUNTDOWN >> 16UL) + 1U;
    register unsigned char c; /* encourage C/C++ optimizer to convert c = inp() ... return c to just return inp() */

    do {
        unsigned int pa_lo = (unsigned int)(SNDSB_IO_COUNTDOWN & 0xFFFFUL);

        do {
            if (inp(status_port) & 0x80) {/* data available? */
                c = inp(read_data_port);
                DEBUG(fprintf(stdout,"sndsb_read_dsp() == 0x%02X\n",c));
                return c;
            }
        } while (--pa_lo != 0U);
    } while (--pa_hi != 0U);

    DEBUG(fprintf(stdout,"sndsb_read_dsp() read timeout\n"));
    return -1;
}

int sndsb_write_dsp(struct sndsb_ctx * const cx,const uint8_t d) {
    /* TODO: convert this to use cx->dspio_adj_base so we don't compute baseio+READ_STATUS+alias every time */
    const unsigned int status_port = cx->baseio+SNDSB_BIO_DSP_WRITE_STATUS+(cx->dsp_alias_port?1:0);
    const unsigned int write_data_port = cx->baseio+SNDSB_BIO_DSP_WRITE_DATA+(cx->dsp_alias_port?1:0);
    register unsigned int pa_hi = (unsigned int)(SNDSB_IO_COUNTDOWN >> 16UL) + 1U;

	DEBUG(fprintf(stdout,"sndsb_write_dsp(0x%02X)\n",d));

    do {
        register unsigned int pa_lo = (unsigned int)(SNDSB_IO_COUNTDOWN & 0xFFFFUL);

        do {
            if ((inp(status_port) & 0x80) == 0) {
                outp(write_data_port,d);
                return 1;
            }
        } while (--pa_lo != 0U);
    } while (--pa_hi != 0U);

	DEBUG(fprintf(stdout,"sndsb_write_dsp() timeout\n"));
	return 0;
}
<commit_msg>refactor sbdspio.c code to encourage the C compiler to generate code as if the readiness of the DSP is unlikely (because the DSP will most likely take 2-30 reads during polling to signal readiness). Refactor 'patience' code to use one variable for 32-bit builds, 2 variables for 16-bit builds.<commit_after>
#include <stdio.h>
#include <conio.h> /* this is where Open Watcom hides the outp() etc. functions */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <assert.h>
#include <fcntl.h>
#include <dos.h>

#include <hw/dos/dos.h>
#include <hw/dos/dosbox.h>
#include <hw/8237/8237.h>       /* 8237 DMA */
#include <hw/8254/8254.h>       /* 8254 timer */
#include <hw/8259/8259.h>       /* 8259 PIC interrupts */
#include <hw/sndsb/sndsb.h>
#include <hw/dos/doswin.h>
#include <hw/dos/tgusmega.h>
#include <hw/dos/tgussbos.h>

/* Windows 9x VxD enumeration */
#include <windows/w9xvmm/vxd_enum.h>

/* uncomment this to enable debugging messages */
//#define DBG

#if defined(DBG)
# define DEBUG(x) (x)
#else
# define DEBUG(x)
#endif

int sndsb_read_dsp(struct sndsb_ctx * const cx) {
    /* TODO: convert this to use cx->dspio_adj_base so we don't compute baseio+READ_STATUS+alias every time */
    const unsigned int status_port = cx->baseio+SNDSB_BIO_DSP_READ_STATUS+(cx->dsp_alias_port?1:0);
    const unsigned int read_data_port = cx->baseio+SNDSB_BIO_DSP_READ_DATA+(cx->dsp_alias_port?1:0);
    register unsigned char c; /* encourage C/C++ optimizer to convert c = inp() ... return c to just return inp() */

    /* check and test in the negative, not the affirmative,
     * to encourage Open Watcom C to generate something like:
     *
     * L$1:
     * in al,dx
     * test al,80h
     * jnz code_to_read_data_port
     * dec patience
     * jnz L$1
     *
     * Open Watcom does not have GCC's likely/unlikely macros,
     * therefore the only way to encourage this is to write the
     * if() statement to test for what we think is most likely
     * to happen. */

    {
#if TARGET_MSDOS == 32
        register unsigned int patience = (unsigned int)(SNDSB_IO_COUNTDOWN);

        do {
            if (!(inp(status_port) & 0x80)) {/* data NOT available? */
                if (--patience == 0U)
                    break;
            }
            else {
                c = inp(read_data_port);
                DEBUG(fprintf(stdout,"sndsb_read_dsp() == 0x%02X\n",c));
                return c;
            }
        } while (1);
#else
        register unsigned int pa_hi = (unsigned int)(SNDSB_IO_COUNTDOWN >> 16UL) + 1U;

        do {
            register unsigned int pa_lo = (unsigned int)(SNDSB_IO_COUNTDOWN & 0xFFFFUL);

            do {
                if (!(inp(status_port) & 0x80)) {/* data NOT available? */
                    if (--pa_lo == 0U)
                        break; /* from inner while loop, to check and loop while --pa_hi != 0U */
                }
                else {
                    c = inp(read_data_port);
                    DEBUG(fprintf(stdout,"sndsb_read_dsp() == 0x%02X\n",c));
                    return c;
                }
            } while (1);
        } while (--pa_hi != 0U);
#endif
    }

    DEBUG(fprintf(stdout,"sndsb_read_dsp() read timeout\n"));
    return -1;
}

int sndsb_write_dsp(struct sndsb_ctx * const cx,const uint8_t d) {
    /* TODO: convert this to use cx->dspio_adj_base so we don't compute baseio+READ_STATUS+alias every time */
    const unsigned int status_port = cx->baseio+SNDSB_BIO_DSP_WRITE_STATUS+(cx->dsp_alias_port?1:0);
    const unsigned int write_data_port = cx->baseio+SNDSB_BIO_DSP_WRITE_DATA+(cx->dsp_alias_port?1:0);

    DEBUG(fprintf(stdout,"sndsb_write_dsp(0x%02X)\n",d));

    /* check and test in the negative, not the affirmative,
     * to encourage Open Watcom C to generate something like:
     *
     * L$1:
     * in al,dx
     * test al,80h
     * jnz code_to_write_data_port
     * dec patience
     * jnz L$1
     *
     * Open Watcom does not have GCC's likely/unlikely macros,
     * therefore the only way to encourage this is to write the
     * if() statement to test for what we think is most likely
     * to happen. */

    {
#if TARGET_MSDOS == 32
        register unsigned int patience = (unsigned int)(SNDSB_IO_COUNTDOWN);

        do {
            if (!(inp(status_port) & 0x80)) {/* data NOT available? */
                if (--patience == 0U)
                    break;
            }
            else {
                outp(write_data_port,d);
                return 1;
            }
        } while (1);
#else
        register unsigned int pa_hi = (unsigned int)(SNDSB_IO_COUNTDOWN >> 16UL) + 1U;

        do {
            register unsigned int pa_lo = (unsigned int)(SNDSB_IO_COUNTDOWN & 0xFFFFUL);

            do {
                if (!(inp(status_port) & 0x80)) {/* data NOT available? */
                    if (--pa_lo == 0U)
                        break;
                }
                else {
                    outp(write_data_port,d);
                    return 1;
                }
            } while (1);
        } while (--pa_hi != 0U);
#endif
    }

    DEBUG(fprintf(stdout,"sndsb_write_dsp() timeout\n"));
    return 0;
}

{'fraction_non_alphanumeric': 0.12531645569620253, 'fraction_numerical': 0.023544303797468354, 'mean_word_length': 2.3924431086303133, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '464627', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2715, 'n_words': 702}