(* * * * * * * * * * * * * * * * * * *
 *
 * prog6-2.pas --     VM exerciser
 *
 *      This test program allocates two big blocks of virtual memory
 *      and runs through them twice, first initializing them and then
 *      reading them to make sure they were correctly initialized.
 *
 * * * * * * * * * * * * * * * * * * *)

program prog6_2;

uses    _globals,
        _vm;

label
    100;

type
    sixtyFourKB=        array[0..65519] of Byte;
    sixtyFourKBPtr=     ^sixtyFourKB;

var
    vm:                 VM_Vm;
    handle1:            VM_VmBlk;
    handle2:            VM_VmBlk;

    addr:               sixtyFourKBPtr;

    i:                  DWord;
    j:                  DWord;

begin

    addr:= Nil;

    if vm.init(245760) then begin
        goto 100;
        end;

    if handle1.alloc(2113536) then begin
        goto 100;
        end;
    if handle2.alloc(2113536) then begin
        goto 100;
        end;

    for i:= 0 to 31 do begin
        (*
         *  Wire a chunk of the first block and initialize it.
         *)
        if handle1.wire(i*65536, 65536, Pointer(addr)) then begin
            goto 100;
            end;
        writeln('--> ', i:2, ': wire #1, ',
                hex(Seg(addr^), 4), ':', hex(Ofs(addr^), 4));

        j:= 0;
        while j < 65534 do begin
            addr^[1]:= 0;
            addr^[16385]:= 1;
            addr^[32769]:= 2;
            addr^[49153]:= 3;
            if addr^[j] <> 0 then begin
                writeln('Error: handle1, i = ', i,
                        ', addr^[j] = ', addr^[j], ', j = ', j);
                end;
            addr^[j]:= i;

            j:= j + 8192;
            end;
        if handle1.unwire(i*65536, 65536, True) then begin
            goto 100;
            end;
        writeln('--> ', i:2, ': unwire #1');

        (*
         *  Wire a chunk of the second block and initialize it.
         *)
        if handle2.wire(i*65536, 65536, Pointer(addr)) then begin
            goto 100;
            end;
        writeln('--> ', i:2, ': wire #2, ',
                hex(Seg(addr^), 4), ':', hex(Ofs(addr^), 4));

        j:= 0;
        while j < 65534 do begin
            addr^[1]:= 0;
            addr^[16385]:= 1;
            addr^[32769]:= 2;
            addr^[49153]:= 3;
            if addr^[j] <> 0 then begin
                writeln('Error: handle2, i = ', i,
                        ', addr^[j] = ', addr^[j], ', j = ', j);
                end;
            addr^[j]:= i + 100;

            j:= j + 8192;
            end;
        if handle2.unwire(i*65536, 65536, True) then begin
            goto 100;
            end;
        writeln('--> ', i:2, ': unwire #2');
        end;

    for i:= 0 to 31 do begin
        (*
         *  Wire a chunk of the first block and make sure we get
         *  the values we expect.
         *)
        if handle1.wire(i*65536, 65536, Pointer(addr)) then begin
            goto 100;
            end;
        writeln('<-- ', i:2, ': wire #1, ',
                        hex(Seg(addr^), 4), ':', hex(Ofs(addr^), 4));
        writeln(
            addr^[1],
            addr^[16385],
            addr^[32769],
            addr^[49153]);
        j:= 0;
        while j < 65534 do begin
            if addr^[j] <> i then begin
                writeln('Error: handle1, i = ', i,
                        ', addr^[j] = ', addr^[j],
                        ', j = ', j);
                end;

            j:= j + 8192;
            end;
        if handle1.unwire(i*65536, 65536, False) then begin
            goto 100;
            end;
        writeln('<-- ', i:2, ': unwire #1');

        (*
         *  Now validate a chunk of the second block.
         *)
        if handle2.wire(i*65536, 65536, Pointer(addr)) then begin
            goto 100;
            end;
        writeln('<-- ', i:2, ': wire #2, ',
                        hex(Seg(addr^), 4), ':', hex(Ofs(addr^), 4));
        writeln(
            addr^[1],
            addr^[16385],
            addr^[32769],
            addr^[49153]);
        j:= 0;
        while j < 65534 do begin
            if addr^[j] <> (i + 100) then begin
                writeln('Error: handle2, i = ', i,
                        ', addr^[j] = ', addr^[j],
                        ', j = ', j);
                end;

            j:= j + 8192;
            end;
        if handle2.unwire(i*65536, 65536, False) then begin
            goto 100;
            end;
        writeln('<-- ', i:2, ': unwire #2');

        end;


    if handle1.free then
        ;
    
    if handle2.free then
        ;


    if vm.shutdown then
        ;

    Halt;
100:
    writeln('Died: Error #',  errno);
end.

