Minos.EnetEnvironment.Mod 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. MODULE EnetEnvironment;
  2. (**
  3. AUTHOR: Alexey Morozov, HighDim GmbH, 2015
  4. PURPOSE: Ethernet networking stack, MINOS - specific environment
  5. *)
  6. IMPORT
  7. Board, Interrupts, GlobalTimer, Caches, UncachedHeap, Trace;
  8. CONST
  9. MaxHandlers = 32;
  10. TYPE
  11. Time* = HUGEINT; (* Time type used in EnetTiming *)
  12. (* The prototype of an interrupt handler *)
  13. InterruptHandler* = PROCEDURE();
  14. (** Uncached memory descriptor *)
  15. UncachedMemDesc* = RECORD
  16. addr-: ADDRESS; (** starting address of the uncached memory available to the user *)
  17. length-: SIZE; (** length of the uncached memory region available to the user; can be greater than the value passed to AllocateUncachedMemory *)
  18. END;
  19. VAR
  20. TraceString-: PROCEDURE(CONST str: ARRAY OF CHAR);
  21. GetTimeCounter-: PROCEDURE(): Time;
  22. InvalidateDCacheRange-: PROCEDURE(addr: ADDRESS; length: SIZE);
  23. FlushDCacheRange-: PROCEDURE(addr: ADDRESS; length: SIZE);
  24. handlers: ARRAY MaxHandlers OF RECORD handle: PROCEDURE; irq: LONGINT END;
  25. (**
  26. Install an interrupt handler
  27. *)
  28. PROCEDURE InstallInterruptHandler*(interruptHandler: InterruptHandler; interruptNumber: LONGINT);
  29. VAR
  30. i: LONGINT;
  31. BEGIN
  32. i := 0;
  33. WHILE (i < MaxHandlers) & (handlers[i].handle # NIL) DO INC(i) END;
  34. handlers[i].handle := interruptHandler;
  35. handlers[i].irq := interruptNumber;
  36. Interrupts.InstallHandler(HandleInterrupt,interruptNumber);
  37. END InstallInterruptHandler;
  38. (**
  39. Allocate length bytes of uncached memory
  40. *)
  41. PROCEDURE AllocateUncachedMemory*(length: SIZE; VAR memDesc: UncachedMemDesc);
  42. BEGIN
  43. ASSERT(memDesc.addr = NIL);
  44. memDesc.addr := UncachedHeap.New(length);
  45. memDesc.length := length;
  46. END AllocateUncachedMemory;
  47. (**
  48. Dispose uncached memory
  49. *)
  50. PROCEDURE DisposeUncachedMemory*(VAR memDesc: UncachedMemDesc);
  51. BEGIN
  52. ASSERT(memDesc.addr # NIL);
  53. memDesc.addr := NIL;
  54. memDesc.length := 0;
  55. END DisposeUncachedMemory;
  56. (** Convert microseconds to time counts *)
  57. PROCEDURE FromMicro*(us: Time): Time;
  58. BEGIN
  59. RETURN us * ENTIERH(0.5D0+Board.CpuClockHz/2.0D6);
  60. END FromMicro;
  61. (** Convert time counts to microseconds *)
  62. PROCEDURE ToMicro*(time: Time): Time;
  63. BEGIN
  64. RETURN ENTIERH((0.5D0 + time) / (LONGREAL(Board.CpuClockHz)) * 2.0D6)
  65. END ToMicro;
  66. (** Convert milliseconds to time counts *)
  67. PROCEDURE FromMilli*(ms: Time): Time;
  68. BEGIN
  69. RETURN ms * ENTIERH(0.5D0+Board.CpuClockHz/2.0D3);
  70. END FromMilli;
  71. (** Convert time counts to milliseconds *)
  72. PROCEDURE ToMilli*(time: Time): Time;
  73. BEGIN
  74. RETURN ENTIERH((0.5D0 + time) / (LONGREAL(Board.CpuClockHz)) * 2.0D3)
  75. END ToMilli;
  76. (** Delegating interrupt handler *)
  77. PROCEDURE HandleInterrupt * (irq: LONGINT);
  78. VAR
  79. i: LONGINT;
  80. handle: PROCEDURE;
  81. BEGIN
  82. FOR i := 0 TO MaxHandlers - 1 DO
  83. handle := handlers[i].handle;
  84. IF (handle # NIL) & (handlers[i].irq = irq) THEN handle END
  85. END
  86. END HandleInterrupt;
  87. BEGIN
  88. TraceString := Trace.String;
  89. GetTimeCounter := GlobalTimer.GetTime;
  90. InvalidateDCacheRange := Caches.InvalidateDCacheRange;
  91. FlushDCacheRange := Caches.CleanDCacheRange;
  92. GlobalTimer.EnableTimer
  93. END EnetEnvironment.