Advertisement
Guest User

Untitled

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