Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1005 barsuk 1
#include "kosSyst.h"
2
#include 
3
 
4
#define atexitBufferSize	32
5
 
6
 
7
char pureCallMessage[] = "PURE function call!";
8
 
9
char *kosExePath = NULL;
10
 
11
//
12
void (__cdecl *atExitList[atexitBufferSize])();
13
int atExitFnNum = 0;
14
//
15
int __cdecl atexit( void (__cdecl *func )( void ))
16
{
17
	//
18
	if ( atExitFnNum < atexitBufferSize )
19
	{
20
		//
21
		atExitList[atExitFnNum++] = func;
22
		return 0;
23
	}
24
	else
25
	{
26
		return 1;
27
	}
28
}
29
 
30
 
31
//
32
Dword RandomSeed = 1;
33
//
34
void rtlSrand( Dword seed )
35
{
36
	RandomSeed = seed;
37
}
38
//
39
Dword rtlRand( void )
40
{
41
  //маска 0x80000776
42
 
43
  Dword dwi, i;
44
 
45
  for ( i = 0; i < 32; i++ )
46
  {
47
 
48
    dwi = RandomSeed & 0x80000776;
49
 
50
      __asm{
51
            mov   eax, dwi
52
            mov   edx, eax
53
            bswap eax
54
            xor   eax, edx
55
            xor   al, ah
56
            setpo al
57
            movzx eax, al
58
            mov   dwi, eax
59
    }
60
 
61
    RandomSeed = ( RandomSeed << 1 ) | ( dwi & 1 );
62
  }
63
 
64
 return RandomSeed;
65
}
66
 
67
#if _MSC_VER >= 1400
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
#endif
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
//////////////////////////////////////////////////////////////////////
178
//
179
// вывод строки на печать
180
//
181
 
182
Dword dectab[] = { 1000000000, 100000000, 10000000, 1000000, 100000,
183
                   10000, 1000, 100, 10, 0 };
184
 
185
//
186
void sprintf( char *Str, char* Format, ... )
187
{
188
	int i, fmtlinesize, j, k, flag;
189
	Dword head, tail;
190
	char c;
191
	va_list arglist;
192
	//
193
	va_start(arglist, Format);
194
 
195
	//
196
	fmtlinesize = strlen( Format );
197
	//
198
	if( fmtlinesize == 0 ) return;
199
 
200
	//
201
	for( i = 0, j = 0; i < fmtlinesize; i++ )
202
	{
203
		//
204
		c = Format[i];
205
		//
206
		if( c != '%' )
207
		{
208
			Str[j++] = c;
209
			continue;
210
		}
211
		//
212
		i++;
213
		//
214
		if( i >= fmtlinesize ) break;
215
 
216
		//
217
		flag = 0;
218
		//
219
		c = Format[i];
220
		//
221
		switch( c )
222
		{
223
		//
224
		case '%':
225
			Str[j++] = c;
226
			break;
227
		// вывод строки
228
		case 'S':
229
			Byte* str;
230
			str = va_arg(arglist, Byte*);
231
			for( k = 0; ( c = str[k] ) != 0; k++ )
232
			{
233
				Str[j++] = c;
234
			}
235
			break;
236
		// вывод байта
237
		case 'B':
238
			k = va_arg(arglist, int) & 0xFF;
239
			Str[j++] = num2hex( ( k >> 4 ) & 0xF );
240
			Str[j++] = num2hex( k & 0xF );
241
			break;
242
		// вывод символа
243
		case 'C':
244
			Str[j++] = va_arg(arglist, int) & 0xFF;
245
			break;
246
		// вывод двойного слова в шестнадцатиричном виде
247
		case 'X':
248
			Dword val;
249
			val = va_arg(arglist, Dword);
250
			for( k = 7; k >= 0; k-- )
251
			{
252
				//
253
				c = num2hex ( ( val >> (k * 4) ) & 0xF );
254
				//
255
				if( c == '0' )
256
				{
257
					if( flag ) Str[j++] = c;
258
				}
259
				else
260
				{
261
					flag++;
262
					Str[j++] = c;
263
				}
264
			}
265
			//
266
			if( flag == 0 ) Str[j++] = '0';
267
			break;
268
		// вывод двойного слова в десятичном виде
269
		case 'U':
270
			head = va_arg(arglist, Dword);
271
			tail = 0;
272
			for( k = 0; dectab[k] != 0; k++ )
273
			{
274
				tail = head % dectab[k];
275
				head /= dectab[k];
276
				c = head + '0';
277
				if( c == '0' )
278
				{
279
					if( flag ) Str[j++] = c;
280
				}
281
				else
282
				{
283
					flag++;
284
					Str[j++] = c;
285
				}
286
				//
287
				head = tail;
288
			}
289
			//
290
			c = head + '0';
291
			Str[j++] = c;
292
			break;
293
		// вывод 64-битного слова в шестнадцатиричном виде
294
		case 'Q':
295
			unsigned int low_dword, high_dword;
296
			low_dword = va_arg(arglist, unsigned int);
297
			high_dword = va_arg(arglist, unsigned int);
298
			for( k = 7; k >= 0; k-- )
299
			{
300
				//
301
				c = num2hex ( ( ( high_dword + 1) >> (k * 4) ) & 0xF );
302
				//
303
				if( c == '0' )
304
				{
305
					if( flag ) Str[j++] = c;
306
				}
307
				else
308
				{
309
					flag++;
310
					Str[j++] = c;
311
				}
312
			}
313
			//
314
			for( k=7; k >= 0; k-- )
315
			{
316
				//
317
				c = num2hex ( ( low_dword >> (k * 4) ) & 0xF );
318
				//
319
				if( c == '0' )
320
				{
321
					if( flag ) Str[j++] = c;
322
				}
323
				else
324
				{
325
					flag++;
326
					Str[j++] = c;
327
				}
328
			}
329
			//
330
			if( flag == 0 ) Str[j++] = '0';
331
			//
332
			break;
333
		//
334
		default:
335
			break;
336
		}
337
	}
338
	//
339
	Str[j] = 0;
340
}
341
 
342
 
343
// функция -1 завершения процесса
344
void kos_ExitApp()
345
{
346
	int i;
347
 
348
	//
349
	for ( i = atExitFnNum - 1; i >= 0; i-- )
350
	{
351
		//
352
		atExitList[i]();
353
	}
354
	//
355
	__asm{
356
		mov eax, -1
357
		int 0x40
358
	}
359
}
360
 
361
 
362
// функция 0
363
void kos_DefineAndDrawWindow(
364
	Word x, Word y,
365
	Word sizeX, Word sizeY,
366
	Byte mainAreaType,
367
	Dword mainAreaColour,
368
	Byte headerType,
369
	Dword headerColour,
370
	Dword borderColour
371
	)
372
{
373
	Dword arg1, arg2, arg3, arg4;
374
 
375
	//
376
	arg1 = ( x << 16 ) + sizeX;
377
	arg2 = ( y << 16 ) + sizeY;
378
	arg3 = ( mainAreaType << 24 ) | mainAreaColour;
379
	arg4 = ( headerType << 24 ) | headerColour;
380
	//
381
	__asm{
382
		mov eax, 0
383
		mov ebx, arg1
384
		mov ecx, arg2
385
		mov edx, arg3
386
		mov esi, arg4
387
		mov edi, borderColour
388
		int 0x40
389
	}
390
}
391
 
392
 
393
// функция 1 поставить точку
394
void kos_PutPixel( Dword x, Dword y, Dword colour )
395
{
396
	//
397
	__asm{
398
		mov eax, 1
399
		mov ebx, x
400
		mov ecx, y
401
		mov edx, colour
402
		int 0x40
403
	}
404
}
405
 
406
 
407
// функция 2 получить код нажатой клавиши
408
bool kos_GetKey( Byte &keyCode )
409
{
410
	Dword result;
411
 
412
	//
413
	__asm{
414
		mov eax, 2
415
		int 0x40
416
		mov result, eax
417
	}
418
	//
419
	keyCode = result >> 8;
420
	//
421
	return ( result & 0xFF ) == 0;
422
}
423
 
424
 
425
// функция 3 получить время
426
Dword kos_GetSystemClock()
427
{
428
//	Dword result;
429
 
430
	//
431
	__asm{
432
		mov eax, 3
433
		int 0x40
434
//		mov result, eax
435
	}
436
	//
437
//	return result;
438
}
439
 
440
 
441
// функция 4
442
void kos_WriteTextToWindow(
443
	Word x,
444
	Word y,
445
	Byte fontType,
446
	Dword textColour,
447
	char *textPtr,
448
	Dword textLen
449
	)
450
{
451
	Dword arg1, arg2;
452
 
453
	//
454
	arg1 = ( x << 16 ) | y;
455
	arg2 = ( fontType << 24 ) | textColour;
456
	//
457
	__asm{
458
		mov eax, 4
459
		mov ebx, arg1
460
		mov ecx, arg2
461
		mov edx, textPtr
462
		mov esi, textLen
463
		int 0x40
464
	}
465
}
466
 
467
 
468
// функция 5 пауза, в сотых долях секунды
469
void kos_Pause( Dword value )
470
{
471
	//
472
	__asm{
473
		mov eax, 5
474
		mov ebx, value
475
		int 0x40
476
	}
477
}
478
 
479
 
480
// функция 7 нарисовать изображение
481
void kos_PutImage( RGB * imagePtr, Word sizeX, Word sizeY, Word x, Word y )
482
{
483
	Dword arg1, arg2;
484
 
485
	//
486
	arg1 = ( sizeX << 16 ) | sizeY;
487
	arg2 = ( x << 16 ) | y;
488
	//
489
	__asm{
490
		mov eax, 7
491
		mov ebx, imagePtr
492
		mov ecx, arg1
493
		mov edx, arg2
494
		int 0x40
495
	}
496
}
497
 
498
 
499
 
500
// функция 8 определить кнопку
501
void kos_DefineButton( Word x, Word y, Word sizeX, Word sizeY, Dword buttonID, Dword colour )
502
{
503
	Dword arg1, arg2;
504
 
505
	//
506
	arg1 = ( x << 16 ) | sizeX;
507
	arg2 = ( y << 16 ) | sizeY;
508
	//
509
	__asm{
510
		mov eax, 8
511
		mov ebx, arg1
512
		mov ecx, arg2
513
		mov edx, buttonID
514
		mov esi, colour
515
		int 0x40
516
	}
517
}
518
 
519
 
520
// функция 9 - информация о процессе
521
Dword kos_ProcessInfo( sProcessInfo *targetPtr, Dword processID )
522
{
523
//	Dword result;
524
 
525
	//
526
	__asm{
527
		mov eax, 9
528
		mov ebx, targetPtr
529
		mov ecx, processID
530
		int 0x40
531
//		mov result, eax
532
	}
533
	//
534
//	return result;
535
}
536
 
537
 
538
// функция 10
539
Dword kos_WaitForEvent()
540
{
541
//	Dword result;
542
 
543
	__asm{
544
		mov eax, 10
545
		int 0x40
546
//		mov result, eax
547
	}
548
 
549
//	return result;
550
}
551
 
552
 
553
// функция 11
554
Dword kos_CheckForEvent()
555
{
556
//	Dword result;
557
 
558
	__asm{
559
		mov eax, 11
560
		int 0x40
561
//		mov result, eax
562
	}
563
 
564
//	return result;
565
}
566
 
567
 
568
// функция 12
569
void kos_WindowRedrawStatus( Dword status )
570
{
571
	__asm{
572
		mov eax, 12
573
		mov ebx, status
574
		int 0x40
575
	}
576
}
577
 
578
 
579
// функция 13 нарисовать полосу
580
void kos_DrawBar( Word x, Word y, Word sizeX, Word sizeY, Dword colour )
581
{
582
	Dword arg1, arg2;
583
 
584
	//
585
	arg1 = ( x << 16 ) | sizeX;
586
	arg2 = ( y << 16 ) | sizeY;
587
	//
588
	__asm{
589
		mov eax, 13
590
		mov ebx, arg1
591
		mov ecx, arg2
592
		mov edx, colour
593
		int 0x40
594
	}
595
}
596
 
597
 
598
// функция 17
599
bool kos_GetButtonID( Dword &buttonID )
600
{
601
	Dword result;
602
 
603
	//
604
	__asm{
605
		mov eax, 17
606
		int 0x40
607
		mov result, eax
608
	}
609
	//
610
	buttonID = result >> 8;
611
	//
612
	return (result & 0xFF) == 0;
613
}
614
 
615
 
616
// функция 23
617
Dword kos_WaitForEvent( Dword timeOut )
618
{
619
//	Dword result;
620
 
621
	__asm{
622
		mov eax, 23
623
		mov ebx, timeOut
624
		int 0x40
625
//		mov result, eax
626
	}
627
 
628
//	return result;
629
}
630
 
631
 
632
// получение информации о состоянии "мыши" функция 37
633
void kos_GetMouseState( Dword & buttons, int & cursorX, int & cursorY )
634
{
635
	Dword mB;
636
	Word curX;
637
	Word curY;
638
	sProcessInfo sPI;
639
 
640
	//
641
	__asm{
642
		mov		eax, 37
643
		mov		ebx, 0
644
		int		0x40
645
		mov		curY, ax
646
		shr		eax, 16
647
		mov		curX, ax
648
		mov		eax, 37
649
		mov		ebx, 2
650
		int		0x40
651
		mov		mB, eax
652
	}
653
	//
654
	kos_ProcessInfo( &sPI );
655
	//
656
	buttons = mB;
657
	cursorX = curX - sPI.processInfo.x_start;
658
	cursorY = curY - sPI.processInfo.y_start;
659
}
660
 
661
 
662
// функция 40 установить маску событий
663
void kos_SetMaskForEvents( Dword mask )
664
{
665
	//
666
	__asm{
667
		mov eax, 40
668
		mov ebx, mask
669
		int 0x40
670
	}
671
}
672
 
673
 
674
// функция 47 вывести в окно приложения число
675
void kos_DisplayNumberToWindow(
676
   Dword value,
677
   Dword digitsNum,
678
   Word x,
679
   Word y,
680
   Dword colour,
681
   eNumberBase nBase,
682
   bool valueIsPointer
683
   )
684
{
685
	Dword arg1, arg2;
686
 
687
	//
688
	arg1 = ( valueIsPointer ? 1 : 0 ) |
689
		( ((Byte)nBase) << 8 ) |
690
		( ( digitsNum & 0x1F ) << 16 );
691
	arg2 = ( x << 16 ) | y;
692
	//
693
	__asm{
694
		mov eax, 47
695
		mov ebx, arg1
696
		mov ecx, value
697
		mov edx, arg2
698
		mov esi, colour
699
		int 0x40
700
	}
701
}
702
 
703
 
704
// функция 70 доступ к файловой системе
705
Dword kos_FileSystemAccess( kosFileInfo *fileInfo )
706
{
707
//	Dword result;
708
 
709
	//
710
	__asm{
711
		mov eax, 70
712
		mov ebx, fileInfo
713
		int 0x40
714
//		mov result, eax
715
	}
716
	//
717
//	return result;
718
}
719
 
720
 
721
// функция 63 вывод символя в окно отладки
722
void kos_DebugOutChar( char ccc )
723
{
724
	//
725
	__asm{
726
		mov eax, 63
727
		mov ebx, 1
728
		mov cl, ccc
729
		int 0x40
730
	}
731
}
732
 
733
 
734
// функция 66 режим получения данных от клавиатуры
735
void kos_SetKeyboardDataMode( Dword mode )
736
{
737
	//
738
	__asm{
739
		mov eax, 66
740
		mov ebx, 1
741
		mov ecx, mode
742
		int 0x40
743
	}
744
}
745
 
746
 
747
// вывод строки в окно отладки
748
void rtlDebugOutString( char *str )
749
{
750
	//
751
	for ( ; str[0] != 0; str++ )
752
	{
753
		kos_DebugOutChar( str[0] );
754
	}
755
	//
756
	kos_DebugOutChar( 13 );
757
	kos_DebugOutChar( 10 );
758
}
759
 
760
 
761
// функция 64 изменение количества памяти, выделенной для программы
762
bool kos_ApplicationMemoryResize( Dword targetSize )
763
{
764
	Dword result;
765
 
766
	//
767
	__asm{
768
		mov eax, 64
769
		mov ebx, 1
770
		mov ecx, targetSize
771
		int 0x40
772
		mov result, eax
773
	}
774
	//
775
	return result == 0;
776
}
777
 
778
 
779
// функция 67 изменить параметры окна, параметр == -1 не меняется
780
void kos_ChangeWindow( Dword x, Dword y, Dword sizeX, Dword sizeY )
781
{
782
	//
783
	__asm{
784
		mov eax, 67
785
		mov ebx, x
786
		mov ecx, y
787
		mov edx, sizeX
788
		mov esi, sizeY
789
		int 0x40
790
	}
791
}
792
 
793
void kos_InitHeap()
794
{
795
	__asm{
796
		mov eax, 68
797
		mov ebx, 11
798
		int 0x40
799
	}
800
}
801
 
802
 
803
 
804
// вызов абстрактного метода
805
int __cdecl _purecall()
806
{
807
	rtlDebugOutString( pureCallMessage );
808
	kos_ExitApp();
809
	return 0;
810
}
811
 
812
 
813
// вызов статических инициализаторов
814
// заодно инициализация генератора случайных чисел
815
//#pragma section(".CRT$XCA",long,read,write)
816
//#pragma section(".CRT$XCZ",long,read,write)
817
#pragma data_seg(".CRT$XCA")
818
#pragma data_seg(".CRT$XCZ")
819
typedef void (__cdecl *_PVFV)(void);
820
__declspec(allocate(".CRT$XCA"))  _PVFV __xc_a[1] = { NULL };
821
__declspec(allocate(".CRT$XCZ"))  _PVFV __xc_z[1] = { NULL };
822
//
823
#pragma comment(linker, "/merge:.CRT=.rdata")
824
//
825
void crtStartUp()
826
{
827
	// вызываем инициализаторы по списку, NULL'ы игнорируем
828
	for ( _PVFV *pbegin = __xc_a; pbegin < __xc_z; pbegin++ )
829
	{
830
		//
831
		if ( *pbegin != NULL )
832
			(**pbegin)();
833
	}
834
	// инициализируем генератор случайных чисел
835
	rtlSrand( kos_GetSystemClock() );
836
	// путь к файлу процесса
837
	kosExePath = *((char **)0x20);
838
	// вызов главной функции приложения
839
	kos_Main();
840
	// выход
841
	kos_ExitApp();
842
}
843