forked from begoon/i8080-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
8085EXER.MAC
1284 lines (1190 loc) · 26.9 KB
/
8085EXER.MAC
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
title 'Z80 instruction set exerciser'
; zexlax.z80 - Z80 instruction set exerciser
; Copyright (C) 1994 Frank D. Cringle
;
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation; either version 2
; of the License, or (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
;******************************************************************************
;
; Modified to exercise an 8085 by Ian Bartholomew, October 2009
;
; I have made the following changes -
;
; Converted all mnemonics to 8080 and rewritten any Z80 code used
; in the original exerciser. Changes are tagged with a #idb in the
; source code listing.
;
; Removed any test descriptors that are not used.
;
; Changed the macro definitions to work in M80
;
; The machine state snapshot has been changed to remove the IX/IY registers.
; They have been replaced by two more copies of HL to obviate the need
; for major changes in the exerciser code.
;
; Changed flag mask in all tests to 0d5h to reflect that in the 8085 the unused bits
; in the flag register are undefined- [S Z X AC X P X C]
;
;******************************************************************************
.8080
aseg
org 100h
begin: jmp start
; machine state before test (needs to be at predictably constant address)
msbt: ds 14
spbt: ds 2
; For the purposes of this test program, the machine state consists of:
; a 2 byte memory operand, followed by
; the registers iy,ix,hl,de,bc,af,sp
; for a total of 16 bytes.
; The program tests instructions (or groups of similar instructions)
; by cycling through a sequence of machine states, executing the test
; instruction for each one and running a 32-bit crc over the resulting
; machine states. At the end of the sequence the crc is compared to
; an expected value that was found empirically on a real Z80.
; A test case is defined by a descriptor which consists of:
; a flag mask byte,
; the base case,
; the incement vector,
; the shift vector,
; the expected crc,
; a short descriptive message.
;
; The flag mask byte is used to prevent undefined flag bits from
; influencing the results. Documented flags are as per Mostek Z80
; Technical Manual.
;
; The next three parts of the descriptor are 20 byte vectors
; corresponding to a 4 byte instruction and a 16 byte machine state.
; The first part is the base case, which is the first test case of
; the sequence. This base is then modified according to the next 2
; vectors. Each 1 bit in the increment vector specifies a bit to be
; cycled in the form of a binary counter. For instance, if the byte
; corresponding to the accumulator is set to 0ffh in the increment
; vector, the test will be repeated for all 256 values of the
; accumulator. Note that 1 bits don't have to be contiguous. The
; number of test cases 'caused' by the increment vector is equal to
; 2^(number of 1 bits). The shift vector is similar, but specifies a
; set of bits in the test case that are to be successively inverted.
; Thus the shift vector 'causes' a number of test cases equal to the
; number of 1 bits in it.
; The total number of test cases is the product of those caused by the
; counter and shift vectors and can easily become unweildy. Each
; individual test case can take a few milliseconds to execute, due to
; the overhead of test setup and crc calculation, so test design is a
; compromise between coverage and execution time.
; This program is designed to detect differences between
; implementations and is not ideal for diagnosing the causes of any
; discrepancies. However, provided a reference implementation (or
; real system) is available, a failing test case can be isolated by
; hand using a binary search of the test space.
start: lhld 6
sphl
lxi d,msg1
mvi c,9
call bdos
lxi h,tests ; first test case
loop: mov a,m ; end of list ?
inx h
ora m
jz done
dcx h
call stt
jmp loop
done: lxi d,msg2
mvi c,9
call bdos
jmp 0 ; warm boot
tests:
dw add16
dw alu8i
dw alu8r
dw daa
dw inca
dw incb
dw incbc
dw incc
dw incd
dw incde
dw ince
dw inch
dw inchl
dw incl
dw incm
dw incsp
dw ld162
dw ld166
dw ld16im
dw ld8bd
dw ld8im
dw ld8rr
dw lda
dw rot8080
dw stabd
dw 0
tstr macro insn,memop,hliy,hlix,hl,de,bc,flags,acc,sp
local lab
lab: db insn
ds lab+4-$,0
dw memop,hliy,hlix,hl,de,bc
db flags
db acc
dw sp
if $-lab ne 20
error 'missing parameter'
endif
endm
tmsg macro m
local lab
lab: db m
if $ ge lab+30
error 'message too long'
else
ds lab+30-$,'.'
endif
db '$'
endm
; add hl,<bc,de,hl,sp> (19,456 cycles)
add16: db 0d5h ; flag mask
tstr 9,0c4a5h,0c4c7h,0d226h,0a050h,058eah,08566h,0c6h,0deh,09bc9h
tstr 030h,0,0,0,0f821h,0,0,0,0,0 ; (512 cycles)
tstr 0,0,0,0,-1,-1,-1,0d7h,0,-1 ; (38 cycles)
db 0,0,0,0 ; expected crc
tmsg 'dad <b,d,h,sp>'
; aluop a,nn (28,672 cycles)
alu8i: db 0d5h ; flag mask
tstr 0c6h,09140h,07e3ch,07a67h,0df6dh,05b61h,00b29h,010h,066h,085b2h
tstr 038h,0,0,0,0,0,0,0,-1,0 ; (2048 cycles)
tstr <0,-1>,0,0,0,0,0,0,0d7h,0,0 ; (14 cycles)
db 0,0,0,0 ; expected crc
tmsg 'aluop nn'
; aluop a,<b,c,d,e,h,l,(hl),a> (753,664 cycles)
alu8r: db 0d5h ; flag mask
tstr 080h,0c53eh,0573ah,04c4dh,msbt,0e309h,0a666h,0d0h,03bh,0adbbh
tstr 03fh,0,0,0,0,0,0,0,-1,0 ; (16,384 cycles)
tstr 0,0ffh,0,0,0,-1,-1,0d7h,0,0 ; (46 cycles)
db 0,0,0,0 ; expected crc
tmsg 'aluop <b,c,d,e,h,l,m,a>'
; <daa,cpl,scf,ccf>
daa: db 0d5h ; flag mask
tstr 027h,02141h,009fah,01d60h,0a559h,08d5bh,09079h,004h,08eh,0299dh
tstr 018h,0,0,0,0,0,0,0d7h,-1,0 ; (65,536 cycles)
tstr 0,0,0,0,0,0,0,0,0,0 ; (1 cycle)
db 0,0,0,0 ; expected crc
tmsg '<daa,cma,stc,cmc>'
; <inc,dec> a (3072 cycles)
inca: db 0d5h ; flag mask
tstr 03ch,04adfh,0d5d8h,0e598h,08a2bh,0a7b0h,0431bh,044h,05ah,0d030h
tstr 001h,0,0,0,0,0,0,0,-1,0 ; (512 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inr,dcr> a'
; <inc,dec> b (3072 cycles)
incb: db 0d5h ; flag mask
tstr 004h,0d623h,0432dh,07a61h,08180h,05a86h,01e85h,086h,058h,09bbbh
tstr 001h,0,0,0,0,0,0ff00h,0,0,0 ; (512 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inr,dcr> b'
; <inc,dec> bc (1536 cycles)
incbc: db 0d5h ; flag mask
tstr 003h,0cd97h,044abh,08dc9h,0e3e3h,011cch,0e8a4h,002h,049h,02a4dh
tstr 008h,0,0,0,0,0,0f821h,0,0,0 ; (256 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inx,dcx> b'
; <inc,dec> c (3072 cycles)
incc: db 0d5h ; flag mask
tstr 00ch,0d789h,00935h,0055bh,09f85h,08b27h,0d208h,095h,005h,00660h
tstr 001h,0,0,0,0,0,0ffh,0,0,0 ; (512 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inr,dcr> c'
; <inc,dec> d (3072 cycles)
incd: db 0d5h ; flag mask
tstr 014h,0a0eah,05fbah,065fbh,0981ch,038cch,0debch,043h,05ch,003bdh
tstr 001h,0,0,0,0,0ff00h,0,0,0,0 ; (512 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inr,dcr> d'
; <inc,dec> de (1536 cycles)
incde: db 0d5h ; flag mask
tstr 013h,0342eh,0131dh,028c9h,00acah,09967h,03a2eh,092h,0f6h,09d54h
tstr 008h,0,0,0,0,0f821h,0,0,0,0 ; (256 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inx,dcx> d'
; <inc,dec> e (3072 cycles)
ince: db 0d5h ; flag mask
tstr 01ch,0602fh,04c0dh,02402h,0e2f5h,0a0f4h,0a10ah,013h,032h,05925h
tstr 001h,0,0,0,0,0ffh,0,0,0,0 ; (512 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inr,dcr> e'
; <inc,dec> h (3072 cycles)
inch: db 0d5h ; flag mask
tstr 024h,01506h,0f2ebh,0e8ddh,0262bh,011a6h,0bc1ah,017h,006h,02818h
tstr 001h,0,0,0,0ff00h,0,0,0,0,0 ; (512 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inr,dcr> h'
; <inc,dec> hl (1536 cycles)
inchl: db 0d5h ; flag mask
tstr 023h,0c3f4h,007a5h,01b6dh,04f04h,0e2c2h,0822ah,057h,0e0h,0c3e1h
tstr 008h,0,0,0,0f821h,0,0,0,0,0 ; (256 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inx,dcx> h'
; <inc,dec> l (3072 cycles)
incl: db 0d5h ; flag mask
tstr 02ch,08031h,0a520h,04356h,0b409h,0f4c1h,0dfa2h,0d1h,03ch,03ea2h
tstr 001h,0,0,0,0ffh,0,0,0,0,0 ; (512 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inr,dcr> l'
; <inc,dec> (hl) (3072 cycles)
incm: db 0d5h ; flag mask
tstr 034h,0b856h,00c7ch,0e53eh,msbt,0877eh,0da58h,015h,05ch,01f37h
tstr 001h,0ffh,0,0,0,0,0,0,0,0 ; (512 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inr,dcr> m'
; <inc,dec> sp (1536 cycles)
incsp: db 0d5h ; flag mask
tstr 033h,0346fh,0d482h,0d169h,0deb6h,0a494h,0f476h,053h,002h,0855bh
tstr 008h,0,0,0,0,0,0,0,0,0f821h ; (256 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<inx,dcx> sp'
; ld hl,(nnnn) (16 cycles)
ld162: db 0d5h ; flag mask
tstr <02ah,low msbt,high msbt>,09863h,07830h,02077h,0b1feh,0b9fah,0abb8h,004h,006h,06015h
tstr 0,0,0,0,0,0,0,0,0,0 ; (1 cycle)
tstr 0,-1,0,0,0,0,0,0,0,0 ; (16 cycles)
db 0,0,0,0 ; expected crc
tmsg 'lhld nnnn'
; ld (nnnn),hl (16 cycles)
ld166: db 0d5h ; flag mask
tstr <022h,low msbt,high msbt>,0d003h,07772h,07f53h,03f72h,064eah,0e180h,010h,02dh,035e9h
tstr 0,0,0,0,0,0,0,0,0,0 ; (1 cycle)
tstr 0,0,0,0,-1,0,0,0,0,0 ; (16 cycles)
db 0,0,0,0 ; expected crc
tmsg 'shld nnnn'
; ld <bc,de,hl,sp>,nnnn (64 cycles)
ld16im: db 0d5h ; flag mask
tstr 1,05c1ch,02d46h,08eb9h,06078h,074b1h,0b30eh,046h,0d1h,030cch
tstr 030h,0,0,0,0,0,0,0,0,0 ; (4 cycles)
tstr <0,0ffh,0ffh>,0,0,0,0,0,0,0,0,0 ; (16 cycles)
db 0,0,0,0 ; expected crc
tmsg 'lxi <b,d,h,sp>,nnnn'
; ld a,<(bc),(de)> (44 cycles)
ld8bd: db 0d5h ; flag mask
tstr 00ah,0b3a8h,01d2ah,07f8eh,042ach,msbt,msbt,0c6h,0b1h,0ef8eh
tstr 010h,0,0,0,0,0,0,0,0,0 ; (2 cycles)
tstr 0,0ffh,0,0,0,0,0,0d7h,-1,0 ; (22 cycles)
db 0,0,0,0 ; expected crc
tmsg 'ldax <b,d>'
; ld <b,c,d,e,h,l,(hl),a>,nn (64 cycles)
ld8im: db 0d5h ; flag mask
tstr 6,0c407h,0f49dh,0d13dh,00339h,0de89h,07455h,053h,0c0h,05509h
tstr 038h,0,0,0,0,0,0,0,0,0 ; (8 cycles)
tstr 0,0,0,0,0,0,0,0,-1,0 ; (8 cycles)
db 0,0,0,0 ; expected crc
tmsg 'mvi <b,c,d,e,h,l,m,a>,nn'
; ld <b,c,d,e,h,l,a>,<b,c,d,e,h,l,a> (3456 cycles)
ld8rr: db 0d5h ; flag mask
tstr 040h,072a4h,0a024h,061ach,msbt,082c7h,0718fh,097h,08fh,0ef8eh
tstr 03fh,0,0,0,0,0,0,0,0,0 ; (64 cycles)
tstr 0,0ffh,0,0,0,-1,-1,0d7h,-1,0 ; (54 cycles)
db 0,0,0,0 ; expected crc
tmsg 'mov <bcdehla>,<bcdehla>'
; ld a,(nnnn) / ld (nnnn),a (44 cycles)
lda: db 0d5h ; flag mask
tstr <032h,low msbt,high msbt>,0fd68h,0f4ech,044a0h,0b543h,00653h,0cdbah,0d2h,04fh,01fd8h
tstr 008h,0,0,0,0,0,0,0,0,0 ; (2 cycle)
tstr 0,0ffh,0,0,0,0,0,0d7h,-1,0 ; (22 cycles)
db 0,0,0,0 ; expected crc
tmsg 'sta nnnn / lda nnnn'
; <rlca,rrca,rla,rra> (6144 cycles)
rot8080: db 0d5h ; flag mask
tstr 7,0cb92h,06d43h,00a90h,0c284h,00c53h,0f50eh,091h,0ebh,040fch
tstr 018h,0,0,0,0,0,0,0,-1,0 ; (1024 cycles)
tstr 0,0,0,0,0,0,0,0d7h,0,0 ; (6 cycles)
db 0,0,0,0 ; expected crc
tmsg '<rlc,rrc,ral,rar>'
; ld (<bc,de>),a (96 cycles)
stabd: db 0d5h ; flag mask
tstr 2,00c3bh,0b592h,06cffh,0959eh,msbt,msbt+1,0c1h,021h,0bde7h
tstr 018h,0,0,0,0,0,0,0,0,0 ; (4 cycles)
tstr 0,-1,0,0,0,0,0,0,-1,0 ; (24 cycles)
db 0,0,0,0 ; expected crc
tmsg 'stax <b,d>'
; start test pointed to by (hl)
stt: push h
mov a,m ; get pointer to test
inx h
mov h,m
mov l,a
mov a,m ; flag mask
sta flgmsk+1
inx h
push h
lxi d,20
dad d ; point to incmask
lxi d,counter
call initmask
pop h
push h
lxi d,20+20
dad d ; point to scanmask
lxi d,shifter
call initmask
lxi h,shifter
mvi m,1 ; first bit
pop h
push h
lxi d,iut ; copy initial instruction under test
lxi b,4
;#idb ldir replaced with following code
ldir1: mov a,m
stax d
inx h
inx d
dcx b
mov a,b
ora c
jnz ldir1
;#idb
lxi d,msbt ; copy initial machine state
lxi b,16
;#idb ldir replaced with following code
ldir2: mov a,m
stax d
inx h
inx d
dcx b
mov a,b
ora c
jnz ldir2
;#idb
lxi d,20+20+4 ; skip incmask, scanmask and expcrc
dad d
xchg
mvi c,9
call bdos ; show test name
call initcrc ; initialise crc
; test loop
tlp: lda iut
cpi 076h ; pragmatically avoid halt intructions
jz tlp2
ani 0dfh
cpi 0ddh
jnz tlp1
lda iut+1
cpi 076h
tlp1: cnz test ; execute the test instruction
tlp2: call count ; increment the counter
cnz shift ; shift the scan bit
pop h ; pointer to test case
jz tlp3 ; done if shift returned NZ
lxi d,20+20+20
dad d ; point to expected crc
call cmpcrc
lxi d,okmsg
jz tlpok
lxi d,ermsg1
mvi c,9
call bdos
call phex8
lxi d,ermsg2
mvi c,9
call bdos
lxi h,crcval
call phex8
lxi d,crlf
tlpok: mvi c,9
call bdos
pop h
inx h
inx h
ret
tlp3: push h
mvi a,1 ; initialise count and shift scanners
sta cntbit
sta shfbit
lxi h,counter
shld cntbyt
lxi h,shifter
shld shfbyt
mvi b,4 ; bytes in iut field
pop h ; pointer to test case
push h
lxi d,iut
call setup ; setup iut
mvi b,16 ; bytes in machine state
lxi d,msbt
call setup ; setup machine state
jmp tlp
; setup a field of the test case
; b = number of bytes
; hl = pointer to base case
; de = destination
setup: call subyte
inx h
dcr b
jnz setup
ret
subyte: push b
push d
push h
mov c,m ; get base byte
lxi d,20
dad d ; point to incmask
mov a,m
cpi 0
jz subshf
mvi b,8 ; 8 bits
subclp: rrc
push psw
mvi a,0
cc nxtcbit ; get next counter bit if mask bit was set
xra c ; flip bit if counter bit was set
rrc
mov c,a
pop psw
dcr b
jnz subclp
mvi b,8
subshf: lxi d,20
dad d ; point to shift mask
mov a,m
cpi 0
jz substr
mvi b,8 ; 8 bits
sbshf1: rrc
push psw
mvi a,0
cc nxtsbit ; get next shifter bit if mask bit was set
xra c ; flip bit if shifter bit was set
rrc
mov c,a
pop psw
dcr b
jnz sbshf1
substr: pop h
pop d
mov a,c
stax d ; mangled byte to destination
inx d
pop b
ret
; get next counter bit in low bit of a
cntbit: ds 1
cntbyt: ds 2
nxtcbit: push b
push h
lhld cntbyt
mov b,m
lxi h,cntbit
mov a,m
mov c,a
rlc
mov m,a
cpi 1
jnz ncb1
lhld cntbyt
inx h
shld cntbyt
ncb1: mov a,b
ana c
pop h
pop b
rz
mvi a,1
ret
; get next shifter bit in low bit of a
shfbit: ds 1
shfbyt: ds 2
nxtsbit: push b
push h
lhld shfbyt
mov b,m
lxi h,shfbit
mov a,m
mov c,a
rlc
mov m,a
cpi 1
jnz nsb1
lhld shfbyt
inx h
shld shfbyt
nsb1: mov a,b
ana c
pop h
pop b
rz
mvi a,1
ret
; clear memory at hl, bc bytes
clrmem: push psw
push b
push d
push h
mvi m,0
mov d,h
mov e,l
inx d
dcx b
;#idb ldir replaced with following code
ldir3: mov a,m
stax d
inx h
inx d
dcx b
mov a,b
ora c
jnz ldir3
;#idb
pop h
pop d
pop b
pop psw
ret
; initialise counter or shifter
; de = pointer to work area for counter or shifter
; hl = pointer to mask
initmask:
push d
xchg
lxi b,20+20
call clrmem ; clear work area
xchg
mvi b,20 ; byte counter
mvi c,1 ; first bit
mvi d,0 ; bit counter
imlp: mov e,m
imlp1: mov a,e
ana c
jz imlp2
inr d
imlp2: mov a,c
rlc
mov c,a
cpi 1
jnz imlp1
inx h
dcr b
jnz imlp
; got number of 1-bits in mask in reg d
mov a,d
ani 0f8h
rrc
rrc
rrc ; divide by 8 (get byte offset)
mov l,a
mvi h,0
mov a,d
ani 7 ; bit offset
inr a
mov b,a
mvi a,080h
imlp3: rlc
dcr b
jnz imlp3
pop d
dad d
lxi d,20
dad d
mov m,a
ret
; multi-byte counter
count: push b
push d
push h
lxi h,counter ; 20 byte counter starts here
lxi d,20 ; somewhere in here is the stop bit
xchg
dad d
xchg
cntlp: inr m
mov a,m
cpi 0
jz cntlp1 ; overflow to next byte
mov b,a
ldax d
ana b ; test for terminal value
jz cntend
mvi m,0 ; reset to zero
cntend: pop b
pop d
pop h
ret
cntlp1: inx h
inx d
jmp cntlp
; multi-byte shifter
shift: push b
push d
push h
lxi h,shifter ; 20 byte shift register starts here
lxi d,20 ; somewhere in here is the stop bit
xchg
dad d
xchg
shflp: mov a,m
ora a
jz shflp1
mov b,a
ldax d
ana b
jnz shlpe
mov a,b
rlc
cpi 1
jnz shflp2
mvi m,0
inx h
inx d
shflp2: mov m,a
xra a ; set Z
shlpe: pop h
pop d
pop b
ret
shflp1: inx h
inx d
jmp shflp
counter: ds 2*20
shifter: ds 2*20
; test harness
test: push psw
push b
push d
push h
if 0
lxi d,crlf
mvi c,9
call bdos
lxi h,iut
mvi b,4
call hexstr
mvi e,' '
mvi c,2
call bdos
mvi b,16
lxi h,msbt
call hexstr
endif
di ; disable interrupts
;#idb ld (spsav),sp replaced by following code
;#idb All registers and flages are immediately overwritten so
;#idb no need to preserve any state.
lxi h,0 ; save stack pointer
dad sp
shld spsav
;#idb
lxi sp,msbt+2 ; point to test-case machine state
;#idb pop iy
;#idb pop ix both replaced by following code
;#idb Just dummy out ix/iy with copies of hl
pop h ; and load all regs
pop h
;#idb
pop h
pop d
pop b
pop psw
;#idb ld sp,(spbt) replaced with the following code
;#idb HL is copied/restored before/after load so no state changed
shld temp
lhld spbt
sphl
lhld temp
;#idb
iut: ds 4 ; max 4 byte instruction under test
;#idb ld (spat),sp replaced with the following code
;#idb Must be very careful to preserve registers and flag
;#idb state resulting from the test. The temptation is to use the
;#idb stack - but that doesn't work because of the way the app
;#idb uses SP as a quick way of pointing to memory.
;#idb Bit of a code smell, but I can't think of an easier way.
shld temp
lxi h,0
jc temp1 ;jump on the state of the C flag set in the test
dad sp ;this code will clear the C flag (0 + nnnn = nc)
jmp temp2 ;C flag is same state as before
temp1: dad sp ;this code will clear the C flag (0 + nnnn = nc)
stc ;C flage needs re-setting to preserve state
temp2: shld spat
lhld temp
;#idb
lxi sp,spat
push psw ; save other registers
push b
push d
push h
;#idb push ix
;#idb push iy both replaced by following code
;#idb Must match change made to pops made before test
push h
push h
;#idb
;#idb ld sp,(spsav) replaced with following code
;#idb No need to preserve state
lhld spsav ; restore stack pointer
sphl
;#idb
ei ; enable interrupts
lhld msbt ; copy memory operand
shld msat
lxi h,flgsat ; flags after test
mov a,m
flgmsk: ani 0ffh ; mask-out irrelevant bits (self-modified code!)
mov m,a
mvi b,16 ; total of 16 bytes of state
lxi d,msat
lxi h,crcval
tcrc: ldax d
inx d
call updcrc ; accumulate crc of this test case
dcr b
jnz tcrc
if 0
mvi e,' '
mvi c,2
call bdos
lxi h,crcval
call phex8
lxi d,crlf
mvi c,9
call bdos
lxi h,msat
mvi b,16
call hexstr
lxi d,crlf
mvi c,9
call bdos
endif
pop h
pop d
pop b
pop psw
ret
;#idb Added to store HL state
temp: ds 2
;#idb
; machine state after test
msat: ds 14 ; memop,iy,ix,hl,de,bc,af
spat: ds 2 ; stack pointer after test
flgsat equ spat-2 ; flags
spsav: ds 2 ; saved stack pointer
; display hex string (pointer in hl, byte count in b)
hexstr: mov a,m
call phex2
inx h
dcr b
jnz hexstr
ret
; display hex
; display the big-endian 32-bit value pointed to by hl
phex8: push psw
push b
push h
mvi b,4
ph8lp: mov a,m
call phex2
inx h
dcr b
jnz ph8lp
pop h
pop b
pop psw
ret
; display byte in a
phex2: push psw
rrc
rrc
rrc
rrc
call phex1
pop psw
; fall through
; display low nibble in a
phex1: push psw
push b
push d
push h
ani 0fh
cpi 10
jc ph11
adi 'a'-'9'-1
ph11: adi '0'
mov e,a
mvi c,2
call bdos
pop h
pop d
pop b
pop psw
ret
bdos: push psw
push b
push d
push h
call 5
pop h
pop d
pop b
pop psw
ret
msg1: db '8085 instruction exerciser',10,13,'$'
msg2: db 'Tests complete$'
okmsg: db ' OK',10,13,'$'
ermsg1: db ' ERROR **** crc expected:$'
ermsg2: db ' found:$'
crlf: db 10,13,'$'
; compare crc
; hl points to value to compare to crcval
cmpcrc: push b
push d
push h
lxi d,crcval
mvi b,4
cclp: ldax d
cmp m
jnz cce
inx h
inx d
dcr b
jnz cclp
cce: pop h
pop d
pop b
ret
; 32-bit crc routine
; entry: a contains next byte, hl points to crc
; exit: crc updated
updcrc: push psw
push b
push d
push h
push h
lxi d,3
dad d ; point to low byte of old crc
xra m ; xor with new byte
mov l,a
mvi h,0
dad h ; use result as index into table of 4 byte entries
dad h
xchg
lxi h,crctab
dad d ; point to selected entry in crctab
xchg
pop h
lxi b,4 ; c = byte count, b = accumulator
crclp: ldax d
xra b
mov b,m
mov m,a
inx d
inx h
dcr c
jnz crclp
if 0
lxi h,crcval
call phex8
lxi d,crlf
mvi c,9