2017-11-02 17:36:50 +01:00
|
|
|
(*
|
2019-03-11 09:59:55 +01:00
|
|
|
BSD 2-Clause License
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
Copyright (c) 2019, Anton Krotov
|
|
|
|
All rights reserved.
|
2016-10-24 01:30:27 +02:00
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE API;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
IMPORT SYSTEM;
|
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
|
|
|
BASE_ADR = 08048000H;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
TP* = ARRAY 2 OF INTEGER;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
VAR
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
eol*: ARRAY 2 OF CHAR;
|
|
|
|
base*, MainParam*: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
libc*, librt*: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
dlopen* : PROCEDURE [linux] (filename, flag: INTEGER): INTEGER;
|
|
|
|
dlsym* : PROCEDURE [linux] (handle, symbol: INTEGER): INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
stdout*,
|
|
|
|
stdin*,
|
|
|
|
stderr* : INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
malloc* : PROCEDURE [linux] (size: INTEGER): INTEGER;
|
|
|
|
free* : PROCEDURE [linux] (ptr: INTEGER);
|
|
|
|
_exit* : PROCEDURE [linux] (code: INTEGER);
|
|
|
|
puts* : PROCEDURE [linux] (pStr: INTEGER);
|
|
|
|
fwrite*,
|
|
|
|
fread* : PROCEDURE [linux] (buffer, bytes, blocks, file: INTEGER): INTEGER;
|
|
|
|
fopen* : PROCEDURE [linux] (fname, fmode: INTEGER): INTEGER;
|
|
|
|
fclose* : PROCEDURE [linux] (file: INTEGER): INTEGER;
|
|
|
|
|
|
|
|
clock_gettime* : PROCEDURE [linux] (clock_id: INTEGER; VAR tp: TP): INTEGER;
|
|
|
|
time* : PROCEDURE [linux] (ptr: INTEGER): INTEGER;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE putc* (c: CHAR);
|
|
|
|
VAR
|
|
|
|
res: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
res := fwrite(SYSTEM.ADR(c), 1, 1, stdout)
|
|
|
|
END putc;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
PROCEDURE DebugMsg* (lpText, lpCaption: INTEGER);
|
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
puts(lpCaption);
|
|
|
|
puts(lpText)
|
2016-10-24 01:30:27 +02:00
|
|
|
END DebugMsg;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
PROCEDURE _NEW* (size: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
res, ptr, words: INTEGER;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
res := malloc(size);
|
|
|
|
IF res # 0 THEN
|
|
|
|
ptr := res;
|
|
|
|
words := size DIV SYSTEM.SIZE(INTEGER);
|
|
|
|
WHILE words > 0 DO
|
|
|
|
SYSTEM.PUT(ptr, 0);
|
|
|
|
INC(ptr, SYSTEM.SIZE(INTEGER));
|
|
|
|
DEC(words)
|
|
|
|
END
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END _NEW;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
PROCEDURE _DISPOSE* (p: INTEGER): INTEGER;
|
2017-11-02 17:36:50 +01:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
free(p)
|
|
|
|
RETURN 0
|
|
|
|
END _DISPOSE;
|
2017-11-02 17:36:50 +01:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
PROCEDURE GetProcAdr (lib: INTEGER; name: ARRAY OF CHAR; VarAdr: INTEGER);
|
|
|
|
VAR
|
|
|
|
sym: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
sym := dlsym(lib, SYSTEM.ADR(name[0]));
|
|
|
|
ASSERT(sym # 0);
|
|
|
|
SYSTEM.PUT(VarAdr, sym)
|
|
|
|
END GetProcAdr;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
PROCEDURE init* (sp, code: INTEGER);
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.GET(code - 1000H - SYSTEM.SIZE(INTEGER) * 2, dlopen);
|
|
|
|
SYSTEM.GET(code - 1000H - SYSTEM.SIZE(INTEGER), dlsym);
|
|
|
|
MainParam := sp;
|
|
|
|
base := BASE_ADR;
|
|
|
|
eol := 0AX;
|
|
|
|
|
|
|
|
libc := dlopen(SYSTEM.SADR("libc.so.6"), 1);
|
|
|
|
GetProcAdr(libc, "malloc", SYSTEM.ADR(malloc));
|
|
|
|
GetProcAdr(libc, "free", SYSTEM.ADR(free));
|
|
|
|
GetProcAdr(libc, "exit", SYSTEM.ADR(_exit));
|
|
|
|
GetProcAdr(libc, "stdout", SYSTEM.ADR(stdout));
|
|
|
|
GetProcAdr(libc, "stdin", SYSTEM.ADR(stdin));
|
|
|
|
GetProcAdr(libc, "stderr", SYSTEM.ADR(stderr));
|
|
|
|
SYSTEM.GET(stdout - SYSTEM.SIZE(INTEGER), stdout);
|
|
|
|
SYSTEM.GET(stdin - SYSTEM.SIZE(INTEGER), stdin);
|
|
|
|
SYSTEM.GET(stderr - SYSTEM.SIZE(INTEGER), stderr);
|
|
|
|
GetProcAdr(libc, "puts", SYSTEM.ADR(puts));
|
|
|
|
GetProcAdr(libc, "fwrite", SYSTEM.ADR(fwrite));
|
|
|
|
GetProcAdr(libc, "fread", SYSTEM.ADR(fread));
|
|
|
|
GetProcAdr(libc, "fopen", SYSTEM.ADR(fopen));
|
|
|
|
GetProcAdr(libc, "fclose", SYSTEM.ADR(fclose));
|
|
|
|
GetProcAdr(libc, "time", SYSTEM.ADR(time));
|
|
|
|
|
|
|
|
librt := dlopen(SYSTEM.SADR("librt.so.1"), 1);
|
|
|
|
GetProcAdr(librt, "clock_gettime", SYSTEM.ADR(clock_gettime))
|
2016-10-24 01:30:27 +02:00
|
|
|
END init;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
PROCEDURE exit* (code: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
_exit(code)
|
|
|
|
END exit;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE exit_thread* (code: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
_exit(code)
|
|
|
|
END exit_thread;
|
|
|
|
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
END API.
|