Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "kosSyst.h"
  2. #include "func.h"
  3. #include <stdarg.h>
  4.  
  5. char kosCmdLine[257];
  6. char kosExePath[1024];
  7. extern "C" char exeStack[];
  8. char exeStack[16384];
  9.  
  10. #define atexitBufferSize        32
  11.  
  12. #ifndef SMALLLIBC_NO_ATEXIT
  13. //
  14. void (__cdecl *atExitList[atexitBufferSize])();
  15. int atExitFnNum = 0;
  16. //
  17. int __cdecl atexit( void (__cdecl *func )( void ))
  18. {
  19.         //
  20.         if ( atExitFnNum < atexitBufferSize )
  21.         {
  22.                 //
  23.                 atExitList[atExitFnNum++] = func;
  24.                 return 0;
  25.         }
  26.         else
  27.         {
  28.                 return 1;
  29.         }
  30. }
  31. #endif
  32.  
  33. //
  34. Dword RandomSeed = 0;
  35. //
  36. void rtlSrand( Dword seed )
  37. {
  38.         RandomSeed = seed;
  39. }
  40. //
  41. Dword rtlRand( void )
  42. {
  43.   //ìàñêà 0x80000776
  44.  
  45.   Dword dwi, i;
  46.  
  47.   for ( i = 0; i < 32; i++ )
  48.   {
  49.  
  50.     dwi = RandomSeed & 0x80000776;
  51.  
  52.       __asm{
  53.             mov   eax, dwi
  54.             mov   edx, eax
  55.             bswap eax
  56.             xor   eax, edx
  57.             xor   al, ah
  58.             setpo al
  59.             movzx eax, al
  60.             mov   dwi, eax
  61.     }
  62.  
  63.     RandomSeed = ( RandomSeed << 1 ) | ( dwi & 1 );
  64.   }
  65.  
  66.  return RandomSeed;
  67. }
  68.  
  69. void* __cdecl memcpy( void *dst, const void *src, size_t bytesCount )
  70. {
  71.         __asm{
  72.                 mov edi, dst
  73.                 mov eax, dst
  74.                 mov esi, src
  75.                 mov ecx, bytesCount
  76.                 rep movsb
  77.         }
  78. }
  79.  
  80. //
  81. void memset( Byte *dst, Byte filler, Dword count )
  82. {
  83.         //
  84.         __asm{
  85.                 mov edi, dst
  86.                 mov al, filler
  87.                 mov ecx, count
  88.                 rep stosb
  89.         }
  90. }
  91.  
  92.  
  93. //
  94. Dword rtlInterlockedExchange( Dword *target, Dword value )
  95. {
  96. //      Dword result;
  97.  
  98.         //
  99.         __asm{
  100.                 mov eax, value
  101.                 mov ebx, target
  102.                 xchg eax, [ebx]
  103. //              mov result, eax
  104.         }
  105.         //
  106. //      return result;
  107. }
  108.  
  109.  
  110. //////////////////////////////////////////////////////////////////////
  111. //
  112. // êîïèðîâàíèå ñòðîêè
  113. //
  114.  
  115. char * __cdecl strcpy( char *target, const char *source )
  116. {
  117.         char *result = target;
  118.  
  119.         while( target[0] = source[0] )
  120.         {
  121.                 target++;
  122.                 source++;
  123.         }
  124.  
  125.         return result;
  126. }
  127.  
  128.  
  129. //////////////////////////////////////////////////////////////////////
  130. //
  131. // ðåâåðñèâíûé ïîèñê ñèìâîëà
  132. //
  133.  
  134. char * __cdecl strrchr( const char * string, int c )
  135. {
  136.         char *cPtr;
  137.  
  138.         //
  139.         for ( cPtr = (char *)string + strlen( string ); cPtr >= string; cPtr-- )
  140.         {
  141.                 //
  142.                 if ( *cPtr == c ) return cPtr;
  143.         }
  144.         //
  145.         return NULL;
  146. }
  147.  
  148.  
  149. //////////////////////////////////////////////////////////////////////
  150. //
  151. // îïðåäåëåíèå äëèíû ñòðîêè
  152. //
  153.  
  154. int __cdecl strlen( const char *line )
  155. {
  156.   int i;
  157.  
  158.   for( i=0; line[i] != 0; i++ );
  159.   return i;
  160. }
  161.  
  162.  
  163.  
  164. //////////////////////////////////////////////////////////////////////
  165. //
  166. // ïåðåâîä øåñòíàäöàòèðè÷íîãî ÷èñëà â ñèìâîë
  167. //
  168.  
  169. unsigned int num2hex( unsigned int num )
  170. {
  171.   if( num < 10 )
  172.     return num + '0';
  173.   return num - 10 + 'A';
  174. }
  175.  
  176.  
  177. inline void __declspec(noreturn) kos_sysfuncm1(void)
  178. {
  179.         __asm or eax, -1
  180.         __asm int 0x40
  181. }
  182.  
  183. // ôóíêöèÿ -1 çàâåðøåíèÿ ïðîöåññà
  184. void kos_ExitApp()
  185. {
  186. #ifndef SMALLLIBC_NO_ATEXIT
  187.         int i;
  188.  
  189.         //
  190.         for ( i = atExitFnNum - 1; i >= 0; i-- )
  191.         {
  192.                 //
  193.                 atExitList[i]();
  194.         }
  195. #endif
  196.         //
  197.         kos_sysfuncm1();
  198. }
  199.  
  200. static void __declspec(noinline) __fastcall kos_sysfunc0(Dword _ecx, Dword _edx, Dword _ebx, Dword _esi, Dword _edi)
  201. {
  202.         __asm xor eax, eax
  203.         __asm mov ebx, _ebx
  204.         __asm mov esi, _esi
  205.         __asm mov edi, _edi
  206.         __asm int 0x40
  207. }
  208.  
  209. // ôóíêöèÿ 0
  210. void kos_DefineAndDrawWindow(
  211.         Word x, Word y,
  212.         Word sizeX, Word sizeY,
  213.         Byte mainAreaType,
  214.         Dword mainAreaColour,
  215.         Byte headerType,
  216.         Dword headerColour,
  217.         Dword borderColour
  218.         )
  219. {
  220.         Dword arg1, arg2, arg3, arg4;
  221.  
  222.         //
  223.         arg1 = ( x << 16 ) + sizeX;
  224.         arg2 = ( y << 16 ) + sizeY;
  225.         arg3 = ( mainAreaType << 24 ) | mainAreaColour;
  226.         arg4 = ( headerType << 24 ) | headerColour;
  227.         //
  228.         kos_sysfunc0(arg2, arg3, arg1, arg4, borderColour);
  229. }
  230.  
  231.  
  232. // ôóíêöèÿ 1 ïîñòàâèòü òî÷êó
  233. void kos_PutPixel( Dword x, Dword y, Dword colour )
  234. {
  235.         //
  236.         __asm{
  237.                 push 1
  238.                 pop eax
  239.                 mov ebx, x
  240.                 mov ecx, y
  241.                 mov edx, colour
  242.                 int 0x40
  243.         }
  244. }
  245.  
  246. inline Dword kos_sysfunc2(void)
  247. {
  248.         __asm push 2
  249.         __asm pop eax
  250.         __asm int 0x40
  251. }
  252.  
  253. // ôóíêöèÿ 2 ïîëó÷èòü êîä íàæàòîé êëàâèøè
  254. bool kos_GetKey( Byte &keyCode )
  255. {
  256.         Dword result = kos_sysfunc2();
  257.         //
  258.         keyCode = result >> 8;
  259.         //
  260.         return ( result & 0xFF ) == 0;
  261. }
  262.  
  263.  
  264. // ôóíêöèÿ 3 ïîëó÷èòü âðåìÿ
  265. Dword kos_GetSystemClock()
  266. {
  267. //      Dword result;
  268.  
  269.         //
  270.         __asm{
  271.                 push 3
  272.                 pop eax
  273.                 int 0x40
  274. //              mov result, eax
  275.         }
  276.         //
  277. //      return result;
  278. }
  279.  
  280. static void __declspec(noinline) __fastcall kos_sysfunc4(Dword _ecx, const char* _edx, Dword _ebx, Dword _esi)
  281. {
  282.         __asm push 4
  283.         __asm pop eax
  284.         __asm mov ebx, [_ebx]
  285.         __asm mov esi, [_esi]
  286.         __asm int 0x40
  287. }
  288.  
  289. // ôóíêöèÿ 4
  290. void kos_WriteTextToWindow(
  291.         Word x,
  292.         Word y,
  293.         Byte fontType,
  294.         Dword textColour,
  295.         const char *textPtr,
  296.         Dword textLen
  297.         )
  298. {
  299.         Dword arg1, arg2;
  300.  
  301.         //
  302.         arg1 = ( x << 16 ) | y;
  303.         arg2 = ( fontType << 24 ) | textColour;
  304.         //
  305.         kos_sysfunc4(arg2, textPtr, arg1, textLen);
  306. }
  307.  
  308.  
  309. // ôóíêöèÿ 5 ïàóçà, â ñîòûõ äîëÿõ ñåêóíäû
  310. void kos_Pause( Dword value )
  311. {
  312.         //
  313.         __asm{
  314.                 push 5
  315.                 pop eax
  316.                 mov ebx, value
  317.                 int 0x40
  318.         }
  319. }
  320.  
  321.  
  322. // ôóíêöèÿ 7 íàðèñîâàòü èçîáðàæåíèå
  323. void kos_PutImage( RGB * imagePtr, Word sizeX, Word sizeY, Word x, Word y )
  324. {
  325.         Dword arg1, arg2;
  326.  
  327.         //
  328.         arg1 = ( sizeX << 16 ) | sizeY;
  329.         arg2 = ( x << 16 ) | y;
  330.         //
  331.         __asm{
  332.                 push 7
  333.                 pop eax
  334.                 mov ebx, imagePtr
  335.                 mov ecx, arg1
  336.                 mov edx, arg2
  337.                 int 0x40
  338.         }
  339. }
  340.  
  341.  
  342.  
  343. // ôóíêöèÿ 8 îïðåäåëèòü êíîïêó
  344. void kos_DefineButton( Word x, Word y, Word sizeX, Word sizeY, Dword buttonID, Dword colour )
  345. {
  346.         Dword arg1, arg2;
  347.  
  348.         //
  349.         arg1 = ( x << 16 ) | sizeX;
  350.         arg2 = ( y << 16 ) | sizeY;
  351.         //
  352.         __asm{
  353.                 push 8
  354.                 pop eax
  355.                 mov ebx, arg1
  356.                 mov ecx, arg2
  357.                 mov edx, buttonID
  358.                 mov esi, colour
  359.                 int 0x40
  360.         }
  361. }
  362.  
  363.  
  364. // ôóíêöèÿ 9 - èíôîðìàöèÿ î ïðîöåññå
  365. Dword kos_ProcessInfo( sProcessInfo *targetPtr, Dword processID )
  366. {
  367. //      Dword result;
  368.  
  369.         //
  370.         __asm{
  371.                 push 9
  372.                 pop eax
  373.                 mov ebx, targetPtr
  374.                 mov ecx, processID
  375.                 int 0x40
  376. //              mov result, eax
  377.         }
  378.         //
  379. //      return result;
  380. }
  381.  
  382.  
  383. // ôóíêöèÿ 10
  384. Dword kos_WaitForEvent()
  385. {
  386. //      Dword result;
  387.  
  388.         __asm{
  389.                 push 10
  390.                 pop eax
  391.                 int 0x40
  392. //              mov result, eax
  393.         }
  394.        
  395. //      return result;
  396. }
  397.  
  398.  
  399. // ôóíêöèÿ 11
  400. Dword kos_CheckForEvent()
  401. {
  402.         Dword result; //
  403.  
  404.         __asm{
  405.                 push 11
  406.                 pop eax
  407.                 int 0x40
  408.                 mov result, eax //
  409.         }
  410.        
  411.         return result; //
  412. }
  413.  
  414.  
  415. // ôóíêöèÿ 12
  416. void kos_WindowRedrawStatus( Dword status )
  417. {
  418.         __asm{
  419.                 push 12
  420.                 pop eax
  421.                 mov ebx, status
  422.                 int 0x40
  423.         }
  424. }
  425.  
  426.  
  427. // ôóíêöèÿ 13 íàðèñîâàòü ïîëîñó
  428. void kos_DrawBar( Word x, Word y, Word sizeX, Word sizeY, Dword colour )
  429. {
  430.         Dword arg1, arg2;
  431.  
  432.         //
  433.         arg1 = ( x << 16 ) | sizeX;
  434.         arg2 = ( y << 16 ) | sizeY;
  435.         //
  436.         __asm{
  437.                 push 13
  438.                 pop eax
  439.                 mov ebx, arg1
  440.                 mov ecx, arg2
  441.                 mov edx, colour
  442.                 int 0x40
  443.         }
  444. }
  445.  
  446.  
  447. // ôóíêöèÿ 17
  448. bool kos_GetButtonID( Dword &buttonID )
  449. {
  450.         Dword result;
  451.  
  452.         //
  453.         __asm{
  454.                 push 17
  455.                 pop eax
  456.                 int 0x40
  457.                 mov result, eax
  458.         }
  459.         //
  460.         buttonID = result >> 8;
  461.         //
  462.         return (result & 0xFF) == 0;
  463. }
  464.  
  465.  
  466. // ôóíêöèÿ 23
  467. Dword kos_WaitForEventTimeout( Dword timeOut )
  468. {
  469. //      Dword result;
  470.  
  471.         __asm{
  472.                 push 23
  473.                 pop eax
  474.                 mov ebx, timeOut
  475.                 int 0x40
  476. //              mov result, eax
  477.         }
  478.        
  479. //      return result;
  480. }
  481.  
  482.  
  483. // ïîëó÷åíèå èíôîðìàöèè î ñîñòîÿíèè "ìûøè" ôóíêöèÿ 37
  484. void kos_GetMouseState( Dword & buttons, int & cursorX, int & cursorY )
  485. {
  486.         Dword mB;
  487.         Word curX;
  488.         Word curY;
  489.         sProcessInfo sPI;
  490.  
  491.         //
  492.         __asm{
  493.                 push 37
  494.                 pop eax
  495.                 xor ebx, ebx
  496.                 int             0x40
  497.                 mov             curY, ax
  498.                 shr             eax, 16
  499.                 mov             curX, ax
  500.                 push 37
  501.                 pop eax
  502.                 push 2
  503.                 pop ebx
  504.                 int             0x40
  505.                 mov             mB, eax
  506.         }
  507.         //
  508.         kos_ProcessInfo( &sPI );
  509.         //
  510.         buttons = mB;
  511.         cursorX = curX - sPI.processInfo.x_start;
  512.         cursorY = curY - sPI.processInfo.y_start;
  513. }
  514.  
  515.  
  516. // ôóíêöèÿ 40 óñòàíîâèòü ìàñêó ñîáûòèé
  517. void kos_SetMaskForEvents( Dword mask )
  518. {
  519.         //
  520.         __asm{
  521.                 push 40
  522.                 pop eax
  523.                 mov ebx, mask
  524.                 int 0x40
  525.         }
  526. }
  527.  
  528.  
  529. // ôóíêöèÿ 47 âûâåñòè â îêíî ïðèëîæåíèÿ ÷èñëî
  530. void kos_DisplayNumberToWindow(
  531.    Dword value,
  532.    Dword digitsNum,
  533.    Word x,
  534.    Word y,
  535.    Dword colour,
  536.    eNumberBase nBase,
  537.    bool valueIsPointer
  538.    )
  539. {
  540.         Dword arg1, arg2;
  541.  
  542.         //
  543.         arg1 = ( valueIsPointer ? 1 : 0 ) |
  544.                 ( ((Byte)nBase) << 8 ) |
  545.                 ( ( digitsNum & 0x1F ) << 16 );
  546.         arg2 = ( x << 16 ) | y;
  547.         //
  548.         __asm{
  549.                 push 47
  550.                 pop eax
  551.                 mov ebx, arg1
  552.                 mov ecx, value
  553.                 mov edx, arg2
  554.                 mov esi, colour
  555.                 int 0x40
  556.         }
  557. }
  558.  
  559.  
  560. // ôóíêöèÿ 70 äîñòóï ê ôàéëîâîé ñèñòåìå
  561. Dword kos_FileSystemAccess( kosFileInfo *fileInfo )
  562. {
  563. //      Dword result;
  564.  
  565.         //
  566.         __asm{
  567.                 push 70
  568.                 pop eax
  569.                 mov ebx, fileInfo
  570.                 int 0x40
  571. //              mov result, eax
  572.         }
  573.         //
  574. //      return result;
  575. }
  576.  
  577.  
  578. // ôóíêöèÿ 63 âûâîä ñèìâîëÿ â îêíî îòëàäêè
  579. void kos_DebugOutChar( char ccc )
  580. {
  581.         //
  582.         __asm{
  583.                 push 63
  584.                 pop eax
  585.                 push 1
  586.                 pop ebx
  587.                 mov cl, ccc
  588.                 int 0x40
  589.         }
  590. }
  591.  
  592.  
  593. // ôóíêöèÿ 66 ðåæèì ïîëó÷åíèÿ äàííûõ îò êëàâèàòóðû
  594. void kos_SetKeyboardDataMode( Dword mode )
  595. {
  596.         //
  597.         __asm{
  598.                 push 66
  599.                 pop eax
  600.                 push 1
  601.                 pop ebx
  602.                 mov ecx, mode
  603.                 int 0x40
  604.         }
  605. }
  606.  
  607.  
  608. // âûâîä ñòðîêè â îêíî îòëàäêè
  609. void rtlDebugOutString( char *str )
  610. {
  611.         //
  612.         for ( ; str[0] != 0; str++ )
  613.         {
  614.                 kos_DebugOutChar( str[0] );
  615.         }
  616.         //
  617.         kos_DebugOutChar( 13 );
  618.         kos_DebugOutChar( 10 );
  619. }
  620.  
  621.  
  622. // ôóíêöèÿ 64 èçìåíåíèå êîëè÷åñòâà ïàìÿòè, âûäåëåííîé äëÿ ïðîãðàììû
  623. bool kos_ApplicationMemoryResize( Dword targetSize )
  624. {
  625.         Dword result;
  626.  
  627.         //
  628.         __asm{
  629.                 push 64
  630.                 pop eax
  631.                 push 1
  632.                 pop ebx
  633.                 mov ecx, targetSize
  634.                 int 0x40
  635.                 mov result, eax
  636.         }
  637.         //
  638.         return result == 0;
  639. }
  640.  
  641.  
  642. // ôóíêöèÿ 67 èçìåíèòü ïàðàìåòðû îêíà, ïàðàìåòð == -1 íå ìåíÿåòñÿ
  643. void kos_ChangeWindow( Dword x, Dword y, Dword sizeX, Dword sizeY )
  644. {
  645.         //
  646.         __asm{
  647.                 push 67
  648.                 pop eax
  649.                 mov ebx, x
  650.                 mov ecx, y
  651.                 mov edx, sizeX
  652.                 mov esi, sizeY
  653.                 int 0x40
  654.         }
  655. }
  656.  
  657. void kos_InitHeap()
  658. {
  659.         __asm{
  660.                 push 68
  661.                 pop eax
  662.                 push 11
  663.                 pop ebx
  664.                 int 0x40
  665.         }
  666. }
  667.  
  668.  
  669.  
  670. // âûçîâ ñòàòè÷åñêèõ èíèöèàëèçàòîðîâ
  671. typedef void (__cdecl *_PVFV)(void);
  672. extern "C" _PVFV __xc_a[];
  673. extern "C" _PVFV __xc_z[];
  674. #pragma comment(linker, "/merge:.CRT=.rdata")
  675. //
  676. void __cdecl crtStartUp()
  677. {
  678. #ifndef SMALLLIBC_NO_INIT
  679.         // âûçûâàåì èíèöèàëèçàòîðû ïî ñïèñêó
  680.         for ( _PVFV *pbegin = __xc_a; pbegin < __xc_z; pbegin++ )
  681.         {
  682.                 //
  683.                 (**pbegin)();
  684.         }
  685. #endif
  686.         // èíèöèàëèçèðóåì ãåíåðàòîð ñëó÷àéíûõ ÷èñåë
  687.         // åñëè íàäî äëÿ ïðèëîæåíèÿ, äåëàòü ýòî â kos_Main()
  688.         //rtlSrand( kos_GetSystemClock() );
  689.         // âûçîâ ãëàâíîé ôóíêöèè ïðèëîæåíèÿ
  690.         kos_Main();
  691.         // âûõîä
  692.         kos_ExitApp();
  693. }
  694.  
  695.  
  696.