(* * * * * * * * * * * * * * * * * * *
 *
 * prog4-7.pas
 *
 *      Demonstrates
 *          - Xfer to/from EMS and Conventional memory
 *
 * * * * * * * * * * * * * * * * * * *)

program prog4_7;

uses    _globals,
        _ems;

var

    ems:                EMS_Ems;

    handle1:            EMS_EmBlk;
    handle2:            EMS_EmBlk;
    handle3:            EMS_EmBlk;
    handle4:            EMS_EmBlk;

    movePacket:         EMS_MoveMemoryInfo;

    text:               String;
    buff:               String;

begin

    (*
     *  First check for presence of EMS
     *)
    if ems.init then begin
        writeln('EMS is not present');
        Halt;
        end;

    (*
     *  Allocate some EMS pages
     *)
    if handle1.allocEM(1) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;
    if handle2.allocEM(1) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;
    if handle3.allocEM(1) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;
    if handle4.allocEM(1) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;

    (*
     *  Now let's copy some text into various places in the
     *  EM blocks, and then copy it back and print it out.
     *)

    (*
     *  Set up a pointer to a string:
     *)
    text:= '     Every year when it''s Chinese New Year Here in New York,';

    (*
     *  Set up the move packet. Note that we round the length
     *  up to an even number. The call requires an even length
     *  transfer.
     *)
    movePacket.length:= Ord(text[0])+1;       { +1 for terminator }
    movePacket.srcType:= EMS_MOVE_CONV;  { Conventional }
    movePacket.srcHandle:= 0;            { indicates real memory }
    movePacket.srcOffset:= Ofs(text);    { actual offset }
    movePacket.srcPage:= Seg(text);      { actual segment }
    movePacket.destType:= EMS_MOVE_EMS;  { EMS }
    movePacket.destHandle:= handle1.handle; { 1st 1K block }
    movePacket.destOffset:= 42;          { A random offset into block }
    movePacket.destPage:= 0;             { page zero }

    (*
     *  Do the actual EMS call:
     *)
    if ems.moveMemRegion40(movePacket) then begin
        ems_demoError('EMS_Ems.moveMemRegion40');
        end;

    (*
     *  Now put the next string into the next EM block:
     *)
    text:= '     there are fireworks going off at all hours. New York mothers';

    movePacket.length:= Ord(text[0])+1;  { +1 for terminator }
    movePacket.srcType:= EMS_MOVE_CONV;  { Conventional }
    movePacket.srcHandle:= 0;            { indicates real memory }
    movePacket.srcOffset:= Ofs(text);    { actual offset }
    movePacket.srcPage:= Seg(text);      { actual segment }
    movePacket.destType:= EMS_MOVE_EMS;  { EMS }
    movePacket.destHandle:= handle2.handle; { 2nd 1K block }
    movePacket.destOffset:= 911;         { A random offset into block }
    movePacket.destPage:= 0;             { page zero }

    if ems.moveMemRegion40(movePacket) then begin
        ems_demoError('EMS_Ems.moveMemRegion40');
        end;

    (*
     *  And something for the the third block:
     *)
    text:= '     calm their frightened children by telling them it''s just gunfire'#13#10;


    movePacket.length:= Ord(text[0])+1;  { +1 for terminator }
    movePacket.srcType:= EMS_MOVE_CONV;  { Conventional }
    movePacket.srcHandle:= 0;            { indicates real memory }
    movePacket.srcOffset:= Ofs(text);    { actual offset }
    movePacket.srcPage:= Seg(text);      { actual segment }
    movePacket.destType:= EMS_MOVE_EMS;  { EMS }
    movePacket.destHandle:= handle3.handle; { 3rd 1K block }
    movePacket.destOffset:= 800;         { A random offset into block }
    movePacket.destPage:= 0;             { page zero }

    if ems.moveMemRegion40(movePacket) then begin
        ems_demoError('EMS_Ems.moveMemRegion40');
        end;

    (*
     *  Now the fourth and last block:
     *)
    text:= '                             -- David Letterman';

    movePacket.length:= Ord(text[0])+1;  { +1 for terminator }
    movePacket.srcType:= EMS_MOVE_CONV;  { Conventional }
    movePacket.srcHandle:= 0;            { indicates real memory }
    movePacket.srcOffset:= Ofs(text);    { actual offset }
    movePacket.srcPage:= Seg(text);      { actual segment }
    movePacket.destType:= EMS_MOVE_EMS;  { EMS }
    movePacket.destHandle:= handle4.handle; { 4th 1K block }
    movePacket.destOffset:= 212;         { A random offset into block }
    movePacket.destPage:= 0;             { page zero }

    if ems.moveMemRegion40(movePacket) then begin
        ems_demoError('EMS_Ems.moveMemRegion40');
        end;

    (*
     *  Now we've copies four strings into EMS blocks.
     *      Block 1 at offset 42,
     *      Block 2 at offset 911,
     *      Block 3 at offset 800,
     *      Block 4 at offset 212.
     *
     *  Now let's retrieve them and print them out.
     *)
    movePacket.length:= 128;             { +1 for terminator }
    movePacket.srcType:= EMS_MOVE_EMS;   { EMS }
    movePacket.srcHandle:= handle1.handle;  { First handle }
    movePacket.srcOffset:= 42;           { A random offset into block }
    movePacket.srcPage:= 0;              { page zero }
    movePacket.destType:= EMS_MOVE_CONV; { Conventional memory }
    movePacket.destHandle:= 0;           { Conventional memory }
    movePacket.destOffset:= Ofs(buff);   { actual offset }
    movePacket.destPage:= Seg(buff);     { actual segment }

    if ems.moveMemRegion40(movePacket) then begin
        ems_demoError('EMS_Ems.moveMemRegion40');
        end;

    writeln(buff);

    (*
     *  Now pick up the second piece:
     *)
    movePacket.length:= 128;             { +1 for terminator }
    movePacket.srcType:= EMS_MOVE_EMS;   { EMS }
    movePacket.srcHandle:= handle2.handle;  { Second handle }
    movePacket.srcOffset:= 911;          { A random offset into block }
    movePacket.srcPage:= 0;              { page zero }
    movePacket.destType:= EMS_MOVE_CONV; { Conventional memory }
    movePacket.destHandle:= 0;           { Conventional memory }
    movePacket.destOffset:= Ofs(buff);   { actual offset }
    movePacket.destPage:= Seg(buff);     { actual segment }

    if ems.moveMemRegion40(movePacket) then begin
        ems_demoError('EMS_Ems.moveMemRegion40');
        end;

    writeln(buff);

    (*
     *  Now pick up the third piece:
     *)
    movePacket.length:= 128;             { +1 for terminator }
    movePacket.srcType:= EMS_MOVE_EMS;   { EMS }
    movePacket.srcHandle:= handle3.handle;  { Third handle }
    movePacket.srcOffset:= 800;          { A random offset into block }
    movePacket.srcPage:= 0;              { page zero }
    movePacket.destType:= EMS_MOVE_CONV; { Conventional memory }
    movePacket.destHandle:= 0;           { Conventional memory }
    movePacket.destOffset:= Ofs(buff);   { actual offset }
    movePacket.destPage:= Seg(buff);     { actual segment }

    if ems.moveMemRegion40(movePacket) then begin
        ems_demoError('EMS_Ems.moveMemRegion40');
        end;

    writeln(buff);

    (*
     *  Now pick up the fourth piece:
     *)
    movePacket.length:= 128;             { +1 for terminator }
    movePacket.srcType:= EMS_MOVE_EMS;   { EMS }
    movePacket.srcHandle:= handle4.handle;  { Fourth handle }
    movePacket.srcOffset:= 212;          { A random offset into block }
    movePacket.srcPage:= 0;              { page zero }
    movePacket.destType:= EMS_MOVE_CONV; { Conventional memory }
    movePacket.destHandle:= 0;           { Conventional memory }
    movePacket.destOffset:= Ofs(buff);   { actual offset }
    movePacket.destPage:= Seg(buff);     { actual segment }

    if ems.moveMemRegion40(movePacket) then begin
        ems_demoError('EMS_Ems.moveMemRegion40');
        end;

    writeln(buff);

    (*
     *  Free up the blocks of memory.
     *)
    if handle1.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;
    if handle2.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;
    if handle3.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;
    if handle4.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;

end.
