Subversion Repositories Kolibri OS

Rev

Rev 7107 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. (*
  2.     BSD 2-Clause License
  3.  
  4.     Copyright (c) 2019, Anton Krotov
  5.     All rights reserved.
  6. *)
  7.  
  8. MODULE API;
  9.  
  10. IMPORT SYSTEM;
  11.  
  12.  
  13. CONST
  14.  
  15.     BASE_ADR = 08048000H;
  16.  
  17.  
  18. TYPE
  19.  
  20.     TP* = ARRAY 2 OF INTEGER;
  21.  
  22.  
  23. VAR
  24.  
  25.     eol*:  ARRAY 2 OF CHAR;
  26.     base*, MainParam*: INTEGER;
  27.  
  28.     libc*, librt*: INTEGER;
  29.  
  30.     dlopen*        : PROCEDURE [linux] (filename, flag: INTEGER): INTEGER;
  31.     dlsym*         : PROCEDURE [linux] (handle, symbol: INTEGER): INTEGER;
  32.  
  33.     stdout*,
  34.     stdin*,
  35.     stderr*        : INTEGER;
  36.  
  37.     malloc*        : PROCEDURE [linux] (size: INTEGER): INTEGER;
  38.     free*          : PROCEDURE [linux] (ptr: INTEGER);
  39.     _exit*         : PROCEDURE [linux] (code: INTEGER);
  40.     puts*          : PROCEDURE [linux] (pStr: INTEGER);
  41.     fwrite*,
  42.     fread*         : PROCEDURE [linux] (buffer, bytes, blocks, file: INTEGER): INTEGER;
  43.     fopen*         : PROCEDURE [linux] (fname, fmode: INTEGER): INTEGER;
  44.     fclose*        : PROCEDURE [linux] (file: INTEGER): INTEGER;
  45.  
  46.     clock_gettime* : PROCEDURE [linux] (clock_id: INTEGER; VAR tp: TP): INTEGER;
  47.     time*          : PROCEDURE [linux] (ptr: INTEGER): INTEGER;
  48.  
  49.  
  50. PROCEDURE putc* (c: CHAR);
  51. VAR
  52.     res: INTEGER;
  53.  
  54. BEGIN
  55.     res := fwrite(SYSTEM.ADR(c), 1, 1, stdout)
  56. END putc;
  57.  
  58.  
  59. PROCEDURE DebugMsg* (lpText, lpCaption: INTEGER);
  60. BEGIN
  61.     puts(lpCaption);
  62.     puts(lpText)
  63. END DebugMsg;
  64.  
  65.  
  66. PROCEDURE _NEW* (size: INTEGER): INTEGER;
  67. VAR
  68.     res, ptr, words: INTEGER;
  69.  
  70. BEGIN
  71.     res := malloc(size);
  72.     IF res # 0 THEN
  73.         ptr := res;
  74.         words := size DIV SYSTEM.SIZE(INTEGER);
  75.         WHILE words > 0 DO
  76.             SYSTEM.PUT(ptr, 0);
  77.             INC(ptr, SYSTEM.SIZE(INTEGER));
  78.             DEC(words)
  79.         END
  80.     END
  81.  
  82.     RETURN res
  83. END _NEW;
  84.  
  85.  
  86. PROCEDURE _DISPOSE* (p: INTEGER): INTEGER;
  87. BEGIN
  88.     free(p)
  89.     RETURN 0
  90. END _DISPOSE;
  91.  
  92.  
  93. PROCEDURE GetProcAdr (lib: INTEGER; name: ARRAY OF CHAR; VarAdr: INTEGER);
  94. VAR
  95.     sym: INTEGER;
  96. BEGIN
  97.     sym := dlsym(lib, SYSTEM.ADR(name[0]));
  98.     ASSERT(sym # 0);
  99.     SYSTEM.PUT(VarAdr, sym)
  100. END GetProcAdr;
  101.  
  102.  
  103. PROCEDURE init* (sp, code: INTEGER);
  104. BEGIN
  105.     SYSTEM.GET(code - 1000H - SYSTEM.SIZE(INTEGER) * 2, dlopen);
  106.     SYSTEM.GET(code - 1000H - SYSTEM.SIZE(INTEGER),     dlsym);
  107.     MainParam := sp;
  108.     base := BASE_ADR;
  109.     eol := 0AX;
  110.  
  111.     libc := dlopen(SYSTEM.SADR("libc.so.6"), 1);
  112.     GetProcAdr(libc, "malloc", SYSTEM.ADR(malloc));
  113.     GetProcAdr(libc, "free", SYSTEM.ADR(free));
  114.     GetProcAdr(libc, "exit", SYSTEM.ADR(_exit));
  115.     GetProcAdr(libc, "stdout", SYSTEM.ADR(stdout));
  116.     GetProcAdr(libc, "stdin", SYSTEM.ADR(stdin));
  117.     GetProcAdr(libc, "stderr", SYSTEM.ADR(stderr));
  118.     SYSTEM.GET(stdout - SYSTEM.SIZE(INTEGER), stdout);
  119.     SYSTEM.GET(stdin  - SYSTEM.SIZE(INTEGER), stdin);
  120.     SYSTEM.GET(stderr - SYSTEM.SIZE(INTEGER), stderr);
  121.     GetProcAdr(libc, "puts", SYSTEM.ADR(puts));
  122.     GetProcAdr(libc, "fwrite", SYSTEM.ADR(fwrite));
  123.     GetProcAdr(libc, "fread", SYSTEM.ADR(fread));
  124.     GetProcAdr(libc, "fopen", SYSTEM.ADR(fopen));
  125.     GetProcAdr(libc, "fclose", SYSTEM.ADR(fclose));
  126.     GetProcAdr(libc, "time", SYSTEM.ADR(time));
  127.  
  128.     librt := dlopen(SYSTEM.SADR("librt.so.1"), 1);
  129.     GetProcAdr(librt, "clock_gettime", SYSTEM.ADR(clock_gettime))
  130. END init;
  131.  
  132.  
  133. PROCEDURE exit* (code: INTEGER);
  134. BEGIN
  135.     _exit(code)
  136. END exit;
  137.  
  138.  
  139. PROCEDURE exit_thread* (code: INTEGER);
  140. BEGIN
  141.     _exit(code)
  142. END exit_thread;
  143.  
  144.  
  145. END API.