-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculator.asm
678 lines (528 loc) · 17.3 KB
/
calculator.asm
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
; Calculator for 6502asm.com, Version 1.02, 2015.JUL.08 by Sid Liu
; It is slow in the 6502 emulator by JavaScript.
; The left top flash point is a busy indicator.
; The calculator supports keys: 'C', 'c', '0', '1', ..., '9', '+', '-' and '='.
main: cld
jsr display
; comparison loop for well response
getch_main: lda $ff
cmp #67 ; 'C' for clear
beq store_main
cmp #99 ; 'c' for clear
beq store_main
cmp #43 ; '+'
beq store_main
cmp #45 ; '-'
beq store_main
cmp #61 ; '='
beq store_main
cmp #48 ; '0'
bmi getch_main
cmp #57 ; '9'
beq store_main
bpl getch_main
store_main: sta key_main
; clear key
lda #0
sta $ff
; clear operand 2 by 'C' or 'c'
lda key_main
cmp #67
beq clear_main
cmp #99
beq clear_main
jmp operate_main
clear_main: jsr clear
lda #61
sta _operator1
lda #0
sta still_main
sta standby_main
jsr display
jmp getch_main
; operate operand 1 and operand 2
operate_main: lda key_main
cmp #43 ; '+'
bne next1_main
sta _operator2
lda just_main
bne jump1_main
jsr operate
lda #1
sta standby_main
sta just_main
jump1_main: jmp getch_main
next1_main: lda key_main
cmp #45 ; '-'
bne next2_main
sta _operator2
lda just_main
bne jump2_main
jsr operate
lda #1
sta standby_main
sta just_main
jump2_main: jmp getch_main
next2_main: lda key_main
cmp #61 ; '='
bne replace_main
sta _operator2
lda just_main
bne jump3_main
jsr operate
lda #61
sta _operator1
lda #1
sta standby_main
sta just_main
jump3_main: jmp getch_main
replace_main: lda standby_main
beq full_main
jsr move
jsr clear
lda #0
sta standby_main
jmp append_main
; whether full of digits
full_main: jsr length
lda $00
cmp #5
bne next3_main
jmp getch_main
next3_main: lda still_main
beq append_main
; shift digits
ldx #3
shift_main: clc
lda _operand2,x
stx tmp_main
ldy tmp_main
iny
clc
sta _operand2,y
dex
txa
bpl shift_main
; append digit
append_main: lda #1
sta still_main
lda #0
sta just_main
lda key_main
sta _operand2
; replace '0' with 's'
ldx #4
loop_main: lda _operand2,x
cmp #48
beq space_main
cmp #115
beq next4_main
jmp display_main
space_main: lda #115
sta _operand2,x
next4_main: dex
beq display_main ; "XXXXY"
bpl loop_main
display_main: jsr display
jmp getch_main
; global variables
_operand1: dcb 48,115,115,115,115 ; 's' for space
_operator1: dcb 61 ; '='
_operand2: dcb 48,115,115,115,115
_operator2: dcb 115
; local variables
key_main: dcb 0
tmp_main: dcb 0
still_main: dcb 0 ; boolean, still editing flag
standby_main: dcb 0 ; boolean, preparing to move and clear flag
just_main: dcb 0 ; boolean, just operator key flag
operate: ; operate operand 1 and operand 2
lda _operator1
cmp #43 ; '+'
beq add_operate
cmp #45 ; '-'
beq sub_operate
jmp end_operate
add_operate: jsr add
jsr display
jmp end_operate
sub_operate: jsr sub
jsr display
end_operate: rts
add: ; add operand 1 and operand 2
ldx #0
ldy #0
sty carry_add
loop1_add: clc
lda _operand2,x
cmp #115
beq space1_add
sec
sbc #48
sta digit_add
jmp next1_add
space1_add: lda #0
sta digit_add
next1_add: clc
lda _operand1,x
cmp #115
beq space2_add
sec
sbc #48
jmp next2_add
space2_add: lda #0
next2_add: clc
adc digit_add
clc
adc carry_add
ldy #0
sty carry_add
cmp #10
bmi ascii_add
sec
sbc #10
ldy #1
sty carry_add
ascii_add: clc
adc #48
clc
sta _operand2,x
inx
cpx #5
bmi loop1_add
; replace '0' with 's'
ldx #4
loop2_add: lda _operand2,x
cmp #48
bne end_add
lda #115
sta _operand2,x
dex
beq end_add ; "XXXXY"
bpl loop2_add
end_add: rts
; local variables
digit_add: dcb 0
carry_add: dcb 0
sub: ; sub operand 1 and operand 2
ldx #0
ldy #0
sty borrow_sub
loop1_sub: clc
lda _operand2,x
cmp #115
beq space1_sub
sec
sbc #48
sta digit_sub
jmp next1_sub
space1_sub: lda #0
sta digit_sub
next1_sub: clc
lda _operand1,x
cmp #115
beq space2_sub
sec
sbc #48
jmp next2_sub
space2_sub: lda #0
next2_sub: sec
sbc digit_sub
sec
sbc borrow_sub
ldy #0
sty borrow_sub
cmp #0
bpl ascii_sub
clc
adc #10
ldy #1
sty borrow_sub
ascii_sub: clc
adc #48
clc
sta _operand2,x
inx
cpx #5
bmi loop1_sub
; replace '0' with 's'
ldx #4
loop2_sub: lda _operand2,x
cmp #48
bne end_sub
lda #115
sta _operand2,x
dex
beq end_sub ; "XXXXY"
bpl loop2_sub
end_sub: rts
; local variables
digit_sub: dcb 0
borrow_sub: dcb 0
length: ; compute the length of operand 2
; input void; output $00;
ldx #0
loop_length: cpx #5
beq end_length
clc
lda _operand2,x
inx
cmp #115
bne loop_length
dex
end_length: stx $00
rts
move: ; move operand 2 to operand 1
; move operator 2 to operator 1
ldx #4
loop_move: clc
lda _operand2,x
sta _operand1,x
dex
bpl loop_move
; move operator 2 to operator 1
lda _operator2
sta _operator1
rts
clear: ; clear operand 2
lda #4
sta count_clear
loop_clear: lda #115
ldx count_clear
clc
sta _operand2,x
dec count_clear
lda count_clear
cmp #1
bpl loop_clear
lda #48
sta _operand2
rts
; local variables
count_clear: dcb 0
display: ; display operand 2
lda #4
sta count_display
loop_display: lda count_display
sta $00
inc $00
lda #6
sta $01
jsr cmul
lda #32
sec
sbc $02
sta $00 ; 32-(count+1)*6
lda #22
sta $01
ldx count_display
clc
lda _operand2,x
sta $02
jsr draw
dec count_display
lda count_display
bpl loop_display
rts
; local variables
count_display: dcb 0
draw: ; draw a digit
; input char x $00, char y $01,char digit $02; output void;
lda $00
sta x_draw
lda $01
sta y_draw
lda $02
cmp #115
bne digital_draw
lda #$ff ; -1 for space
sta value_draw
jmp init_draw
digital_draw: sec
sbc #48
sta value_draw
init_draw: lda #0
sta i_draw
sta j_draw
loop_draw: ; converse white
lda i_draw
cmp #0
bne converse_draw
lda #1
sta $00
sta $01
lda #$01
sta $02
jsr pixel
; converse black
converse_draw: lda i_draw
cmp #3
bne start_draw
lda #1
sta $00
sta $01
lda #$00
sta $02
jsr pixel
start_draw: lda value_draw
sta $00
bmi black_draw
lda #9
sta $01
jsr cmul
lda $02
clc
adc j_draw
tay
clc
lda font_draw,y ; byte=[font+value*9+j]
sta byte_draw
lda #1
sta mask_draw
ldx i_draw
shift_draw: beq and_draw
asl mask_draw
dex
jmp shift_draw
and_draw: lda mask_draw
and byte_draw
beq black_draw
white_draw: lda #$01
sta color_draw
jmp pixel_draw
black_draw: lda #$00
sta color_draw
pixel_draw: lda i_draw
clc
adc x_draw
sta $00
lda j_draw
clc
adc y_draw
sta $01
lda color_draw
sta $02
jsr pixel
; increase for next step
inc i_draw
lda i_draw
cmp #5
bpl next_draw
jmp loop_draw
next_draw: lda #0
sta i_draw
inc j_draw
lda j_draw
cmp #9
bpl end_draw
jmp loop_draw
end_draw: rts
; local variables
x_draw: dcb 0
y_draw: dcb 0
value_draw: dcb 0
i_draw: dcb 0
j_draw: dcb 0
mask_draw: dcb 0
byte_draw: dcb 0
color_draw: dcb 0
font_draw: ; '0'~'9', 5x9
dcb $0e,$11,$11,$13,$15,$19,$11,$11,$0e
dcb $04,$06,$04,$04,$04,$04,$04,$04,$0e
dcb $0e,$11,$10,$10,$08,$04,$02,$01,$1f
dcb $0e,$11,$10,$10,$0e,$10,$10,$11,$0e
dcb $08,$0c,$0a,$09,$09,$09,$1f,$08,$08
dcb $1f,$01,$01,$01,$0f,$10,$10,$11,$0e
dcb $0e,$11,$01,$01,$0f,$11,$11,$11,$0e
dcb $1f,$11,$10,$10,$08,$08,$08,$04,$04
dcb $0e,$11,$11,$11,$0e,$11,$11,$11,$0e
dcb $0e,$11,$11,$11,$1e,$10,$10,$10,$0e
pixel: ; draw a pixel
; pixel page memory address, m=512+y*32+x
; (or m0=y%8*32+x, m1=2+y/8)
; color: $00 is black, $01 is white
; input char x $00,y $01,color $02; output void; effect $60, $61
lda $00
sta x_pixel
lda $01
sta y_pixel
lda $02
sta color_pixel
lda y_pixel
sta $00
lda #8
sta $01
jsr cmod
lda $02
sta $00
lda #32
sta $01
jsr cmul
lda $02
clc
adc x_pixel
sta $60 ; m0
lda y_pixel
sta $00
lda #8
sta $01
jsr cdiv
lda $02
clc
adc #2
sta $61 ; m1
ldy #0
lda color_pixel
clc
sta ($60),y
rts
; local variables
x_pixel: dcb 0
y_pixel: dcb 0
color_pixel: dcb 0
cmod: ; char modulus
; c=a%b=a-a/b*b
; input char a $00,b $01; output char c $02;
LDA $00
STA a_cmod
JSR cdiv
LDA $02
STA $00
JSR cmul
LDA $02
STA p_cmod
LDA a_cmod
SEC
SBC p_cmod
STA $02
RTS
a_cmod: DCB 0
p_cmod: DCB 0 ; p=a/b*b
cmul: ; char multiply
; c=a*b
; input char a $00,b $01; output char c $02;
LDA #0
LDX $01
loop_cmul: BEQ take_cmul
CLC
ADC $00
DEX
JMP loop_cmul
take_cmul: STA $02
RTS
cdiv: ; char divide
; c=a/b
; input char a $00,b $01; output char c $02;
LDA #0
LDX #0
CLC
loop_cdiv: CMP $00
BEQ take2_cdiv
BPL take1_cdiv
ADC $01
BCS take2_cdiv
INX
JMP loop_cdiv
take1_cdiv: DEX
take2_cdiv: STX $02
RTS