Advertisement
Guest User

Untitled

a guest
Apr 12th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 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. lda #"H"
  57. jsr print_char
  58. lda #"e"
  59. jsr print_char
  60. lda #"l"
  61. jsr print_char
  62. lda #"l"
  63. jsr print_char
  64. lda #"o"
  65. jsr print_char
  66.  
  67. cli ; enable interrupts
  68.  
  69. lda #$FF
  70. sta T1CL ; load timer 1 counter low with 255
  71. sta T1CH ; load timer 1 counter high with 255, and start the count
  72.  
  73. loop:
  74. ; do nothing loop
  75. jmp loop
  76.  
  77. lcd_wait:
  78. pha
  79. lda #%11110000 ; LCD data is input
  80. sta DDRB
  81. lcdbusy:
  82. lda #RW
  83. sta PORTB
  84. lda #(RW | E)
  85. sta PORTB
  86. lda PORTB ; Read high nibble
  87. pha ; and put on stack since it has the busy flag
  88. lda #RW
  89. sta PORTB
  90. lda #(RW | E)
  91. sta PORTB
  92. lda PORTB ; Read low nibble
  93. pla ; Get high nibble off stack
  94. and #%00001000
  95. bne lcdbusy
  96.  
  97. lda #RW
  98. sta PORTB
  99. lda #%11111111 ; LCD data is output
  100. sta DDRB
  101. pla
  102. rts
  103.  
  104. lcd_init:
  105. lda #%00000010 ; Set 4-bit mode
  106. sta PORTB
  107. ora #E
  108. sta PORTB
  109. and #%00001111
  110. sta PORTB
  111. rts
  112.  
  113. lcd_instruction:
  114. jsr lcd_wait
  115. pha
  116. lsr
  117. lsr
  118. lsr
  119. lsr ; Send high 4 bits
  120. sta PORTB
  121. ora #E ; Set E bit to send instruction
  122. sta PORTB
  123. eor #E ; Clear E bit
  124. sta PORTB
  125. pla
  126. and #%00001111 ; Send low 4 bits
  127. sta PORTB
  128. ora #E ; Set E bit to send instruction
  129. sta PORTB
  130. eor #E ; Clear E bit
  131. sta PORTB
  132. rts
  133.  
  134. print_char:
  135. jsr lcd_wait
  136. pha
  137. lsr
  138. lsr
  139. lsr
  140. lsr ; Send high 4 bits
  141. ora #RS ; Set RS
  142. sta PORTB
  143. ora #E ; Set E bit to send instruction
  144. sta PORTB
  145. eor #E ; Clear E bit
  146. sta PORTB
  147. pla
  148. and #%00001111 ; Send low 4 bits
  149. ora #RS ; Set RS
  150. sta PORTB
  151. ora #E ; Set E bit to send instruction
  152. sta PORTB
  153. eor #E ; Clear E bit
  154. sta PORTB
  155. rts
  156.  
  157. .org $C000
  158. nmi:
  159. rti
  160. irq:
  161. pha
  162. bit HOLD
  163. bne slow ; jump to slow if HOLD is not zero
  164.  
  165. lda COUNT ; load count into a register
  166.  
  167. bit MODE
  168. bmi invert ; jump to invert if MODE bit 7 is a 1
  169.  
  170. inc ; increment the counter
  171. jmp finish
  172.  
  173. invert:
  174. dec ; decrement the counter
  175.  
  176. finish:
  177. beq switch ; if the counter is now 0, jump to switch
  178. sta PORTA ; output the counter to port A LEDs
  179. sta COUNT ; save new counter value
  180. jmp exit_irq_hold
  181.  
  182. switch:
  183. sta COUNT ; save counter without outputting
  184. lda #%10000000
  185. eor MODE ; flip bit 7 of MODE
  186. sta MODE ; save new MODE value
  187.  
  188. exit_irq_hold:
  189. lda #$02
  190. sta HOLD ; save a 2 into HOLD
  191. jmp exit_irq
  192.  
  193. slow:
  194. dec HOLD ; decrement HOLD
  195.  
  196. exit_irq:
  197. bit T1CL ; read timer 1 counter low to clear interrupt
  198. pla
  199. rti ; return from interrupt
  200.  
  201. .org $FFFA
  202. .word nmi
  203. .word reset
  204. .word irq
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement