(* * * * * * * * * * * * * * * * * * *
 *
 * prog3-5.pas
 *
 *      Demonstrates
 *          - Save/Restore of the page map using 3.2 calls
 *
 * * * * * * * * * * * * * * * * * * *)

program prog3_5;

uses    _globals,
        _ems;


var
    ems:            EMS_Ems;

    pageFrame:      EMS_PageFrame;

    handle1:        EMS_EmBlk;
    handle2:        EMS_EmBlk;

    mapBuffer1:    ^DWord;
    mapBuffer2:    ^DWord;
    mapInfoSize:    Word;

    i:              Word;

    three:          Word;

begin

    three:= 3;

    (*
     *  First check for presence of EMS
     *)
    if ems.init then begin
        writeln('EMS is not present');
        Halt;
        end;

    (*
     *  Get the Page Frame Address.
     *)
    if ems.getPFA(pageFrame) then begin
        ems_demoError('EMS_Ems.getPFA');
        end;

    (*
     *  Get a couple of 4 page blocks of memory (64K each).
     *)
    if handle1.allocEM(4) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;
    if handle2.allocEM(4) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;

    (*
     *  Now let's map the first.
     *)
    for i:= 0 to 3 do begin
        if handle1.mapPage(i, i) then begin
            ems_demoError('EMS_EmBlk.mapPage'); 
            end;
        end;

    (*
     *  The memory is now mapped into the page frame. Let's
     *  move some text into random spots in it.
     *)
    StrPtr(@pageFrame^[0][400])^:=
        '     Neurotic means he is not as sensible as I am,';

    StrPtr(@pageFrame^[1][801])^:=
        '     and psychotic means that he is even worse than';

    StrPtr(@pageFrame^[2][4000])^:=
        '     my brother-in-law.';

    StrPtr(@pageFrame^[three][2000])^:=
        '                     -- Karl Menninger'#13#10;

    (*
     *  Now print it back.
     *)
    writeln(StrPtr(@pageFrame^[0][400])^);
    writeln(StrPtr(@pageFrame^[1][801])^);
    writeln(StrPtr(@pageFrame^[2][4000])^);
    writeln(StrPtr(@pageFrame^[three][2000])^);

    (*
     *  Save the map state. First we query as to how much
     *  memory is required, allocate the memory, then do
     *  the actual save.
     *)
    if ems.getMapInfoSize32(mapInfoSize) then begin
        ems_demoError('EMS_Ems.mapInfoSize');
        end;
    GetMem(mapBuffer1, mapInfoSize);
    GetMem(mapBuffer2, mapInfoSize);

    if ems.savePageMap32(mapBuffer1) then begin
        ems_demoError('EMS_Ems.savePageMap32');
        end;

    (*
     *  Now let's map the second block
     *)
    for i:= 0 to 3 do begin
        if handle2.mapPage(i, i) then begin
            ems_demoError('EMS_EmBlk.mapPage'); 
            end;
        end;

    (*
     *  The memory is now mapped into the page frame. Let's
     *  move some text into random spots in it.
     *)
    StrPtr(@pageFrame^[0][400])^:=
        '     When I can no longer bear to think of the victims';

    StrPtr(@pageFrame^[1][801])^:=
        '     of broken homes, I begin to think of the victims';

    StrPtr(@pageFrame^[2][4000])^:=
        '     of intact ones.';

    StrPtr(@pageFrame^[three][2000])^:=
        '                     -- Peter De Vries'#13#10;

    (*
     *  Now print it back.
     *)
    writeln(StrPtr(@pageFrame^[0][400])^);
    writeln(StrPtr(@pageFrame^[1][801])^);
    writeln(StrPtr(@pageFrame^[2][4000])^);
    writeln(StrPtr(@pageFrame^[three][2000])^);
        
    (*
     *  Now let's swap maps, saving the current map into mapBuffer2,
     *  and loading the new map from mapBuffer1.
     *)
    if ems.swapPageMap32(mapBuffer1, mapBuffer2) then begin
        ems_demoError('EMS_Ems.swapPageMap32');
        end;

    (*
     *  Now when we print out, we should get the first set
     *  of lines.
     *)
    writeln(StrPtr(@pageFrame^[0][400])^);
    writeln(StrPtr(@pageFrame^[1][801])^);
    writeln(StrPtr(@pageFrame^[2][4000])^);
    writeln(StrPtr(@pageFrame^[three][2000])^);

    (*
     *  Now let's finish by restoring the second page map.
     *)
    if ems.restPageMap32(mapBuffer2) then begin
        ems_demoError('EMS_Ems.restPageMap32');
        end;

    (*
     *  And print the contents:
     *)
    writeln(StrPtr(@pageFrame^[0][400])^);
    writeln(StrPtr(@pageFrame^[1][801])^);
    writeln(StrPtr(@pageFrame^[2][4000])^);
    writeln(StrPtr(@pageFrame^[three][2000])^);

    (*
     *  We're done. Free the memory we've allocated.
     *)
    if handle1.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;
    if handle2.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;
end.


