📄 3_04alu
字号:
B_IN; // input operand B c0242
input [ 1:0] OP_IN; // input opcode c0243
c0244
reg [31:0] RESULT; // result c0245
wire [31:0] A_IN, // operand A c0246
B_IN; // operand B c0247
wire [ 1:0] OP_IN; // opcode c0248
c0249
always @(A_IN or B_IN or OP_IN) begin c0250
case(OP_IN) c0251
2'b00: RESULT = A_IN & B_IN; // AND c0252
2'b01: RESULT = A_IN | B_IN; // OR c0253
2'b10: RESULT = A_IN ^ B_IN; // XOR c0254
endcase c0255
end c0256
c0257
endmodule // logic c0258
c0259
c0260
//---------------------------------------------------------------------------- c0261
// c0262
// shift: execute shift operations c0263
// c0264
// Operations: c0265
// 2'b00: RESULT = A << B left shift c0266
// 2'b01: RESULT = A >> B right shift c0267
// 2'b10: RESULT = A ASR B right shift (sign maintaining) c0268
// 2'b11: RESULT = A ROT B right rotation c0269
// c0270
// Flags: c0271
// C_OUT: B#0: bit from A shifted one position crossing word border c0272
// B=0: 0 c0273
// c0274
//---------------------------------------------------------------------------- c0275
c0276
module shift ( c0277
RESULT, C_OUT, c0278
A_IN, B_IN, OP_IN c0279
); c0280
c0281
output [31:0] RESULT; // output result c0282
output C_OUT; // output carry flag c0283
c0284
input [31:0] A_IN; // input operand A c0285
input [ 4:0] B_IN; // input operand B c0286
input [ 1:0] OP_IN; // input opcode c0287
c0288
reg [31:0] RESULT; // result c0289
reg C_OUT; // carry flag c0290
wire [31:0] A_IN; // operand A c0291
wire [ 4:0] B_IN; // operand B c0292
wire [ 1:0] OP_IN; // opcode c0293
c0294
reg [31:0] ROTATED, // result barrel shifter c0295
AND_PATTERN, // pattern of filter mask c0296
SIGN_PATTERN, // pattern of sign mask c0297
AND_MASK, // bit filter mask for rotation c0298
SIGN_MASK; // sign mask for rotation c0299
reg [ 4:0] ROL_VAL; // rotation left value c0300
c0301
// c0302
// Left rotation value for barrel shifter rotating left only c0303
// left shift: B_IN c0304
// right shift and rotation: 32-B_IN (= -B_IN for 5 bit encoding) c0305
// c0306
always @(B_IN or OP_IN) c0307
ROL_VAL = (OP_IN) ? -B_IN : B_IN; c0308
c0309
// c0310
// Output of left rotating barrel shifter c0311
// c0312
always @(A_IN or ROL_VAL) c0313
ROTATED = (A_IN << ROL_VAL) | (A_IN >> -ROL_VAL); c0314
c0315
// c0316
// Carry bit for operation executed c0317
// left shift: bit 0 of rotation result c0318
// right shift and rotation: bit 31 of rotation result c0319
// no shift (B_IN is 0): 0 c0320
// c0321
always @(OP_IN or ROTATED or ROL_VAL) begin c0322
C_OUT = ((OP_IN) ? ROTATED[31] : ROTATED[0]) & (|ROL_VAL); c0323
end c0324
c0325
// c0326
// Pattern of filter mask for deletion of c0327
// rotated bits in left shifts and pattern of c0328
// sign mask for sign maintaining right shift c0329
// c0330
always @(A_IN or ROL_VAL) begin c0331
AND_PATTERN = {32{1'b1}} << ROL_VAL; c0332
SIGN_PATTERN = {32{A_IN[31]}} << ROL_VAL; c0333
end c0334
c0335
// c0336
// Set filter mask in rotation or 0-shifts to full transmission; c0337
// take from pattern directly for left shifts c0338
// and inverted for right shifts; c0339
// c0340
// take sign mask from pattern only for c0341
// sign maintaining right shift and c0342
// shift not equal 0, otherwise, erase c0343
// c0344
always @(AND_PATTERN or SIGN_PATTERN or OP_IN or ROL_VAL) begin c0345
AND_MASK = (AND_PATTERN ^ {32{|OP_IN}}) | {32{(&OP_IN) | ~(|ROL_VAL)}}; c0346
SIGN_MASK = SIGN_PATTERN & {32{OP_IN[1] & ~OP_IN[0] & (|ROL_VAL)}}; c0347
end c0348
c0349
// c0350
// Compute result of shifter from barrel shifter result c0351
// by combination with filter mask and sign mask c0352
// c0353
always @(ROTATED or AND_MASK or SIGN_MASK) begin c0354
RESULT = (ROTATED & AND_MASK) | SIGN_MASK; c0355
end c0356
c0357
endmodule // shift c0358
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -