/
calc.4th
616 lines (547 loc) · 12.2 KB
/
calc.4th
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
\ simple calculator
base @ decimal
30 constant box-width
30 constant box-height
: draw-box ( x y -- )
lcd-move-to lcd-black
box-width box-height lcd-box
;
: (highlight-box) ( x y -- )
lcd-move-to
1 1 lcd-move-rel
box-width 2 - box-height 2 - lcd-box
1 1 lcd-move-rel
box-width 4 - box-height 4 - lcd-box
;
: highlight-box ( x y -- )
lcd-black (highlight-box)
;
: unhighlight-box ( x y -- )
lcd-white (highlight-box)
lcd-black
;
: within-box ( x y x0 y0 x1 y1 -- flag )
swap >r rot >r \ x y y0 y1
within \ x flag
swap r> r> \ flag x x0 x1
within and
;
48 constant y-offset
4 font-width * constant x-offset
create positions
\ x is multiple of font-width
0 font-width * x-offset + c, 0 y-offset + c, char 1 c, bl c,
4 font-width * x-offset + c, 0 y-offset + c, char 2 c, bl c,
8 font-width * x-offset + c, 0 y-offset + c, char 3 c, bl c,
12 font-width * x-offset + c, 0 y-offset + c, char + c, bl c,
16 font-width * x-offset + c, 0 y-offset + c, char C c, bl c,
0 font-width * x-offset + c, 32 y-offset + c, char 4 c, bl c,
4 font-width * x-offset + c, 32 y-offset + c, char 5 c, bl c,
8 font-width * x-offset + c, 32 y-offset + c, char 6 c, bl c,
12 font-width * x-offset + c, 32 y-offset + c, char - c, bl c,
16 font-width * x-offset + c, 32 y-offset + c, char M c, char C c,
0 font-width * x-offset + c, 64 y-offset + c, char 7 c, bl c,
4 font-width * x-offset + c, 64 y-offset + c, char 8 c, bl c,
8 font-width * x-offset + c, 64 y-offset + c, char 9 c, bl c,
12 font-width * x-offset + c, 64 y-offset + c, char * c, bl c,
16 font-width * x-offset + c, 64 y-offset + c, char M c, char + c,
0 font-width * x-offset + c, 96 y-offset + c, char . c, bl c,
4 font-width * x-offset + c, 96 y-offset + c, char 0 c, bl c,
8 font-width * x-offset + c, 96 y-offset + c, char = c, bl c,
12 font-width * x-offset + c, 96 y-offset + c, char / c, bl c,
16 font-width * x-offset + c, 96 y-offset + c, char M c, char R c,
20 constant position-count
: box-data ( u -- x y c1 c2 )
dup 0< if drop 0 then
dup position-count < 0= if
drop 0
then
2* 2* positions + dup c@
swap char+ dup c@
swap char+ dup c@
swap char+ c@
;
: inside-box ( x y u -- flag )
box-data 2drop \ x y x0 y0
over box-width + \ x y x0 y0 x1
over box-height + \ x y x0 y0 x1 yi
within-box
;
variable v-sign
variable v-int
variable v-frac
variable v-point
variable v-op
variable total-sign
variable total-int
variable total-frac
variable mem-sign
variable mem-int
variable mem-frac
variable auto-clear
\ define 8.8 number
99999999 constant v-max
v-max 1+ constant v-overflow
8 constant int-digits
8 constant frac-digits
: half-add ( carry u1 u2 -- u carry )
+ + dup v-max > if
v-max - 1- 1
else
0
then
;
: half-sub ( borrow u1 u2 -- u borrow )
- swap - dup 0< if
v-max + 1+ 1
else
0
then
;
: add ( -- )
v-sign @ total-sign @ =
if
0 total-frac @ v-frac @ half-add \ frac carry
total-int @ v-int @ half-add \ overflow
if
2drop v-max v-max
then
else
total-int @ v-int @ 2dup < if
2drop true
else
= if
total-frac @ v-frac @ <
else
false
then
then
if
0 v-frac @ total-frac @ half-sub \ frac borrow
v-int @ total-int @ half-sub
v-sign @ total-sign !
else
0 total-frac @ v-frac @ half-sub \ frac borrow
total-int @ v-int @ half-sub
then
drop
then
2dup or 0= if
false total-sign !
then
total-int !
total-frac !
;
: mem-add ( -- )
total-sign @ mem-sign @ =
if
0 mem-frac @ total-frac @ half-add \ frac carry
mem-int @ total-int @ half-add \ overflow
if
2drop v-max v-max
then
else
mem-int @ total-int @ 2dup < if
2drop true
else
= if
mem-frac @ total-frac @ <
else
false
then
then
if
0 total-frac @ mem-frac @ half-sub \ frac borrow
total-int @ mem-int @ half-sub
total-sign @ mem-sign !
else
0 mem-frac @ total-frac @ half-sub \ frac borrow
mem-int @ total-int @ half-sub
then
drop
then
2dup or 0= if
false mem-sign !
then
mem-int !
mem-frac !
;
: sub ( -- )
v-sign @ dup invert v-sign !
add
v-sign !
;
: mul ( -- )
total-sign @ v-sign @ <> total-sign !
total-frac @ v-frac @ um*
v-overflow fm/mod
total-int @ v-frac @ um*
v-overflow fm/mod
>r +
total-frac @ v-int @ um*
v-overflow fm/mod
>r +
s>d v-overflow fm/mod
total-int @ v-int @ um*
v-overflow fm/mod
>r + r> swap
r> + r> +
s>d v-overflow fm/mod
rot +
if
2drop
v-max v-max
then
total-int !
total-frac !
v-overflow 2/ > if
total-frac @ 1+ v-max > if
total-int @ 1+ v-max > if
v-max v-max
total-int !
total-frac !
else
0 total-frac !
1 total-int +!
then
else
1 total-frac +!
then
then
;
variable r-int
variable r-frac
variable rs-int
variable rs-frac
variable q-int
variable q-frac
: div-gr ( -- flag )
r-int @ total-int @ >
if
true exit
then
r-int @ total-int @ =
r-frac @ total-frac @ > and
;
: div-q*10 ( -- )
q-frac @ 10 *
s>d v-overflow fm/mod
q-int @ 10 * +
q-int ! q-frac !
;
: div-r*10 ( -- )
r-frac @ 10 *
s>d v-overflow fm/mod
r-int @ 10 * +
r-int ! r-frac !
;
: div-t*10 ( -- )
total-frac @ 10 *
s>d v-overflow fm/mod
total-int @ 10 * +
total-int ! total-frac !
;
: div ( -- )
v-int @ v-frac @ or 0=
if
v-max v-max
total-int !
total-frac !
exit
then
total-int @ total-frac @ or 0=
if
exit
then
v-sign @ total-sign @ xor total-sign !
v-int @ dup r-int ! rs-int !
v-frac @ dup r-frac ! rs-frac !
0 q-int ! 0 q-frac !
\ shift count
div-gr if
1
else
0
then
begin
div-gr 0=
while
r-frac @ rs-frac !
r-int @ rs-int !
div-r*10
1+
repeat
rs-frac @ r-frac !
rs-int @ r-int !
\ loop count on stack
frac-digits +
0 ?do
div-q*10
begin
0 total-frac @ r-frac @ half-sub
total-int @ r-int @ half-sub 0=
while
total-int !
total-frac !
1 q-frac +! \ never gets bigger than 9 so no need for carry
repeat
2drop
div-t*10
loop
q-frac @ total-frac !
q-int @ total-int !
;
: output-number ( frac int sign x y -- )
lcd-at-xy
if
[char] -
else
bl
then
lcd-emit
8 lcd-u.r
[char] . lcd-emit
s>d <# # # # # # # # # #> lcd-type
;
: refresh-display ( -- )
total-frac @
total-int @
total-sign @
5 1 output-number
lcd-space [char] T lcd-emit
v-frac @
v-int @
v-sign @
5 2 output-number
26 1 lcd-at-xy
v-op @
case
-1 of bl endof
0 of bl endof
1 of [char] + endof
2 of [char] - endof
3 of [char] * endof
4 of [char] / endof
endcase
lcd-emit
mem-frac @
mem-int @
mem-sign @
5 lcd-text-rows 2 - output-number
lcd-space [char] M lcd-emit
;
: clear-mem ( -- )
false mem-sign !
0 mem-int !
0 mem-frac !
;
: clear-entry ( -- )
false v-sign !
0 v-int !
0 v-frac !
0 v-point !
false auto-clear !
v-op @ -1 = if
0 v-op !
then
;
: clear-all ( -- )
clear-entry
0 v-op !
false total-sign !
0 total-int !
0 total-frac !
;
: recall-mem ( -- )
clear-entry
mem-sign @ v-sign !
mem-int @ v-int !
mem-frac @ v-frac !
;
: copy-to-total ( -- )
v-sign @ total-sign !
v-int @ total-int !
v-frac @ total-frac !
;
: new-digit ( u -- )
auto-clear @ if
clear-entry
then
v-point @ if
v-point @ 0> if
v-point @ 10 / dup
0= if
drop -1 v-point !
else
dup v-point !
* v-frac +!
then
then
else
v-int @ 10 * + dup v-max > if
drop v-max
then
v-int !
then
;
: operator-set ( u -- )
v-op @
case
0 of copy-to-total endof
1 of add endof
2 of sub endof
3 of mul endof
4 of div endof
endcase
v-op !
true auto-clear !
;
: process ( u -- )
case
0 of \ 1
1 new-digit
endof
1 of \ 2
2 new-digit
endof
2 of \ 3
3 new-digit
endof
3 of \ +
1 operator-set
endof
4 of \ C
clear-all
endof
5 of \ 4
4 new-digit
endof
6 of \ 5
5 new-digit
endof
7 of \ 6
6 new-digit
endof
8 of \ -
2 operator-set
endof
9 of \ MC
clear-mem
endof
10 of \ 7
7 new-digit
endof
11 of \ 8
8 new-digit
endof
12 of \ 9
9 new-digit
endof
13 of \ *
3 operator-set
endof
14 of \ M+
mem-add
endof
15 of \ .
auto-clear @ if
clear-entry
then
v-overflow v-point !
endof
16 of \ 0
0 new-digit
endof
17 of \ =
-1 operator-set
endof
18 of \ /
4 operator-set
endof
19 of \ MR
recall-mem
endof
endcase
refresh-display
;
variable down
variable box-number
variable can-exit
: calculator ( -- )
lcd-cls
button-flush
key-flush
ctp-flush
can-exit @ if
24 lcd-text-rows 1- lcd-at-xy s" Exit" lcd-type
then
position-count 0 ?do
i box-data >r >r 2dup draw-box
8 + >r font-width + r> lcd-move-to
r> lcd-emit r> lcd-emit
loop
false down !
refresh-display
-1 box-number !
begin
ctp-pos? if
ctp-pos dup 0<
if
2drop
down @ if
box-number @ dup
box-data 2drop
unhighlight-box
1 1 lcd-at-xy 2 lcd-spaces
0 0 unhighlight-box
process
-1 box-number !
then
false down !
else
position-count 0 ?do
2dup i inside-box if
true down !
box-number @ 0< 0=
box-number @ i <> and
if
box-number @
box-data 2drop
1 1 lcd-at-xy 2 lcd-spaces
unhighlight-box
0 0 unhighlight-box
then
i dup box-number !
box-data
swap 1 1 lcd-at-xy lcd-emit lcd-emit
highlight-box
0 0 highlight-box
then
loop
2drop
then
then
key? if
key-flush
then
button? if
button
case
button-left of
endof
button-centre of
endof
button-right of
can-exit @ if
exit
then
endof
button-power of
power-off
endof
endcase
then
wait-for-event
again
;
base !