Subversion Repositories Kolibri OS

Rev

Rev 1005 | Details | Compare with Previous | Last modification | View Log | RSS feed

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