Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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