SHARE
TWEET

Untitled

a guest Dec 16th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //--------------------------------------------------------------------
  2. // TUCN, Computer Science Department
  3. // Input/Output Systems and Peripheral Devices
  4. //--------------------------------------------------------------------
  5. // http://users.utcluj.ro/~baruch/en/pages/teaching/inputoutput-systems/laboratory.php
  6. //--------------------------------------------------------------------
  7. // File:        AppScroll-e.cpp
  8. // Date:        10.10.2015
  9. // Modified:    21.11.2015
  10. //--------------------------------------------------------------------
  11. // IOS application example with vertical scroll bar
  12. //--------------------------------------------------------------------
  13.  
  14. #include <windows.h>
  15. #include "Hw.h"
  16. #include "ATA-ATAPI-e.h"
  17.  
  18. #define NLIN 500                                // number of lines in the display window
  19. #define NCOL 240                                // number of columns in the display window
  20.  
  21. // Global variables
  22. char szBuffer[NLIN][NCOL];                      // buffer for the window contents
  23. int  cLine;                                     // number of current line in the display buffer
  24.  
  25. // Declarations of external functions
  26. void DisplWindow(HWND hWnd);
  27.  
  28. PSATA_PCI_CFG getHeaderPCIe(BYTE bus_nr, BYTE device_nr, BYTE function_nr){
  29.  
  30.     DWORD64 qwADR = PCI_CONFIG_START;
  31.     //We need to shift the values manually according to 2.6.3
  32.     qwADR |= (bus_nr << 20);
  33.     qwADR |= (device_nr << 15);
  34.     qwADR |= (function_nr << 12);
  35.  
  36.     return (PSATA_PCI_CFG)qwADR;
  37. }
  38.  
  39. DWORD function(BYTE bus, BYTE device, BYTE function, int which){
  40.     PSATA_PCI_CFG cfg = getHeaderPCIe(bus, device, function);
  41.     WORD cmd;
  42.     WORD ctl;
  43.     DWORD ret;
  44.     if (which == 0){
  45.         // return the base addresses for the primary ATA channel of the SATA controller
  46.         cmd = LOWORD(_inmdw((DWORD_PTR)&cfg->PCMD_BAR)) & 0xFFF8;
  47.         ctl = LOWORD(_inmdw((DWORD_PTR)&cfg->PCNL_BAR)) & 0xFFFC;  
  48.     }
  49.     else if (which == 1){
  50.         // return the base addresses for the secondary ATA channel of the SATA controller
  51.         cmd = LOWORD(_inmdw((DWORD_PTR)&cfg->SCMD_BAR)) & 0xFFF8;
  52.         ctl = LOWORD(_inmdw((DWORD_PTR)&cfg->SCNL_BAR)) & 0xFFFC;
  53.     }
  54.     wsprintf(szBuffer[cLine], "Base Address of Command Block Register: %x", cmd);
  55.     cLine += 2;
  56.     wsprintf(szBuffer[cLine], "Base Address of Control Block Register: %x", ctl);
  57.     cLine += 2;
  58.     ret = ctl << 16;
  59.     ret = ret | cmd;
  60.     return ret;
  61. }
  62.  
  63. int executeDeviceDiagnostic(WORD adr, int which){
  64.     BYTE status;
  65.     int time = 0;
  66.     do{
  67.         status = __inp(adr + ATA_STATUS);
  68.         time++;
  69.     } while ((status & (STATUS_BSY | STATUS_DRQ)) != 0);
  70.  
  71.     //Clear Dev bit
  72.     BYTE device = __inp(adr + ATA_DEVICE);
  73.     device &= (~DEVICE_DEV);
  74.     __outp(adr + ATA_DEVICE, device);
  75.  
  76.     //Send command
  77.     __outp(adr + ATA_COMMAND, ATA_EXEC_DEVICE_DIAG);
  78.     Sleep(4);
  79.     time = 0;
  80.     do{
  81.         status = __inp(adr + ATA_STATUS);
  82.  
  83.         time++;
  84.     } while ((status & STATUS_BSY) != 0);
  85.  
  86.     BYTE code = __inp(adr + ATA_ERROR);
  87.     if (which == 0){
  88.         wsprintf(szBuffer[cLine], "Peimary code: %02X", code);
  89.         cLine++;
  90.     }
  91.     else{
  92.         wsprintf(szBuffer[cLine], "Secondary code: %02X", code);
  93.         cLine++;
  94.     }
  95.  
  96.     return 0;
  97. }
  98.  
  99. void idDeviceCmd(WORD cmd, WORD ctrl, int which){
  100.  
  101. }
  102.  
  103.  
  104. //--------------------------------------------------------------------
  105. // Function AppScroll
  106. //--------------------------------------------------------------------
  107. //
  108. // Function:    IOS application example with vertical scroll bar
  109. //
  110. // Parameters:  - Handle to the application window
  111. //
  112. // Returns:     0 - Operation completed successfully
  113. //              1 - Error at initializing the Hw driver
  114. //
  115. //--------------------------------------------------------------------
  116.  
  117. int AppScroll(HWND hWnd)
  118. {
  119.     int   i;
  120.  
  121.     char szMes0[] = "Error initializing the Hw driver";
  122.     char szMes1[] = "IOS Application";
  123.  
  124.     // Initialize the Hw library
  125.     if (!HwOpen()) {
  126.         wsprintf(szBuffer[0], szMes0);
  127.         MessageBox(NULL, szBuffer[0], "HwOpen", MB_ICONSTOP);
  128.         return 1;
  129.     }
  130.  
  131.     // Erase the display buffer and the window contents
  132.     for (i = 0; i < NLIN; i++) {
  133.         memset (szBuffer[i], ' ', NCOL);
  134.     }
  135.     cLine = 1;
  136.  
  137.     // Copy the start message into the display buffer and display the message
  138.     wsprintf(szBuffer[cLine], szMes1);
  139.     cLine += 2;
  140.     DisplWindow(hWnd);
  141.  
  142. //--------------------------------------------------------------------
  143. // To be completed with the application's code
  144. //--------------------------------------------------------------------
  145.  
  146.     DWORD primary = function(0, 31, 2, 0);
  147.     DWORD secondary = function(0, 31, 2, 1);
  148.  
  149.     int diagnosis = executeDeviceDiagnostic(LOWORD(primary), 0);
  150.     // Display the messages
  151.     DisplWindow(hWnd);
  152.  
  153.     HwClose();
  154.     return 0;
  155. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top