Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .var code_start = $c000
  2.  
  3. // basic loader --------------------------------------------
  4. :BasicUpstart2(code_start)
  5.  
  6. mainStartup:    
  7. * = code_start "Main Startup"                      
  8.  
  9. // main code start -----------------------------------------
  10. main:  
  11.    
  12. TestKeyboard:
  13.     jsr Keyb_Init
  14.  
  15. @Loop:
  16.     lda #$FF
  17. @Raster:
  18.     cmp $D012
  19.     bne @Raster
  20.  
  21.     jsr ReadKeyb
  22.     jsr GetKey
  23.     bcs @Skip
  24.    
  25.     sta $0400
  26. @Skip:
  27.     jmp @Loop
  28.  
  29.  
  30. KeyMapVec:
  31.     .word KeyMap1, KeyMap2, KeyMap3, KeyMap4
  32.  
  33. //  Unshifted      
  34. KeyMap1:
  35.     .byte $14, $0D, $1D, $88, $85, $86, $87, $11
  36.     .byte $33, $57, $41, $34, $5A, $53, $45, $01
  37.     .byte $35, $52, $44, $36, $43, $46, $54, $58
  38.     .byte $37, $59, $47, $38, $42, $48, $55, $56
  39.     .byte $39, $49, $4A, $30, $4D, $4B, $4F, $4E
  40.     .byte $2B, $50, $4C, $2D, $2E, $3A, $40, $2C
  41.     .byte $5C, $2A, $3B, $13, $01, $3D, $5E, $2F
  42.     .byte $31, $5F, $04, $32, $20, $02, $51, $03
  43.     .byte $FF
  44.  
  45. //  Shifted
  46. KeyMap2:
  47.     .byte $94, $8D, $9D, $8C, $89, $8A, $8B, $91
  48.     .byte $23, $D7, $C1, $24, $DA, $D3, $C5, $01
  49.     .byte $25, $D2, $C4, $26, $C3, $C6, $D4, $D8
  50.     .byte $27, $D9, $C7, $28, $C2, $C8, $D5, $D6
  51.     .byte $29, $C9, $CA, $30, $CD, $CB, $CF, $CE
  52.     .byte $DB, $D0, $CC, $DD, $3E, $5B, $BA, $3C
  53.     .byte $A9, $C0, $5D, $9e, $01, $3D, $DE, $3F
  54.     .byte $21, $5F, $04, $22, $A0, $02, $D1, $83
  55.     .byte $FF
  56.  
  57. //  Commodore
  58. KeyMap3:
  59.     .byte $94, $8D, $9D, $8C, $89, $8A, $8B, $91
  60.     .byte $96, $B3, $B0, $97, $AD, $AE, $B1, $01
  61.     .byte $98, $B2, $AC, $99, $BC, $BB, $A3, $BD
  62.     .byte $9A, $B7, $A5, $9B, $BF, $B4, $B8, $BE
  63.     .byte $29, $A2, $B5, $30, $A7, $A1, $B9, $AA
  64.     .byte $A6, $AF, $B6, $DC, $3E, $5B, $A4, $3C
  65.     .byte $A8, $DF, $5D, $93, $01, $3D, $DE, $3F
  66.     .byte $81, $5F, $04, $95, $A0, $02, $AB, $83
  67.     .byte $FF
  68.  
  69. //  Control
  70. KeyMap4:
  71.     .byte $FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF
  72.     .byte $1C, $17, $01, $9F, $1A, $13, $05, $FF
  73.     .byte $9C, $12, $04, $1E, $03, $06, $14, $18
  74.     .byte $1F, $19, $07, $9E, $02, $08, $15, $16
  75.     .byte $12, $09, $0A, $92, $0D, $0B, $0F, $0E
  76.     .byte $FF, $10, $0C, $FF, $FF, $1B, $00, $FF
  77.     .byte $1C, $FF, $1D, $FF, $FF, $1F, $1E, $FF
  78.     .byte $90, $06, $FF, $05, $FF, $FF, $11, $FF
  79.     .byte $FF
  80.  
  81. .var CIA1_KeybWrite    = $DC00
  82. .var CIA1_KeybRead     = $DC01
  83.  
  84. .var cSYS_DelayValue   = 32
  85. .var cKeybW_Row1       = $FE
  86.  
  87. KeyR:              .byte 0
  88.  
  89. SYS_Keyd:          .byte 0 * 10 //  keyboard buffer
  90. SYS_Ndx:           .byte 0   //  Count of chars in keyboard buffer
  91. SYS_Xmax:          .byte 10  //  Maximum number of chars in the keyboard buffer
  92.  
  93. SYS_Shflag:        .byte 0   //  Shift collection (Shift=1, Commodore = 2, Control = 4)
  94. SYS_Sfdx:          .byte 0   //  Previous key pressed. Keyboard Matrix Coordinate. Same value as SYS_Lstx.
  95. SYS_Lstx:          .byte 0   //  Newest key Pressed (not shifts). $40 = no key.
  96. SYS_Delay:         .byte 0   //  Repeat countdown. 16 to 0, so 0.25 second.
  97. SYS_Kount:         .byte 0
  98. SYS_Lstshf:        .byte 0
  99.  
  100. Keyb_Init:
  101.     lda #64
  102.     sta SYS_Lstx
  103.     sta SYS_Sfdx
  104.  
  105.     lda #cSYS_DelayValue
  106.     sta SYS_Delay
  107.  
  108.     lda #6
  109.     sta SYS_Kount
  110.  
  111.     lda #0
  112.     sta SYS_Shflag
  113.     sta SYS_Lstshf
  114.  
  115.     sta SYS_Ndx
  116.     rts
  117.  
  118. ReadKeyb:
  119.     lda #<KeyMap1
  120.     sta @SMC_Vec + 1
  121.     lda #>KeyMap1
  122.     sta @SMC_Vec + 2
  123.    
  124.     //  Clear Shift Flag
  125.     lda #$40
  126.     sta SYS_Sfdx
  127.  
  128.     lda #0
  129.     sta SYS_Shflag
  130.  
  131.     sta CIA1_KeybWrite
  132.     ldx CIA1_KeybRead
  133.     cpx #$FF
  134.     beq @Cleanup
  135.  
  136.     ldy #$00
  137.  
  138.     lda #7
  139.     sta KeyR
  140.  
  141.     lda #cKeybW_Row1
  142.     sta @SMC_Row + 1
  143. @SMC_Row:
  144.     lda #0
  145.     sta CIA1_KeybWrite
  146.  
  147. @Loop_Debounce:
  148.     lda CIA1_KeybRead
  149.     cmp CIA1_KeybRead
  150.     bne @Loop_Debounce
  151.  
  152.     ldx #7
  153. @Loop_Col:
  154.     lsr
  155.     bcs  @NextKey
  156.     sta @SMC_A + 1
  157.  
  158. @SMC_Vec:
  159.     lda $FFFF,Y
  160.  
  161.     //  If <4 then is Stop or a Shift Key
  162.     cmp #$05
  163.     bcs @NotShift //  Not Shift
  164.  
  165.     cmp #$03
  166.     beq @NotShift //  Stop Key
  167.          
  168.     //  Accumulate shift key types (SHIFT=1, COMM=2, CTRL=4)
  169.     ora SYS_Shflag
  170.     sta SYS_Shflag
  171.     bpl @SMC_A
  172.  
  173. @NotShift:
  174.     sty SYS_Sfdx
  175.  
  176. @SMC_A:
  177.     lda #0
  178.  
  179. @NextKey:
  180.     iny
  181.     dex
  182.     bpl @Loop_Col
  183.  
  184.     sec
  185.     rol @SMC_Row + 1
  186.     dec KeyR
  187.     bpl @SMC_Row
  188.  
  189.     jmp @ProcKeyImg
  190.  
  191. //  Handles the key repeat
  192. @Process:
  193.     ldy SYS_Sfdx
  194. @SMC_Key:
  195.     lda $FFFF,Y
  196.     tax
  197.     cpy SYS_Lstx
  198.     beq @SameKey
  199.     ldy #cSYS_DelayValue
  200.     sty SYS_Delay     //  Repeat delay counter
  201.     bne @Cleanup
  202.          
  203. @SameKey:
  204.     and #$7F
  205.     ldy SYS_Delay
  206.     beq @EndDelay
  207.     dec SYS_Delay
  208.     bne @Exit
  209.  
  210. @EndDelay:
  211.     dec SYS_Kount
  212.     bne @Exit
  213.     ldy #$04
  214.     sty SYS_Kount
  215.     ldy SYS_Ndx
  216.     dey
  217.     bpl @Exit
  218.  
  219. //  Updates the previous key and shift storage
  220. @Cleanup:
  221.     ldy SYS_Sfdx
  222.     sty SYS_Lstx
  223.     ldy SYS_Shflag
  224.     sty SYS_Lstshf
  225.     cpx #$FF
  226.     beq @Exit
  227.     txa
  228.     ldx SYS_Ndx
  229.     cpx SYS_Xmax
  230.     bcs @Exit
  231.     sta SYS_Keyd,X
  232.     inx
  233.     stx SYS_Ndx
  234.  
  235. @Exit:
  236.     lda #$7F
  237.     sta CIA1_KeybWrite
  238.     rts
  239.  
  240. @ProcKeyImg:
  241.     lda SYS_Shflag
  242.     cmp #$03 //  C= + SHIFT
  243.     bne @SetDecodeTable
  244.     cmp SYS_Lstshf
  245.     beq @Exit
  246.  
  247. @SetDecodeTable:
  248.     asl
  249.     cmp #8   //  CONTROL
  250.     bcc @Cont
  251.     lda #$06
  252. @Cont:
  253.     tax
  254.     lda KeyMapVec,X
  255.     sta @SMC_Key + 1
  256.     lda KeyMapVec + 1,X
  257.     sta @SMC_Key + 2
  258.     jmp @Process
  259.  
  260. //  --------------------------
  261. GetKey:
  262.     lda SYS_Ndx
  263.     bne @IsKey
  264. @NoKey:
  265.     lda #255 //  Null
  266.     sec
  267.     rts
  268.  
  269. @IsKey:
  270.     ldy SYS_Keyd
  271.     ldx #0
  272. @Loop2:
  273.     lda SYS_Keyd + 1,X
  274.     sta SYS_Keyd,X
  275.     inx
  276.     cpx SYS_Ndx
  277.     bne @Loop2
  278.     dec SYS_Ndx
  279.     tya
  280.     clc
  281.     rts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement