How do I properly allocate a memory buffer to apply double buffering in dosbox using turbo c?

1.2k views Asked by At

Okay so I am trying to apply a double buffering technique in an emulated environment (DosBox) while using the IDE Turbo C++ 3.0 I am running windows 7 64bit(Not sure if that matters) and I have no clue how to properly execute the buffering routine in this environment.

The main problem I am having is that I can't seem to execute the following assignment statement:

double_buffer = (byte_t far*)farmalloc((unsigned long)320*200);

(Note that 320 and 200 are the screen sizes)... I just get NULL for the assignment.

I tried changing the default RAM usage of the DosBox to 32 instead of 16, but that didn't do anything. I'm not sure if it's the emulator or there is something wrong with the code for Turbo C. (Note that it complies just fine).

Here is a sample program I found online:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <string.h>
#include <alloc.h>

typedef unsigned char byte_t;

byte_t far* video_buffer = (byte_t far*)0xA0000000;

void vid_mode(byte_t mode){
    union REGS regs;
    regs.h.ah = 0;
    regs.h.al = mode;
    int86(0x10,&regs,&regs);
}

void blit(byte_t far* what){
    _fmemcpy(video_buffer,what,320*200);
}

int main(){

    int x,y;
    byte_t far* double_buffer;

    double_buffer = (byte_t far*)farmalloc((unsigned long)320*200);
    if(double_buffer == NULL){
        printf("sorry, not enough memory.\n");
        return 1;
    }
    _fmemset(double_buffer,0,(unsigned long)320*200);

    vid_mode(0x13);

    while(!kbhit()){
        x = rand()%320;
        y = rand()%200;
        double_buffer[y * 320 + x] = (byte_t)(rand()%256);
        blit(double_buffer);
    }

    vid_mode(0x03);
    farfree(double_buffer);
    return 0;
}
1

There are 1 answers

0
Michael Petch On

Your problem is related to running your application within the Turbo-C IDE debugger. If you compile it and then exit the IDE and run it directly from the DosBox command line without the IDE it should work as expected.

When running via the IDE, the default debug option is to only allocate an additional 64KiB memory for your program's heap. This isn't enough to handle your request for the 64000 bytes (320*200). In the Turbo-C IDE pull down the options menu, click on debugger. You should get a screen that looks like this:

enter image description here

The default value for Program Heap Size is 64. Change it to the maximum 640 and then click Ok. Rerun your program and it should display randomly colored pixels on the display at random locations.