5

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;
}
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Drozerix
  • 53
  • 6
  • 1
    `320 * 200 = 64000` which is close enough to 65536 that I would wonder whether `farmalloc` can allocate a block that large. Is there perhaps a different interface which will give you an entire segment? – zwol Jan 08 '14 at 00:34
  • @Zack My knowledge of dos interfaces is quite weak. But thank you for your thoughts. I guess I could try something different, at this point anything goes really. – Drozerix Jan 08 '14 at 00:55
  • I really don't know anything about it myself. And internet searches suggest that `farmalloc` is *supposed* to let you allocate considerably *more* than 64K in one go, so that kills my original theory... – zwol Jan 08 '14 at 02:08
  • Did you try to run it with the Turbo C++ IDE turned off? I am thinking you may have fragmented memory. Many old school DOS games required that you started the game immediately after DOS booted, so that no other program would claim memory the game needed. – Prof. Falken Sep 08 '14 at 12:18
  • 2
    Also the program must also be compiled under the right memory model. Try to find the boundary of what you can allocate. Can you allocate 1 byte? Can you allocate 32768? Can you allocate 65536? – Prof. Falken Sep 08 '14 at 12:21

1 Answers1

3

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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198