home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_8 / vblank.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-20  |  2.9 KB  |  124 lines

  1.  
  2. // VBLANK.C - demo of the vertical blank interrupt supported by a few VGA cards
  3.  
  4. // I N C L U D E S ///////////////////////////////////////////////////////////
  5.  
  6. #include <io.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include <bios.h>
  12. #include <fcntl.h>
  13. #include <memory.h>
  14. #include <malloc.h>
  15. #include <math.h>
  16. #include <string.h>
  17.  
  18. #include "black3.h"
  19. #include "black8.h"
  20.  
  21. // D E F I N E S /////////////////////////////////////////////////////////////
  22.  
  23. #define VERTICAL_BLANK_INTERRUPT    0x0A   // index of vblank in vector table
  24. #define CRT_VERTICAL_END            0x11   // register with vblank enable bits
  25.  
  26. // G L O B A L S  ////////////////////////////////////////////////////////////
  27.  
  28. void (_interrupt far *Old_Vertical)();
  29.  
  30. // F U N C T I O N S /////////////////////////////////////////////////////////
  31.  
  32. void _interrupt _far Vertical_Blank(void)
  33. {
  34. // this function is the vertical blank handler
  35.  
  36. static int count = 0; // used to count the vertical blanks
  37. char buffer[64];      // used to print a string
  38. unsigned char data;   // used to read data
  39.  
  40. _asm sti ; // re-enable interrupts
  41.  
  42. // reset latch on VGA
  43.  
  44. outp(CRT_CONTROLLER,CRT_VERTICAL_END);
  45.  
  46. data = inp(CRT_CONTROLLER+1);
  47.  
  48. data = RESET_BITS(data,0x10);
  49.  
  50. outp(CRT_CONTROLLER+1,data);
  51.  
  52. // do whatever you want here, but make it quick!
  53.  
  54. sprintf(buffer,"Number of vertical blanks is %d",count++);
  55. Print_String(0,0,10,buffer,1);
  56.  
  57. // end process vertical blank
  58.  
  59. // re-enable interrupts on PIC, send end of interrupt command EOI, 20h
  60.  
  61. outp(PIC_ICR, PIC_EOI);
  62.  
  63. } // end Vertical_Blank
  64.  
  65. // M A I N ///////////////////////////////////////////////////////////////////
  66.  
  67. void main(void)
  68. {
  69. unsigned char data;
  70.  
  71. // set the graphics mode to mode 13h
  72.  
  73. Set_Graphics_Mode(GRAPHICS_MODE13);
  74.  
  75. // save old vertical blank interrupt
  76.  
  77. Old_Vertical = _dos_getvect(VERTICAL_BLANK_INTERRUPT);
  78.  
  79. // set new interrupt
  80.  
  81. _dos_setvect(VERTICAL_BLANK_INTERRUPT,Vertical_Blank);
  82.  
  83. // enable interrupt on VGA
  84.  
  85. outp(CRT_CONTROLLER,CRT_VERTICAL_END);
  86. data = (unsigned char)inp(CRT_CONTROLLER+1);
  87.  
  88. data = RESET_BITS(data,0x20);   // for your VGA you may need to SET this bit
  89. outp(CRT_CONTROLLER+1,data);
  90.  
  91. data = RESET_BITS(data,0x10);
  92. outp(CRT_CONTROLLER+1,data);
  93.  
  94. // enable interrupt on PIC, i.e. enable IRQ 2
  95.  
  96. data = (unsigned char)inp(PIC_IMR);
  97. data = RESET_BITS(data,0x40);
  98. outp(PIC_IMR,data);
  99.  
  100. while(!kbhit());
  101.  
  102. // disable interrupts from VGA
  103.  
  104. outp(CRT_CONTROLLER,CRT_VERTICAL_END);
  105. data =(unsigned char)inp(CRT_CONTROLLER+1);
  106.  
  107. data = SET_BITS(data,0x20);    // for your VGA you may need to reset this bit
  108. outp(CRT_CONTROLLER+1,data);
  109.  
  110. // disable vertical blank interrupt on PIC
  111.  
  112. data =(unsigned char)inp(PIC_IMR);
  113. data = SET_BITS(data,0x04);
  114. outp(PIC_IMR,data);
  115.  
  116. // restore old vector
  117.  
  118. _dos_setvect(VERTICAL_BLANK_INTERRUPT,Old_Vertical);
  119.  
  120. Set_Graphics_Mode(TEXT_MODE);
  121.  
  122. } // end main
  123.  
  124.