Advertisement
Guest User

CP/M-86 Aztec C 3.2d VGA TEST pixel.c

a guest
Sep 30th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.07 KB | None | 0 0
  1. /**************************************************************************
  2.  * pixel.c                                                                *
  3.  * written by David Brackeen                                              *
  4.  * http://www.brackeen.com/home/vga/                                      *
  5.  *                                                                        *
  6.  * This is a 16-bit program.                                              *
  7.  * Tab stops are set to 2.                                                *
  8.  * Remember to compile in the SMALL memory model!                         *
  9.  * To compile in CP/M-86 Aztec C 3.2d                                     *
  10.  *      cc pixel.c                                                        *
  11.  *      ln pixel.o m.lib c.lib                                            *
  12.  *                                                                        *
  13.  * This program will only work on CP/M86 systems with a                   *
  14.  * VGA, SuperVGA or compatible video adapter.                             *
  15.  *                                                                        *
  16.  * Please feel free to copy this source code.                             *
  17.  *                                                                        *
  18.  * DESCRIPTION: This program demostrates how much faster writing directly *
  19.  * to video memory is.                                                    *
  20.  **************************************************************************/
  21.  
  22. #include <stdio.h>
  23. /*
  24. #include <stdlib.h>
  25. #include <dos.h>
  26. */
  27. #include <regs.h>
  28. #include <math.h>
  29.  
  30. #define VIDEO_INT           0x10      /* the BIOS video interrupt. */
  31. #define WRITE_DOT           0x0C      /* BIOS func to plot a pixel. */
  32. #define SET_MODE            0x00      /* BIOS func to set the video mode. */
  33. #define VGA_256_COLOR_MODE  0x13      /* use to set 256-color mode. */
  34. #define TEXT_MODE           0x03      /* use to set 80x25 text mode. */
  35.  
  36. #define SCREEN_WIDTH        320       /* width in pixels of mode 0x13 */
  37. #define SCREEN_HEIGHT       200       /* height in pixels of mode 0x13 */
  38. #define NUM_COLORS          256       /* number of colors in mode 0x13 */
  39.  
  40. typedef unsigned char  byte;
  41. typedef unsigned short word;
  42.  
  43. /* byte *VGA=(byte *)0xA0000000L; */       /* this points to video memory. */
  44. word VGA_SEG = 0xa000;
  45.  
  46. /* word *my_clock=(word *)0x0000046C;  */    /* this points to the 18.2hz system
  47.                                          clock. */
  48. word my_clk_seg = 0x0000;
  49. word my_clk_ofst = 0x046c;
  50.  
  51. word peekw();
  52.  
  53. int rand(){
  54.     int a;
  55.     a = (int)(ran() * 32767.0);
  56.     if (a < 0) a = -a;
  57.     return a;
  58. }
  59.  
  60. /**************************************************************************
  61.  *  set_mode                                                              *
  62.  *     Sets the video mode.                                               *
  63.  **************************************************************************/
  64.  
  65. void set_mode(mode)
  66. byte mode;
  67. {
  68.   union REGS regs;
  69.  
  70.   regs.h.ah = SET_MODE;
  71.   regs.h.al = mode;
  72.   int86(VIDEO_INT, &regs, &regs);
  73. }
  74.  
  75. /**************************************************************************
  76.  *  plot_pixel_slow                                                       *
  77.  *    Plot a pixel by using BIOS function 0x0C (Write Dot).               *
  78.  **************************************************************************/
  79.  
  80. void plot_pixel_slow(x, y, color)
  81. int x;
  82. int y;
  83. byte color;
  84. {
  85.   union REGS regs;
  86.  
  87.   regs.h.ah = WRITE_DOT;
  88.   regs.h.al = color;
  89.   regs.x.cx = x;
  90.   regs.x.dx = y;
  91.   int86(VIDEO_INT, &regs, &regs);
  92. }
  93.  
  94. /**************************************************************************
  95.  *  plot_pixel_fast                                                       *
  96.  *    Plot a pixel by directly writing to video memory.                   *
  97.  **************************************************************************/
  98.  
  99. void plot_pixel_fast(x, y, color)
  100. int x;
  101. int y;
  102. byte color;
  103. {
  104. /*  VGA[y*SCREEN_WIDTH+x]=color; */
  105.   pokeb(y * SCREEN_WIDTH + x, VGA_SEG, color);
  106.  
  107. }
  108.  
  109. /**************************************************************************
  110.  *  Main                                                                  *
  111.  *    Plots 50000 pixels two different ways: using the BIOS and by        *
  112.  *    directly writing to video memory.                                   *
  113.  **************************************************************************/
  114.  
  115. main()
  116. {
  117.   int x,y,color;
  118.   float t1,t2;
  119.   word i,start;
  120.  
  121. /*  srand(*my_clock); */                  /* seed the number generator. */
  122.   set_mode(VGA_256_COLOR_MODE);       /* set the video mode. */
  123.  
  124. /*  start=*my_clock; */                   /* record the starting time. */
  125.   start = peekw(my_clk_ofst, my_clk_seg);
  126.  
  127.   for(i=0;i<50000L;i++)               /* randomly plot 50000 pixels. */
  128.   {
  129.     x=rand()%SCREEN_WIDTH;
  130.     y=rand()%SCREEN_HEIGHT;
  131.     color=rand()%NUM_COLORS;
  132.     plot_pixel_slow(x,y,color);
  133.   }
  134.  
  135. /*  t1=(*my_clock-start)/18.2; */         /* calculate how long it took. */
  136.   t1 = (peekw(my_clk_ofst, my_clk_seg) - start)/18.2;
  137.  
  138.   set_mode(VGA_256_COLOR_MODE);       /* set the video mode again in order
  139.                                          to clear the screen. */
  140.  
  141. /*  start=*my_clock; */                   /* record the starting time. */
  142.   start = peekw(my_clk_ofst, my_clk_seg);
  143.  
  144.   for(i=0;i<50000L;i++)               /* randomly plot 50000 pixels. */
  145.   {
  146.     x=rand()%SCREEN_WIDTH;
  147.     y=rand()%SCREEN_HEIGHT;
  148.     color=rand()%NUM_COLORS;
  149.     plot_pixel_fast(x,y,color);
  150.   }
  151.  
  152. /*  t2=(*my_clock-start)/18.2; */          /* calculate how long it took. */
  153.   t2 = (peekw(my_clk_ofst, my_clk_seg) - start)/18.2;
  154.  
  155.   set_mode(TEXT_MODE);                /* set the video mode back to
  156.                                          text mode. */
  157.   /* output the results... */
  158.  
  159.   printf("\n");
  160.   printf("Slow pixel plotting took %f seconds.\n",t1);
  161.   printf("Fast pixel plotting took %f seconds.\n",t2);
  162.   if (t2 != 0) printf("Fast pixel plotting was %f times faster.\n",t1/t2);
  163.  
  164.   return 0;
  165.  
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement