(* * * * * * * * * * * * * * * * * * *
 *
 * prog5-4.pas
 *
 *  Demonstrates use of move to/from eXtended Memory
 *
 * * * * * * * * * * * * * * * * * * *)


program prog5_4;


uses
    _Globals,
    _Xms;

var
    xms:            XMS_Xms;

    handle1:        XMS_XmBlk;
    handle2:        XMS_XmBlk;
    handle3:        XMS_XmBlk;
    handle4:        XMS_XmBlk;

    movePacket:     XMS_MovePacket;

    text:           String;
    buff:           String;


begin

    (*
     *  Attempt to initialize XMS. If there is an error, report it.
     *)
    if xms.init then begin
        writeln('XMS is not present');
        end;

    (*
     *  Now allocate 4 1K blocks:
     *)
    if handle1.allocXM(1) then begin
        xms_demoError('XMS_XmsBlk.allocXM');
        end;
    if handle2.allocXM(1) then begin
        xms_demoError('XMS_XmsBlk.allocXM');
        end;
    if handle3.allocXM(1) then begin
        xms_demoError('XMS_XmsBlk.allocXM');
        end;
    if handle4.allocXM(1) then begin
        xms_demoError('XMS_XmsBlk.allocXM');
        end;

    (*
     *  Now let's copy some text into various places in the
     *  XM blocks, and then copy it back and print it out.
     *)

    (*
     *  Set up a pointer to a string:
     *)
    text:= 'Fourscore and seven years ago our fathers brought forth ';

    (*
     *  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:= even(Ord(text[0])+1);{ +1 for terminator            }
    movePacket.srcHandle:= 0;               { indicates real memory         }
    movePacket.srcOffset:= DWord(@Text);    { actual segment:offset         }
    movePacket.destHandle:= handle1.handle; { 1st 1K block                  }
    movePacket.destOffset:= 42;             { A random offset into block    }

    (*
     *  Do the actual XMS call:
     *)
    if xms.moveXM(movePacket) then begin
        xms_demoError('XMS_Xms.moveXM');
        end;

    (*
     *  Now put the next string into the next XM block:
     *)
    text:= 'on this continent a'#13#10'new nation, ';

    movePacket.length:= even(Ord(text[0])+1);{ +1 for terminator            }
    movePacket.srcHandle:= 0;               { indicates real memory         }
    movePacket.srcOffset:= DWord(@Text);    { actual segment:offset         }
    movePacket.destHandle:= handle2.handle; { 2nd 1K block                  }
    movePacket.destOffset:= 911;            { A random offset into block    }

    if xms.moveXM(movePacket) then begin
        xms_demoError('XMS_Xms.moveXM');
        end;

    (*
     *  And something for the the third block:
     *)
    text:= 'conceived in liberty and dedicated to the proposition ';

    movePacket.length:= even(Ord(text[0])+1);{ +1 for terminator           }
    movePacket.srcHandle:= 0;               { indicates real memory        }
    movePacket.srcOffset:= DWord(@Text);    { actual segment:offset        }
    movePacket.destHandle:= handle3.handle; { 3rd 1K block                 }
    movePacket.destOffset:= 800;            { A random offset into block   }

    if xms.moveXM(movePacket) then begin
        xms_demoError('XMS_Xms.moveXM');
        end;

    (*
     *  Now the fourth and last block:
     *)
    text:= 'that all'#13#10'men are created equal.'#13#10'';

    movePacket.length:= even(Ord(text[0])+1);{ +1 for terminator           }
    movePacket.srcHandle:= 0;               { indicates real memory        }
    movePacket.srcOffset:= DWord(@Text);    { actual segment:offset        }
    movePacket.destHandle:= handle4.handle; { 4th 1K block                 }
    movePacket.destOffset:= 212;            { A random offset into block   }

    if xms.moveXM(movePacket) then begin
        xms_demoError('XMS_Xms.moveXM');
        end;

    (*
     *  Now we've copies four strings into XM 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:= 96;                   { Length of buffer             }
    movePacket.srcHandle:= handle1.handle;    { 1st block                    }
    movePacket.srcOffset:= 42;                { offset                       }
    movePacket.destHandle:= 0;                { real memory                  }
    movePacket.destOffset:= DWord(@buff);     { real address                 }

    if xms.moveXM(movePacket) then begin
        xms_demoError('XMS_Xms.moveXM');
        end;

    write(buff);

    (*
     *  Now pick up the second piece:
     *)
    movePacket.length:= 96;                   { Length of buffer             }
    movePacket.srcHandle:= handle2.handle;    { 2nd block                    }
    movePacket.srcOffset:= 911;               { offset                       }
    movePacket.destHandle:= 0;                { real memory                  }
    movePacket.destOffset:= DWord(@buff);     { real address                 }

    if xms.moveXM(movePacket) then begin
        xms_demoError('XMS_Xms.moveXM');
        end;

    write(buff);

    (*
     *  Now pick up the third piece:
     *)
    movePacket.length:= 96;                   { Length of buffer             }
    movePacket.srcHandle:= handle3.handle;    { 3rd block                    }
    movePacket.srcOffset:= 800;               { offset                       }
    movePacket.destHandle:= 0;                { real memory                  }
    movePacket.destOffset:= DWord(@buff);     { real address                 }

    if xms.moveXM(movePacket) then begin
        xms_demoError('XMS_Xms.moveXM');
        end;

    write(buff);

    (*
     *  Now pick up the fourth piece:
     *)
    movePacket.length:= 96;                   { Length of buffer             }
    movePacket.srcHandle:= handle4.handle;    { 4th block                    }
    movePacket.srcOffset:= 212;               { offset                       }
    movePacket.destHandle:= 0;                { real memory                  }
    movePacket.destOffset:= DWord(@buff);     { real address                 }

    if xms.moveXM(movePacket) then begin
        xms_demoError('XMS_Xms.moveXM');
        end;

    write(buff);

    (*
     *  Finally, free up the blocks:
     *)
    if handle1.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;
    if handle2.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;
    if handle3.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;
    if handle4.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;

end.
