19
ĐI HC ĐÀ NNG TRƯNG ĐI HC BÁCH KHOA KHOA ĐIN T- VIN THÔNG -------------- ------------- B B Á Á O O C C Á Á O O L L A A B B C C U U T T R R Ú Ú C C M M Á Á Y Y T T Í Í N N H H LAB 2: MIPS 32-bit ALU SVTH : TRN VĂN DŨNG TRNH NGC ĐT HTHPHƯC MNHÓM : 14A LP : 08DT1 Đà Nng, tháng 11 năm 2011.

thiết kế ALU-32bit

  • Upload
    nam-hai

  • View
    712

  • Download
    39

Embed Size (px)

DESCRIPTION

thiết kế bộ ALU có thể thực hiện các phép toán cộng, trừ, xor, slt với thanh ghi 32 bit

Citation preview

Page 1: thiết kế ALU-32bit

ĐĐẠẠII HHỌỌCC ĐĐÀÀ NNẴẴNNGG

TTRRƯƯỜỜNNGG ĐĐẠẠII HHỌỌCC BBÁÁCCHH KKHHOOAA

KKHHOOAA ĐĐIIỆỆNN TTỬỬ -- VVIIỄỄNN TTHHÔÔNNGG

------------------------------------------------------

BBÁÁOO CCÁÁOO LLAABB

CCẤẤUU TTRRÚÚCC MMÁÁYY TTÍÍNNHH

LLAABB 22:: MMIIPPSS 3322--bbiitt AALLUU

SSVVTTHH :: TTRRẦẦNN VVĂĂNN DDŨŨNNGG

TTRRỊỊNNHH NNGGỌỌCC ĐĐẠẠTT

HHỒỒ TTHHỊỊ PPHHƯƯỚỚCC MMỸỸ

NNHHÓÓMM :: 1144AA

LLỚỚPP :: 0088DDTT11

ĐĐàà NNẵẵnngg,, tthháánngg 1111 nnăămm 22001111..

Page 2: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 11

Contents

11.. YYêêuu ccầầuu tthhiiếếtt kkếế:: .......................................................................................................................................................................................................................... 22

22.. GGiiớớii tthhiiệệuu MMIIPPSS AALLUU:: ........................................................................................................................................................................................................ 22

33.. TThhiiếếtt kkếế:: ............................................................................................................................................................................................................................................................ 33

33..11.. TThhiiếếtt kkếế bbộộ ccộộnngg 3322 bbiitt .............................................................................................................................................................................................. 33

33..11..11.. BBộộ ccộộnngg 11 bbiitt ........................................................................................................................................................................................................................ 33

33..11..22.. BBộộ ccộộnngg 3322 bbiitt .................................................................................................................................................................................................................... 44

33..22.. TThhiiếếtt kkếế bbộộ ttrrừừ 3322 bbiitt:: .................................................................................................................................................................................................. 55

33..22..11.. BBộộ ttrrừừ 11 bbiitt:: ............................................................................................................................................................................................................................ 55

33..22..22.. BBộộ ttrrừừ 3322 bbiitt .......................................................................................................................................................................................................................... 66

33..33.. TThhiiếếtt kkếế bbộộ XXOORR 3322 bbiitt:: .............................................................................................................................................................................................. 77

33..44.. TThhiiếếtt kkếế SSLLTT:: ...................................................................................................................................................................................................................................... 88

33..55.. XXéétt ccáácc ccờờ:: .................................................................................................................................................................................................................................................. 99

33..55..11.. CCờờ zzeerroo:: ............................................................................................................................................................................................................................................ 99

33..55..22.. CCờờ nneeggaattiivvee:: .......................................................................................................................................................................................................................... 99

33..55..33.. CCờờ ccaarrrryyoouutt:: .......................................................................................................................................................................................................................... 99

33..55..44.. CCờờ oovveerrffllooww:: .................................................................................................................................................................................................................... 1100

33..66.. XXáácc đđịịnnhh nnggõõ rraa:: .................................................................................................................................................................................................................... 1111

33..77.. AALLUU 3322 bbiitt ............................................................................................................................................................................................................................................ 1133

44.. KKiiểểmm ttrraa bbằằnngg cchhưươơnngg ttrrììnnhh TTeessttbbeenncchh .................................................................................................................. 1166

Page 3: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 22

LAB 2 : MIPS 32 BIT ALU 1. Yêu cầu thiết kế:

- Thiết kế 1 bộ MIPS ALU 32 bit đơn giản.

- C|c phép to|n yêu cầu: ADD, SUB, XOR, SLT

- Yêu cầu chung:

+ Dùng cấu trúc lệnh structural

+ C|c cổng logic không được qu| 4 ngõ vào

+ Delay 50ps

2. Giới thiệu MIPS ALU:

- Sơ đồ khối

- Cấu tạo MIPS ALU:

+ 2 ngõ vào BusA và BusB 32 bit

+ 1 ngõ ra Output 32 bit

+ C|c cờ ngõ ra: zero, overflow, carryout, negative

Cờ zero: được set khi kết quả bằng 0

Cờ overflow (tr{n khi thực hiện cộng trừ với số có dấu): đươc set khi xảy ra

tràn

Cờ carryout (tr{n khi thực hiện cộng trừ với số không dấu): xảy ra khi có

nhớ (mượn) từ MSB

Page 4: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 33

Cờ negative: được set nếu kết quả }m

+ Ngõ v{o điều khiển ALUcontrol gồm 2 bit x|c định phép to|n m{ ALU cần thực

hiện:

3. Thiết kế:

- Hướng thiết kế:

+ 2 đầu v{o A,B 32 bit, đầu ra cũng l{ 32 bit

=> Chia ra th{nh 32 khối nhỏ, mỗi khối xử lí 2 bit dữ liệu v{o (lấy từ A,B) để đưa

ra 1 bit của ngõ ra

3.1. Thiết kế bộ cộng 32 bit

3.1.1. Bộ cộng 1 bit

- Để thiết kế bộ cộng 32 bit, ta thiết kế bộ cộng 1 bit

- Sơ đồ khối bộ cộng to{n phần:

- Thiết lập bảng gi| trị:

IInnppuutt OOuuttppuutt

aa bb ccAAddddIInn SSuumm ccAAddddOOuutt

00 00 00 00 00

00 00 11 11 00

00 11 00 11 00

00 11 11 00 11

11 00 00 11 00

11 00 11 00 11

11 11 00 00 11

11 11 11 11 11

Page 5: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 44

- Tối thiểu hóa bằng bảng Karnaugh:

- Phương trình trạng th|i:

sum= a xor b xor cAddIn

cAddOut = ab + cAddIn(a+b)

- Code verilog:

/**************** Add for 1 bit ****************/

module add1bit(sum,cAddOut,A,B,cAddIn);

output sum,cAddOut;

input A,B,cAddIn;

wire tem1,tem2,tem3;

and #50 and1(tem1,A,B);

or #50 or1(tem2,A,B);

and #50 and2(tem3,tem2,cAddIn);

or #50 or2(cAddOut,tem3,tem1);

xor #50 xor1(sum,A,B,cAddIn);

endmodule

3.1.2. Bộ cộng 32 bit

- Gọi 32 lần bộ cộng 1 bit theo thứ tự từ 0 đến 31 ta được bộ cộng 32 bit:

- Code Verilog: /************** Adder 32 bit *****************/

module add32bit(sum,cAddOut,A,B,overflow);

output [31:0]sum;

output cAddOut,overflow;

input [31:0]A,B;

wire [30:0]Co;

add1bit add0(sum[0],Co[0],A[0],B[0],1'b0);

add1bit add1(sum[1],Co[1],A[1],B[1],Co[0]);

add1bit add2(sum[2],Co[2],A[2],B[2],Co[1]);

add1bit add3(sum[3],Co[3],A[3],B[3],Co[2]);

add1bit add4(sum[4],Co[4],A[4],B[4],Co[3]);

add1bit add5(sum[5],Co[5],A[5],B[5],Co[4]);

add1bit add6(sum[6],Co[6],A[6],B[6],Co[5]);

add1bit add7(sum[7],Co[7],A[7],B[7],Co[6]);

add1bit add8(sum[8],Co[8],A[8],B[8],Co[7]);

add1bit add9(sum[9],Co[9],A[9],B[9],Co[8]);

Page 6: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 55

add1bit add10(sum[10],Co[10],A[10],B[10],Co[9]);

add1bit add11(sum[11],Co[11],A[11],B[11],Co[10]);

add1bit add12(sum[12],Co[12],A[12],B[12],Co[11]);

add1bit add13(sum[13],Co[13],A[13],B[13],Co[12]);

add1bit add14(sum[14],Co[14],A[14],B[14],Co[13]);

add1bit add15(sum[15],Co[15],A[15],B[15],Co[14]);

add1bit add16(sum[16],Co[16],A[16],B[16],Co[15]);

add1bit add17(sum[17],Co[17],A[17],B[17],Co[16]);

add1bit add18(sum[18],Co[18],A[18],B[18],Co[17]);

add1bit add19(sum[19],Co[19],A[19],B[19],Co[18]);

add1bit add20(sum[20],Co[20],A[20],B[20],Co[19]);

add1bit add21(sum[21],Co[21],A[21],B[21],Co[20]);

add1bit add22(sum[22],Co[22],A[22],B[22],Co[21]);

add1bit add23(sum[23],Co[23],A[23],B[23],Co[22]);

add1bit add24(sum[24],Co[24],A[24],B[24],Co[23]);

add1bit add25(sum[25],Co[25],A[25],B[25],Co[24]);

add1bit add26(sum[26],Co[26],A[26],B[26],Co[25]);

add1bit add27(sum[27],Co[27],A[27],B[27],Co[26]);

add1bit add28(sum[28],Co[28],A[28],B[28],Co[27]);

add1bit add29(sum[29],Co[29],A[29],B[29],Co[28]);

add1bit add30(sum[30],Co[30],A[30],B[30],Co[29]);

add1bit add31(sum[31],cAddOut,A[31],B[31],Co[30]);

xor (overflow,cAddOut,Co[30]);

endmodule

3.2. Thiết kế bộ trừ 32 bit:

3.2.1. Bộ trừ 1 bit:

- Tương tự bộ cộng, thiết kế bộ trừ 1 bit, ta có bảng gi| trị:

IInnppuutt OOuuttppuutt

aa bb ccSSuubbIInn SSuubb ccSSuubbOOuutt

00 00 00 00 00

00 00 11 11 11

00 11 00 11 11

00 11 11 00 11

11 00 00 11 00

11 00 11 00 00

11 11 00 00 00

11 11 11 11 11

- Tương tự, ta cũng có phương trình trạng th|i:

Sub= a xor b xor cSubIn

cSubOut = nota.b + cSubIn(nota + b)

- Code Verilog: /*************** Sub for 1 bit ****************/

module sub1bit(sub,cSubOut,A,B,cSubIn);

Page 7: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 66

output sub,cSubOut;

input A,B,cSubIn;

wire tem1,tem2,tem3;

and #50 and1(tem1,(~A),B);

or #50 or1(tem2,(~A),B);

and #50 and2(tem3,tem2,cSubIn);

or #50 or2(cSubOut,tem3,tem1);

xor #50 xor1(sub,A,B,cSubIn);

endmodule

3.2.2. Bộ trừ 32 bit

- Gọi 32 lần bộ trừ 1 bit theo thứ tự bit 0 đến bit 31, ta được bộ trừ 32 bit: /********************Subtractor for 32 bit******************/

module sub32bit(sub,cSubOut,A,B,overflow);

output [31:0]sub;

output cSubOut,overflow;

input [31:0]A,B;

wire [30:0]Co;

sub1bit sub0(sub[0],Co[0],A[0],B[0],1'b0);

sub1bit sub1(sub[1],Co[1],A[1],B[1],Co[0]);

sub1bit sub2(sub[2],Co[2],A[2],B[2],Co[1]);

sub1bit sub3(sub[3],Co[3],A[3],B[3],Co[2]);

sub1bit sub4(sub[4],Co[4],A[4],B[4],Co[3]);

sub1bit sub5(sub[5],Co[5],A[5],B[5],Co[4]);

sub1bit sub6(sub[6],Co[6],A[6],B[6],Co[5]);

sub1bit sub7(sub[7],Co[7],A[7],B[7],Co[6]);

sub1bit sub8(sub[8],Co[8],A[8],B[8],Co[7]);

sub1bit sub9(sub[9],Co[9],A[9],B[9],Co[8]);

sub1bit sub10(sub[10],Co[10],A[10],B[10],Co[9]);

sub1bit sub11(sub[11],Co[11],A[11],B[11],Co[10]);

sub1bit sub12(sub[12],Co[12],A[12],B[12],Co[11]);

sub1bit sub13(sub[13],Co[13],A[13],B[13],Co[12]);

sub1bit sub14(sub[14],Co[14],A[14],B[14],Co[13]);

sub1bit sub15(sub[15],Co[15],A[15],B[15],Co[14]);

sub1bit sub16(sub[16],Co[16],A[16],B[16],Co[15]);

sub1bit sub17(sub[17],Co[17],A[17],B[17],Co[16]);

sub1bit sub18(sub[18],Co[18],A[18],B[18],Co[17]);

sub1bit sub19(sub[19],Co[19],A[19],B[19],Co[18]);

sub1bit sub20(sub[20],Co[20],A[20],B[20],Co[19]);

sub1bit sub21(sub[21],Co[21],A[21],B[21],Co[20]);

sub1bit sub22(sub[22],Co[22],A[22],B[22],Co[21]);

sub1bit sub23(sub[23],Co[23],A[23],B[23],Co[22]);

sub1bit sub24(sub[24],Co[24],A[24],B[24],Co[23]);

sub1bit sub25(sub[25],Co[25],A[25],B[25],Co[24]);

sub1bit sub26(sub[26],Co[26],A[26],B[26],Co[25]);

sub1bit sub27(sub[27],Co[27],A[27],B[27],Co[26]);

Page 8: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 77

sub1bit sub28(sub[28],Co[28],A[28],B[28],Co[27]);

sub1bit sub29(sub[29],Co[29],A[29],B[29],Co[28]);

sub1bit sub30(sub[30],Co[30],A[30],B[30],Co[29]);

sub1bit sub31(sub[31],cSubOut,A[31],B[31],Co[30]);

xor (overflow,cSubOut,Co[30]);

endmodule

3.3. Thiết kế bộ XOR 32 bit:

- Để thiết kế bộ xor 32 ta thiết kế từ 32 bộ xor 1 bit

- Bảng trạng th|i:

- Code Verilog:

/****************** XOR 32 bit *****************/

module xor32bit(xor32,A,B);

output [31:0]xor32;

input [31:0]A,B;

xor #50 xor0(xor32[0],A[0],B[0]);

xor #50 xor1(xor32[1],A[1],B[1]);

xor #50 xor2(xor32[2],A[2],B[2]);

xor #50 xor3(xor32[3],A[3],B[3]);

xor #50 xor4(xor32[4],A[4],B[4]);

xor #50 xor5(xor32[5],A[5],B[5]);

xor #50 xor6(xor32[6],A[6],B[6]);

xor #50 xor7(xor32[7],A[7],B[7]);

xor #50 xor8(xor32[8],A[8],B[8]);

xor #50 xor9(xor32[9],A[9],B[9]);

xor #50 xor10(xor32[10],A[10],B[10]);

xor #50 xor11(xor32[11],A[11],B[11]);

xor #50 xor12(xor32[12],A[12],B[12]);

xor #50 xor13(xor32[13],A[13],B[13]);

xor #50 xor14(xor32[14],A[14],B[14]);

xor #50 xor15(xor32[15],A[15],B[15]);

xor #50 xor16(xor32[16],A[16],B[16]);

xor #50 xor17(xor32[17],A[17],B[17]);

xor #50 xor18(xor32[18],A[18],B[18]);

xor #50 xor19(xor32[19],A[19],B[19]);

xor #50 xor20(xor32[20],A[20],B[20]);

xor #50 xor21(xor32[21],A[21],B[21]);

xor #50 xor22(xor32[22],A[22],B[22]);

aa bb aa xxoorr bb

00 00 00

00 11 11

11 00 11

11 11 00

Page 9: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 88

xor #50 xor23(xor32[23],A[23],B[23]);

xor #50 xor24(xor32[24],A[24],B[24]);

xor #50 xor25(xor32[25],A[25],B[25]);

xor #50 xor26(xor32[26],A[26],B[26]);

xor #50 xor27(xor32[27],A[27],B[27]);

xor #50 xor28(xor32[28],A[28],B[28]);

xor #50 xor29(xor32[29],A[29],B[29]);

xor #50 xor30(xor32[30],A[30],B[30]);

xor #50 xor31(xor32[31],A[31],B[31]);

endmodule

3.4. Thiết kế SLT:

- Cho kết quả bằng 1 nếu A < B

- Quan t}m tới bit LSB, c|c bit còn lại = 0

- A < B A – B < 0

=> LSB bằng bit dấu thực sự của kết quả phép trừ. Tuy nhiên bit dấu thực sự

không phải l{ MSB (most signification bit) của Output, do có thể xảy ra tr{n số

(Overflow)

slt[31:1] = 31’b0

slt[0] = sub[31] xor overflow

- Code verilog:

/********************SLT 32 bit******************/

//if A<B set = 1

module SLT32bit(slt,A,B);

output [31:0]slt;

input [31:0]A,B;

wire overflow;

wire[31:0]sub;

wire cOut;

Page 10: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 99

sub32bit sub32(sub,cOut,A,B,overflow);

xor #50 xor1(slt[0],sub[31],overflow);

assign slt[31:1] = 31'b0;

endmodule

3.5. Xét các cờ:

3.5.1. Cờ zero:

- Khi kết quả bằng 0 thì cờ zero được set lên 1

- Dùng lệnh nor tất cả c|c bit từ 0 đến 31 của kết quả để kiểm tra cờ Zero.

- Code verilog: //Determine zero flag

or #50 or01(term01, Out[0], Out[1], Out[2], Out[3]);

or #50 or02(term02, Out[4], Out[5], Out[6], Out[7]);

or #50 or03(term03, Out[8], Out[9], Out[10], Out[11]);

or #50 or04(term04, Out[12], Out[13], Out[14], Out[15]);

or #50 or05(term05, Out[16], Out[17], Out[18], Out[19]);

or #50 or06(term06, Out[20], Out[21], Out[22], Out[23]);

or #50 or07(term07, Out[24], Out[25], Out[26], Out[27]);

or #50 or08(term08, Out[28], Out[29], Out[30], Out[31]);

or #50 or11(term11, term01, term02, term03, term04);

or #50 or12(term12, term05, term06, term07, term08);

nor #50 nor0(zero, term11, term12);

3.5.2. Cờ negative:

- Cờ Negative: g|n cờ Negative bằng bit thứ 31 của kết quả.

+ Bit thứ 31 = 1 (kết quả }m): cờ được set

+ Bit thứ 31 = 0 (kết quả dương): cờ không được set

- Code Verilog: // Determine negative flag

assign negative = Out[31];

3.5.3. Cờ carryout:

- Chỉ xét đối với phép cộng trừ.

- Cờ carry chính l{ cAddOut hoặc cSubOut của phép cộng hoặc phép trừ.

- Code verilog:

//Determine carry flag, it just turns on when ALU does subtract

or add

wire [1:0]f;

and #50 and3(f[0],(~ALUcontrol[1]),carryout1);

and #50 and4(f[1],ALUcontrol[1],carryout2);

Page 11: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1100

or #50 or1(carry,f[0],f[1]);

and #50 and5(CarryOut, carry, (~ALUcontrol[0]));

endmodule

3.5.4. Cờ overflow:

- Overflow (tr{n có dấu –xảy ra khi kết quả phép to|n vượt qu| dải giới hạn tính

toán).

- A, B khác dấu thì không xảy ra Overflow

=> Overflow chỉ xảy ra khi cộng 2 số cùng dấu

- Nhận biết:

- Đối với phép cộng: 2 số hạng của phép cộng cùng dấu nhưng kết quả kh|c dấu

với 2 số hạng đó.

+ Cộng 2 số dương:

Không xảy ra overflow: bit 31 bằng 0 (số dương), carryin của bit 31 bằng 0

Xảy ra overflow: bit 31 bằng 1 (số }m), kết quả sai dấu, carry in của bit 31

bằng 1

Carry out của bit 31 luôn bằng 0

+ Cộng 2 số }m:

Không xảy ra overflow: bit 31 bằng 1 (số }m), carryin của bit 31 bằng 1

Xảy ra overflow: bit 31 bằng 0 (số dương), kết quả sai dấu, carryin của bit 31

phải bằng 0

Carryout của bit 31 luôn bằng 1

+ Cộng hai số tr|i dấu không thể vượt qu| giới hạn phép tính nên không xảy ra

coverflow

- Đối với phép trừ: số bị trừ l{ số dương v{ số trừ l{ số }m, kết quả l{ số }m hoặc

số bị trừ l{ số }m v{ số trừ l{ số dương, kết quả l{ số dương.

+ Số bị trừ l{ số dương v{ số trừ l{ số }m:

Không xảy ra overflow: bit 31 bằng 0 (số dương), carryin của bit 31 bằng 1

Xảy ra overflow: bit 31 bằng 1 (số }m), kết quả sai dấu, carryin của bit 31

bằng 0

Page 12: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1111

Carryout của bit 31 luôn bằng 1

+ Số bị trừ l{ số }m, số trừ l{ số dương:

Không xảy ra overflow: bit 31 bằng 1 (số }m), carryin của bit 31 bằng 0

Xảy ra overflow: bit 31 bằng 0 (số dương), kết quả sai dấu, carryin của bit 31

phải bằng 1

Carryout của bit 31 luôn bằng 0

+ Trường hợp số bị trừ v{ số trừ cùng dấu không thể vượt qu| giới hạn phép tính

nên không xảy ra overflow

- Vì vậy, ta có thể ph|t hiện overflow bằng c|ch so s|nh carryin v{ carryout của

bit thứ 31, nếu tr|i dấu thì xảy ra overflow.

- Xét bảng sau:

- Vậy:

Carryin Carryout Overflow

0 0 0

0 1 1

1 0 1

1 1 0

- Overflow = CAddOut[31] xor CAddOut[30] (phép cộng)

Overflow = CSubOut[31] xor CSubOut[30] (phép trừ)

- Code Verilog: // Determine overflow, it just turns on when ALU does subtract or

add

wire [1:0]a;

and #50 and0(a[0],(~ALUcontrol[1]),ovflow1);

and #50 and1(a[1],ALUcontrol[1],ovflow2);

or #50 or0(ovflow,a[0],a[1]);

and #50 and2(overflow, ovflow, (~ALUcontrol[0]))

3.6. Xác định ngõ ra:

Page 13: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1122

- Sau khi có được 4 kết quả của ADD,SUB,XOR,SLT căn cứ v{o 2bit điều khiển của

ALUControl để x|c định dữ liệu n{o được đưa ra port ngõ ra.

- Sử dụng bộ mux 4x32 to 32 được x}y dựng từ 32 bộ mux 4 to 1

- Code verilog: /***************mux4to1**********************/

module mux4to1(f, f1, f2, f3, f4, sel);

output f;

input f1, f2, f3, f4;

input [1:0]sel;

wire [3:0]f0;

and #50 and0(f0[0],f1,(~sel[1]),(~sel[0]));

and #50 and1(f0[1],f2,(~sel[1]),sel[0]);

and #50 and2(f0[2],f3,sel[1],(~sel[0]));

and #50 and3(f0[3],f4,sel[1],sel[0]);

or #50 or0(f,f0[0],f0[1],f0[2],f0[3]);

endmodule

/*****************mux4to1 for 32 bit********************/

//f1 = Add (00) , f2 = Xor (01), f3 = Sub (10), f4 = SLT(11)

module mux4to1_32(f, f1, f2, f3, f4, sel);

output [31:0]f;

input [31:0]f1, f2, f3, f4;

input [1:0]sel;

mux4to1 mux0(f[0], f1[0], f2[0], f3[0], f4[0], sel);

mux4to1 mux1(f[1], f1[1], f2[1], f3[1], f4[1], sel);

mux4to1 mux2(f[2], f1[2], f2[2], f3[2], f4[2], sel);

mux4to1 mux3(f[3], f1[3], f2[3], f3[3], f4[3], sel);

mux4to1 mux4(f[4], f1[4], f2[4], f3[4], f4[4], sel);

mux4to1 mux5(f[5], f1[5], f2[5], f3[5], f4[5], sel);

mux4to1 mux6(f[6], f1[6], f2[6], f3[6], f4[6], sel);

mux4to1 mux7(f[7], f1[7], f2[7], f3[7], f4[7], sel);

Page 14: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1133

mux4to1 mux8(f[8], f1[8], f2[8], f3[8], f4[8], sel);

mux4to1 mux9(f[9], f1[9], f2[9], f3[9], f4[9], sel);

mux4to1 mux10(f[10], f1[10], f2[10], f3[10], f4[10], sel);

mux4to1 mux11(f[11], f1[11], f2[11], f3[11], f4[11], sel);

mux4to1 mux12(f[12], f1[12], f2[12], f3[12], f4[12], sel);

mux4to1 mux13(f[13], f1[13], f2[13], f3[13], f4[13], sel);

mux4to1 mux14(f[14], f1[14], f2[14], f3[14], f4[14], sel);

mux4to1 mux15(f[15], f1[15], f2[15], f3[15], f4[15], sel);

mux4to1 mux16(f[16], f1[16], f2[16], f3[16], f4[16], sel);

mux4to1 mux17(f[17], f1[17], f2[17], f3[17], f4[17], sel);

mux4to1 mux18(f[18], f1[18], f2[18], f3[18], f4[18], sel);

mux4to1 mux19(f[19], f1[19], f2[19], f3[19], f4[19], sel);

mux4to1 mux20(f[20], f1[20], f2[20], f3[20], f4[20], sel);

mux4to1 mux21(f[21], f1[21], f2[21], f3[21], f4[21], sel);

mux4to1 mux22(f[22], f1[22], f2[22], f3[22], f4[22], sel);

mux4to1 mux23(f[23], f1[23], f2[23], f3[23], f4[23], sel);

mux4to1 mux24(f[24], f1[24], f2[24], f3[24], f4[24], sel);

mux4to1 mux25(f[25], f1[25], f2[25], f3[25], f4[25], sel);

mux4to1 mux26(f[26], f1[26], f2[26], f3[26], f4[26], sel);

mux4to1 mux27(f[27], f1[27], f2[27], f3[27], f4[27], sel);

mux4to1 mux28(f[28], f1[28], f2[28], f3[28], f4[28], sel);

mux4to1 mux29(f[29], f1[29], f2[29], f3[29], f4[29], sel);

mux4to1 mux30(f[30], f1[30], f2[30], f3[30], f4[30], sel);

mux4to1 mux31(f[31], f1[31], f2[31], f3[31], f4[31], sel);

endmodule

3.7. ALU 32 bit

- Kết hợp c|c module đ~ viết ở c|c phần trước lại, ta x}y dựng được 1 bộ MIPS

ALU với cấu trúc, chức năng như đ~ giới thiệu: //======================== ALU for 32 bit =======================//

module alu(Output, zero, negative, overflow, CarryOut, BussA,

BussB, ALUcontrol);

output [31:0]Output;

output zero, negative, overflow, CarryOut;

input [31:0]BussA, BussB;

input [1:0] ALUcontrol;

wire [31:0]sum,sub;

wire ovflow1,ovflow,ovflow2;

wire [31:0]SLT;

wire [31:0]Xor;

wire carryout1,carryout2;

Page 15: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1144

add32bit add0(sum,carryout1,BussA,BussB,ovflow1);

sub32bit sub0(sub,carryout2,BussA,BussB,ovflow2);

SLT32bit slt0(SLT,BussA,BussB);

xor32bit xor32(Xor,BussA,BussB);

// Determine overflow, it just turns on when ALU does subtract or add

wire [1:0]a;

and #50 and0(a[0],(~ALUcontrol[1]),ovflow1);

and #50 and1(a[1],ALUcontrol[1],ovflow2);

or #50 or0(ovflow,a[0],a[1]);

and #50 and2(overflow, ovflow, (~ALUcontrol[0]));

//Determine Out

//f1 = Add (00) , f2 = XOR (01), f3 = SUB ( 10), f4 = SLT(11)

mux4to1_32 mymux(Output,sum,Xor,sub,SLT, ALUcontrol);

// Determine negative flag

assign negative = Output[31];

//Determine zero flag

wire

term01,term02,term03,term04,term05,term06,term07,term08,term11,term12

;

or #50 or01(term01, Output[0], Output[1], Output[2], Output[3]);

or #50 or02(term02, Output[4], Output[5], Output[6], Output[7]);

or #50 or03(term03, Output[8], Output[9], Output[10], Output[11]);

or #50 or04(term04, Output[12], Output[13], Output[14],

Output[15]);

or #50 or05(term05, Output[16], Output[17], Output[18],

Output[19]);

or #50 or06(term06, Output[20], Output[21], Output[22],

Output[23]);

or #50 or07(term07, Output[24], Output[25], Output[26],

Output[27]);

or #50 or08(term08, Output[28], Output[29], Output[30],

Output[31]);

or #50 or11(term11, term01, term02, term03, term04);

or #50 or12(term12, term05, term06, term07, term08);

nor #50 nor0(zero, term11, term12);

//Determine carry flag, it just turns on when ALU does subtract or

add

wire [1:0]f;

and #50 and3(f[0],(~ALUcontrol[1]),carryout1);

and #50 and4(f[1],ALUcontrol[1],carryout2);

or #50 or1(carry,f[0],f[1]);

and #50 and5(CarryOut, carry, (~ALUcontrol[0]));

endmodule

KẾT QUẢ MÔ PHỎNG

Page 16: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1155

Page 17: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1166

4. Kiểm tra bằng chương trình Testbench

`timescale 1 ps / 100 fs

// If the verilog file containing your register file is

// not named "alu.v" then you will have to change

// to next line as appropriate.

module ALUStimulus();

parameter ClockDelay = 100000;

reg [31:0] BussA, BussB;

reg [1:0] ALUControl;

wire [31:0] Output;

wire zero, overflow, CarryOut, negative;

integer i;

// If your register file module is not named "alu" then you will

// have to change the following line in order to create an instance

of

// your register file. Also you must make sure that the port

declarations

// match up with the module instance in this stimulus file.

alu alu1(Output, CarryOut, zero, overflow, negative, BussA, BussB,

ALUControl);

initial

begin

$dumpfile("alustim.vcd");

$dumpvars(2,alu1);

end

initial

begin

$monitor($time, " Output=%h, CarryOut=%b, BussA=%h,

BussB=%h, ALUControl=%b, Zero=%b, Overflow=%b, Negative=%b",

Output, CarryOut, BussA, BussB, ALUControl, zero,

overflow, negative);

/* Addition unit testing */

ALUControl=00;

BussA=32'h00000DEF; BussB=32'h00000ABC; // Should output 000018AB

#(ClockDelay);

BussA=32'h00001234; BussB=32'h00000105; // Should output 00001339

Page 18: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1177

#(ClockDelay);

BussA=32'h7FFFFFFF; BussB=32'h00000001; // Should output 80000000,

overflow, negative

#(ClockDelay);

BussA=32'h8FFFFFFF; BussB=32'h80000001; // Should output

10000000,overflow,carryout

#(ClockDelay);

/* Xor unit testing */

ALUControl=01;

BussA=32'h00000DEF; BussB=32'h00000ABC; //Should output 00000753

#(ClockDelay);

BussA=32'h00001234; BussB=32'h00000105; //Should output 00001331

#(ClockDelay);

BussA=32'h80000000; BussB=32'h00000001; //Should output 80000001

#(ClockDelay);

/* Subtraction unit testing */

ALUControl=10;

BussA=32'h00000DEF; BussB=32'h00000ABC; // Should output 00000333

#(ClockDelay);

BussA=32'h00001234; BussB=32'h00000105; // Should output 0000112F

#(ClockDelay);

BussA=32'h80000000; BussB=32'h00000001; // Should output 7FFFFFFF,

overflow

#(ClockDelay);

BussA=32'h00001234; BussB=32'h00001234; // Should output

00000000,zero

#(ClockDelay);

BussA=32'h00001234; BussB=32'h80001234; // Should output 80000000

#(ClockDelay);

/* slt unit testing */

ALUControl=11;

BussA=32'h00000DEF; BussB=32'h00000ABC; //Should output 00000000

#(ClockDelay);

BussA=32'h00001234; BussB=32'h00000105; //Should output 00000000

#(ClockDelay);

BussA=32'h8EEEEEEE; BussB=32'h12345678; //Should output 00000001

#(ClockDelay);

end

endmodule

Page 19: thiết kế ALU-32bit

LLaabb 22 –– CCấấuu ttrrúúcc mmááyy ttíínnhh MMIIPPSS AALLUU

NNhhóómm 1144AA –– 0088DDTT11 1188

KẾT QUẢ HIỂN THỊ TRÊN GTKWAVE

- Nhận xét: Với chương trình kiểm tra trên thì MIPS 32bits-ALU hoạt động đúng

với yêu cầu thiết kế.