📄 3_04alu
字号:
endcase c0121
end c0122
c0123
endmodule // alu c0124
c0125
c0126
//---------------------------------------------------------------------------- c0127
// c0128
// arithmetic: execution of arithmetic operations c0129
// c0130
// Operations: c0131
// 2'b00: RESULT = A_IN + B_IN c0132
// 2'b01: RESULT = A_IN + B_IN + C_IN c0133
// 2'b10: RESULT = A_IN - B_IN c0134
// 2'b11: RESULT = A_IN - B_IN - C_IN c0135
// c0136
// Flags: c0137
// C_OUT: 1: overflow c0138
// V_OUT: 1: carry c0139
// c0140
//---------------------------------------------------------------------------- c0141
c0142
module arithmetic ( c0143
RESULT, C_OUT, V_OUT, c0144
A_IN, B_IN, C_IN, OP_IN c0145
); c0146
c0147
output [31:0] RESULT; // output result c0148
output C_OUT, // output carry flag c0149
V_OUT; // output overflow flag c0150
c0151
input [31:0] A_IN, // input operand A c0152
B_IN; // input operand B c0153
input [ 1:0] OP_IN; // input opcode c0154
input C_IN; // input carry operand c0155
c0156
reg [31:0] RESULT; // result c0157
reg C_OUT, // carry flag c0158
V_OUT; // overflow flag c0159
wire [31:0] A_IN, // operand A c0160
B_IN; // operand B c0161
wire [ 1:0] OP_IN; // opcode c0162
wire C_IN; // carry operand c0163
c0164
reg [31:0] B_EFF; // operand B to be added effectively c0165
reg C_EFF, // carry operand to be added effectively c0166
C_31, // carry to bit 31 c0167
C_32; // carry from bit 31 c0168
c0169
// c0170
// Operand B to be added effectively c0171
// ADD, ADDC: B_IN c0172
// SUB, SUBC: -B_IN-1 (one's complement) c0173
// c0174
always @(B_IN or OP_IN) c0175
B_EFF = B_IN ^ {32{OP_IN[1]}}; c0176
c0177
// c0178
// Carry operand to be added effectively c0179
// ADD: 0 c0180
// ADDC: C_IN c0181
// SUB: 1 (=> adding two's complement of B_IN) c0182
// SUBC: ~C_IN (=> adding one's complement of B_IN, if carry) c0183
// c0184
always @(C_IN or OP_IN) c0185
C_EFF = (C_IN & OP_IN[0]) ^ OP_IN[1]; c0186
c0187
// c0188
// Add depending on A_IN, B_EFF, and C_EFF c0189
// c0190
always @(A_IN or B_EFF or C_EFF) c0191
RESULT = A_IN + B_EFF + C_EFF; c0192
c0193
// c0194
// Carry to bit 31 (2^31) c0195
// c0196
always @(RESULT or A_IN or B_EFF) c0197
C_31 = RESULT[31] ^ A_IN[31] ^ B_EFF[31]; c0198
c0199
// c0200
// Carry from bit 31 c0201
// c0202
always @(A_IN or B_EFF or C_31) c0203
C_32 = ((A_IN[31] & (B_EFF[31] | C_31)) | (B_EFF[31] & C_31)); c0204
c0205
// c0206
// Carry bit for operation executed c0207
// ADD, ADDC: C_32 c0208
// SUB, SUBC: ~C_32 (carry inverted due to complement addition) c0209
// c0210
always @(C_32 or OP_IN) c0211
C_OUT = C_32 ^ OP_IN[1]; c0212
c0213
// c0214
// Overflow bit c0215
// c0216
always @(C_31 or C_32) c0217
V_OUT = C_31 ^ C_32; c0218
c0219
endmodule // arithmetic c0220
c0221
c0222
//---------------------------------------------------------------------------- c0223
// c0224
// logic: execution of logic operations c0225
// c0226
// Operations: c0227
// 2'b00: RESULT = A AND B c0228
// 2'b01: RESULT = A OR B c0229
// 2'b10: RESULT = A XOR B c0230
// c0231
//---------------------------------------------------------------------------- c0232
c0233
module logic ( c0234
RESULT, c0235
A_IN, B_IN, OP_IN c0236
); c0237
c0238
output [31:0] RESULT; // output result c0239
c0240
input [31:0] A_IN, // input operand A c0241
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -