VXDISR.C
上传用户:lx1888888
上传日期:2007-01-04
资源大小:136k
文件大小:4k
源码类别:

驱动编程

开发平台:

Visual C++

  1. #include <basedef.h>
  2. #include <vmm.h>
  3. #include <debug.h>
  4. #include <vxdwraps.h>
  5. #include <vpicd.h>
  6. #include <vxdcall.h>
  7. #include <wrappers.h>
  8. #include <intrinsi.h>
  9. #define RTC_IRQ 8
  10. #define RTC_STATUSA 0xA
  11. #define RTC_STATUSB 0xB
  12. #define RTC_STATUSC 0xC
  13. #define STATUSB_ENINT 0x40
  14. #define CMOS_ADDR    0x70
  15. #define CMOS_DATA    0x71
  16. typedef struct
  17. {
  18.    VPICD_IRQ_DESCRIPTOR descIrq;
  19.    IRQHANDLE    hndIrq;
  20.    EVENTHANDLE  hEvent;
  21.    DWORD       EventCounter;
  22.    BYTE StatusA;
  23.    BYTE StatusB;
  24. } DEVICE_CONTEXT;
  25. DEVICE_CONTEXT rtc;
  26. BOOL OnDeviceInit(VMHANDLE hVM);
  27. void OnSystemExit(VMHANDLE hVM);
  28. BOOL _stdcall HwIntProcHandler(VMHANDLE hVM, IRQHANDLE hIRQ, void *Refdata);
  29. VOID _stdcall EventHandler(VMHANDLE hVM, PVOID Refdata, CRS *pRegs);
  30. void CmosWriteReg( BYTE reg, BYTE val );
  31. BYTE CmosReadReg( BYTE reg );
  32. // functions in asm module
  33. void EventThunk( void );
  34. void HwIntProcThunk( void );
  35. BOOL OnSysDynamicDeviceInit(VMHANDLE hVM)
  36. {
  37.    OnDeviceInit( hVM );
  38.    return TRUE;
  39. }
  40. BOOL OnSysDynamicDeviceExit(void)
  41. {
  42.    OnSystemExit(Get_Cur_VM_Handle() );
  43.    return TRUE;
  44. }
  45. BOOL OnDeviceInit(VMHANDLE hVM)
  46. {
  47. rtc.descIrq.VID_IRQ_Number = RTC_IRQ;
  48. rtc.descIrq.VID_Options = VPICD_OPT_REF_DATA;
  49. rtc.descIrq.VID_Hw_Int_Ref = &rtc;
  50. rtc.descIrq.VID_Hw_Int_Proc = (ULONG)HwIntProcThunk;
  51. rtc.descIrq.VID_EOI_Proc = 
  52. rtc.descIrq.VID_Virt_Int_Proc = 
  53. rtc.descIrq.VID_Mask_Change_Proc = 
  54. rtc.descIrq.VID_IRET_Proc = 0;
  55. rtc.descIrq.VID_IRET_Time_Out = 500;
  56. if (!(rtc.hndIrq = VPICD_Virtualize_IRQ(&rtc.descIrq)))
  57. return FALSE;
  58. rtc.StatusA = CmosReadReg(RTC_STATUSA);
  59. rtc.StatusB = CmosReadReg(RTC_STATUSB);
  60. // set interrupt frequency to only 2 times a sec
  61. CmosWriteReg(RTC_STATUSA, rtc.StatusA | 0x0F );
  62. // enable clock interrupts
  63. CmosWriteReg(RTC_STATUSB, rtc.StatusB | STATUSB_ENINT);
  64. // clear flags 
  65. CmosReadReg(RTC_STATUSC);
  66. rtc.EventCounter = 0;
  67. VPICD_Physically_Unmask(rtc.hndIrq);
  68. return TRUE;
  69. }
  70. VOID OnSystemExit(VMHANDLE hVM)
  71. {
  72. CmosWriteReg(RTC_STATUSA, rtc.StatusA );         
  73. CmosWriteReg(RTC_STATUSB, rtc.StatusB );
  74.    Cancel_Global_Event(rtc.hEvent);
  75. VPICD_Physically_Mask(rtc.hndIrq);
  76. VPICD_Force_Default_Behavior(rtc.hndIrq);
  77. }
  78. BOOL __stdcall HwIntProcHandler(VMHANDLE hVM, IRQHANDLE hIRQ, void *Refdata)
  79. {
  80.    DEVICE_CONTEXT *pRtc = (DEVICE_CONTEXT *)Refdata;
  81. Out_Debug_String("ISRrn");
  82. CmosReadReg( RTC_STATUSC );
  83. VPICD_Phys_EOI(hIRQ); // tell VPICD to clear the interrupt
  84. pRtc->hEvent = Schedule_Global_Event(EventThunk, (ULONG)pRtc );
  85. return TRUE;      // thunk will clear carry
  86. }
  87. VOID __stdcall EventHandler(VMHANDLE hVM, PVOID Refdata, CRS* pRegs)
  88. {
  89.    DEVICE_CONTEXT *rtc = (DEVICE_CONTEXT *)Refdata;
  90. rtc->hEvent = 0;
  91.    rtc->EventCounter++;
  92. }
  93. BYTE CmosReadReg( BYTE reg )
  94. {
  95. BYTE data;
  96. _asm 
  97. {
  98. ; disable NMI then ints
  99. mov al, reg
  100. or al, 80h     
  101. cli
  102. ; first output reg to address port
  103. out CMOS_ADDR, al
  104.       jmp   _1
  105. _1:
  106.       jmp   _2
  107. _2:   
  108. ; then read data from data port
  109. in al, CMOS_DATA
  110. mov data, al
  111.       jmp   _3
  112. _3:
  113.       jmp   _4
  114. _4:
  115. ; reenable NMI then ints 
  116. xor al, al 
  117. out CMOS_ADDR, al
  118. sti
  119. }
  120. return data;
  121. }
  122. void CmosWriteReg( BYTE reg, BYTE val )
  123. {
  124. _asm 
  125. {
  126. ; disable NMI then ints
  127. mov al, reg
  128. or al, 80h
  129. cli
  130. ; first output reg to address port
  131. out CMOS_ADDR, al
  132.       jmp   __1
  133. __1:
  134.       jmp   __2
  135. __2:
  136. ; then output val to data port
  137.       mov   al, val
  138. out  CMOS_DATA, al
  139.       jmp   __3
  140. __3:
  141.       jmp   __4
  142. __4:
  143. ; reenable NMI then ints 
  144. xor al, al 
  145. out CMOS_ADDR, al
  146. sti
  147. }
  148. }