-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexec.S
3855 lines (3164 loc) · 102 KB
/
exec.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
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ****************************************************************************
;
; Execute buttons
;
; ****************************************************************************
#include "include.inc"
.text
; bridges
.global _CalcGetMem
_CalcGetMem: jmp CalcGetMem
.global _Calc
_Calc: jmp Calc
.global _CalcSetMemDelX ; set register X
_CalcSetMemDelX:
ldi r24,REG_X
.global _CalcSetMemDel ; set register and delete stack
_CalcSetMemDel:
jmp CalcSetMemDel
.global _DispChar
_DispChar:
jmp DispChar
.global _CalcConst
_CalcConst:
jmp CalcConst
.global _EditStop
_EditStop:
jmp EditStop
.global __Disp
__Disp: jmp Disp
MainProgText:
.asciz "Main Program"
.balign 2
ClearTxt:
.asciz " Clear? (1=YES) "
.balign 2
; ----- compare operations of ExecIf
ExecIfTab:
.byte C_LTEQ ; 0 (8) <=
.byte C_LT ; 1 (9) <
.byte C_EQU ; 2 (A) =
.byte C_LTEQ ; 3 (B) <=
.byte C_GR ; 4 (C) >
.byte C_NEQU ; 5 (D) <> (not equal)
.byte C_GREQ ; 6 (E) >=
.byte C_GR ; 7 (F) >
.balign 2
; ----------------------------------------------------------------------------
; CLR (0x25)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecClr
ExecClr:
; CLR do not clear T register!
call CalcInit ; reset calculator stack
rcall ClearErr ; clear error
rcall FlagClrDispText ; clear text mode
call LCD_SetFontDef ; set default font
std Y+DATA_FLAG2ND,R_ZERO ; clear 2nd flag
; DESTROYS: -
rcall FlagClrEE ; clear EE flag (but not ENG mode)
rcall EditStart ; start edit mode
; DESTROYS: -
rjmp DispFlags ; display flags
; ----------------------------------------------------------------------------
; 2nd (0x21)
; ----------------------------------------------------------------------------
; INPUT: R24 = key code
; R23 = INV flag (1=set) ... in this '2nd' case flag INV is not cleared
; Z = clear if INV flag was set (=brne)
; DESTROYS: R31, R30, R25, R24
; ----------------------------------------------------------------------------
.global Exec2nd
Exec2nd:
; 2nd flag
; #define F_NONE 0 // no 2nd flag
; #define F_2ND 1 // 2nd flag
; #define F_3RD 2 // 3rd flag
ldd r24,Y+DATA_FLAG2ND ; load 2nd flag
inc r24 ; increase 2nd flag
cpi r24,F_3RD+1 ; check overflow
brcs 2f ; ok
ldi r24,F_NONE ; overflow to no 2nd flag
2: std Y+DATA_FLAG2ND,r24 ; set new 2nd flag
; DESTROYS: -
rjmp DispFlags ; display flags
; ----------------------------------------------------------------------------
; INV (0x22, 0x27)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecInv
ExecInv:
tst r23 ; was INV set?
breq 2f ; INV was not set
; DESTROYS: -
rcall FlagClrInv ; clear INV flag
; DESTROYS: -
rjmp DispFlags ; display flags
; DESTROYS: -
2: rcall FlagSetInv ; set INV flag
; DESTROYS: -
rjmp DispFlags ; display flags
; ----------------------------------------------------------------------------
; CP (0x29)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecCp
ExecCp:
rcall _EditStop ; stop edit mode
ldi r24,CONST_0 ; constant 0
; INPUT: R24 = index of the constant in ConstTab
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcConst
ldi r24,REG_T
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: -1
rcall _CalcSetMemDel ; clear register T
IF_RUNNING ; running?
ret ; running program
ldd r24,Y+DATA_PROGINX ; current program index
tst r24 ; main program?
brne 9f ; not main program
; diplay prompt to confirm operation
call DispSetRow1 ; set cursor to ROW1
ldi r30,lo8(ClearTxt)
ldi r31,hi8(ClearTxt)
call DispTextRom ; display text
ldi r24,NOKEY
call ReturnKey ; flush old key
call WaitKey ; wait for a key
cpi r24,KEY_1 ; check key '1'
brne 8f
rcall DispC ; display C
ldi r26,lo8(PROG_NUM-1)
ldi r27,hi8(PROG_NUM-1)
ldi r25,CLEARKEY
; INPUT: R27:R26 = destination address
; R25 = data
; OUTPUT: R24 = old byte
1: call EEWrite ; clear byte
sbiw r26,1
brpl 1b
std Y+DATA_ADDR,R_ZERO
std Y+DATA_ADDR+1,R_ZERO
; DESTROYS: R0
8:
rjmp Disp ; display all
9: ret
; ----------------------------------------------------------------------------
; Code (0x2b)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecCode
ExecCode:
; ----- return if running
IF_RUNNING ; if running, no operation
ret
; ----- load 2 hex digits: from program, 2 digits from user, or as a key
; OUTPUT: R24 = byte
; DESTROYS: R25
call Load2Hex
; ----- save key into keyboad buffer
; INPUT: R24 = key HEX code
; DESTROYS: -
jmp ReturnKey
; ----------------------------------------------------------------------------
; x<>t (0x32)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecXt
ExecXt:
rcall _EditStop ; stop edit mode
rcall _Calc
.byte C_GETMEM(REG_X) ; load X into stack
.byte C_GETMEM(REG_T) ; load T into stack
.byte C_SETMEMDEL(REG_X) ; set X back from stack
.byte C_SETMEMDEL(REG_T) ; set T back from stack
.byte C_END
.balign 2
; DESTROYS: R0
rjmp Disp ; display all
; ----------------------------------------------------------------------------
; x<>y (0x3B)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecXy
ExecXy:
rcall _EditStop ; stop edit mode
; exchange last operation
ldd r25,Y+DATA_LEVEL ; current number of arithmetics operations
tst r25 ; already base level?
brne 2f ; not base level
; check oper level
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
call ExecLevel ; get current level
andi r24,OPER_MASK ; mask current level
brne 2f ; not invalid operation
; exchange X and LAST
rcall _Calc
.byte C_GETMEM(REG_X) ; load X into stack
.byte C_GETMEM(REG_LAST) ; load last into stack
.byte C_SETMEMDEL(REG_X) ; set X back from stack
.byte C_SETMEMDEL(REG_LAST) ; set last back from stack
.byte C_END
.balign 2
; DESTROYS: R0
rjmp Disp ; display all
2: ; exchange X and current operand
ldi r24,REG_X
; INPUT: R24 = index of variable 0..MEM_NUM-1
; OUTPUT: R31:R30 = address of variable
; R1 = 0
; DESTROYS: R0
call CalcAddrMem
ldi r26,NUM_BYTES ; bytes per number
mul r25,r26 ; offset of number
movw r26,r0 ; offset of number
subi r26,lo8(-(CalcStack)) ; address in calculator stack
sbci r27,hi8(-(CalcStack))
clr r1 ; restore R1 (R_ZERO)
call CalcExcXZ ; exchange numbers at X and Z (destroys R25, r24, R23)
; DESTROYS: R0
rjmp Disp ; display all
; ----------------------------------------------------------------------------
; Pgm Ind (0x62)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecPgmInd
ExecPgmInd:
; ----- get program index
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20
rcall ExecMemNum ; get memory index, with indirect from keyboard
brcs ExecPgm9 ; error
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcGetMem ; load number into stack
; OUTPUT: R24 = unsigned integer
; C flag is set = overflow valid range
; Z flag is set = number is positive or 0 (breq), NZ = number is negative (brne)
; DESTROYS: R31, R30, R25, R_M1..R_M10
; CALCULATOR STACK: -1
call CalcUnstackB ; load byte from the stack
brcs ExecPgm9 ; overflow
brne ExecPgm9 ; negative
rjmp ExecPgm2
; ----------------------------------------------------------------------------
; Pgm (0x36)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecPgm
ExecPgm:
; ----- INV - load current program index
breq 1f ; INV not set
rcall _EditStop ; stop edit mode
ldd r24,Y+DATA_PROGINX ; current program index
rcall ExecLoadByte ; load byte into X
IF_RUNNING
ret
rjmp ExdecPgm6
; ----- get program index
; OUTPUT: R24 = typically 0..159 (0 on break), but can be 0..0xFF
; C is set on invalid key from keyboard, break input (in such case R24 = 0)
; R1 = 0
; DESTROYS: R31, R30, R27..R25, R_M1..R_M10, R0
1: call Load2Dig ; load 2 digits (from user with indirect or from program)
brcs ExecPgm9 ; error
; ----- check program index
ExecPgm2:
ldd r25,Y+DATA_PROGNUM ; number of programs in library module
cp r25,r24 ; check program index
brcc 2f ; program index is OK
; OUTPUT: CY = error flag
; DESTROYS: -
jmp CalcError ; error
; ----- if running, open program later
2: IF_RUNNING ; if running
std Y+DATA_PROGNEXT,r24 ; next program index
IF_RUNNING ; if running
ExecPgm9:
ret
; ----- save address in main program
ldd r25,Y+DATA_PROGINX ; current program index
tst r25 ; main program
brne 4f ; not main program
ldd r25,Y+DATA_ADDR
std Y+DATA_SAVEADDR,r25
ldd r25,Y+DATA_ADDR+1
std Y+DATA_SAVEADDR+1,r25
; ----- open program
; INPUT: R24 = program index (0=main)
; DESTROYS: R31, R30, R24, R0
4: rcall OpenProg
; ----- inicialize current address (reset to begin, or restore main address)
ldd r24,Y+DATA_PROGINX ; current program index
tst r24 ; main program?
ldd r24,Y+DATA_SAVEADDR ; saved address
ldd r25,Y+DATA_SAVEADDR+1
breq 5f ; main program, restore old address
ldd r24,Y+DATA_PROGBEG ; program begin
ldd r25,Y+DATA_PROGBEG+1
5: std Y+DATA_ADDR,r24 ; set new current address
std Y+DATA_ADDR+1,r25
; ----- display program
ExdecPgm6:
; DESTROYS: -
call DispSetRow1 ; set cursor to start of ROW1
; ----- main program
ldd r24,Y+DATA_PROGINX ; current program index
tst r24 ; main program?
brne 2f ; not main program
; INPUT: R31:R30 = text in ROM (terminated with 0)
; DESTROYS: R31, R30, R24
ldi r30,lo8(MainProgText)
ldi r31,hi8(MainProgText)
call DispTextRom ; display text from ROM
rjmp 3f
; ----- module program
; INPUT: R24 = program index (1,...)
; OUTPUT: R31:R30 = program address in ROM
; DESTROYS: R0
2: ldd r24,Y+DATA_PROGNUM ; number of programs
inc r24 ; end of programs
rcall GetProgAddr ; get address of program end
; module name
lpm r24,Z+ ; module name
; INPUT: R24 = character or data
; DESTROYS: -
rcall _DispChar
lpm r24,Z
; INPUT: R24 = character or data
; DESTROYS: -
rcall _DispChar
; separator
ldi r24,'-'
; INPUT: R24 = character or data
; DESTROYS: -
rcall _DispChar
; program number
; INPUT: R24 = number
; DESTROYS: -
ldd r24,Y+DATA_PROGINX
rcall Disp2Dig ; display program number
; DESTROYS: -
call DispSpc
; program length
ldi r24,'('
; INPUT: R24 = character or data
; DESTROYS: -
rcall _DispChar
ldd r24,Y+DATA_PROGEND
ldd r25,Y+DATA_PROGEND+1
ldd r30,Y+DATA_PROGBEG
sub r24,r30
ldd r30,Y+DATA_PROGBEG+1
sbc r25,r30
; INPUT: R25:R24 = number
; DESTROYS: -
rcall Disp3Dig
ldi r24,')'
; INPUT: R24 = character or data
; DESTROYS: -
rcall _DispChar
; clear rest of line
; DESTROYS: -
3: call DispSpcClr
; wait some time (0.75 sec)
; DESTROYS: R24
call Wait250ms ; wait 250 ms
; DESTROYS: R24
call Wait250ms ; wait 250 ms
; DESTROYS: R24
call Wait250ms ; wait 250 ms
; restore display
; DESTROYS: -
rjmp DispFlags
; ----------------------------------------------------------------------------
; P->R (0x37)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecPr
ExecPr:
breq ExecPr4 ; not INV
; ----- INV, convert R->P (cartesian -> polar)
; input cartesian: x=Y, t=X
; output polar: x=angle, t=radius
; stop edit mode, validate X and display C if not running
rcall _EditStop ; stop edit mode
rcall _Calc ; calculator
.byte C_GETMEM(REG_T) ; load T (x) ... X
.byte C_GETMEM(REG_X) ; load X (x,y) ... Y
.byte C_RP ; convert to polar (r,a)
.byte C_FROMRAD ; convert from radians (r,a)
.byte C_SETMEMDEL(REG_X) ; save angle (r)
.byte C_SETMEMDEL(REG_T) ; save radius ()
.byte C_END
.balign 2
rjmp Disp ; display
; ----- not INV, convert P->R (polar -> cartesian)
; input polar: x=angle, t=radius
; output cartesian: x=Y, t=X
ExecPr4:
; stop edit mode, validate X and display C if not running
rcall _EditStop
rcall _Calc ; calculator
.byte C_GETMEM(REG_T) ; load radius (r)
.byte C_GETMEM(REG_X) ; load angle (r,a)
.byte C_TORAD ; convert to radians (r,a)
.byte C_PR ; convert to cartesian (x,y)
.byte C_SETMEMDEL(REG_X) ; save Y (x)
.byte C_SETMEMDEL(REG_T) ; save X ()
.byte C_END
.balign 2
rjmp Disp ; display
; ----------------------------------------------------------------------------
; LCD display contrast (0x5A)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecLCD
ExecLCD:
brne 4f ; INV is set
; set LCD contrast
; OUTPUT: R24 = typically digit 0..15 (0 on break), but can be 0..0xFF
; C is set on invalid key from keyboard, break input (in such case R24 = 0)
; R1 = 0
; DESTROYS: R31, R30, R27..R25, R_M1..R_M10, R0
rcall Load1Dig ; load 1 key from program or from user, with indirect
brcs 2f ; key is invalid
cpi r24,10 ; check max. value
brcc 2f ; out of range
std Y+DATA_LCDVO,r24 ; set contrast
; INPUT: R27:R26 = destination address
; R25 = data
; OUTPUT: R24 = old byte
mov r25,r24
ldi r26,lo8(CFG_LCD)
ldi r27,hi8(CFG_LCD)
call EEWrite ; write correction to EEPROM
; DESTROYS: R31, R30, R25..R20
; call LCD_Update ; update display
; ----- Restore LCD display
call LCD_Restore
rcall Disp
2: ret
; get LCD contrast
4: rcall _EditStop ; stop edit mode
ldd r24,Y+DATA_LCDVO ; current contrast
ExecLoadByte:
; INPUT: (R25:)R24 = unsigned integer
; DESTROYS: R31, R30, R25, R24, R_M1..R_M10, R0
; CALCULATOR STACK: +1
call CalcStackB ; stack unsigned byte R24
rcall _CalcSetMemDelX ; set X
SET_XVALID ; set X valid
; DESTROYS: R0
rjmp Disp ; display all
; ----------------------------------------------------------------------------
; Ind (0x40)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecInd
ExecInd:
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20
rcall ExecMemNum ; get memory index, with indirect from keyboard
brcs 9f ; error
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcGetMem ; load number into stack
; OUTPUT: R24 = unsigned integer
; C flag is set = overflow valid range
; Z flag is set = number is positive or 0 (breq), NZ = number is negative (brne)
; DESTROYS: R31, R30, R25, R_M1..R_M10
; CALCULATOR STACK: -1
call CalcUnstackB ; load byte from the stack
brcs 8f ; overflow
brne 8f ; negative
rjmp Exec ; execute key code
; OUTPUT: CY = error flag
; DESTROYS: -
8: call CalcError ; error
9: ret
; ----------------------------------------------------------------------------
; Get memory index, with indirect from keyboard
; ----------------------------------------------------------------------------
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20
; ----------------------------------------------------------------------------
.global ExecMemNum
ExecMemNum:
push r23
; DESTROYS: R27, R26, R25, R24, R20
rcall _EditStop ; stop edit mode
; OUTPUT: R24 = typically 0..159 (0 on break), but can be 0..0xFF
; C is set on invalid key from keyboard, break input (in such case R24 = 0)
; R1 = 0
; DESTROYS: R31, R30, R27..R25, R_M1..R_M10, R0
call Load2Dig ; load 2 digits, with indirect from keyboard
brcs 1f ; error
cpi r24,USER_NUM ; max. memory
brcs 2f ; OK
; OUTPUT: CY = error flag
; DESTROYS: -
call CalcError ; error
1: pop r23
ret
2: subi r24,-USER_FIRST
clc
pop r23
ret
; ----------------------------------------------------------------------------
; Get memory index indirected
; ----------------------------------------------------------------------------
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20, R_M1..R_M10
; ----------------------------------------------------------------------------
.global ExecMemNumInd
ExecMemNumInd:
push r23
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20
rcall ExecMemNum ; get memory index, with indirect from keyboard
brcs 9f ; error
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcGetMem ; load number into stack
; OUTPUT: R24 = unsigned integer
; C flag is set = overflow valid range
; Z flag is set = number is positive or 0 (breq), NZ = number is negative (brne)
; DESTROYS: R31, R30, R25, R_M1..R_M10
; CALCULATOR STACK: -1
call CalcUnstackB ; load byte from the stack
brcs 8f ; overflow
brne 8f ; negative
cpi r24,USER_NUM ; max. memory
brcc 8f ; invalid index
subi r24,-USER_FIRST ; real index
pop r23
clc
ret
; OUTPUT: CY = error flag
; DESTROYS: -
8: call CalcError ; error
9: pop r23
ret
; ----------------------------------------------------------------------------
; STO Ind (0x72)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecStoInd
ExecStoInd:
; ---- get memory index indirected
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20, R_M1..R_M10
rcall ExecMemNumInd
brcs ExecSto9 ; error
rjmp ExecSto2
; ----------------------------------------------------------------------------
; STO (0x42)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecSto
ExecSto:
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20
rcall ExecMemNum ; get memory index
brcs ExecSto9 ; error
ExecSto2:
push r24
ldi r24,REG_X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcGetMem ; load X into stack
pop r24
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: -1
rjmp _CalcSetMemDel ; set memory
ExecSto9:
ret
; ----------------------------------------------------------------------------
; RCL Ind (0x73)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecRclInd
ExecRclInd:
; ---- get memory index indirected
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20, R_M1..R_M10
rcall ExecMemNumInd
brcs ExecSto9 ; error
rjmp ExecRcl2
; ----------------------------------------------------------------------------
; RCL (0x43)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecRcl
ExecRcl:
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20
rcall ExecMemNum ; get memory index
brcs ExecSto9 ; error
ExecRcl2:
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcGetMem ; load memory into stack
rcall _CalcSetMemDelX ; set X from stack
; DESTROYS: R0
rjmp Disp ; display all
; ----------------------------------------------------------------------------
; SUM Ind (0x74)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecSumInd
ExecSumInd:
; ---- get memory index indirected
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R23, R20, R_M1..R_M10
rcall ExecMemNumInd
brcs ExecSto9 ; error
rjmp ExecSum2
; ----------------------------------------------------------------------------
; SUM (0x44)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecSum
ExecSum:
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20
rcall ExecMemNum ; get memory index
brcs ExecSto9 ; error
ExecSum2:
push r24
push r23
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcGetMem ; load memory into stack
ldi r24,REG_X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcGetMem ; load X into stack
pop r23
tst r23 ; INV ?
breq 2f ; INV not set
; DESTROYS: R31, R30, R25, R24
call CalcNeg ; negate X
; DESTROYS: all
; CALCULATOR STACK: -1
2: call CalcAdd ; add or subtract
pop r24
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: -1
rjmp _CalcSetMemDel ; set memory
; ----------------------------------------------------------------------------
; Inc Ind (0x6B)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecIncInd
ExecIncInd:
; ---- get memory index indirected
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20, R_M1..R_M10
rcall ExecMemNumInd
brcs ExecSto9 ; error
rjmp ExecInc2
; ----------------------------------------------------------------------------
; Inc (0x9c)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecInc
ExecInc:
; OUTPUT: R24 = memory index
; C = set if error
; DESTROYS: R31, R30, R27, R26, R25, R20
rcall ExecMemNum ; get memory index
brcs ExecSto9 ; error
ExecInc2:
push r24
push r23
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcGetMem ; load memory into stack
pop r23
tst r23 ; INV ?
breq 2f ; INV not set
; DESTROYS: R31, R30, R25, R24
call CalcDec ; decrement
rjmp 3f
; DESTROYS: all
; CALCULATOR STACK: -1
2: call CalcInc ; increment
3: pop r24
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: -1
rjmp _CalcSetMemDel ; set memory
; ----------------------------------------------------------------------------
; CMs (0x47)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecCms
ExecCms:
push r23
rcall _EditStop ; stop edit mode
; load constant 0
ldi r24,CONST_0
; INPUT: R24 = index of the constant in ConstTab
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall _CalcConst ; load constant 0
pop r23
tst r23 ; INV ?
breq 4f ; not INV, clear whole memory
rcall _EditStop ; stop edit mode
; clear registers X and T
ldi r24,REG_X
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
call CalcSetMem ; clear register X
ldi r24,REG_T