(* * * * * * * * * * * * * * * * * * *
 *
 * prog3-4.pas
 *
 *      Demonstrates
 *          - Save/Restore of the page map
 *
 * * * * * * * * * * * * * * * * * * *)

program prog3_4;

uses    _globals,
        _ems;

var
    ems:            EMS_Ems;

    pageFrame:      EMS_PageFrame;
    handle1:        EMS_EmBlk;
    handle2:        EMS_EmBlk;

    text:           String;

    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.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])^:=
        '     Be careful not to impart your wisdom to a guest';

    StrPtr(@pageFrame^[1][801])^:=
        '     whose background you do not know. You may be instructing';

    StrPtr(@pageFrame^[2][4000])^:=
        '     a Nobel Laureate in his own field';

    StrPtr(@pageFrame^[three][2000])^:=
        '                     -- David Brown'#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. This will save the current map
     *  of the PFA and associate the saved state with handle1.
     *  We could as easily save it with handle2 as long as
     *  we restore it from the same place we saved it.
     *)
    if handle1.savePageMap then begin
        ems_demoError('EMS_EmBlk.savePageMap');
        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])^:=
        '     I''m astounded by people who want to ''know''';

    StrPtr(@pageFrame^[1][801])^:=
        '     the universe when it''s hard enough to find your';

    StrPtr(@pageFrame^[2][4000])^:=
        '     way around Chinatown.';

    StrPtr(@pageFrame^[three][2000])^:=
        '                     -- Woody Allen'#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 restore the old map.
     *)
    if handle1.restorePageMap then begin
        ems_demoError('EMS_EmBlk.restorePageMap');
        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])^);

    (*
     *  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.


