Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
958 leency 1
#include "kosSyst.h"
2
#include "stdarg.h"
3
 
4
#define atexitBufferSize	32
5
 
6
 
7
char pureCallMessage[] = "PURE function call!";
8
 
9
char *kosExePath = NULL;
10
 
11
int abs(int value)
12
{
13
  if (value<0) value=0-value;
14
  return value;
15
}
16
 
17
//
18
void (__cdecl *atExitList[atexitBufferSize])();
19
int atExitFnNum = 0;
20
//
21
int __cdecl atexit( void (__cdecl *func )( void ))
22
{
23
	//
24
	if ( atExitFnNum < atexitBufferSize )
25
	{
26
		//
27
		atExitList[atExitFnNum++] = func;
28
		return 0;
29
	}
30
	else
31
	{
32
		return 1;
33
	}
34
}
35
 
36
 
37
//
38
Dword RandomSeed = 1;
39
//
40
void rtlSrand( Dword seed )
41
{
42
	RandomSeed = seed;
43
}
44
//
45
Dword rtlRand( void )
46
{
47
  //маска 0x80000776
48
 
49
  Dword dwi, i;
50
 
51
  for ( i = 0; i < 32; i++ )
52
  {
53
 
54
    dwi = RandomSeed & 0x80000776;
55
 
56
      __asm{
57
            mov   eax, dwi
58
            mov   edx, eax
59
            bswap eax
60
            xor   eax, edx
61
            xor   al, ah
62
            setpo al
63
            movzx eax, al
64
            mov   dwi, eax
65
    }
66
 
67
    RandomSeed = ( RandomSeed << 1 ) | ( dwi & 1 );
68
  }
69
 
70
 return RandomSeed;
71
}
72
 
73
#if _MSC_VER >= 1400
74
//
75
void * __cdecl memcpy( void *dst, const void *src, size_t bytesCount )
76
{
77
	__asm{
78
		mov edi, dst
79
		mov eax, dst
80
		mov esi, src
81
		mov ecx, bytesCount
82
		rep movsb
83
	}
84
}
85
 
86
//
87
void memset( Byte *dst, Byte filler, Dword count )
88
{
89
	//
90
	__asm{
91
		mov edi, dst
92
		mov al, filler
93
		mov ecx, count
94
		rep stosb
95
	}
96
}
97
#endif
98
 
99
//
100
Dword rtlInterlockedExchange( Dword *target, Dword value )
101
{
102
//	Dword result;
103
 
104
	//
105
	__asm{
106
		mov eax, value
107
		mov ebx, target
108
		xchg eax, [ebx]
109
//		mov result, eax
110
	}
111
	//
112
//	return result;
113
}
114
 
115
 
116
//////////////////////////////////////////////////////////////////////
117
//
118
// копирование строки
119
//
120
 
121
char * __cdecl strcpy( char *target, const char *source )
122
{
123
	char *result = target;
124
 
125
	while( target[0] = source[0] )
126
	{
127
		target++;
128
		source++;
129
	}
130
 
131
	return result;
132
}
133
 
134
 
135
//////////////////////////////////////////////////////////////////////
136
//
137
// реверсивный поиск символа
138
//
139
 
140
char * __cdecl strrchr( const char * string, int c )
141
{
142
	char *cPtr;
143
 
144
	//
145
	for ( cPtr = (char *)string + strlen( string ); cPtr >= string; cPtr-- )
146
	{
147
		//
148
		if ( *cPtr == c ) return cPtr;
149
	}
150
	//
151
	return NULL;
152
}
153
 
154
 
155
//////////////////////////////////////////////////////////////////////
156
//
157
// определение длины строки
158
//
159
 
160
int __cdecl strlen( const char *line )
161
{
162
  int i;
163
 
164
  for( i=0; line[i] != 0; i++ );
165
  return i;
166
}
167
 
168
 
169
 
170
//////////////////////////////////////////////////////////////////////
171
//
172
// Сравнение строк
173
//
174
 
175
int strcmp(char* string1, char* string2)
176
{
177
  Dword  retval;
178
  if (strlen(string1)==strlen(string2))
179
  {
180
    __asm
181
    {
182
      mov  dword ptr [retval],0
183
      mov  esi,string1
184
      mov  edi,string2
185
     next_char:
186
      mov  al,byte ptr [esi]
187
      mov  bl,byte ptr [edi]
188
      inc  esi
189
      inc  edi
190
      cmp  bl,0
191
      je   fin
192
      cmp  al,bl
193
      je   next_char
194
      jmp  not_equal
195
     fin:
196
      mov  dword ptr [retval],1
197
     not_equal:
198
    }
199
    return retval;
200
  } else {
201
    return 0;
202
  }
203
}
204
 
205
 
206
//////////////////////////////////////////////////////////////////////
207
//
208
// перевод шестнадцатиричного числа в символ
209
//
210
 
211
unsigned int num2hex( unsigned int num )
212
{
213
  if( num < 10 )
214
    return num + '0';
215
  return num - 10 + 'A';
216
}
217
 
218
 
219
 
220
//////////////////////////////////////////////////////////////////////
221
//
222
// перевод строки в нижний регистр
223
//
224
 
225
void lcase(char* string)
226
{
227
  int i;
228
  char chr;
229
  for(i=0;i
230
  {
231
    chr=((char*)string)[i];
232
    if (chr>=65 && chr<=90) chr=chr+32;    //a-z
233
    if (chr>=128 && chr<=143) chr=chr+32;  //а-п
234
    if (chr>=144 && chr<=159) chr=chr+80;  //р-я
235
    if (chr==240) chr=241;                 //ё
236
    ((char*)string)[i]=chr;
237
  }
238
}
239
 
240
 
241
//////////////////////////////////////////////////////////////////////
242
//
243
// вывод строки на печать
244
//
245
 
246
Dword dectab[] = { 1000000000, 100000000, 10000000, 1000000, 100000,
247
                   10000, 1000, 100, 10, 0 };
248
 
249
//
250
void sprintf( char *Str, char* Format, ... )
251
{
252
	int i, fmtlinesize, j, k, flag;
253
	Dword head, tail;
254
	char c;
255
	va_list arglist;
256
	//
257
	va_start(arglist, Format);
258
 
259
	//
260
	fmtlinesize = strlen( Format );
261
	//
262
	if( fmtlinesize == 0 ) return;
263
 
264
	//
265
	for( i = 0, j = 0; i < fmtlinesize; i++ )
266
	{
267
		//
268
		c = Format[i];
269
		//
270
		if( c != '%' )
271
		{
272
			Str[j++] = c;
273
			continue;
274
		}
275
		//
276
		i++;
277
		//
278
		if( i >= fmtlinesize ) break;
279
 
280
		//
281
		flag = 0;
282
		//
283
		c = Format[i];
284
		//
285
		switch( c )
286
		{
287
		//
288
		case '%':
289
			Str[j++] = c;
290
			break;
291
		// вывод строки
292
		case 'S':
293
			Byte* str;
294
			str = va_arg(arglist, Byte*);
295
			for( k = 0; ( c = str[k] ) != 0; k++ )
296
			{
297
				Str[j++] = c;
298
			}
299
			break;
300
		// вывод байта
301
		case 'B':
302
			k = va_arg(arglist, int) & 0xFF;
303
			Str[j++] = num2hex( ( k >> 4 ) & 0xF );
304
			Str[j++] = num2hex( k & 0xF );
305
			break;
306
		// вывод символа
307
		case 'C':
308
			Str[j++] = va_arg(arglist, int) & 0xFF;
309
			break;
310
		// вывод двойного слова в шестнадцатиричном виде
311
		case 'X':
312
			Dword val;
313
			val = va_arg(arglist, Dword);
314
			for( k = 7; k >= 0; k-- )
315
			{
316
				//
317
				c = num2hex ( ( val >> (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
			break;
332
		// вывод двойного слова в десятичном виде
333
		case 'U':
334
			head = va_arg(arglist, Dword);
335
			tail = 0;
336
			for( k = 0; dectab[k] != 0; k++ )
337
			{
338
				tail = head % dectab[k];
339
				head /= dectab[k];
340
				c = head + '0';
341
				if( c == '0' )
342
				{
343
					if( flag ) Str[j++] = c;
344
				}
345
				else
346
				{
347
					flag++;
348
					Str[j++] = c;
349
				}
350
				//
351
				head = tail;
352
			}
353
			//
354
			c = head + '0';
355
			Str[j++] = c;
356
			break;
357
		// вывод 64-битного слова в шестнадцатиричном виде
358
		case 'Q':
359
			unsigned int low_dword, high_dword;
360
			low_dword = va_arg(arglist, unsigned int);
361
			high_dword = va_arg(arglist, unsigned int);
362
			for( k = 7; k >= 0; k-- )
363
			{
364
				//
365
				c = num2hex ( ( ( high_dword + 1) >> (k * 4) ) & 0xF );
366
				//
367
				if( c == '0' )
368
				{
369
					if( flag ) Str[j++] = c;
370
				}
371
				else
372
				{
373
					flag++;
374
					Str[j++] = c;
375
				}
376
			}
377
			//
378
			for( k=7; k >= 0; k-- )
379
			{
380
				//
381
				c = num2hex ( ( low_dword >> (k * 4) ) & 0xF );
382
				//
383
				if( c == '0' )
384
				{
385
					if( flag ) Str[j++] = c;
386
				}
387
				else
388
				{
389
					flag++;
390
					Str[j++] = c;
391
				}
392
			}
393
			//
394
			if( flag == 0 ) Str[j++] = '0';
395
			//
396
			break;
397
		//
398
		default:
399
			break;
400
		}
401
	}
402
	//
403
	Str[j] = 0;
404
}
405
 
406
 
407
// функция -1 завершения процесса
408
void kos_ExitApp()
409
{
410
	int i;
411
 
412
	//
413
	for ( i = atExitFnNum - 1; i >= 0; i-- )
414
	{
415
		//
416
		atExitList[i]();
417
	}
418
	//
419
	__asm{
420
		mov eax, -1
421
		int 0x40
422
	}
423
}
424
 
425
 
426
// функция 0
427
void kos_DefineAndDrawWindow(
428
	Word x, Word y,
429
	Word sizeX, Word sizeY,
430
	Byte mainAreaType,
431
	Dword mainAreaColour,
432
	Byte headerType,
433
	Dword headerColour,
434
	Dword borderColour
435
	)
436
{
437
	Dword arg1, arg2, arg3, arg4;
438
 
439
	//
440
	arg1 = ( x << 16 ) + sizeX;
441
	arg2 = ( y << 16 ) + sizeY;
442
	arg3 = ( mainAreaType << 24 ) | mainAreaColour;
443
	arg4 = ( headerType << 24 ) | headerColour;
444
	//
445
	__asm{
446
		mov eax, 0
447
		mov ebx, arg1
448
		mov ecx, arg2
449
		mov edx, arg3
450
		mov esi, arg4
451
		mov edi, borderColour
452
		int 0x40
453
	}
454
}
455
 
456
 
457
// функция 1 поставить точку
458
void kos_PutPixel( Dword x, Dword y, Dword colour )
459
{
460
	//
461
	__asm{
462
		mov eax, 1
463
		mov ebx, x
464
		mov ecx, y
465
		mov edx, colour
466
		int 0x40
467
	}
468
}
469
 
470
 
471
// функция 2 получить код нажатой клавиши
472
bool kos_GetKey( Byte &keyCode )
473
{
474
	Dword result;
475
 
476
	//
477
	__asm{
478
		mov eax, 2
479
		int 0x40
480
		mov result, eax
481
	}
482
	//
483
	keyCode = result >> 8;
484
	//
485
	return ( result & 0xFF ) == 0;
486
}
487
 
488
 
489
// функция 3 получить время
490
Dword kos_GetSystemClock()
491
{
492
//	Dword result;
493
 
494
	//
495
	__asm{
496
		mov eax, 3
497
		int 0x40
498
//		mov result, eax
499
	}
500
	//
501
//	return result;
502
}
503
 
504
 
505
// функция 4
506
void kos_WriteTextToWindow(
507
	Word x,
508
	Word y,
509
	Byte fontType,
510
	Dword textColour,
511
	char *textPtr,
512
	Dword textLen
513
	)
514
{
515
	Dword arg1, arg2;
516
 
517
	//
518
	arg1 = ( x << 16 ) | y;
519
	arg2 = ( fontType << 24 ) | textColour;
520
	//
521
	__asm{
522
		mov eax, 4
523
		mov ebx, arg1
524
		mov ecx, arg2
525
		mov edx, textPtr
526
		mov esi, textLen
527
		int 0x40
528
	}
529
}
530
 
531
 
532
// функция 5 пауза, в сотых долях секунды
533
void kos_Pause( Dword value )
534
{
535
	//
536
	__asm{
537
		mov eax, 5
538
		mov ebx, value
539
		int 0x40
540
	}
541
}
542
 
543
 
544
// функция 7 нарисовать изображение
545
void kos_PutImage( RGB * imagePtr, Word sizeX, Word sizeY, Word x, Word y )
546
{
547
	Dword arg1, arg2;
548
 
549
	//
550
	arg1 = ( sizeX << 16 ) | sizeY;
551
	arg2 = ( x << 16 ) | y;
552
	//
553
	__asm{
554
		mov eax, 7
555
		mov ebx, imagePtr
556
		mov ecx, arg1
557
		mov edx, arg2
558
		int 0x40
559
	}
560
}
561
 
562
 
563
 
564
// функция 8 определить кнопку
565
void kos_DefineButton( Word x, Word y, Word sizeX, Word sizeY, Dword buttonID, Dword colour )
566
{
567
	Dword arg1, arg2;
568
 
569
	//
570
	arg1 = ( x << 16 ) | sizeX;
571
	arg2 = ( y << 16 ) | sizeY;
572
	//
573
	__asm{
574
		mov eax, 8
575
		mov ebx, arg1
576
		mov ecx, arg2
577
		mov edx, buttonID
578
		mov esi, colour
579
		int 0x40
580
	}
581
}
582
 
583
 
584
// функция 9 - информация о процессе
585
Dword kos_ProcessInfo( sProcessInfo *targetPtr, Dword processID )
586
{
587
//	Dword result;
588
 
589
	//
590
	__asm{
591
		mov eax, 9
592
		mov ebx, targetPtr
593
		mov ecx, processID
594
		int 0x40
595
//		mov result, eax
596
	}
597
	//
598
//	return result;
599
}
600
 
601
 
602
// функция 10
603
Dword kos_WaitForEvent()
604
{
605
//	Dword result;
606
 
607
	__asm{
608
		mov eax, 10
609
		int 0x40
610
//		mov result, eax
611
	}
612
 
613
//	return result;
614
}
615
 
616
 
617
// функция 11
618
Dword kos_CheckForEvent()
619
{
620
//	Dword result;
621
 
622
	__asm{
623
		mov eax, 11
624
		int 0x40
625
//		mov result, eax
626
	}
627
 
628
//	return result;
629
}
630
 
631
 
632
// функция 12
633
void kos_WindowRedrawStatus( Dword status )
634
{
635
	__asm{
636
		mov eax, 12
637
		mov ebx, status
638
		int 0x40
639
	}
640
}
641
 
642
 
643
// функция 13 нарисовать полосу
644
void kos_DrawBar( Word x, Word y, Word sizeX, Word sizeY, Dword colour )
645
{
646
	Dword arg1, arg2;
647
 
648
	//
649
	arg1 = ( x << 16 ) | sizeX;
650
	arg2 = ( y << 16 ) | sizeY;
651
	//
652
	__asm{
653
		mov eax, 13
654
		mov ebx, arg1
655
		mov ecx, arg2
656
		mov edx, colour
657
		int 0x40
658
	}
659
}
660
 
661
 
662
// функция 17
663
bool kos_GetButtonID( Dword &buttonID )
664
{
665
	Dword result;
666
 
667
	//
668
	__asm{
669
		mov eax, 17
670
		int 0x40
671
		mov result, eax
672
	}
673
	//
674
	buttonID = result >> 8;
675
	//
676
	return (result & 0xFF) == 0;
677
}
678
 
679
 
680
// функция 23
681
Dword kos_WaitForEvent( Dword timeOut )
682
{
683
//	Dword result;
684
 
685
	__asm{
686
		mov eax, 23
687
		mov ebx, timeOut
688
		int 0x40
689
//		mov result, eax
690
	}
691
 
692
//	return result;
693
}
694
 
695
 
696
// получение информации о состоянии "мыши" функция 37
697
void kos_GetMouseState( Dword & buttons, int & cursorX, int & cursorY )
698
{
699
	Dword mB;
700
	Word curX;
701
	Word curY;
702
	sProcessInfo sPI;
703
 
704
	//
705
	__asm{
706
		mov		eax, 37
707
		mov		ebx, 1
708
		int		0x40
709
		mov		curY, ax
710
		shr		eax, 16
711
		mov		curX, ax
712
		mov		eax, 37
713
		mov		ebx, 2
714
		int		0x40
715
		mov		mB, eax
716
	}
717
	//
718
	kos_ProcessInfo( &sPI );
719
	//
720
	buttons = mB;
721
	cursorX = curX - sPI.processInfo.left;
722
	cursorY = curY - sPI.processInfo.top;
723
}
724
 
725
 
726
// функция 40 установить маску событий
727
void kos_SetMaskForEvents( Dword mask )
728
{
729
	//
730
	__asm{
731
		mov eax, 40
732
		mov ebx, mask
733
		int 0x40
734
	}
735
}
736
 
737
 
738
// функция 47 вывести в окно приложения число
739
void kos_DisplayNumberToWindow(
740
   Dword value,
741
   Dword digitsNum,
742
   Word x,
743
   Word y,
744
   Dword colour,
745
   eNumberBase nBase,
746
   bool valueIsPointer
747
   )
748
{
749
	Dword arg1, arg2;
750
 
751
	//
752
	arg1 = ( valueIsPointer ? 1 : 0 ) |
753
		( ((Byte)nBase) << 8 ) |
754
		( ( digitsNum & 0x1F ) << 16 );
755
	arg2 = ( x << 16 ) | y;
756
	//
757
	__asm{
758
		mov eax, 47
759
		mov ebx, arg1
760
		mov ecx, value
761
		mov edx, arg2
762
		mov esi, colour
763
		int 0x40
764
	}
765
}
766
 
767
 
768
// функция 70 доступ к файловой системе
769
Dword kos_FileSystemAccess( kosFileInfo *fileInfo )
770
{
771
//	Dword result;
772
 
773
	//
774
	__asm{
775
		mov eax, 70
776
		mov ebx, fileInfo
777
		int 0x40
778
//		mov result, eax
779
	}
780
	//
781
//	return result;
782
}
783
 
784
 
785
// функция 63 вывод символя в окно отладки
786
void kos_DebugOutChar( char ccc )
787
{
788
	//
789
	__asm{
790
		mov eax, 63
791
		mov ebx, 1
792
		mov cl, ccc
793
		int 0x40
794
	}
795
}
796
 
797
 
798
// функция 66 режим получения данных от клавиатуры
799
void kos_SetKeyboardDataMode( Dword mode )
800
{
801
	//
802
	__asm{
803
		mov eax, 66
804
		mov ebx, 1
805
		mov ecx, mode
806
		int 0x40
807
	}
808
}
809
 
810
 
811
// вывод строки в окно отладки
812
void rtlDebugOutString( char *str )
813
{
814
	//
815
	for ( ; str[0] != 0; str++ )
816
	{
817
		kos_DebugOutChar( str[0] );
818
	}
819
	//
820
	kos_DebugOutChar( 13 );
821
	kos_DebugOutChar( 10 );
822
}
823
 
824
 
825
// функция 64 изменение количества памяти, выделенной для программы
826
bool kos_ApplicationMemoryResize( Dword targetSize )
827
{
828
	Dword result;
829
 
830
	//
831
	__asm{
832
		mov eax, 64
833
		mov ebx, 1
834
		mov ecx, targetSize
835
		int 0x40
836
		mov result, eax
837
	}
838
	//
839
	return result == 0;
840
}
841
 
842
 
843
// функция 67 изменить параметры окна, параметр == -1 не меняется
844
void kos_ChangeWindow( Dword x, Dword y, Dword sizeX, Dword sizeY )
845
{
846
	//
847
	__asm{
848
		mov eax, 67
849
		mov ebx, x
850
		mov ecx, y
851
		mov edx, sizeX
852
		mov esi, sizeY
853
		int 0x40
854
	}
855
}
856
 
857
 
858
 
859
Byte* kos_GetMemory(Dword count)
860
{
861
  __asm
862
  {
863
    mov  eax,68
864
    mov  ebx,12
865
    mov  ecx,count
866
    int  40h
867
  }
868
}
869
 
870
Dword kos_FreeMemory(Byte* pMemory)
871
{
872
  __asm
873
  {
874
    mov  eax,68
875
    mov  ebx,13
876
    mov  ecx,pMemory
877
    int  40h
878
  }
879
}
880
 
881
// вызов абстрактного метода
882
int __cdecl _purecall()
883
{
884
	rtlDebugOutString( pureCallMessage );
885
	kos_ExitApp();
886
	return 0;
887
}
888
 
889
 
890
// вызов статических инициализаторов
891
// заодно инициализация генератора случайных чисел
892
//#pragma section(".CRT$XCA",long,read,write)
893
//#pragma section(".CRT$XCZ",long,read,write)
894
#pragma data_seg(".CRT$XCA")
895
#pragma data_seg(".CRT$XCZ")
896
typedef void (__cdecl *_PVFV)(void);
897
__declspec(allocate(".CRT$XCA"))  _PVFV __xc_a[1] = { NULL };
898
__declspec(allocate(".CRT$XCZ"))  _PVFV __xc_z[1] = { NULL };
899
//
900
#pragma comment(linker, "/merge:.CRT=.rdata")
901
//
902
void crtStartUp()
903
{
904
	// вызываем инициализаторы по списку, NULL'ы игнорируем
905
	for ( _PVFV *pbegin = __xc_a; pbegin < __xc_z; pbegin++ )
906
	{
907
		//
908
		if ( *pbegin != NULL )
909
			(**pbegin)();
910
	}
911
	// инициализируем генератор случайных чисел
912
	rtlSrand( kos_GetSystemClock() );
913
	// путь к файлу процесса
914
	kosExePath = *((char **)0x20);
915
	// вызов главной функции приложения
916
	kos_Main();
917
	// выход
918
	kos_ExitApp();
919
}
920
 
921