-
Notifications
You must be signed in to change notification settings - Fork 2
/
rpm_ca65.s
346 lines (312 loc) · 7.54 KB
/
rpm_ca65.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
; ASC
; DFB
.include "ca65_fixes.inc"
ZERO_LEAD EQU $00
KEY_CR EQU $8D
KEY_ESC EQU $9B
KEY_0 EQU $B0 ; '0' = $30
KEY_9 EQU $B9 ; '9' = $39
pDigits EQU $FA
Carry EQU $FC
Div EQU $FD
KEYBOARD EQU $C000
KEYSTROBE EQU $C010
TEXT EQU $FB2F ; Init text mode and window
HOME EQU $FC58
RDKEY EQU $FD0C
COUT EQU $FDED
__main = $2000
; Header for DOS 3.3 binary file; remove 2 lines for ProDOS
DW __main
DW __end - __main
; main()
ORG __main
LDA #7 ; Version
JSR TEXT ; Just in case we are in graphics mode
JSR HOME
JSR InitABS ; Init A,B,S
JSR IntroH
JSR InputA ; Input
JSR InputB
JSR IntroS
MulDigit ; Calc
JSR IsZeroB
BCS OutS
JSR IsOddB
BCC EvenS
JSR AddAToS
EvenS
JSR ShlA
JSR ShrB
CLC
BCC MulDigit ; ^ (always)
OutS
JSR PrintS ; output
;RTS ; NOTE: DOS3.3 BRUN bug doesn't exit, should JMP $3D0
JMP $0000
InputA
JSR IntroA
JSR GetPtrA
BNE GetString
InputB
JSR IntroB
JSR GetPtrB
; intentional fall int GetString
GetString ; common entry point
STX pDigits
STY pDigits+1
LDX #0 ; digits length
_linechar
JSR GetDigit ; C=1 enter, C=0 digit
BCS _linedone
JSR COUT ; preserves A,Y
AND #$F ; C=0 digit
STA Buffer,X
INX
BPL _linechar ; or BNE for 255 digits
BMI IntroE ; error if > 127 digits
_linedone ; Now move digits from $200 -> DigitsA, DigitsB, etc.
LDY #0 ; X = src, Y = dst offset
CPX #0 ; any digits?
BEQ _movedone ; No digits!
_moveline
DEX
LDA Buffer,X
STA (pDigits),Y ; digit[y] = char & 0xF
INY
CPX #0
BNE _moveline ; ^ max 256 digits
_movedone
BEQ PrintCR ; v (always)
GetDigit
; LDA KEYBOARD
; BPL GetDigit
; STA KEYSTROBE
JSR RDKEY
CMP #KEY_CR
BEQ _eol
CMP #KEY_ESC
BEQ _esc
CMP #KEY_0
BCC GetDigit
CMP #KEY_9+1
BCS GetDigit
RTS
_eol SEC ; C=1 enter
RTS
_esc ; Unwind stack of InputA/B
PLA
PLA
RTS ; Return to who originally called main()
GetPtrA
LDX #<DigitsA ; lo byte
LDY #>DigitsA ; hi byte
RTS
GetPtrB
LDX #<DigitsB
LDY #>DigitsB
RTS
GetPtrS
LDX #<DigitsS
LDY #>DigitsS
RTS
InitABS
LDA #$00
TAX
_zero
STA DigitsA,X
STA DigitsB,X
STA DigitsS,X
INX
BNE _zero ; zero digits[0..$FF]
RTS
IntroA LDY #OffsetA
BNE _intro
IntroB LDY #OffsetB
BNE _intro
IntroS LDY #OffsetS
BNE _intro
IntroE LDY #OffsetE
BNE _intro
IntroH
LDY #OffsetH
_intro
LDA _text,Y
BEQ _outro ; asciiz
JSR COUT
INY
BNE _intro
_outro
RTS
PrintCR
LDA #KEY_CR
JMP COUT
; ________________________________________________________________________
; Pseudo BCD - one byte per digit
; ________________________________________________________________________
; ========================================
PrintA
JSR GetPtrA
BNE _print
PrintB
JSR GetPtrB
BNE _print
PrintS
JSR GetPtrS
_print
STX _bcd+1
STY _bcd+2
LDX #ZERO_LEAD ; Default to leading zero ($0)
LDY #$FF ; start at digits[255]
_bcd
LDA $FFFF,Y ; NOTE: Self-Modified by _print
BNE _digit ; digits[i] == 0 && leadingzero
CPX #ZERO_LEAD ; X != 0
BEQ _skip
_digit
CLC
ADC #KEY_0 ; 0 -> $B0 .. 9 -> $B9
TAX ; If digit printed stop checking for leading zero
JSR COUT
_skip
DEY ;
CPY #$FF ; digits[255] ?
BNE _bcd ; ^
; Bug fix: If no digits were printed then number must be zero
LDA #0 ; print '0'
TAY ; no length
CPX #ZERO_LEAD
BEQ _digit
BNE PrintCR ; ^ (always) move to next line when done
; ========================================
AddAToS
LDY #0
CLC
_add
LDA DigitsS,Y
ADC DigitsA,Y
CMP #$A ; Base=10, C = (a+s+carry) >= 10
BCC _overflow
SBC #$A ; C=1, A = A - 9 - C
_overflow
STA DigitsS,Y
INY
BNE _add
RTS
; ========================================
IsOddB
LDY #0
LDA DigitsB,Y
AND #1
BEQ _not_zero ; Even -> C=0
BNE _is_zero ; Odd _> C=1
; ========================================
IsZeroB
LDY #0
_is_0
LDA DigitsB,Y
BNE _not_zero
INY
BNE _is_0
_is_zero
SEC
RTS
_not_zero
CLC
RTS
; ========================================
ShlA
LDY #0
CLC
_shl
LDA DigitsA,Y
TAX
LDA Mul2Digit,X
ADC #0
STA DigitsA,Y
LDA Mul2Carry,X
ROR
INY
BNE _shl
RTS
; ========================================
ShrB
LDY #$FF ; start at digits[255]
LDX #0 ; digits processed
STX Carry ; $00 .. 9*2 = $18
_shr
CLC ;
LDA DigitsB,Y ; sum = digit[i] + carry
ADC Carry ; A=0000_00xy C=0/1
ROR ; A=c000_000x, C=y
AND #$F ; A=0000_000x
STA DigitsB,Y ; digit[i] = (sum / 2)
LDA #0 ;
BCC _shr_next ; C=0 -> Carry = 0
LDA #10 ; C=1 -> Carry = 10 (base)
_shr_next
STA Carry ; carry = (sum & 1) ? BASE : 0
DEY
INX ; we need to compare y=$FF without disturbing carry
BNE _shr ; max 256 digits -- solution, use X reg :-)
RTS
Mul2Digit ; Digit
DFB 0 ; 0*2 = 0 0
DFB 2 ; 1*2 = 0 2
DFB 4 ; 2*2 = 0 4
DFB 6 ; 3*2 = 0 6
DFB 8 ; 4*2 = 0 8
DFB 0 ; 5*2 = 1 0
DFB 2 ; 6*2 = 1 2
DFB 4 ; 7*2 = 1 4
DFB 6 ; 8*2 = 1 6
DFB 8 ; 9*2 = 1 8
Mul2Carry ; Carry
DFB 0 ; 0*2 = 0 0
DFB 0 ; 1*2 = 0 2
DFB 0 ; 2*2 = 0 4
DFB 0 ; 3*2 = 0 6
DFB 0 ; 4*2 = 0 8
DFB 1 ; 5*2 = 1 0
DFB 1 ; 6*2 = 1 2
DFB 1 ; 7*2 = 1 4
DFB 1 ; 8*2 = 1 6
DFB 1 ; 9*2 = 1 8
_text
TextH ; Header
ASC "RUSSIAN PEASANT MULTIPLICATION"
DFB KEY_CR
DFB KEY_CR
DFB 0
TextA
ASC "INPUT A"
DFB KEY_CR
ASC "?"
DFB 0
TextB
ASC "INPUT B"
DFB KEY_CR
ASC "?"
DFB 0
TextE
DFB KEY_CR
ASC "WARNING"
ASC ": MORE THAN 127 CHARS"
DFB KEY_CR
DFB 0
TextS
ASC "= "
DFB 0
OffsetH EQU TextH - _text
OffsetA EQU TextA - _text
OffsetB EQU TextB - _text
OffsetE EQU TextE - _text
OffsetS EQU TextS - _text
__end
; Pad until end of page, these globals are not stored on disk
pad EQU 256 - <*
page EQU *+pad
DigitsA EQU page ; stupid assembler can't use variable 'a'
DigitsB EQU page+256
DigitsS EQU page+512
Buffer EQU page+768