[comp.sys.atari.st] Memory Access in Modula 2

terrell@druhi.ATT.COM (Eric R. Terrell) (06/29/88)

Reading and writing from/to an absolute memory address in Modula 2:

I know of a "dirty trick" to accomplish this in Pascal, which will probably work
with Modula 2 as well.  It's based on the fact that variant records
allow Pascal's normally strong typing to be circumvented.

Declare a variant record with a long (4 byte) integer field and a pointer to a
character field in the variant part).  Recall that fields in the variant part
are placed in the same region of memory.

Then to peek from memory, set the long integer field to the desired address,
and get the desired value by dereferencing the pointer field.

Similarly, to poke a value into memory, set the long integer field to the
desired address, and assign the value that you want to poke to the 
dereferenced pointer.

For example:



program peek_poke(input, output);

type
  mem_rec = record

              case tag : boolean of

                true:  (addr : integer);
                false: (ptr  : ^char);

              end; 

var
  dirtytrick : mem_rec;
  value      : char;

begin

  (* Poke value 128 into address 12345. *)
  dirtytrick.addr := 12345;
  dirtytrick.ptr^ := 128;

  (* Fetch the value from address 12345. *)
  dirtytrick.addr := 12345;
  value := dirtytrick.ptr^;

end.


Note that the addr field of the variant record must be a 4 byte integer.

Of course, code written with this trick will tend to be non-portable, so if
you can dig up a library routine that accomplishes this, that would be
preferable.

dew@esl.UUCP (Douglas Wood) (07/02/88)

In article <3188@druhi.ATT.COM> terrell@druhi.UUCP (TerrellE) writes:
>Reading and writing from/to an absolute memory address in Modula 2:
>I know of a "dirty trick" to accomplish this in Pascal, which will probably work
>with Modula 2 as well.  It's based on the fact that variant records
>allow Pascal's normally strong typing to be circumvented.
...
<delete more comments and small program>

This is good, but there is one additional piece of information needed.
The original poster asked to access location $4ab.  One needs to be
in supervisor mode to do that.  In TDI Modula-2, one can not pass
parameters to a superexec'ed procedures, they must be global to the
procedure.  But, essentially, all that is needed is to peek and poke
while in supervisor mode with a structure essentially as written by terrel.

ames!esl!dew

michael@pbinfo.uucp (Michael Schmidt) (07/07/88)

In article <3188@druhi.ATT.COM>, terrell@druhi (Eric R. Terrell) writes:
>Reading and writing from/to an absolute memory address in Modula 2:
>

Well, in PIM2, 3rd Edition (in german, the only I have at hand at
the   moment)  Wirth  describes in   chapter   31 about   "device
processes, concurrency and  interrupts" (translated from  german,
may vary in english) a possibility to give the absolute address
of a variable on a pdp-11 by saying:

VAR s[777560B] : BITSET;
    x[777562B] : CHAR;

Try, whether your compiler groks it. (I doubt it.)

	Michael Schmidt