Subversion Repositories Kolibri OS

Rev

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

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