이재하

Single Cycle CPU

1 +module ALU(aluin1, aluin2, aluctrl, aluout, alubranch);
2 +
3 +input[31:0] aluin1, aluin2;
4 +input[3:0] aluctrl;
5 +output reg[31:0] aluout;
6 +output alubranch;
7 +
8 +reg overflow;
9 +reg[63:0] temp;
10 +reg[31:0] HI, LO; // HI, LO register for multiplication and division.
11 +
12 +assign alubranch = aluout == 32'h00000000 ? 1'b1 : 1'b0;
13 +
14 +initial begin
15 + temp = 64'h0000000000000000;
16 + HI = 32'h00000000;
17 + LO = 32'h00000000;
18 +end
19 +
20 +always @(*) begin
21 +overflow = 0;
22 +case(aluctrl)
23 + 4'b0000: aluout = aluin1 & aluin2; // and
24 + 4'b0001: aluout = aluin1 | aluin2; // or
25 + 4'b0010: begin // add
26 + aluout = aluin1 + aluin2;
27 + overflow = aluin1[31]==aluin2[31] && aluin1[31]!=aluout[31] ? 1'b1 : 1'b0; // overflow detection.
28 + end
29 + 4'b0110: begin // sub
30 + aluout = aluin1 - aluin2;
31 + overflow = aluin1[31]!=aluin2[31] && aluin1[31]!=aluout[31] ? 1'b1 : 1'b0; // overflow detection.
32 + end
33 +
34 + 4'b0111: begin // slt
35 + aluout[31:1] = {31{1'b0}};
36 + aluout[0] = aluin1 < aluin2 ? 1'b1 : 1'b0;
37 + end
38 + 4'b1000: begin // mult
39 + temp = aluin1 * aluin2;
40 + HI = temp[63:32];
41 + LO = temp[31:0];
42 + end
43 + 4'b1001: begin // div
44 + HI = aluin1 % aluin2;
45 + LO = aluin1 / aluin2;
46 + end
47 + 4'b1010: aluout = HI; // mfhi
48 + 4'b1011: aluout = LO; // mflo
49 + 4'b1100: aluout = ~(aluin1 | aluin2); // nor
50 + 4'b1101: aluout = aluin1 ^ aluin2; // xor
51 + default: aluout = 32'b0;
52 +endcase
53 +end
54 +
55 +endmodule
1 +module ALUControl(funct, aluop, aluctrl);
2 +
3 +input[5:0] funct;
4 +input[1:0] aluop;
5 +output reg[3:0] aluctrl;
6 +
7 +always @(*) begin
8 + case(aluop)
9 + 2'b00: aluctrl = 4'b0010; // add
10 + 2'b01: aluctrl = 4'b0110; // sub
11 + 2'b10: case(funct) // R type instruction
12 + 6'b100000: aluctrl = 4'b0010; // add
13 + // 6'b100001: aluctrl = 4'b0010; // addu
14 + 6'b100010: aluctrl = 4'b0110; // sub
15 + // 6'b100011: aluctrl = 4'b0110; // subu
16 + 6'b100100: aluctrl = 4'b0000; // and
17 + 6'b100101: aluctrl = 4'b0001; // or
18 + 6'b100110: aluctrl = 4'b1101; // xor
19 + 6'b011000: aluctrl = 4'b1000; // mult
20 + // 6'b011001: aluctrl = 4'b1000; // multu
21 + 6'b011010: aluctrl = 4'b1001; // div
22 + // 6'b011011: aluctrl = 4'b1001; // divu
23 + 6'b101010: aluctrl = 4'b0111; // slt
24 + // 6'b101011: aluctrl = 4'b0111; // sltu
25 + 6'b010000: aluctrl = 4'b1010; // mfhi
26 + 6'b010010: aluctrl = 4'b1011; // mflo
27 +
28 + default: aluctrl = 4'b1111;
29 + endcase
30 + default: aluctrl = 4'b1111;
31 + endcase
32 +end
33 +
34 +endmodule
1 +module Adder(adderinput1, adderinput2, adderoutput);
2 +
3 +input[31:0] adderinput1, adderinput2;
4 +output[31:0] adderoutput;
5 +
6 +assign adderoutput = adderinput1 + adderinput2;
7 +
8 +endmodule
9 +
1 +module Control(opcode, regdst, regwrite, alusrc, aluop, memread, memwrite, memtoreg, branch, jump);
2 +
3 +input[5:0] opcode;
4 +output reg regdst, jump, branch, memread, memtoreg, memwrite, alusrc, regwrite;
5 +output reg[1:0] aluop;
6 +
7 +always @(*) begin
8 + case(opcode)
9 + 6'b000000: begin // R type instruction
10 + regdst = 1'b1;
11 + regwrite = 1'b1;
12 + alusrc = 1'b0;
13 + aluop = 2'b10;
14 + memread = 1'b0;
15 + memwrite = 1'b0;
16 + memtoreg = 1'b0;
17 + branch = 1'b0;
18 + jump = 1'b0;
19 + end
20 + 6'b001000: begin // addi
21 + regdst = 1'b0;
22 + regwrite = 1'b1;
23 + alusrc = 1'b1;
24 + aluop = 2'b00;
25 + memread = 1'b0;
26 + memwrite = 1'b0;
27 + memtoreg = 1'b0;
28 + branch = 1'b0;
29 + jump = 1'b0;
30 + end
31 + 6'b001001: begin // addiu
32 + regdst = 1'b0;
33 + regwrite = 1'b1;
34 + alusrc = 1'b1;
35 + aluop = 2'b00;
36 + memread = 1'b0;
37 + memwrite = 1'b0;
38 + memtoreg = 1'b0;
39 + branch = 1'b0;
40 + jump = 1'b0;
41 + end
42 + 6'b000100: begin // beq
43 + // regdst = 1'bx; // don't care
44 + regwrite = 1'b0;
45 + alusrc = 1'b0;
46 + aluop = 2'b01;
47 + memread = 1'b0;
48 + memwrite = 1'b0;
49 + // memtoreg = 1'bx;
50 + branch = 1'b1;
51 + jump = 1'b0;
52 + end
53 + 6'b000010: begin // jump
54 + // regdst = 1'bx;
55 + regwrite = 1'b0;
56 + // alusrc = 1'bx;
57 + // aluop = 2'bxx;
58 + memread = 1'b0;
59 + memwrite = 1'b0;
60 + // memtoreg = 1'bx;
61 + branch = 1'b0;
62 + jump = 1'b1;
63 + end
64 + 6'b100011: begin // lw
65 + regdst = 1'b0;
66 + regwrite = 1'b1;
67 + alusrc = 1'b1;
68 + aluop = 2'b00;
69 + memread = 1'b1;
70 + memwrite = 1'b0;
71 + memtoreg = 1'b1;
72 + branch = 1'b0;
73 + jump = 1'b0;
74 + end
75 + 6'b101011: begin // sw
76 + regdst = 1'b0;
77 + regwrite = 1'b0;
78 + alusrc = 1'b1;
79 + aluop = 2'b00;
80 + memread = 1'b0;
81 + memwrite = 1'b1;
82 + // memtoreg = 1'bx;
83 + branch = 1'b0;
84 + jump = 1'b0;
85 + end
86 + default: begin
87 + // regdst = 1'bx;
88 + regwrite = 1'b0;
89 + // alusrc = 1'bx;
90 + aluop = 2'b00;
91 + memread = 1'b0;
92 + memwrite = 1'b0;
93 + // memtoreg = 1'bx;
94 + // branch = 1'bx;
95 + // jump = 1'bx;
96 + end
97 + endcase
98 +end
99 +
100 +endmodule
1 +module DataMemory(address, writedata, memread, memwrite, readdata);
2 +
3 +input[31:0] address, writedata;
4 +input memread, memwrite;
5 +output[31:0] readdata;
6 +
7 +reg[31:0] mem[255:0];
8 +
9 +assign readdata = memread ? mem[address/4] : writedata;
10 +
11 +always @(*) begin
12 + if(memwrite==1'b1) begin
13 + mem[address/4] = writedata;
14 + end
15 +end
16 +
17 +endmodule
1 +module InstructionMemory(address, instruction);
2 +
3 +input[31:0] address;
4 +output reg[31:0] instruction;
5 +
6 +reg[31:0] instr_mem[127:0];
7 +
8 +initial begin
9 +instr_mem[0] = 32'd0;
10 +instr_mem[1] = 32'b00100100000010000000000011111111; // addi $0 $8 255
11 +instr_mem[2] = 32'b00000001000010000100100000100000; // add $8 $8 $9
12 +instr_mem[3] = 32'b00000001000000000101000000100000; // add $8 $0 $10
13 +instr_mem[4] = 32'b00010001000010110000000000000001; // beq $8 $11 +1
14 +instr_mem[5] = 32'b00000001000010010000000000011000; // mult $8 $9
15 +instr_mem[6] = 32'd0;
16 +instr_mem[7] = 32'b00000000000000000110000000010000; // mfhi $12
17 +instr_mem[8] = 32'b00000000000000000110100000010010; // mflo $13
18 +instr_mem[9] = 32'b10101100000011010000000000111100; // sw $0 $13 60
19 +instr_mem[10] = 32'd0;
20 +instr_mem[11] = 32'b00010001000010110000000000000001; // beq $8 $11 +1
21 +instr_mem[12] = 32'b10001100000000010000000000111100; // lw $0 $1 60
22 +instr_mem[13] = 32'd0;
23 +
24 +end
25 +
26 +always @ (*) begin
27 + instruction = instr_mem[address/4];
28 +end
29 +
30 +endmodule
1 +module InstructionMemory(address, instruction);
2 +
3 +input[31:0] address;
4 +output reg[31:0] instruction;
5 +
6 +reg[31:0] instr_mem[127:0];
7 +
8 +initial begin
9 +instr_mem[0] = 32'd0;
10 +instr_mem[1] = 32'b00100100000010000000000011111111; // addi $0 $8 255
11 +instr_mem[2] = 32'b00000001000010000100100000100000; // add $8 $8 $9
12 +instr_mem[3] = 32'b00000001000000000101000000100000; // add $8 $0 $10
13 +instr_mem[4] = 32'b00010001000010110000000000000001; // beq $8 $11 +1
14 +instr_mem[5] = 32'b00000001000010010000000000011000; // mult $8 $9
15 +instr_mem[6] = 32'd0;
16 +instr_mem[7] = 32'b00000000000000000110000000010000; // mfhi $12
17 +instr_mem[8] = 32'b00000000000000000110100000010010; // mflo $13
18 +instr_mem[9] = 32'b10101100000011010000000000111100; // sw $0 $13 15
19 +instr_mem[10] = 32'd0;
20 +instr_mem[11] = 32'b00010001000010110000000000000001; // beq $8 $11 +1
21 +instr_mem[12] = 32'b10001100000000010000000000111100; // lw $0 $1 60
22 +instr_mem[13] = 32'd0;
23 +
24 +end
25 +
26 +always @ (*) begin
27 + instruction = instr_mem[address/4];
28 +end
29 +
30 +endmodule
1 +D:/class/Capstone1/KNW_Project2/Project/SingleCycle/testbench.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/testbench.v
2 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
3 +-- Compiling module testbench
4 +
5 +Top level modules:
6 + testbench
7 +
8 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Adder.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Adder.v
9 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
10 +-- Compiling module Adder
11 +
12 +Top level modules:
13 + Adder
14 +
15 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/SignExtend.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/SignExtend.v
16 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
17 +-- Compiling module SignExtend
18 +
19 +Top level modules:
20 + SignExtend
21 +
22 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU_Control.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU_Control.v
23 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
24 +-- Compiling module ALUControl
25 +
26 +Top level modules:
27 + ALUControl
28 +
29 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ShiftLeft2.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ShiftLeft2.v
30 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
31 +-- Compiling module ShiftLeft2
32 +
33 +Top level modules:
34 + ShiftLeft2
35 +
36 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/test.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/test.v
37 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
38 +-- Compiling module test
39 +
40 +Top level modules:
41 + test
42 +
43 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Control.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Control.v
44 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
45 +-- Compiling module Control
46 +
47 +Top level modules:
48 + Control
49 +
50 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU.v
51 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
52 +-- Compiling module ALU
53 +
54 +Top level modules:
55 + ALU
56 +
57 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Mux.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Mux.v
58 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
59 +-- Compiling module Mux32bit
60 +-- Compiling module Mux5bit
61 +
62 +Top level modules:
63 + Mux32bit
64 + Mux5bit
65 +
66 +} {} {}} {D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Data Memory.v} {1 {vlog -work work -stats=none {D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Data Memory.v}
67 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
68 +-- Compiling module DataMemory
69 +
70 +Top level modules:
71 + DataMemory
72 +
73 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/clock.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/clock.v
74 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
75 +-- Compiling module Clock
76 +
77 +Top level modules:
78 + Clock
79 +
80 +} {} {}} D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Register.v {1 {vlog -work work -stats=none D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Register.v
81 +Model Technology ModelSim PE Student Edition vlog 10.4a Compiler 2015.03 Apr 7 2015
82 +-- Compiling module Register
83 +
84 +Top level modules:
85 + Register
86 +
87 +} {} {}}
This diff is collapsed. Click to expand it.
1 +module Mux32bit(muxin1, muxin2, signal, muxout);
2 +
3 +input[31:0] muxin1, muxin2;
4 +input signal;
5 +output reg[31:0] muxout;
6 +
7 +always @(*) begin
8 +case(signal)
9 + 1'b0: muxout = muxin1;
10 + 1'b1: muxout = muxin2;
11 +endcase
12 +end
13 +
14 +endmodule
15 +
16 +module Mux5bit(muxin1, muxin2, signal, muxout);
17 +
18 +input[4:0] muxin1, muxin2;
19 +input signal;
20 +output reg[4:0] muxout;
21 +
22 +always @(*) begin
23 +case(signal)
24 + 1'b0: muxout = muxin1;
25 + 1'b1: muxout = muxin2;
26 +endcase
27 +end
28 +
29 +endmodule
1 +module Register(readin1, readin2, writein, writedata, regwrite, regout1, regout2);
2 +
3 +input[4:0] readin1, readin2, writein;
4 +input[31:0] writedata;
5 +input regwrite;
6 +output[31:0] regout1, regout2;
7 +
8 +integer i;
9 +reg[31:0] register[31:0];
10 +
11 +assign regout1 = register[readin1];
12 +assign regout2 = register[readin2];
13 +
14 +initial begin
15 + for(i=0; i<32; i=i+1) register[i] = 32'd0;
16 +end
17 +
18 +always @(*) begin
19 + if(regwrite == 1'b1 && writein != 5'd0) begin
20 + register[writein] = writedata;
21 + end
22 +end
23 +
24 +endmodule
1 +module ShiftLeft2(shiftinput, shiftoutput);
2 +
3 +input[31:0] shiftinput;
4 +output[31:0] shiftoutput;
5 +
6 +assign shiftoutput = {shiftinput[29:0], 2'b00};
7 +
8 +endmodule
1 +module SignExtend(signedinput, signedoutput);
2 +
3 +input[15:0] signedinput;
4 +output[31:0] signedoutput;
5 +
6 +assign signedoutput = {{16{signedinput[15]}},signedinput};
7 +
8 +endmodule
1 +module Clock(clk);
2 +
3 +output reg clk;
4 +
5 +initial clk = 0;
6 +always #50 clk = ~clk;
7 +
8 +endmodule
9 +
1 +module test;
2 +
3 +reg[31:0] in1, in2;
4 +reg[3:0] ctrl;
5 +wire[31:0] out;
6 +wire a;
7 +
8 +ALU alu(in1, in2, ctrl, out, a);
9 +
10 +initial begin
11 + in1 = 32'd128;
12 + in2 = 32'd982;
13 + ctrl = 4'b1000;
14 + #100;
15 + in1 = 32'd123;
16 + in2 = 32'd246;
17 + ctrl = 4'b0010;
18 + #100;
19 + ctrl = 4'b1010;
20 + #100;
21 + ctrl = 4'b1011;
22 + #100;
23 +end
24 +
25 +/*
26 +wire clk;
27 +
28 +Clock clock(clk);
29 +*/
30 +
31 +/*
32 +reg[31:0] address, wdata;
33 +reg mr, mw;
34 +wire[31:0] rdata;
35 +
36 +DataMemory damem(address, wdata, mr, mw, rdata);
37 +
38 +initial begin
39 + address = 32'd0;
40 + wdata = 32'd127;
41 + mr = 1'b0;
42 + mw = 1'b1;
43 + #100;
44 + address = 32'd48;
45 + wdata = 32'd255;
46 + mr = 1'b0;
47 + mw = 1'b1;
48 + #100;
49 + address = 32'd48;
50 + wdata = 32'd255;
51 + mr = 1'b1;
52 + mw = 1'b0;
53 + #100;
54 + address = 32'd48;
55 + wdata = 32'd4;
56 + mr = 1'b0;
57 + mw = 1'b1;
58 + #100;
59 +end
60 +*/
61 +
62 +/*
63 +wire[31:0] regout1, regout2;
64 +reg[4:0] ins1, ins2, ins3;
65 +wire[31:0] aluresult;
66 +reg[3:0] aluctrl;
67 +reg rwrite;
68 +reg[31:0] aluin2;
69 +
70 +Register Regi(ins1, ins2, ins3, aluresult, rwrite, regout1, regout2);
71 +ALU alu(regout1, aluin2, aluctrl, aluresult);
72 +
73 +initial begin
74 + rwrite = 1;
75 + ins1 = 5'd0;
76 + ins2 = 5'd29;
77 + ins3 = 5'd7;
78 + aluin2 = 32'h7fffffff;
79 + aluctrl = 4'b0010;
80 + #100;
81 + rwrite = 1;
82 + ins1 = 5'd7;
83 + ins2 = 5'd7;
84 + ins3 = 5'd8;
85 + aluctrl = 4'b0010;
86 + #100;
87 + rwrite = 0;
88 + #100;
89 + rwrite = 1;
90 + ins1 = 5'd0;
91 + ins2 = 5'd29;
92 + ins3 = 5'd7;
93 + aluin2 = 32'h7fffffff;
94 + aluctrl = 4'b0010;
95 + #100;
96 + rwrite = 1;
97 + ins1 = 5'd7;
98 + ins2 = 5'd7;
99 + ins3 = 5'd8;
100 + aluctrl = 4'b0010;
101 + #100;
102 +end
103 +*/
104 +
105 +/*
106 +reg[31:0] input1, input2;
107 +reg[3:0] ctrl;
108 +wire[31:0] output1;
109 +wire zero;
110 +
111 +ALU testalu(input1, input2, ctrl, output1, zero);
112 +
113 +initial begin
114 + input1 <= 32'h0000000f;
115 + input2 <= 32'h000000f0;
116 + ctrl <= 4'h0; // add
117 + #100;
118 + ctrl <= 4'h1; // or
119 + #100;
120 + ctrl <= 4'h2; // add
121 + #100;
122 + ctrl <= 4'h6; // sub
123 + #100;
124 + ctrl <= 4'h7; // slt
125 + #100;
126 + ctrl <= 4'hc; // nor
127 + #100;
128 + input1 <= 32'h000000f0;
129 + input2 <= 32'h0000000f;
130 + ctrl <= 4'h6; // sub
131 + #100;
132 + input1 <= 32'h000000f0;
133 + input2 <= 32'h0000000f;
134 + ctrl <= 4'h6; // sub
135 + #100;
136 + input1 <= 32'h000000f0;
137 + input2 <= 32'h0000000f;
138 + ctrl <= 4'h6; // sub
139 + #100;
140 + input1 <= 32'h000000f0;
141 + input2 <= 32'h0000000f;
142 + ctrl <= 4'h6; // sub
143 + #100;
144 +end
145 +*/
146 +
147 +/*
148 +reg[31:0] input1;
149 +wire[31:0] output1;
150 +
151 +InstructionMemory im(input1, output1);
152 +
153 +initial
154 +begin
155 + input1 = {{28{1'b0}}, 4'b0000};
156 + #100;
157 + input1 = {{28{1'b0}}, 4'b1100};
158 + #100;
159 + input1 = {{28{1'b0}}, 4'b1000};
160 + #100;
161 + input1 = {{28{1'b0}}, 4'b0100};
162 + #100;
163 + input1 = {{28{1'b0}}, 4'b0000};
164 + #100;
165 +end
166 +*/
167 +
168 +/*
169 +reg[7:0] input1;
170 +wire[7:0] output1;
171 +
172 +Adder adder1(input1, 8'b00000001, output1);
173 +
174 +initial
175 +begin
176 + input1 = 8'b00001111;
177 + #100;
178 + input1 = 8'b00001000;
179 + #100;
180 + input1 = 8'b00000000;
181 + #100;
182 + input1 = 8'b11111111;
183 + #100;
184 +end
185 +*/
186 +endmodule
1 +module testbench;
2 +/*
3 +wire clk; // clock
4 +reg[31:0] PC; // program counter
5 +reg[31:0] instr_address;
6 +wire[31:0] addPC4, addPCbranch, tempPC1, nextPC;
7 +
8 +wire[31:0] instr; // loaded instruction.
9 +
10 +wire[4:0] reg_writereg1; // register number for the write data.
11 +wire[31:0] reg_writedata; // data that will be written in the register.
12 +wire[31:0] reg_readdata1, reg_readdata2; // data from the requested register.
13 +
14 +wire[31:0] alu_input2; // input data of ALU.
15 +wire[31:0] alu_result; // result data of ALU.
16 +wire alu_branch; // indicator for branch operation.
17 +
18 +wire[31:0] mem_readdata; // data from the requested address.
19 +
20 +wire ctrl_regdst, ctrl_regwrite, ctrl_alusrc, ctrl_memread, ctrl_memwrite, ctrl_memtoreg, ctrl_branch, ctrl_jump;
21 +wire[1:0] ctrl_aluop; // control signals.
22 +
23 +wire[3:0] aluctrl; // alu control signal.
24 +
25 +wire[31:0] extend_output;
26 +
27 +wire[31:0] shiftBranch_output;
28 +wire[31:0] shiftJump_output;
29 +
30 +Clock clock(clk);
31 +InstructionMemory instrmem(instr_address, instr);
32 +Register register(instr[25:21], instr[20:16], reg_writereg1, reg_writedata, ctrl_regwrite, reg_readdata1, reg_readdata2);
33 +ALU alu(reg_readdata1, alu_input2, aluctrl, alu_result, alu_branch);
34 +DataMemory datamem(alu_result, reg_readdata2, ctrl_memread, ctrl_memwrite, mem_readdata);
35 +Control ctrl(instr[31:26], ctrl_regdst, ctrl_regwrite, ctrl_alusrc, ctrl_aluop, ctrl_memread, ctrl_memwrite, ctrl_memtoreg, ctrl_branch, ctrl_jump);
36 +ALUControl ALUctrl(instr[5:0], ctrl_aluop, aluctrl);
37 +Mux5bit mux_writereg(instr[20:16], instr[15:11], ctrl_regdst, reg_writereg1);
38 +Mux32bit mux_alu(reg_readdata2, extend_output, ctrl_alusrc, alu_input2);
39 +Mux32bit mux_writedata(alu_result, mem_readdata, ctrl_memtoreg, reg_writedata);
40 +Mux32bit mux_branch(addPC4, addPCbranch, {ctrl_branch&alu_branch} , tempPC1);
41 +Mux32bit mux_jump(tempPC1, {addPC4[31:28], shiftJump_output[27:0]}, ctrl_jump, nextPC);
42 +SignExtend extend(instr[15:0], extend_output);
43 +Adder add_pc4(PC, 32'h00000004, addPC4);
44 +Adder add_branch(addPC4, shiftBranch_output, addPCbranch);
45 +ShiftLeft2 shiftBranch(extend_output, shiftBranch_output);
46 +ShiftLeft2 shiftJump({6'b000000, instr[25:0]}, shiftJump_output);
47 +
48 +initial begin
49 + PC = 32'h00000000;
50 +end
51 +
52 +always @(posedge clk) begin
53 + case(nextPC[31]) // if nextPC is available, PC = nextPC.
54 + 1'b0: PC = nextPC;
55 + 1'b1: PC = nextPC;
56 + endcase
57 +
58 + instr_address = PC;
59 +end
60 +*/
61 +
62 +wire clk; // clock
63 +reg[31:0] PC, nextPC; // program counter
64 +
65 +// Instruction Memory (IM)
66 +reg[31:0] address; // instruction address. input of IM.
67 +wire[31:0] instr; // loaded instruction. output of IM
68 +
69 +// Register
70 +reg[4:0] reg_readreg1, reg_readreg2; // register numbers of the read data. input of register.
71 +reg[4:0] reg_writereg1; // register number for the write data. input of register.
72 +reg[31:0] reg_writedata; // data that will be written in the register. input of register.
73 +reg reg_sig_regwrite; // regwrite control signal. input of register
74 +wire[31:0] reg_readdata1, reg_readdata2; // data from the requested register. outputs of register.
75 +
76 +// ALU
77 +reg[31:0] alu_input1, alu_input2; // input data of ALU. inputs of ALU.
78 +reg[3:0] alu_control; // ALU control signal. input of ALU.
79 +wire[31:0] alu_result; // result data of ALU. output of ALU.
80 +wire alu_branch; // indicator for branch operation. output of ALU.
81 +
82 +//Data Memory (DM)
83 +reg[31:0] mem_addr; // address of the read data. input of DM.
84 +reg[31:0] mem_writedata; // data that will be written in the memory. input of DM.
85 +reg mem_memread, mem_memwrite; // control signals for DM. input of DM.
86 +wire[31:0] mem_readdata; // data from the requested address. output of DM.
87 +
88 +// Control Unit
89 +reg[5:0] ctrl_opcode; // opcode of the instruction. input of control unit.
90 +wire ctrl_regdst, ctrl_regwrite, ctrl_alusrc, ctrl_memread; // ??
91 +wire ctrl_memwrite, ctrl_memtoreg, ctrl_branch, ctrl_jump; // ??? control signals outputs of control unit.
92 +wire[1:0] ctrl_aluop; // ??
93 +
94 +// ALU Control Unit
95 +reg[5:0] aluctrl_funct; // function code of the R type instructions. input of ALU control unit.
96 +reg[1:0] aluctrl_aluop; // aluop signal. input of ALU control unit.
97 +wire[3:0] aluctrl_sig; // alu control signal. output of ALU control unit.
98 +
99 +// Multiplexer (Mux)
100 + // mux_writereg Mux for Write Register.
101 +reg[4:0] mux_writereg_input1, mux_writereg_input2;
102 +reg mux_writereg_signal;
103 +wire[4:0] mux_writereg_output;
104 + // mux_alu Mux for ALU input 2.
105 +reg[31:0] mux_alu_input1, mux_alu_input2;
106 +reg mux_alu_signal;
107 +wire[31:0] mux_alu_output;
108 + // mux_writedata Mux for Write Data of Register.
109 +reg[31:0] mux_writedata_input1, mux_writedata_input2;
110 +reg mux_writedata_signal;
111 +wire[31:0] mux_writedata_output;
112 + // mux_branch Mux for Branch
113 +reg[31:0] mux_branch_input1, mux_branch_input2;
114 +reg mux_branch_signal;
115 +wire[31:0] mux_branch_output;
116 + // mux_jump Mux for Jump
117 +reg[31:0] mux_jump_input1, mux_jump_input2;
118 +reg mux_jump_signal;
119 +wire[31:0] mux_jump_output;
120 +
121 +// Sign Extend
122 +reg[15:0] extend_input;
123 +wire[31:0] extend_output;
124 +
125 +// Adder
126 + // add_pc4
127 +reg[31:0] add_pc4_input; // input2 is 4.
128 +wire[31:0] add_pc4_output;
129 + // add_branch
130 +reg[31:0] add_branch_input1, add_branch_input2;
131 +wire[31:0] add_branch_output;
132 +
133 +// Shift Left 2
134 + // shiftBranch ShiftLeft2 which is used for Branch instructions.
135 +reg[31:0] shiftBranch_input;
136 +wire[31:0] shiftBranch_output;
137 + // shiftJump ShiftLeft2 which is used for Jump instructions.
138 +reg[31:0] shiftJump_input;
139 +wire[31:0] shiftJump_output;
140 +
141 +
142 +Clock clock(clk);
143 +InstructionMemory instrmem(address, instr);
144 +Register register(reg_readreg1, reg_readreg2, reg_writereg1, reg_writedata, reg_sig_regwrite, reg_readdata1, reg_readdata2);
145 +ALU alu(alu_input1, alu_input2, alu_control, alu_result, alu_branch);
146 +DataMemory datamem(mem_addr, mem_writedata, mem_memread, mem_memwrite, mem_readdata);
147 +Control ctrl(ctrl_opcode, ctrl_regdst, ctrl_regwrite, ctrl_alusrc, ctrl_aluop, ctrl_memread, ctrl_memwrite, ctrl_memtoreg, ctrl_branch, ctrl_jump);
148 +ALUControl aluctrl(aluctrl_funct, aluctrl_aluop, aluctrl_sig);
149 +Mux5bit mux_writereg(mux_writereg_input1, mux_writereg_input2, mux_writereg_signal, mux_writereg_output);
150 +Mux32bit mux_alu(mux_alu_input1, mux_alu_input2, mux_alu_signal, mux_alu_output);
151 +Mux32bit mux_writedata(mux_writedata_input1, mux_writedata_input2, mux_writedata_signal, mux_writedata_output);
152 +Mux32bit mux_branch(mux_branch_input1, mux_branch_input2, mux_branch_signal, mux_branch_output);
153 +Mux32bit mux_jump(mux_jump_input1, mux_jump_input2, mux_jump_signal, mux_jump_output);
154 +SignExtend extend(extend_input, extend_output);
155 +Adder add_pc4(add_pc4_input, 32'h00000004, add_pc4_output);
156 +Adder add_branch(add_branch_input1, add_branch_input2, add_branch_output);
157 +ShiftLeft2 shiftBranch(shiftBranch_input, shiftBranch_output);
158 +ShiftLeft2 shiftJump(shiftJump_input, shiftJump_output);
159 +
160 +initial begin
161 + PC = 32'h00000000;
162 + nextPC = 32'h00000000;
163 +end
164 +
165 +always @(posedge clk) begin
166 +// IF
167 + case(nextPC[0])
168 + 1'b0: PC = nextPC;
169 + 1'b1: PC = nextPC;
170 + endcase
171 +#1;
172 + address = PC;
173 + add_pc4_input = PC;
174 +#1;
175 +// ID
176 + ctrl_opcode <= instr[31:26];
177 + reg_readreg1 <= instr[25:21];
178 + reg_readreg2 <= instr[20:16];
179 + mux_writereg_input1 <= instr[20:16];
180 + mux_writereg_input2 <= instr[15:11];
181 + extend_input <= instr[15:0];
182 + aluctrl_funct <= instr[5:0];
183 + shiftJump_input <= {6'b000000, instr[25:0]};
184 +#1;
185 + mux_writereg_signal <= ctrl_regdst;
186 + aluctrl_aluop <= ctrl_aluop;
187 +
188 +// EX
189 + mux_alu_input1 <= reg_readdata2;
190 + mux_alu_input2 <= extend_output;
191 + mux_alu_signal <= ctrl_alusrc;
192 + shiftBranch_input <= extend_output;
193 +#1;
194 + alu_input1 <= reg_readdata1;
195 + alu_input2 <= mux_alu_output;
196 + alu_control <= aluctrl_sig;
197 + add_branch_input1 <= add_pc4_output;
198 + add_branch_input2 <= shiftBranch_output;
199 +#1;
200 + mux_branch_input1 <= add_pc4_output;
201 + mux_branch_input2 <= add_branch_output;
202 + mux_branch_signal <= ctrl_branch & alu_branch;
203 +#1;
204 +
205 +// MEM
206 + mux_jump_input1 <= mux_branch_output;
207 + mux_jump_input2 <= {add_pc4_output[31:28], shiftJump_output[27:0]};
208 + mux_jump_signal <= ctrl_jump;
209 + mem_addr <= alu_result;
210 + mem_writedata <= reg_readdata2;
211 + mem_memread <= ctrl_memread;
212 + mem_memwrite <= ctrl_memwrite;
213 +#1;
214 +// WB
215 + mux_writedata_input1 <= alu_result;
216 + mux_writedata_input2 <= mem_readdata;
217 + mux_writedata_signal <= ctrl_memtoreg;
218 +#1;
219 + reg_sig_regwrite <= ctrl_regwrite;
220 + reg_writereg1 <= mux_writereg_output;
221 + reg_writedata <= mux_writedata_output;
222 +#1;
223 + nextPC <= mux_jump_output;
224 +end
225 +
226 +endmodule
No preview for this file type
1 +m255
2 +K4
3 +z2
4 +13
5 +!s112 1.1
6 +!i10d 8192
7 +!i10e 25
8 +!i10f 100
9 +cModel Technology
10 +dC:/Modeltech_pe_edu_10.4a/examples
11 +vAdder
12 +Z0 !s110 1589714636
13 +!i10b 1
14 +!s100 C]ac0M5ljAN8jKk:YMWUe1
15 +IPABU2L8BVV2T>WDY4a1F@3
16 +Z1 VDg1SIo80bB@j0V0VzS_@n1
17 +Z2 dD:/class/Capstone1/KNW_Project2/Project/SingleCycle
18 +w1589585757
19 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Adder.v
20 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/Adder.v
21 +L0 1
22 +Z3 OP;L;10.4a;61
23 +r1
24 +!s85 0
25 +31
26 +!s108 1589714635.000000
27 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Adder.v|
28 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Adder.v|
29 +!s101 -O0
30 +!i113 1
31 +Z4 o-work work -L mtiAvm -L mtiRnm -L mtiOvm -L mtiUvm -L mtiUPF -L infact -O0
32 +n@adder
33 +vALU
34 +R0
35 +!i10b 1
36 +!s100 iQ=Ee?Z0WEj<ciYnJKX4Q1
37 +Ib>08dCP?2Sj=]CBL[iS`d3
38 +R1
39 +R2
40 +w1589611061
41 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU.v
42 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU.v
43 +L0 1
44 +R3
45 +r1
46 +!s85 0
47 +31
48 +Z5 !s108 1589714636.000000
49 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU.v|
50 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU.v|
51 +!s101 -O0
52 +!i113 1
53 +R4
54 +n@a@l@u
55 +vALUControl
56 +R0
57 +!i10b 1
58 +!s100 5M;8KH=?8dC:nP8HEC8AT0
59 +Ij7f_HKS32W^mlmZIBXe=P0
60 +R1
61 +R2
62 +w1589611047
63 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU_Control.v
64 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU_Control.v
65 +L0 1
66 +R3
67 +r1
68 +!s85 0
69 +31
70 +R5
71 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU_Control.v|
72 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ALU_Control.v|
73 +!s101 -O0
74 +!i113 1
75 +R4
76 +n@a@l@u@control
77 +vClock
78 +R0
79 +!i10b 1
80 +!s100 OWQXV6kDYiT>ChTOoCFa]2
81 +IQ3e7_okQ4UFF5[gGVRJ]L2
82 +R1
83 +R2
84 +w1589585780
85 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/clock.v
86 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/clock.v
87 +L0 1
88 +R3
89 +r1
90 +!s85 0
91 +31
92 +R5
93 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/clock.v|
94 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/clock.v|
95 +!s101 -O0
96 +!i113 1
97 +R4
98 +n@clock
99 +vControl
100 +R0
101 +!i10b 1
102 +!s100 gY[fP@o0^cofOhmoM3fic0
103 +IT_Y=g<>d6ldLOEgP5h`JH3
104 +R1
105 +R2
106 +w1589611718
107 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Control.v
108 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/Control.v
109 +L0 1
110 +R3
111 +r1
112 +!s85 0
113 +31
114 +R5
115 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Control.v|
116 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Control.v|
117 +!s101 -O0
118 +!i113 1
119 +R4
120 +n@control
121 +vDataMemory
122 +R0
123 +!i10b 1
124 +!s100 OEL;4hO=oB7l=fz0MFd4@3
125 +I;k?z>AFPT82RV0<M7@TV02
126 +R1
127 +R2
128 +w1589611018
129 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Data Memory.v
130 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/Data Memory.v
131 +L0 1
132 +R3
133 +r1
134 +!s85 0
135 +31
136 +R5
137 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Data Memory.v|
138 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Data Memory.v|
139 +!s101 -O0
140 +!i113 1
141 +R4
142 +n@data@memory
143 +vInstructionMemory
144 +!s110 1589714794
145 +!i10b 1
146 +!s100 K@_X5c@ME[5c??9<ZFd<Z1
147 +I7Z2BcWI<YSdPm=L6;9b7=0
148 +R1
149 +R2
150 +w1589714789
151 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/InstructionMemory.v
152 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/InstructionMemory.v
153 +L0 1
154 +R3
155 +r1
156 +!s85 0
157 +31
158 +!s108 1589714794.000000
159 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/InstructionMemory.v|
160 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/InstructionMemory.v|
161 +!s101 -O0
162 +!i113 1
163 +R4
164 +n@instruction@memory
165 +vMux32bit
166 +R0
167 +!i10b 1
168 +!s100 6HOE]5bDRSA[<HBJLTnYP0
169 +InTEk>^`Yjc4Xl2[WnZ3^Y3
170 +R1
171 +R2
172 +Z6 w1589610975
173 +Z7 8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Mux.v
174 +Z8 FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/Mux.v
175 +L0 1
176 +R3
177 +r1
178 +!s85 0
179 +31
180 +R5
181 +Z9 !s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Mux.v|
182 +Z10 !s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Mux.v|
183 +!s101 -O0
184 +!i113 1
185 +R4
186 +n@mux32bit
187 +vMux5bit
188 +R0
189 +!i10b 1
190 +!s100 1oCn>`bHoZ=?A6[JF911T3
191 +I]EmV`Adl6kHglkN9IK>LX2
192 +R1
193 +R2
194 +R6
195 +R7
196 +R8
197 +L0 16
198 +R3
199 +r1
200 +!s85 0
201 +31
202 +R5
203 +R9
204 +R10
205 +!s101 -O0
206 +!i113 1
207 +R4
208 +n@mux5bit
209 +vRegister
210 +R0
211 +!i10b 1
212 +!s100 nIf0^jhJBW[j`45[PBZ8o0
213 +Izh6D:XX;Ak7Z3_7[J=3113
214 +R1
215 +R2
216 +w1589611738
217 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Register.v
218 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/Register.v
219 +L0 1
220 +R3
221 +r1
222 +!s85 0
223 +31
224 +R5
225 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Register.v|
226 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/Register.v|
227 +!s101 -O0
228 +!i113 1
229 +R4
230 +n@register
231 +vShiftLeft2
232 +R0
233 +!i10b 1
234 +!s100 eI5Ec:gWMIfN>mTKQIBY93
235 +IWRY1:Un@<nHGbA7hoKFL[1
236 +R1
237 +R2
238 +w1589586193
239 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ShiftLeft2.v
240 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/ShiftLeft2.v
241 +L0 1
242 +R3
243 +r1
244 +!s85 0
245 +31
246 +R5
247 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ShiftLeft2.v|
248 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/ShiftLeft2.v|
249 +!s101 -O0
250 +!i113 1
251 +R4
252 +n@shift@left2
253 +vSignExtend
254 +Z11 !s110 1589714637
255 +!i10b 1
256 +!s100 ahVKzC^1fD@70fO3WnVUV0
257 +Izf_2?i[S@:;mKbJ:CXF753
258 +R1
259 +R2
260 +w1589586199
261 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/SignExtend.v
262 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/SignExtend.v
263 +L0 1
264 +R3
265 +r1
266 +!s85 0
267 +31
268 +Z12 !s108 1589714637.000000
269 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/SignExtend.v|
270 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/SignExtend.v|
271 +!s101 -O0
272 +!i113 1
273 +R4
274 +n@sign@extend
275 +vtest
276 +R11
277 +!i10b 1
278 +!s100 Sm5D9nHWJl4JV?TA`lU4`0
279 +IW:K2A@A@^WBUal;dbVMEN0
280 +R1
281 +R2
282 +w1589610276
283 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/test.v
284 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/test.v
285 +L0 1
286 +R3
287 +r1
288 +!s85 0
289 +31
290 +R12
291 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/test.v|
292 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/test.v|
293 +!s101 -O0
294 +!i113 1
295 +R4
296 +vtestbench
297 +R11
298 +!i10b 1
299 +!s100 z4HT6K50i`GDM7=h[az0B1
300 +Ik;o@8lk>glJB?9fgUD_;W3
301 +R1
302 +R2
303 +w1589625001
304 +8D:/class/Capstone1/KNW_Project2/Project/SingleCycle/testbench.v
305 +FD:/class/Capstone1/KNW_Project2/Project/SingleCycle/testbench.v
306 +L0 1
307 +R3
308 +r1
309 +!s85 0
310 +31
311 +R12
312 +!s107 D:/class/Capstone1/KNW_Project2/Project/SingleCycle/testbench.v|
313 +!s90 -reportprogress|300|-work|work|-stats=none|D:/class/Capstone1/KNW_Project2/Project/SingleCycle/testbench.v|
314 +!s101 -O0
315 +!i113 1
316 +R4
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
1 +m255
2 +K4
3 +z0
4 +cModel Technology