(* * * * * * * * * * * * * * * * * * *
 *
 * prog5-7.pas
 *
 *  Demonstrates use of move to/from Upper Memory
 *
 * * * * * * * * * * * * * * * * * * *)


program prog5_7;


uses
    _Globals,
    _Xms;

var
    xms:            XMS_Xms;

    movePacket:     XMS_MovePacket;

    text:           String;
    block1:        ^String;
    block2:        ^String;
    block3:        ^String;
    block4:        ^String;
    block5:        ^String;

    actualSize:     Word;


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 5 128 byte blocks of Upper Memory
     *)
    if xms.allocUM(8, Pointer(block1), actualSize) then begin
        writeln('    Biggest block available was ',
                    actualSize * 16, ' bytes');
        xms_demoError('XMS_Xms.allocUM');
        end;
    if xms.allocUM(8, Pointer(block2), actualSize) then begin
        writeln('    Biggest block available was ',
                    actualSize * 16, ' bytes');
        xms_demoError('XMS_Xms.allocUM');
        end;
    if xms.allocUM(8, Pointer(block3), actualSize) then begin
        writeln('    Biggest block available was ',
                    actualSize * 16, ' bytes');
        xms_demoError('XMS_Xms.allocUM');
        end;
    if xms.allocUM(8, Pointer(block4), actualSize) then begin
        writeln('    Biggest block available was ',
                    actualSize * 16, ' bytes');
        xms_demoError('XMS_Xms.allocUM');
        end;
    if xms.allocUM(8, Pointer(block5), actualSize) then begin
        writeln('    Biggest block available was ',
                    actualSize * 16, ' bytes');
        xms_demoError('XMS_Xms.allocUM');
        end;

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

    (*
     *  Copy the text:
     *)
    block1^:=
'We the people of the United States, in order to form a more perfect Union,'#13#10;


    (*
     *  Now put the next string into the next UM block:
     *)
    block2^:=
'establish justice, insure domestic tranquility, provide for the common'#13#10;

    (*
     *  And something for the the third block:
     *)
    block3^:=
'defense, promote the general welfare, and secure the blessings of liberty'#13#10;

    (*
     *  Now the fourth block;
     *)    
    block4^:=
'to ourselves and our posterity do ordain and establish this Constitution'#13#10;

    (*
     *  And the fifth and final block:
     *)
    block5^:= 'for the United States of America.'#13#10;

    (*
     *  Now we've copied five strings into UM blocks.
     *
     *  Let's retrieve them and print them out.
     *)
    write(block1^);
    write(block2^);
    write(block3^);
    write(block4^);
    write(block5^);

    (*
     *  Free up the blocks of memory.
     *)
    if xms.freeUM(block1) then begin
        xms_demoError('XMS_Xms.freeUM');
        end;
    if xms.freeUM(block2) then begin
        xms_demoError('XMS_Xms.freeUM');
        end;
    if xms.freeUM(block3) then begin
        xms_demoError('XMS_Xms.freeUM');
        end;
    if xms.freeUM(block4) then begin
        xms_demoError('XMS_Xms.freeUM');
        end;
    if xms.freeUM(block5) then begin
        xms_demoError('XMS_Xms.freeUM');
        end;

end.
