-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi8080_alu_util.vhd
165 lines (135 loc) · 4.38 KB
/
i8080_alu_util.vhd
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
-- Code your design here
library IEEE;
use IEEE.std_logic_1164.all;
use ieee.numeric_std.all;
package i8080_alu_util is
-- function "+"(left, right: std_logic_vector(7 downto 0)) return std_logic_vector;
-- function "+"(left: std_logic_vector(7 downto 0); right: std_logic)
-- return std_logic_vector;
function get_carry(lhs, rhs, res: std_logic_vector(7 downto 0))
return std_logic;
function get_carry(lhs, rhs: std_logic_vector(7 downto 0); res: signed(7 downto 0))
return std_logic;
function get_carry(lhs, rhs, res: signed(7 downto 0))
return std_logic;
function get_carry(lhs, rhs, res: unsigned(7 downto 0))
return std_logic;
function get_aux_carry(lhs, rhs, res: std_logic_vector(7 downto 0))
return std_logic;
function get_aux_carry(lhs, rhs: std_logic_vector(7 downto 0); res: signed(7 downto 0))
return std_logic;
function get_aux_carry(lhs, rhs, res: signed(7 downto 0))
return std_logic;
function get_aux_carry(lhs, rhs, res: unsigned(7 downto 0))
return std_logic;
function get_sign(a: std_logic_vector(7 downto 0)) return std_logic;
function get_zero(a: std_logic_vector(7 downto 0)) return std_logic;
function get_parity(a: std_logic_vector(7 downto 0)) return std_logic;
end package;
package body i8080_alu_util is
function get_sign(a: std_logic_vector(7 downto 0)) return std_logic is
begin
return a(7);
end function;
function get_zero(a: std_logic_vector(7 downto 0)) return std_logic is
begin
for i in a'range loop
if a(i) = '1' then
return '0';
end if;
end loop;
return '1';
end function;
function get_parity(a: std_logic_vector(7 downto 0)) return std_logic is
variable bitcount: natural range 0 to 8;
begin
for i in a'range loop
if a(i) = '1' then
bitcount := bitcount + 1;
end if;
end loop;
if bitcount mod 2 = 0 then
return '1';
else
return '0';
end if;
end function;
function "+"(left, right: std_logic_vector(7 downto 0)) return std_logic_vector is
variable ret: std_logic_vector(7 downto 0) := X"00";
begin
ret := std_logic_vector(unsigned(left) + unsigned(right));
return ret;
end function;
function "+"(left: std_logic_vector(7 downto 0); right: std_logic)
return std_logic_vector
is
variable right_slv: std_logic_vector(7 downto 0) := X"00";
variable ret: std_logic_vector(7 downto 0) := X"00";
begin
right_slv(7 downto 1) := (others => '0');
right_slv(0) := right;
ret := std_logic_vector(unsigned(left) + unsigned(right_slv));
return ret;
end function;
function get_carry_bitwise(lhs, rhs, res: std_logic) return std_logic is
variable lr: std_logic_vector(1 downto 0) := "00";
variable ret: std_logic := '0';
begin
lr := lhs & rhs;
case lr is
when "00" =>
ret := '0';
when "01"|"10" =>
if res = '1' then
ret := '0';
elsif ret = '0' then
ret := '1';
else
assert false report "get_carry(): ret is invalid value"
severity Failure;
end if;
when "11" =>
ret := '1';
when others =>
assert false report "get_carry: lhs/rhs is/are invalid value(s)"
severity Failure;
end case;
return ret;
end function;
function get_carry(lhs, rhs, res: std_logic_vector(7 downto 0)) return std_logic is
begin
return get_carry_bitwise(lhs(7), rhs(7), res(7));
end function;
function get_carry(lhs, rhs: std_logic_vector(7 downto 0); res: signed(7 downto 0))
return std_logic is
begin
return get_carry_bitwise(lhs(7), rhs(7), res(7));
end function;
function get_carry(lhs, rhs, res: signed(7 downto 0))
return std_logic is
begin
return get_carry_bitwise(lhs(7), rhs(7), res(7));
end function;
function get_carry(lhs, rhs, res: unsigned(7 downto 0))
return std_logic is
begin
return get_carry_bitwise(lhs(7), rhs(7), res(7));
end function;
function get_aux_carry(lhs, rhs, res: std_logic_vector(7 downto 0)) return std_logic is
begin
return get_carry_bitwise(lhs(3), rhs(3), res(3));
end function;
function get_aux_carry(lhs, rhs: std_logic_vector(7 downto 0);
res: signed(7 downto 0)) return std_logic is
begin
return get_carry_bitwise(lhs(3), rhs(3), res(3));
end function;
function get_aux_carry(lhs, rhs, res: signed(7 downto 0)) return std_logic is
begin
return get_carry_bitwise(lhs(3), rhs(3), res(3));
end function;
function get_aux_carry(lhs, rhs, res: unsigned(7 downto 0)) return std_logic is
begin
return get_carry_bitwise(lhs(3), rhs(3), res(3));
end function;
end package body;