-
Notifications
You must be signed in to change notification settings - Fork 1
/
fltptlib.z80
1501 lines (1237 loc) · 38 KB
/
fltptlib.z80
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
;AUTHOR: ROALD FREDERICKX
;DATE: started @ 21/04/2009
;DESCRIPTION:
;24 bit floating point routines for the z80 microprocessor
;NUMBER FORMAT:
; 8bits exponent -- two's complement
; 16bits base -- bit 15 is sign bit => only 15bits precision
;Roughly meaning:
;smallest non-zero value: 2.938e-39
;largest value from exponent: 1.7014e38
;base values: -32767 to 32767
;largest total value: 5.575e42
;significant digits (decimal): "4.5"
;GENERAL NOTES:
; I'm trying to keep everything in-register.
; Outputs will always be:
; mantisse: HL
; exponent: C
; Inputs are in the form of:
; mantisse1: HL
; exponent1: C
; mantisse2: DE
; exponent2: B
; Flags:
; Overflows will be denoted with either the Carry of the Parity/overflow flag
;
; Speed gets priority above size most of the time
;Frequently used stuff:
;======================
;OVERFLOW
;--------
;We probably won't be able to jump to here using JR, but that is a bonus, since hopefully overflows
;won't happen too often, and when the condition is negative, JP is actually faster than JR :-)
floatOverflowPE:
LD A,%01111111 ;+127
INC A ;+127 + 1 -> -128 => overflow flag set
RET ;return
;NORMAL RETURN
;-------------
;We need to reset the overflow flag...
floatReturnPVreset:
XOR A ; A = 0
INC A ; 0 + (+1) = +1, so overflow flag is reset
RET
;Debugging stuff:
;================
;Pushes everyting, including OP1 and OP2, doesn't alter registers
#define PUSHALL push af\ push bc\ push de\ push hl\ push ix\ push af\ push bc\ push de\ push hl\ push ix\ bcall(_PushRealO1)\ bcall(_PushRealO2)\ pop ix\ pop hl\ pop de\ pop bc\ pop af
;Pops everything that has been pushed with PUSHALL
#define POPALL bcall(_PopRealO2)\ bcall(_PopRealO1)\ pop ix\ pop hl\ pop de\ pop bc\ pop af
;PRINT A
;-------
printa:
PUSHALL
push af
bcall(_NewLine)
pop af
ld h,0
ld l,a
bcall(_DispHL)
POPALL
ret
;PRINT HL & C
;------------
printhlc:
PUSHALL
push bc
push hl
bcall(_NewLine)
ld A,7
ld (CurCol),A
pop hl
bcall(_DispHL)
ld A,0
ld (CurCol),A
pop hl
ld h,0
bcall(_DispHL)
POPALL
ret
;PRINT DE AND B
;--------------
printdeb:
EX DE,HL
push bc
ld c,b
call printhlc
pop bc
EX DE,HL
ret
;PRINT BCD FLOATING POINT NUMBER IN OP1
;--------------------------------------
printop1:
PUSHALL
bcall(_NewLine)
ld A,(IY+fmtFlags)
ld (IY+fmtOVerride),A ;use current formatting, is this necessary??
bcall(_FormBase) ;gets OP1 to a string starting at OP3
ld hl,OP3 ;load pointer
bcall(_PutS) ;print it
POPALL
ret
printop2:
PUSHALL
bcall(_OP2toOP1)
call printop1
POPALL
ret
;PRINT C-HL AS A FLOATING POINT
;------------------------------
pchl:
PUSHALL
call fltpttoop1
call printop1
POPALL
ret
;PRINT B-DE AS A FLOATING POINT
;------------------------------
pbde:
PUSHALL
ld c,b
ex de,hl
call fltpttoop1
call printop1
POPALL
ret
;PRINT MARKER
;------------
_printmarker: ;gets pointer to string in hl
PUSHALL
push hl
bcall(_NewLine)
pop hl
bcall(_PutS)
POPALL
ret
printmrk1:
push hl
ld hl,marker1
call _printmarker
pop hl
ret
printmrk2:
push hl
ld hl,marker2
call _printmarker
pop hl
ret
printmrk3:
push hl
ld hl,marker3
call _printmarker
pop hl
ret
printmrk4:
push hl
ld hl,marker4
call _printmarker
pop hl
ret
printmrk5:
push hl
ld hl,marker5
call _printmarker
pop hl
ret
printmrk6:
push hl
ld hl,marker6
call _printmarker
pop hl
ret
marker1:
.DB "MARKER1",0
marker2:
.DB "MARKER2",0
marker3:
.DB "MARKER3",0
marker4:
.DB "MARKER4",0
marker5:
.DB "MARKER5",0
marker6:
.DB "MARKER6",0
;PAUSE
;-----
pause:
push hl
push af
ld hl,0
pauseloop:
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
dec hl
ld a,h
or l
jr nz,pauseloop
pop af
pop hl
ret
;MINIPAUSE
minipause:
push bc
ld b,100
minipauseloop:
djnz minipauseloop
pop bc
ret
;ENTER-PAUSE
;-----------
enterpause:
PUSHALL
bcall(_GetKey)
POPALL
ret
;=========================================================================================
; DIFF
;=========================================================================================
; See: SUM
diff:
FLTPTsub:
;just negate DE and add
LD A,D
XOR %10000000 ;flip sign bit
LD D,A
;natural fall-through to sum below:
;=========================================================================================
; SUM
;=========================================================================================
; Destroys: xxxxxxxxx
; P/V denotes overflow
sum:
FLTPTadd:
;test for one of the arguments being zero
;if DE is zero, return is easy
LD A,D
AND %01111111 ;loose the sign flag (might get set doing a FLTPTsub, or it might
;have been set in another strange way ... either way: it's irrelevant)
OR E
JP Z,floatReturnPVreset ;just return: DE is zero, so HL and C make up the answer
;test for HL being zero
LD A,H
AND %01111111
OR L
JR NZ,sumBothNot0
;HL was zero: return DE,B in HL,C and reset P/V
LD C,B
EX DE,HL
XOR A
INC A ;P/V is now reset
RET
sumBothNot0:
;get our mantissas in two's complement form for easy adding
;NOTE: isn't there a nicer way to do this, using NEG for example, or bitmaksing the
;sign bit and doing a: SUB HL,DE with HL being 0 and DE being the number to negate...?
BIT 7,H
JR Z,sumFirstPos
;no need to bitmask the signbit either, since it is zero
;negation in two's complement == flip bits and add one
LD A,L ;start with lower byte (even though we already had upper byte,
;we need to know if the lower byte triggered a carry or not)
CPL ;XOR %11111111
;do the negation the manual (and slow) way instead of NEG because...
ADD A,1 ;... we need to get the carry here ...
LD L,A
LD A,H
JR C,sumFirstNegCarry ;... to test for it here ...
;no carry, so just flip bits, keep sign bit intact (it is already set in our format
;and we want to keep it set to have a correct two's complement format)
XOR %01111111
LD H,A
JR sumFirstPos ;well, "it is now" :-)
sumFirstNegCarry:
XOR %01111111
INC A ;... to add it here.
;Can we get an "overflow" here? That happens if A was 1000 0000 (-0) before
;flipping and now reads 1111 1111 + 1 = (1) 0000 0000, which is OK (+0).
;No "true" overflow can happen here [[ NOTE: VERIFY THIS TO BE SURE ]] )
LD H,A ;store
;NOTE: the above looks hideous, but a simple XOR & ADC won't do because XORing A will
;reset the carry flag before it can be added tot the flipped number :-(
;NOTE2: maybe do a CPL and an ADC 128 (CPL doesn't change carry flag, and ADC adds
;the carry flag and re-sets bit 7 (( is this faster/smaller? ))
;NOTE3: better yet, maybe I should just push/pop de and do a:
;ld de,0 \ ex de,hl \ and a \ sbc hl,de
;but then i'd have to unset signflag first, so probably not faster :-(
;[TODO: check to be sure]
sumFirstPos:
;test for the second argument being negative
BIT 7,D
JR Z, sumSecondPos
LD A,E
CPL ;XOR %11111111
ADD A,1 ;INC A doesn't set carry >_<
LD E,A
LD A,D
JR C,sumSecondNegCarry
XOR %01111111
LD D,A
JR sumSecondPos
sumSecondNegCarry:
XOR %01111111
INC A
LD D,A
;arguments are now in two's complement form
sumSecondPos:
LD A,C ;grab our first exponent to work with
SUB B ;substract second exponent from first one
JR Z,sumAdd ;if both are the same: just add
;We now need to find out wether C or B is the larger one
;remeber that we're dealing with signed numbers, so a simple JR C,xx
;won't do here.
;It follows that C=A > B <=> Sign and P/V flags are the same
;In the code below, we switch the mantissa's (HL and DE) in such a way
;that the one representing the largest value is in HL. The corresponding
;exponent will be in C. This means that we know that the difference in
;the exponents (1st - 2nd) will be positive. Therefore, we make the
;difference (in A) an *un*signed number below, to ease comparison *and*
;to avoid overflow-issues. We only need this number to determin how
;many bits to shift the smaller mantissa (DE) before we add them.
;http://dragonfire.unitedti.org/asmin28/lesson/day08.html#sig
;TODO: do some XOR magic with sign flags
;TODO: do some XOR magic with sign flags
;TODO: do some XOR magic with sign flags
;ugly way... TODO: find alternative
;sub won't do ... signed overflows >_<
BIT 7,C
JR Z,sumCpos
;C is negative
BIT 7,B
JR Z,sumDeltaExpSwap ;C negative, B positive => swap numbers
;both are negative
;A still holds C-B
;we now still need to check wich one was larger, we can use the sign
;flag for that, because there can't have been any signed overflows
;(subtracting numbers of like sign always decreases the absolute value)
AND A ;set flags
JP M,sumDeltaExpSwap
JR sumDeltaExpPositive
sumCpos:
BIT 7,B
JR Z,sumCposBpos
;C is positive, B negative: numbers in correct order and A hold a positive number
JR sumDeltaExpPositive
sumCposBpos:
;watch sign of A for the comparison, there can't have been an overflow
;(sub of like signs)
AND A
JP P,sumDeltaExpPositive
;and fall-through to swap
sumDeltaExpSwap:
;result was negative: swap
NEG ;make A positive
LD C,B ;get largest exponent in C to use later on
EX DE,HL ;and swap mantissas
sumDeltaExpPositive:
;difference of exponents is strictly positive ("|HL| is larger than |DE|")
;difference is stored in A as an unsigned number
CP 15
JR NC,sumReformatFrom2cpl
;We need to shift 16b or more, this will just have the effect
;of adding zero, so return with HL being sum and C being exponent.
CP 8 ;see if we can discard the LSB of DE
JR C,sumShiftLessThan8 ;we need to shift strictly less than 8 bits,
;so we need to keep an eye on both bytes (including MSB)
;we need to shift 8 bits or more, either way: we can switch the MSB of DE to the LSB
;and clear the MSB, but keeping an eye on the sign!
LD E,D
LD D,0 ;zero MSB -- note, this is only OK if number was positive, so:
BIT 7,E ;grab sign-bit
JR Z,sum8orLessDEPositive
DEC D ;dec D to get 11111111
sum8orLessDEPositive:
SUB 8 ;We've already shifted 8 bits
JR Z,sumAdd ;we needed to shift 8 bits, which we just did, so we're ready to add
;we need to shift the LSB between 1 and 7 bits. Do so here:
LD B,A ;set counter
sumSMT8Loop:
SRA E; ;arithemetic shift, b7 (sign) gets preserved
DJNZ sumSMT8Loop
JR sumAdd; ;ready to add!
;we need to shift less than 8 bits, so keep an eye on both bytes of DE:
sumShiftLessThan8:
LD B,A ;set our counter
sumSLT8Loop:
SRA D ;arithmetic shift, preserves sign (b7 gets preserved); b0 to C
RR E ;rotate, C to b7
DJNZ sumSLT8Loop ;loopty-loop
;I've put this part at the bottom (shifting less than 8, so I don't need a jump
;to sumAdd here. I'm guessing that it will be more common to add values that are
;close together, so give them the speed-advantage
;Finally, do the actual adding!
;Wat we've got: HL and DE are nicely aligned to add, and C contains the exponent of HL
;(the largest number)
sumAdd:
;we need to add each byte seperatly because for some reason doing an ADD HL,DE
;doesn't set the P/V flag, so no way of checking for overflows
;ADD HL,DE: 11 T-sates
LD A,L ;4
ADD A,E ;4
LD L,A ;4
LD A,H ;4
ADC A,D ;4 add with carry, P/V flag gets set here
LD H,A ;4
;total: 24 T-states, that's 13 extra for getting an overflow flag >_<
JP PO,sumReformatFrom2cpl
;we had an overflow, so the sum of two positives became negative, or the sum
;of two negatives became positive
JP P,sumOverflowPositive
;we overflowed from two positives to a "negative answer" (the MSbit got set,
;but it isn't supposed to be a sign flag)
SRL H ;shift HL to the right, putting a zero at b15 (answer is positive!)
RR L
INC C
;that's all folks, no need to negate the answer or annything ...
RET ;note: P/V correctly indicates overflows
sumOverflowPositive:
;we overflowed from two negatives to a positive answer
;we need to shift the entire positive answer to the right, and set the
;sign bit, then reformat it (it should still be negated!)
SCF ;set carry flag
RR H ;rotate H to the right, putting a 1 at b15 (to indicate negative answer)
RR L
INC C ;increment exponent
; RET ;all is set, we've got sign flag at b15 of HL and the rest of
; ;HL represents the 15 bytes of the positive mantisse
; ;also: the P/V flag correctly indicates overflows (it's unset if the INC C
; ;went smoothly, or set if the INC C overflowed, in which case we can't
; ;store the number in our floating point format)
;reformatting necesarry! (two's compelement)
;natural fall-through:
sumReformatFrom2cpl:
;if there was no overflow, we need to negate the answer if it is negative,
;we also need a way to save a sign-flag bitmask, do so here:
LD D,0 ;empty bitmask in D
BIT 7,H
JR Z,sumNoOverPositive
;two's complement answer is negative -> negate answer
;don't append a sign flag yet
ex de,hl
ld hl,0
and a ;clear carry
sbc hl,de ; hl(=0) - de('=hl) - carry
; => hl now holds the negated value
ld d,$80 ;and set bitmask
sumNoOverPositive: ;HL is now positive and signmask is in D
;now shift everything to the left until bit 14 of HL (b6 of H) becomes 1
;first check for the answer being zero so we won't loop infinitly...
LD A,H
OR L ;HL == 0 <=> ( H | L ) == 0
JR NZ,sumNotZero
;it's zero!
LD C,0 ;NOTE, MAYBE PICK $80 (-128), FOR IT IS THE SMALLEST POSSIBLE NUMBER
;so that, for example, in an addition, it always gets recognised as
;smallest...
XOR A ;zero A TODO: better way to reset P/V?
;A is now zero, so do an INC to reset P/V flag and return
INC A
RET
sumNotZero:
BIT 6,H
JR NZ,sumReformat ;if bit 6 of H is set, then no need to shift
sumShiftloop:
ADD HL,HL ;HL * 2
DEC C ;don't forget to adjust exponent...
JP PE,floatOverflowPE
BIT 6,H
JR Z,sumShiftLoop
;ready to reformat:
sumReformat:
;set sign flag according to mask
LD A,H
OR D ;OR with bitmask in D
LD H,A
;all done: return and reset P/V
XOR A
INC A
ret
;=========================================================================================
; PROD
;=========================================================================================
;P/V denotes overflow
;Destorys: a lot, !!INCLUDING IX!!
;NOTE: MAYBE HANDLE AN OVERFLOW "IN THE TINY DIRECTION" BY RETURNING ZERO? (in case
;some rouding errors end up with litlle values that should actually be zero, etc etc ..?)
prod:
FLTPTmult:
;check for at least one value being zero:
;if HL is zero, return is easy...
LD A,H
AND %01111111 ;ignore sign flag
OR L
JR NZ,prodHLnot0 ;wasn't zero
JP floatReturnPVreset ;HL was zero, so return (I assume that the exponent C is
;set correctly, also being zero)
prodHLnot0:
LD A,D
AND %01111111 ;ignore sign flag
OR E
JR NZ,prodDEnot0
;it's zero, so return zero:
EX DE,HL ;faster than: LD HL,0
LD C,B ;faster than: LD C,0. I assume B is correctly zero
;reset P/V:
XOR A
INC A
RET
prodDEnot0:
;let's compute the resulting exponent first, see if we won't overflow there
;we'll be multiplying two positive numbers of "15 bits" (upper bit is sign and will be made zero)
;the result will be a 30bit number, (upper two bits zero). We'll need to shift it to the
;left at least once (we only need "one zero bit" in front, the sign bit). But it's also
;possible we'll need to shift twice as we also might end up with a "29 bit" number, eg:
; 010 * 010 = 000100 "29 bit" -> 010000 (exponent--)
; 011 * 011 = 001001 "30 bit" -> 010010 (exponent is OK)
; ^sign ^sign
LD A,C
SCF ;set carry
ADC A,B ;A = B + C + 1 TODO: why do i need this??
RET PE ;overflow can happen (using ADC: only need to check once)
.DB $DD
LD L,A ;no overflow, store exponent in IXL
;no overflow: too bad, we actualy need to get numbercrunching
;use XOR to find sign of outcome
LD A,H
XOR D ;XOR MSB of first argument with MSB of second argument
AND %10000000 ;if an uneven number of "sign flags" -> b7 is high
.DB $DD
LD H,A ;get the sign-bitmask stored in IXH for later use
;clear the sign bits
LD A,H
AND %01111111
LD H,A
LD A,D
AND %01111111
LD D,A
;multiply:
;BC is free to modify, HL and DE need to be multiplied (A is also free)
LD B,15 ;set up our counter NOTE: scince b15 of HL is zero, we only need
;to loop over it 15 times actually, [IMPROVE THIS IN THE FUTURE]
LD C,H ;get HL to "CA" -> multiply CA by DE
LD A,L
LD HL,0 ;lower 16 bits of result
;it looks as though we've got ourselves 16b of storage space too little, but as we
;shift through CA, we free up bits that we can use to store the upper 16 bits of
;the result in! => multiply CA by DE, result in CAHL
;inspiration: http://map.grauw.nl/articles/mult_div_shifts.php
prodLoop:
ADD HL,HL ;shift HL to the left ("HL * 2")
RLA ;get carry to A if any and rotate the bits of the argument
RL C ;we've now rotated "CA" to the left (or AC to the right) getting
;bit number B of the original AC in the carry
JR NC,prodNoAdd ;if the original AC didn't contain a 1 at position B, don't add
ADD HL,DE
ADC A,0 ;add carry to A
JR NC,prodNoAdd ;
INC C ;if above created carry on A: increment C
prodNoAdd:
DJNZ prodLoop
;the 16'th iteration can be done by just shifting CAHL to the left,
;because bit15 of the (second) multiplicand is always zero
;We only need to shift CAH now, HL will be discarded anyway
SLA H ;grab carry
RLA ;rotate A
RL C ;rotate C
;as explained above, we always need to shift once more
SLA H
RLA
RL C
;there is a chance we'll need to shift once more, test for it here:
BIT 6,C
JR NZ,prodNoExtraShift
SLA H
RLA
RL C
.DB $DD
DEC L ;don't forget to decrement exponent (IXL)
RET PE ;overflow
prodNoExtraShift:
LD L,A ;get A stored in L, it is ready to be returned now
LD A,C ;get the upper byte of answer in A, so we can set sign flag
.DB $DD
OR H ;OR A with IHX, our signbitmask
LD H,A ;HL is now correct for return
.DB $DD
LD C,L ;and get exponent from IXL
;all went wel to return: clear P/V
XOR A
INC A
RET
;=========================================================================================
; FLTPTCOMP
;=========================================================================================
;Compare two floating point numbers.
;Returns: Carry gets set if (C-HL) < (B-DE)
;Destroys: A, C-HL and B-DE
;NOTE: I HAVEN'T TESTED ANY OF THIS YET !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FLTPTcomp:
;first see if one is zero (because sign and exponent will be random then)
LD A,D
AND %01111111
OR E
JR NZ,FLTPTcompDEnot0
;B-DE was zero: sign of C-HL determines relation
SLA H ;Sign flag of C-HL to carry. This gives us a correct return value
RET
FLTPTcompDEnot0:
;maybe C-HL is zero?
LD A,H
AND %01111111
OR L
JR NZ,FLTPTcompBothNot0
LD A,D
CPL ;flip all bits (including sign)
SLA A ;carry gets set correctly
RET
FLTPTcompBothNot0:
;compare signs
LD A,H
AND A
JP P,FLTPTcompHLpos
;C-HL is negative
LD A,D
AND A
JP M,FLTPTcompBothNegative
;C-HL is negative, B-DE is positive: return with carry set
SCF
RET
FLTPTcompHLpos:
LD A,D
AND A
JP P,FLTPTcompBothPositive
;C-HL is positive, B-DE is negative: return with carry cleared
;note that the "AND A" above has left carry unset, so just return
RET
FLTPTcompBothNegative:
;both are negative: make positive and swap values, continue as if they are positive
;swap exponents:
LD A,C
LD C,B
LD B,A
;make mantissas positive
LD A,H
AND %01111111
LD H,A
LD A,D
AND %01111111
LD D,A
EX DE,HL ;swap mantissas
;fall-through to FLTPTcompBothPositive
FLTPTcompBothPositive:
;compare exponents
LD A,C
SUB B
JR Z,FLTPTcompExpEqual
;Exponents not equal, give this the speed-advantage
;compare sign of exponents. Note that they are signed values.
;http://dragonfire.unitedti.org/asmin28/lesson/day08.html#sig
JP PO, $+5 ;no overflow, skip the XORing ("XOR with zero")
XOR $80 ;XOR sign bit
;We can now use M for (C < B), P for (C >= B)
;note: we already know: C != B
;So return with carry unset if we have M flag, else return with carry set
RET P ;note that XORing has unset carry
SCF
RET
FLTPTcompExpEqual:
;both HL and DE are now positive (unsigned) numbers
;subtract them and carry gives what we want
AND A ;reset carry for the subtraction
SBC HL,DE
RET ;carry correctly indicates the relation
;=========================================================================================
; TOA
;=========================================================================================
;Converts a floating point number in C-HL to a regular signed number in A.
;No rounding gets done.
;Carry flag denotes overflow.
;Destroys HL,B
;NOTE: -128 will be regarded as an overflow and will not be returned as -128!
toA:
FLTPTtoA:
;Get HL in two's complement form. Note that I do this before testing for
;trivial cases to make return easy when C is 6.
BIT 7,H
JR Z,toACheckTriv ;it's positive
LD A,L
CPL ;XOR %11111111
ADD A,1 ;INC A doesn't set carry >_<
LD L,A
LD A,H
JR C,toANegCarry;
XOR %01111111
LD H,A
JR toACheckTriv
toANegCarry:
XOR %01111111
INC A
LD H,A
toACheckTriv:
;Check for trivial cases. If C is less than zero, output will be zero.
;So, check sign bit of C
BIT 7,C
JP NZ,toAReturnZero ;if C is strictly less than zero
LD A,6
SUB C ;we need to shift (6-C) bits
;because: the mantissa is a "1.xxx number". We multiply
;it with 2^C to get the full number. C can be 6 at most,
;because a signed byte can only hold up to 2^6 * "1.999"
;NOTE: we regard (-128) as an overflow here, although that
;is a perfectly acceptable signed number...
LD B,A ;B now stores the number of bits to shift to the right
RET C ;if (6-C) triggers a carry, then C was 7 or more and
;an overflow will happen, so return with carry set
LD A,H ;get the number to shift in A
RET Z ;if (6-C) is zero (C=6), then no need to shift
;we can just return with carry correctly unset, and A
;containing the right number in the right form
;B is set up correctly as counter (1 <= B <= 5), so just start shifting:
toAShift:
SRA A
DJNZ toAShift
AND A ;clear carry
RET ;all done!
toAReturnZero:
XOR A ;zero A and clear carry in one swing
RET
;=========================================================================================
; TOAROUND
;=========================================================================================
;Converts a floating point number in C-HL to a regular signed number in A.
;Number gets rounded.
;P/V flag denotes overflow, [WARNING:] this is unlike the above routine which uses carry!!
;Destroys HL,B,C
;NOTE: -128 will be regarded as an overflow and will not be returned as -128!
toAround:
FLTPTtoAround:
;Get HL in two's complement form.
BIT 7,H
JR Z,FLTPTtoAroundCheckTriv ;it's positive
LD A,L
CPL ;XOR %11111111
ADD A,1 ;INC A doesn't set carry >_<
LD L,A
LD A,H
JR C,FLTPTtoAroundNegCarry;
XOR %01111111
LD H,A
JR FLTPTtoAroundCheckTriv
FLTPTtoAroundNegCarry:
XOR %01111111
INC A
LD H,A
FLTPTtoAroundCheckTriv:
;Check for trivial cases. If C is less than zero, output will be one (rounded)
;or zero. So, check sign bit of C
BIT 7,C
JP NZ,FLTPTtoAroundCnegative ;if C is strictly less than zero
;C is positive
LD A,6
SUB C ;we need to shift (6-C) bits
;because: the mantissa is a "1.xxx number". We multiply
;it with 2^C to get the full number. C can be 6 at most,
;because a signed byte can only hold up to 2^6 * "1.999"
;NOTE: we regard (-128) as an overflow here, although that
;is a perfectly acceptable signed number...
JR C,FLTPTtoAroundOverflow ;if (6-C) triggers a carry, then C was 7 or more
;and an overflow will happen
JR Z,FLTPTtoAroundNoShifting ;if (6-C) is zero (C=6), then no need to shift
LD B,A ;B now stores the number of bits to shift to the right
LD A,H ;get the number to shift in A
;B is set up correctly as counter (1 <= B <= 5), so just start shifting:
FLTPTtoAroundShift:
SRA A
DJNZ FLTPTtoAroundShift
;done shifting, carry gives the last bit shifted. To round our answer, we
;need to add this to A
LD B,0
ADC A,B ;add carry, an overflow can happen here
RET ;all done! P/V detects overflow
FLTPTtoAroundCnegative:
;If C is exactly -1, then our value lies between 0.5 and 1: so return one
;else return zero
LD A,C
CP -1
JR NZ,FLTPTtoAroundReturnZero
XOR A
INC A ;A is now 1, and P/V is unset
RET
FLTPTtoAroundReturnZero:
XOR A
;C is a negative value between -127 and -2 (inclusive), doing an INC C
;will therefore reset the P/V flag
INC C
RET
FLTPTtoAroundNoShifting:
;but still some rounding...
XOR A
SLA L
ADC A,H ;A = H + (b7 of L)
RET ;P/V correctly denotes overflow
FLTPTtoAroundOverflow:
;set the P/V flag:
LD A,-128
DEC A
RET
;=========================================================================================
; TOHL
;=========================================================================================
;Converts a floating point number in C-HL to a regular signed number in HL
;Carry flag denotes overflowr.
;No rounding gets done.
;Destroys: A,B
;NOTE: -32768 is regarded as an overflow and will not be returned as -32768!
;NOTE: I HAVEN'T TESTED THIS YET [??]
toHL: