Subversion Repositories Kolibri OS

Rev

Rev 1805 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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