home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / oberon / loader / peeknpoke.mod < prev    next >
Text File  |  1977-12-31  |  3KB  |  116 lines

  1. IMPLEMENTATION MODULE PeekNPoke;
  2. (*
  3.  The routines of this module allow to read/build a data structure in a  machine
  4.  independent way. Instead of aliasing datatypes to memory areas, the structure
  5.  is read/built by copying individual bytes from/to the correct place.
  6.  
  7.  In all routines, base is a pointer (anchor) to the memory block you want to
  8.  manipulate and offset an address relativ to this base.
  9.  
  10.  Word is used to denominate a 2 byte entity, long to denominate a 4 byte entity.
  11. *)
  12.  
  13. FROM SYSTEM IMPORT ADDRESS;
  14.  
  15. PROCEDURE PutByte(base:ADDRESS; offset:LONGINT; value:SHORTCARD);
  16. (* Write a single byte *)
  17. VAR
  18.  tmpPtr:POINTER TO SHORTCARD;
  19. BEGIN
  20.  tmpPtr:=ADDRESS(base+offset); tmpPtr^:=value;
  21. END PutByte;
  22.  
  23. PROCEDURE PutWordB(base:ADDRESS; offset:LONGINT; value:CARDINAL);
  24. (*
  25.  Write a word in big endian fashion, i.e. most significant byte
  26.  at lowest memory address.
  27. *)
  28. BEGIN
  29.  PutByte(base,offset,value DIV 100H); PutByte(base,offset+1,value MOD 100H);
  30. END PutWordB;
  31.  
  32. PROCEDURE PutWordL(base:ADDRESS; offset:LONGINT; value:CARDINAL);
  33. (*
  34.  Write a word in little endian fashion, i.e. most significant byte
  35.  at highest memory address.
  36. *)
  37. BEGIN
  38.  PutByte(base,offset+1,value DIV 100H); PutByte(base,offset,value MOD 100H);
  39. END PutWordL;
  40.  
  41. PROCEDURE PutLongB(base:ADDRESS; offset:LONGINT; value:LONGCARD);
  42. (*
  43.  Write a long word in big endian fashion, i.e. most significant byte
  44.  at lowest memory address.
  45. *)
  46. BEGIN
  47.  PutWordB(base,offset,value DIV 10000H); PutWordB(base,offset+2,value MOD 10000H);
  48. END PutLongB;
  49.  
  50. PROCEDURE PutLongL(base:ADDRESS; offset:LONGINT; value:LONGCARD);
  51. (*
  52.  Write a long word in little endian fashion, i.e. most significant byte
  53.  at highest memory address.
  54. *)
  55. BEGIN
  56.  PutWordL(base,offset+2,value DIV 10000H); PutWordL(base,offset,value MOD 10000H);
  57. END PutLongL;
  58.  
  59. PROCEDURE GetByte(base:ADDRESS; offset:LONGINT):SHORTCARD;
  60. (* Read a byte *)
  61. VAR
  62.  tmpPtr:POINTER TO SHORTCARD;
  63. BEGIN
  64.  tmpPtr:=ADDRESS(base+offset); RETURN tmpPtr^;
  65. END GetByte;
  66.  
  67. PROCEDURE GetWordB(base:ADDRESS; offset:LONGINT):CARDINAL;
  68. (*
  69.  Read a word in big endian fashion, i.e. the most significant
  70.  byte from the lowest memory address.
  71. *)
  72. VAR
  73.  tmp1,tmp2:SHORTCARD;
  74. BEGIN
  75.  tmp1:=GetByte(base,offset); tmp2:=GetByte(base,offset+1);
  76.  RETURN CARDINAL(tmp1)*100H+CARDINAL(tmp2);
  77. END GetWordB;
  78.  
  79. PROCEDURE GetWordL(base:ADDRESS; offset:LONGINT):CARDINAL;
  80. (*
  81.  Read a word in little endian fashion, i.e. the most significant
  82.  byte from the highest memory address.
  83. *)
  84. VAR
  85.  tmp1,tmp2:SHORTCARD;
  86. BEGIN
  87.  tmp1:=GetByte(base,offset); tmp2:=GetByte(base,offset+1);
  88.  RETURN CARDINAL(tmp2)*100H+CARDINAL(tmp1);
  89. END GetWordL;
  90.  
  91. PROCEDURE GetLongB(base:ADDRESS; offset:LONGINT):LONGCARD;
  92. (*
  93.  Read a long word in big endian fashion, i.e. the most significant
  94.  byte from the lowest memory address.
  95. *)
  96. VAR
  97.  tmp1,tmp2:CARDINAL;
  98. BEGIN
  99.  tmp1:=GetWordB(base,offset); tmp2:=GetWordB(base,offset+2);
  100.  RETURN LONGCARD(tmp1)*10000H+LONGCARD(tmp2);
  101. END GetLongB;
  102.  
  103. PROCEDURE GetLongL(base:ADDRESS; offset:LONGINT):LONGCARD;
  104. (*
  105.  Read a long word in little endian fashion, i.e. the most significant
  106.  byte from the highest memory address.
  107. *)
  108. VAR
  109.  tmp1,tmp2:CARDINAL;
  110. BEGIN
  111.  tmp1:=GetWordL(base,offset); tmp2:=GetWordL(base,offset+2);
  112.  RETURN LONGCARD(tmp2)*10000H+LONGCARD(tmp1);
  113. END GetLongL;
  114.  
  115. END PeekNPoke.
  116.