Advertisement
Guest User

Untitled

a guest
Apr 12th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. ; set VIA address
  2. PORTB = $6000
  3. PORTA = $6001
  4. DDRB = $6002
  5. DDRA = $6003
  6. T1CL = $6004
  7. T1CH = $6005
  8. ACR = $600B
  9. IFR = $600D
  10. IER = $600E
  11.  
  12. ; define variables
  13. MODE = $0300
  14. HOLD = $0301
  15. COUNT = $0302
  16.  
  17. E = %01000000
  18. RW = %00100000
  19. RS = %00010000
  20.  
  21. .org $8000
  22.  
  23. reset:
  24. sei ;disable interrupts
  25.  
  26. ldx #$FF
  27. txs ;inlisize stack pointer
  28.  
  29. lda #$FF
  30. sta DDRB ; set all port b pins to output
  31. sta DDRA ; set all port a pins to output
  32.  
  33. lda #%01000000
  34. sta ACR ; enable timer 1 continuous mode
  35.  
  36. lda #%11000000
  37. sta IER ; enable timer 1 interrupts
  38.  
  39. stz HOLD ; set hold to 0
  40. stz MODE ; set mode to 0
  41.  
  42. lda #$01
  43. sta COUNT ; set count to 1
  44. sta PORTA ; set port a to output 1
  45.  
  46. jsr lcd_init
  47. lda #%00101000 ; Set 4-bit mode; 2-line display; 5x8 font
  48. jsr lcd_instruction
  49. lda #%00001110 ; Display on; cursor on; blink off
  50. jsr lcd_instruction
  51. lda #%00000110 ; Increment and shift cursor; don't shift display
  52. jsr lcd_instruction
  53. lda #$00000001 ; Clear display
  54. jsr lcd_instruction
  55.  
  56. cli ; enable interrupts
  57.  
  58. lda #$FF
  59. sta T1CL ; load timer 1 counter low with 255
  60. sta T1CH ; load timer 1 counter high with 255, and start the count
  61.  
  62. loop:
  63. ; do nothing loop
  64. jmp loop
  65.  
  66. lcd_wait:
  67. pha
  68. lda #%11110000 ; LCD data is input
  69. sta DDRB
  70. lcdbusy:
  71. lda #RW
  72. sta PORTB
  73. lda #(RW | E)
  74. sta PORTB
  75. lda PORTB ; Read high nibble
  76. pha ; and put on stack since it has the busy flag
  77. lda #RW
  78. sta PORTB
  79. lda #(RW | E)
  80. sta PORTB
  81. lda PORTB ; Read low nibble
  82. pla ; Get high nibble off stack
  83. and #%00001000
  84. bne lcdbusy
  85.  
  86. lda #RW
  87. sta PORTB
  88. lda #%11111111 ; LCD data is output
  89. sta DDRB
  90. pla
  91. rts
  92.  
  93. lcd_init:
  94. lda #%00000010 ; Set 4-bit mode
  95. sta PORTB
  96. ora #E
  97. sta PORTB
  98. and #%00001111
  99. sta PORTB
  100. rts
  101.  
  102. lcd_instruction:
  103. jsr lcd_wait
  104. pha
  105. lsr
  106. lsr
  107. lsr
  108. lsr ; Send high 4 bits
  109. sta PORTB
  110. ora #E ; Set E bit to send instruction
  111. sta PORTB
  112. eor #E ; Clear E bit
  113. sta PORTB
  114. pla
  115. and #%00001111 ; Send low 4 bits
  116. sta PORTB
  117. ora #E ; Set E bit to send instruction
  118. sta PORTB
  119. eor #E ; Clear E bit
  120. sta PORTB
  121. rts
  122.  
  123. print_char:
  124. jsr lcd_wait
  125. pha
  126. lsr
  127. lsr
  128. lsr
  129. lsr ; Send high 4 bits
  130. ora #RS ; Set RS
  131. sta PORTB
  132. ora #E ; Set E bit to send instruction
  133. sta PORTB
  134. eor #E ; Clear E bit
  135. sta PORTB
  136. pla
  137. and #%00001111 ; Send low 4 bits
  138. ora #RS ; Set RS
  139. sta PORTB
  140. ora #E ; Set E bit to send instruction
  141. sta PORTB
  142. eor #E ; Clear E bit
  143. sta PORTB
  144. rts
  145.  
  146. nmi:
  147. rti
  148. irq:
  149. pha
  150. bit HOLD
  151. bne slow ; jump to slow if HOLD is not zero
  152.  
  153. lda COUNT ; load count into a register
  154.  
  155. bit MODE
  156. bmi invert ; jump to invert if MODE bit 7 is a 1
  157.  
  158. inc ; increment the counter
  159. jmp finish
  160.  
  161. invert:
  162. dec ; decrement the counter
  163.  
  164. finish:
  165. beq switch ; if the counter is now 0, jump to switch
  166. sta PORTA ; output the counter to port A LEDs
  167. sta COUNT ; save new counter value
  168. jmp exit_irq_hold
  169.  
  170. switch:
  171. sta COUNT ; save counter without outputting
  172. lda #%10000000
  173. eor MODE ; flip bit 7 of MODE
  174. sta MODE ; save new MODE value
  175.  
  176. exit_irq_hold:
  177. lda #$02
  178. sta HOLD ; save a 2 into HOLD
  179. jmp exit_irq
  180.  
  181. slow:
  182. dec HOLD ; decrement HOLD
  183.  
  184. exit_irq:
  185. bit T1CL ; read timer 1 counter low to clear interrupt
  186. pla
  187. rti ; return from interrupt
  188.  
  189. .org $FFFA
  190. .word nmi
  191. .word reset
  192. .word irq
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement