-
Notifications
You must be signed in to change notification settings - Fork 0
/
picalc.asm
1996 lines (1638 loc) · 42.1 KB
/
picalc.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
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
;//////////////////////////////////////////////////////////////////////////////;
; Project: ;
; PICALC-Z80 ;
; ;
; Description: ;
; A calculator that yields an arbitrary number of digits of the number PI. ;
; ;
; Target: ;
; Any Z80-based computer. Details of memory allocation and stack positioning;
; must be adjusted for the specifics of the target. In addition, INT 08h ;
; is used as a function call to print character over TTY (A = character), ;
; which implementation has to be adjusted to the target. ;
; ;
; Compiler: ;
; z80asm ;
; https://manpages.ubuntu.com/manpages/trusty/man1/z80asm.1.html ;
; ;
; Other compilers/assemblers may require minor changes in the source code. ;
; ;
; Usage: ;
; The desired amount of PI digits is defined in the macro NUM_DECS. Default ;
; value is 100. Note that incrementing this value will increase RAM usage ;
; proportionally, and the needed CPU cycles quadratically. Don't change the ;
; other algorithm constants unless you really know what you are doing. ;
; ;
; The algorithm was validated for 1,000 digits of PI, all of them checked ;
; against the generally known published digits. For an actual Z80, yielding ;
; this number of digits may prove to be a daunting task, especially because ;
; of the gigantic number of CPU cycles needed (RAM is a lesser problem in ;
; this regard). 100 digits is way more practical amount, unless you have A ;
; LOT of time to spare. ;
; ;
; The main loop iteracts as many times as needed to calculate all the ;
; decimal places requested (the number of iterations / number of digits have;
; a ratio close to 0.9). With each iteration the message 'TOTAL:' is printed;
; along the PI approximation calculated so far. ;
; ;
; Version & Date: ;
; 1.0 - 2024-MAY-27 ;
; ;
; Author: ;
; Milton Maldonado Jr (ARM_Coder) ;
; ;
; License: ;
; GPL V2 ;
; ;
; Disclaimer: ;
; This code is supplied 'as is' with no warranty against bugs. It was tested;
; on a Z80 simulator that *I* wrote (haha), so it was not tested against any;
; actual, validated target. ;
; ;
; Note: ;
; Along the ASM source, you will see some commented 'C' statemens. The ;
; project was initially built and tested in C, and then hand-translated ;
; to Z80 ASM. ;
; ;
; Note 2: ;
; I hope this is my last ASM project ever. Writing code in ASM is a real ;
; PITA, but this project was a challenge I've set for me. ;
; ;
; Funny note: ;
; This project implements the Bailey-Borwein-Plouffe method of calculating ;
; PI. This method is much, much faster that the classic Leibniz series. ;
; The funny thing is that the method was discovered (invented?) in 1995, ;
; when the Z80 had already passed its heyday and was fading into a niche, ;
; retro platform. ;
;//////////////////////////////////////////////////////////////////////////////;
; Example: z80asm picalc.asm -o - | xxd -ps -c 16 > test.hex
; Hardware constants
ROMBASE: equ 0
ROMSZ: equ 8192
RAMBASE: equ 8192
RAMSZ: equ 8192
; Algorithm constants
NUM_DECS: equ 100
NUM_IT: equ ((NUM_DECS*9)/10)
NBITS_FR: equ (3*NUM_DECS + (NUM_DECS >> 1))
NBITS_INT: equ 16
NBITS_FRAC: equ 8*(1 + (NBITS_FR >> 3)) ; The number of bits should be at least 3.33x (1 / log(2)) the number of decimal places, plus a small cushion.
NBYTES_INT: equ NBITS_INT >> 3
NBITS: equ NBITS_INT+NBITS_FRAC
NBYTES: equ NBITS>>3
NBYTES1: equ NBYTES-1
org ROMBASE ; Expected to be 0x00. Other values will clash with the
; interrupt handlers below.
ld sp,RAMBASE + RAMSZ
call _main
halt
seek 0x0008 ; SW Interrupt. Used here to print characters.
org 0x0008
out (0),a ; Change it for whatever your target needs.
ret
seek 0x0038 ; HW Interrupt IM 1.
org 0x0038
reti
seek 0x0066 ; NMI.
org 0x0066
retn
;///////////////////////////////////////////////////////////////////////////////
; prints
; void prints(const char *string);
; Parameters: The string must be supplied inline after the call to prints,
; and must be null-terminated. The function will return to the
; first instruction after the aforementioned null terminator.
; Because of that, the call must be always unconditional (opcode
; 0xCD).
; Returns: Nothing
; Affects: HL, AF & whatever INT 08H also affects
prints:
pop hl
prints_1:
ld a,(hl)
or a
jr z,prints_2
rst 08h
inc hl
jr prints_1
prints_2:
inc hl
push hl
ret
;///////////////////////////////////////////////////////////////////////////////
; print_crlf
; void print_crlf(void);
; Parameters: Nothing
; Returns: Nothing
; Affects: HL
print_crlf:
call prints
db 13,10,0
ret
;///////////////////////////////////////////////////////////////////////////////
; zero_reg
; void zero_reg(uint8_t *reg);
; Parameters:
; HL: reg
; Returns: Nothing
; Affects: BC DE HL AF
zero_reg:
; memset(reg,0,NBYTES);
ld bc,NBYTES-1
xor a
ld (hl),a
ld d,h
ld e,l
inc de
ldir
ret
;///////////////////////////////////////////////////////////////////////////////
; set_bit_reg
; void set_bit_reg(uint8_t *reg, int place);
; Parameters:
; HL: reg
; BC: place
; Returns: Nothing
; Affects: BC DE HL AF
set_bit_reg:
; int byte = NBYTES1 - (place >> 3);
push hl
ld hl,NBYTES1
set_bit_reg0:
ld d,b
ld e,c
srl d
rr e
srl d
rr e
srl d
rr e
scf
ccf
sbc hl,de
ex de,hl ;DE: byte
pop hl
; int bits = place & 0x07;
ld a,c
and 0x07 ;A: bits
; reg[byte] |= 1<<bits;
add hl,de
ld b,1
or a
jr z,set_bit_reg2
set_bit_reg1:
sla b
dec a
jr nz,set_bit_reg1
set_bit_reg2:
ld a,(hl)
or b
ld (hl),a
ret
;///////////////////////////////////////////////////////////////////////////////
; set_bit_reg_int
; void set_bit_reg_int(uint8_t *reg, int place);
; Parameters:
; HL: reg
; BC: place
; Returns: Nothing
; Affects: BC DE HL AF
set_bit_reg_int:
; int byte = NBYTES_INT - 1 - (place >> 3);
push hl
ld hl,NBYTES_INT-1
jr set_bit_reg0
;///////////////////////////////////////////////////////////////////////////////
; print_reg
; void print_reg(uint8_t *reg);
; Não portada
;///////////////////////////////////////////////////////////////////////////////
; add_reg2_to_reg1
; void add_reg2_to_reg1(uint8_t *reg1, const uint8_t *reg2);
; Parameters:
; HL: reg2
; DE: reg1
; Returns: Nothing
; Affects: BC DE HL AF BC' DE' HL' AF'
add_reg2_to_reg1:
; uint16_t cy = 0;
; int i;
; for (i = NBYTES1; i >= 0; i--){
; uint16_t sum = (uint16_t)reg1[i] + (uint16_t)reg2[i] + cy;
; if (sum & 0x100)
; cy = 1;
; else
; cy = 0;
; reg1[i] = sum & 0xff;
; }
ld bc,NBYTES1
add hl,bc
ex de,hl
add hl,bc
ex de,hl
exx
ld bc,NBYTES1
inc bc
exx
sub a ;zero Carry Flag
add_reg2_to_reg1_0:
ld b,(hl)
ld a,(de)
adc a,b
ld (de),a
dec hl
dec de
exx ; Save HL & DE, restores counter in BC
ex af,af' ; Save CY
dec bc
ld a,b
or c
ret z ; End loop, bye
ex af,af' ; Restore CY
exx ; Save counter in BC, restores HL & DE
jr add_reg2_to_reg1_0
;///////////////////////////////////////////////////////////////////////////////
; add_reg_to_acc
; void add_reg_to_acc(const uint8_t *reg);
; Parameters:
; HL: reg
; Returns: Nothing
; Affects: BC DE HL AF BC' DE' HL' AF'
add_reg_to_acc:
ld de,acc
jr add_reg2_to_reg1
;///////////////////////////////////////////////////////////////////////////////
; inc_reg_int8
; void inc_reg_int8(uint8_t *reg, uint8_t byteval);
; Parameters:
; DE: reg
; A: byteval
; Returns: Nothing
; Affects: BC DE HL AF BC' DE' HL' AF'
inc_reg_int8:
; uint16_t cy = byteval;
; int i;
; for (i = NBYTES_INT - 1; i >= 0; i--){
; uint16_t sum =(uint16_t)reg[i] + cy;
; reg[i] = sum & 0xff;
; if (!(sum & 0x100))
; return;
; cy = 1;
; }
ld bc,NBYTES_INT
ex de,hl
add hl,bc
dec hl
ld e,a
inc_reg_int8_0:
ld a,(hl)
add a,e
ld (hl),a
ld e,0
jr nc, inc_reg_int8_1
inc e ; Here E propagates the carry for the sums.
inc_reg_int8_1:
dec bc
ld a,b
or c
ret z ; End loop, bye
dec hl
jr inc_reg_int8_0
;///////////////////////////////////////////////////////////////////////////////
; load_reg_int
; void load_reg_int(uint8_t *reg, int val);
; Parameters:
; DE: reg
; HL: val
; Returns: Nothing
; Affects: BC DE HL AF BC' DE' HL' AF'
load_reg_int:
; for (int i = NBYTES_INT - 1; i >= 0; i--){
; reg[i] = val & 0xff;
; val >>= 8;
; }
ex de,hl
ld bc,NBYTES_INT
add hl,bc
dec hl
ld b,NBYTES_INT
ld a,e
ld (hl),a
dec hl
ld a,d
ld (hl),a
dec b
dec b
ld a,b
or a
ret z
dec hl
xor a
load_reg_int_0:
ld (hl),a
dec hl
djnz load_reg_int_0
ret
;///////////////////////////////////////////////////////////////////////////////
; sub_reg2_from_reg1
; void sub_reg2_from_reg1(uint8_t *reg1, const uint8_t *reg2);
; Parameters:
; DE: reg1
; HL: reg2
; Returns: Nothing
; Affects: BC DE HL AF BC' DE' HL' AF'
sub_reg2_from_reg1:
; uint16_t cy = 0;
; int i;
; for (i = NBYTES1; i >= 0; i--){
; uint16_t diff = (uint16_t)reg1[i] - (uint16_t)reg2[i] - cy;
; if (diff & 0x8000)
; cy = 1;
; else
; cy = 0;
; reg1[i] = diff & 0xff;
; }
ld bc,NBYTES1
add hl,bc
ex de,hl
add hl,bc
ex de,hl
exx
ld bc,NBYTES1
inc bc
exx
sub a ;zera CY
sub_reg2_to_reg1_0:
ld b,(hl)
ld a,(de)
sbc a,b
ld (de),a
dec hl
dec de
exx ; Save HL & DE, restores counter in BC
ex af,af' ; Save CY
dec bc
ld a,b
or c
ret z ; End loop, bye
ex af,af' ; Restore CY
exx ; Save counter in BC, restore HL & DE
jr sub_reg2_to_reg1_0
;///////////////////////////////////////////////////////////////////////////////
; sub_reg_from_acc
; void sub_reg_from_acc(const uint8_t *reg);
; Parameters:
; HL: reg
; Returns: Nothing
; Affects: BC DE HL AF BC' DE' HL' AF'
sub_reg_from_acc:
ld de,acc
call sub_reg2_from_reg1
ret
;///////////////////////////////////////////////////////////////////////////////
; shl_reg
; void shl_reg(uint8_t *reg, int places)
; Parameters:
; DE: reg
; BC: places
; Returns: Nothing
; Affects: BC DE HL AF
shl_reg:
push ix
ld ix,0xFFF8 ; Allocates 8 bytes
add ix,sp
ld sp,ix
ld (ix+0),e ;IX+0, IX+1 = 'reg'
ld (ix+1),d
; if (places > NBITS)
; places = NBITS;
ld a,NBITS >> 8
sub b
jr c,shl_reg_0
jr nz,shl_reg_1
ld a,NBITS & 255
sub c
jr nc, shl_reg_1
shl_reg_0:
ld bc,NBITS
shl_reg_1:
; int bytes = places >> 3;
; int bits = places & 0x07;
ld a,c
and 0x07
ld (ix+2),a ; IX+2 = 'bits'
srl b
rr c
srl b
rr c
srl b
rr c ; BC = 'bytes'
; int leftbytes = NBYTES - bytes;
ld hl,NBYTES
xor a
sbc hl,bc ; HL = 'leftbytes'
ld (ix+4),l ; IX+4, IX+5 = 'leftbytes'
ld (ix+5),h
; if (bytes){
ld a,b
or c
jr z,shl_reg_2
; if (leftbytes)
ld a,h
or l
jr z,shl_reg_1a
push bc ; Save 'bytes'
push hl ; Save 'leftbytes'
; memmove(®[0], ®[bytes], leftbytes);
ld h,d
ld l,e
add hl,bc ; DE='reg' HL='reg[bytes]'
pop bc ; Restore 'leftbytes'
ldir
pop bc ; Restore 'bytes'
shl_reg_1a:
; memset(®[leftbytes],0,bytes);
; }
ld h,d ; Here DE points to the 1st byte to fill w/ zero (obtained from the
ld l,e ; previous LDIR if it took place, or the previous value).
shl_reg_1b:
xor a
ld (hl),a
inc hl
dec bc
ld a,b
or c
jr nz,shl_reg_1b
shl_reg_2:
; if (bits){
ld a,(ix+2) ; 'bits'
or a
jr z,shl_reg_end
; for (i = 0; i < (leftbytes-1); i++){
; Vai fazer o shift
ld c,(ix+4)
ld b,(ix+5) ;'leftbytes'
ld a,b
or c
jr z,shl_reg_3
dec bc
ld a,b
or c
jr z,shl_reg_3
ld l,(ix+0)
ld h,(ix+1) ; HL = 'reg'
shl_reg_2a:
; reg[i] <<= bits;
ld a,(ix+2) ; 'bits'
ld e,a ; 'bits'
neg
add a,8
ld d,a
ld a,(hl)
shl_reg_2b:
sla a
dec e
jr nz,shl_reg_2b
; uint8_t aux = reg[i+1] >> (8-bits);
ld e,a
inc hl
ld a,(hl)
dec hl
shl_reg_2c:
srl a
dec d
jr nz,shl_reg_2c
; reg[i] |= aux;
; }
or e
ld (hl),a
inc hl
dec bc
ld a,b
or c
jr nz,shl_reg_2a
shl_reg_3:
; reg[leftbytes - 1] <<= bits;
ld a,(hl)
ld b,(IX+2) ; 'bits'
shl_reg_2d:
sla a
djnz shl_reg_2d
ld (hl),a
; }
shl_reg_end:
ld hl,0x0008 ; Frees 8 bytes
add hl,sp
ld sp,hl
pop ix
ret
;///////////////////////////////////////////////////////////////////////////////
stack_test:
push ix
ld ix,0xFFF8 ; Allocates 8 bytes
add ix,sp
ld sp,ix
ld c,(ix+0)
ld b,(ix+1)
;....
ld hl,0x0008 ; Frees 8 bytes
add hl,sp
ld sp,hl
pop ix
ret
;///////////////////////////////////////////////////////////////////////////////
; shr_reg
; void shr_reg(uint8_t *reg, int places);
; Parameters:
; DE: reg
; BC: places
; Returns: Nothing
; Affects: BC DE HL AF
shr_reg:
push ix
ld ix,0xFFF8 ; Allocates 8 bytes
add ix,sp
ld sp,ix
ld (ix+0),e ; IX+0, IX+1 = 'reg'
ld (ix+1),d
; if (places > NBITS)
; places = NBITS;
ld a,NBITS >> 8
sub b
jr c,shr_reg_0
jr nz,shr_reg_1
ld a,NBITS & 255
sub c
jr nc, shr_reg_1
shr_reg_0:
ld bc,NBITS
shr_reg_1:
; int bytes = places >> 3;
; int bits = places & 0x07;
ld a,c
and 0x07
ld (ix+2),a ; IX+2 = 'bits'
srl b
rr c
srl b
rr c
srl b
rr c ; BC = 'bytes'
ld (ix+6),c
ld (ix+7),b ; IX+6, IX+7 = 'bytes'
; int rightbytes = NBYTES - bytes;
ld hl,NBYTES
xor a
sbc hl,bc ; HL = 'rightbytes'
ld (ix+4),l ; IX+4, IX+5 = 'rightbytes'
ld (ix+5),h
; if (bytes){
ld a,b
or c
jr z,shr_reg_2
; if (rightbytes)
ld a,h
or l
jr z,shr_reg_1a
; memmove(®[bytes], ®[0], rightbytes);
ld hl,NBYTES1
add hl,de
ld d,h
ld e,l ; DE = last buffer's byte (destination)
xor a ; BC = 'bytes'
sbc hl,bc ; HL = origem
ld c,(ix+4) ; 'rightbytes'
ld b,(ix+5)
lddr
shr_reg_1a:
; memset(®[0],0,bytes);
ld c,(ix+6)
ld b,(ix+7) ; IX+6, IX+7 = 'bytes'
ld l,(ix+0)
ld h,(ix+1) ; IX+0, IX+1 = 'reg'
shr_reg_1b:
xor a
ld (hl),a
inc hl
dec bc
ld a,b
or c
jr nz,shr_reg_1b
; }
shr_reg_2:
; if (bits){
ld a,(ix+2) ; 'bits'
or a
jr z,shr_reg_end
; for (i = NBYTES1; i > bytes; i--){
ld hl,NBYTES1
ld c,(ix+6)
ld b,(ix+7) ; IX+6, IX+7 = 'bytes'
xor a
sbc hl,bc
ld a,h
or l
jr z,shr_reg_2e
ld b,h
ld c,l ; BC = # of bytes to process
ld l,(ix+0)
ld h,(ix+1) ; HL = 'reg'
ld de,NBYTES1
add hl,de
shr_reg_2a:
; reg[i] >>= bits;
ld a,(ix+2) ; 'bits'
ld e,a ; E = 'bits'
neg
add a,8
ld d,a ; D = 8 - 'bits'
ld a,(hl)
shr_reg_2b:
srl a
dec e
jr nz,shr_reg_2b
ld e,a
; uint8_t aux = reg[i-1] << (8-bits);
dec hl
ld a,(hl)
inc hl
shr_reg_2c:
sla a
dec d
jr nz,shr_reg_2c
; reg[i] |= aux;
or e
ld (hl),a
; }
dec hl
dec bc
ld a,b
or c
jr nz,shr_reg_2a
shr_reg_2e:
; reg[bytes] >>= bits;
ld a,(hl)
ld b,(IX+2) ; 'bits'
shr_reg_2d:
srl a
djnz shr_reg_2d
ld (hl),a
; }
shr_reg_end:
ld hl,0x0008 ; Frees 8 bytes
add hl,sp
ld sp,hl
pop ix
ret
;///////////////////////////////////////////////////////////////////////////////
; mul_reg2_by_reg1
; void mul_reg2_by_reg1(const uint8_t *reg1, uint8_t *reg2);
; Parameters:
; HL: reg1
; DE: reg2
; Returns: Nothing
; Affects: BC DE HL AF IY BC' DE' HL' AF'
mul_reg2_by_reg1:
push ix
ld ix,0xFFF8-2*NBYTES ; Allocates 8 bytes + 2 buffers
add ix,sp
ld sp,ix
ld (ix+0),l
ld (ix+1),h ; IX+0, IX+1: reg1
ld (ix+2),e
ld (ix+3),d ; IX+2, IX+3: reg2
; uint8_t regmdiv[NBYTES];
ld c,ixl
ld b,ixh ; Copia IX em BC
ld HL,8
add hl,bc
ld (ix+4),l
ld (ix+5),h ; IX+4, IX+5: regmdiv
; uint8_t regmdiv2[NBYTES];
ld bc,NBYTES
add hl,bc
ld (ix+6),l
ld (ix+7),h ; IX+6, IX+7: regmdiv2
; memcpy(regmdiv, reg2, NBYTES);
ld e,(ix+4)
ld d,(ix+5) ; IX+4, IX+5: regmdiv
ld l,(ix+2)
ld h,(ix+3) ; IX+2, IX+3: reg2
ld bc,NBYTES
ldir
; memcpy(regmdiv2, reg2, NBYTES);
ld e,(ix+6)
ld d,(ix+7) ; IX+6, IX+7: regmdiv2
ld l,(ix+2)
ld h,(ix+3) ; IX+2, IX+3: reg2
ld bc,NBYTES
ldir
; zero_reg(reg2);
ld l,(ix+2)
ld h,(ix+3)
call zero_reg ;Zera reg2
; int places = 0;
ld iy,0 ; IY = places
; // Integer part
; for (int i = NBYTES_INT-1; i>=0; i--){
ld bc,NBYTES_INT
ld l,(ix+0)
ld h,(ix+1) ; IX+0, IX+1: reg1
add hl,bc
dec hl ; HL: buffer to process
mul_reg2_by_reg1_1:
; int msk = 1;
ld d,1
; for (int j = 0; j < 8; j++){
ld e,8
mul_reg2_by_reg1_2:
; if (reg1[i] & msk){
ld a,(hl)
and d
jr z, mul_reg2_by_reg1_3
; shl_reg(regmdiv,places);
push bc
push de
push hl
ld e,(ix+4)
ld d,(ix+5) ; IX+4, IX+5: regmdiv
ld b,iyh
ld c,iyl ; BC gets IY = 'places'
call shl_reg
; add_reg2_to_reg1(reg2, regmdiv);
ld e,(ix+2)
ld d,(ix+3) ; IX+2, IX+3: reg2
ld l,(ix+4)
ld h,(ix+5) ; IX+4, IX+5: regmdiv
call add_reg2_to_reg1
pop hl
pop de
pop bc
; places = 1;
ld iy,1 ; IY = places
; }
; else
; places++;
jr mul_reg2_by_reg1_4
mul_reg2_by_reg1_3:
inc iy
mul_reg2_by_reg1_4:
; msk <<= 1;
ld a,d
sla a
ld d,a
; }
dec e
jr nz,mul_reg2_by_reg1_2
; }
dec hl
dec bc
ld a,b
or c
jr nz,mul_reg2_by_reg1_1
; memcpy(regmdiv, regmdiv2, NBYTES);
ld l,(ix+6)
ld h,(ix+7) ; IX+6, IX+7: regmdiv2
ld e,(ix+4)
ld d,(ix+5) ; IX+4, IX+5: regmdiv
ld bc,NBYTES
ldir
; shr_reg(regmdiv,1);
ld e,(ix+4)
ld d,(ix+5) ; IX+4, IX+5: regmdiv
ld bc,1
call shr_reg
; places = 0;
ld iy,0
; //Frac part
; for (int i = NBYTES_INT; i < NBYTES; i++){
ld l,(ix+0)
ld h,(ix+1) ; IX+0, IX+1: reg1
ld bc,NBYTES_INT
add hl,bc
ld bc, NBYTES - NBYTES_INT
mul_reg2_by_reg1_5:
; int msk = 128;
ld d,128
; for (int j = 0; j < 8; j++){
ld e,8
mul_reg2_by_reg1_6:
; if (reg1[i] & msk){