(* * * * * * * * * * * * * * * * * * *
 *
 * prog5-5.pas
 *
 *  Demonstrates use of the HMA
 *
 * * * * * * * * * * * * * * * * * * *)


program prog5_5;


uses
    _Globals,
    _Xms;

var
    xms:            XMS_Xms;
    text:           ^String;
    a20State:       Word;

    (*
     *  Set up the HMA. It's address is $FFFF:$10.
     *  Note that ($FFFF shl 4) + $10 = 0xFFFF0 + 0x10 = 0x100000.
     *)
    hma:            array[0..$FFEF] of char absolute $FFFF:$10;


begin

    (*
     *  Attempt to initialize XMS. If there is an error, report it.
     *)
    if xms.init then begin
        writeln('XMS is not present');
        end;


    (*
     *  Now try to allocate the HMA
     *)
    if xms.allocHMA($FFFF) then begin
        xms_demoError('XMS_Xms.allocHMA');
        end;

    (*
     *  Now that we've got the HMA, we need to enable the A20
     *  line so that addresses won't wrap.
     *)
    if xms.globEnabA20 then begin
        xms_demoError('XMS_Xms.globEnabA20');
        end;
   
    (*
     *  Now let's copy some text into various places in the
     *  HMA, and then copy it back and print it out.
     *)
    text:= @hma[0];
    text^:= 'Oh say can you see by the dawn''s early light'#13#10;
    StrPtr(@hma[100])^:= '  What so proudly we hailed at the twilight''s last gleaming ?'#13#10;
    StrPtr(@hma[200])^:= 'Whose broad stripes and bright stars ';
    StrPtr(@hma[300])^:= 'through the perilous fight'#13#10;
    StrPtr(@hma[400])^:= '  O''er the ramparts we watched were so gallantly streaming ?'#13#10;

    (*
     *  Query and print out the A20 state (we know it's enabled):
     *)
    if xms.getA20State(a20State) then begin
        xms_demoError('XMS_Xms.getA20State');
        end;

    write('ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍA20Í');
    if a20State <> 0 then begin
        writeln('EnabledÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ');
        end
    else begin
        writeln('DisabledÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ');
        end;

    (*
     *  Now print the stuff right out of the HMA
     *)
    write(StrPtr(@hma[0])^);
    write(StrPtr(@hma[100])^);
    write(StrPtr(@hma[200])^);
    write(StrPtr(@hma[300])^);
    write(StrPtr(@hma[400])^);

    (*
     *  Disable the A20 line.
     *)
    if xms.globDisabA20 then begin
        xms_demoError('XMS_Xms.globDisabA20');
        end;

    (*
     *  Now note the garbage the same code will produce without
     *  the A20 line enabled. This will be looking at addresses
     *  0:0, 0:100, etc. instead of 10000:0, 10000:100, etc.
     *)

    if xms.getA20State(a20State) then begin
        xms_demoError('XMS_Xms.getA20State');
        end;

    write('ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍA20Í');
    if a20State <> 0 then begin
        writeln('EnabledÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ');
        end
    else begin
        writeln('DisabledÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ');
        end;

    (*
     *  Now print the stuff right out of the HMA
     *)
    write(StrPtr(@hma[0])^);
    write(StrPtr(@hma[100])^);
    write(StrPtr(@hma[200])^);
    write(StrPtr(@hma[300])^);
    write(StrPtr(@hma[400])^);

    (*
     *  Free up the HMA
     *)
    if xms.freeHMA then begin
        xms_demoError('XMS_Xms.freeHMA');
        end;

end.
