home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / c2p.lha / C2P / CPU_Only / c2p_020_test.c < prev    next >
C/C++ Source or Header  |  1994-07-01  |  6KB  |  298 lines

  1. // set tabs to 4
  2.  
  3. #define WIDTH    320            // MUST be a multiple of 32
  4. #define HEIGHT    200
  5.  
  6. struct TagItem TagArray[] = {
  7.     // can add other tags here
  8.     TAG_DONE,0
  9. };
  10.  
  11. // screen related variables ---------------------
  12.  
  13. struct Screen *s;
  14.  
  15. char perm[8] = {0, 1, 2, 3, 4, 5, 6, 7};    // bitplane order
  16.  
  17. PLANEPTR raster;            // 8 contiguous bitplanes
  18. struct BitMap bitmap_bm;    // The full depth-8 bitmap
  19.  
  20.  
  21. struct ExtNewScreen NewScreenStructure = {
  22.     0,0,
  23.     WIDTH,HEIGHT,
  24.     8,                // depth
  25.     0,1,
  26.     NULL,
  27.     CUSTOMSCREEN+CUSTOMBITMAP+SCREENQUIET+NS_EXTENDED,
  28.     NULL,
  29.     NULL,
  30.     NULL,
  31.     &bitmap_bm,
  32.     (struct TagItem *)&TagArray
  33. };
  34.  
  35.  
  36. // external function prototypes -----------------
  37.  
  38. void __asm c2p_020(    register __a0 UBYTE *chunky,
  39.                     register __a1 PLANEPTR raster );
  40.  
  41. // internal function prototypes -----------------
  42.  
  43. long get_timer(void);
  44. void free_timer(void);
  45. long get_chunky_mem(void);
  46. void free_chunky_mem(void);
  47. void init_chunky(void);
  48. long get_screen(void);
  49. void free_screen(void);
  50.  
  51.  
  52. // library bases --------------------------------
  53.  
  54. struct DosLibrary        *DOSBase;
  55. struct IntuitionBase    *IntuitionBase;
  56. struct ExecBase            *SysBase;
  57. struct GfxBase            *GfxBase;
  58.  
  59. struct Library            *TimerBase;
  60. struct Library            *MathIeeeDoubBasBase;
  61.  
  62. // timer related variables ----------------------
  63.  
  64. struct timerequest    timerio_m;
  65. struct EClockVal    time0_m;
  66. struct EClockVal    time1_m;
  67.  
  68. struct timerequest    *timerio = &timerio_m;
  69. struct EClockVal    *time0    = &time0_m;
  70. struct EClockVal    *time1    = &time1_m;
  71.  
  72. ULONG timerclosed = TRUE;
  73. double micros_per_eclock;        // Length of EClock tick in microseconds
  74.  
  75. // chunky data ----------------------------------
  76.  
  77. UBYTE *chunky;        // chunky data (preferably in fast ram)
  78.  
  79.  
  80. #define nokick    "This needs Kickstart 3.0!\n"
  81. #define REPEAT_COUNT 10
  82.  
  83. long __saveds main(void)
  84. {
  85.     int count;
  86.     double micros, sum_micros;
  87.  
  88.     SysBase = *(struct ExecBase **)4;
  89.  
  90.     if(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library",33))
  91.     {
  92.         // check what kickstart version we are using
  93.         // inform the user and exit if lower than 39
  94.  
  95.         if( DOSBase->dl_lib.lib_Version < 39)
  96.         {
  97.             Write(Output(), nokick, sizeof(nokick) );
  98.             CloseLibrary( (struct Library *) DOSBase);
  99.             return(0);
  100.         }
  101.  
  102.  
  103.         // if compiling with 68020+ code, exit before we crash
  104.         // a 68000 machine
  105.  
  106.  
  107.         #ifdef _M68020
  108.         if(! ( SysBase->AttnFlags & AFF_68020) )
  109.         {
  110.             Printf("This version needs at least a 68020!\n");
  111.             return(0);
  112.         }
  113.         #endif
  114.  
  115.  
  116.         if(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",39))
  117.         if(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",39))
  118.         {
  119.  
  120.             if( get_timer() )
  121.             if( get_screen() )
  122.             if( get_chunky_mem() )
  123.             {
  124.                 Printf ("\nWidth = %ld, Height = %ld, Depth = 8\n\n",WIDTH, HEIGHT );
  125.         
  126.                 sum_micros = 0.0;
  127.  
  128.     
  129.                 // time each c2p call and average it out over 10 calls
  130.  
  131.                 for (count = 0; count < REPEAT_COUNT; count++)
  132.                 {
  133.             
  134.                     Forbid();
  135.  
  136.                     // fill the chunky buffer with a distinct pattern and a triangle
  137.                     init_chunky();
  138.  
  139.                     ReadEClock(time0);
  140.  
  141.                     c2p_020(chunky,raster);        // c2p_020() destroys given
  142.                                                 // chunky buffer - need to
  143.                                                 // render a new frame for
  144.                                                 // each c2p call
  145.  
  146.                     ReadEClock (time1);
  147.                     Permit();
  148.             
  149.                     micros = (time1->ev_lo - time0->ev_lo) * micros_per_eclock;
  150.                     sum_micros += micros;
  151.             
  152.                     Printf (" %8ld : %9ld µs\n", count, (long)micros);
  153.  
  154.                 }
  155.  
  156.  
  157.                 Printf ("\nMean time = %9ld microseconds\n\n", (long)(sum_micros / REPEAT_COUNT) );
  158.  
  159.             }
  160.  
  161.             free_chunky_mem();
  162.             free_screen();
  163.             free_timer();
  164.  
  165.             CloseLibrary((struct Library *)GfxBase);
  166.  
  167.         }
  168.  
  169.         if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  170.  
  171.         CloseLibrary((struct Library *)DOSBase);
  172.     }
  173.  
  174.     return(0);
  175.  
  176. }
  177.  
  178.  
  179. // open timer.device and the math library -------
  180.  
  181. long get_timer(void)
  182. {
  183.     long ok = 0;
  184.  
  185.     if(MathIeeeDoubBasBase = OpenLibrary("mathieeedoubbas.library",33))
  186.     if( ! (timerclosed = OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)timerio, 0)))
  187.     {
  188.         TimerBase = (struct Library *)timerio->tr_node.io_Device;
  189.         micros_per_eclock = 1000000.0 / (double)ReadEClock (time0);
  190.  
  191.         ok = 1;
  192.     }
  193.  
  194.     return(ok);
  195.  
  196. }
  197.  
  198. void free_timer(void)
  199. {
  200.     if(!timerclosed)
  201.         CloseDevice( (struct IORequest *) timerio);
  202.  
  203.     if(MathIeeeDoubBasBase)
  204.         CloseLibrary(MathIeeeDoubBasBase);
  205. }
  206.  
  207.  
  208. // get memory for chunky buffer -----------------
  209.  
  210. long get_chunky_mem(void)
  211. {
  212.     long ok = 0, size = WIDTH * HEIGHT;
  213.  
  214.     if( chunky = AllocVec(size, MEMF_CLEAR+MEMF_ANY))
  215.     {
  216.         ok = 1;
  217.     }
  218.  
  219.     return(ok);
  220.  
  221. }
  222.  
  223. void free_chunky_mem(void)
  224. {
  225.     if(chunky)
  226.         FreeVec(chunky);
  227. }
  228.  
  229.  
  230. // Write a distinctive pattern to chunky buffer and a triangle
  231.  
  232. #define write_pixel(x,y,p) (chunky[y * WIDTH + x] = p )
  233.  
  234. void init_chunky(void)
  235. {
  236.     int i, j;
  237.     UBYTE *p;
  238.  
  239.     p = chunky;
  240.     for (j = 0; j < HEIGHT; j++)
  241.     for (i = 0; i < WIDTH; i++)
  242.     *p++ = (i + j) & 255;
  243.  
  244.     // Draw a triangle to check orientation
  245.  
  246.     for (i = 50; i < 150; i++)
  247.     {
  248.         write_pixel (i, 150, i);
  249.         write_pixel (i+120, 150, i);
  250.  
  251.         write_pixel (50, i, i);
  252.         write_pixel (170, i, i);
  253.  
  254.         write_pixel (i, i, i);
  255.         write_pixel (i+120, i, i);
  256.     }
  257. }
  258.  
  259.  
  260. // get bitplanes and open screen  ---------------
  261.  
  262. long get_screen(void)
  263. {
  264.     long depth, ok = 0;
  265.  
  266.     InitBitMap(&bitmap_bm, 8, WIDTH, HEIGHT);    // Full depth-8 bm
  267.  
  268.     // since c2p_020() needs 8 contiguous bitplanes, it is not
  269.     // possible to just use OpenScreen() or AllocBitmap() as they
  270.     // may give the bitplanes in a few chunks of memory (noncontiguous)
  271.  
  272.     if( raster = (PLANEPTR)AllocRaster (WIDTH, 8 * HEIGHT))
  273.     {
  274.         for(depth = 0; depth < 8; depth++)
  275.             bitmap_bm.Planes[depth] = raster + perm[depth] * RASSIZE (WIDTH, HEIGHT);
  276.  
  277.         
  278.         if(s = OpenScreen( (struct NewScreen *) &NewScreenStructure))
  279.         {
  280.             SetRast(&s->RastPort, 0);        // clear screen memory
  281.             WaitBlit();                        // wait until it's finished
  282.     
  283.             ok = 1;
  284.         }
  285.     }
  286.  
  287.     return(ok);
  288.  
  289. }
  290.  
  291. void free_screen(void)
  292. {
  293.     if(s) CloseScreen(s);
  294.  
  295.     if(raster) FreeRaster (raster, WIDTH, 8 * HEIGHT);
  296.  
  297. }
  298.