aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/gna/issue30/alu.vhdl
blob: 10ae2e6efb5ace38d458c6af0835da52feb3838c (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
library	ieee;
use		ieee.std_logic_1164.all;

library work;
use		work.definitions.all;

entity ccf_operation is
port(
	flags_in:	in	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_ccf_operation of ccf_operation is
begin
	-- A point of disagreement has been found between the Z80 user manual
	-- and Lance Levinthal's book entitled "Z80 Assembly Language Programming".
	-- The Z80 user manual says the half-carry bit gets the previous carry;
	-- Levinthal says the half-carry bit is unchanged.  For now, go with
	-- Levinthal's version as the Z80 users manual is inconsistent with
	-- itself on other instructions.  At this time, no such inconsistencies
	-- have been found with Levinthal's work.

	flags_out <= (	carry_bit => not flags_in(carry_bit),
					half_carry_bit => flags_in(carry_bit),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library work;
use		work.definitions.all;

entity sll8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_sll8bit of sll8bit is

signal	sll_result:	std_logic_vector(7 downto 0);
begin
	-- This operation is not documented by Zilog, but seems to work in their
	-- finished chip.  This code may not work the same way as the Z80 hardware
	-- works.  The functionality is assumed from the SRL instruction.

	sll_result <= operand(6 downto 0) & '0';

	output <= sll_result;
	flags_out <= (	carry_bit => operand(7),
					zero_bit => not (sll_result(7) or sll_result(6) or sll_result(5) or sll_result(4) or
									 sll_result(3) or sll_result(2) or sll_result(1) or sll_result(0)),
					parity_overflow_bit => not (sll_result(7) xor sll_result(6) xor sll_result(5) xor
												sll_result(4) xor sll_result(3) xor sll_result(2) xor
												sll_result(1) xor sll_result(0)),
					sign_bit => operand(6),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library work;
use		work.definitions.all;

entity srl8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_srl8bit of srl8bit is

signal	srl_result:	std_logic_vector(7 downto 0);

begin
	srl_result <= '0' & operand(7 downto 1);

	output <= srl_result;
	flags_out <= (	carry_bit => operand(0),
					zero_bit => not (srl_result(7) or srl_result(6) or srl_result(5) or srl_result(4) or
									 srl_result(3) or srl_result(2) or srl_result(1) or srl_result(0)),
					parity_overflow_bit => not (srl_result(7) xor srl_result(6) xor srl_result(5) xor
												srl_result(4) xor srl_result(3) xor srl_result(2) xor
												srl_result(1) xor srl_result(0)),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library work;
use		work.definitions.all;

entity and8bit is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_and8bit of and8bit is

signal	and_result:		std_logic_vector(7 downto 0);

begin
	and_result <= operand1 and operand2;

	flags_out <= (	sign_bit => and_result(7),
					zero_bit => not (and_result(7) or and_result(6) or and_result(5) or and_result(4) or
									 and_result(3) or and_result(2) or and_result(1) or and_result(0)),
					half_carry_bit => '1',
					parity_overflow_bit => not (and_result(7) xor and_result(6) xor and_result(5) xor
												and_result(4) xor and_result(3) xor and_result(2) xor
												and_result(1) xor and_result(0)),
					others => '0');

	output <= and_result;
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity or8bit is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_or8bit of or8bit is

signal	or_result:		std_logic_vector(7 downto 0);

begin
	or_result <= operand1 or operand2;

	output <= or_result;
	flags_out <= (	sign_bit => or_result(7),
					half_carry_bit => '1',
					zero_bit => not (or_result(7) or or_result(6) or or_result(5) or or_result(4) or
									 or_result(3) or or_result(2) or or_result(1) or or_result(0)),
					parity_overflow_bit => not (or_result(7) xor or_result(6) xor or_result(5) xor
												or_result(4) xor or_result(3) xor or_result(2) xor
												or_result(1) xor or_result(0)),
					others => '0');
end;


library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity sra8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_sra8bit of sra8bit is

signal	sra_result:	std_logic_vector(7 downto 0);

begin
	sra_result <= operand(7) & operand(7 downto 1);

	output <= sra_result;
	flags_out <= (	carry_bit => operand(0),
					zero_bit => not (sra_result(7) or sra_result(6) or sra_result(5) or sra_result(4) or
									 sra_result(3) or sra_result(2) or sra_result(1) or sra_result(0)),
					parity_overflow_bit => not (sra_result(7) xor sra_result(6) xor sra_result(5) xor
												sra_result(4) xor sra_result(3) xor sra_result(2) xor
												sra_result(1) xor sra_result(0)),
					sign_bit => operand(7),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity xor8bit is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_xor8bit of xor8bit is

signal	xor_result:		std_logic_vector(7 downto 0);

begin
	xor_result <= operand1 xor operand2;

	output <= xor_result;
	flags_out <= (	sign_bit => xor_result(7),
					half_carry_bit => '1',
					zero_bit => not (xor_result(7) or xor_result(6) or xor_result(5) or xor_result(4) or
									 xor_result(3) or xor_result(2) or xor_result(1) or xor_result(0)),
					parity_overflow_bit => not (xor_result(7) xor xor_result(6) xor xor_result(5) xor
												xor_result(4) xor xor_result(3) xor xor_result(2) xor
												xor_result(1) xor xor_result(0)),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity sla8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_sla8bit of sla8bit is

signal	sla_result:	std_logic_vector(7 downto 0);

begin
	sla_result <= operand(6 downto 0) & '0';

	output <= sla_result;
	flags_out <= (	sign_bit => sla_result(7),
					half_carry_bit => '1',
					zero_bit => not (sla_result(7) or sla_result(6) or sla_result(5) or sla_result(4) or
									 sla_result(3) or sla_result(2) or sla_result(1) or sla_result(0)),
					parity_overflow_bit => not (sla_result(7) xor sla_result(6) xor sla_result(5) xor
												sla_result(4) xor sla_result(3) xor sla_result(2) xor
												sla_result(1) xor sla_result(0)),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

entity subtractor is
port(
	minuend, subtrahend:	in	std_logic;
	borrow_in:				in	std_logic;
	difference:				out	std_logic;
	borrow_out:				out	std_logic
);
end;

architecture struct_subtractor of subtractor is
begin

-- These expressions were derived from the truth table of a single bit subtractor and simplified with
-- a Karnaugh map.

	difference <=	(borrow_in and (not minuend) and (not subtrahend)) or
					((not borrow_in) and (not minuend) and subtrahend) or
					(borrow_in and minuend and subtrahend) or
					((not borrow_in) and minuend and (not subtrahend));

	borrow_out <=	(not minuend and subtrahend) or
					(borrow_in and (not minuend)) or
					(borrow_in and subtrahend);
end;

library	ieee;
use		ieee.std_logic_1164.all;

entity subtractorN is
generic(
	N:	positive
);
port(
	minuend:	in	std_logic_vector((N-1) downto 0);
	subtrahend:	in	std_logic_vector((N-1) downto 0);
	borrow_in:	in	std_logic;
	difference:	out	std_logic_vector((N-1) downto 0);
	borrow_out:	out	std_logic
);
end;

architecture struct_subtractorN of subtractorN is
component subtractor is
port(
	minuend, subtrahend:	in	std_logic;
	borrow_in:				in	std_logic;
	difference:				out	std_logic;
	borrow_out:				out	std_logic
);
end component;

signal	borrow:	std_logic_vector(N downto 0);

begin
--	These expressions were derived from the truth table of a single bit subtractor and simplified with a
--	Karnaugh map.
--	d = difference, m = minuend, s = subtrahend, b = borrow
--
--	d(i) =	(b(i) and (not m(i)) and (not s(i))) or
--			((not b(i)) and (not m(i)) and s(i)) or
--			(b(i) and m(i) and s(i)) or
--			((not b(i)) and m(i) and (not s(i)))
--
--	b(i+1) =	(not m(i) and s(i)) or
--				(b(i) and (not m(i))) or
--				(b(i) and s(i)

	borrow(0) <= borrow_in;

	u1: for i in 0 to (N-1) generate
		u: subtractor port map(
				minuend => minuend(i),
				subtrahend => subtrahend(i),
				borrow_in => borrow(i),
				difference => difference(i),
				borrow_out => borrow(i+1)
			);
		end generate;

	borrow_out <= borrow(N);
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity subtractor8x2 is
port(
	minuend, subtrahend:	in	std_logic_vector(7 downto 0);
	borrow_in:				in	std_logic;
	difference:				out	std_logic_vector(7 downto 0);
	flags_out:				out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_subtractor8x2 of subtractor8x2 is
component subtractor is
port(
	minuend, subtrahend:	in	std_logic;
	borrow_in:				in	std_logic;
	difference:				out	std_logic;
	borrow_out:				out	std_logic
);
end component;

signal	borrow:	std_logic_vector(8 downto 0);
signal	d:		std_logic_vector(7 downto 0);

begin
	borrow(0) <= borrow_in;

	u1: for i in 0 to 7 generate
		u: subtractor port map(
				minuend => minuend(i),
				subtrahend => subtrahend(i),
				borrow_in => borrow(i),
				difference => d(i),
				borrow_out => borrow(i+1)
			);
		end generate;

	difference <= d;

	flags_out <= (	sign_bit => d(7),
					zero_bit => not (d(0) or d(1) or d(2) or d(3) or d(4) or d(5) or d(6) or d(7)),
					half_carry_bit => borrow(4),
					parity_overflow_bit => (minuend(7) xor subtrahend(7)) and (minuend(7) xor d(7)),
					add_sub_bit => '1',
					carry_bit => borrow(8),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

entity adder is
port(
	addend, augend:	in	std_logic;
	carry_in:		in	std_logic;
	sum:			out	std_logic;
	carry_out:		out	std_logic
);
end;

architecture struct_adder of adder is
begin
--	These expressions are derived from a single bit full adder truth table and simplified with a
--	Karnaugh map.
	sum <=	((not (carry_in)) and (not addend) and augend) or
			((not carry_in) and addend and (not augend)) or
			(carry_in and (not addend) and (not augend)) or
			(carry_in and addend and augend);

	carry_out <=	(addend and augend) or
					(carry_in and addend) or
					(carry_in and augend);
end;

library	ieee;
use		ieee.std_logic_1164.all;

entity adderN is
generic(
	N:	positive
);
port(
	addend:		in	std_logic_vector((N-1) downto 0);
	augend:		in	std_logic_vector((N-1) downto 0);
	carry_in:	in	std_logic;
	sum:		out	std_logic_vector((N-1) downto 0);
	carry_out:	out	std_logic
);
end;

-- Tested with Modelsim 2015/12/11, works.

architecture struct_adderN of adderN is
component adder is
port(
	addend, augend:	in	std_logic;
	carry_in:		in	std_logic;
	sum:			out	std_logic;
	carry_out:		out	std_logic
);
end component;

signal	carry:	std_logic_vector(N downto 0);

begin
	carry(0) <= carry_in;

	u1: for i in 0 to (N-1) generate
		u: adder port map(
				addend => addend(i),
				augend => augend(i),
				carry_in => carry(i),
				sum => sum(i),
				carry_out => carry(i+1)
			);
		end generate;

	carry_out <= carry(N);
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity adder8x2 is
port(
	addend, augend:		in	std_logic_vector(7 downto 0);
	carry_in:			in	std_logic;
	sum:				out	std_logic_vector(7 downto 0);
	flags_out:			out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.
-- The adderN version is not used because access to carry out of bit 3 is required.

architecture struct_adder8x2 of adder8x2 is
component adder is
port(
	addend, augend:	in	std_logic;
	carry_in:		in	std_logic;
	sum:			out	std_logic;
	carry_out:		out	std_logic
);
end component;

signal	result:	std_logic_vector(7 downto 0);
signal	carry:	std_logic_vector(8 downto 0);

begin
	carry(0) <= carry_in;

	u1: for i in 0 to 7 generate
		u: adder port map(
				addend => addend(i),
				augend => augend(i),
				carry_in => carry(i),
				sum => result(i),
				carry_out => carry(i+1)
			);
		end generate;

	sum <= result;
	flags_out <= (	sign_bit => result(7),
					zero_bit => not (result(7) or result(6) or result(5) or result(4) or
									 result(3) or result(2) or result(1) or result(0)),
					half_carry_bit => carry(4),
					parity_overflow_bit => not	(addend(7) xor augend(7)) and
												(addend(7) xor result(7)),
					add_sub_bit => '0',
					carry_bit => carry(8),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity cpl is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_cpl of cpl is
begin
	output <= not operand;
	flags_out <= (	half_carry_bit => '1',
					add_sub_bit => '1',
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity rlc8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_rlc8bit of rlc8bit is

signal	rlc_result:	std_logic_vector(7 downto 0);

begin
	rlc_result(7 downto 1) <= operand(6 downto 0);
	rlc_result(0) <= operand(7);

	output <= rlc_result;
	flags_out <= (	carry_bit => operand(7),
					parity_overflow_bit => not (rlc_result(7) xor rlc_result(6) xor rlc_result(5) xor
												rlc_result(4) xor rlc_result(3) xor rlc_result(2) xor
												rlc_result(1) xor rlc_result(0)),
					zero_bit => not (rlc_result(7) or rlc_result(6) or rlc_result(5) or rlc_result(4) or
									 rlc_result(3) or rlc_result(2) or rlc_result(1) or rlc_result(0)),
					sign_bit => rlc_result(7),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity rrc8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_rrc8bit of rrc8bit is

signal	rrc_result:	std_logic_vector(7 downto 0);

begin
	rrc_result(6 downto 0) <= operand(7 downto 1);
	rrc_result(7) <= operand(0);

	output <= rrc_result;
	flags_out <= (	carry_bit => operand(0),
					zero_bit => not (rrc_result(7) or rrc_result(6) or rrc_result(5) or rrc_result(4) or
									 rrc_result(3) or rrc_result(2) or rrc_result(1) or rrc_result(0)),
					parity_overflow_bit => not (rrc_result(7) xor rrc_result(6) xor rrc_result(5) xor
												rrc_result(4) xor rrc_result(3) xor rrc_result(2) xor
												rrc_result(1) xor rrc_result(0)),
					sign_bit => operand(0),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity rl8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	carry_in:	in	std_logic;
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_rl8bit of rl8bit is

signal	rl_result:	std_logic_vector(7 downto 0);

begin
	rl_result (7 downto 1) <= operand(6 downto 0);
	rl_result(0) <= carry_in;

	output <= rl_result;
	flags_out <= (	carry_bit => operand(7),
					zero_bit => not (rl_result(7) or rl_result(6) or rl_result(5) or rl_result(4) or
									 rl_result(3) or rl_result(2) or rl_result(1) or rl_result(0)),
					parity_overflow_bit => not ((rl_result(7) xor rl_result(6) xor rl_result(5) xor
												 rl_result(4) xor rl_result(3) xor rl_result(2) xor
												 rl_result(1) xor rl_result(0))),
					sign_bit => operand(6),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity rr8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	carry_in:	in	std_logic;
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_rr8bit of rr8bit is

signal	rr_result:	std_logic_vector(7 downto 0);

begin
	rr_result(6 downto 0) <= operand(7 downto 1);
	rr_result(7) <= carry_in;

	output <= rr_result;
	flags_out <= (	carry_bit => operand(0),
					zero_bit => not (rr_result(7) or rr_result(6) or rr_result(5) or rr_result(4) or
									 rr_result(3) or rr_result(2) or rr_result(1) or rr_result(0)),
					parity_overflow_bit => not (rr_result(7) xor rr_result(6) xor rr_result(5) xor
												rr_result(4) xor rr_result(3) xor rr_result(2) xor
												rr_result(1) xor rr_result(0)),
					sign_bit => carry_in,
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

entity daa is
port(
	operand:	in	std_logic_vector(7 downto 0);
	flags_in:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Untested, this is nothing more than a stub with code to prevent unassigned variable warnings/errors.

architecture struct_daa of daa is
begin
	output <= operand;
	flags_out <= flags_in;
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity bit_op is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_bit_op of bit_op is

signal	zero:	std_logic;

begin
	zero <= '1' when (operand1 and operand2) = x"00" else '0';

	flags_out <= (	zero_bit => zero,
					half_carry_bit => '1',
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity rld is
port(
	primary_op:			in	std_logic_vector(7 downto 0);
	secondary_op:		in	std_logic_vector(7 downto 0);
	result:				out	std_logic_vector(7 downto 0);
	secondary_result:	out	std_logic_vector(7 downto 0);
	flags_out:			out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_rld of rld is

signal	primary_result:	std_logic_vector(7 downto 0);

begin
	primary_result(7 downto 4) <= primary_op(7 downto 4);
	primary_result(3 downto 0) <= secondary_op(7 downto 4);
	result <= primary_result;
	secondary_result(7 downto 4) <= secondary_op(3 downto 0);
	secondary_result(3 downto 0) <= primary_op(3 downto 0);
	flags_out <= (	sign_bit => primary_result(7),
					zero_bit => not (primary_result(7) or primary_result(6) or primary_result(5) or
									 primary_result(4) or primary_result(3) or primary_result(2) or
									 primary_result(1) or primary_result(0)),
					parity_overflow_bit => not (primary_result(7) xor primary_result(6) xor
												primary_result(5) xor primary_result(4) xor
												primary_result(3) xor primary_result(2) xor
												primary_result(1) xor primary_result(0)),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity rrd is
port(
	primary_op:			in	std_logic_vector(7 downto 0);
	secondary_op:		in	std_logic_vector(7 downto 0);
	result:				out	std_logic_vector(7 downto 0);
	secondary_result:	out	std_logic_vector(7 downto 0);
	flags_out:			out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_rrd of rrd is

signal	primary_result:	std_logic_vector(7 downto 0);

begin
	primary_result(7 downto 4) <= primary_op(7 downto 4);
	primary_result(3 downto 0) <= secondary_op(3 downto 0);
	result <= primary_result;
	secondary_result(7 downto 4) <= primary_op(3 downto 0);
	secondary_result(3 downto 0) <= secondary_op(7 downto 4);
	flags_out <= (	sign_bit => primary_result(7),
					zero_bit => not (primary_result(7) or primary_result(6) or primary_result(5) or
									 primary_result(4) or primary_result(3) or primary_result(2) or
									 primary_result(1) or primary_result(0)),
					parity_overflow_bit => not (primary_result(7) xor primary_result(6) xor
												primary_result(5) xor primary_result(4) xor
												primary_result(3) xor primary_result(2) xor
												primary_result(1) xor primary_result(0)),
					others => '0');

end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity in_rc_flags is
port(
	operand:	in	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_in_rc_flags of in_rc_flags is
begin
	flags_out <= (	zero_bit => not (operand(7) or operand(6) or operand(5) or operand(4) or
									 operand(3) or operand(2) or operand(1) or operand(0)),
					sign_bit => operand(7),
					parity_overflow_bit => not (operand(7) xor operand(6) xor operand(5) xor
												operand(4) xor operand(3) xor operand(2) xor
												operand(1) xor operand(0)),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity bmtc is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end;

-- Tested with Modelsim 2015/11/25, works.

architecture struct_bmtc of bmtc is

signal	result:	std_logic_vector(7 downto 0);

begin
	result <= operand1 or operand2;
	output <= result;
	flags_out <= (	parity_overflow_bit => not (result(7) or result(6) or result(5) or result(4) or
												result(3) or result(2) or result(1) or result(0)),
					others => '0');
end;

library	ieee;
use		ieee.std_logic_1164.all;

library	work;
use		work.definitions.all;

entity alu is
port(
	-- control
	operation:			in	std_logic_vector(4 downto 0);

	-- operands
	primary_operand:	in	std_logic_vector(7 downto 0);
	secondary_operand:	in	std_logic_vector(7 downto 0);
	flags_in:			in	std_logic_vector(7 downto 0);

	--	results
	output, flags_out:	out	std_logic_vector(7 downto 0);
	secondary_out:		out	std_logic_vector(7 downto 0)
);
end;

-- Tested 2016/11/22, works on Modelsim simulator along with all components.

architecture struct_alu of alu is
component bmtc is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component srl8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component sll8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component sra8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component sla8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component in_rc_flags is
port(
	operand:	in	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component ccf_operation is
port(
	flags_in:	in	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component cpl is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component xor8bit is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component or8bit is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component and8bit is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component subtractor8x2 is
port(
	minuend, subtrahend:	in	std_logic_vector(7 downto 0);
	borrow_in:				in	std_logic;
	difference:				out	std_logic_vector(7 downto 0);
	flags_out:				out	std_logic_vector(7 downto 0)
);
end component;

component adder8x2 is
port(
	addend, augend:		in	std_logic_vector(7 downto 0);
	carry_in:			in	std_logic;
	sum:				out	std_logic_vector(7 downto 0);
	flags_out:			out	std_logic_vector(7 downto 0)
);
end component;

component magnitudeN is
generic(
	N:	positive
);
port(
	a:		in	std_logic_vector((N-1) downto 0);
	b:		in	std_logic_vector((N-1) downto 0);
	equal:	out	std_logic;
	lt:		out	std_logic; 	-- '1' if a < b
	gt:		out	std_logic	-- '1' if a > b
);
end component;

component rrd is
port(
	primary_op:			in	std_logic_vector(7 downto 0);
	secondary_op:		in	std_logic_vector(7 downto 0);
	result:				out	std_logic_vector(7 downto 0);
	secondary_result:	out	std_logic_vector(7 downto 0);
	flags_out:			out	std_logic_vector(7 downto 0)
);
end component;

component rld is
port(
	primary_op:			in	std_logic_vector(7 downto 0);
	secondary_op:		in	std_logic_vector(7 downto 0);
	result:				out	std_logic_vector(7 downto 0);
	secondary_result:	out	std_logic_vector(7 downto 0);
	flags_out:			out	std_logic_vector(7 downto 0)
);
end component;

component rlc8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component rl8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	carry_in:	in	std_logic;
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component rrc8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component rr8bit is
port(
	operand:	in	std_logic_vector(7 downto 0);
	carry_in:	in	std_logic;
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component daa is
port(
	operand:	in	std_logic_vector(7 downto 0);
	flags_in:	in	std_logic_vector(7 downto 0);
	output:		out	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component bit_op is
port(
	operand1:	in	std_logic_vector(7 downto 0);
	operand2:	in	std_logic_vector(7 downto 0);
	flags_out:	out	std_logic_vector(7 downto 0)
);
end component;

component encoder32xN is
generic(
	N:	positive
);
port(
	data0:		in	std_logic_vector((N-1) downto 0);
	data1:		in	std_logic_vector((N-1) downto 0);
	data2:		in	std_logic_vector((N-1) downto 0);
	data3:		in	std_logic_vector((N-1) downto 0);
	data4:		in	std_logic_vector((N-1) downto 0);
	data5:		in	std_logic_vector((N-1) downto 0);
	data6:		in	std_logic_vector((N-1) downto 0);
	data7:		in	std_logic_vector((N-1) downto 0);
	data8:		in	std_logic_vector((N-1) downto 0);
	data9:		in	std_logic_vector((N-1) downto 0);
	data10:		in	std_logic_vector((N-1) downto 0);
	data11:		in	std_logic_vector((N-1) downto 0);
	data12:		in	std_logic_vector((N-1) downto 0);
	data13:		in	std_logic_vector((N-1) downto 0);
	data14:		in	std_logic_vector((N-1) downto 0);
	data15:		in	std_logic_vector((N-1) downto 0);
	data16:		in	std_logic_vector((N-1) downto 0);
	data17:		in	std_logic_vector((N-1) downto 0);
	data18:		in	std_logic_vector((N-1) downto 0);
	data19:		in	std_logic_vector((N-1) downto 0);
	data20:		in	std_logic_vector((N-1) downto 0);
	data21:		in	std_logic_vector((N-1) downto 0);
	data22:		in	std_logic_vector((N-1) downto 0);
	data23:		in	std_logic_vector((N-1) downto 0);
	data24:		in	std_logic_vector((N-1) downto 0);
	data25:		in	std_logic_vector((N-1) downto 0);
	data26:		in	std_logic_vector((N-1) downto 0);
	data27:		in	std_logic_vector((N-1) downto 0);
	data28:		in	std_logic_vector((N-1) downto 0);
	data29:		in	std_logic_vector((N-1) downto 0);
	data30:		in	std_logic_vector((N-1) downto 0);
	data31:		in	std_logic_vector((N-1) downto 0);
	address:	in	std_logic_vector(4 downto 0);
	output:		out	std_logic_vector((N-1) downto 0)
);
end component;

component encoder2xN_oe is
generic(
	N:	positive
);
port(
	data0:		in	std_logic_vector((N-1) downto 0);
	data1:		in	std_logic_vector((N-1) downto 0);
	selector:	in	std_logic;
	enable:		in	std_logic;
	output:		out	std_logic_vector((N-1) downto 0)
);
end component;

signal	add_result:				std_logic_vector(7 downto 0);
signal	add_carry_in:			std_logic;
signal	add_flags:				std_logic_vector(7 downto 0);

signal	and_result:				std_logic_vector(7 downto 0);
signal	and_flags:				std_logic_vector(7 downto 0);

signal	or_result:				std_logic_vector(7 downto 0);
signal	or_flags:				std_logic_vector(7 downto 0);

signal	xor_result:				std_logic_vector(7 downto 0);
signal	xor_flags:				std_logic_vector(7 downto 0);

signal	cpl_result:				std_logic_vector(7 downto 0);
signal	cpl_flags:				std_logic_vector(7 downto 0);

signal	subtract_result:		std_logic_vector(7 downto 0);
signal	subtract_borrow_in:		std_logic;
signal	subtract_flags:			std_logic_vector(7 downto 0);

signal	rlc_result:				std_logic_vector(7 downto 0);
signal	rlc_flags:				std_logic_vector(7 downto 0);

signal	rrc_result:				std_logic_vector(7 downto 0);
signal	rrc_flags:				std_logic_vector(7 downto 0);

signal	rl_result:				std_logic_vector(7 downto 0);
signal	rl_flags:				std_logic_vector(7 downto 0);

signal	rr_result:				std_logic_vector(7 downto 0);
signal	rr_flags:				std_logic_vector(7 downto 0);

signal	daa_result:				std_logic_vector(7 downto 0);
signal	daa_flags:				std_logic_vector(7 downto 0);

signal	scf_flags:				std_logic_vector(7 downto 0);

signal	ccf_carry:				std_logic;
signal	ccf_flags:				std_logic_vector(7 downto 0);

signal	bit_zero:				std_logic;
signal	bit_flags:				std_logic_vector(7 downto 0);

signal	in_flags:				std_logic_vector(7 downto 0);	-- flags for IN r, C instruction

signal	secondary_out_enable:	std_logic;						-- '1' when executing a rrd/rld
																-- instruction

signal	rld_result:				std_logic_vector(7 downto 0);
signal	secondary_rld_result:	std_logic_vector(7 downto 0);
signal	rld_flags:				std_logic_vector(7 downto 0);
signal	is_rld:					std_logic;

signal	rrd_result:				std_logic_vector(7 downto 0);
signal	secondary_rrd_result:	std_logic_vector(7 downto 0);
signal	rrd_flags:				std_logic_vector(7 downto 0);
signal	is_rrd:					std_logic;

signal	sla_result:				std_logic_vector(7 downto 0);
signal	sla_flags:				std_logic_vector(7 downto 0);

signal	sra_result:				std_logic_vector(7 downto 0);
signal	sra_flags:				std_logic_vector(7 downto 0);

signal	sll_result:				std_logic_vector(7 downto 0);
signal	sll_flags:				std_logic_vector(7 downto 0);

signal	srl_result:				std_logic_vector(7 downto 0);
signal	srl_flags:				std_logic_vector(7 downto 0);

signal	bmtc_result:			std_logic_vector(7 downto 0);
signal	bmtc_flags:				std_logic_vector(7 downto 0);	-- block move termination criterion
																-- flags

begin
	-- result multiplexer, 32x8
	u1: encoder32xN
			generic map(
			N => 8
			)
			port map(
			data0 => add_result,								-- add, ignore carry bit
			data1 => add_result,								-- add, add carry bit
			data2 => subtract_result,							-- sub, ignore borrow bit
			data3 => subtract_result, 							-- sub, subtract borrow bit
			data4 => and_result,								-- and
			data5 => xor_result,								-- xor
			data6 => or_result,									-- or
			data7 => subtract_result,							-- compare (no-borrow sub with result
																-- discarded, used to set flags)
			data8 => rlc_result,								-- RLC
			data9 => rrc_result,								-- RRC
			data10 => rl_result,								-- RL
			data11 => rr_result,								-- RR
			data12 => daa_result,								-- DAA
			data13 => cpl_result,								-- CPL
			data14 => primary_operand,							-- SCF
			data15 => primary_operand,							-- CCF
			data16 => sla_result,								-- SLA
			data17 => sra_result,								-- SRA
			data18 => sll_result,								-- SLL
			data19 => srl_result,								-- SRL
			data20 => secondary_operand,						-- BIT
			data21 => and_result,								-- RES
			data22 => or_result,								-- SET
			data23 => primary_operand,							-- IN r, (C)
			data24 => rld_result,								-- RLD
			data25 => rrd_result,								-- RRD
			data26 => bmtc_result,								-- block move termination criterion
			data27 => (others => '0'),							-- reserved
			data28 => (others => '0'),							-- reserved
			data29 => (others => '0'),							-- reserved
			data30 => (others => '0'),							-- reserved
			data31 => (others => '0'),							-- reserved
			address => operation,
			output => output
		);

	-- result flags multiplexer
	u2: encoder32xN
			generic map(
			N => 8
			)
			port map(
			data0 => add_flags,								-- add
			data1 => add_flags,								-- adc
			data2 => subtract_flags,						-- sub
			data3 => subtract_flags,						-- sbc
			data4 => and_flags,								-- and
			data5 => xor_flags,								-- xor
			data6 => or_flags,								-- or
			data7 => subtract_flags,						-- cmp
			data8 => rlc_flags,								-- rlc
			data9 => rrc_flags,								-- rrc
			data10 => rl_flags,								-- rl
			data11 => rr_flags,								-- rr
			data12 => daa_flags,							-- daa
			data13 => cpl_flags,							-- cpl
			data14 => scf_flags,							-- scf
			data15 => ccf_flags,							-- ccf
			data16 => sla_flags,							-- SLA
			data17 => sra_flags,							-- SRA
			data18 => sll_flags,							-- SLL
			data19 => srl_flags,							-- SRL
			data20 => bit_flags,							-- BIT
			data21 => (others => '0'),						-- RES, no flags affected
			data22 => (others => '0'),						-- SET, no flags affected
			data23 => in_flags,								-- IN r, (C)
			data24 => rld_flags,							-- RLD
			data25 => rrd_flags,							-- RRD
			data26 => bmtc_flags,							-- block move termination criterion
			data27 => (others => '0'),						-- reserved
			data28 => (others => '0'),						-- reserved
			data29 => (others => '0'),						-- reserved
			data30 => (others => '0'),						-- reserved
			data31 => (others => '0'),						-- reserved
			address => operation,
			output => flags_out
		);

	scf_flags <= (carry_bit => '1', others => '0');

	-- adder: This version gets flagged by ModelSim on the carry_in line as an error.  Only signals or
	-- maybe variables are allowed. Expressions are not.
--	u3: adder8x2 port map(
--			addend => primary_operand,
--			augend => secondary_operand,
--			carry_in => (flags_in(carry_bit) and operation(0)),	-- carry only with adc opcode, others
--																-- made irrelevant by result mux
--			sum => add_result,
--			carry_out => carry_out,
--			overflow => add_overflow,
--			interdigit_carry => interdigit_carry,
--			zero => add_zero
--		);

	-- adder
	u3: adder8x2 port map(
			addend => primary_operand,
			augend => secondary_operand,
			carry_in => add_carry_in,
			sum => add_result,
			flags_out => add_flags
		);

	add_carry_in <= flags_in(carry_bit) and operation(0);	-- carry only with adc opcode, others made
															-- irrelevant by result mux

	-- subtractor
	u4: subtractor8x2 port map(
			minuend => primary_operand,
			subtrahend => secondary_operand,
			borrow_in => subtract_borrow_in,
			difference => subtract_result,
			flags_out => subtract_flags
		);

	-- borrow only with sbc opcode, must remove compare opcode (operation(2 downto 0) = "111"), others
	-- made irrelevant by result mux
	subtract_borrow_in <= flags_in(carry_bit) and (not operation(2)) and operation(1) and operation(0);

	-- bitwise and operation
	u5: and8bit port map(
			operand1 => primary_operand,
			operand2 => secondary_operand,
			output => and_result,
			flags_out => and_flags
		);

	-- bitwise exclusive-or operation
	u6: xor8bit port map(
			operand1 => primary_operand,
			operand2 => secondary_operand,
			output => xor_result,
			flags_out => xor_flags
		);

	-- bitwise or operation
	u7: or8bit port map(
			operand1 => primary_operand,
			operand2 => secondary_operand,
			output => or_result,
			flags_out => or_flags
		);

	-- RLC generator
	u8:	rlc8bit port map(
			operand => primary_operand,
			output => rlc_result,
			flags_out => rlc_flags
		);

	-- RRC generator
	u9:	rrc8bit port map(
			operand => primary_operand,
			output => rrc_result,
			flags_out => rrc_flags
		);

	-- RL generator
	u10: rl8bit port map(
			operand => primary_operand,
			carry_in => flags_in(carry_bit),
			output => rl_result,
			flags_out => rl_flags
		);

	-- RR generator
	u11: rr8bit port map(
			operand => primary_operand,
			carry_in => flags_in(carry_bit),
			output => rr_result,
			flags_out => rr_flags
		);

	-- DAA
	u12: daa port map(
			operand => primary_operand,
			flags_in => flags_in,
			output => daa_result,
			flags_out => daa_flags
		);

	-- bit testing of secondary operand against mask in primary operand
	u13: bit_op port map(
		 	operand1 => primary_operand,
			operand2 => secondary_operand,
			flags_out => bit_flags
		);

	u14: rld port map(
			primary_op => primary_operand,
			secondary_op => secondary_operand,
			result => rld_result,
			secondary_result => secondary_rld_result,
			flags_out => rld_flags
		);

	u15: magnitudeN
			generic map(
				N => 5
			)
			port map(
			a => operation,
			b => rrd_operation,
			equal => is_rrd,
			lt => open,
			gt => open
		);

	u16: magnitudeN
			generic map(
				N => 5
			)
			port map(
			a => operation,
			b => rld_operation,
			equal => is_rld,
			lt => open,
			gt => open
		);

	u17: rrd port map(
			primary_op => primary_operand,
			secondary_op => secondary_operand,
			result => rrd_result,
			secondary_result => secondary_rrd_result,
			flags_out => rrd_flags
		);

	u18: encoder2xN_oe
			generic map(
			N => 8
			)
			port map(
			data0 => secondary_rld_result,
			data1 => secondary_rrd_result,
			selector => is_rrd,
			enable => secondary_out_enable,
			output => secondary_out
		);

	secondary_out_enable <= is_rrd or is_rld;

	u19: cpl port map(
			operand => primary_operand,
			output => cpl_result,
			flags_out => cpl_flags
		);

	u20: ccf_operation port map(
			flags_in => flags_in,
			flags_out => ccf_flags
		);

	u21: in_rc_flags port map(
			operand => primary_operand,
			flags_out => in_flags
		);

	u22: sla8bit port map(
			operand => primary_operand,
			output => sla_result,
			flags_out => sla_flags
		);

	u23: sra8bit port map(
			operand => primary_operand,
			output => sra_result,
			flags_out => sra_flags
		);

	u24: sll8bit port map(
			operand => primary_operand,
			output => sll_result,
			flags_out => sll_flags
		);

	u25: srl8bit port map(
			operand => primary_operand,
			output => srl_result,
			flags_out => srl_flags
		);

	u26: bmtc port map(
			operand1 => primary_operand,
			operand2 => secondary_operand,
			output => bmtc_result,
			flags_out => bmtc_flags
		);
end;