-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalu32.h
284 lines (268 loc) · 11.2 KB
/
alu32.h
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
/*
* alu32.h -- Unidad Aritm'etico-l'ogica de 32 bits.
* Ultima modificaci'on: 14-11-00.
* gse.
*/
/*
* Sumador de 1 bit.
*
* carry_in
* | carry_in a b |sum carry_out
* | -------------+-------------
* | +---+ 0 0 0 | 0 0
* a ----+-----|--|AND|____t0 0 0 1 | 1 0
* | +--|a0 | | 0 1 0 | 1 0
* | | +---+ | +---+ 0 1 1 | 0 1
* | | +---+ t1 +--|OR | 1 0 0 | 1 0
* b ----|--+--|--|AND|__-----| o |---+ 1 0 1 | 0 1
* | | +--|a1 | +--| | | 1 1 0 | 0 1
* | | | +---+ | +---+ | 1 1 1 | 1 1
* | | | +---+ | |
* | +--|--|AND|____| |
* +--|--|--|a2 | t2 |
* | | | +---+ |
* | | | +---+ |
* | +--|--|XOR|____ |
* +-----|--|x0 | t3 | +---+ |
* | +---+ +-|XOR|____|____ sum
* +-------------|x1 | |
* +---+ |
* carry_out
*/
class ADDER_BIT {
WIRE t[4];
AND a0,a1,a2;
XOR x0,x1;
OR o;
public:
void run(WIRE &a, WIRE &b, // Entradas de datos
WIRE &carry_in, // Acarreo de entrada
WIRE &sum, // Resultado de la suma
WIRE &carry_out); // Acarreo de salida
};
/*
* ALU de 1 bit.
*
* binvert carry_in op
* | | |
* less ---|-----------------------|--+ |
* | +---+ | | /2
* a ---|-------------+--|AND|__|__|__ |
* | +--|--|and| | | | |
* | | | +---+ | | | +-+-
* | | | +---+ | | +--|i0 \
* | | +--|OR |__|__|_____|i1 \
* | +--|--| or| | | | mux +- res
* | | | +---+ | | +--|i2 /
* | | | +----+ +--|--|i3 /
* | | | | | +---
* | | | +---+ |
* | +---+ | +--| + |________| less=i3
* +--|XOR|___+-----|sum|
* b ------|xor| t +---+
* +---+ |
* carry_out
* binvert op | res
* -----------+------
* 0 00 | a and b
* 0 01 | a or b
* 0 10 | a add b
* 0 11 | less
* 1 00 | a and (not b)
* 1 01 | a or (not b)
* 1 10 | a sub b (si carry_in=1)
* 1 11 | less
*/
class ALU_BIT {
WIRE t, // Salida de la puerta XOR
i[4]; // Entradas del multiplexor
XOR x;
OR o;
AND y;
ADDER_BIT sum;
MUX_4_1 mux;
public:
void run(WIRE &a, WIRE &b, // Bits de datos
WIRE &binvert, // Complemento de la l'inea "b"
WIRE &carry_in, // Acarreo de entrada
WIRE op[2], // Operaci'on de la ALU
WIRE &less, // Entrada directa a la salida
WIRE &res, // Resultado de la ALU
WIRE &carry_out); // Acarreo de salida
};
/*
* ALU-MSb de 1 bit.
*
* binvert carry_in op
* | | |
* less ---|-----------------------|--+ |
* | +---+ | | /2
* a ---|-------------+--|AND|__|__|__ |
* | +--|--|and| | | | |
* | | | +---+ | | | +-+-
* | | | +---+ | | +--|i0 \
* | | +--|OR |__|__|_____|i1 \
* | +--|--| or| | | | mux +-------- res
* | | | +---+ | | +--|i2 /
* | | | +----+ +--|--|i3 /
* | | | | | | +---
* | | | +---+ | | +---+
* | +---+ | +--| + |__|_____+--------|XOR|_____ set
* +--|XOR|___+-----|sum| | +--| x1|
* b ------|xor| t +---+ | +---+ | +---+
* +---+ | +----|XOR|__|____________ overflow
* +---------| x0|
* | +---+
* carry_out
*/
class ALU_MSBIT {
WIRE t, // Salida de la puerta XOR
i[4]; // Entradas del multiplexor
XOR x;
OR o;
AND y;
ADDER_BIT sum;
MUX_4_1 mux;
XOR x0,x1;
public:
void run(WIRE &a, WIRE &b,
WIRE &binvert,
WIRE &carry_in,
WIRE op[2],
WIRE &less,
WIRE &res,
WIRE &carry_out,
WIRE &set,
WIRE &overflow);
};
/*
* ALU de 32 bits ripple-carry.
*
* bnegate op[2]
* | |
* +------+--------------+ +--------+
* | | | | |
* | +----+--------------+-----------+------+ |
* | | binvert carry_in op | |
* | | | |
* +---+ less | |
* | | | ALU[0] | |
* a[0]-----+ a res +----+------ res[0]
* | | | | | |
* b[0]-----+ b | | |
* | | | carry_out | | |
* | | +-------------------+------------------+ | |
* | | | | |
* | +------+ | c[0] +--------+ |
* | | | | | | |
* | | +----+--------------+-----------+------+ | |
* | | | binvert carry_in op | | |
* | | | | | |
* 0 -----+ less | | |
* | | | ALU[1] | | |
* a[1]-----+ a res +-----+----- res[1]
* | | | | | ||
* b[1]-----+ b | | ||
* | | | carry_out | | ||
* | | +-------------------+------------------+ | ||
* : : : c[1] : ::
* | +------+ | +--------+ ||
* | | | c[30] | ||
* | +----+--------------+-----------+------+ ||
* | | binvert carry_in op | ||
* | | | ||
* 0 -----+ less set +-+ ||
* | | ALU[31] | | ||
* a[31]-----+ a res +--------+-- res[31]
* | | | | ||..|
* b[31]-----+ b overflow +----------- overflow
* | | carry_out | | ||..|
* | +-------------------+------------------+ | ||..|
* |less | | ||..|
* +--------------------------------------------+ ||..|
* | ||..|
* carry_out +-++..+-+
* | NOR |
* | zero |
* +---+---+
* |
* zero
* bnegate oper | output
* -------------+----------
* * 0 00 | A and B
* * 0 01 | A or B
* * 0 10 | A add B
* 0 11 | 000...0A donde A = overflow XOR sum[MSb]
* 1 00 | A and (not B)
* 1 01 | A or (not B)
* * 1 10 | A sub B = A add ((not B) + 1)
* * 1 11 | (A<B)
*
* * -> Operaciones normalmente 'utiles
*/
class ALU_32BIT_RC {
WIRE c[31]; // Bits de acarreo entre ALU's de 1 bit
WIRE less; // Bit de realimentaci'on del MSb al LSb
ALU_BIT alu[31]; // ALU's menos significativas
ALU_MSBIT alu31; // La ALU m'as significativa
NOR no; // C'alculo del zero
public:
void run(WIRE a[32], WIRE b[32], // Buses de datos de entrada
WIRE &bnegate, WIRE op[2], // Entradas de control de operaci'on
WIRE res[32], // Bus de datos de salida
WIRE &overflow, // Overflow en la operaci'on
WIRE &carry_out, // Acarreo de salida de la ALU
WIRE &zero); // El resultado es cero
};
/*******************************************************************
* Jose Domingo Alvarez Hervas.
* Laboratorio de Arquitectura: 1999-2000.
* ALU de 32 bits con Carry-Look-Ahead.
*/
class ADDER_BIT_CLA {
WIRE t;
XOR x0,x1;
public:
void run(WIRE &a, WIRE &b, // Entradas de datos
WIRE &carry_in, // Acarreo de entrada
WIRE &sum); // Resultado de la suma
};
class ALU_BIT_CLA {
WIRE t, // Salida de la puerta XOR
i[4]; // Entradas del multiplexor
XOR x;
OR o;
AND y;
ADDER_BIT_CLA sum;
MUX_4_1 mux;
public:
void run(WIRE &a, WIRE &b, // Bits de datos
WIRE &binvert, // Complemento de la l'inea "b"
WIRE &carry_in, // Acarreo de entrada
WIRE op[2], // Operaci'on de la ALU
WIRE &less, // Entrada directa a la salida
WIRE &res); // Resultado de la ALU
};
class ALU_32BIT_CLA {
XOR xoraux[31]; // Con esto se si utilizo b o b negada para calcular los gi y pi
WIRE salida[31]; // Salida de las puestas anteriores
AND andg[31]; // Para calcular los gi
WIRE g[31]; // Las lineas que llevan los gi
OR orp[31]; // Para calcular los pi
WIRE p[31]; // Las lineas que llevan los pi
AND andaux[496]; // Puertas AND auxiliares para calcular los carry
WIRE aux[496]; // Salidas de las puertas AND
OR oraux[31]; // Puertas OR auxiliares para calcular los carry
WIRE c[31]; // Lineas de salida de las puertas OR anteriores
WIRE less; // Bit de realimentaci'on del MSb al LSb
ALU_BIT_CLA alu[31]; // ALU's menos significativas
ALU_MSBIT alu31; // La ALU m'as significativa
NOR no; // C'alculo del zero
public:
void run(WIRE a[32], WIRE b[32], // Buses de datos de entrada
WIRE &bnegate, WIRE op[2], // Entradas de control de operaci'on
WIRE res[32], // Bus de datos de salida
WIRE &overflow, // Overflow en la operaci'on
WIRE &carry_out, // Acarreo de salida de la ALU
WIRE &zero); // El resultado es cero
};