Advertisement
Guest User

Untitled

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