Chapter 8 -- Overlays





Introduction

Hi again! I'm really satisfied with your faithfulness so far. I hope you're not bored. :-) This time I'd like to talk about overlays. Well, overlays is pretty obsolete nowadays. :-) If you feel so, you can just skip this lesson and pretend nothing has happened. :-) Be cautious to the jargon I've used here. :-) However, in developing countries, there are so many old computers. There are still a lot of XTs and 286s are floating around with memory only 1 MB (and even less). In that case, you need to learn overlay techniques in order to tackle memory shortage. Oh yes! There are no quiz for this chapter! Have fun!
 

What to Learn

Overlays, of course. :-) I'm just explaining overlays to intermediate level, not so advanced ones. Take it easy, this tutorial is intended for those who haven't known anything of this topic. I explain what exactly overlay is, how can we use it, and then using EMS to put our overlay program in. I also tell you about additional settings such as overlay buffers and probation area. That's all. Seems short, right? Let's go straight in!
 

What is Overlay

Overlay is a chunk of files that is a part of a program. It's not a data, it's the code and it IS executable. Overlay is somewhat similar to Dynamic Linked Library (DLL), but overlay is kinda prehistoric version of DLL. :-) The overlay contains the procedure codes that is, usually, not very often to use.

Why should we do overlaying? It's because the memory is not enough to hold all the codes of the program. The trick is to separate it into several parts then load them when needed. If we don't need it, we can simply throw it. Just like dispose in pointers. That trick is called overlays.

Real mode programs can use only 640 kilobytes of memory. You can not use more. That's why protected mode invented. But before protected mode is invented, programmers do overlays. Clear?

I learn programming with my first computer, XT. I use this technique to overcome the problem. However, I finally found out protected mode programming in about 1992 and learn it. Then I considered overlay method is obsolete. After I found that protected mode uses similar method (using DLL) to reserve memory, I think that overlay technique is adapted in such a way so that I stopped considering overlay technique obsolete. It's merely adapted in a new, advanced custom.
 

The Beginning

Making overlays is pretty easy in Borland Pascal. All the tools are ready to use. The first thing that you should put {$F+} on above of your program. That means that we enable far call. What is far call? It is later discussed throughly in low level matters. But first, accept this: The usual calling is near, so we need to make it far in order to satisfy the need of overlay unit.

The routines we want to overlay is put in separate units. In each unit, you can put several procedures and functions. It is better for us to group related routines together inside one unit since it will surely enhance the speed. We also need to add the $O+ switch inside the unit, so that it may look like this: {$F+,O+}. This is needed for units we want to overlay.

All the procedures and functions are made normally. Of course you need to specify uses overlay clause in either overlayed unit or the main program. That's one of the difference in main program. Everything is done transparently. You don't even need to access overlay manager routines. Look at these excerpts:


{ This is OVR.PAS }
{$F+,O+}
uses crt,overlay,ovr1,ovr2;
{$O ovr1.ovr}
{$O ovr2.ovr}

begin
  clrscr;

  { Initialize overlayed files }
  ovrinit('ovr1.ovr');
  if OvrResult <> ovrOk then
  begin
    Writeln('Overlay initialization failed.'); Halt;
  end;

  ovrinit('ovr2.ovr');
  if OvrResult <> ovrOk then
  begin
    Writeln('Overlay initialization failed.'); Halt;
  end;

  { Call the overlayed routines }
  print1;
  print2;
end.

{ This is OVR1.PAS }
{$F+,O+}
unit ovr1;
interface
uses overlay;

procedure print1;

implementation

procedure print1;
begin
  writeln('This is unit 1 reporting!');
end;

end.


Then, you make the file OVR2.PAS. It's just the same as OVR1.PAS except all number 1 is changed into 2. Build each unit and compile the OVR.PAS. The strange thing is it hangs under Borland Pascal 7. What's wrong? It suppose to be correct. Any corrections? Anyway, you can imagine how it works, right? :-)

Accessing Overlay Manager

Most of the times, you don't need to access this. Usually you do this if you have an overlay file made in another language programs. Yeah you could do that too. What you need is just the OVL or OVR file.

The next step is initializing it using ovrinit. The only parameter it takes is the file name of the overlay file, in string. After that, you can check whether the initialization is successful or not. Just check OvrResult. If it is 0, then the initialization success. Otherwise, spits an error. The error number can be reviewed in Borland Pascal Help. Here's an excerpt:

  ovrinit('ovr.ovr');
  if OvrResult <> ovrOk then
  begin
    case OvrResult of
      ovrError: Writeln('Program has no overlays.');
      ovrNotFound: Writeln('Overlay file not found.');
    end;
    Writeln('Overlay initialization failed.'); Halt;
   end;

That's the initialization part. You can put the overlay file into EMS if EMS driver is exist. Just put ovrinitEMS after initialization part. You can check OvrResult after calling ovrinitEMS. If OvrResult is 0, then the overlay file is successfully loaded into EMS.

To speed up things, you can enlarge the overlay buffer. Setting overlay buffer is done through ovrsetbuf. The only parameter it takes is the size of the buffer in bytes. You can get the amount of bytes assigned for buffer by calling ovrgetbuf. Ovrgetbuf is a function with no parameters. It returns the overlay buffer size in bytes.

Overlay manager in Borland Pascal lets you specify the probation area. What is that? That's about the approximation area. The overlay manager usually cannot exactly define the buffer size since program codes may not be cut in the middle of operation. So, by specifying probation area, you let the overlay manager breathe and allow it to manage the overlay files more comfortably.

Setting probation area is done through ovrsetretry. It takes one parameter, that is the size of probation area in bytes. Its complement function, ovrgetretry, returns the probation area size in bytes.

You can flush the overlay by invoking ovrclearbuf. However, after calling this procedure, you need to reload all needed overlayed programs. However, overlay manager never requires you to do so. Doing so will decrease the performance of overlay.

Notes

That's all folks! Not quite long, right? Shall we go to the next lesson?


Where to go?

Back to main page
Back to Pascal Tutorial Lesson 2 contents
To the quiz? No quiz today! Have fun!
To Chapter 9 about printers.
My page of programming link
Contact me here


By: Roby Joehanes, © 1997, 2000