-
Notifications
You must be signed in to change notification settings - Fork 10
/
chapter-instr-close.tex
4110 lines (3227 loc) · 199 KB
/
chapter-instr-close.tex
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
\chapter{Instructions up Close}
\label{instruction_details}
% ████████████████████████████████████████████████████████████████████████████
% █░░░░░░░░░░░░░░█░░░░░░█████████░░░░░░░░░░░░░░█░░░░░░░░░░░░░░█░░░░░░░░░░░░░░█
% █░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░█████████░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█
% █░░▄▀░░░░░░░░░░█░░▄▀░░█████████░░▄▀░░░░░░▄▀░░█░░▄▀░░░░░░░░░░█░░▄▀░░░░░░░░░░█
% █░░▄▀░░█████████░░▄▀░░█████████░░▄▀░░██░░▄▀░░█░░▄▀░░█████████░░▄▀░░█████████
% █░░▄▀░░█████████░░▄▀░░█████████░░▄▀░░██░░▄▀░░█░░▄▀░░░░░░░░░░█░░▄▀░░░░░░░░░░█
% █░░▄▀░░█████████░░▄▀░░█████████░░▄▀░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█
% █░░▄▀░░█████████░░▄▀░░█████████░░▄▀░░██░░▄▀░░█░░░░░░░░░░▄▀░░█░░▄▀░░░░░░░░░░█
% █░░▄▀░░█████████░░▄▀░░█████████░░▄▀░░██░░▄▀░░█████████░░▄▀░░█░░▄▀░░█████████
% █░░▄▀░░░░░░░░░░█░░▄▀░░░░░░░░░░█░░▄▀░░░░░░▄▀░░█░░░░░░░░░░▄▀░░█░░▄▀░░░░░░░░░░█
% █░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█
% █░░░░░░░░░░░░░░█░░░░░░░░░░░░░░█░░░░░░░░░░░░░░█░░░░░░░░░░░░░░█░░░░░░░░░░░░░░█
% ████████████████████████████████████████████████████████████████████████████
\input{defines-instr-close.tex}
The following pages describe all instructions in detail. Alphabetical order is used as much as possible, but some deviations were made to better fit to pages. Each instruction includes:
\begin{itemize}
\setlength\itemsep{1pt}
\item Mnemonic
\item Symbolic operation for quick info on what instruction does
\item All variants (where applicable)
\item Description with further details
\item Effects on flags
\item Timing table with machine cycles, T states and time required for execution on different CPU speeds
\end{itemize}
Where possible, multiple variants of same instruction are grouped together and where multiple timings are possible, timing table is sorted from quickest to slowest.
\pagebreak
\thispagestyle{plain} % use toc style without headers for this explanation page, it better matches chapter start page
\subsubsection{Flags}
\newcommand{\DetailsFlagTableList}[1]{
\vspace*{-2ex}
\setlist{leftmargin=1em,after=\vspace{-2ex}}
\begin{itemize}
\setlength\itemsep{-4pt}
#1
\end{itemize}
}
\newcommand{\DetailsFlagTableParagraph}{\vspace{1ex}}
{
\renewcommand{\arraystretch}{1.5}
\begin{tabularx}{\linewidth}{cX}
\FlagSF{} &
\textbf{Sign Flag} is set to twos-complement of the most-significant bit (bit 7) of the result of an instruction. If the result is positive (bit 7 is {\tt 0}), \FlagSF{} is set, and if the result is negative (bit 7 is {\tt 1}), \FlagSF{} is reset. This leaves bits 0-6 to represent the value. Positive numbers range from {\tt 0} to {\tt 127} and negative from {\tt -1} to {\tt -128}.
\\
\FlagZF{} &
\textbf{Zero Flag} depends on whether the result of an instruction is {\tt 0}. \FlagZF{} is set if the result if {\tt 0} and reset otherwise.
\\
\FlagHF{} &
\textbf{Half Carry Flag} represents a carry or borrow status between bits 3 and 4 of an 8-bit arithmetic operation (bits 11 and 12 for 16-bit operations). Set if:
\DetailsFlagTableList{
\item A carry from bit 3 to bit 4 occurs during addition (bit 11 to 12 for 16-bit operations)
\item A borrow from bit 4 occurs during subtraction (from bit 12 for 16-bit operations)
}
\\
\FlagPV{} &
\textbf{Parity/Overflow Flag} value depends on the type of the operation.
\DetailsFlagTableParagraph
For arithmetic operations, \FlagPV{} indicates an overflow. The flag is set when the sign of the result is different from the sign of the operands:
\DetailsFlagTableList{
\item all operands are positive but the result is negative or
\item all operands are negative but the result is positive
}
\DetailsFlagTableParagraph
For logical and rotate operations, \FlagPV{} indicates the parity of the result. The number of set bits in the result are counted. If the total is an even value, \FlagPV{} is set. If the total is odd, \FlagPV{} is reset.
\\
\FlagNF{} &
\textbf{Add/Subtract Flag} is used primarily for {\tt DAA} instruction to distinguish between add and subtract operations. But other instructions may also affect it as described in the following pages.
\\
\textbf{CF} &
\textbf{Carry Flag} represents a carry or borrow status for arithmetic operations. \textbf{CF} is set if add instruction generates a carry, or subtract generates a borrow.
\DetailsFlagTableParagraph
For rotate and shift instructions, \textbf{CF} is used:
\DetailsFlagTableList{
\item as a link between least-significat and most significant bit for {\tt RLA}, {\tt RL}, {\tt RRA} and {\tt RR}
\item contains the value shifted out of bit 7 for {\tt RLC}, {\tt RLCA} and {\tt SLA}
\item contains the value shifted out of bit 0 for {\tt RRC}, {\tt RRCA}, {\tt SRA} and {\tt SRL}
}
\DetailsFlagTableParagraph
Finally, some instructions directly affect the value of \textbf{CF}:
\DetailsFlagTableList{
\item reset with {\tt AND}, {\tt OR} and {\tt XOR}
\item set with {\tt SCF}
\item completed with {\tt CCF}
}
\\
\end{tabularx}
}
\subsubsection{Effects}
\begin{tabular}{cl}
{\tt 0} & Flag is set to {\tt 0} \\
{\tt 1} & Flag is set to {\tt 1} \\
{\tt \FS} & Flag is modified according to operation \\
{\tt \FN} & Flag is not affected \\
{\tt \FU} & Effect on flag is unpredictable \\
{\tt \FX} & Special case, see notes below effects table \\
\DetailParityOverflow{v} & P/V flag is used as overflow \\
\DetailParityOverflow{p} & P/V flag is used as parity \\
\DetailParityOverflow{} & P/V is undefined or indicates other result \\
\end{tabular}
\subsubsection{Abbreviations}
\begin{tabularx}{\textwidth}{lX}
{\tt r} &
8-bit register {\tt A}-{\tt L} \\
{\tt n} &
8-bit immediate value \\
{\tt rr} &
16-bit register pair {\tt AF}, {\tt BC}, {\tt DE}, {\tt HL}, {\tt IX}, {\tt IY}, {\tt SP} (note in some cases particular register pairs may use different timing from the rest; if so, those will be explicitly indicated in their own line; {\tt rr} may still be used, though in those cases it will cover the remaining registers only) \\
{\tt nn} &
16-bit immediate value \\
{\tt s} &
Placeholder for argument when multiple variants are possible \\
{\tt d} &
If instruction takes 2 operands, {\tt d} indicates destination and {\tt s} source \\
\UNDOC & Indicates undocumented instruction \\
\ZXN & Indicates ZX Spectrum Next extended instruction \\
\end{tabularx}
\pagebreak
\IntentionallyEmpty
\pagebreak
\begin{basedescript}{
% setup basedescript styling for labels
\desclabelstyle{\multilinelabel}
\desclabelwidth{3cm}}
% setup spacing between items
\setlength\itemsep{1.5em}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{ADC}{d,s}
{\IH{AD}d with \IH{C}arry}
{\SymADC{d}{s}}
\begin{DetailVariants}[p{1.9cm}p{1.9cm}Xp{2.8cm}p{2.4cm}]
\textnormal{8 bit} & \textnormal{8 bit} & \textnormal{8 bit} & \textnormal{8 bit} & \textnormal{16 bit} \\
ADC A,A & ADC A,E & ADC A,(HL) & ADC A,IXH\UNDOC & ADC HL,BC \\
ADC A,B & ADC A,H & ADC A,(IX+d) & ADC A,IXL\UNDOC & ADC HL,DE \\
ADC A,C & ADC A,L & ADC A,(IY+d) & ADC A,IYH\UNDOC & ADC HL,HL \\
ADC A,D & ADC A,n & & ADC A,IYL\UNDOC & ADC HL,SP \\
\end{DetailVariants}
Adds source operand {\tt s} or contents of the memory location addressed by {\tt s} and value of carry flag to destination {\tt d}. Result is then stored to destination {\tt d}.
\begin{DetailEffects}[v]
\FlagsADCr[8-bit]
\FlagsADCrr[16-bit]
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagSF{\DetailFlagResultSign}
\DetailFlagZF{\DetailFlagResultZero}
\DetailFlagHF{\DetailFlagResultHalfCarry}*
\DetailFlagPV{\DetailFlagResultOverflow}*
\DetailFlagCF{\DetailFlagResultCarry}
\end{DetailEffectsFlags}
\begin{DetailTiming}
\DetailTime[A,r]{1}{4}
\DetailTime[A,n]{2}{7}
\DetailTime[A,(HL)]{2}{7}
\DetailTime[HL,rr]{4}{15}
\DetailTime[A,(IX+d)]{5}{19}
\DetailTime[A,(IY+d)]{5}{19}
\end{DetailTiming}
\end{DetailItem}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{ADD}{d,s}
{\IH{ADD}}
{\SymADD{d}{s}}
\begin{DetailVariants}[p{1.9cm}Xp{2.3cm}p{2.3cm}p{2.4cm}]
\textnormal{8-bit} & \textnormal{8-bit} & \textnormal{16-bit} & \textnormal{16-bit} & \textnormal{ZX Next} \\
ADD A,A & ADD A,(HL) & ADD IX,BC & ADD HL,BC & ADD BC,A\ZXN \\
ADD A,B & ADD A,(IX+d) & ADD IX,DE & ADD HL,DE & ADD DE,A\ZXN \\
ADD A,C & ADD A,(IY+d) & ADD IX,IX & ADD HL,HL & ADD HL,A\ZXN \\
ADD A,D & ADD A,IXH\UNDOC & ADD IX,SP & ADD HL,SP & ADD BE,nn\ZXN \\
ADD A,E & ADD A,IXL\UNDOC & ADD IY,BC & & ADD DE,nn\ZXN \\
ADD A,H & ADD A,IYH\UNDOC & ADD IY,DE & & ADD HL,nn\ZXN \\
ADD A,L & ADD A,IYL\UNDOC & ADD IY,IY \\
ADD A,n & & ADD IY,SP \\
\end{DetailVariants}
Similar to {\tt ADC} except carry flag is not used in calculation: adds operand {\tt s} or contents of the memory location addressed by {\tt s} to destination {\tt d}. Result is then stored to destination {\tt d}.
ZX Next Extended instructions for adding {\tt A} to 16-bit register pair, zero extend {\tt A} to 16-bits.
\begin{DetailEffects}[v]
\FlagsADDr[8-bit]
\FlagsADDrr[16-bit]
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagSF[8-bit only, set if:]{\DetailFlagResultSign}
\DetailFlagZF[8-bit only, set if:]{\DetailFlagResultZero}
\DetailFlagHF{\DetailFlagResultHalfCarry*}*
\DetailFlagPV[8-bit only, set if:]{\DetailFlagResultOverflow}*
\DetailFlagCF{\DetailFlagResultCarry*}
\end{DetailEffectsFlags}
\begin{DetailEffects}
\FlagsADDrra[\tt ADD rr,A\ZXN]
\FlagsADDrrnn[\tt ADD rr,nn\ZXN]
\end{DetailEffects}
\begin{DetailTiming}
\DetailTime[A,r]{1}{4}
\DetailTime[A,n]{2}{7}
\DetailTime[A,(HL)]{2}{7}
\DetailTime[rr,A\ZXN]{2}{8}
\DetailTime[HL,rr]{3}{11}
\DetailTime[IX,rr]{4}{15}
\DetailTime[IY,rr]{4}{15}
\DetailTime[rr,nn\ZXN]{4}{16}
\DetailTime[A,(IX+d)]{5}{19}
\DetailTime[A,(IY+d)]{5}{19}
\end{DetailTiming}
\end{DetailItem}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{AND}{s}
{bitwise \IH{AND}}
{\SymAND{s}}
\begin{DetailVariants}[4]
AND A\\
AND B\\
AND C\\
AND D
\columnbreak
AND E\\
AND H\\
AND L\\
AND n
\columnbreak
AND (HL)\\
AND (IX+d)\\
AND (IY+d)
\columnbreak
AND IXH\UNDOC\\
AND IXL\UNDOC\\
AND IYH\UNDOC\\
AND IYL\UNDOC
\end{DetailVariants}
\begin{tabularx}{\linewidth}{@{}Xl}
Performs bitwise AND between accumulator {\tt A} and the given operand. The result is then stored back to the accumulator. Individual bits are AND'ed as shown on the right:
&
\begin{tabular}[t]{cc|c}
{\tt A} & {\tt s} & Result \\
\hline
{\tt 0} & {\tt 0} & {\tt 0} \\
{\tt 0} & {\tt 1} & {\tt 0} \\
{\tt 1} & {\tt 0} & {\tt 0} \\
{\tt 1} & {\tt 1} & {\tt 1} \\
\end{tabular}
\\
\end{tabularx}
\begin{DetailEffects}[p]
\FlagsANDr
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagSF{\DetailFlagResultSign}
\DetailFlagZF{\DetailFlagResultZero}
\DetailFlagPV{\DetailFlagResultParity}
\end{DetailEffectsFlags}
\begin{DetailTiming}
\DetailTime[r]{1}{4}
\DetailTime[n]{2}{7}
\DetailTime[(HL)]{2}{7}
\DetailTime[(IX+d)]{5}{19}
\DetailTime[(IY+d)]{5}{19}
\end{DetailTiming}
\end{DetailItem}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{BIT}{b,s}
{test \IH{BIT}}
{\SymBIT{s}}
\begin{DetailVariants}
BIT b,A\\
BIT b,B\\
BIT b,C\\
BIT b,D
\columnbreak
BIT b,E\\
BIT b,H\\
BIT b,L
\columnbreak
BIT b,(HL)\\
BIT b,(IX+d)\\
BIT b,(IY+d)
\end{DetailVariants}
Tests specified bit {\tt b} ({\tt 0-7}) of the given register {\tt s} or contents of memory addressed by {\tt s} and sets zero flag according to result; if bit was 1, \FlagZF{} is 0 and vice versa.
\begin{DetailEffects}
\FlagsBITr
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagZF{bit {\tt b} of the given source argument is {\tt 0}}
\end{DetailEffectsFlags}
\begin{DetailTiming}
\DetailTime[b,r]{2}{8}
\DetailTime[b,(HL)]{3}{12}
\DetailTime[b,(IX+d)]{5}{20}
\DetailTime[b,(IY+d)]{5}{20}
\end{DetailTiming}
\end{DetailItem}
% these instructions should be here in alphabetical order, but I want them to be placed on the same page spread so reader can check the differences easily
\DetailItemsSeePageReference{BRLC, BSLA, BSRA, BSRF, BSRL}{See pages \DetailItemPageRef{BRLC} and \DetailItemPageRef{BSRL}}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{CALL}{nn}
{\IH{CALL} subroutine}
{\SymCALL{nn}}
Pushes program counter {\tt PC} to stack and calls subroutine at the given location {\tt nn} by changing {\tt PC} to point to address {\tt nn}.
\begin{DetailEffects}
\FlagsCALLnn
\end{DetailEffects}
\begin{DetailTiming}
\DetailTime{5}{17}
\end{DetailTiming}
\end{DetailItem}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{CALL c,nn}{}
{\IH{CALL} subroutine conditionally}
{\SymCALLc{nn}}
\vspace{1ex} % we need some vertical space to achieve same separation as multicols
\begin{tabular}{@{}llcll}
{\tt CALL C,nn} & calls if \FlagCF{} is set & &
{\tt CALL M,nn} & calls if \FlagSF{} is set\\
{\tt CALL NC,nn} & calls if \FlagCF{} is reset & &
{\tt CALL P,nn} & calls if \FlagSF{} is reset\\
{\tt CALL Z,nn} & calls if \FlagZF{} is set & &
{\tt CALL PE,nn} & calls if \FlagPV{} is set\\
{\tt CALL NZ,nn} & calls if \FlagZF{} is reset & &
{\tt CALL PO,nn} & calls if \FlagPV{} is reset\\
\end{tabular}
If the given condition is met, {\tt CALL nn} is performed, as described above.
\begin{DetailEffects}
\FlagsCALLccnn
\end{DetailEffects}
\begin{DetailTiming}
\DetailTime[c\normalfont{=false}]{3}{10}
\DetailTime[c\normalfont{=true}]{5}{17}
\end{DetailTiming}
\end{DetailItem}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{BRLC}{DE,B\ZXN}
{\IH{B}arrel \IH{R}otate \IH{L}eft \IH{C}ircular}
{\SymBRLC}
Rotates value in register pair {\tt DE} left for the amount given in bits 3-0 (low nibble) of register {\tt B}. To rotate right, use formula: {\tt B=16-places}. The result is stored in {\tt DE}.
\begin{DetailEffects}
\FlagsBRLC
\end{DetailEffects}
\begin{DetailTiming}
\DetailTime{2}{8}
\end{DetailTiming}
\end{DetailItem}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{BSLA}{DE,B\ZXN}
{\IH{B}arrel \IH{S}hift \IH{L}eft \IH{A}rithmetic}
{\SymBSLA}
Performs shift left of the value in register pair {\tt DE} for the amount given in lower 5 bits of register {\tt B}. The result is stored in {\tt DE}.
\begin{DetailEffects}
\FlagsBSLA
\end{DetailEffects}
\begin{DetailTiming}
\DetailTime{2}{8}
\end{DetailTiming}
\end{DetailItem}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{BSRA}{DE,B\ZXN}
{\IH{B}arrel \IH{S}hift \IH{R}ight \IH{A}rithmetic}
{\SymBSRA}
Performs arithmetical shift right of the value in register pair {\tt DE} for the amount given in lower 5 bits of register {\tt B}. The result is stored in {\tt DE}.
\begin{DetailEffects}
\FlagsBSRA
\end{DetailEffects}
\begin{DetailTiming}
\DetailTime{2}{8}
\end{DetailTiming}
\end{DetailItem}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{BSRF}{DE,B\ZXN}
{\IH{B}arrel \IH{S}hift \IH{R}ight \IH{F}ill-one}
{\SymBSRF}
Performs fill-one-way shift right of the value in register pair {\tt DE} for the amount given in lower 5 bits of register {\tt B}. The result is stored in {\tt DE}.
\begin{DetailEffects}
\FlagsBSRF
\end{DetailEffects}
\begin{DetailTiming}
\DetailTime{2}{8}
\end{DetailTiming}
\end{DetailItem}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{BSRL}{DE,B\ZXN}
{\IH{B}arrel \IH{S}hift \IH{R}ight \IH{L}ogical}
{\SymBSRL}
Performs logical shift right of the value in register pair {\tt DE} for the amount given in lower 5 bits of register {\tt B}. The result is stored in {\tt DE}.
\begin{DetailEffects}
\FlagsBSRL
\end{DetailEffects}
\begin{DetailTiming}
\DetailTime{2}{8}
\end{DetailTiming}
\end{DetailItem}
% CALL instructions should be here in alphabetical order, but I placed them before B*** above so they can be in the same page spread
\DetailItemSeePageReference{CALL}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{CCF}{}
{\IH{C}omplement \IH{C}arry \IH{F}lag}
{\SymCCF}
Complements (inverts) carry flag \FlagCF{}; if \FlagCF{} was {\tt 0} it's now {\tt 1} and vice versa. Previous value of \FlagCF{} is copied to \FlagHF{}.
\begin{DetailEffects}
\FlagsCCF
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagHF[]{Documentation says original value of \FlagCF{} is copied to \FlagHF{}, however under my tests \FlagHF{} remained unchanged}
\DetailFlagCF[]{if \FlagCF{} was {\tt 0} it's now {\tt 1} and vice versa}
\end{DetailEffectsFlags}
\begin{DetailTiming}
\DetailTime{1}{4}
\end{DetailTiming}
\end{DetailItem}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{CP}{s}
{\IH{C}om\IH{P}are}
{\SymCP{s}}
\begin{DetailVariants}[4]
CP A\\
CP B\\
CP C\\
CP D
\columnbreak
CP E\\
CP H\\
CP L\\
CP n
\columnbreak
CP (HL)\\
CP (IX+d)\\
CP (IY+d)
\columnbreak
CP IXH\UNDOC\\
CP IXL\UNDOC\\
CP IYH\UNDOC\\
CP IYL\UNDOC
\end{DetailVariants}
Operand {\tt s} or content of the memory location addressed by {\tt s} is subtracted from accumulator {\tt A}. Status flags are updated according to the result, but the result is then discarded (value of {\tt A} is not changed).
\begin{DetailEffects}[v]
\FlagsCPr
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagHF{\DetailFlagResultHalfBorrow}*
\DetailFlagPV{
\DetailFlagsList{
\item {\tt A} and {\tt s} positive and {\tt A-s} result negative
\item {\tt A} and {\tt s} negative and {\tt A-s} result positve
}
\DetailFlagResultOverflowListBottomSpacing % we don't want additional space below the table, otherwise next item would have larger gap
}
\end{DetailEffectsFlags}
Other flags are set like this when {\tt A} is greater than, equal or less than {\tt s}:
\begin{DetailEffects}[v][]
\Flags[A$>$s]{0}{0}{\FS}{\FS}{1}{0}
\Flags[A$=$s]{0}{1}{\FS}{\FS}{1}{0}
\Flags[A$<$s]{1}{0}{\FS}{\FS}{1}{1}
\end{DetailEffects}
With this in mind, we can derive the programs for common comparisons:
\newcommand{\CPExampleTitle}[1]{{\tt #1}\vspace{-1.5ex}}
%-----------------------------------------------------------------------------------------------------
\begin{multicols}{2}
\CPExampleTitle{A=s}
\begin{tcblisting}{right skip=1em}
CP s
JP Z, true ; A=s?
false: ; A!=s
true: ; A=s
\end{tcblisting}
\CPExampleTitle{A$\neq$s}
\begin{tcblisting}{}
CP s
JP NZ, true ; A!=s?
false: ; A=s
true: ; A!=s
\end{tcblisting}
\end{multicols}
%-----------------------------------------------------------------------------------------------------
\begin{multicols}{2}
\CPExampleTitle{A$\leqslant$s}
\begin{tcblisting}{right skip=1em}
CP s
JP M, true ; A<s?
JP Z, true ; A=s?
false: ; A>s
true: ; A<=s
\end{tcblisting}
\columnbreak
\CPExampleTitle{A<s}
\begin{tcblisting}{}
CP s
JP M, true ; A<s?
false: ; A>=s
true: ; A<s
\end{tcblisting}
\end{multicols}
%-----------------------------------------------------------------------------------------------------
\begin{multicols}{2}
\CPExampleTitle{A$\geqslant$s}
\begin{tcblisting}{right skip=1em}
CP s
JP M, false ; A<s?
true: ; A>=s
false: ; A<s
\end{tcblisting}
\columnbreak
\CPExampleTitle{A>s}
\begin{tcblisting}{}
CP s
JP M, false ; A<s?
JP Z, false ; A=s?
true: ; A>s
false: ; A<=s
\end{tcblisting}
\end{multicols}
\DetailNote[-1ex]{
Note: the examples use two labels to emphasize both results. But only one is needed. Furthermore, depending on the actual needs, the programs can also use no label at all. For example, we can use {\tt RET} instead of {\tt JP} when used within a subroutine, and the desired outcome is to return if the condition is not met:
}
%-----------------------------------------------------------------------------------------------------
\begin{multicols}{2}
\CPExampleTitle{A=s}
\begin{tcblisting}{right skip=1em}
CP s
RET NZ ; A!=s?
; A=s
\end{tcblisting}
\CPExampleTitle{A$\neq$s}
\begin{tcblisting}{}
CP s
RET Z ; A=s?
; A!=s
\end{tcblisting}
\end{multicols}
%-----------------------------------------------------------------------------------------------------
\begin{multicols}{2}
\CPExampleTitle{A$\leqslant$s}
\begin{tcblisting}{right skip=1em}
CP s
JP M, true ; A<s?
JP Z, true ; A=s?
RET ; A>s
true: ; A<=s
\end{tcblisting}
\columnbreak
\CPExampleTitle{A<s}
\begin{tcblisting}{}
CP s
RET P ; A>=s?
; A<s
\end{tcblisting}
\end{multicols}
%-----------------------------------------------------------------------------------------------------
\begin{multicols}{2}
\CPExampleTitle{A$\geqslant$s}
\begin{tcblisting}{right skip=1em}
CP s
RET M ; A<s?
; A>=s
\end{tcblisting}
\CPExampleTitle{A>s}
\begin{tcblisting}{}
CP s
RET M ; A<s?
RET Z ; A=s?
; A>s
\end{tcblisting}
\end{multicols}
\DetailNote[-1ex]{
Note: some of the comparisons are reversed. And {\tt A$\leqslant$s} still requires a label because the condition is true if either \FlagSF{} or \FlagZF{} is set.
}
\DetailNote{
Note: I opted to use \FlagSF{} for some of the comparisons. It makes more sense to me this way. But you can just as well use \FlagCF{} instead. As evident from the table on the previous page, both flags are updated the same way, so you could use {\tt JP C} or {\tt RET C} instead of {\tt M} and {\tt JP NZ} or {\tt RET NZ} instead of {\tt P}. This is due to {\tt CP} performing a subtraction {\tt A-s} internally. So when {\tt s} is greater than {\tt A}, the result of the subtraction is negative, meaning the sign flag is set. At the same time a borrow is needed, so the carry is set too. I thought it's worth mentioning since you may find examples using the carry flag elsewhere and wonder why.
}
\vspace{2ex} % some vertical gap between notes and timings
\begin{DetailTiming}
\DetailTime[r]{1}{4}
\DetailTime[n]{2}{7}
\DetailTime[(HL)]{2}{7}
\DetailTime[(IX+d)]{5}{19}
\DetailTime[(IY+d)]{5}{19}
\end{DetailTiming}
\end{DetailItem}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{CPD}{}
{\IH{C}om\IH{P}are and \IH{D}ecrement}
{\SymCPD}
Subtracts contents of memory location addressed by {\tt HL} register pair from accumulator {\tt A}. Result is then discarded. Afterwards both {\tt HL} and {\tt BC} are decremented.
\begin{DetailEffects}
\FlagsCPD
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagSF{{\tt A<(HL)} before {\tt HL} is decremented}
\DetailFlagZF{{\tt A=(HL)} before {\tt HL} is decremented}
\DetailFlagPV{{\tt BC$\neq$0} after execution}
\end{DetailEffectsFlags}
\begin{DetailTiming}
\DetailTime{4}{16}
\end{DetailTiming}
\end{DetailItem}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{CPDR}{}
{\IH{C}om\IH{P}are and \IH{D}ecrement \IH{R}epeated}
{\SymCPDR}
Repeats {\tt CPD} until either {\tt A=(HL)} or {\tt BC=0}. If {\tt BC} is set to {\tt 0} before instruction execution, it loops through 64KB if no match is found. See {\tt CPIR} for example.
\begin{DetailEffects}
\FlagsCPDR
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagSF{{\tt A<(HL)} before {\tt HL} is decremented}
\DetailFlagZF{{\tt A=(HL)} before {\tt HL} is decremented}
\DetailFlagPV{{\tt BC$\neq$0} after execution}
\end{DetailEffectsFlags}
\begin{DetailTiming}
\DetailTimeRegular[{\tt BC}=0 or {\tt A}={\tt (HL)}]{4}{16}
\DetailTimeRegular[{\tt BC}$\neq$0 and {\tt A}$\neq${\tt (HL)}]{5}{21}
\end{DetailTiming}
\end{DetailItem}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{CPI}{}
{\IH{C}om\IH{P}are and \IH{I}ncrement}
{\SymCPI}
Subtracts contents of memory location addressed by {\tt HL} register pair from accumulator {\tt A}. Result is then discarded. Afterwards {\tt HL} is incremented and {\tt BC} decremented.
\begin{DetailEffects}
\FlagsCPI
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagSF{{\tt A<(HL)} before {\tt HL} is decremented}
\DetailFlagZF{{\tt A=(HL)} before {\tt HL} is decremented}
\DetailFlagPV{{\tt BC$\neq$0} after execution}
\end{DetailEffectsFlags}
\begin{DetailTiming}
\DetailTime{4}{16}
\end{DetailTiming}
\end{DetailItem}
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
\begin{DetailItem}{CPIR}{}
{\IH{C}om\IH{P}are and \IH{D}ecrement \IH{R}epeated}
{\SymCPIR}
Repeats {\tt CPI} until either {\tt A=(HL)} or {\tt BC=0}. If {\tt BC} is set to {\tt 0} before instruction execution, it loops through 64KB if no match is found.
Example, searching for {\tt \$AB} in memory from \MemAddr{0000}-\MemAddr{999}:
\begin{multicols}{2}
{\tt CPIR} = finding first occurrence:
\begin{tcblisting}{right skip=1em}
LD HL, &0000
LD BC, &0999
LD A, &AB
CPIR
\end{tcblisting}
{\tt CPDR} = finding last occurrence:
\begin{tcblisting}{}
LD HL, &0999
LD BC, &0999
LD A, &AB
CPDR
\end{tcblisting}
\end{multicols}
\begin{DetailEffects}
\FlagsCPIR
\end{DetailEffects}
\begin{DetailEffectsFlags}
\DetailFlagSF{{\tt A<(HL)} before {\tt HL} is decremented}
\DetailFlagZF{{\tt A=(HL)} before {\tt HL} is decremented}
\DetailFlagPV{{\tt BC$\neq$0} after execution}
\end{DetailEffectsFlags}
\begin{DetailTiming}
\DetailTimeRegular[{\tt BC}=0 or {\tt A}={\tt (HL)}]{4}{16}
\DetailTimeRegular[{\tt BC}$\neq$0 and {\tt A}$\neq${\tt (HL)}]{5}{21}
\end{DetailTiming}
\end{DetailItem}
\pagebreak
%----------------------------------------------------------------------------------------------------------------------
% ███████████████████████████████████████████████████████████████████
% █▄─▄█▄─▀█▄─▄█─▄▄▄▄█─▄─▄─█▄─▄▄▀█▄─██─▄█─▄▄▄─█─▄─▄─█▄─▄█─▄▄─█▄─▀█▄─▄█
% ██─███─█▄▀─██▄▄▄▄─███─████─▄─▄██─██─██─███▀███─████─██─██─██─█▄▀─██
% ▀▄▄▄▀▄▄▄▀▀▄▄▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▄▀▄▄▄▄▀▄▄▄▀▀▄▄▀
%----------------------------------------------------------------------------------------------------------------------
% by moving CPL to next page, we can have DAA fit the whole of the page. Otherwise last part (timing) doesn't fit the same page (it's still in the same spread, but this way it's nicer)
\DetailItemsSeePageReference{CPL}{See next page}
\begin{DetailItem}{DAA}{}
{\IH{D}ecimal \IH{A}djust \IH{A}ccumulator}
{}
Updates accumulator {\tt A} for BCD correction after arithmetic operations using the following algorithm:
\begin{enumerate}
\item The least significant 4 bits of accumulator {\tt A} (low nibble) are checked first. If they contain invalid BCD number (greater than 9), or \FlagHF{} is set, the value of {\tt A} is adjusted based on the value of \FlagNF{}: if it's reset, {\tt \$06} is added to {\tt A}, if set, {\tt \$06} is removed from {\tt A}.
\item Then 4 most significant bits of accumulator {\tt A} (high nibble) are checked in a similar fashion. If they contain invalid BCD number, or \textbf{CF} is set, the value of {\tt A} is adjusted: if \FlagNF{} is not set, {\tt \$60} is added to {\tt A}, if \FlagNF{} is set, {\tt \$60} is removed from {\tt A}.
\item Finally flags are changed accordingly, as described below.
\end{enumerate}
% Symbolically, the algorithm could be expressed as:
% {
% \tt \small
% if (A$\wedge$\$0F>\$09)$\vee$(HF=1):\\
% \hspace*{1em}if NF=0:~A$\leftarrow$A+\$06~else:~A$\leftarrow$A-\$06\\
% if (A$\wedge$\$F0>\$90)$\vee$(CF=1):\\
% \hspace*{1em}if NF=0:~A$\leftarrow$A+\$60~else:~A$\leftarrow$A-\$60\\
% }
\begin{DetailEffects}[p]
\FlagsDAA
\end{DetailEffects}
\newcommand{\DAAIn}[1]{#1}
\newcommand{\DAAOut}[1]{\RArrow{6pt}#1}
\begin{DetailEffectsFlags}
\DetailFlagSF{\DetailFlagResultSign[A] after operation}