home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Examples / DriverKit / S3_IOVPCode / s3.vp < prev    next >
Encoding:
Text File  |  1993-08-28  |  9.9 KB  |  528 lines

  1. // Copyright (c) 1993 NeXT Computer, Inc.  All rights reserved. 
  2. //
  3. // s3.vp - S3 928 video driver P-code.
  4. //
  5. // HISTORY
  6. //   29 July 1993    Derek B Clegg
  7. //    Created.
  8. //
  9. //
  10.  
  11. #define S3_XCRTC_COUNT        48
  12.  
  13. #define S3_CHIP_ID_INDEX    0x30    // Chip ID/REV register.
  14. #define S3_CHIP_ID_MASK        0xF0
  15. #define S3_CHIP_ID_805        0xA0
  16. #define S3_CHIP_ID_928        0x90
  17. #define S3_REVISION_MASK    0x0F
  18.  
  19. #define S3_LAW_CTL        0x58    // Linear address window control
  20.                     // register.
  21. #define S3_LAW_SIZE_MASK    0x03
  22. #define S3_LAW_SIZE_64K        0x00
  23. #define S3_LAW_SIZE_1M        0x01
  24. #define S3_LAW_SIZE_2M        0x02
  25. #define S3_LAW_SIZE_4M        0x03
  26.  
  27. #define RS_00        0x3C8
  28. #define RS_01        0x3C9
  29. #define RS_02        0x3C6
  30. #define RS_03        0x3C7
  31.  
  32. #define DAC_Unknown    -1
  33. #define DAC_Bt484    0x40
  34. #define DAC_Bt485    0x80
  35. #define DAC_Bt485A    0x20
  36.  
  37.     .text
  38.  
  39. // Initialize the mode.
  40.  
  41. getDisplayInfo:
  42.     debug                // Turn on debugging info.
  43.     load    @modeWidth, s0
  44.     load    @modeHeight, s1
  45.     load    @modeTotalWidth, s2
  46.     load    @modeRowBytes, s3
  47.     load    @modeRefresh, s4
  48.     load    @modeBitsPerPixel, s5
  49.     load    @modeColorSpace, s6
  50.     load    @modeFlags, s7
  51.     return
  52.  
  53. getPixelEncoding:
  54.     debug                // Turn on debugging info.
  55.     load    pixelEncoding, r0
  56.     load    @r0, s0
  57.     add    1, r0, r0
  58.     load    @r0, s1
  59.     add    1, r0, r0
  60.     load    @r0, s2
  61.     add    1, r0, r0
  62.     load    @r0, s3
  63.     return
  64.  
  65. setDefaultMode:
  66.     debug                // Turn on debugging info.
  67.     return
  68.  
  69. // verifyConfiguration
  70. // Arguments: none.
  71. // Returns: s0: Zero if selected mode is valid, nonzero otherwise.
  72. //
  73. verifyConfiguration:
  74.     debug                // Turn on debugging info.
  75.     call    unlockRegisters
  76.  
  77.     // Get the adapter type; make sure that this is an S3 928.
  78.  
  79.     inx    VGA_CRTC_INDEX, S3_CHIP_ID_INDEX, r0
  80.     and    r0, S3_CHIP_ID_MASK, r0
  81.     cmp    r0, S3_CHIP_ID_928
  82.     bne    invalidConfiguration
  83.  
  84.     // We only support the Bt484, Bt485, or Bt485A dacs.
  85.     call    determineDACtype
  86.     load    @dacType, r0
  87.     cmp    r0, DAC_Unknown
  88.     beq    invalidConfiguration
  89.  
  90.     // Get the memory configuration.
  91.  
  92.     inx    VGA_CRTC_INDEX, 0x36, r0
  93.     and    r0, 0xC0, r0
  94.     cmp    r0, 0x00        // 4 Megabytes
  95.     beq    _4M
  96.     cmp    r0, 0x40        // 3 Megabytes
  97.     beq    _3M
  98.     cmp    r0, 0x80        // 2 Megabytes
  99.     beq    _2M
  100.     load    1, r0            // 1 Megabyte
  101.     br    checkMemoryConfiguration
  102. _4M:    load    4, r0
  103.     br    checkMemoryConfiguration
  104. _3M:    load    3, r0
  105.     br    checkMemoryConfiguration
  106. _2M:    load    2, r0
  107.  
  108. checkMemoryConfiguration:
  109.     load    @modeMemorySize, r1
  110.     cmp    r0, r1
  111.     blt    invalidConfiguration
  112.     load    0, s0
  113.     call    lockRegisters
  114.     return
  115.  
  116. invalidConfiguration:
  117.     load    -1, s0
  118.     call    lockRegisters
  119.     return
  120.  
  121. initializeMode:
  122.     debug
  123.  
  124.     // Load the sequencer.
  125.     load    seqxParameters, r0
  126.     load    0, r1
  127. 0:    load    @r0, r2
  128.     outx    VGA_SEQ_INDEX, r1, r2
  129.     add    1, r0, r0
  130.     add    1, r1, r1
  131.     cmp    r1, VGA_SEQ_COUNT
  132.     blt    0b
  133.     
  134.     // Turn off the screen.
  135.     inx    VGA_SEQ_INDEX, 0x01, r0
  136.     or    0x20, r0, r0
  137.     outx    VGA_SEQ_INDEX, 0x01, r0
  138.  
  139.     // Unlock the S3 extended registers.
  140.     call    unlockRegisters
  141.  
  142.     // Unlock the CRTC registers.
  143.     call    unlockCRTC
  144.  
  145.     // Set up the CRTC parameters.
  146.     load    crtcParameters, r0
  147.     load    0, r1
  148. 0:    load    @r0, r2
  149.     outx    VGA_CRTC_INDEX, r1, r2
  150.     add    1, r0, r0
  151.     add    1, r1, r1
  152.     cmp    r1, VGA_CRTC_COUNT
  153.     blt    0b
  154.  
  155.     // Initialize the address flip-flop for the attribute controller.
  156.     inb    VGA_INPUT_STATUS_1, r0
  157.  
  158.     // Set up the attribute controller registers.
  159.     load    attrParameters, r0
  160.     load    0, r1
  161. 0:    load    @r0, r2
  162.     outb    VGA_ATTR_INDEX, r1
  163.     outb    VGA_ATTR_DATA, r2
  164.     add    1, r0, r0
  165.     add    1, r1, r1
  166.     cmp    r1, VGA_ATTR_COUNT
  167.     blt    0b
  168.  
  169.     // Start the sequencer.
  170.  
  171.     outx    VGA_SEQ_INDEX, 0x00, 0x03
  172.  
  173.     // Set up the graphics controller registers.
  174.  
  175.     load    grfxParameters, r0
  176.     load    0, r1
  177. 0:    load    @r0, r2
  178.     outx    VGA_GRFX_INDEX, r1, r2
  179.     add    1, r0, r0
  180.     add    1, r1, r1
  181.     cmp    r1, VGA_GRFX_COUNT
  182.     blt    0b
  183.  
  184.     // Set the miscellaneous output register (0x3C2).
  185.  
  186.     load    @miscOutputRegister, r0
  187.     outb    VGA_MISC_OUTPUT, r0
  188.  
  189.     // Reset the address flip-flop for the attribute controller and
  190.     // enable the palette.
  191.  
  192.     inb    VGA_INPUT_STATUS_1, r0
  193.     outb    VGA_ATTR_INDEX, 0x20
  194.  
  195.     // Set up the extended CRTC registers.
  196.  
  197.     load    0, r1
  198.     load    xCrtcParameters, r0
  199. 0:    load    @r0, r2
  200.     add    r0, 1, r0
  201.     load    @r0, r3
  202.     add    r0, 1, r0
  203.     outx    VGA_CRTC_INDEX, r2, r3
  204.     add    2, r1, r1
  205.     cmp    r1, S3_XCRTC_COUNT
  206.     blt    0b
  207.  
  208.     // Set the mode control register.
  209.     load    @modeControl, r0
  210.     outx    VGA_CRTC_INDEX, 0x42, r0
  211.  
  212.     // Unlock access to the enhanced commands registers.
  213.  
  214.     inx    VGA_CRTC_INDEX, 0x40, r0
  215.     or    0x01, r0, r0
  216.     outx    VGA_CRTC_INDEX, 0x40, r0
  217.  
  218.     // Set the advanced function control register (0x4AE8).
  219.  
  220.     load    @advFunctionControl, r0
  221.     outw    0x4AE8, r0
  222.  
  223.     // Lock the register set.
  224.  
  225.     inx    VGA_CRTC_INDEX, 0x40, r0
  226.     and    ~0x01, r0, r0
  227.     outx    VGA_CRTC_INDEX, 0x40, r0
  228.  
  229.     // Program the DAC.
  230.     call    programDAC
  231.  
  232.     // Lock the registers.
  233.     call    lockRegisters
  234.  
  235.     // Enable the screen.
  236.     inx    VGA_SEQ_INDEX, 0x01, r0
  237.     and    ~0x20, r0, r0
  238.     outx    VGA_SEQ_INDEX, 0x01, r0
  239.  
  240.     return
  241.  
  242.     .data
  243. lawSize:
  244.     .word    -1        // 0 megabytes
  245.     .word    S3_LAW_SIZE_1M    // 1 megabyte
  246.     .word    S3_LAW_SIZE_2M    // 2 megabytes
  247.     .word    S3_LAW_SIZE_4M    // 3 megabytes
  248.     .word    S3_LAW_SIZE_4M    // 4 megabytes
  249.  
  250.     .text
  251. //
  252. // videoRamAddress in s0.
  253. //
  254. enableLinearFrameBuffer:
  255.     debug
  256.     call    unlockRegisters
  257.  
  258.     // Tell the chip where the frame buffer is mapped in.
  259.     lsr    s0, 16, r1
  260.     and    r1, 0xFF, r1
  261.     outx    VGA_CRTC_INDEX, 0x5A, r1
  262.  
  263.     lsr    s0, 24, r1
  264.     and    r1, 0xFF, r1
  265.     outx    VGA_CRTC_INDEX, 0x59, r1
  266.     
  267.     // Set the linear address window size.
  268.     load    @modeMemorySize, r0
  269.     load    lawSize, r1
  270.     add    r0, r1, r0
  271.     load    @r0, r0
  272.  
  273.     inx    VGA_CRTC_INDEX, 0x58, r1
  274.     and    r1, 0xFC, r1
  275.     or    r1, r0, r1
  276.     outx    VGA_CRTC_INDEX, 0x58, r1
  277.     
  278.     // Disable 8514 register access.
  279.  
  280.     call    disable_8514
  281.  
  282.     // Turn off mmio.
  283.     inx    VGA_CRTC_INDEX, 0x53, r0
  284.     and    r0, 0xEF, r0
  285.     outx    VGA_CRTC_INDEX, 0x53, r0
  286.     
  287.     // Enable fast write buffer (write posting into FIFO).
  288.     inx    VGA_CRTC_INDEX, 0x40, r0
  289.     or    r0, 0x08, r0
  290.     outx    VGA_CRTC_INDEX, 0x40, r0
  291.  
  292.     // Enable read-ahead cache.
  293.     inx    VGA_CRTC_INDEX, 0x58, r0
  294.     or    r0, 0x04, r0
  295.     outx    VGA_CRTC_INDEX, 0x58, r0
  296.  
  297.     // Max out the read-ahead cache.
  298.     inx    VGA_CRTC_INDEX, 0x54, r0
  299.     or    r0, 0x07, r0
  300.     outx    VGA_CRTC_INDEX, 0x54, r0
  301.  
  302.     // Turn on the linear address window.
  303.     inx    VGA_CRTC_INDEX, 0x58, r0
  304.     or    r0, 0x10, r0
  305.     outx    VGA_CRTC_INDEX, 0x58, r0
  306.  
  307.     call    lockRegisters
  308.  
  309.     return
  310.  
  311.     .data
  312. vgaXCrtcParameters:
  313.     .word    0x31, 0x85, 0x32, 0x10, 0x33, 0x00, 0x34, 0x00, 0x35, 0x00
  314.     .word    0x3A, 0x85, 0x3B, 0x5A, 0x3C, 0x10, 0x40, 0x58, 0x43, 0x00
  315.     .word    0x50, 0x00, 0x51, 0x00, 0x53, 0x00, 0x54, 0x38, 0x56, 0x00
  316.     .word    0x57, 0x00, 0x5C, 0x31, 0x5D, 0x00, 0x5E, 0x00, 0x5F, 0x00
  317.     .word    0x60, 0x07, 0x61, 0x80, 0x62, 0xA1, 0x63, 0xA1
  318.  
  319.     .text
  320. resetVGA:
  321.     // Disable the linear framebuffer.
  322.  
  323.     call    unlockRegisters
  324.  
  325.     // Disable 8514 register access.
  326.     call    disable_8514
  327.     
  328.     // Turn off the linear address window.
  329.     inx    VGA_CRTC_INDEX, 0x58, r0
  330.     and    ~0x10, r0, r0
  331.     outx    VGA_CRTC_INDEX, 0x58, r0
  332.  
  333.     // Turn off the display.
  334.     inx    VGA_SEQ_INDEX, 0x01, r0
  335.     or    0x20, r0, r0
  336.     outx    VGA_SEQ_INDEX, 0x01, r0
  337.  
  338.     // Unlock the CRTC registers.
  339.     call    unlockCRTC
  340.  
  341.     // Unlock access to the enhanced commands registers.
  342.     inx    VGA_CRTC_INDEX, 0x40, r0
  343.     or    0x01, r0, r0
  344.     outx    VGA_CRTC_INDEX, 0x40, r0
  345.  
  346.     // Set VGA mode.
  347.     outw    0x4AE8, 0x02
  348.  
  349.     // Set the DAC for VGA mode.
  350.     call    resetDAC
  351.  
  352.     // Set up the extended CRTC registers.
  353.     load    vgaXCrtcParameters, r0
  354.     load    0, r1
  355. 0:    load    @r0, r2
  356.     add    r0, 1, r0
  357.     load    @r0, r3
  358.     add    r0, 1, r0
  359.     outx    VGA_CRTC_INDEX, r2, r3
  360.     add    2, r1, r1
  361.     cmp    r1, S3_XCRTC_COUNT
  362.     blt    0b
  363.  
  364.     // Set mode 0x03.
  365.     call    vgaSetMode3
  366.  
  367.     call    lockRegisters
  368.  
  369.     return
  370.  
  371. setCommandRegister0:
  372.     outx    VGA_CRTC_INDEX, 0x55, 0x01
  373.     outb    RS_02, s0
  374.     outx    VGA_CRTC_INDEX, 0x55, 0x00
  375.     return
  376.  
  377. setCommandRegister1:
  378.     outx    VGA_CRTC_INDEX, 0x55, 0x02
  379.     outb    RS_00, s0
  380.     outx    VGA_CRTC_INDEX, 0x55, 0x00
  381.     return
  382.  
  383. setCommandRegister2:
  384.     outx    VGA_CRTC_INDEX, 0x55, 0x02
  385.     outb    RS_01, s0
  386.     outx    VGA_CRTC_INDEX, 0x55, 0x00
  387.     return
  388.  
  389. setCommandRegister3:
  390.     // Don't set command register 3 if this is a Bt484.
  391.     load    @dacType, r0
  392.     cmp    r0, DAC_Bt484
  393.     beq    1f
  394.  
  395.     outx    VGA_CRTC_INDEX, 0x55, 0x01
  396.     inb    RS_02, r0        // Save command register 0.
  397.     move    r0, r7
  398.     or    r7, 0x80, r7
  399.     outb    RS_02, r7        // Set the high bit of command reg 0.
  400.     outx    VGA_CRTC_INDEX, 0x55, 0x00;
  401.     inb    RS_00, r1        // Save the address register.
  402.     outb    RS_00, 0x01
  403.     outx    VGA_CRTC_INDEX, 0x55, 0x02
  404.     outb    RS_02, s0
  405.     outx    VGA_CRTC_INDEX, 0x55, 0x01;
  406.     outb    RS_02, r0        // Restore command register 0.
  407.     outx    VGA_CRTC_INDEX, 0x55, 0x00;
  408.     outb    RS_00, r1        // Restore the address register.
  409. 1:    return
  410.  
  411. programDAC:
  412.     load    @commandRegister0, s0
  413.     call    setCommandRegister0
  414.  
  415.     load    @commandRegister1, s0
  416.     call    setCommandRegister1
  417.  
  418.     load    @commandRegister2, s0
  419.     call    setCommandRegister2
  420.  
  421.     load    @commandRegister3, s0
  422.     call    setCommandRegister3
  423.  
  424.     load    @crtc_0x45, r0
  425.     outx    VGA_CRTC_INDEX, 0x45, r0
  426.  
  427.     load    @crtc_0x53, r0
  428.     outx    VGA_CRTC_INDEX, 0x53, r0
  429.  
  430.     load    @crtc_0x55, r0
  431.     outx    VGA_CRTC_INDEX, 0x55, r0
  432.  
  433.     // Restore the PIXEL mask.
  434.     outb    RS_02, 0xFF
  435.  
  436.     // Set correct falling edge mode.
  437.     inx    VGA_CRTC_INDEX, 0x43, r0
  438.     and    r0, ~0x01, r0
  439.     outx    VGA_CRTC_INDEX, 0x43, r0
  440.  
  441.     return
  442.  
  443.     .data
  444. dacType:    .word    DAC_Unknown
  445.  
  446.     .text
  447. determineDACtype:
  448.     // Save the value of command register 0.
  449.     outx    VGA_CRTC_INDEX, 0x55, 0x01
  450.     inb    RS_02, r0
  451.  
  452.     // Write a zero to bit 7 of command register 0.
  453.     and    r0, 0x7F, r1
  454.     outb    RS_02, r1
  455.  
  456.     // Read the status register.
  457.     outx    VGA_CRTC_INDEX, 0x55, 0x02
  458.     inb    RS_02, r1
  459.     and    r1, 0xF0, r1
  460.     cmp    r1, DAC_Bt484
  461.     beq    1f
  462.     cmp    r1, DAC_Bt485
  463.     beq    1f
  464.     cmp    r1, DAC_Bt485A
  465.     beq    1f
  466.     load    DAC_Unknown, r1
  467. 1:    store    r1, @dacType
  468.  
  469.     move    r0, s0
  470.     call    setCommandRegister0
  471.  
  472.     // Make sure that we are addressing RS(00xx).
  473.     outx    VGA_CRTC_INDEX, 0x55, 0x00
  474.  
  475.     return
  476.  
  477. resetDAC:
  478.     load    0x00, s0
  479.     call    setCommandRegister0
  480.     load    0x00, s0
  481.     call    setCommandRegister1
  482.     load    0x00, s0
  483.     call    setCommandRegister2
  484.     load    0x00, s0
  485.     call    setCommandRegister3
  486.  
  487.     outx    VGA_CRTC_INDEX, 0x45, 0x00
  488.     outx    VGA_CRTC_INDEX, 0x53, 0x00
  489.     outx    VGA_CRTC_INDEX, 0x55, 0x00
  490.  
  491.     // Restore the PIXEL mask.
  492.     outb    RS_02, 0xFF
  493.  
  494.     // Set correct falling edge mode.
  495.     inx    VGA_CRTC_INDEX, 0x43, r0
  496.     and    ~0x01, r0, r0
  497.     outx    VGA_CRTC_INDEX, 0x43, r0
  498.  
  499.     return
  500.  
  501. // Stored as xrgb.
  502.     .data
  503.  
  504. transferTable:
  505.     .space    256
  506.  
  507.     .text
  508.  
  509. setTransferTable:
  510.     debug
  511.     outb    RS_00, 0x00
  512.     load    0, r0
  513.     load    transferTable, r1
  514. 0:    load    @r1, r2
  515.     add    1, r1, r1
  516.     lsr    r2, 16, r3    // Red
  517.     and    r3, 0xFF, r3
  518.     outb    RS_01, r3
  519.     lsr    r2, 8, r3    // Green
  520.     and    r3, 0xFF, r3
  521.     outb    RS_01, r3
  522.     and    r2, 0xFF, r3    // Blue
  523.     outb    RS_01, r3
  524.     add    1, r0, r0
  525.     cmp    r0, 256
  526.     blt    0b
  527.     return
  528.