home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / demos / speedtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-27  |  4.6 KB  |  224 lines

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <vga.h> 
  6. #include <vgagl.h>
  7.  
  8.  
  9. /* #define LINEAR_ADDRESSING */
  10.  
  11.  
  12. int VGAMODE, USEGL;
  13. GraphicsContext *physicalscreen;
  14. GraphicsContext *backscreen;
  15.  
  16.  
  17. void screen1() {
  18.     int x, y;
  19.     for (y = 0; y < HEIGHT; y++)
  20.         for (x = 0; x < WIDTH; x++)
  21.             /* limited RGB palette in 256-color modes */
  22.             /* some color information is not used in */
  23.             /* 15-bit color modes */
  24.             gl_setpixelrgb(x, y,
  25.                 x * 256 / WIDTH, 
  26.                 255 - x * 256 / WIDTH,
  27.                 y * 256 / HEIGHT);
  28. }
  29.  
  30.  
  31. void configure() {
  32.     int allowed[GLASTMODE + 1];
  33.  
  34.     for (;;) {
  35.         int i;
  36.         int m;
  37.         for (i = G320x200x16; i <= GLASTMODE; i++) {
  38.             allowed[i] = 0;
  39.             if (vga_hasmode(i)) {
  40.                 printf("%2d  %s\n", i, vga_getmodename(i));
  41.                 allowed[i] = 1;
  42.             }
  43.         }
  44.  
  45.         printf("\nWhich mode? ");
  46.         scanf("%d", &m);
  47.         getchar();
  48.         printf("\n");
  49.         if (m >= G320x200x16 && m <= GLASTMODE) {
  50.             VGAMODE = m;
  51.             if (vga_getmodeinfo(m)->bytesperpixel >= 1)
  52.                 USEGL = 1;
  53.             else
  54.                 USEGL = 0;
  55.             break;
  56.         }
  57.     }
  58.  
  59.     vga_setmode(VGAMODE);
  60. #ifdef LINEAR_ADDRESSING    
  61.     vga_setlinearaddressing();
  62. #endif    
  63.     if (USEGL) {
  64.         gl_setcontextvga(VGAMODE);
  65.         physicalscreen = gl_allocatecontext();
  66.         gl_getcontext(physicalscreen);
  67.     }
  68. }
  69.  
  70.  
  71. void blit() {
  72.     gl_clearscreen(0x86);
  73.     vga_imageblt(vga_getgraphmem(), 0, WIDTH - 128, HEIGHT - 128,
  74.         WIDTH * BYTESPERPIXEL);
  75. /*    vga_bitblt(0, 100 * WIDTH * BYTESPERPIXEL, 50, 50, WIDTH * BYTESPERPIXEL);
  76.     vga_fillblt(100 * BYTESPERPIXEL, 50, 50, WIDTH * BYTESPERPIXEL, 0x86);
  77. */
  78. }
  79.  
  80.  
  81. #if 1
  82.  
  83. /* May help on well-designed motherboards. */
  84.  
  85. /* IT DOES MAKE A DIFFERENCE! REP STOSL IS SLOWER */
  86. /* (CL-GD5434 VLB zero-wait writes -- 2/3 cycles); rep stos takes 4 */
  87.  
  88. static inline void *__memset( void *s, char c, size_t count ) {
  89. __asm__(
  90.     "cld\n\t"
  91.     "cmpl $12,%%edx\n\t"
  92.     "jl 1f\n\t"            /* if (count >= 12) */
  93.  
  94.     "movzbl %%al,%%ax\n\t"
  95.     "movl %%eax,%%ecx\n\t"
  96.     "shll $8,%%ecx\n\t"        /* c |= c << 8 */
  97.     "orl %%ecx,%%eax\n\t"
  98.     "movl %%eax,%%ecx\n\t"
  99.     "shll $16,%%ecx\n\t"        /* c |= c << 16 */
  100.     "orl %%ecx,%%eax\n\t" 
  101.  
  102.     "movl %%edx,%%ecx\n\t"
  103.     "negl %%ecx\n\t"
  104.     "andl $3,%%ecx\n\t"        /* (-s % 4) */
  105.     "subl %%ecx,%%edx\n\t"        /* count -= (-s % 4) */
  106.     "rep ; stosb\n\t"        /* align to longword boundary */
  107.  
  108.     "movl %%edx,%%ecx\n\t"
  109.     "shrl $2,%%ecx\n\t"
  110.     
  111.     "cmpl $32,%%ecx\n\t"        /* do loop unrolling for */
  112.     "jl 2f\n\t"            /* chunks of 128 bytes */
  113.     "jmp 3f\n\t"
  114.     ".align 4,0x90\n\t"
  115.  
  116.     "3:\n\t"
  117.     "movl %%eax,(%%edi)\n\t"
  118.     "movl %%eax,4(%%edi)\n\t"
  119.     "movl %%eax,8(%%edi)\n\t"
  120.     "movl %%eax,12(%%edi)\n\t"
  121.     "movl %%eax,16(%%edi)\n\t"
  122.     "movl %%eax,20(%%edi)\n\t"
  123.     "movl %%eax,24(%%edi)\n\t"
  124.     "movl %%eax,28(%%edi)\n\t"
  125.     "movl %%eax,32(%%edi)\n\t"
  126.     "movl %%eax,36(%%edi)\n\t"
  127.     "movl %%eax,40(%%edi)\n\t"
  128.     "movl %%eax,44(%%edi)\n\t"
  129.     "movl %%eax,48(%%edi)\n\t"
  130.     "movl %%eax,52(%%edi)\n\t"
  131.     "movl %%eax,56(%%edi)\n\t"
  132.     "movl %%eax,60(%%edi)\n\t"
  133.     "movl %%eax,64(%%edi)\n\t"
  134.     "movl %%eax,68(%%edi)\n\t"
  135.     "movl %%eax,72(%%edi)\n\t"
  136.     "movl %%eax,76(%%edi)\n\t"
  137.     "movl %%eax,80(%%edi)\n\t"
  138.     "movl %%eax,84(%%edi)\n\t"
  139.     "movl %%eax,88(%%edi)\n\t"
  140.     "movl %%eax,92(%%edi)\n\t"
  141.     "movl %%eax,96(%%edi)\n\t"
  142.     "movl %%eax,100(%%edi)\n\t"
  143.     "movl %%eax,104(%%edi)\n\t"
  144.     "movl %%eax,108(%%edi)\n\t"
  145.     "subl $32,%%ecx\n\t"
  146.     "movl %%eax,112(%%edi)\n\t"
  147.     "movl %%eax,116(%%edi)\n\t"
  148.     "movl %%eax,120(%%edi)\n\t"
  149.     "movl %%eax,124(%%edi)\n\t"
  150.     "addl $128,%%edi\n\t"
  151.     "cmpl $32,%%ecx\n\t"
  152.     "jge 3b\n\t"
  153.  
  154.     "2:\n\t"
  155.     "rep ; stosl\n\t"        /* fill remaining longwords */
  156.  
  157.     "andl $3,%%edx\n"        /* fill last few bytes */
  158.     "1:\tmovl %%edx,%%ecx\n\t"    /* <= 12 entry point */
  159.     "rep ; stosb\n\t"
  160.     ::"a" (c),"D" (s),"d" (count)
  161.     :"ax","cx","dx","di");
  162. return s;
  163. }
  164.  
  165. #define memset __memset
  166.  
  167. #endif
  168.  
  169.  
  170. void speed() {
  171.     int i;
  172.     int start_clock;
  173.     int finish_clock;
  174.     int diff_clock;
  175.     unsigned char *vgabase = vga_getgraphmem();
  176.  
  177. #ifndef LINEAR_ADDRESSING
  178.     if (VGAMODE >= G640x480x256)
  179.         vga_setpage(0);
  180. #endif        
  181.  
  182.     start_clock = clock();
  183.  
  184.     for (i = 0; i < 500; i++) {
  185.         memset(vgabase, i & 255, 65536);
  186. /*        vga_imageblt(backscreen.vbuf, 0x100000, 256, 256, WIDTH); */
  187. /*        vga_bitblt(WIDTH * BYTESPERPIXEL * 256 + (i & 63), 0, 256,
  188.             256, WIDTH * BYTESPERPIXEL); */
  189. /*        vga_fillblt(0, 256, 256, WIDTH * BYTESPERPIXEL, i & 255); */
  190.     }
  191.  
  192.     finish_clock = clock();
  193.  
  194.     diff_clock = finish_clock - start_clock;
  195.     printf("Timing: %3d.%1ds, %dK/s\n", diff_clock / 100, 
  196.         (diff_clock % 100) / 10, 3200000 / diff_clock);
  197. }
  198.  
  199.  
  200. main()
  201. {
  202.     vga_init();
  203.  
  204.     printf("This is a video memory speed tester. Note that the first "
  205.         "screen doesn't test\nanything (nor does the 3 second pause "
  206.         "that follows).\n\n");
  207.  
  208.     configure();
  209.  
  210.     if (COLORS == 256);
  211.         gl_setrgbpalette();    /* set RGB palette */
  212.  
  213.     if (USEGL)
  214.         screen1();
  215.     sleep(2);
  216.  
  217. /*    vga_screenoff(); */
  218.  
  219.     speed();
  220.  
  221.        vga_setmode(TEXT);
  222.        exit(0);
  223. }
  224.