phys_memory.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2011-2012 Zeex
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the "Software"),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the
  8. // Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #if defined PHYS_MEMORY_INC
  21. #endinput
  22. #endif
  23. #define PHYS_MEMORY_INC
  24. #include <core>
  25. #include "amx_base"
  26. static stock GetDat() {
  27. #emit lctrl 1
  28. #emit retn
  29. return 0; // make compiler happy
  30. }
  31. static stock AbsToRel(address) {
  32. return address - (GetAmxBaseAddress() + GetDat());
  33. }
  34. static stock RelToAbs(address) {
  35. return address + (GetAmxBaseAddress() + GetDat());
  36. }
  37. // Returns the absolute address of a variable/array.
  38. stock refabs(...) {
  39. assert(numargs() == 1);
  40. new address;
  41. #emit load.s.pri 12
  42. #emit stor.s.pri address
  43. return RelToAbs(address);
  44. }
  45. stock ReadPhysMemory(address, dest[], num = sizeof(dest)) {
  46. new rel_addr = AbsToRel(address);
  47. // Current destination cell address.
  48. new cur_dest;
  49. #emit load.s.pri dest
  50. #emit stor.s.pri cur_dest
  51. // Currently reading address.
  52. new cur_addr = rel_addr;
  53. // Read num cells to dest.
  54. for (new i = 0; i < num; i++, cur_addr += 4, cur_dest += 4) {
  55. #emit lref.s.pri cur_addr
  56. #emit sref.s.pri cur_dest
  57. }
  58. #emit stack 12
  59. #emit retn
  60. return 0; // make compiler happy
  61. }
  62. stock WritePhysMemory(address, src[], num = sizeof(src)) {
  63. new rel_addr = AbsToRel(address);
  64. // Current destination cell address..
  65. new cur_src;
  66. #emit load.s.pri src
  67. #emit stor.s.pri cur_src
  68. // Currently reading address.
  69. new cur_addr = rel_addr;
  70. // Write num cells from src.
  71. for (new i = 0; i < num; i++, cur_addr += 4, cur_src += 4) {
  72. #emit lref.s.pri cur_src
  73. #emit sref.s.pri cur_addr
  74. }
  75. #emit stack 12
  76. #emit retn
  77. return 0; // make compiler happy
  78. }
  79. stock ReadPhysMemoryCell(address) {
  80. new rel_addr = AbsToRel(address);
  81. #emit lref.s.pri rel_addr
  82. #emit stack 4
  83. #emit retn
  84. return 0; // make compiler happy
  85. }
  86. stock WritePhysMemoryCell(address, what) {
  87. new rel_addr = AbsToRel(address);
  88. #emit load.s.pri what
  89. #emit sref.s.pri rel_addr
  90. #emit stack 4
  91. #emit retn
  92. return 0; // make compiler happy
  93. }