-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.s
213 lines (177 loc) · 3.15 KB
/
counter.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
PORTB = $6000
PORTA = $6001
DDRB = $6002
DDRA = $6003
PCR = $600c
IFR = $600d
IER = $600e
value = $0200 ; 2 bytes
mod10 = $0202 ; 2 bytes
message = $0204 ; 6 bytes
counter = $020a ; 2 bytes
E = %10000001 ; LCD Enable
RW = %01000000 ; LCD R/W
RS = %00100000 ; Register Select
.org $8000
reset:
ldx #$ff
txs
cli
lda #$82
sta IER
lda #$00
sta PCR
lda #%11111111 ; Set all pins on port B to output
sta DDRB
lda #%11100001 ; Set top 3 pins + last pin on port A to output
sta DDRA
lda #%00111000 ; Set 8-bit mode; 2-line display; 5x8 font
jsr lcd_instruction
lda #%00001110 ; Display on; cursor on; blink off
jsr lcd_instruction
lda #%00000110 ; Increment and shift cursor; don't shift display
jsr lcd_instruction
lda #%00000001 ; Clear display
jsr lcd_instruction
lda #0
sta counter
sta counter + 1
loop:
lda #%00000010 ; Cursor to home
jsr lcd_instruction
lda #0
sta message
; Initialise value to be number to convert
sei
lda counter
sta value
lda counter + 1
sta value + 1
cli
divide:
; Initialise the remainder to be zero
lda #0
sta mod10
sta mod10 + 1
clc
ldx #16
divloop:
; Rotate quotient and remainder
rol value
rol value + 1
rol mod10
rol mod10 + 1
; a, y = dividend - divisor
sec
lda mod10
sbc #10
tay ; save low byte in Y
lda mod10 + 1
sbc #0
bcc ignore_result ; branching if dividend is less than divisor
sty mod10
sta mod10 + 1
ignore_result:
dex
bne divloop
rol value ; shift the last bit of the quotient
rol value + 1
lda mod10
clc
adc #"0"
jsr push_char
; if value != 0, then continue dividing
lda value
ora value + 1
bne divide ; branch if value not zero
ldx #0
print:
lda message,x
beq loop
jsr print_char
inx
jmp print
number: .word 1729
; Add the character in the A register to the beginning of the null-terminated string 'message'
push_char:
pha ; Push new first char onto the stack
ldy #0
char_loop:
lda message,y ; Get char on string and push into X register
tax
pla
sta message,y ; Pul char off stack and add it to string
iny
txa
pha ; Push char from string onto stack
bne char_loop
pla
sta message,y ; Pull the null off the stack and add to the end of the string
rts
lcd_wait:
pha
lda #%00000000 ; Port B is input
sta DDRB
lcdbusy:
lda #RW
sta PORTA
lda #(RW | E)
sta PORTA
lda PORTB
and #%10000000
bne lcdbusy
lda #RW
sta PORTA
lda #%11111111 ; Port B is output
sta DDRB
pla
rts
lcd_instruction:
jsr lcd_wait
sta PORTB
lda #0 ; Clear RS/RW/E bits
sta PORTA
lda #E ; Set E bit to send instruction
sta PORTA
lda #0 ; Clear RS/RW/E bits
sta PORTA
rts
print_char:
jsr lcd_wait
sta PORTB
lda #RS ; Set RS; Clear RW/E bits
sta PORTA
lda #(RS | E) ; Set E bit to send instruction
sta PORTA
lda #RS ; Clear E bits
sta PORTA
rts
nmi:
irq:
pha
txa
pha
tya
pha
inc counter
bne exit_irq
inc counter + 1
exit_irq:
ldy #$ff
ldx #$ff
delay:
dex
bne delay
dey
bne delay
bit PORTA
pla
tay
pla
tax
pla
rti
.org $fffa
.word nmi
.word reset
.word irq