(* * * * * * * * * * * * * * * * * * *
 *
 * prog5-6.pas
 *
 *  Demonstrates use of move to/from eXtended Memory using raw move.
 *
 * * * * * * * * * * * * * * * * * * *)


program prog5_6;


uses
    _Globals,
    _Xms;

var
    xms:            XMS_Xms;

    handle1:        XMS_XmBlk;
    handle2:        XMS_XmBlk;
    handle3:        XMS_XmBlk;
    handle4:        XMS_XmBlk;

    physAddr1:      DWord;
    physAddr2:      DWord;
    physAddr3:      DWord;
    physAddr4:      DWord;

    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 lock the blocks, getting their physical addresses
     *  so we can do some raw moves to and from them.
     *)
    if handle1.lockXM(physAddr1) then begin
        xms_demoError('XMS_XmBlk.lockXM');
        end;
    if handle2.lockXM(physAddr2) then begin
        xms_demoError('XMS_XmBlk.lockXM');
        end;
    if handle3.lockXM(physAddr3) then begin
        xms_demoError('XMS_XmBlk.lockXM');
        end;
    if handle4.lockXM(physAddr4) then begin
        xms_demoError('XMS_XmBlk.lockXM');
        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 ';

    (*
     *  Do the raw move. Note that the length must be an
     *  even number of bytes.
     *)
    if xms.rawMove(physAddr1+42,
                    segOffToPhys(@text),
                    even(Ord(text[0])+1)) then begin
        xms_demoError('XMS_Xms.rawMove');
        end;

    (*
     *  Now put the next string into the next XM block:
     *)
    text:= 'on this continent a'#13#10'new nation, ';
    if xms.rawMove(physAddr1+911,
                    segOffToPhys(@text),
                    even(Ord(text[0])+1)) then begin
        xms_demoError('XMS_Xms.rawMove');
        end;

    (*
     *  And something for the the third block:
     *)
    text:= 'conceived in liberty and dedicated to the proposition ';
    if xms.rawMove(physAddr1+800,
                    segOffToPhys(@text),
                    even(Ord(text[0])+1)) then begin
        xms_demoError('XMS_Xms.rawMove');
        end;

    (*
     *  Now the fourth and last block:
     *)    
    text:= 'that all'#13#10'men are created equal.'#13#10;
    if xms.rawMove(physAddr1+212,
                    segOffToPhys(@text),
                    even(Ord(text[0])+1)) then begin
        xms_demoError('XMS_Xms.rawMove');
        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.
     *)
    if xms.rawMove(segOffToPhys(@buff),
                    physAddr1+42,
                    256) then begin
        xms_demoError('XMS_Xms.rawMove');
        end;

    write(buff);


    (*
     *  Now pick up the second piece:
     *)
    if xms.rawMove(segOffToPhys(@buff),
                    physAddr1+911,
                    256) then begin
        xms_demoError('XMS_Xms.rawMove');
        end;

    write(buff);


    (*
     *  Now pick up the third piece:
     *)
    if xms.rawMove(segOffToPhys(@buff),
                    physAddr1+800,
                    256) then begin
        xms_demoError('XMS_Xms.rawMove');
        end;

    write(buff);


    (*
     *  Now pick up the fourth piece:
     *)
    if xms.rawMove(segOffToPhys(@buff),
                    physAddr1+212,
                    256) then begin
        xms_demoError('XMS_Xms.rawMove');
        end;

    write(buff);


    (*
     *  Now unlock the blocks.
     *)
    if handle1.unlockXM then begin
        xms_demoError('XMS_XmBlk.unlockXM');
        end;
    if handle2.unlockXM then begin
        xms_demoError('XMS_XmBlk.unlockXM');
        end;
    if handle3.unlockXM then begin
        xms_demoError('XMS_XmBlk.unlockXM');
        end;
    if handle4.unlockXM then begin
        xms_demoError('XMS_XmBlk.unlockXM');
        end;

    (*
     *  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.
