Advertisement
Guest User

Untitled

a guest
Mar 7th, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;KBD routines
  2. ;KBDINIT - initializes 8042/8242 PS/2 keyboard controller
  3. ;Affects: A, X, Z
  4. ;Returns result code in  X:
  5. ;0x00 - OK
  6. ;0x01 - Controller self test failed
  7. ;0x02 - CLK stuck low
  8. ;0x03 - CLK stuck high
  9. ;0x04 - KBD DATA stuck low
  10. ;0x05 - KBD DATA stuck high
  11. ;0x06 - Interface didn't pass the test
  12. ;0x07 - Keyboard reset failure/no keyboard present        
  13. KBDINIT:
  14.     ;1. Disable devices
  15.     JSR KBDWAITINBUF             ;Send 0xAD command to the PS/2 controller
  16.     LDA #$AD
  17.     STA KBD_CMD
  18.     ;2. Flush The Output Buffer
  19.     LDA KBD_STATUS
  20.     AND #$01                     ;Check if there is data to flush
  21.     BEQ KBDCRTLSET               ;No? Next step then
  22.     LDA KBD_DATA                 ;Yes? Get the data byte        
  23. KBDCRTLSET:
  24.     ;3. Set the Controller Configuration Byte (temp)
  25.     JSR KBDWAITINBUF             ;Send 0x60 command to the PS/2 controller
  26.     LDA #$60
  27.     STA KBD_CMD
  28.     JSR KBDWAITINBUF             ;Send actual configuration byte
  29.     LDA #$08                     ;Interrupts disabled, system flag reset, first port clock enabled
  30.     STA KBD_DATA                 ;second port clock enabled, first port translation disabled
  31.     ;4. Controller self test
  32.     JSR KBDWAITINBUF
  33.     LDA #$AA                     ;Send 0xAA command to the PS/2 controller
  34.     STA KBD_CMD
  35.     JSR KBDWAITOUTBUF            ;Wait for response
  36.     LDA KBD_DATA                 ;Get byte
  37.     CMP #$55                     ;Is it 0x55?
  38.     BEQ KBDIFTEST
  39.     LDX #$01                     ;Return result code if not
  40.     RTS
  41. KBDIFTEST:                       ;No? Return then
  42.     ;5. Interface test
  43.     JSR KBDWAITINBUF
  44.     LDA #$AB                     ;Send 0xAB command
  45.     STA KBD_CMD
  46.     JSR KBDWAITOUTBUF            ;Wait for response
  47.     LDA KBD_DATA                 ;Get byte
  48.     CMP #$01                     ;Check if it is CLK stuck low error
  49.     BNE KBDIFTEST_CLKSH            
  50.     LDX #$02                     ;Return result code if it is
  51.     RTS
  52. KBDIFTEST_CLKSH:                             
  53.     CMP #$02                     ;Check if it is CLK stuck high error
  54.     BNE KBDIFTEST_DATSL        
  55.     LDX #$03                     ;Return result code if it is
  56.     RTS
  57. KBDIFTEST_DATSL:                             
  58.     CMP #$03                     ;Check if it is KBD DATA stuck low error
  59.     BNE KBDIFTEST_DATSH                    
  60.     LDX #$04                     ;Return result code if it is
  61.     RTS
  62. KBDIFTEST_DATSH:   
  63.     CMP #$04                     ;Check if it is KBD DATA stuck high error
  64.     BNE KBDIFTEST_OK
  65.     LDX #$05                     ;Return result code if it is
  66.     RTS
  67. KBDIFTEST_OK:                            
  68.     CMP #$00                     ;Is it 0x00? Did it pass the test?
  69.     BEQ KBDENDEVS
  70.     LDX #$06                     ;Return result code if not
  71.     RTS                          ;No? Return then
  72. KBDENDEVS: 
  73.     ;6. Enable Devices
  74.     JSR KBDWAITINBUF
  75.     LDA #$AE                     ;Send 0xAE command
  76.     STA KBD_CMD
  77.     ;7. Reset Device
  78.     JSR KBDWAITINBUF             ;Wait untill ready to send
  79.     LDA #$FF                     ;Send 0xFF to device
  80.     STA KBD_DATA                 ;Send it to device, not the controller
  81.     LDY 130                      ;Setup DELAY routine
  82.     JSR DELAY                    ;This is required to avoid freeze
  83.     JSR KBDWAITOUTBUF            ;Wait for response
  84.     LDA KBD_DATA                 ;Get byte
  85.     CMP #$FA                     ;Is it 0xFA? 0xFC means failure. No response means no device present.
  86.     BEQ KBD_OK
  87.     LDX #$07                     ;Return result code if not
  88.     RTS                          ;No? Return then
  89.     ;3. Set the Controller Configuration Byte
  90.     JSR KBDWAITINBUF             ;Send 0x60 command to the PS/2 controller
  91.     LDA #$60
  92.     STA KBD_CMD
  93.     JSR KBDWAITINBUF             ;Send actual configuration byte
  94.     LDA #$08                     ;Interrupts enabled, system flag reset, first port clock enabled
  95.     STA KBD_DATA                 ;second port clock enabled, first port translation disabled    
  96. KBD_OK:
  97.     ;LDA #$00                    ;Zero KBDDATA
  98.     ;STA KBDDATA
  99.     LDX #$00                     ;Return result code
  100.     RTS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement