SAIFIINDUSTRIES commited on
Commit
ca60100
·
verified ·
1 Parent(s): 37c5363

Add Repo: aptx1231_BUAA_CO (part 4)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/ID.v +118 -0
  2. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/MEM.v +91 -0
  3. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/Mult_Div.v +94 -0
  4. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/WB.v +61 -0
  5. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/alu.v +76 -0
  6. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/cmp.v +33 -0
  7. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/controller.v +1330 -0
  8. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/dm.v +89 -0
  9. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/ext.v +36 -0
  10. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/forward_mux.v +117 -0
  11. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/grf.v +59 -0
  12. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/hazardUnit.v +271 -0
  13. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/im.v +35 -0
  14. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/mips.v +46 -0
  15. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/mux.v +78 -0
  16. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/npc.v +63 -0
  17. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/pc.v +47 -0
  18. aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/test.v +48 -0
  19. aptx1231_BUAA_CO/P7/p7_旧/p7_7/Bridge.v +51 -0
  20. aptx1231_BUAA_CO/P7/p7_旧/p7_7/CP0.v +133 -0
  21. aptx1231_BUAA_CO/P7/p7_旧/p7_7/CPU.v +212 -0
  22. aptx1231_BUAA_CO/P7/p7_旧/p7_7/DEV0.v +132 -0
  23. aptx1231_BUAA_CO/P7/p7_旧/p7_7/DEV1.v +131 -0
  24. aptx1231_BUAA_CO/P7/p7_旧/p7_7/DataExt.v +61 -0
  25. aptx1231_BUAA_CO/P7/p7_旧/p7_7/EX.v +92 -0
  26. aptx1231_BUAA_CO/P7/p7_旧/p7_7/ExcCode.v +41 -0
  27. aptx1231_BUAA_CO/P7/p7_旧/p7_7/ID.v +118 -0
  28. aptx1231_BUAA_CO/P7/p7_旧/p7_7/MEM.v +91 -0
  29. aptx1231_BUAA_CO/P7/p7_旧/p7_7/Mult_Div.v +94 -0
  30. aptx1231_BUAA_CO/P7/p7_旧/p7_7/WB.v +61 -0
  31. aptx1231_BUAA_CO/P7/p7_旧/p7_7/alu.v +76 -0
  32. aptx1231_BUAA_CO/P7/p7_旧/p7_7/cmp.v +33 -0
  33. aptx1231_BUAA_CO/P7/p7_旧/p7_7/controller.v +1330 -0
  34. aptx1231_BUAA_CO/P7/p7_旧/p7_7/dm.v +89 -0
  35. aptx1231_BUAA_CO/P7/p7_旧/p7_7/ext.v +36 -0
  36. aptx1231_BUAA_CO/P7/p7_旧/p7_7/forward_mux.v +117 -0
  37. aptx1231_BUAA_CO/P7/p7_旧/p7_7/grf.v +59 -0
  38. aptx1231_BUAA_CO/P7/p7_旧/p7_7/hazardUnit.v +271 -0
  39. aptx1231_BUAA_CO/P7/p7_旧/p7_7/im.v +35 -0
  40. aptx1231_BUAA_CO/P7/p7_旧/p7_7/mips.v +46 -0
  41. aptx1231_BUAA_CO/P7/p7_旧/p7_7/mux.v +78 -0
  42. aptx1231_BUAA_CO/P7/p7_旧/p7_7/npc.v +63 -0
  43. aptx1231_BUAA_CO/P7/p7_旧/p7_7/pc.v +47 -0
  44. aptx1231_BUAA_CO/P7/p7_旧/p7_7/test.v +48 -0
  45. aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/Bridge.v +51 -0
  46. aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/CP0.v +136 -0
  47. aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/CPU.v +212 -0
  48. aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/DEV0.v +132 -0
  49. aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/DEV1.v +131 -0
  50. aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/DataExt.v +61 -0
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/ID.v ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:56:51 11/23/2018
7
+ // Design Name:
8
+ // Module Name: ID
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module ID(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input [31:0] Instr,
26
+ input [31:0] PC,
27
+ output reg[31:0] IR_D,
28
+ output reg[31:0] PC_D,
29
+ output reg[31:0] PC4_D,
30
+ output reg[31:0] PC8_D,
31
+ output [4:0] excode_D
32
+ );
33
+
34
+ `define op 31:26
35
+ `define rs 25:21
36
+ `define rt 20:16
37
+ `define func 5:0
38
+
39
+ `define R 6'b000000
40
+ `define j 6'b000010
41
+ `define lw 6'b100011
42
+ `define lb 6'b100000
43
+ `define lbu 6'b100100
44
+ `define lh 6'b100001
45
+ `define lhu 6'b100101
46
+ `define sw 6'b101011
47
+ `define sb 6'b101000
48
+ `define sh 6'b101001
49
+ `define lui 6'b001111
50
+ `define ori 6'b001101
51
+ `define andi 6'b001100
52
+ `define xori 6'b001110
53
+ `define addi 6'b001000
54
+ `define addiu 6'b001001
55
+ `define slti 6'b001010
56
+ `define sltiu 6'b001011
57
+ `define beq 6'b000100
58
+ `define bne 6'b000101
59
+ `define bgtz 6'b000111
60
+ `define blez 6'b000110
61
+ `define jal 6'b000011
62
+ `define addu 6'b100001
63
+ `define add 6'b100000
64
+ `define subu 6'b100011
65
+ `define sub 6'b100010
66
+ `define and_ 6'b100100
67
+ `define or_ 6'b100101
68
+ `define xor_ 6'b100110
69
+ `define nor_ 6'b100111
70
+ `define sll 6'b000000
71
+ `define srl 6'b000010
72
+ `define sra 6'b000011
73
+ `define sllv 6'b000100
74
+ `define srlv 6'b000110
75
+ `define srav 6'b000111
76
+ `define jr 6'b001000
77
+ `define jalr 6'b001001
78
+ `define mult 6'b011000
79
+ `define multu 6'b011001
80
+ `define div 6'b011010
81
+ `define divu 6'b011011
82
+ `define mfhi 6'b010000
83
+ `define mflo 6'b010010
84
+ `define mthi 6'b010001
85
+ `define mtlo 6'b010011
86
+ `define slt 6'b101010
87
+ `define sltu 6'b101011
88
+
89
+ initial begin
90
+ IR_D <= 0;
91
+ end
92
+
93
+ wire RI;//�����쳣 ����ʶ��op func
94
+ assign RI = (IR_D[`op]==`j)||(IR_D[`op]==`jal)
95
+ ||(IR_D[`op]==`beq)||(IR_D[`op]==`bne)||(IR_D[`op]==`blez)||(IR_D[`op]==`bgtz)||(IR_D[`op]==6'b000001&&IR_D[`rt]==5'b00000)||(IR_D[`op]==6'b000001&&IR_D[`rt]==5'b00001)
96
+ ||IR_D[`op]==`lui||IR_D[`op]==`ori||IR_D[`op]==`addi||IR_D[`op]==`addiu||IR_D[`op]==`andi||IR_D[`op]==`xori||IR_D[`op]==`slti||IR_D[`op]==`sltiu
97
+ ||IR_D[`op]==`lw||IR_D[`op]==`lb||IR_D[`op]==`lbu||IR_D[`op]==`lh||IR_D[`op]==`lhu
98
+ ||IR_D[`op]==`sw||IR_D[`op]==`sh||IR_D[`op]==`sb
99
+ ||(IR_D[`op]==6'b010000&&(IR_D[`rs]==5'b00000||IR_D[`rs]==5'b00100))
100
+ ||(IR_D==32'h42000018)
101
+ ||(IR_D[`op]==`R&&(IR_D[`func]==`jr||IR_D[`func]==`jalr||IR_D[`func]==`addu||IR_D[`func]==`subu||IR_D[`func]==`add||IR_D[`func]==`sub||IR_D[`func]==`sll||IR_D[`func]==`srl||IR_D[`func]==`sra
102
+ ||IR_D[`func]==`sllv||IR_D[`func]==`srlv||IR_D[`func]==`srav||IR_D[`func]==`and_||IR_D[`func]==`or_||IR_D[`func]==`nor_||IR_D[`func]==`xor_||IR_D[`func]==`slt||IR_D[`func]==`sltu
103
+ ||IR_D[`func]==`mult||IR_D[`func]==`multu||IR_D[`func]==`div||IR_D[`func]==`divu||IR_D[`func]==`mfhi||IR_D[`func]==`mflo||IR_D[`func]==`mthi||IR_D[`func]==`mtlo));
104
+
105
+ assign excode_D = (RI==0) ? 10 : 0;
106
+
107
+ always@(posedge clk) begin
108
+ if(reset) begin
109
+ IR_D <= 32'h00000000;
110
+ end
111
+ else if(en) begin
112
+ IR_D <= Instr;
113
+ PC_D <= PC;
114
+ PC4_D <= PC+4;
115
+ PC8_D <= PC+8;
116
+ end
117
+ end
118
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/MEM.v ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:27:06 11/23/2018
7
+ // Design Name:
8
+ // Module Name: MEM
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module MEM(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input [31:0] IR_E,
26
+ input [31:0] PC_E,
27
+ input [31:0] PC4_E,
28
+ input [31:0] PC8_E,
29
+ input [31:0] ALU,
30
+ input [31:0] Mult_Div,
31
+ input [31:0] RT_E,
32
+ input Zero_E,
33
+ input more_E,
34
+ input less_E,
35
+ output reg[31:0] IR_M,
36
+ output reg[31:0] PC_M,
37
+ output reg[31:0] PC4_M,
38
+ output reg[31:0] PC8_M,
39
+ output reg[31:0] AO_M,
40
+ output reg[31:0] MDO_M,
41
+ output reg[31:0] RT_M,
42
+ output [6:2] excode_M,
43
+ output reg Zero_M,
44
+ output reg more_M,
45
+ output reg less_M
46
+ );
47
+
48
+ `define op 31:26
49
+ `define lw 6'b100011
50
+ `define lb 6'b100000
51
+ `define lbu 6'b100100
52
+ `define lh 6'b100001
53
+ `define lhu 6'b100101
54
+ `define sw 6'b101011
55
+ `define sb 6'b101000
56
+ `define sh 6'b101001
57
+
58
+ initial begin
59
+ IR_M <= 0;
60
+ end
61
+
62
+ assign excode_M = ((IR_M[`op]==`lw&&AO_M[1:0]!=2'b00)||(IR_M[`op]==`lh&&AO_M[0]!=1'b0)||(IR_M[`op]==`lhu&&AO_M[0]!=1'b0)||
63
+ ((IR_M[`op]==`lw||IR_M[`op]==`lb||IR_M[`op]==`lbu||IR_M[`op]==`lh||IR_M[`op]==`lhu)&&(AO_M<32'h00000000||
64
+ (AO_M>32'h00002fff&&AO_M<32'h00007f00)||(AO_M>32'h00007f0b&&AO_M<32'h00007f10)||AO_M>32'h00007f1b))||
65
+ ((IR_M[`op]==`lb||IR_M[`op]==`lbu||IR_M[`op]==`lh||IR_M[`op]==`lhu)&&((AO_M>=32'h00007f00&&AO_M<=32'h00007f0b)||
66
+ (AO_M>=32'h00007f10&&AO_M<=32'h00007f1b)))) ? 4 :
67
+ ((IR_M[`op]==`sw&&AO_M[1:0]!=2'b00)||(IR_M[`op]==`sh&&AO_M[0]!=1'b0)||
68
+ ((IR_M[`op]==`sw||IR_M[`op]==`sb||IR_M[`op]==`sh)&&(AO_M<32'h00000000||
69
+ (AO_M>32'h00002fff&&AO_M<32'h00007f00)||(AO_M>32'h00007f07&&AO_M<32'h00007f10)||AO_M>32'h00007f1b))|| //������COUNTд 7f07
70
+ ((IR_M[`op]==`sb||IR_M[`op]==`sh)&&((AO_M>=32'h00007f00&&AO_M<=32'h00007f0b)||
71
+ (AO_M>=32'h00007f10&&AO_M<=32'h00007f1b)))) ? 5 : 0;
72
+
73
+ always@(posedge clk) begin
74
+ if(reset) begin
75
+ IR_M <= 32'h00000000;
76
+ end
77
+ else if(en) begin
78
+ IR_M <= IR_E;
79
+ PC_M <= PC_E;
80
+ PC4_M <= PC4_E;
81
+ PC8_M <= PC8_E;
82
+ AO_M <= ALU;
83
+ MDO_M <= Mult_Div;
84
+ RT_M <= RT_E;
85
+ Zero_M <= Zero_E;
86
+ more_M <= more_E;
87
+ less_M <= less_E;
88
+ end
89
+ end
90
+
91
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/Mult_Div.v ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 11:13:10 11/30/2018
7
+ // Design Name:
8
+ // Module Name: Mult_Div
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module Mult_Div(
22
+ input clk,
23
+ input reset,
24
+ input [31:0] A,
25
+ input [31:0] B,
26
+ input [1:0] op,
27
+ input start,
28
+ input if_mthi,
29
+ input if_mtlo,
30
+ output reg Busy,
31
+ output [31:0] High,
32
+ output [31:0] Low
33
+ );
34
+ reg [31:0] Hi;
35
+ reg [31:0] Lo;
36
+ assign High = Hi;
37
+ assign Low = Lo;
38
+ reg[31:0] cycles;
39
+ reg [63:0] tmp;
40
+
41
+ initial begin
42
+ Hi<=0;
43
+ Lo<=0;
44
+ cycles=0;
45
+ Busy<=0;
46
+ end
47
+
48
+ always@(posedge clk) begin
49
+ if(reset) begin
50
+ Hi<=0;
51
+ Lo<=0;
52
+ cycles=0;
53
+ Busy<=0;
54
+ end
55
+ else if(!Busy) begin
56
+ if(start) begin
57
+ case(op)
58
+ 2'b00: begin//mult
59
+ tmp<=$signed(A)*$signed(B);
60
+ cycles=5;
61
+ end
62
+ 2'b01: begin//multu
63
+ tmp<=A*B;
64
+ cycles=5;
65
+ end
66
+ 2'b10: begin//div
67
+ tmp<={$signed(A)%$signed(B),$signed(A)/$signed(B)};
68
+ cycles=10;
69
+ end
70
+ 2'b11: begin//divu
71
+ tmp<={A%B,A/B};
72
+ cycles=10;
73
+ end
74
+ endcase
75
+ Busy<=1;
76
+ end
77
+ if(if_mthi) begin
78
+ Hi<=A;
79
+ end
80
+ if(if_mtlo) begin
81
+ Lo<=A;
82
+ end
83
+ end
84
+ else if(Busy) begin
85
+ if(cycles>0) begin
86
+ cycles=cycles-1;
87
+ if(cycles==0) begin
88
+ {Hi,Lo}<=tmp;
89
+ Busy<=0;
90
+ end
91
+ end
92
+ end
93
+ end
94
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/WB.v ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:30:22 11/23/2018
7
+ // Design Name:
8
+ // Module Name: WB
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module WB(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input [31:0] IR_M,
26
+ input [31:0] PC_M,
27
+ input [31:0] PC4_M,
28
+ input [31:0] PC8_M,
29
+ input [31:0] AO_M,
30
+ input [31:0] MDO_M,
31
+ input [31:0] DM,
32
+ input [31:0] CP0_M,
33
+ output reg[31:0] IR_W,
34
+ output reg[31:0] PC_W,
35
+ output reg[31:0] PC4_W,
36
+ output reg[31:0] PC8_W,
37
+ output reg[31:0] AO_W,
38
+ output reg[31:0] MDO_W,
39
+ output reg[31:0] DR_W,
40
+ output reg[31:0] CP0_W
41
+ );
42
+ initial begin
43
+ IR_W <= 0;
44
+ end
45
+ always@(posedge clk) begin
46
+ if(reset) begin
47
+ IR_W <= 32'h00000000;
48
+ end
49
+ else if(en) begin
50
+ IR_W <= IR_M;
51
+ PC_W <= PC_M;
52
+ PC4_W <= PC4_M;
53
+ PC8_W <= PC8_M;
54
+ AO_W <= AO_M;
55
+ MDO_W <= MDO_M;
56
+ DR_W <= DM;
57
+ CP0_W <= CP0_M;
58
+ end
59
+ end
60
+
61
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/alu.v ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:01:30 11/23/2018
7
+ // Design Name:
8
+ // Module Name: alu
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module alu(
22
+ input [31:0] A,
23
+ input [31:0] B,
24
+ input [3:0] ALUCtrl,
25
+ output reg[31:0] Result,
26
+ output over
27
+ );
28
+
29
+ reg[32:0] temp;
30
+
31
+ assign over = (temp[32]!=temp[31]);
32
+
33
+ always@(*) begin
34
+ case(ALUCtrl)
35
+ 4'b0000: begin
36
+ Result = A & B;
37
+ end
38
+ 4'b0001: begin
39
+ Result = A | B;
40
+ end
41
+ 4'b0010: begin
42
+ temp = {A[31],A[31:0]} + {B[31],B[31:0]};
43
+ Result = A + B;
44
+ end
45
+ 4'b0011: begin
46
+ temp = {A[31],A[31:0]} - {B[31],B[31:0]};
47
+ Result = A - B;
48
+ end
49
+ 4'b0100: begin
50
+ Result = A^B;
51
+ end
52
+ 4'b0101: begin
53
+ Result = ~(A|B);
54
+ end
55
+ 4'b0110: begin//sll sllv �߼���
56
+ Result = B << A[4:0];
57
+ end
58
+ 4'b0111: begin//srl srlv �߼���
59
+ Result = B >> A[4:0];
60
+ end
61
+ 4'b1000: begin//sra srav ������
62
+ Result = $signed($signed(B) >>> A[4:0]);
63
+ end
64
+ 4'b1001: begin
65
+ Result = ($signed(A)<$signed(B)) ? 32'b1 : 32'b0;
66
+ end
67
+ 4'b1010: begin
68
+ Result = (A<B) ? 32'b1 : 32'b0;
69
+ end
70
+ default: begin
71
+ Result = 0;
72
+ end
73
+ endcase
74
+ end
75
+
76
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/cmp.v ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:11:38 11/23/2018
7
+ // Design Name:
8
+ // Module Name: cmp
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module cmp(
22
+ input [31:0] D1,
23
+ input [31:0] D2,
24
+ output Equal,
25
+ output more,
26
+ output less
27
+ );
28
+
29
+ assign Equal = (D1==D2) ? 1'b1 : 1'b0;
30
+ assign more = ($signed(D1)>0) ? 1'b1 : 1'b0;
31
+ assign less = ($signed(D1)<0) ? 1'b1 : 1'b0;
32
+
33
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/controller.v ADDED
@@ -0,0 +1,1330 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 11:53:56 11/24/2018
7
+ // Design Name:
8
+ // Module Name: controller
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module controller(
22
+ input [5:0] op,
23
+ input [5:0] func,
24
+ input [4:0] rs,
25
+ input [4:0] rt,
26
+ output reg[3:0] ALUCtrl,
27
+ output reg[1:0] RegDst,
28
+ output reg ALUASrc,
29
+ output reg ALUBSrc,
30
+ output reg RegWrite,
31
+ output reg MemRead,
32
+ output reg MemWrite,
33
+ output reg [2:0] MemtoReg,
34
+ output reg [1:0]ExtOp,
35
+ output reg if_beq,
36
+ output reg if_bne,
37
+ output reg if_blez,
38
+ output reg if_bgez,
39
+ output reg if_bltz,
40
+ output reg if_bgtz,
41
+ output reg if_j,
42
+ output reg [1:0]PCsel,
43
+ output reg if_sh,
44
+ output reg if_sb,
45
+ output reg[2:0] dataOp,
46
+ output reg[1:0] multdivOp,
47
+ output reg start,
48
+ output reg if_mthi,
49
+ output reg if_mtlo,
50
+ output reg if_mfhi,
51
+ output reg if_mflo,
52
+ output reg if_1,
53
+ output reg if_2,
54
+ output reg if_3,
55
+ output reg cp0WE
56
+ );
57
+ initial begin
58
+ PCsel <= 2'b00;
59
+ end
60
+ always@(*)
61
+ begin
62
+ case (op)
63
+ 6'b000000://R
64
+ begin
65
+ RegDst<=2'b01;
66
+ ALUBSrc<=0;
67
+ MemRead<=0;
68
+ MemWrite<=0;
69
+ ExtOp<=2'b00;
70
+ if_beq<=0;
71
+ if_bne<=0;
72
+ if_bgtz<=0;
73
+ if_blez<=0;
74
+ if_bgez<=0;
75
+ if_bltz<=0;
76
+ if_sh<=0;
77
+ if_sb<=0;
78
+ if_j<=0;
79
+ dataOp<=3'b000;
80
+ if_1<=0;
81
+ if_2<=0;
82
+ if_3<=0;
83
+ cp0WE<=0;
84
+ case(func)
85
+ 6'b100001: begin //addu
86
+ ALUASrc<=0;
87
+ MemtoReg<=3'b000;
88
+ ALUCtrl<=4'b0010;
89
+ PCsel<=2'b00;
90
+ RegWrite<=1;
91
+ multdivOp<=2'b00;
92
+ start<=0;
93
+ if_mthi<=0;
94
+ if_mtlo<=0;
95
+ if_mfhi<=0;
96
+ if_mflo<=0;
97
+ end
98
+ 6'b100000: begin //add
99
+ ALUASrc<=0;
100
+ MemtoReg<=3'b000;
101
+ ALUCtrl<=4'b0010;
102
+ PCsel<=2'b00;
103
+ RegWrite<=1;
104
+ multdivOp<=2'b00;
105
+ start<=0;
106
+ if_mthi<=0;
107
+ if_mtlo<=0;
108
+ if_mfhi<=0;
109
+ if_mflo<=0;
110
+ end
111
+ 6'b100011: begin //subu
112
+ ALUASrc<=0;
113
+ MemtoReg<=3'b000;
114
+ ALUCtrl<=4'b0011;
115
+ PCsel<=2'b00;
116
+ RegWrite<=1;
117
+ multdivOp<=2'b00;
118
+ start<=0;
119
+ if_mthi<=0;
120
+ if_mtlo<=0;
121
+ if_mfhi<=0;
122
+ if_mflo<=0;
123
+ end
124
+ 6'b100010: begin //sub
125
+ ALUASrc<=0;
126
+ MemtoReg<=3'b000;
127
+ ALUCtrl<=4'b0011;
128
+ PCsel<=2'b00;
129
+ RegWrite<=1;
130
+ multdivOp<=2'b00;
131
+ start<=0;
132
+ if_mthi<=0;
133
+ if_mtlo<=0;
134
+ if_mfhi<=0;
135
+ if_mflo<=0;
136
+ end
137
+ 6'b100100: begin //and
138
+ ALUASrc<=0;
139
+ MemtoReg<=3'b000;
140
+ ALUCtrl<=4'b0000;
141
+ PCsel<=2'b00;
142
+ RegWrite<=1;
143
+ multdivOp<=2'b00;
144
+ start<=0;
145
+ if_mthi<=0;
146
+ if_mtlo<=0;
147
+ if_mfhi<=0;
148
+ if_mflo<=0;
149
+ end
150
+ 6'b100101: begin //or
151
+ ALUASrc<=0;
152
+ MemtoReg<=3'b000;
153
+ ALUCtrl<=4'b0001;
154
+ PCsel<=2'b00;
155
+ RegWrite<=1;
156
+ multdivOp<=2'b00;
157
+ start<=0;
158
+ if_mthi<=0;
159
+ if_mtlo<=0;
160
+ if_mfhi<=0;
161
+ if_mflo<=0;
162
+ end
163
+ 6'b100110: begin //xor
164
+ ALUASrc<=0;
165
+ MemtoReg<=3'b000;
166
+ ALUCtrl<=4'b0100;
167
+ PCsel<=2'b00;
168
+ RegWrite<=1;
169
+ multdivOp<=2'b00;
170
+ start<=0;
171
+ if_mthi<=0;
172
+ if_mtlo<=0;
173
+ if_mfhi<=0;
174
+ if_mflo<=0;
175
+ end
176
+ 6'b100111: begin //nor
177
+ ALUASrc<=0;
178
+ MemtoReg<=3'b000;
179
+ ALUCtrl<=4'b0101;
180
+ PCsel<=2'b00;
181
+ RegWrite<=1;
182
+ multdivOp<=2'b00;
183
+ start<=0;
184
+ if_mthi<=0;
185
+ if_mtlo<=0;
186
+ if_mfhi<=0;
187
+ if_mflo<=0;
188
+ end
189
+ 6'b001000: begin //jr
190
+ ALUASrc<=0;
191
+ MemtoReg<=3'b000;
192
+ ALUCtrl<=4'b0000;
193
+ PCsel<=2'b01;
194
+ RegWrite<=1;
195
+ multdivOp<=2'b00;
196
+ start<=0;
197
+ if_mthi<=0;
198
+ if_mtlo<=0;
199
+ if_mfhi<=0;
200
+ if_mflo<=0;
201
+ end
202
+ 6'b001001: begin //jalr
203
+ ALUASrc<=0;
204
+ MemtoReg<=3'b010;
205
+ ALUCtrl<=4'b0000;
206
+ PCsel<=2'b01;
207
+ RegWrite<=1;
208
+ multdivOp<=2'b00;
209
+ start<=0;
210
+ if_mthi<=0;
211
+ if_mtlo<=0;
212
+ if_mfhi<=0;
213
+ if_mflo<=0;
214
+ end
215
+ 6'b000000: begin //sll nop
216
+ ALUASrc<=1;
217
+ MemtoReg<=3'b000;
218
+ ALUCtrl<=4'b0110;
219
+ PCsel<=2'b00;
220
+ RegWrite<=1;
221
+ multdivOp<=2'b00;
222
+ start<=0;
223
+ if_mthi<=0;
224
+ if_mtlo<=0;
225
+ if_mfhi<=0;
226
+ if_mflo<=0;
227
+ end
228
+ 6'b000100: begin //sllv
229
+ ALUASrc<=0;
230
+ MemtoReg<=3'b000;
231
+ ALUCtrl<=4'b0110;
232
+ PCsel<=2'b00;
233
+ RegWrite<=1;
234
+ multdivOp<=2'b00;
235
+ start<=0;
236
+ if_mthi<=0;
237
+ if_mtlo<=0;
238
+ if_mfhi<=0;
239
+ if_mflo<=0;
240
+ end
241
+ 6'b000010: begin //srl
242
+ ALUASrc<=1;
243
+ MemtoReg<=3'b000;
244
+ ALUCtrl<=4'b0111;
245
+ PCsel<=2'b00;
246
+ RegWrite<=1;
247
+ multdivOp<=2'b00;
248
+ start<=0;
249
+ if_mthi<=0;
250
+ if_mtlo<=0;
251
+ if_mfhi<=0;
252
+ if_mflo<=0;
253
+ end
254
+ 6'b000110: begin //srlv
255
+ ALUASrc<=0;
256
+ MemtoReg<=3'b000;
257
+ ALUCtrl<=4'b0111;
258
+ PCsel<=2'b00;
259
+ RegWrite<=1;
260
+ multdivOp<=2'b00;
261
+ start<=0;
262
+ if_mthi<=0;
263
+ if_mtlo<=0;
264
+ if_mfhi<=0;
265
+ if_mflo<=0;
266
+ end
267
+ 6'b000011: begin //sra
268
+ ALUASrc<=1;
269
+ MemtoReg<=3'b000;
270
+ ALUCtrl<=4'b1000;
271
+ PCsel<=2'b00;
272
+ RegWrite<=1;
273
+ multdivOp<=2'b00;
274
+ start<=0;
275
+ if_mthi<=0;
276
+ if_mtlo<=0;
277
+ if_mfhi<=0;
278
+ if_mflo<=0;
279
+ end
280
+ 6'b000111: begin //srav
281
+ ALUASrc<=0;
282
+ MemtoReg<=3'b000;
283
+ ALUCtrl<=4'b1000;
284
+ PCsel<=2'b00;
285
+ RegWrite<=1;
286
+ multdivOp<=2'b00;
287
+ start<=0;
288
+ if_mthi<=0;
289
+ if_mtlo<=0;
290
+ if_mfhi<=0;
291
+ if_mflo<=0;
292
+ end
293
+ 6'b101010: begin //slt
294
+ ALUASrc<=0;
295
+ MemtoReg<=3'b000;
296
+ ALUCtrl<=4'b1001;
297
+ PCsel<=2'b00;
298
+ RegWrite<=1;
299
+ multdivOp<=2'b00;
300
+ start<=0;
301
+ if_mthi<=0;
302
+ if_mtlo<=0;
303
+ if_mfhi<=0;
304
+ if_mflo<=0;
305
+ end
306
+ 6'b101011: begin //sltu
307
+ ALUASrc<=0;
308
+ MemtoReg<=3'b000;
309
+ ALUCtrl<=4'b1010;
310
+ PCsel<=2'b00;
311
+ RegWrite<=1;
312
+ multdivOp<=2'b00;
313
+ start<=0;
314
+ if_mthi<=0;
315
+ if_mtlo<=0;
316
+ if_mfhi<=0;
317
+ if_mflo<=0;
318
+ end
319
+ 6'b011000: begin //mult
320
+ ALUASrc<=0;
321
+ MemtoReg<=3'b000;
322
+ ALUCtrl<=4'b0000;
323
+ PCsel<=2'b00;
324
+ RegWrite<=0;
325
+ multdivOp<=2'b00;
326
+ start<=1;
327
+ if_mthi<=0;
328
+ if_mtlo<=0;
329
+ if_mfhi<=0;
330
+ if_mflo<=0;
331
+ end
332
+ 6'b011001: begin //multu
333
+ ALUASrc<=0;
334
+ MemtoReg<=3'b000;
335
+ ALUCtrl<=4'b0000;
336
+ PCsel<=2'b00;
337
+ RegWrite<=0;
338
+ multdivOp<=2'b01;
339
+ start<=1;
340
+ if_mthi<=0;
341
+ if_mtlo<=0;
342
+ if_mfhi<=0;
343
+ if_mflo<=0;
344
+ end
345
+ 6'b011010: begin //div
346
+ ALUASrc<=0;
347
+ MemtoReg<=3'b000;
348
+ ALUCtrl<=4'b0000;
349
+ PCsel<=2'b00;
350
+ RegWrite<=0;
351
+ multdivOp<=2'b10;
352
+ start<=1;
353
+ if_mthi<=0;
354
+ if_mtlo<=0;
355
+ if_mfhi<=0;
356
+ if_mflo<=0;
357
+ end
358
+ 6'b011011: begin //divu
359
+ ALUASrc<=0;
360
+ MemtoReg<=3'b000;
361
+ ALUCtrl<=4'b0000;
362
+ PCsel<=2'b00;
363
+ RegWrite<=0;
364
+ multdivOp<=2'b11;
365
+ start<=1;
366
+ if_mthi<=0;
367
+ if_mtlo<=0;
368
+ if_mfhi<=0;
369
+ if_mflo<=0;
370
+ end
371
+ 6'b010000: begin //mfhi
372
+ ALUASrc<=0;
373
+ MemtoReg<=3'b011;
374
+ ALUCtrl<=4'b0000;
375
+ PCsel<=2'b00;
376
+ RegWrite<=1;
377
+ multdivOp<=2'b00;
378
+ start<=0;
379
+ if_mthi<=0;
380
+ if_mtlo<=0;
381
+ if_mfhi<=1;
382
+ if_mflo<=0;
383
+ end
384
+ 6'b010010: begin //mflo
385
+ ALUASrc<=0;
386
+ MemtoReg<=3'b011;
387
+ ALUCtrl<=4'b0000;
388
+ PCsel<=2'b00;
389
+ RegWrite<=1;
390
+ multdivOp<=2'b00;
391
+ start<=0;
392
+ if_mthi<=0;
393
+ if_mtlo<=0;
394
+ if_mfhi<=0;
395
+ if_mflo<=1;
396
+ end
397
+ 6'b010001: begin //mthi
398
+ ALUASrc<=0;
399
+ MemtoReg<=3'b000;
400
+ ALUCtrl<=4'b0000;
401
+ PCsel<=2'b00;
402
+ RegWrite<=0;
403
+ multdivOp<=2'b00;
404
+ start<=0;
405
+ if_mthi<=1;
406
+ if_mtlo<=0;
407
+ if_mfhi<=0;
408
+ if_mflo<=0;
409
+ end
410
+ 6'b010011: begin //mtlo
411
+ ALUASrc<=0;
412
+ MemtoReg<=3'b000;
413
+ ALUCtrl<=4'b0000;
414
+ PCsel<=2'b00;
415
+ RegWrite<=0;
416
+ multdivOp<=2'b00;
417
+ start<=0;
418
+ if_mthi<=0;
419
+ if_mtlo<=1;
420
+ if_mfhi<=0;
421
+ if_mflo<=0;
422
+ end
423
+ default: begin
424
+ ALUASrc<=0;
425
+ MemtoReg<=3'b000;
426
+ ALUCtrl<=4'b0000;
427
+ PCsel<=2'b00;
428
+ RegWrite<=1;
429
+ multdivOp<=2'b00;
430
+ start<=0;
431
+ if_mthi<=0;
432
+ if_mtlo<=0;
433
+ if_mfhi<=0;
434
+ if_mflo<=0;
435
+ end
436
+ endcase
437
+ end
438
+ 6'b100011://lw
439
+ begin
440
+ RegDst<=2'b00;
441
+ ALUASrc<=0;
442
+ ALUBSrc<=1;
443
+ RegWrite<=1;
444
+ MemRead<=1;
445
+ MemWrite<=0;
446
+ MemtoReg<=3'b001;
447
+ ExtOp<=2'b00;
448
+ if_beq<=0;
449
+ if_bne<=0;
450
+ if_bgtz<=0;
451
+ if_blez<=0;
452
+ if_bgez<=0;
453
+ if_bltz<=0;
454
+ ALUCtrl<=4'b0010;
455
+ if_j<=0;
456
+ PCsel<=2'b00;
457
+ if_sh<=0;
458
+ if_sb<=0;
459
+ dataOp<=3'b000;
460
+ multdivOp<=2'b00;
461
+ start<=0;
462
+ if_mthi<=0;
463
+ if_mtlo<=0;
464
+ if_mfhi<=0;
465
+ if_mflo<=0;
466
+ if_1<=0;
467
+ if_2<=0;
468
+ if_3<=0;
469
+ cp0WE<=0;
470
+ end
471
+
472
+ 6'b100000://lb
473
+ begin
474
+ RegDst<=2'b00;
475
+ ALUASrc<=0;
476
+ ALUBSrc<=1;
477
+ RegWrite<=1;
478
+ MemRead<=1;
479
+ MemWrite<=0;
480
+ MemtoReg<=3'b001;
481
+ ExtOp<=2'b00;
482
+ if_beq<=0;
483
+ if_bne<=0;
484
+ if_bgtz<=0;
485
+ if_blez<=0;
486
+ if_bgez<=0;
487
+ if_bltz<=0;
488
+ ALUCtrl<=4'b0010;
489
+ if_j<=0;
490
+ PCsel<=2'b00;
491
+ if_sh<=0;
492
+ if_sb<=0;
493
+ dataOp<=3'b010;
494
+ multdivOp<=2'b00;
495
+ start<=0;
496
+ if_mthi<=0;
497
+ if_mtlo<=0;
498
+ if_mfhi<=0;
499
+ if_mflo<=0;
500
+ if_1<=0;
501
+ if_2<=0;
502
+ if_3<=0;
503
+ cp0WE<=0;
504
+ end
505
+
506
+ 6'b100100://lbu
507
+ begin
508
+ RegDst<=2'b00;
509
+ ALUASrc<=0;
510
+ ALUBSrc<=1;
511
+ RegWrite<=1;
512
+ MemRead<=1;
513
+ MemWrite<=0;
514
+ MemtoReg<=3'b001;
515
+ ExtOp<=2'b00;
516
+ if_beq<=0;
517
+ if_bne<=0;
518
+ if_bgtz<=0;
519
+ if_blez<=0;
520
+ if_bgez<=0;
521
+ if_bltz<=0;
522
+ ALUCtrl<=4'b0010;
523
+ if_j<=0;
524
+ PCsel<=2'b00;
525
+ if_sh<=0;
526
+ if_sb<=0;
527
+ dataOp<=3'b001;
528
+ multdivOp<=2'b00;
529
+ start<=0;
530
+ if_mthi<=0;
531
+ if_mtlo<=0;
532
+ if_mfhi<=0;
533
+ if_mflo<=0;
534
+ if_1<=0;
535
+ if_2<=0;
536
+ if_3<=0;
537
+ cp0WE<=0;
538
+ end
539
+
540
+ 6'b100001://lh
541
+ begin
542
+ RegDst<=2'b00;
543
+ ALUASrc<=0;
544
+ ALUBSrc<=1;
545
+ RegWrite<=1;
546
+ MemRead<=1;
547
+ MemWrite<=0;
548
+ MemtoReg<=3'b001;
549
+ ExtOp<=2'b00;
550
+ if_beq<=0;
551
+ if_bne<=0;
552
+ if_bgtz<=0;
553
+ if_blez<=0;
554
+ if_bgez<=0;
555
+ if_bltz<=0;
556
+ ALUCtrl<=4'b0010;
557
+ if_j<=0;
558
+ PCsel<=2'b00;
559
+ if_sh<=0;
560
+ if_sb<=0;
561
+ dataOp<=3'b100;
562
+ multdivOp<=2'b00;
563
+ start<=0;
564
+ if_mthi<=0;
565
+ if_mtlo<=0;
566
+ if_mfhi<=0;
567
+ if_mflo<=0;
568
+ if_1<=0;
569
+ if_2<=0;
570
+ if_3<=0;
571
+ cp0WE<=0;
572
+ end
573
+
574
+ 6'b100101://lhu
575
+ begin
576
+ RegDst<=2'b00;
577
+ ALUASrc<=0;
578
+ ALUBSrc<=1;
579
+ RegWrite<=1;
580
+ MemRead<=1;
581
+ MemWrite<=0;
582
+ MemtoReg<=3'b001;
583
+ ExtOp<=2'b00;
584
+ if_beq<=0;
585
+ if_bne<=0;
586
+ if_bgtz<=0;
587
+ if_blez<=0;
588
+ if_bgez<=0;
589
+ if_bltz<=0;
590
+ ALUCtrl<=4'b0010;
591
+ if_j<=0;
592
+ PCsel<=2'b00;
593
+ if_sh<=0;
594
+ if_sb<=0;
595
+ dataOp<=3'b011;
596
+ multdivOp<=2'b00;
597
+ start<=0;
598
+ if_mthi<=0;
599
+ if_mtlo<=0;
600
+ if_mfhi<=0;
601
+ if_mflo<=0;
602
+ if_1<=0;
603
+ if_2<=0;
604
+ if_3<=0;
605
+ cp0WE<=0;
606
+ end
607
+
608
+ 6'b101011://sw
609
+ begin
610
+ RegDst<=2'b00;
611
+ ALUASrc<=0;
612
+ ALUBSrc<=1;
613
+ RegWrite<=0;
614
+ MemRead<=0;
615
+ MemWrite<=1;
616
+ MemtoReg<=3'b000;
617
+ ExtOp<=2'b00;
618
+ if_beq<=0;
619
+ if_bne<=0;
620
+ if_bgtz<=0;
621
+ if_blez<=0;
622
+ if_bgez<=0;
623
+ if_bltz<=0;
624
+ ALUCtrl<=4'b0010;
625
+ if_j<=0;
626
+ PCsel<=2'b00;
627
+ if_sh<=0;
628
+ if_sb<=0;
629
+ dataOp<=3'b000;
630
+ multdivOp<=2'b00;
631
+ start<=0;
632
+ if_mthi<=0;
633
+ if_mtlo<=0;
634
+ if_mfhi<=0;
635
+ if_mflo<=0;
636
+ if_1<=0;
637
+ if_2<=0;
638
+ if_3<=0;
639
+ cp0WE<=0;
640
+ end
641
+
642
+ 6'b101001://sh
643
+ begin
644
+ RegDst<=2'b00;
645
+ ALUASrc<=0;
646
+ ALUBSrc<=1;
647
+ RegWrite<=0;
648
+ MemRead<=0;
649
+ MemWrite<=1;
650
+ MemtoReg<=3'b000;
651
+ ExtOp<=2'b00;
652
+ if_beq<=0;
653
+ if_bne<=0;
654
+ if_bgtz<=0;
655
+ if_blez<=0;
656
+ if_bgez<=0;
657
+ if_bltz<=0;
658
+ ALUCtrl<=4'b0010;
659
+ if_j<=0;
660
+ PCsel<=2'b00;
661
+ if_sh<=1;
662
+ if_sb<=0;
663
+ dataOp<=3'b000;
664
+ multdivOp<=2'b00;
665
+ start<=0;
666
+ if_mthi<=0;
667
+ if_mtlo<=0;
668
+ if_mfhi<=0;
669
+ if_mflo<=0;
670
+ if_1<=0;
671
+ if_2<=0;
672
+ if_3<=0;
673
+ cp0WE<=0;
674
+ end
675
+
676
+ 6'b101000://sb
677
+ begin
678
+ RegDst<=2'b00;
679
+ ALUASrc<=0;
680
+ ALUBSrc<=1;
681
+ RegWrite<=0;
682
+ MemRead<=0;
683
+ MemWrite<=1;
684
+ MemtoReg<=3'b000;
685
+ ExtOp<=2'b00;
686
+ if_beq<=0;
687
+ if_bne<=0;
688
+ if_bgtz<=0;
689
+ if_blez<=0;
690
+ if_bgez<=0;
691
+ if_bltz<=0;
692
+ ALUCtrl<=4'b0010;
693
+ if_j<=0;
694
+ PCsel<=2'b00;
695
+ if_sh<=0;
696
+ if_sb<=1;
697
+ dataOp<=3'b000;
698
+ multdivOp<=2'b00;
699
+ start<=0;
700
+ if_mthi<=0;
701
+ if_mtlo<=0;
702
+ if_mfhi<=0;
703
+ if_mflo<=0;
704
+ if_1<=0;
705
+ if_2<=0;
706
+ if_3<=0;
707
+ cp0WE<=0;
708
+ end
709
+
710
+ 6'b000100://beq
711
+ begin
712
+ RegDst<=2'b00;
713
+ ALUASrc<=0;
714
+ ALUBSrc<=0;
715
+ RegWrite<=0;
716
+ MemRead<=0;
717
+ MemWrite<=0;
718
+ MemtoReg<=3'b000;
719
+ ExtOp<=2'b00;
720
+ if_beq<=1;
721
+ if_bne<=0;
722
+ if_bgtz<=0;
723
+ if_blez<=0;
724
+ if_bgez<=0;
725
+ if_bltz<=0;
726
+ ALUCtrl<=4'b0000;
727
+ if_j<=0;
728
+ PCsel<=2'b10;
729
+ if_sh<=0;
730
+ if_sb<=0;
731
+ dataOp<=3'b000;
732
+ multdivOp<=2'b00;
733
+ start<=0;
734
+ if_mthi<=0;
735
+ if_mtlo<=0;
736
+ if_mfhi<=0;
737
+ if_mflo<=0;
738
+ if_1<=0;
739
+ if_2<=0;
740
+ if_3<=0;
741
+ cp0WE<=0;
742
+ end
743
+
744
+ 6'b000101://bne
745
+ begin
746
+ RegDst<=2'b00;
747
+ ALUASrc<=0;
748
+ ALUBSrc<=0;
749
+ RegWrite<=0;
750
+ MemRead<=0;
751
+ MemWrite<=0;
752
+ MemtoReg<=3'b000;
753
+ ExtOp<=2'b00;
754
+ if_beq<=0;
755
+ if_bne<=1;
756
+ if_bgtz<=0;
757
+ if_blez<=0;
758
+ if_bgez<=0;
759
+ if_bltz<=0;
760
+ ALUCtrl<=4'b0000;
761
+ if_j<=0;
762
+ PCsel<=2'b10;
763
+ if_sh<=0;
764
+ if_sb<=0;
765
+ dataOp<=3'b000;
766
+ multdivOp<=2'b00;
767
+ start<=0;
768
+ if_mthi<=0;
769
+ if_mtlo<=0;
770
+ if_mfhi<=0;
771
+ if_mflo<=0;
772
+ if_1<=0;
773
+ if_2<=0;
774
+ if_3<=0;
775
+ cp0WE<=0;
776
+ end
777
+
778
+ 6'b000111://bgtz
779
+ begin
780
+ RegDst<=2'b00;
781
+ ALUASrc<=0;
782
+ ALUBSrc<=0;
783
+ RegWrite<=0;
784
+ MemRead<=0;
785
+ MemWrite<=0;
786
+ MemtoReg<=3'b000;
787
+ ExtOp<=2'b00;
788
+ if_beq<=0;
789
+ if_bne<=0;
790
+ if_bgtz<=1;
791
+ if_blez<=0;
792
+ if_bgez<=0;
793
+ if_bltz<=0;
794
+ ALUCtrl<=4'b0000;
795
+ if_j<=0;
796
+ PCsel<=2'b10;
797
+ if_sh<=0;
798
+ if_sb<=0;
799
+ dataOp<=3'b000;
800
+ multdivOp<=2'b00;
801
+ start<=0;
802
+ if_mthi<=0;
803
+ if_mtlo<=0;
804
+ if_mfhi<=0;
805
+ if_mflo<=0;
806
+ if_1<=0;
807
+ if_2<=0;
808
+ if_3<=0;
809
+ cp0WE<=0;
810
+ end
811
+
812
+ 6'b000110://blez
813
+ begin
814
+ RegDst<=2'b00;
815
+ ALUASrc<=0;
816
+ ALUBSrc<=0;
817
+ RegWrite<=0;
818
+ MemRead<=0;
819
+ MemWrite<=0;
820
+ MemtoReg<=3'b000;
821
+ ExtOp<=2'b00;
822
+ if_beq<=0;
823
+ if_bne<=0;
824
+ if_bgtz<=0;
825
+ if_blez<=1;
826
+ if_bgez<=0;
827
+ if_bltz<=0;
828
+ ALUCtrl<=4'b0000;
829
+ if_j<=0;
830
+ PCsel<=2'b10;
831
+ if_sh<=0;
832
+ if_sb<=0;
833
+ dataOp<=3'b000;
834
+ multdivOp<=2'b00;
835
+ start<=0;
836
+ if_mthi<=0;
837
+ if_mtlo<=0;
838
+ if_mfhi<=0;
839
+ if_mflo<=0;
840
+ if_1<=0;
841
+ if_2<=0;
842
+ if_3<=0;
843
+ cp0WE<=0;
844
+ end
845
+
846
+ 6'b000001://bgez/bltz
847
+ begin
848
+ RegDst<=2'b00;
849
+ ALUASrc<=0;
850
+ ALUBSrc<=0;
851
+ RegWrite<=0;
852
+ MemRead<=0;
853
+ MemWrite<=0;
854
+ MemtoReg<=3'b000;
855
+ ExtOp<=2'b00;
856
+ if_beq<=0;
857
+ if_bne<=0;
858
+ if_bgtz<=0;
859
+ if_blez<=0;
860
+ ALUCtrl<=4'b0000;
861
+ if_j<=0;
862
+ PCsel<=2'b10;
863
+ if_sh<=0;
864
+ if_sb<=0;
865
+ dataOp<=3'b000;
866
+ multdivOp<=2'b00;
867
+ start<=0;
868
+ if_mthi<=0;
869
+ if_mtlo<=0;
870
+ if_mfhi<=0;
871
+ if_mflo<=0;
872
+ if_1<=0;
873
+ if_2<=0;
874
+ if_3<=0;
875
+ cp0WE<=0;
876
+ case(rt)
877
+ 5'b00001:begin//bgez
878
+ if_bgez<=1;
879
+ if_bltz<=0;
880
+ end
881
+ 5'b00000:begin//bltz
882
+ if_bgez<=0;
883
+ if_bltz<=1;
884
+ end
885
+ default:begin
886
+ if_bgez<=0;
887
+ if_bltz<=0;
888
+ end
889
+ endcase
890
+ end
891
+
892
+ 6'b001111://lui
893
+ begin
894
+ RegDst<=2'b00;
895
+ ALUASrc<=0;
896
+ ALUBSrc<=1;
897
+ RegWrite<=1;
898
+ MemRead<=0;
899
+ MemWrite<=0;
900
+ MemtoReg<=3'b000;
901
+ ExtOp<=2'b10;
902
+ if_beq<=0;
903
+ if_bne<=0;
904
+ if_bgtz<=0;
905
+ if_blez<=0;
906
+ if_bgez<=0;
907
+ if_bltz<=0;
908
+ ALUCtrl<=4'b0010;
909
+ if_j<=0;
910
+ PCsel<=2'b00;
911
+ if_sh<=0;
912
+ if_sb<=0;
913
+ dataOp<=3'b000;
914
+ multdivOp<=2'b00;
915
+ start<=0;
916
+ if_mthi<=0;
917
+ if_mtlo<=0;
918
+ if_mfhi<=0;
919
+ if_mflo<=0;
920
+ if_1<=0;
921
+ if_2<=0;
922
+ if_3<=0;
923
+ cp0WE<=0;
924
+ end
925
+
926
+ 6'b001101://ori
927
+ begin
928
+ RegDst<=2'b00;
929
+ ALUASrc<=0;
930
+ ALUBSrc<=1;
931
+ RegWrite<=1;
932
+ MemRead<=0;
933
+ MemWrite<=0;
934
+ MemtoReg<=3'b000;
935
+ ExtOp<=2'b01;
936
+ if_beq<=0;
937
+ if_bne<=0;
938
+ if_bgtz<=0;
939
+ if_blez<=0;
940
+ if_bgez<=0;
941
+ if_bltz<=0;
942
+ ALUCtrl<=4'b0001;
943
+ if_j<=0;
944
+ PCsel<=2'b00;
945
+ if_sh<=0;
946
+ if_sb<=0;
947
+ dataOp<=3'b000;
948
+ multdivOp<=2'b00;
949
+ start<=0;
950
+ if_mthi<=0;
951
+ if_mtlo<=0;
952
+ if_mfhi<=0;
953
+ if_mflo<=0;
954
+ if_1<=0;
955
+ if_2<=0;
956
+ if_3<=0;
957
+ cp0WE<=0;
958
+ end
959
+
960
+ 6'b001010://slti
961
+ begin
962
+ RegDst<=2'b00;
963
+ ALUASrc<=0;
964
+ ALUBSrc<=1;
965
+ RegWrite<=1;
966
+ MemRead<=0;
967
+ MemWrite<=0;
968
+ MemtoReg<=3'b000;
969
+ ExtOp<=2'b00;
970
+ if_beq<=0;
971
+ if_bne<=0;
972
+ if_bgtz<=0;
973
+ if_blez<=0;
974
+ if_bgez<=0;
975
+ if_bltz<=0;
976
+ ALUCtrl<=4'b1001;
977
+ if_j<=0;
978
+ PCsel<=2'b00;
979
+ if_sh<=0;
980
+ if_sb<=0;
981
+ dataOp<=3'b000;
982
+ multdivOp<=2'b00;
983
+ start<=0;
984
+ if_mthi<=0;
985
+ if_mtlo<=0;
986
+ if_mfhi<=0;
987
+ if_mflo<=0;
988
+ if_1<=0;
989
+ if_2<=0;
990
+ if_3<=0;
991
+ cp0WE<=0;
992
+ end
993
+
994
+ 6'b001011://sltiu
995
+ begin
996
+ RegDst<=2'b00;
997
+ ALUASrc<=0;
998
+ ALUBSrc<=1;
999
+ RegWrite<=1;
1000
+ MemRead<=0;
1001
+ MemWrite<=0;
1002
+ MemtoReg<=3'b000;
1003
+ ExtOp<=2'b00;
1004
+ if_beq<=0;
1005
+ if_bne<=0;
1006
+ if_bgtz<=0;
1007
+ if_blez<=0;
1008
+ if_bgez<=0;
1009
+ if_bltz<=0;
1010
+ ALUCtrl<=4'b1010;
1011
+ if_j<=0;
1012
+ PCsel<=2'b00;
1013
+ if_sh<=0;
1014
+ if_sb<=0;
1015
+ dataOp<=3'b000;
1016
+ multdivOp<=2'b00;
1017
+ start<=0;
1018
+ if_mthi<=0;
1019
+ if_mtlo<=0;
1020
+ if_mfhi<=0;
1021
+ if_mflo<=0;
1022
+ if_1<=0;
1023
+ if_2<=0;
1024
+ if_3<=0;
1025
+ cp0WE<=0;
1026
+ end
1027
+
1028
+ 6'b001100://andi
1029
+ begin
1030
+ RegDst<=2'b00;
1031
+ ALUASrc<=0;
1032
+ ALUBSrc<=1;
1033
+ RegWrite<=1;
1034
+ MemRead<=0;
1035
+ MemWrite<=0;
1036
+ MemtoReg<=3'b000;
1037
+ ExtOp<=2'b01;
1038
+ if_beq<=0;
1039
+ if_bne<=0;
1040
+ if_bgtz<=0;
1041
+ if_blez<=0;
1042
+ if_bgez<=0;
1043
+ if_bltz<=0;
1044
+ ALUCtrl<=4'b0000;
1045
+ if_j<=0;
1046
+ PCsel<=2'b00;
1047
+ if_sh<=0;
1048
+ if_sb<=0;
1049
+ dataOp<=3'b000;
1050
+ multdivOp<=2'b00;
1051
+ start<=0;
1052
+ if_mthi<=0;
1053
+ if_mtlo<=0;
1054
+ if_mfhi<=0;
1055
+ if_mflo<=0;
1056
+ if_1<=0;
1057
+ if_2<=0;
1058
+ if_3<=0;
1059
+ cp0WE<=0;
1060
+ end
1061
+
1062
+ 6'b001110://xori
1063
+ begin
1064
+ RegDst<=2'b00;
1065
+ ALUASrc<=0;
1066
+ ALUBSrc<=1;
1067
+ RegWrite<=1;
1068
+ MemRead<=0;
1069
+ MemWrite<=0;
1070
+ MemtoReg<=3'b000;
1071
+ ExtOp<=2'b01;
1072
+ if_beq<=0;
1073
+ if_bne<=0;
1074
+ if_bgtz<=0;
1075
+ if_blez<=0;
1076
+ if_bgez<=0;
1077
+ if_bltz<=0;
1078
+ ALUCtrl<=4'b0100;
1079
+ if_j<=0;
1080
+ PCsel<=2'b00;
1081
+ if_sh<=0;
1082
+ if_sb<=0;
1083
+ dataOp<=3'b000;
1084
+ multdivOp<=2'b00;
1085
+ start<=0;
1086
+ if_mthi<=0;
1087
+ if_mtlo<=0;
1088
+ if_mfhi<=0;
1089
+ if_mflo<=0;
1090
+ if_1<=0;
1091
+ if_2<=0;
1092
+ if_3<=0;
1093
+ cp0WE<=0;
1094
+ end
1095
+
1096
+ 6'b001001://addiu
1097
+ begin
1098
+ RegDst<=2'b00;
1099
+ ALUASrc<=0;
1100
+ ALUBSrc<=1;
1101
+ RegWrite<=1;
1102
+ MemRead<=0;
1103
+ MemWrite<=0;
1104
+ MemtoReg<=3'b000;
1105
+ ExtOp<=2'b00;
1106
+ if_beq<=0;
1107
+ if_bne<=0;
1108
+ if_bgtz<=0;
1109
+ if_blez<=0;
1110
+ if_bgez<=0;
1111
+ if_bltz<=0;
1112
+ ALUCtrl<=4'b0010;
1113
+ if_j<=0;
1114
+ PCsel<=2'b00;
1115
+ if_sh<=0;
1116
+ if_sb<=0;
1117
+ dataOp<=3'b000;
1118
+ multdivOp<=2'b00;
1119
+ start<=0;
1120
+ if_mthi<=0;
1121
+ if_mtlo<=0;
1122
+ if_mfhi<=0;
1123
+ if_mflo<=0;
1124
+ if_1<=0;
1125
+ if_2<=0;
1126
+ if_3<=0;
1127
+ cp0WE<=0;
1128
+ end
1129
+
1130
+ 6'b001000://addi
1131
+ begin
1132
+ RegDst<=2'b00;
1133
+ ALUASrc<=0;
1134
+ ALUBSrc<=1;
1135
+ RegWrite<=1;
1136
+ MemRead<=0;
1137
+ MemWrite<=0;
1138
+ MemtoReg<=3'b000;
1139
+ ExtOp<=2'b00;
1140
+ if_beq<=0;
1141
+ if_bne<=0;
1142
+ if_bgtz<=0;
1143
+ if_blez<=0;
1144
+ if_bgez<=0;
1145
+ if_bltz<=0;
1146
+ ALUCtrl<=4'b0010;
1147
+ if_j<=0;
1148
+ PCsel<=2'b00;
1149
+ if_sh<=0;
1150
+ if_sb<=0;
1151
+ dataOp<=3'b000;
1152
+ multdivOp<=2'b00;
1153
+ start<=0;
1154
+ if_mthi<=0;
1155
+ if_mtlo<=0;
1156
+ if_mfhi<=0;
1157
+ if_mflo<=0;
1158
+ if_1<=0;
1159
+ if_2<=0;
1160
+ if_3<=0;
1161
+ cp0WE<=0;
1162
+ end
1163
+
1164
+ 6'b000011://jal
1165
+ begin
1166
+ RegDst<=2'b10;
1167
+ ALUASrc<=0;
1168
+ ALUBSrc<=0;
1169
+ RegWrite<=1;
1170
+ MemRead<=0;
1171
+ MemWrite<=0;
1172
+ MemtoReg<=3'b010;
1173
+ ExtOp<=2'b00;
1174
+ if_beq<=0;
1175
+ if_bne<=0;
1176
+ if_bgtz<=0;
1177
+ if_blez<=0;
1178
+ if_bgez<=0;
1179
+ if_bltz<=0;
1180
+ ALUCtrl<=4'b0000;
1181
+ if_j<=1;
1182
+ PCsel<=2'b10;
1183
+ if_sh<=0;
1184
+ if_sb<=0;
1185
+ dataOp<=3'b000;
1186
+ multdivOp<=2'b00;
1187
+ start<=0;
1188
+ if_mthi<=0;
1189
+ if_mtlo<=0;
1190
+ if_mfhi<=0;
1191
+ if_mflo<=0;
1192
+ if_1<=0;
1193
+ if_2<=0;
1194
+ if_3<=0;
1195
+ cp0WE<=0;
1196
+ end
1197
+
1198
+ 6'b000010://j
1199
+ begin
1200
+ RegDst<=2'b00;
1201
+ ALUASrc<=0;
1202
+ ALUBSrc<=0;
1203
+ RegWrite<=0;
1204
+ MemRead<=0;
1205
+ MemWrite<=0;
1206
+ MemtoReg<=3'b000;
1207
+ ExtOp<=2'b00;
1208
+ if_beq<=0;
1209
+ if_bne<=0;
1210
+ if_bgtz<=0;
1211
+ if_blez<=0;
1212
+ if_bgez<=0;
1213
+ if_bltz<=0;
1214
+ ALUCtrl<=4'b0000;
1215
+ if_j<=1;
1216
+ PCsel<=2'b10;
1217
+ if_sh<=0;
1218
+ if_sb<=0;
1219
+ dataOp<=3'b000;
1220
+ multdivOp<=2'b00;
1221
+ start<=0;
1222
+ if_mthi<=0;
1223
+ if_mtlo<=0;
1224
+ if_mfhi<=0;
1225
+ if_mflo<=0;
1226
+ if_1<=0;
1227
+ if_2<=0;
1228
+ if_3<=0;
1229
+ cp0WE<=0;
1230
+ end
1231
+
1232
+ 6'b010000:
1233
+ begin
1234
+ if_beq<=0;
1235
+ if_bne<=0;
1236
+ if_bgtz<=0;
1237
+ if_blez<=0;
1238
+ if_bgez<=0;
1239
+ if_bltz<=0;
1240
+ ExtOp<=2'b00;
1241
+ ALUCtrl<=4'b0000;
1242
+ ALUASrc<=0;
1243
+ ALUBSrc<=0;
1244
+ if_j<=0;
1245
+ MemRead<=0;
1246
+ MemWrite<=0;
1247
+ RegDst<=2'b00;
1248
+ if_sh<=0;
1249
+ if_sb<=0;
1250
+ dataOp<=3'b000;
1251
+ multdivOp<=2'b00;
1252
+ start<=0;
1253
+ if_mthi<=0;
1254
+ if_mtlo<=0;
1255
+ if_mfhi<=0;
1256
+ if_mflo<=0;
1257
+ if_1<=0;
1258
+ if_2<=0;
1259
+ if_3<=0;
1260
+ case(rs)
1261
+ 5'b00100: //mtc0
1262
+ begin
1263
+ RegWrite<=0;
1264
+ MemtoReg<=3'b000;
1265
+ PCsel<=2'b00;
1266
+ cp0WE<=1;
1267
+ end
1268
+ 5'b00000: //mfc0
1269
+ begin
1270
+ RegWrite<=1;
1271
+ MemtoReg<=3'b100;
1272
+ PCsel<=2'b00;
1273
+ cp0WE<=0;
1274
+ end
1275
+ default:
1276
+ begin
1277
+
1278
+ end
1279
+ endcase
1280
+ case(func)
1281
+ 6'b011000://eret
1282
+ begin
1283
+ RegWrite<=0;
1284
+ MemtoReg<=3'b000;
1285
+ PCsel<=2'b11;
1286
+ cp0WE<=0;
1287
+ end
1288
+ default: begin
1289
+
1290
+ end
1291
+ endcase
1292
+ end
1293
+
1294
+ default:
1295
+ begin
1296
+ RegDst<=2'b00;
1297
+ ALUASrc<=0;
1298
+ ALUBSrc<=0;
1299
+ RegWrite<=0;
1300
+ MemRead<=0;
1301
+ MemWrite<=0;
1302
+ MemtoReg<=3'b000;
1303
+ ExtOp<=2'b00;
1304
+ if_beq<=0;
1305
+ if_bne<=0;
1306
+ if_bgtz<=0;
1307
+ if_blez<=0;
1308
+ if_bgez<=0;
1309
+ if_bltz<=0;
1310
+ ALUCtrl<=4'b0000;
1311
+ if_j<=0;
1312
+ PCsel<=2'b00;
1313
+ if_sh<=0;
1314
+ if_sb<=0;
1315
+ dataOp<=3'b000;
1316
+ multdivOp<=2'b00;
1317
+ start<=0;
1318
+ if_mthi<=0;
1319
+ if_mtlo<=0;
1320
+ if_mfhi<=0;
1321
+ if_mflo<=0;
1322
+ if_1<=0;
1323
+ if_2<=0;
1324
+ if_3<=0;
1325
+ cp0WE<=0;
1326
+ end
1327
+ endcase
1328
+ end
1329
+
1330
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/dm.v ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:57:57 11/23/2018
7
+ // Design Name:
8
+ // Module Name: dm
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module dm(
22
+ input clk,
23
+ input reset,
24
+ input MemWrite,
25
+ input MemRead,
26
+ input if_sh,
27
+ input if_sb,
28
+ input [31:0] MemAddr,
29
+ input [31:0] WD,
30
+ input [31:0] PC,
31
+ output [31:0] RD
32
+ );
33
+
34
+ reg[31:0] dm[0:4095];
35
+ integer i;
36
+ initial begin
37
+ for(i=0;i<4096;i=i+1) begin //1024
38
+ dm[i]<=0;
39
+ end
40
+ end
41
+
42
+ assign RD = MemRead ? dm[MemAddr[13:2]] : 0;
43
+
44
+ always@(posedge clk) begin
45
+ if(reset) begin
46
+ for(i=0;i<4096;i=i+1) begin//1024
47
+ dm[i]<=0;
48
+ end
49
+ end
50
+ else begin
51
+ if(MemWrite) begin
52
+ if(if_sh) begin
53
+ if(MemAddr[1]==1'b1) begin
54
+ dm[MemAddr[13:2]][31:16] <= WD[15:0];
55
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{WD[15:0],dm[MemAddr[13:2]][15:0]});
56
+ end
57
+ else if(MemAddr[1]==1'b0) begin
58
+ dm[MemAddr[13:2]][15:0] <= WD[15:0];
59
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{dm[MemAddr[13:2]][31:16],WD[15:0]});
60
+ end
61
+ end
62
+ else if(if_sb) begin
63
+ case(MemAddr[1:0])
64
+ 2'b00: begin
65
+ dm[MemAddr[13:2]][7:0] <= WD[7:0];
66
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{dm[MemAddr[13:2]][31:8],WD[7:0]});
67
+ end
68
+ 2'b01: begin
69
+ dm[MemAddr[13:2]][15:8] <= WD[7:0];
70
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{dm[MemAddr[13:2]][31:16],WD[7:0],dm[MemAddr[13:2]][7:0]});
71
+ end
72
+ 2'b10: begin
73
+ dm[MemAddr[13:2]][23:16] <= WD[7:0];
74
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{dm[MemAddr[13:2]][31:24],WD[7:0],dm[MemAddr[13:2]][15:0]});
75
+ end
76
+ 2'b11: begin
77
+ dm[MemAddr[13:2]][31:24] <= WD[7:0];
78
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{WD[7:0],dm[MemAddr[13:2]][23:0]});
79
+ end
80
+ endcase
81
+ end
82
+ else begin
83
+ dm[MemAddr[13:2]] <= WD;
84
+ $display("%d@%h: *%h <= %h",$time,PC,MemAddr,WD);
85
+ end
86
+ end
87
+ end
88
+ end
89
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/ext.v ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:59:19 11/23/2018
7
+ // Design Name:
8
+ // Module Name: ext
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module ext(
22
+ input [15:0] in,
23
+ input [1:0] ExtOp,
24
+ output reg [31:0] out
25
+ );
26
+
27
+ always@(*) begin
28
+ case(ExtOp)
29
+ 2'b00: out <= {{16{in[15]}},in};
30
+ 2'b01: out <= {{16{1'b0}},in};
31
+ 2'b10: out <= {in,{16{1'b0}}};
32
+ 2'b11: out <= 0;
33
+ endcase
34
+ end
35
+
36
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/forward_mux.v ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:57:34 11/24/2018
7
+ // Design Name:
8
+ // Module Name: forward_mux
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module forward_mux(
22
+ input [31:0] RS_E,
23
+ input [31:0] RT_E,
24
+ input [31:0] RT_M,
25
+ input [31:0] WD,
26
+ input [31:0] AO_M,
27
+ input [31:0] MDO_M,
28
+ input [31:0] CP0_M,
29
+ input [31:0] MD_out,
30
+ input [31:0] PC8_E,
31
+ input [31:0] PC8_M,
32
+ input [31:0] PC8_W,
33
+ input [31:0] RF_RD1,
34
+ input [31:0] RF_RD2,
35
+ input [31:0] EPCout,
36
+ input [2:0] ForwardRSD,
37
+ input [2:0] ForwardRTD,
38
+ input [2:0] ForwardRSE,
39
+ input [2:0] ForwardRTE,
40
+ input [2:0] ForwardRTM,
41
+ input [2:0] ForwardERET,
42
+ output reg[31:0] MFRSD,
43
+ output reg[31:0] MFRTD,
44
+ output reg[31:0] MFRSE,
45
+ output reg[31:0] MFRTE,
46
+ output reg[31:0] MFRTM,
47
+ output reg[31:0] MFERET
48
+ );
49
+
50
+ always@(*) begin
51
+ case(ForwardRSD)
52
+ 3'b000 : MFRSD <= RF_RD1;
53
+ 3'b001 : MFRSD <= AO_M;
54
+ 3'b010 : MFRSD <= WD;
55
+ 3'b011 : MFRSD <= PC8_E;
56
+ 3'b100 : MFRSD <= PC8_M;
57
+ 3'b101 : MFRSD <= CP0_M;
58
+ 3'b110 : MFRSD <= MD_out;
59
+ 3'b111 : MFRSD <= MDO_M;
60
+ default : MFRSD <= 0;
61
+ endcase
62
+
63
+ case(ForwardRTD)
64
+ 0: MFRTD <= RF_RD2;
65
+ 1: MFRTD <= AO_M;
66
+ 2: MFRTD <= WD;
67
+ 3: MFRTD <= PC8_E;
68
+ 4: MFRTD <= PC8_M;
69
+ 5: MFRTD <= CP0_M;
70
+ 6: MFRTD <= MD_out;
71
+ 7: MFRTD <= MDO_M;
72
+ default: MFRTD <= 0;
73
+ endcase
74
+
75
+ case(ForwardRSE)
76
+ 0:MFRSE <= RS_E;
77
+ 1:MFRSE <= AO_M;
78
+ 2:MFRSE <= WD;
79
+ 3:MFRSE <= 0;
80
+ 4:MFRSE <= PC8_M;
81
+ 5:MFRSE <= CP0_M;
82
+ 6:MFRSE <= 0;
83
+ 7:MFRSE <= MDO_M;
84
+ default:MFRSE <= 0;
85
+ endcase
86
+
87
+ case(ForwardRTE)
88
+ 0:MFRTE <= RT_E;
89
+ 1:MFRTE <= AO_M;
90
+ 2:MFRTE <= WD;
91
+ 3:MFRTE <= 0;
92
+ 4:MFRTE <= PC8_M;
93
+ 5:MFRTE <= CP0_M;
94
+ 6:MFRTE <= 0;
95
+ 7:MFRTE <= MDO_M;
96
+ default:MFRTE <= 0;
97
+ endcase
98
+
99
+ case(ForwardRTM)
100
+ 0:MFRTM <= RT_M;
101
+ 1:MFRTM <= 0;
102
+ 2:MFRTM <= WD;
103
+ 3:MFRTM <= 0;
104
+ 4:MFRTM <= 0;
105
+ 5:MFRTM <= 0;
106
+ 6:MFRTM <= 0;
107
+ 7:MFRTM <= 0;
108
+ default:MFRTM <= 0;
109
+ endcase
110
+
111
+ case(ForwardERET)
112
+ 0:MFERET <= EPCout;
113
+ 1:MFERET <= MFRTM;
114
+ default:MFERET <= 0;
115
+ endcase
116
+ end
117
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/grf.v ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:02:24 11/23/2018
7
+ // Design Name:
8
+ // Module Name: grf
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module grf(
22
+ input clk,
23
+ input reset,
24
+ input RegWrite,
25
+ input [4:0] RA1,
26
+ input [4:0] RA2,
27
+ input [4:0] WA,
28
+ input [31:0] WD,
29
+ input [31:0] PC,
30
+ output [31:0] RD1,
31
+ output [31:0] RD2
32
+ );
33
+
34
+ reg[31:0] register[31:0];
35
+ integer i;
36
+ initial begin
37
+ for(i=0;i<32;i=i+1) begin
38
+ register[i] <= 0;
39
+ end
40
+ end
41
+
42
+ assign RD1 = register[RA1];
43
+ assign RD2 = register[RA2];
44
+
45
+ always@(posedge clk) begin
46
+ if(reset) begin
47
+ for(i=0;i<32;i=i+1) begin
48
+ register[i] <= 0;
49
+ end
50
+ end
51
+ else begin
52
+ if(RegWrite&&WA!=5'b00000) begin
53
+ register[WA] <= WD;
54
+ //$display("@%h: $%d <= %h", PC, WA,WD);
55
+ $display("%d@%h: $%d <= %h",$time,PC,WA,WD);
56
+ end
57
+ end
58
+ end
59
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/hazardUnit.v ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12:55:59 11/24/2018
7
+ // Design Name:
8
+ // Module Name: hazardUnit
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module hazardUnit(
22
+ input [31:0] IR_D,
23
+ input [31:0] IR_E,
24
+ input [31:0] IR_M,
25
+ input [31:0] IR_W,
26
+ input Busy,
27
+ input start,
28
+ output IR_D_en,
29
+ output IR_E_clr,
30
+ output PC_en,
31
+ output [2:0]ForwardRSD,
32
+ output [2:0]ForwardRTD,
33
+ output [2:0]ForwardRSE,
34
+ output [2:0]ForwardRTE,
35
+ output [2:0]ForwardRTM,
36
+ output [2:0]ForwardERET
37
+ );
38
+
39
+ `define op 31:26
40
+ `define rs 25:21
41
+ `define rt 20:16
42
+ `define rd 15:11
43
+ `define shamt 10:6
44
+ `define func 5:0
45
+ `define imm16 15:0
46
+
47
+ `define R 6'b000000
48
+ `define lw 6'b100011
49
+ `define lb 6'b100000
50
+ `define lbu 6'b100100
51
+ `define lh 6'b100001
52
+ `define lhu 6'b100101
53
+ `define sw 6'b101011
54
+ `define sb 6'b101000
55
+ `define sh 6'b101001
56
+ `define lui 6'b001111
57
+ `define ori 6'b001101
58
+ `define andi 6'b001100
59
+ `define xori 6'b001110
60
+ `define addi 6'b001000
61
+ `define addiu 6'b001001
62
+ `define slti 6'b001010
63
+ `define sltiu 6'b001011
64
+ `define beq 6'b000100
65
+ `define bne 6'b000101
66
+ `define bgtz 6'b000111
67
+ `define blez 6'b000110
68
+ `define jal 6'b000011
69
+ `define addu 6'b100001
70
+ `define add 6'b100000
71
+ `define subu 6'b100011
72
+ `define sub 6'b100010
73
+ `define and_ 6'b100100
74
+ `define or_ 6'b100101
75
+ `define xor_ 6'b100110
76
+ `define nor_ 6'b100111
77
+ `define sll 6'b000000
78
+ `define srl 6'b000010
79
+ `define sra 6'b000011
80
+ `define sllv 6'b000100
81
+ `define srlv 6'b000110
82
+ `define srav 6'b000111
83
+ `define jr 6'b001000
84
+ `define jalr 6'b001001
85
+ `define mult 6'b011000
86
+ `define multu 6'b011001
87
+ `define div 6'b011010
88
+ `define divu 6'b011011
89
+ `define mfhi 6'b010000
90
+ `define mflo 6'b010010
91
+ `define mthi 6'b010001
92
+ `define mtlo 6'b010011
93
+ `define slt 6'b101010
94
+ `define sltu 6'b101011
95
+
96
+ `define cal_r_D (IR_D[`op]==`R&&IR_D[`func]!=`jalr&&IR_D[`func]!=`jr&&IR_D[`func]!=`mfhi&&IR_D[`func]!=`mflo&&IR_D!=0)
97
+ `define cal_r_E (IR_E[`op]==`R&&IR_E[`func]!=`jalr&&IR_E[`func]!=`jr&&IR_E[`func]!=`mfhi&&IR_E[`func]!=`mflo&&IR_E!=0)
98
+ `define cal_r_M (IR_M[`op]==`R&&IR_M[`func]!=`jalr&&IR_M[`func]!=`jr&&IR_M[`func]!=`mfhi&&IR_M[`func]!=`mflo&&IR_M!=0)
99
+ `define cal_r_W (IR_W[`op]==`R&&IR_W[`func]!=`jalr&&IR_W[`func]!=`jr&&IR_W[`func]!=`mfhi&&IR_W[`func]!=`mflo&&IR_W!=0)
100
+
101
+ `define cal_i_D (IR_D[`op]==`lui||IR_D[`op]==`ori||IR_D[`op]==`addi||IR_D[`op]==`addiu||IR_D[`op]==`andi||IR_D[`op]==`xori||IR_D[`op]==`slti||IR_D[`op]==`sltiu)
102
+ `define cal_i_E (IR_E[`op]==`lui||IR_E[`op]==`ori||IR_E[`op]==`addi||IR_E[`op]==`addiu||IR_E[`op]==`andi||IR_E[`op]==`xori||IR_E[`op]==`slti||IR_E[`op]==`sltiu)
103
+ `define cal_i_M (IR_M[`op]==`lui||IR_M[`op]==`ori||IR_M[`op]==`addi||IR_M[`op]==`addiu||IR_M[`op]==`andi||IR_M[`op]==`xori||IR_M[`op]==`slti||IR_M[`op]==`sltiu)
104
+ `define cal_i_W (IR_W[`op]==`lui||IR_W[`op]==`ori||IR_W[`op]==`addi||IR_W[`op]==`addiu||IR_W[`op]==`andi||IR_W[`op]==`xori||IR_W[`op]==`slti||IR_W[`op]==`sltiu)
105
+
106
+ `define load_D (IR_D[`op]==`lw||IR_D[`op]==`lb||IR_D[`op]==`lbu||IR_D[`op]==`lh||IR_D[`op]==`lhu)
107
+ `define load_E (IR_E[`op]==`lw||IR_E[`op]==`lb||IR_E[`op]==`lbu||IR_E[`op]==`lh||IR_E[`op]==`lhu)
108
+ `define load_M (IR_M[`op]==`lw||IR_M[`op]==`lb||IR_M[`op]==`lbu||IR_M[`op]==`lh||IR_M[`op]==`lhu)
109
+ `define load_W (IR_W[`op]==`lw||IR_W[`op]==`lb||IR_W[`op]==`lbu||IR_W[`op]==`lh||IR_W[`op]==`lhu)
110
+
111
+ `define store_D (IR_D[`op]==`sw||IR_D[`op]==`sh||IR_D[`op]==`sb)
112
+ `define store_E (IR_E[`op]==`sw||IR_E[`op]==`sh||IR_E[`op]==`sb)
113
+ `define store_M (IR_M[`op]==`sw||IR_M[`op]==`sh||IR_M[`op]==`sb)
114
+ `define store_W (IR_W[`op]==`sw||IR_W[`op]==`sh||IR_W[`op]==`sb)
115
+
116
+ `define beq_D (IR_D[`op]==`beq||IR_D[`op]==`bne||IR_D[`op]==`bgtz||IR_D[`op]==`blez||(IR_D[`op]==6'b000001&&(IR_D[`rt]==5'b00000||IR_D[`rt]==5'b00001)))
117
+ `define beq_E (IR_E[`op]==`beq||IR_E[`op]==`bne||IR_E[`op]==`bgtz||IR_E[`op]==`blez||(IR_E[`op]==6'b000001&&(IR_E[`rt]==5'b00000||IR_E[`rt]==5'b00001)))
118
+ `define beq_M (IR_M[`op]==`beq||IR_M[`op]==`bne||IR_M[`op]==`bgtz||IR_M[`op]==`blez||(IR_M[`op]==6'b000001&&(IR_M[`rt]==5'b00000||IR_M[`rt]==5'b00001)))
119
+ `define beq_W (IR_W[`op]==`beq||IR_W[`op]==`bne||IR_W[`op]==`bgtz||IR_W[`op]==`blez||(IR_W[`op]==6'b000001&&(IR_W[`rt]==5'b00000||IR_W[`rt]==5'b00001)))
120
+
121
+ `define jal_D (IR_D[`op]==`jal)
122
+ `define jal_E (IR_E[`op]==`jal)
123
+ `define jal_M (IR_M[`op]==`jal)
124
+ `define jal_W (IR_W[`op]==`jal)
125
+
126
+ `define jalr_D (IR_D[`op]==`R&&IR_D[`func]==`jalr)
127
+ `define jalr_E (IR_E[`op]==`R&&IR_E[`func]==`jalr)
128
+ `define jalr_M (IR_M[`op]==`R&&IR_M[`func]==`jalr)
129
+ `define jalr_W (IR_W[`op]==`R&&IR_W[`func]==`jalr)
130
+
131
+ `define jr_D (IR_D[`op]==`R&&IR_D[`func]==`jr)
132
+ `define jr_E (IR_E[`op]==`R&&IR_E[`func]==`jr)
133
+ `define jr_M (IR_M[`op]==`R&&IR_M[`func]==`jr)
134
+ `define jr_W (IR_W[`op]==`R&&IR_W[`func]==`jr)
135
+
136
+ `define mfhi_D (IR_D[`op]==`R&&IR_D[`func]==`mfhi)
137
+ `define mfhi_E (IR_E[`op]==`R&&IR_E[`func]==`mfhi)
138
+ `define mfhi_M (IR_M[`op]==`R&&IR_M[`func]==`mfhi)
139
+ `define mfhi_W (IR_W[`op]==`R&&IR_W[`func]==`mfhi)
140
+
141
+ `define mflo_D (IR_D[`op]==`R&&IR_D[`func]==`mflo)
142
+ `define mflo_E (IR_E[`op]==`R&&IR_E[`func]==`mflo)
143
+ `define mflo_M (IR_M[`op]==`R&&IR_M[`func]==`mflo)
144
+ `define mflo_W (IR_W[`op]==`R&&IR_W[`func]==`mflo)
145
+
146
+ `define mf_D (`mfhi_D||`mflo_D)
147
+ `define mf_E (`mfhi_E||`mflo_E)
148
+ `define mf_M (`mfhi_M||`mflo_M)
149
+ `define mf_W (`mfhi_W||`mflo_W)
150
+
151
+ `define mfc0_D (IR_D[`op]==6'b010000&&IR_D[`rs]==5'b00000)
152
+ `define mfc0_E (IR_E[`op]==6'b010000&&IR_E[`rs]==5'b00000)
153
+ `define mfc0_M (IR_M[`op]==6'b010000&&IR_M[`rs]==5'b00000)
154
+ `define mfc0_W (IR_W[`op]==6'b010000&&IR_W[`rs]==5'b00000)
155
+
156
+ `define mtc0_D (IR_D[`op]==6'b010000&&IR_D[`rs]==5'b00100)
157
+ `define mtc0_E (IR_E[`op]==6'b010000&&IR_E[`rs]==5'b00100)
158
+ `define mtc0_M (IR_M[`op]==6'b010000&&IR_M[`rs]==5'b00100)
159
+ `define mtc0_W (IR_W[`op]==6'b010000&&IR_W[`rs]==5'b00100)
160
+
161
+ reg stall=0;
162
+ wire stall_b,stall_cal_r,stall_cal_i,stall_load,stall_store,stall_jr,stall_jalr,stall_busy,stall_mfmt,stall_eret;
163
+
164
+ assign stall_b = (`beq_D & `cal_r_E & (IR_D[`rs]==IR_E[`rd]&IR_E[`rd]!=0||IR_D[`rt]==IR_E[`rd]&IR_E[`rd]!=0))||
165
+ (`beq_D & `cal_i_E & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0||IR_D[`rt]==IR_E[`rt]&IR_E[`rt]!=0))||
166
+ (`beq_D & `load_E & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0||IR_D[`rt]==IR_E[`rt]&IR_E[`rt]!=0))||
167
+ (`beq_D & `load_M & (IR_D[`rs]==IR_M[`rt]&IR_M[`rt]!=0||IR_D[`rt]==IR_M[`rt]&IR_M[`rt]!=0));
168
+ assign stall_cal_r = (`cal_r_D) && (`load_E) && (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0||IR_D[`rt]==IR_E[`rt]&IR_E[`rt]!=0);
169
+ assign stall_cal_i = (`cal_i_D) && (`load_E) && (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0);
170
+ assign stall_load = (`load_D) && (`load_E) && (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0);
171
+ assign stall_store = (`store_D) && (`load_E) && (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0);
172
+ assign stall_jr = (`jr_D & (`cal_r_E) & (IR_D[`rs]==IR_E[`rd]&IR_E[`rd]!=0))||
173
+ (`jr_D & (`cal_i_E) & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0))||
174
+ (`jr_D & (`load_E) & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0))||
175
+ (`jr_D & (`load_M) & (IR_D[`rs]==IR_M[`rt]&IR_M[`rt]!=0));
176
+ assign stall_jalr = (`jalr_D & (`cal_r_E) & (IR_D[`rs]==IR_E[`rd]&IR_E[`rd]!=0))||
177
+ (`jalr_D & (`cal_i_E) & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0))||
178
+ (`jalr_D & (`load_E) & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0))||
179
+ (`jalr_D & (`load_M) & (IR_D[`rs]==IR_M[`rt]&IR_M[`rt]!=0));
180
+ assign stall_busy = (IR_D[`op]==`R&(IR_D[`func]==`mult||IR_D[`func]==`multu||IR_D[`func]==`div||IR_D[`func]==`divu||
181
+ IR_D[`func]==`mflo||IR_D[`func]==`mfhi||IR_D[`func]==`mthi||IR_D[`func]==`mtlo)) & (Busy||start);
182
+ //assign stall_mfmt = (IR_D[`op]==`R&(IR_D[`func]==`mult||IR_D[`func]==`multu||IR_D[`func]==`div||IR_D[`func]==`divu||
183
+ // IR_D[`func]==`mflo||IR_D[`func]==`mfhi||IR_D[`func]==`mthi||IR_D[`func]==`mtlo))
184
+ // &(IR_E[`op]==`R&(IR_E[`func]==`mult||IR_E[`func]==`multu||IR_E[`func]==`div||IR_E[`func]==`divu));
185
+ assign stall_eret = (IR_E[`op]==6'b010000&&IR_E[`rs]==5'b00100&&IR_E[`rd]==14)&&IR_D==32'h42000018;
186
+ always@(*) begin
187
+ stall <= stall_b||stall_cal_r||stall_cal_i||stall_load||stall_store||stall_jr||stall_jalr||stall_busy||stall_eret;
188
+ end
189
+
190
+ assign IR_D_en = !stall;
191
+ assign IR_E_clr = stall;
192
+ assign PC_en = !stall;
193
+
194
+ `define RSD (`cal_r_D||`cal_i_D||`load_D||`store_D||`beq_D||`jr_D||`jalr_D)
195
+ `define RTD (`cal_r_D||`store_D||`beq_D||`mtc0_D)
196
+ `define RSE (`cal_r_E||`cal_i_E||`load_E||`store_E)
197
+ `define RTE (`cal_r_E||`store_E||`mtc0_E)
198
+ `define RTM (`store_M||`mtc0_M)
199
+
200
+ assign ForwardRSD = (`RSD & `jal_E & IR_D[`rs]==31 & IR_D[`rs]!=0) ? 3 :
201
+ (`RSD & `jalr_E & IR_D[`rs]==IR_E[`rd] & IR_D[`rs]!=0) ? 3 :
202
+ (`RSD & `mf_E & IR_D[`rs]==IR_E[`rd] & IR_D[`rs]!=0) ? 6 :
203
+ (`RSD & `cal_r_M & IR_D[`rs]==IR_M[`rd] & IR_D[`rs]!=0) ? 1 :
204
+ (`RSD & `cal_i_M & IR_D[`rs]==IR_M[`rt] & IR_D[`rs]!=0) ? 1 :
205
+ (`RSD & `jal_M & IR_D[`rs]==31 & IR_D[`rs]!=0) ? 4 :
206
+ (`RSD & `jalr_M & IR_D[`rs]==IR_M[`rd] & IR_D[`rs]!=0) ? 4 :
207
+ (`RSD & `mf_M & IR_D[`rs]==IR_M[`rd] & IR_D[`rs]!=0) ? 5 :
208
+ (`RSD & `mfc0_M & IR_D[`rs]==IR_M[`rt] & IR_D[`rs]!=0) ? 7 :
209
+ (`RSD & `cal_r_W & IR_D[`rs]==IR_W[`rd] & IR_D[`rs]!=0) ? 2 :
210
+ (`RSD & `cal_i_W & IR_D[`rs]==IR_W[`rt] & IR_D[`rs]!=0) ? 2 :
211
+ (`RSD & `load_W & IR_D[`rs]==IR_W[`rt] & IR_D[`rs]!=0) ? 2 :
212
+ (`RSD & `mf_W & IR_D[`rs]==IR_W[`rd] & IR_D[`rs]!=0) ? 2 :
213
+ (`RSD & `mfc0_W & IR_D[`rs]==IR_W[`rt] & IR_D[`rs]!=0) ? 2 :
214
+ (`RSD & `jal_W & IR_D[`rs]==31 & IR_D[`rs]!=0) ? 2 :
215
+ (`RSD & `jalr_W & IR_D[`rs]==IR_W[`rd] & IR_D[`rs]!=0) ? 2 : 0 ;
216
+
217
+ assign ForwardRTD = (`RTD & `jal_E & IR_D[`rt]==31 & IR_D[`rt]!=0) ? 3 :
218
+ (`RTD & `jalr_E & IR_D[`rt]==IR_E[`rd] & IR_D[`rt]!=0) ? 3 :
219
+ (`RTD & `mf_E & IR_D[`rt]==IR_E[`rd] & IR_D[`rt]!=0) ? 6 :
220
+ (`RTD & `cal_r_M & IR_D[`rt]==IR_M[`rd] & IR_D[`rt]!=0) ? 1 :
221
+ (`RTD & `cal_i_M & IR_D[`rt]==IR_M[`rt] & IR_D[`rt]!=0) ? 1 :
222
+ (`RTD & `jal_M & IR_D[`rt]==31 & IR_D[`rt]!=0) ? 4 :
223
+ (`RTD & `jalr_M & IR_D[`rt]==IR_M[`rd] & IR_D[`rt]!=0) ? 4 :
224
+ (`RTD & `mf_M & IR_D[`rt]==IR_M[`rd] & IR_D[`rt]!=0) ? 7 :
225
+ (`RTD & `mfc0_M & IR_D[`rt]==IR_M[`rt] & IR_D[`rt]!=0) ? 5 :
226
+ (`RTD & `cal_r_W & IR_D[`rt]==IR_W[`rd] & IR_D[`rt]!=0) ? 2 :
227
+ (`RTD & `cal_i_W & IR_D[`rt]==IR_W[`rt] & IR_D[`rt]!=0) ? 2 :
228
+ (`RTD & `load_W & IR_D[`rt]==IR_W[`rt] & IR_D[`rt]!=0) ? 2 :
229
+ (`RTD & `mf_W & IR_D[`rt]==IR_W[`rd] & IR_D[`rt]!=0) ? 2 :
230
+ (`RTD & `mfc0_W & IR_D[`rt]==IR_W[`rt] & IR_D[`rt]!=0) ? 2 :
231
+ (`RTD & `jal_W & IR_D[`rt]==31 & IR_D[`rt]!=0) ? 2 :
232
+ (`RTD & `jalr_W & IR_D[`rt]==IR_W[`rd] & IR_D[`rt]!=0) ? 2 : 0;
233
+
234
+ assign ForwardRSE = (`RSE & `cal_r_M & (IR_E[`rs]==IR_M[`rd]) & IR_E[`rs]!=0) ? 1 :
235
+ (`RSE & `cal_i_M & (IR_E[`rs]==IR_M[`rt]) & IR_E[`rs]!=0) ? 1 :
236
+ (`RSE & `jal_M & (IR_E[`rs]==31) & IR_E[`rs]!=0) ? 4 :
237
+ (`RSE & `jalr_M & (IR_E[`rs]==IR_M[`rd]) & IR_E[`rs]!=0) ? 4 :
238
+ (`RSE & `mf_M & (IR_E[`rs]==IR_M[`rd]) & IR_E[`rs]!=0) ? 7 :
239
+ (`RSE & `mfc0_M & (IR_E[`rs]==IR_M[`rt]) & IR_E[`rs]!=0) ? 5 :
240
+ (`RSE & `cal_r_W & (IR_E[`rs]==IR_W[`rd]) & IR_E[`rs]!=0) ? 2 :
241
+ (`RSE & `cal_i_W & (IR_E[`rs]==IR_W[`rt]) & IR_E[`rs]!=0) ? 2 :
242
+ (`RSE & `load_W & (IR_E[`rs]==IR_W[`rt]) & IR_E[`rs]!=0) ? 2 :
243
+ (`RSE & `mf_W & (IR_E[`rs]==IR_W[`rd]) & IR_E[`rs]!=0) ? 2 :
244
+ (`RSE & `mfc0_W & (IR_E[`rs]==IR_W[`rt]) & IR_E[`rs]!=0) ? 2 :
245
+ (`RSE & `jal_W & (IR_E[`rs]==31) & IR_E[`rs]!=0) ? 2 :
246
+ (`RSE & `jalr_W & (IR_E[`rs]==IR_W[`rd]) & IR_E[`rs]!=0) ? 2 : 0;
247
+
248
+ assign ForwardRTE = (`RTE & `cal_r_M & (IR_E[`rt]==IR_M[`rd]) & IR_E[`rt]!=0) ? 1 :
249
+ (`RTE & `cal_i_M & (IR_E[`rt]==IR_M[`rt]) & IR_E[`rt]!=0) ? 1 :
250
+ (`RTE & `jal_M & (IR_E[`rt]==31) & IR_E[`rt]!=0) ? 4 :
251
+ (`RTE & `jalr_M & (IR_E[`rt]==IR_M[`rd]) & IR_E[`rt]!=0) ? 4 :
252
+ (`RTE & `mf_M & (IR_E[`rt]==IR_M[`rd]) & IR_E[`rt]!=0) ? 7 :
253
+ (`RTE & `mfc0_M & (IR_E[`rt]==IR_M[`rt]) & IR_E[`rt]!=0) ? 5 :
254
+ (`RTE & `cal_r_W & (IR_E[`rt]==IR_W[`rd]) & IR_E[`rt]!=0) ? 2 :
255
+ (`RTE & `cal_i_W & (IR_E[`rt]==IR_W[`rt]) & IR_E[`rt]!=0) ? 2 :
256
+ (`RTE & `load_W & (IR_E[`rt]==IR_W[`rt]) & IR_E[`rt]!=0) ? 2 :
257
+ (`RTE & `mf_W & (IR_E[`rt]==IR_W[`rd]) & IR_E[`rt]!=0) ? 2 :
258
+ (`RTE & `mfc0_W & (IR_E[`rt]==IR_W[`rt]) & IR_E[`rt]!=0) ? 2 :
259
+ (`RTE & `jal_W & (IR_E[`rt]==31) & IR_E[`rt]!=0) ? 2 :
260
+ (`RTE & `jalr_W & (IR_E[`rt]==IR_W[`rd]) & IR_E[`rt]!=0) ? 2 : 0;
261
+
262
+ assign ForwardRTM = (`RTM & `cal_r_W & (IR_M[`rt]==IR_W[`rd]) & IR_M[`rt]!=0) ? 2 :
263
+ (`RTM & `cal_i_W & (IR_M[`rt]==IR_W[`rt]) & IR_M[`rt]!=0) ? 2 :
264
+ (`RTM & `load_W & (IR_M[`rt]==IR_W[`rt]) & IR_M[`rt]!=0) ? 2 :
265
+ (`RTM & `mf_W & (IR_M[`rt]==IR_W[`rd]) & IR_M[`rt]!=0) ? 2 :
266
+ (`RTM & `mfc0_W & (IR_M[`rt]==IR_W[`rt]) & IR_M[`rt]!=0) ? 2 :
267
+ (`RTM & `jal_W & (IR_M[`rt]==31) & IR_M[`rt]!=0) ? 2 :
268
+ (`RTM & `jalr_W & (IR_M[`rt]==IR_W[`rd]) & IR_M[`rt]!=0) ? 2 : 0;
269
+
270
+ assign ForwardERET = ((IR_D==32'h42000018)&&(IR_M[`op]==6'b010000&&IR_M[`rs]==5'b00100&&IR_M[`rd]==14)) ? 1 : 0;
271
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/im.v ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:57:01 11/23/2018
7
+ // Design Name:
8
+ // Module Name: im
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module im(
22
+ input [31:0] PC,
23
+ output[31:0] instruction
24
+ );
25
+
26
+ reg[31:0] im[0:4095];
27
+ initial begin
28
+ $readmemh("code.txt",im);
29
+ $readmemh("code_handler.txt",im,1120,2047);
30
+ end
31
+
32
+ wire[31:0] pc=PC-32'h00003000;
33
+ assign instruction = im[pc[13:2]];
34
+
35
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/mips.v ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:46:09 11/23/2018
7
+ // Design Name:
8
+ // Module Name: mips
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module mips(
22
+ input clk,
23
+ input reset
24
+ );
25
+
26
+ wire [31:0] PrRD;
27
+ wire [6:2] ExcCode;
28
+ wire [31:0] PrAddr;
29
+ wire [31:0] PrWD;
30
+ wire PrWe;
31
+ wire [31:0] DEV_Addr;
32
+ wire [31:0] DEV_WD;
33
+ wire [31:0] DEV0_RD;
34
+ wire [31:0] DEV1_RD;
35
+ wire WeDEV0;
36
+ wire WeDEV1;
37
+ wire IRQ0;
38
+ wire IRQ1;
39
+
40
+ CPU my_CPU(.clk(clk),.reset(reset),.HWInt({4'b0,IRQ1,IRQ0}),.PrRD(PrRD),.ExcCode(ExcCode),.PrAddr(PrAddr),.PrWD(PrWD),.PrWe(PrWe));
41
+ Bridge my_Bridge(PrAddr,PrWe,PrRD,PrWD,DEV_Addr,DEV_WD,DEV0_RD,DEV1_RD,WeDEV0,WeDEV1);
42
+ DEV0 my_time0(clk,reset,DEV_Addr,WeDEV0,DEV_WD,DEV0_RD,IRQ0);
43
+ DEV1 my_time1(clk,reset,DEV_Addr,WeDEV1,DEV_WD,DEV1_RD,IRQ1);
44
+
45
+ assign ExcCode = (IRQ0||IRQ1) ? 5'b00000 : 5'b11111;
46
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/mux.v ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:31:53 11/23/2018
7
+ // Design Name:
8
+ // Module Name: mux
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module mux(
22
+ input [31:0] EXT_E,
23
+ input [31:0] IR_E,
24
+ input [31:0] IR_W,
25
+ input [31:0] DR_Wnew,
26
+ input [31:0] AO_W,
27
+ input [31:0] MDO_W,
28
+ input [31:0] PC8_W,
29
+ input [31:0] CP0_W,
30
+ input [31:0] MFRSE,
31
+ input [31:0] MFRTE,
32
+ input [31:0] High,
33
+ input [31:0] Low,
34
+ input ALUasel,
35
+ input ALUbsel,
36
+ input if_mfhi,
37
+ input if_mflo,
38
+ input [1:0] RegDst,
39
+ input [2:0] MemtoReg,
40
+ output reg[31:0] ALU_A,
41
+ output reg[31:0] ALU_B,
42
+ output reg[4:0] MUX_A3,
43
+ output reg[31:0] MUX_WD,
44
+ output reg[31:0] MD_out
45
+ );
46
+
47
+ always@(*) begin
48
+ case(ALUasel)
49
+ 1'b0: ALU_A<=MFRSE;
50
+ 1'b1: ALU_A<={27'b0,IR_E[10:6]};
51
+ endcase
52
+
53
+ case(ALUbsel)
54
+ 1'b0: ALU_B<=MFRTE;
55
+ 1'b1: ALU_B<=EXT_E;
56
+ endcase
57
+
58
+ case(RegDst)
59
+ 2'b00: MUX_A3<=IR_W[20:16];
60
+ 2'b01: MUX_A3<=IR_W[15:11];
61
+ 2'b10: MUX_A3<=32'h1f;
62
+ 2'b11: MUX_A3<=0;
63
+ endcase
64
+
65
+ case(MemtoReg)
66
+ 3'b000: MUX_WD<=AO_W;
67
+ 3'b001: MUX_WD<=DR_Wnew;
68
+ 3'b010: MUX_WD<=PC8_W;
69
+ 3'b011: MUX_WD<=MDO_W;
70
+ 3'b100: MUX_WD<=CP0_W;
71
+ endcase
72
+
73
+ if(if_mfhi) MD_out<=High;
74
+ else if(if_mflo) MD_out<=Low;
75
+ else MD_out<=0;
76
+ end
77
+
78
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/npc.v ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:13:22 11/23/2018
7
+ // Design Name:
8
+ // Module Name: npc
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module npc(
22
+ input [31:0] PC4,
23
+ input [31:0] PC4D,
24
+ input [25:0] I26,
25
+ input [31:0] MFRSD,
26
+ input [31:0] EPC,
27
+ input Zero,
28
+ input more,
29
+ input less,
30
+ input if_beq,
31
+ input if_bne,
32
+ input if_bgtz,
33
+ input if_blez,
34
+ input if_bgez,
35
+ input if_bltz,
36
+ input if_j,//j��jal
37
+ input[1:0] PC_sel,
38
+ input Interrupt,
39
+ output reg[31:0] next_pc
40
+ );
41
+ reg[31:0] NPC;
42
+ always@(*) begin
43
+ if((if_beq&&Zero)||(if_bne&&!Zero)||(if_bgtz&&more)||(if_blez&&!more)||(if_bgez&&!less)||(if_bltz&&less)) begin
44
+ NPC<=PC4D+{{14{I26[15]}},I26[15:0],2'b00};
45
+ end
46
+ else if(if_j) begin
47
+ NPC<={PC4D[31:28],I26[25:0],2'b00};
48
+ end
49
+ else begin
50
+ NPC<=PC4D+4;
51
+ end
52
+ case(PC_sel)
53
+ 2'b00: next_pc<=PC4;
54
+ 2'b01: next_pc<=MFRSD;
55
+ 2'b10: next_pc<=NPC;
56
+ 2'b11: next_pc<=EPC;
57
+ endcase
58
+ if(Interrupt) begin
59
+ next_pc<=32'h00004180;
60
+ end
61
+ end
62
+
63
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/pc.v ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:02:00 11/23/2018
7
+ // Design Name:
8
+ // Module Name: pc
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module pc(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input[31:0] next_pc,
26
+ output reg[31:0] pc,
27
+ output [4:0] excode_F
28
+ );
29
+
30
+ initial begin
31
+ pc<=32'h00003000;
32
+ end
33
+
34
+ assign excode_F = ((pc[1:0]!=2'b00)||(pc < 32'h00003000)||(pc > 32'h00004ffc)) ? 4 : 0;
35
+
36
+ always@(posedge clk) begin
37
+ if(reset) begin
38
+ pc <= 32'h00003000;
39
+ end
40
+ else if(en) begin
41
+ pc <= next_pc;
42
+ end
43
+ else begin
44
+ pc <= pc;
45
+ end
46
+ end
47
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_6bd位保持1/test.v ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////
4
+ // Company:
5
+ // Engineer:
6
+ //
7
+ // Create Date: 23:05:00 11/25/2018
8
+ // Design Name: mips
9
+ // Module Name: E:/Verilog/p5/test.v
10
+ // Project Name: p5
11
+ // Target Device:
12
+ // Tool versions:
13
+ // Description:
14
+ //
15
+ // Verilog Test Fixture created by ISE for module: mips
16
+ //
17
+ // Dependencies:
18
+ //
19
+ // Revision:
20
+ // Revision 0.01 - File Created
21
+ // Additional Comments:
22
+ //
23
+ ////////////////////////////////////////////////////////////////////////////////
24
+
25
+ module test;
26
+
27
+ // Inputs
28
+ reg clk;
29
+ reg reset;
30
+
31
+ // Instantiate the Unit Under Test (UUT)
32
+ mips uut (
33
+ .clk(clk),
34
+ .reset(reset)
35
+ );
36
+
37
+ initial begin
38
+ clk = 0;
39
+ reset = 1;
40
+ #12 reset = 0;
41
+ end
42
+
43
+ always #10 clk = ~clk;
44
+
45
+
46
+ endmodule
47
+
48
+
aptx1231_BUAA_CO/P7/p7_旧/p7_7/Bridge.v ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 23:05:11 12/12/2018
7
+ // Design Name:
8
+ // Module Name: Bridge
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module Bridge(
22
+ input [31:0] PrAddr,
23
+ input PrWE,
24
+ output [31:0] PrRD,
25
+ input [31:0] PrWD,
26
+ output [31:0] DEV_Addr,
27
+ output [31:0] DEV_WD,
28
+ input [31:0] DEV0_RD,
29
+ input [31:0] DEV1_RD,
30
+ output WeDEV0,
31
+ output WeDEV1
32
+ );
33
+
34
+ `define DEBUG_DEV_DATA 32'b0
35
+
36
+ //DEV0 00007F00-00007F0B
37
+ //DEV1 00007F10-00007F1B
38
+ wire HitDEV0,HitDEV1;
39
+ assign HitDEV0 = (PrAddr[31:4]==28'h00007F0);
40
+ assign HitDEV1 = (PrAddr[31:4]==28'h00007F1);
41
+
42
+ assign PrRD = (HitDEV0) ? DEV0_RD :
43
+ (HitDEV1) ? DEV1_RD :
44
+ `DEBUG_DEV_DATA;
45
+
46
+ assign WeDEV0 = PrWE&&HitDEV0;
47
+ assign WeDEV1 = PrWE&&HitDEV1;
48
+
49
+ assign DEV_Addr = PrAddr;
50
+ assign DEV_WD = PrWD;
51
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/CP0.v ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 15:46:14 12/13/2018
7
+ // Design Name:
8
+ // Module Name: CP0
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module CP0(
22
+ input[4:0] A1,//��CP0�Ĵ������ ִ��MFC0ָ��ʱ����
23
+ input[4:0] A2,//дCP0�Ĵ������ ִ��MTC0ָ��ʱ����
24
+ input[31:0] DIn,//CP0�Ĵ�����д������ ִ��MTC0ָ��ʱ���� ��������GPR
25
+ input[31:0] PC,//�ж�/�쳣ʱ��PC
26
+ input[31:0] IR_M,//ָ��
27
+ input Zero,
28
+ input more,
29
+ input less,
30
+ input if_bd,
31
+ input[6:2] ExcCode,//�ж�/�쳣������
32
+ input[5:0] HWInt,//6���豸�ж�
33
+ input We,//CP0дʹ�� ִ��MTC0ָ��ʱ����
34
+ input EXLSet,//������λSR��EXL(EXLΪ1) ��ˮ����M�׶β���
35
+ input EXLClr,//�������SR��EXL(EXLΪ0) ִ��ERETָ��ʱ����
36
+ input clk,
37
+ input reset,
38
+ output Interrupt,//�жϺ��쳣 ��HWInt/IM/EXL/IE�ĺ���
39
+ output[31:0] EPC,//EPC�Ĵ��������NPC
40
+ output[31:0] DOut //CP0�Ĵ������������ ִ��MFC0ָ��ʱ���������������GRF
41
+ );
42
+
43
+ `define SR 12
44
+ `define CAUSE 13
45
+ `define EPC 14
46
+ `define PRID 15
47
+
48
+ `define R 6'b000000
49
+ `define beq 6'b000100
50
+ `define bne 6'b000101
51
+ `define bgtz 6'b000111
52
+ `define blez 6'b000110
53
+ `define jal 6'b000011
54
+ `define jr 6'b001000
55
+ `define j 6'b000010
56
+ `define jalr 6'b001001
57
+
58
+ //SR {16'b0, im, 8'b0, exl, ie}
59
+ reg[15:10] im; //6λ�ж�����λ���ֱ��Ӧ6���ⲿ�ж� 1-�����жϣ�0-��ֹ�ж�
60
+ reg exl;//EXL���쳣�� 1-�����쳣�����������жϣ�0-�����ж�
61
+ reg ie;//ȫ���ж�ʹ�� 1-�����жϣ�0-��ֹ�ж�
62
+ //CAUSE {bd, 15'b0, hwint_pend[15:10], 3'b0, exccode[6:2], 2'b0}
63
+ reg bd;//�쳣�������ӳٲ۵�ָ��Ļ� bd=1
64
+ reg[6:2] exccode;
65
+ reg[15:10] hwint_pend;//��clock�����ز��ϵı����ⲿ6���ж�(HWInt[5:0])
66
+ //EPC
67
+ reg[31:0] epc;
68
+ //PRID
69
+ reg[31:0] prid = 32'h12345678;
70
+ wire Exception;//�쳣
71
+ wire IntReq;//�ж�
72
+ assign EPC = epc;
73
+ //�� ����SR/Cause/EPC/PRId�⣬���õļĴ���һ�����0
74
+ assign DOut = (A1==`SR) ? {16'b0, im, 8'b0, exl, ie} :
75
+ (A1==`CAUSE) ? {bd, 15'b0, hwint_pend[15:10], 3'b0, exccode[6:2], 2'b0} :
76
+ (A1==`EPC) ? epc :
77
+ (A1==`PRID) ? prid : 0;
78
+ assign IntReq = (|(HWInt[5:0] & im[15:10])) & ie & !exl ;
79
+ assign Exception = (ExcCode>0) ? 1'b1 : 1'b0;
80
+ assign Interrupt = IntReq||Exception;
81
+
82
+ integer i;
83
+ always@(posedge clk) begin
84
+ hwint_pend <= HWInt;//��clock�����ز��ϵı����ⲿ6���ж�(HWInt[5:0])
85
+ if(reset) begin
86
+ im<=6'b0;//SR
87
+ exl<=1'b0;
88
+ ie<=1'b0;
89
+ hwint_pend<=6'b0;//CAUSE
90
+ bd<=0;
91
+ exccode<=0;
92
+ epc<=0;//EPC
93
+ end
94
+ else begin
95
+ epc <= (Interrupt&&bd) ? {PC[31:2], 2'b00} - 4 :
96
+ (Interrupt&&!bd) ? {PC[31:2], 2'b00} :
97
+ epc;
98
+ if(bd==1'b0) begin
99
+ bd <= (((IR_M[31:26]==`j)||(IR_M[31:26]==`jal)||(IR_M[31:26]==`beq&&Zero)||(IR_M[31:26]==`bne&&!Zero)||(IR_M[31:26]==`blez&&!more)||(IR_M[31:26]==`bgtz&&more)
100
+ ||(IR_M[31:26]==`R&&(IR_M[5:0]==`jr||IR_M[5:0]==`jalr))||(IR_M[31:26]==6'b000001&&IR_M[20:16]==5'b00000&&less)||(IR_M[31:26]==6'b000001&&IR_M[20:16]==5'b00001&&!less)));
101
+ end
102
+ if(We) begin //�
103
+ case(A2)
104
+ `SR: begin
105
+ {im, exl, ie} <= {DIn[15:10], DIn[1], DIn[0]};
106
+ end
107
+ `CAUSE: begin
108
+ hwint_pend <= DIn[15:10];
109
+ end
110
+ `EPC: begin
111
+ epc <= DIn;
112
+ end
113
+ `PRID: begin
114
+ prid <= DIn;
115
+ end
116
+ default: begin
117
+ end
118
+ endcase
119
+ end
120
+ if(EXLSet||Interrupt) begin
121
+ exl<=1'b1;
122
+ exccode<=ExcCode;
123
+ end
124
+ if(EXLClr) begin
125
+ exl<=1'b0;
126
+ bd<=1'b0;
127
+ end
128
+ //if(IntReq) begin
129
+ // bd<=1'b0;
130
+ //end
131
+ end
132
+ end
133
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/CPU.v ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 11:30:40 12/15/2018
7
+ // Design Name:
8
+ // Module Name: CPU
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module CPU(
22
+ input clk,
23
+ input reset,
24
+ input [7:2] HWInt,
25
+ input [31:0] PrRD,
26
+ input [6:2] ExcCode,
27
+ output [31:0] PrAddr,
28
+ output [31:0] PrWD,
29
+ output PrWe
30
+ );
31
+
32
+ `define op 31:26
33
+ `define rs 25:21
34
+ `define rt 20:16
35
+ `define rd 15:11
36
+ `define shamt 10:6
37
+ `define func 5:0
38
+ `define imm16 15:0
39
+ `define imm26 25:0
40
+
41
+ wire PC_en;
42
+ wire[1:0] PC_sel;
43
+ wire[31:0] PC;
44
+ wire[31:0] PC4;
45
+ wire[31:0] Instr;
46
+ wire ID_en;
47
+ wire EX_en;
48
+ wire MEM_en;
49
+ wire WB_en;
50
+ wire ID_reset;
51
+ wire EX_reset;
52
+ wire MEM_reset;
53
+ wire WB_reset;
54
+ wire[31:0] IR_D;
55
+ wire[31:0] PC_D;
56
+ wire[31:0] PC4_D;
57
+ wire[31:0] PC8_D;
58
+ wire[31:0] IR_E;
59
+ wire[31:0] PC_E;
60
+ wire[31:0] PC4_E;
61
+ wire[31:0] PC8_E;
62
+ wire[31:0] IR_M;
63
+ wire[31:0] PC_M;
64
+ wire[31:0] PC4_M;
65
+ wire[31:0] PC8_M;
66
+ wire[31:0] IR_W;
67
+ wire[31:0] PC_W;
68
+ wire[31:0] PC4_W;
69
+ wire[31:0] PC8_W;
70
+ wire Zero;
71
+ wire Zero_E;
72
+ wire Zero_M;
73
+ wire more;
74
+ wire less;
75
+ wire more_E;
76
+ wire less_E;
77
+ wire more_M;
78
+ wire less_M;
79
+ wire if_beq;
80
+ wire if_bne;
81
+ wire if_blez;
82
+ wire if_bgtz;
83
+ wire if_bltz;
84
+ wire if_bgez;
85
+ wire if_j;
86
+ wire[1:0] EXTop;
87
+ wire if_sh;
88
+ wire if_sb;
89
+
90
+ wire[3:0] ALUCtrl;
91
+ wire[1:0] RegDst;
92
+ wire ALUASrc;
93
+ wire ALUBSrc;
94
+ wire RegWrite;
95
+ wire MemRead;
96
+ wire MemWrite;
97
+ wire [2:0] MemtoReg;
98
+ wire [2:0] dataOp;
99
+ wire [1:0] multdivOp;
100
+ wire if_mthi;
101
+ wire if_mtlo;
102
+ wire if_mfhi;
103
+ wire if_mflo;
104
+ wire start;
105
+ wire Busy;
106
+ wire[31:0] High;
107
+ wire[31:0] Low;
108
+
109
+ wire [31:0] RS_E;
110
+ wire [31:0] RT_E;
111
+ wire [31:0] EXT_E;
112
+ wire [31:0] AO_M;
113
+ wire [31:0] MDO_M;
114
+ wire [31:0] RT_M;
115
+ wire [31:0] DR_W;
116
+ wire [31:0] DR_Wnew;
117
+ wire [31:0] AO_W;
118
+ wire [31:0] MDO_W;
119
+
120
+ wire[4:0] MUX_A3;
121
+ wire[31:0] MUX_WD;
122
+ wire[31:0] RF_RD1;
123
+ wire[31:0] RF_RD2;
124
+ wire[31:0] MFRSD;
125
+ wire[31:0] MFRTD;
126
+ wire[31:0] MFRSE;
127
+ wire[31:0] MFRTE;
128
+ wire[31:0] MFRTM;
129
+ wire[31:0] MFERET;
130
+ wire[31:0] EXT_out;
131
+ wire[31:0] ALU_A;
132
+ wire[31:0] ALU_B;
133
+ wire[31:0] ALU_out;
134
+ wire[31:0] MD_out;
135
+ wire[31:0] DM_out;
136
+ wire[31:0] next_pc;
137
+ wire[2:0] ForwardRSD;
138
+ wire[2:0] ForwardRTD;
139
+ wire[2:0] ForwardRSE;
140
+ wire[2:0] ForwardRTE;
141
+ wire[2:0] ForwardRTM;
142
+ wire[2:0] ForwardERET;
143
+
144
+ assign EX_en = 1;
145
+ assign MEM_en = 1;
146
+ assign WB_en = 1;
147
+ assign ID_reset = 0;
148
+ assign MEM_reset = 0;
149
+ assign WB_reset = 0;
150
+ assign PC4 = PC + 4;
151
+
152
+ wire[6:2] excode_F,excode_D,excode_E,excode_M;
153
+ wire[6:2] excode_outF,excode_outD,excode_outE,excode_outM;
154
+ wire over;
155
+ wire cp0WE;
156
+ wire[31:0] CP0_M;
157
+ wire[31:0] CP0_W;
158
+ wire[31:0] EPCout;
159
+ wire Interrupt;
160
+ wire EXLSet;
161
+ wire EXLClr;
162
+ wire[31:0] DMDEV;
163
+ wire if_eret;
164
+
165
+ assign PrAddr = AO_M;
166
+ assign PrWD = MFRTM;
167
+ assign PrWe = (MemWrite&&(~Interrupt));//û�����쳣�ſ���д
168
+ assign EXLSet = Interrupt;
169
+ assign EXLClr = (IR_M[`op]==6'b010000&&IR_M[`func]==6'b011000); //eret
170
+
171
+ pc my_pc(clk,reset,PC_en||Interrupt,next_pc,PC,excode_F);
172
+ im my_im(PC,Instr);
173
+
174
+ ID my_ID(clk,(ID_reset||reset||Interrupt||(IR_D==32'h42000018&&ID_en)),ID_en,Instr,PC,IR_D,PC_D,PC4_D,PC8_D,excode_D);
175
+ ExcCode my_exc_D(clk,ID_reset||reset||Interrupt,ID_en,excode_F,excode_outD);
176
+ controller my_controllerD(.op(IR_D[`op]),.func(IR_D[`func]),.rs(IR_D[`rs]),.rt(IR_D[`rt]),.ExtOp(EXTop),.if_beq(if_beq),.if_bne(if_bne),
177
+ .if_blez(if_blez),.if_bgtz(if_bgtz),.if_bgez(if_bgez),.if_bltz(if_bltz),.if_j(if_j),.PCsel(PC_sel));
178
+ grf my_grf(clk,reset,RegWrite,IR_D[`rs],IR_D[`rt],MUX_A3,MUX_WD,PC_W,RF_RD1,RF_RD2);
179
+ cmp my_cmp(MFRSD,MFRTD,Zero,more,less);
180
+ ext my_ext(IR_D[`imm16],EXTop,EXT_out);
181
+ npc my_npc(PC4,PC4_D,IR_D[`imm26],MFRSD,MFERET,Zero,more,less,if_beq,if_bne,if_bgtz,if_blez,if_bgez,if_bltz,if_j,PC_sel,Interrupt,next_pc);
182
+
183
+ EX my_EX(clk,EX_reset||reset||Interrupt,EX_en,over,IR_D,PC_D,PC4_D,PC8_D,MFRSD,MFRTD,EXT_out,Zero,more,less,IR_E,PC_E,PC4_E,PC8_E,RS_E,RT_E,EXT_E,excode_E,Zero_E,more_E,less_E);
184
+ ExcCode my_exc_E(clk,EX_reset||reset||Interrupt,EX_en,(excode_outD==0?excode_D:excode_outD),excode_outE);
185
+ controller my_controllerE(.op(IR_E[`op]),.func(IR_E[`func]),.rs(IR_E[`rs]),.rt(IR_E[`rt]),.ALUCtrl(ALUCtrl),.ALUASrc(ALUASrc),.ALUBSrc(ALUBSrc),
186
+ .multdivOp(multdivOp),.start(start),.if_mthi(if_mthi),.if_mtlo(if_mtlo),.if_mfhi(if_mfhi),.if_mflo(if_mflo));
187
+ alu my_alu(ALU_A,ALU_B,ALUCtrl,ALU_out,over);
188
+ Mult_Div my_Mult_Div(clk,reset,MFRSE,MFRTE,multdivOp,start,if_mthi,if_mtlo,Busy,High,Low);
189
+
190
+ MEM my_MEM(clk,MEM_reset||reset||Interrupt,MEM_en,IR_E,PC_E,PC4_E,PC8_E,ALU_out,MD_out,MFRTE,Zero_E,more_E,less_E,IR_M,PC_M,PC4_M,PC8_M,AO_M,MDO_M,RT_M,excode_M,Zero_M,more_M,less_M);
191
+ ExcCode my_exc_M(clk,MEM_reset||reset||Interrupt,MEM_en,(excode_outE==0?excode_E:excode_outE),excode_outM);
192
+ controller my_controllerM(.op(IR_M[`op]),.func(IR_M[`func]),.rs(IR_M[`rs]),.rt(IR_M[`rt]),.MemRead(MemRead),.MemWrite(MemWrite),.if_sh(if_sh),.if_sb(if_sb),.cp0WE(cp0WE));
193
+ dm my_dm(clk,reset,(MemWrite&&~Interrupt),MemRead,if_sh,if_sb,AO_M,MFRTM,PC_M,DM_out);
194
+
195
+ //�ж����ȼ���
196
+ CP0 my_CP0(.A1(IR_M[`rd]),.A2(IR_M[`rd]),.DIn(MFRTM),.PC(PC_M),.IR_M(IR_M),.Zero(Zero_M),.more(more_M),.less(less_M),.if_bd(excode_E>0||excode_outE>0),
197
+ .ExcCode(((|HWInt)? ExcCode : (excode_outM==0) ? excode_M : excode_outM)),.HWInt(HWInt),.We(cp0WE),.EXLSet(EXLSet),.EXLClr(EXLClr),
198
+ .clk(clk),.reset(reset),.Interrupt(Interrupt),.EPC(EPCout),.DOut(CP0_M));
199
+
200
+ WB my_WB(clk,WB_reset||reset||Interrupt,WB_en,IR_M,PC_M,PC4_M,PC8_M,AO_M,MDO_M,DM_out,CP0_M,IR_W,PC_W,PC4_W,PC8_W,AO_W,MDO_W,DR_W,CP0_W);
201
+ controller my_controllerW(.op(IR_W[`op]),.func(IR_W[`func]),.rs(IR_W[`rs]),.rt(IR_W[`rt]),.RegDst(RegDst),.RegWrite(RegWrite),.MemtoReg(MemtoReg),.dataOp(dataOp));
202
+ DataExt my_DataExt(DR_W,dataOp,AO_W[1:0],DR_Wnew);
203
+
204
+ assign DMDEV = (AO_W>=32'h00000000&&AO_W<=32'h00002ffc) ? DR_Wnew : PrRD;//��dm����dev
205
+
206
+ mux my_mux(EXT_E,IR_E,IR_W,DMDEV,AO_W,MDO_W,PC8_W,CP0_W,MFRSE,MFRTE,High,Low,ALUASrc,ALUBSrc,if_mfhi,if_mflo,RegDst,MemtoReg,ALU_A,ALU_B,MUX_A3,MUX_WD,MD_out);
207
+ forward_mux my_forward(RS_E,RT_E,RT_M,MUX_WD,AO_M,MDO_M,CP0_M,MD_out,PC8_E,PC8_M,PC8_W,RF_RD1,RF_RD2,EPCout,
208
+ ForwardRSD,ForwardRTD,ForwardRSE,ForwardRTE,ForwardRTM,ForwardERET,MFRSD,MFRTD,MFRSE,MFRTE,MFRTM,MFERET);
209
+ hazardUnit my_hazard(IR_D,IR_E,IR_M,IR_W,Busy,start,ID_en,EX_reset,PC_en,ForwardRSD,ForwardRTD,ForwardRSE,ForwardRTE,ForwardRTM,ForwardERET);
210
+
211
+ endmodule
212
+
aptx1231_BUAA_CO/P7/p7_旧/p7_7/DEV0.v ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 22:05:26 12/12/2018
7
+ // Design Name:
8
+ // Module Name: DEV0
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module DEV0(
22
+ input clk,
23
+ input reset,
24
+ input [31:0] Addr,
25
+ input WE,
26
+ input [31:0] DataIn,
27
+ output reg [31:0] DataOut,
28
+ output reg IRQ
29
+ );
30
+
31
+ `define Idle 2'b00
32
+ `define Counting 2'b01
33
+ `define Interrupt 2'b10
34
+
35
+ reg[1:0] state;
36
+ reg[31:0] PRESET;
37
+ reg[31:0] COUNT;
38
+ reg[2:1] Mode;//ģʽѡ��
39
+ reg IM;//�����
40
+ reg Enable;//������ʹ��
41
+ reg irq;
42
+
43
+ always@(posedge clk) begin
44
+ if(reset) begin
45
+ PRESET<=0;
46
+ COUNT<=0;
47
+ state<=`Idle;
48
+ IM<=0;
49
+ Enable<=0;
50
+ irq<=0;
51
+ end
52
+ if(WE) begin
53
+ case(Addr)
54
+ 32'h0000_7f00: begin //ctrl
55
+ {IM,Mode,Enable} <= DataIn[3:0];
56
+ //������������ʱ����������ʹ�ܱ� store ��ָ���޸�Ϊ 0 ��ֹͣ����
57
+ if(state == `Counting && DataIn[0]==1'b0) begin
58
+ COUNT<=PRESET;
59
+ end
60
+ end
61
+ 32'h0000_7f04: begin
62
+ PRESET<=DataIn;
63
+ end
64
+ 32'h0000_7f08: begin
65
+ //COUNT���ᱻд��
66
+ end
67
+ default: begin
68
+ //ʲô������
69
+ end
70
+ endcase
71
+ end
72
+ else begin
73
+ case(state)
74
+ `Idle: begin
75
+ if(Enable==1'b1) begin
76
+ state<=`Counting;
77
+ COUNT<=PRESET;
78
+ irq<=0;
79
+ end
80
+ else begin
81
+ state<=`Idle;
82
+ end
83
+ end
84
+
85
+ `Counting: begin
86
+ if(Enable==1'b0) begin
87
+ state<=`Idle;
88
+ end
89
+ else if(Enable==1'b1&&COUNT<=1) begin
90
+ state<=`Interrupt;
91
+ irq<=0;
92
+ end
93
+ else if(Enable==1'b1) begin
94
+ state<=`Counting;
95
+ COUNT<=COUNT-1;
96
+ irq<=0;
97
+ end
98
+ end
99
+
100
+ `Interrupt: begin
101
+ if(Enable==1'b1) begin
102
+ irq<=1;
103
+ if(Mode==2'b00) begin
104
+ Enable<=0;
105
+ state<=`Idle;
106
+ end
107
+ else if(Mode==2'b01) begin
108
+ state<=`Counting;
109
+ COUNT<=PRESET;
110
+ end
111
+ end
112
+ end
113
+ endcase
114
+ end
115
+ //IRQ<=IM&&(Mode==2'b0)&&(COUNT==0);
116
+ IRQ<=IM&&irq;
117
+ case(Addr[3:2])
118
+ 2'b00: begin
119
+ DataOut<={28'b0,IM,Mode,Enable};
120
+ end
121
+ 2'b01: begin
122
+ DataOut<=PRESET;
123
+ end
124
+ 2'b10: begin
125
+ DataOut<=COUNT;
126
+ end
127
+ 2'b11: begin
128
+ DataOut<=0;
129
+ end
130
+ endcase
131
+ end
132
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/DEV1.v ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 22:40:05 12/12/2018
7
+ // Design Name:
8
+ // Module Name: DEV1
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module DEV1(
22
+ input clk,
23
+ input reset,
24
+ input [31:0] Addr,
25
+ input WE,
26
+ input [31:0] DataIn,
27
+ output reg [31:0] DataOut,
28
+ output reg IRQ
29
+ );
30
+
31
+ `define Idle 2'b00
32
+ `define Counting 2'b01
33
+ `define Interrupt 2'b10
34
+
35
+ reg[1:0] state;
36
+ reg[31:0] PRESET;
37
+ reg[31:0] COUNT;
38
+ reg[2:1] Mode;
39
+ reg IM;
40
+ reg Enable;
41
+ reg irq;
42
+
43
+ always@(posedge clk) begin
44
+ if(reset) begin
45
+ PRESET<=0;
46
+ COUNT<=0;
47
+ state<=`Idle;
48
+ IM<=0;
49
+ Enable<=0;
50
+ irq<=0;
51
+ end
52
+ if(WE) begin
53
+ case(Addr)
54
+ 32'h0000_7f10: begin //ctrl
55
+ {IM,Mode,Enable} <= DataIn[3:0];
56
+ //������������ʱ����������ʹ�ܱ� store ��ָ���޸�Ϊ 0 ��ֹͣ����
57
+ if(state == `Counting && DataIn[0]==1'b0) begin
58
+ COUNT<=PRESET;
59
+ end
60
+ end
61
+ 32'h0000_7f14: begin
62
+ PRESET<=DataIn;
63
+ end
64
+ 32'h0000_7f18: begin
65
+
66
+ end
67
+ default: begin
68
+
69
+ end
70
+ endcase
71
+ end
72
+ else begin
73
+ case(state)
74
+ `Idle: begin
75
+ if(Enable==1'b1) begin
76
+ state<=`Counting;
77
+ COUNT<=PRESET;
78
+ irq<=0;
79
+ end
80
+ else begin
81
+ state<=`Idle;
82
+ end
83
+ end
84
+
85
+ `Counting: begin
86
+ if(Enable==1'b0) begin
87
+ state<=`Idle;
88
+ end
89
+ else if(Enable==1'b1&&COUNT<=1) begin
90
+ state<=`Interrupt;
91
+ irq<=0;
92
+ end
93
+ else if(Enable==1'b1) begin
94
+ state<=`Counting;
95
+ COUNT<=COUNT-1;
96
+ irq<=0;
97
+ end
98
+ end
99
+
100
+ `Interrupt: begin
101
+ if(Enable==1'b1) begin
102
+ irq<=1;
103
+ if(Mode==2'b00) begin
104
+ Enable<=0;
105
+ state<=`Idle;
106
+ end
107
+ else if(Mode==2'b01) begin
108
+ state<=`Counting;
109
+ COUNT<=PRESET;
110
+ end
111
+ end
112
+ end
113
+ endcase
114
+ end
115
+ IRQ<=irq&&IM;
116
+ case({WE,Addr[3:2]})
117
+ 3'b000: begin
118
+ DataOut<={28'b0,IM,Mode,Enable};
119
+ end
120
+ 3'b001: begin
121
+ DataOut<=PRESET;
122
+ end
123
+ 3'b010: begin
124
+ DataOut<=COUNT;
125
+ end
126
+ 3'b011: begin
127
+ DataOut<=0;
128
+ end
129
+ endcase
130
+ end
131
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/DataExt.v ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 11:13:10 12/01/2018
7
+ // Design Name:
8
+ // Module Name: DataExt
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module DataExt(
22
+ input [31:0] Din,
23
+ input [2:0] dataOp,
24
+ input [1:0] Addr,
25
+ output reg[31:0] Dout
26
+ );
27
+
28
+ always@(*) begin
29
+ case(dataOp)
30
+ 3'b000: Dout<=Din;
31
+ 3'b001: begin//lbu
32
+ case(Addr)
33
+ 2'b00: Dout<={24'b0,Din[7:0]};
34
+ 2'b01: Dout<={24'b0,Din[15:8]};
35
+ 2'b10: Dout<={24'b0,Din[23:16]};
36
+ 2'b11: Dout<={24'b0,Din[31:24]};
37
+ endcase
38
+ end
39
+ 3'b010: begin//lb
40
+ case(Addr)
41
+ 2'b00: Dout<={{24{Din[7]}},Din[7:0]};
42
+ 2'b01: Dout<={{24{Din[15]}},Din[15:8]};
43
+ 2'b10: Dout<={{24{Din[23]}},Din[23:16]};
44
+ 2'b11: Dout<={{24{Din[31]}},Din[31:24]};
45
+ endcase
46
+ end
47
+ 3'b011: begin//lhu
48
+ case(Addr[1])
49
+ 1'b0: Dout<={24'b0,Din[15:0]};
50
+ 1'b1: Dout<={24'b0,Din[31:16]};
51
+ endcase
52
+ end
53
+ 3'b100: begin//lh
54
+ case(Addr[1])
55
+ 1'b0: Dout<={{16{Din[15]}},Din[15:0]};
56
+ 1'b1: Dout<={{16{Din[31]}},Din[31:16]};
57
+ endcase
58
+ end
59
+ endcase
60
+ end
61
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/EX.v ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:21:31 11/23/2018
7
+ // Design Name:
8
+ // Module Name: EX
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module EX(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input over,
26
+ input [31:0] IR_D,
27
+ input [31:0] PC_D,
28
+ input [31:0] PC4_D,
29
+ input [31:0] PC8_D,
30
+ input [31:0] RF_RD1,
31
+ input [31:0] RF_RD2,
32
+ input [31:0] EXT,
33
+ input Zero,
34
+ input more,
35
+ input less,
36
+ output reg[31:0] IR_E,
37
+ output reg[31:0] PC_E,
38
+ output reg[31:0] PC4_E,
39
+ output reg[31:0] PC8_E,
40
+ output reg[31:0] RS_E,
41
+ output reg[31:0] RT_E,
42
+ output reg[31:0] EXT_E,
43
+ output [6:2] excode_E,
44
+ output reg Zero_E,
45
+ output reg more_E,
46
+ output reg less_E
47
+ );
48
+
49
+ `define op 31:26
50
+ `define rt 20:16
51
+ `define func 5:0
52
+
53
+ `define R 6'b000000
54
+ `define lw 6'b100011
55
+ `define lb 6'b100000
56
+ `define lbu 6'b100100
57
+ `define lh 6'b100001
58
+ `define lhu 6'b100101
59
+ `define sw 6'b101011
60
+ `define sb 6'b101000
61
+ `define sh 6'b101001
62
+ `define add 6'b100000
63
+ `define sub 6'b100010
64
+ `define addi 6'b001000
65
+
66
+ initial begin
67
+ IR_E <= 0;
68
+ end
69
+
70
+ assign excode_E = ((IR_E[`op]==`lw||IR_E[`op]==`lb||IR_E[`op]==`lbu||IR_E[`op]==`lh||IR_E[`op]==`lhu)&&over) ? 4 :
71
+ ((IR_E[`op]==`sw||IR_E[`op]==`sh||IR_E[`op]==`sb)&&over) ? 5 :
72
+ (IR_E[`op]==`R&&(IR_E[`func]==`add||IR_E[`func]==`sub||IR_E[`func]==`addi)&&over) ? 12 : 0;
73
+
74
+ always@(posedge clk) begin
75
+ if(reset) begin
76
+ IR_E <= 32'h00000000;
77
+ end
78
+ else if(en) begin
79
+ IR_E <= IR_D;
80
+ PC_E <= PC_D;
81
+ PC4_E <= PC4_D;
82
+ PC8_E <= PC8_D;
83
+ RS_E <= RF_RD1;
84
+ RT_E <= RF_RD2;
85
+ EXT_E <= EXT;
86
+ Zero_E <= Zero;
87
+ more_E <= more;
88
+ less_E <= less;
89
+ end
90
+ end
91
+
92
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/ExcCode.v ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 00:29:07 12/15/2018
7
+ // Design Name:
8
+ // Module Name: ExcCode
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module ExcCode(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input [4:0] ExcCodeIn,
26
+ output reg[4:0] ExcCodeOut
27
+ );
28
+
29
+ initial begin
30
+ ExcCodeOut<=0;
31
+ end
32
+
33
+ always@(posedge clk) begin
34
+ if(reset) begin
35
+ ExcCodeOut<=0;
36
+ end
37
+ else if(en) begin
38
+ ExcCodeOut<=ExcCodeIn;
39
+ end
40
+ end
41
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/ID.v ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:56:51 11/23/2018
7
+ // Design Name:
8
+ // Module Name: ID
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module ID(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input [31:0] Instr,
26
+ input [31:0] PC,
27
+ output reg[31:0] IR_D,
28
+ output reg[31:0] PC_D,
29
+ output reg[31:0] PC4_D,
30
+ output reg[31:0] PC8_D,
31
+ output [4:0] excode_D
32
+ );
33
+
34
+ `define op 31:26
35
+ `define rs 25:21
36
+ `define rt 20:16
37
+ `define func 5:0
38
+
39
+ `define R 6'b000000
40
+ `define j 6'b000010
41
+ `define lw 6'b100011
42
+ `define lb 6'b100000
43
+ `define lbu 6'b100100
44
+ `define lh 6'b100001
45
+ `define lhu 6'b100101
46
+ `define sw 6'b101011
47
+ `define sb 6'b101000
48
+ `define sh 6'b101001
49
+ `define lui 6'b001111
50
+ `define ori 6'b001101
51
+ `define andi 6'b001100
52
+ `define xori 6'b001110
53
+ `define addi 6'b001000
54
+ `define addiu 6'b001001
55
+ `define slti 6'b001010
56
+ `define sltiu 6'b001011
57
+ `define beq 6'b000100
58
+ `define bne 6'b000101
59
+ `define bgtz 6'b000111
60
+ `define blez 6'b000110
61
+ `define jal 6'b000011
62
+ `define addu 6'b100001
63
+ `define add 6'b100000
64
+ `define subu 6'b100011
65
+ `define sub 6'b100010
66
+ `define and_ 6'b100100
67
+ `define or_ 6'b100101
68
+ `define xor_ 6'b100110
69
+ `define nor_ 6'b100111
70
+ `define sll 6'b000000
71
+ `define srl 6'b000010
72
+ `define sra 6'b000011
73
+ `define sllv 6'b000100
74
+ `define srlv 6'b000110
75
+ `define srav 6'b000111
76
+ `define jr 6'b001000
77
+ `define jalr 6'b001001
78
+ `define mult 6'b011000
79
+ `define multu 6'b011001
80
+ `define div 6'b011010
81
+ `define divu 6'b011011
82
+ `define mfhi 6'b010000
83
+ `define mflo 6'b010010
84
+ `define mthi 6'b010001
85
+ `define mtlo 6'b010011
86
+ `define slt 6'b101010
87
+ `define sltu 6'b101011
88
+
89
+ initial begin
90
+ IR_D <= 0;
91
+ end
92
+
93
+ wire RI;//�����쳣 ����ʶ��op func
94
+ assign RI = (IR_D[`op]==`j)||(IR_D[`op]==`jal)
95
+ ||(IR_D[`op]==`beq)||(IR_D[`op]==`bne)||(IR_D[`op]==`blez)||(IR_D[`op]==`bgtz)||(IR_D[`op]==6'b000001&&IR_D[`rt]==5'b00000)||(IR_D[`op]==6'b000001&&IR_D[`rt]==5'b00001)
96
+ ||IR_D[`op]==`lui||IR_D[`op]==`ori||IR_D[`op]==`addi||IR_D[`op]==`addiu||IR_D[`op]==`andi||IR_D[`op]==`xori||IR_D[`op]==`slti||IR_D[`op]==`sltiu
97
+ ||IR_D[`op]==`lw||IR_D[`op]==`lb||IR_D[`op]==`lbu||IR_D[`op]==`lh||IR_D[`op]==`lhu
98
+ ||IR_D[`op]==`sw||IR_D[`op]==`sh||IR_D[`op]==`sb
99
+ ||(IR_D[`op]==6'b010000&&(IR_D[`rs]==5'b00000||IR_D[`rs]==5'b00100))
100
+ ||(IR_D==32'h42000018)
101
+ ||(IR_D[`op]==`R&&(IR_D[`func]==`jr||IR_D[`func]==`jalr||IR_D[`func]==`addu||IR_D[`func]==`subu||IR_D[`func]==`add||IR_D[`func]==`sub||IR_D[`func]==`sll||IR_D[`func]==`srl||IR_D[`func]==`sra
102
+ ||IR_D[`func]==`sllv||IR_D[`func]==`srlv||IR_D[`func]==`srav||IR_D[`func]==`and_||IR_D[`func]==`or_||IR_D[`func]==`nor_||IR_D[`func]==`xor_||IR_D[`func]==`slt||IR_D[`func]==`sltu
103
+ ||IR_D[`func]==`mult||IR_D[`func]==`multu||IR_D[`func]==`div||IR_D[`func]==`divu||IR_D[`func]==`mfhi||IR_D[`func]==`mflo||IR_D[`func]==`mthi||IR_D[`func]==`mtlo));
104
+
105
+ assign excode_D = (RI==0) ? 10 : 0;
106
+
107
+ always@(posedge clk) begin
108
+ if(reset) begin
109
+ IR_D <= 32'h00000000;
110
+ end
111
+ else if(en) begin
112
+ IR_D <= Instr;
113
+ PC_D <= PC;
114
+ PC4_D <= PC+4;
115
+ PC8_D <= PC+8;
116
+ end
117
+ end
118
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/MEM.v ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:27:06 11/23/2018
7
+ // Design Name:
8
+ // Module Name: MEM
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module MEM(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input [31:0] IR_E,
26
+ input [31:0] PC_E,
27
+ input [31:0] PC4_E,
28
+ input [31:0] PC8_E,
29
+ input [31:0] ALU,
30
+ input [31:0] Mult_Div,
31
+ input [31:0] RT_E,
32
+ input Zero_E,
33
+ input more_E,
34
+ input less_E,
35
+ output reg[31:0] IR_M,
36
+ output reg[31:0] PC_M,
37
+ output reg[31:0] PC4_M,
38
+ output reg[31:0] PC8_M,
39
+ output reg[31:0] AO_M,
40
+ output reg[31:0] MDO_M,
41
+ output reg[31:0] RT_M,
42
+ output [6:2] excode_M,
43
+ output reg Zero_M,
44
+ output reg more_M,
45
+ output reg less_M
46
+ );
47
+
48
+ `define op 31:26
49
+ `define lw 6'b100011
50
+ `define lb 6'b100000
51
+ `define lbu 6'b100100
52
+ `define lh 6'b100001
53
+ `define lhu 6'b100101
54
+ `define sw 6'b101011
55
+ `define sb 6'b101000
56
+ `define sh 6'b101001
57
+
58
+ initial begin
59
+ IR_M <= 0;
60
+ end
61
+
62
+ assign excode_M = ((IR_M[`op]==`lw&&AO_M[1:0]!=2'b00)||(IR_M[`op]==`lh&&AO_M[0]!=1'b0)||(IR_M[`op]==`lhu&&AO_M[0]!=1'b0)||
63
+ ((IR_M[`op]==`lw||IR_M[`op]==`lb||IR_M[`op]==`lbu||IR_M[`op]==`lh||IR_M[`op]==`lhu)&&(AO_M<32'h00000000||
64
+ (AO_M>32'h00002fff&&AO_M<32'h00007f00)||(AO_M>32'h00007f0b&&AO_M<32'h00007f10)||AO_M>32'h00007f1b))||
65
+ ((IR_M[`op]==`lb||IR_M[`op]==`lbu||IR_M[`op]==`lh||IR_M[`op]==`lhu)&&((AO_M>=32'h00007f00&&AO_M<=32'h00007f0b)||
66
+ (AO_M>=32'h00007f10&&AO_M<=32'h00007f1b)))) ? 4 :
67
+ ((IR_M[`op]==`sw&&AO_M[1:0]!=2'b00)||(IR_M[`op]==`sh&&AO_M[0]!=1'b0)||
68
+ ((IR_M[`op]==`sw||IR_M[`op]==`sb||IR_M[`op]==`sh)&&(AO_M<32'h00000000||
69
+ (AO_M>32'h00002fff&&AO_M<32'h00007f00)||(AO_M>32'h00007f07&&AO_M<32'h00007f10)||AO_M>32'h00007f1b))|| //������COUNTд 7f07
70
+ ((IR_M[`op]==`sb||IR_M[`op]==`sh)&&((AO_M>=32'h00007f00&&AO_M<=32'h00007f0b)||
71
+ (AO_M>=32'h00007f10&&AO_M<=32'h00007f1b)))) ? 5 : 0;
72
+
73
+ always@(posedge clk) begin
74
+ if(reset) begin
75
+ IR_M <= 32'h00000000;
76
+ end
77
+ else if(en) begin
78
+ IR_M <= IR_E;
79
+ PC_M <= PC_E;
80
+ PC4_M <= PC4_E;
81
+ PC8_M <= PC8_E;
82
+ AO_M <= ALU;
83
+ MDO_M <= Mult_Div;
84
+ RT_M <= RT_E;
85
+ Zero_M <= Zero_E;
86
+ more_M <= more_E;
87
+ less_M <= less_E;
88
+ end
89
+ end
90
+
91
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/Mult_Div.v ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 11:13:10 11/30/2018
7
+ // Design Name:
8
+ // Module Name: Mult_Div
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module Mult_Div(
22
+ input clk,
23
+ input reset,
24
+ input [31:0] A,
25
+ input [31:0] B,
26
+ input [1:0] op,
27
+ input start,
28
+ input if_mthi,
29
+ input if_mtlo,
30
+ output reg Busy,
31
+ output [31:0] High,
32
+ output [31:0] Low
33
+ );
34
+ reg [31:0] Hi;
35
+ reg [31:0] Lo;
36
+ assign High = Hi;
37
+ assign Low = Lo;
38
+ reg[31:0] cycles;
39
+ reg [63:0] tmp;
40
+
41
+ initial begin
42
+ Hi<=0;
43
+ Lo<=0;
44
+ cycles=0;
45
+ Busy<=0;
46
+ end
47
+
48
+ always@(posedge clk) begin
49
+ if(reset) begin
50
+ Hi<=0;
51
+ Lo<=0;
52
+ cycles=0;
53
+ Busy<=0;
54
+ end
55
+ else if(!Busy) begin
56
+ if(start) begin
57
+ case(op)
58
+ 2'b00: begin//mult
59
+ tmp<=$signed(A)*$signed(B);
60
+ cycles=5;
61
+ end
62
+ 2'b01: begin//multu
63
+ tmp<=A*B;
64
+ cycles=5;
65
+ end
66
+ 2'b10: begin//div
67
+ tmp<={$signed(A)%$signed(B),$signed(A)/$signed(B)};
68
+ cycles=10;
69
+ end
70
+ 2'b11: begin//divu
71
+ tmp<={A%B,A/B};
72
+ cycles=10;
73
+ end
74
+ endcase
75
+ Busy<=1;
76
+ end
77
+ if(if_mthi) begin
78
+ Hi<=A;
79
+ end
80
+ if(if_mtlo) begin
81
+ Lo<=A;
82
+ end
83
+ end
84
+ else if(Busy) begin
85
+ if(cycles>0) begin
86
+ cycles=cycles-1;
87
+ if(cycles==0) begin
88
+ {Hi,Lo}<=tmp;
89
+ Busy<=0;
90
+ end
91
+ end
92
+ end
93
+ end
94
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/WB.v ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:30:22 11/23/2018
7
+ // Design Name:
8
+ // Module Name: WB
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module WB(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input [31:0] IR_M,
26
+ input [31:0] PC_M,
27
+ input [31:0] PC4_M,
28
+ input [31:0] PC8_M,
29
+ input [31:0] AO_M,
30
+ input [31:0] MDO_M,
31
+ input [31:0] DM,
32
+ input [31:0] CP0_M,
33
+ output reg[31:0] IR_W,
34
+ output reg[31:0] PC_W,
35
+ output reg[31:0] PC4_W,
36
+ output reg[31:0] PC8_W,
37
+ output reg[31:0] AO_W,
38
+ output reg[31:0] MDO_W,
39
+ output reg[31:0] DR_W,
40
+ output reg[31:0] CP0_W
41
+ );
42
+ initial begin
43
+ IR_W <= 0;
44
+ end
45
+ always@(posedge clk) begin
46
+ if(reset) begin
47
+ IR_W <= 32'h00000000;
48
+ end
49
+ else if(en) begin
50
+ IR_W <= IR_M;
51
+ PC_W <= PC_M;
52
+ PC4_W <= PC4_M;
53
+ PC8_W <= PC8_M;
54
+ AO_W <= AO_M;
55
+ MDO_W <= MDO_M;
56
+ DR_W <= DM;
57
+ CP0_W <= CP0_M;
58
+ end
59
+ end
60
+
61
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/alu.v ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:01:30 11/23/2018
7
+ // Design Name:
8
+ // Module Name: alu
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module alu(
22
+ input [31:0] A,
23
+ input [31:0] B,
24
+ input [3:0] ALUCtrl,
25
+ output reg[31:0] Result,
26
+ output over
27
+ );
28
+
29
+ reg[32:0] temp;
30
+
31
+ assign over = (temp[32]!=temp[31]);
32
+
33
+ always@(*) begin
34
+ case(ALUCtrl)
35
+ 4'b0000: begin
36
+ Result = A & B;
37
+ end
38
+ 4'b0001: begin
39
+ Result = A | B;
40
+ end
41
+ 4'b0010: begin
42
+ temp = {A[31],A[31:0]} + {B[31],B[31:0]};
43
+ Result = A + B;
44
+ end
45
+ 4'b0011: begin
46
+ temp = {A[31],A[31:0]} - {B[31],B[31:0]};
47
+ Result = A - B;
48
+ end
49
+ 4'b0100: begin
50
+ Result = A^B;
51
+ end
52
+ 4'b0101: begin
53
+ Result = ~(A|B);
54
+ end
55
+ 4'b0110: begin//sll sllv �߼���
56
+ Result = B << A[4:0];
57
+ end
58
+ 4'b0111: begin//srl srlv �߼���
59
+ Result = B >> A[4:0];
60
+ end
61
+ 4'b1000: begin//sra srav ������
62
+ Result = $signed($signed(B) >>> A[4:0]);
63
+ end
64
+ 4'b1001: begin
65
+ Result = ($signed(A)<$signed(B)) ? 32'b1 : 32'b0;
66
+ end
67
+ 4'b1010: begin
68
+ Result = (A<B) ? 32'b1 : 32'b0;
69
+ end
70
+ default: begin
71
+ Result = 0;
72
+ end
73
+ endcase
74
+ end
75
+
76
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/cmp.v ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:11:38 11/23/2018
7
+ // Design Name:
8
+ // Module Name: cmp
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module cmp(
22
+ input [31:0] D1,
23
+ input [31:0] D2,
24
+ output Equal,
25
+ output more,
26
+ output less
27
+ );
28
+
29
+ assign Equal = (D1==D2) ? 1'b1 : 1'b0;
30
+ assign more = ($signed(D1)>0) ? 1'b1 : 1'b0;
31
+ assign less = ($signed(D1)<0) ? 1'b1 : 1'b0;
32
+
33
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/controller.v ADDED
@@ -0,0 +1,1330 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 11:53:56 11/24/2018
7
+ // Design Name:
8
+ // Module Name: controller
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module controller(
22
+ input [5:0] op,
23
+ input [5:0] func,
24
+ input [4:0] rs,
25
+ input [4:0] rt,
26
+ output reg[3:0] ALUCtrl,
27
+ output reg[1:0] RegDst,
28
+ output reg ALUASrc,
29
+ output reg ALUBSrc,
30
+ output reg RegWrite,
31
+ output reg MemRead,
32
+ output reg MemWrite,
33
+ output reg [2:0] MemtoReg,
34
+ output reg [1:0]ExtOp,
35
+ output reg if_beq,
36
+ output reg if_bne,
37
+ output reg if_blez,
38
+ output reg if_bgez,
39
+ output reg if_bltz,
40
+ output reg if_bgtz,
41
+ output reg if_j,
42
+ output reg [1:0]PCsel,
43
+ output reg if_sh,
44
+ output reg if_sb,
45
+ output reg[2:0] dataOp,
46
+ output reg[1:0] multdivOp,
47
+ output reg start,
48
+ output reg if_mthi,
49
+ output reg if_mtlo,
50
+ output reg if_mfhi,
51
+ output reg if_mflo,
52
+ output reg if_1,
53
+ output reg if_2,
54
+ output reg if_3,
55
+ output reg cp0WE
56
+ );
57
+ initial begin
58
+ PCsel <= 2'b00;
59
+ end
60
+ always@(*)
61
+ begin
62
+ case (op)
63
+ 6'b000000://R
64
+ begin
65
+ RegDst<=2'b01;
66
+ ALUBSrc<=0;
67
+ MemRead<=0;
68
+ MemWrite<=0;
69
+ ExtOp<=2'b00;
70
+ if_beq<=0;
71
+ if_bne<=0;
72
+ if_bgtz<=0;
73
+ if_blez<=0;
74
+ if_bgez<=0;
75
+ if_bltz<=0;
76
+ if_sh<=0;
77
+ if_sb<=0;
78
+ if_j<=0;
79
+ dataOp<=3'b000;
80
+ if_1<=0;
81
+ if_2<=0;
82
+ if_3<=0;
83
+ cp0WE<=0;
84
+ case(func)
85
+ 6'b100001: begin //addu
86
+ ALUASrc<=0;
87
+ MemtoReg<=3'b000;
88
+ ALUCtrl<=4'b0010;
89
+ PCsel<=2'b00;
90
+ RegWrite<=1;
91
+ multdivOp<=2'b00;
92
+ start<=0;
93
+ if_mthi<=0;
94
+ if_mtlo<=0;
95
+ if_mfhi<=0;
96
+ if_mflo<=0;
97
+ end
98
+ 6'b100000: begin //add
99
+ ALUASrc<=0;
100
+ MemtoReg<=3'b000;
101
+ ALUCtrl<=4'b0010;
102
+ PCsel<=2'b00;
103
+ RegWrite<=1;
104
+ multdivOp<=2'b00;
105
+ start<=0;
106
+ if_mthi<=0;
107
+ if_mtlo<=0;
108
+ if_mfhi<=0;
109
+ if_mflo<=0;
110
+ end
111
+ 6'b100011: begin //subu
112
+ ALUASrc<=0;
113
+ MemtoReg<=3'b000;
114
+ ALUCtrl<=4'b0011;
115
+ PCsel<=2'b00;
116
+ RegWrite<=1;
117
+ multdivOp<=2'b00;
118
+ start<=0;
119
+ if_mthi<=0;
120
+ if_mtlo<=0;
121
+ if_mfhi<=0;
122
+ if_mflo<=0;
123
+ end
124
+ 6'b100010: begin //sub
125
+ ALUASrc<=0;
126
+ MemtoReg<=3'b000;
127
+ ALUCtrl<=4'b0011;
128
+ PCsel<=2'b00;
129
+ RegWrite<=1;
130
+ multdivOp<=2'b00;
131
+ start<=0;
132
+ if_mthi<=0;
133
+ if_mtlo<=0;
134
+ if_mfhi<=0;
135
+ if_mflo<=0;
136
+ end
137
+ 6'b100100: begin //and
138
+ ALUASrc<=0;
139
+ MemtoReg<=3'b000;
140
+ ALUCtrl<=4'b0000;
141
+ PCsel<=2'b00;
142
+ RegWrite<=1;
143
+ multdivOp<=2'b00;
144
+ start<=0;
145
+ if_mthi<=0;
146
+ if_mtlo<=0;
147
+ if_mfhi<=0;
148
+ if_mflo<=0;
149
+ end
150
+ 6'b100101: begin //or
151
+ ALUASrc<=0;
152
+ MemtoReg<=3'b000;
153
+ ALUCtrl<=4'b0001;
154
+ PCsel<=2'b00;
155
+ RegWrite<=1;
156
+ multdivOp<=2'b00;
157
+ start<=0;
158
+ if_mthi<=0;
159
+ if_mtlo<=0;
160
+ if_mfhi<=0;
161
+ if_mflo<=0;
162
+ end
163
+ 6'b100110: begin //xor
164
+ ALUASrc<=0;
165
+ MemtoReg<=3'b000;
166
+ ALUCtrl<=4'b0100;
167
+ PCsel<=2'b00;
168
+ RegWrite<=1;
169
+ multdivOp<=2'b00;
170
+ start<=0;
171
+ if_mthi<=0;
172
+ if_mtlo<=0;
173
+ if_mfhi<=0;
174
+ if_mflo<=0;
175
+ end
176
+ 6'b100111: begin //nor
177
+ ALUASrc<=0;
178
+ MemtoReg<=3'b000;
179
+ ALUCtrl<=4'b0101;
180
+ PCsel<=2'b00;
181
+ RegWrite<=1;
182
+ multdivOp<=2'b00;
183
+ start<=0;
184
+ if_mthi<=0;
185
+ if_mtlo<=0;
186
+ if_mfhi<=0;
187
+ if_mflo<=0;
188
+ end
189
+ 6'b001000: begin //jr
190
+ ALUASrc<=0;
191
+ MemtoReg<=3'b000;
192
+ ALUCtrl<=4'b0000;
193
+ PCsel<=2'b01;
194
+ RegWrite<=1;
195
+ multdivOp<=2'b00;
196
+ start<=0;
197
+ if_mthi<=0;
198
+ if_mtlo<=0;
199
+ if_mfhi<=0;
200
+ if_mflo<=0;
201
+ end
202
+ 6'b001001: begin //jalr
203
+ ALUASrc<=0;
204
+ MemtoReg<=3'b010;
205
+ ALUCtrl<=4'b0000;
206
+ PCsel<=2'b01;
207
+ RegWrite<=1;
208
+ multdivOp<=2'b00;
209
+ start<=0;
210
+ if_mthi<=0;
211
+ if_mtlo<=0;
212
+ if_mfhi<=0;
213
+ if_mflo<=0;
214
+ end
215
+ 6'b000000: begin //sll nop
216
+ ALUASrc<=1;
217
+ MemtoReg<=3'b000;
218
+ ALUCtrl<=4'b0110;
219
+ PCsel<=2'b00;
220
+ RegWrite<=1;
221
+ multdivOp<=2'b00;
222
+ start<=0;
223
+ if_mthi<=0;
224
+ if_mtlo<=0;
225
+ if_mfhi<=0;
226
+ if_mflo<=0;
227
+ end
228
+ 6'b000100: begin //sllv
229
+ ALUASrc<=0;
230
+ MemtoReg<=3'b000;
231
+ ALUCtrl<=4'b0110;
232
+ PCsel<=2'b00;
233
+ RegWrite<=1;
234
+ multdivOp<=2'b00;
235
+ start<=0;
236
+ if_mthi<=0;
237
+ if_mtlo<=0;
238
+ if_mfhi<=0;
239
+ if_mflo<=0;
240
+ end
241
+ 6'b000010: begin //srl
242
+ ALUASrc<=1;
243
+ MemtoReg<=3'b000;
244
+ ALUCtrl<=4'b0111;
245
+ PCsel<=2'b00;
246
+ RegWrite<=1;
247
+ multdivOp<=2'b00;
248
+ start<=0;
249
+ if_mthi<=0;
250
+ if_mtlo<=0;
251
+ if_mfhi<=0;
252
+ if_mflo<=0;
253
+ end
254
+ 6'b000110: begin //srlv
255
+ ALUASrc<=0;
256
+ MemtoReg<=3'b000;
257
+ ALUCtrl<=4'b0111;
258
+ PCsel<=2'b00;
259
+ RegWrite<=1;
260
+ multdivOp<=2'b00;
261
+ start<=0;
262
+ if_mthi<=0;
263
+ if_mtlo<=0;
264
+ if_mfhi<=0;
265
+ if_mflo<=0;
266
+ end
267
+ 6'b000011: begin //sra
268
+ ALUASrc<=1;
269
+ MemtoReg<=3'b000;
270
+ ALUCtrl<=4'b1000;
271
+ PCsel<=2'b00;
272
+ RegWrite<=1;
273
+ multdivOp<=2'b00;
274
+ start<=0;
275
+ if_mthi<=0;
276
+ if_mtlo<=0;
277
+ if_mfhi<=0;
278
+ if_mflo<=0;
279
+ end
280
+ 6'b000111: begin //srav
281
+ ALUASrc<=0;
282
+ MemtoReg<=3'b000;
283
+ ALUCtrl<=4'b1000;
284
+ PCsel<=2'b00;
285
+ RegWrite<=1;
286
+ multdivOp<=2'b00;
287
+ start<=0;
288
+ if_mthi<=0;
289
+ if_mtlo<=0;
290
+ if_mfhi<=0;
291
+ if_mflo<=0;
292
+ end
293
+ 6'b101010: begin //slt
294
+ ALUASrc<=0;
295
+ MemtoReg<=3'b000;
296
+ ALUCtrl<=4'b1001;
297
+ PCsel<=2'b00;
298
+ RegWrite<=1;
299
+ multdivOp<=2'b00;
300
+ start<=0;
301
+ if_mthi<=0;
302
+ if_mtlo<=0;
303
+ if_mfhi<=0;
304
+ if_mflo<=0;
305
+ end
306
+ 6'b101011: begin //sltu
307
+ ALUASrc<=0;
308
+ MemtoReg<=3'b000;
309
+ ALUCtrl<=4'b1010;
310
+ PCsel<=2'b00;
311
+ RegWrite<=1;
312
+ multdivOp<=2'b00;
313
+ start<=0;
314
+ if_mthi<=0;
315
+ if_mtlo<=0;
316
+ if_mfhi<=0;
317
+ if_mflo<=0;
318
+ end
319
+ 6'b011000: begin //mult
320
+ ALUASrc<=0;
321
+ MemtoReg<=3'b000;
322
+ ALUCtrl<=4'b0000;
323
+ PCsel<=2'b00;
324
+ RegWrite<=0;
325
+ multdivOp<=2'b00;
326
+ start<=1;
327
+ if_mthi<=0;
328
+ if_mtlo<=0;
329
+ if_mfhi<=0;
330
+ if_mflo<=0;
331
+ end
332
+ 6'b011001: begin //multu
333
+ ALUASrc<=0;
334
+ MemtoReg<=3'b000;
335
+ ALUCtrl<=4'b0000;
336
+ PCsel<=2'b00;
337
+ RegWrite<=0;
338
+ multdivOp<=2'b01;
339
+ start<=1;
340
+ if_mthi<=0;
341
+ if_mtlo<=0;
342
+ if_mfhi<=0;
343
+ if_mflo<=0;
344
+ end
345
+ 6'b011010: begin //div
346
+ ALUASrc<=0;
347
+ MemtoReg<=3'b000;
348
+ ALUCtrl<=4'b0000;
349
+ PCsel<=2'b00;
350
+ RegWrite<=0;
351
+ multdivOp<=2'b10;
352
+ start<=1;
353
+ if_mthi<=0;
354
+ if_mtlo<=0;
355
+ if_mfhi<=0;
356
+ if_mflo<=0;
357
+ end
358
+ 6'b011011: begin //divu
359
+ ALUASrc<=0;
360
+ MemtoReg<=3'b000;
361
+ ALUCtrl<=4'b0000;
362
+ PCsel<=2'b00;
363
+ RegWrite<=0;
364
+ multdivOp<=2'b11;
365
+ start<=1;
366
+ if_mthi<=0;
367
+ if_mtlo<=0;
368
+ if_mfhi<=0;
369
+ if_mflo<=0;
370
+ end
371
+ 6'b010000: begin //mfhi
372
+ ALUASrc<=0;
373
+ MemtoReg<=3'b011;
374
+ ALUCtrl<=4'b0000;
375
+ PCsel<=2'b00;
376
+ RegWrite<=1;
377
+ multdivOp<=2'b00;
378
+ start<=0;
379
+ if_mthi<=0;
380
+ if_mtlo<=0;
381
+ if_mfhi<=1;
382
+ if_mflo<=0;
383
+ end
384
+ 6'b010010: begin //mflo
385
+ ALUASrc<=0;
386
+ MemtoReg<=3'b011;
387
+ ALUCtrl<=4'b0000;
388
+ PCsel<=2'b00;
389
+ RegWrite<=1;
390
+ multdivOp<=2'b00;
391
+ start<=0;
392
+ if_mthi<=0;
393
+ if_mtlo<=0;
394
+ if_mfhi<=0;
395
+ if_mflo<=1;
396
+ end
397
+ 6'b010001: begin //mthi
398
+ ALUASrc<=0;
399
+ MemtoReg<=3'b000;
400
+ ALUCtrl<=4'b0000;
401
+ PCsel<=2'b00;
402
+ RegWrite<=0;
403
+ multdivOp<=2'b00;
404
+ start<=0;
405
+ if_mthi<=1;
406
+ if_mtlo<=0;
407
+ if_mfhi<=0;
408
+ if_mflo<=0;
409
+ end
410
+ 6'b010011: begin //mtlo
411
+ ALUASrc<=0;
412
+ MemtoReg<=3'b000;
413
+ ALUCtrl<=4'b0000;
414
+ PCsel<=2'b00;
415
+ RegWrite<=0;
416
+ multdivOp<=2'b00;
417
+ start<=0;
418
+ if_mthi<=0;
419
+ if_mtlo<=1;
420
+ if_mfhi<=0;
421
+ if_mflo<=0;
422
+ end
423
+ default: begin
424
+ ALUASrc<=0;
425
+ MemtoReg<=3'b000;
426
+ ALUCtrl<=4'b0000;
427
+ PCsel<=2'b00;
428
+ RegWrite<=1;
429
+ multdivOp<=2'b00;
430
+ start<=0;
431
+ if_mthi<=0;
432
+ if_mtlo<=0;
433
+ if_mfhi<=0;
434
+ if_mflo<=0;
435
+ end
436
+ endcase
437
+ end
438
+ 6'b100011://lw
439
+ begin
440
+ RegDst<=2'b00;
441
+ ALUASrc<=0;
442
+ ALUBSrc<=1;
443
+ RegWrite<=1;
444
+ MemRead<=1;
445
+ MemWrite<=0;
446
+ MemtoReg<=3'b001;
447
+ ExtOp<=2'b00;
448
+ if_beq<=0;
449
+ if_bne<=0;
450
+ if_bgtz<=0;
451
+ if_blez<=0;
452
+ if_bgez<=0;
453
+ if_bltz<=0;
454
+ ALUCtrl<=4'b0010;
455
+ if_j<=0;
456
+ PCsel<=2'b00;
457
+ if_sh<=0;
458
+ if_sb<=0;
459
+ dataOp<=3'b000;
460
+ multdivOp<=2'b00;
461
+ start<=0;
462
+ if_mthi<=0;
463
+ if_mtlo<=0;
464
+ if_mfhi<=0;
465
+ if_mflo<=0;
466
+ if_1<=0;
467
+ if_2<=0;
468
+ if_3<=0;
469
+ cp0WE<=0;
470
+ end
471
+
472
+ 6'b100000://lb
473
+ begin
474
+ RegDst<=2'b00;
475
+ ALUASrc<=0;
476
+ ALUBSrc<=1;
477
+ RegWrite<=1;
478
+ MemRead<=1;
479
+ MemWrite<=0;
480
+ MemtoReg<=3'b001;
481
+ ExtOp<=2'b00;
482
+ if_beq<=0;
483
+ if_bne<=0;
484
+ if_bgtz<=0;
485
+ if_blez<=0;
486
+ if_bgez<=0;
487
+ if_bltz<=0;
488
+ ALUCtrl<=4'b0010;
489
+ if_j<=0;
490
+ PCsel<=2'b00;
491
+ if_sh<=0;
492
+ if_sb<=0;
493
+ dataOp<=3'b010;
494
+ multdivOp<=2'b00;
495
+ start<=0;
496
+ if_mthi<=0;
497
+ if_mtlo<=0;
498
+ if_mfhi<=0;
499
+ if_mflo<=0;
500
+ if_1<=0;
501
+ if_2<=0;
502
+ if_3<=0;
503
+ cp0WE<=0;
504
+ end
505
+
506
+ 6'b100100://lbu
507
+ begin
508
+ RegDst<=2'b00;
509
+ ALUASrc<=0;
510
+ ALUBSrc<=1;
511
+ RegWrite<=1;
512
+ MemRead<=1;
513
+ MemWrite<=0;
514
+ MemtoReg<=3'b001;
515
+ ExtOp<=2'b00;
516
+ if_beq<=0;
517
+ if_bne<=0;
518
+ if_bgtz<=0;
519
+ if_blez<=0;
520
+ if_bgez<=0;
521
+ if_bltz<=0;
522
+ ALUCtrl<=4'b0010;
523
+ if_j<=0;
524
+ PCsel<=2'b00;
525
+ if_sh<=0;
526
+ if_sb<=0;
527
+ dataOp<=3'b001;
528
+ multdivOp<=2'b00;
529
+ start<=0;
530
+ if_mthi<=0;
531
+ if_mtlo<=0;
532
+ if_mfhi<=0;
533
+ if_mflo<=0;
534
+ if_1<=0;
535
+ if_2<=0;
536
+ if_3<=0;
537
+ cp0WE<=0;
538
+ end
539
+
540
+ 6'b100001://lh
541
+ begin
542
+ RegDst<=2'b00;
543
+ ALUASrc<=0;
544
+ ALUBSrc<=1;
545
+ RegWrite<=1;
546
+ MemRead<=1;
547
+ MemWrite<=0;
548
+ MemtoReg<=3'b001;
549
+ ExtOp<=2'b00;
550
+ if_beq<=0;
551
+ if_bne<=0;
552
+ if_bgtz<=0;
553
+ if_blez<=0;
554
+ if_bgez<=0;
555
+ if_bltz<=0;
556
+ ALUCtrl<=4'b0010;
557
+ if_j<=0;
558
+ PCsel<=2'b00;
559
+ if_sh<=0;
560
+ if_sb<=0;
561
+ dataOp<=3'b100;
562
+ multdivOp<=2'b00;
563
+ start<=0;
564
+ if_mthi<=0;
565
+ if_mtlo<=0;
566
+ if_mfhi<=0;
567
+ if_mflo<=0;
568
+ if_1<=0;
569
+ if_2<=0;
570
+ if_3<=0;
571
+ cp0WE<=0;
572
+ end
573
+
574
+ 6'b100101://lhu
575
+ begin
576
+ RegDst<=2'b00;
577
+ ALUASrc<=0;
578
+ ALUBSrc<=1;
579
+ RegWrite<=1;
580
+ MemRead<=1;
581
+ MemWrite<=0;
582
+ MemtoReg<=3'b001;
583
+ ExtOp<=2'b00;
584
+ if_beq<=0;
585
+ if_bne<=0;
586
+ if_bgtz<=0;
587
+ if_blez<=0;
588
+ if_bgez<=0;
589
+ if_bltz<=0;
590
+ ALUCtrl<=4'b0010;
591
+ if_j<=0;
592
+ PCsel<=2'b00;
593
+ if_sh<=0;
594
+ if_sb<=0;
595
+ dataOp<=3'b011;
596
+ multdivOp<=2'b00;
597
+ start<=0;
598
+ if_mthi<=0;
599
+ if_mtlo<=0;
600
+ if_mfhi<=0;
601
+ if_mflo<=0;
602
+ if_1<=0;
603
+ if_2<=0;
604
+ if_3<=0;
605
+ cp0WE<=0;
606
+ end
607
+
608
+ 6'b101011://sw
609
+ begin
610
+ RegDst<=2'b00;
611
+ ALUASrc<=0;
612
+ ALUBSrc<=1;
613
+ RegWrite<=0;
614
+ MemRead<=0;
615
+ MemWrite<=1;
616
+ MemtoReg<=3'b000;
617
+ ExtOp<=2'b00;
618
+ if_beq<=0;
619
+ if_bne<=0;
620
+ if_bgtz<=0;
621
+ if_blez<=0;
622
+ if_bgez<=0;
623
+ if_bltz<=0;
624
+ ALUCtrl<=4'b0010;
625
+ if_j<=0;
626
+ PCsel<=2'b00;
627
+ if_sh<=0;
628
+ if_sb<=0;
629
+ dataOp<=3'b000;
630
+ multdivOp<=2'b00;
631
+ start<=0;
632
+ if_mthi<=0;
633
+ if_mtlo<=0;
634
+ if_mfhi<=0;
635
+ if_mflo<=0;
636
+ if_1<=0;
637
+ if_2<=0;
638
+ if_3<=0;
639
+ cp0WE<=0;
640
+ end
641
+
642
+ 6'b101001://sh
643
+ begin
644
+ RegDst<=2'b00;
645
+ ALUASrc<=0;
646
+ ALUBSrc<=1;
647
+ RegWrite<=0;
648
+ MemRead<=0;
649
+ MemWrite<=1;
650
+ MemtoReg<=3'b000;
651
+ ExtOp<=2'b00;
652
+ if_beq<=0;
653
+ if_bne<=0;
654
+ if_bgtz<=0;
655
+ if_blez<=0;
656
+ if_bgez<=0;
657
+ if_bltz<=0;
658
+ ALUCtrl<=4'b0010;
659
+ if_j<=0;
660
+ PCsel<=2'b00;
661
+ if_sh<=1;
662
+ if_sb<=0;
663
+ dataOp<=3'b000;
664
+ multdivOp<=2'b00;
665
+ start<=0;
666
+ if_mthi<=0;
667
+ if_mtlo<=0;
668
+ if_mfhi<=0;
669
+ if_mflo<=0;
670
+ if_1<=0;
671
+ if_2<=0;
672
+ if_3<=0;
673
+ cp0WE<=0;
674
+ end
675
+
676
+ 6'b101000://sb
677
+ begin
678
+ RegDst<=2'b00;
679
+ ALUASrc<=0;
680
+ ALUBSrc<=1;
681
+ RegWrite<=0;
682
+ MemRead<=0;
683
+ MemWrite<=1;
684
+ MemtoReg<=3'b000;
685
+ ExtOp<=2'b00;
686
+ if_beq<=0;
687
+ if_bne<=0;
688
+ if_bgtz<=0;
689
+ if_blez<=0;
690
+ if_bgez<=0;
691
+ if_bltz<=0;
692
+ ALUCtrl<=4'b0010;
693
+ if_j<=0;
694
+ PCsel<=2'b00;
695
+ if_sh<=0;
696
+ if_sb<=1;
697
+ dataOp<=3'b000;
698
+ multdivOp<=2'b00;
699
+ start<=0;
700
+ if_mthi<=0;
701
+ if_mtlo<=0;
702
+ if_mfhi<=0;
703
+ if_mflo<=0;
704
+ if_1<=0;
705
+ if_2<=0;
706
+ if_3<=0;
707
+ cp0WE<=0;
708
+ end
709
+
710
+ 6'b000100://beq
711
+ begin
712
+ RegDst<=2'b00;
713
+ ALUASrc<=0;
714
+ ALUBSrc<=0;
715
+ RegWrite<=0;
716
+ MemRead<=0;
717
+ MemWrite<=0;
718
+ MemtoReg<=3'b000;
719
+ ExtOp<=2'b00;
720
+ if_beq<=1;
721
+ if_bne<=0;
722
+ if_bgtz<=0;
723
+ if_blez<=0;
724
+ if_bgez<=0;
725
+ if_bltz<=0;
726
+ ALUCtrl<=4'b0000;
727
+ if_j<=0;
728
+ PCsel<=2'b10;
729
+ if_sh<=0;
730
+ if_sb<=0;
731
+ dataOp<=3'b000;
732
+ multdivOp<=2'b00;
733
+ start<=0;
734
+ if_mthi<=0;
735
+ if_mtlo<=0;
736
+ if_mfhi<=0;
737
+ if_mflo<=0;
738
+ if_1<=0;
739
+ if_2<=0;
740
+ if_3<=0;
741
+ cp0WE<=0;
742
+ end
743
+
744
+ 6'b000101://bne
745
+ begin
746
+ RegDst<=2'b00;
747
+ ALUASrc<=0;
748
+ ALUBSrc<=0;
749
+ RegWrite<=0;
750
+ MemRead<=0;
751
+ MemWrite<=0;
752
+ MemtoReg<=3'b000;
753
+ ExtOp<=2'b00;
754
+ if_beq<=0;
755
+ if_bne<=1;
756
+ if_bgtz<=0;
757
+ if_blez<=0;
758
+ if_bgez<=0;
759
+ if_bltz<=0;
760
+ ALUCtrl<=4'b0000;
761
+ if_j<=0;
762
+ PCsel<=2'b10;
763
+ if_sh<=0;
764
+ if_sb<=0;
765
+ dataOp<=3'b000;
766
+ multdivOp<=2'b00;
767
+ start<=0;
768
+ if_mthi<=0;
769
+ if_mtlo<=0;
770
+ if_mfhi<=0;
771
+ if_mflo<=0;
772
+ if_1<=0;
773
+ if_2<=0;
774
+ if_3<=0;
775
+ cp0WE<=0;
776
+ end
777
+
778
+ 6'b000111://bgtz
779
+ begin
780
+ RegDst<=2'b00;
781
+ ALUASrc<=0;
782
+ ALUBSrc<=0;
783
+ RegWrite<=0;
784
+ MemRead<=0;
785
+ MemWrite<=0;
786
+ MemtoReg<=3'b000;
787
+ ExtOp<=2'b00;
788
+ if_beq<=0;
789
+ if_bne<=0;
790
+ if_bgtz<=1;
791
+ if_blez<=0;
792
+ if_bgez<=0;
793
+ if_bltz<=0;
794
+ ALUCtrl<=4'b0000;
795
+ if_j<=0;
796
+ PCsel<=2'b10;
797
+ if_sh<=0;
798
+ if_sb<=0;
799
+ dataOp<=3'b000;
800
+ multdivOp<=2'b00;
801
+ start<=0;
802
+ if_mthi<=0;
803
+ if_mtlo<=0;
804
+ if_mfhi<=0;
805
+ if_mflo<=0;
806
+ if_1<=0;
807
+ if_2<=0;
808
+ if_3<=0;
809
+ cp0WE<=0;
810
+ end
811
+
812
+ 6'b000110://blez
813
+ begin
814
+ RegDst<=2'b00;
815
+ ALUASrc<=0;
816
+ ALUBSrc<=0;
817
+ RegWrite<=0;
818
+ MemRead<=0;
819
+ MemWrite<=0;
820
+ MemtoReg<=3'b000;
821
+ ExtOp<=2'b00;
822
+ if_beq<=0;
823
+ if_bne<=0;
824
+ if_bgtz<=0;
825
+ if_blez<=1;
826
+ if_bgez<=0;
827
+ if_bltz<=0;
828
+ ALUCtrl<=4'b0000;
829
+ if_j<=0;
830
+ PCsel<=2'b10;
831
+ if_sh<=0;
832
+ if_sb<=0;
833
+ dataOp<=3'b000;
834
+ multdivOp<=2'b00;
835
+ start<=0;
836
+ if_mthi<=0;
837
+ if_mtlo<=0;
838
+ if_mfhi<=0;
839
+ if_mflo<=0;
840
+ if_1<=0;
841
+ if_2<=0;
842
+ if_3<=0;
843
+ cp0WE<=0;
844
+ end
845
+
846
+ 6'b000001://bgez/bltz
847
+ begin
848
+ RegDst<=2'b00;
849
+ ALUASrc<=0;
850
+ ALUBSrc<=0;
851
+ RegWrite<=0;
852
+ MemRead<=0;
853
+ MemWrite<=0;
854
+ MemtoReg<=3'b000;
855
+ ExtOp<=2'b00;
856
+ if_beq<=0;
857
+ if_bne<=0;
858
+ if_bgtz<=0;
859
+ if_blez<=0;
860
+ ALUCtrl<=4'b0000;
861
+ if_j<=0;
862
+ PCsel<=2'b10;
863
+ if_sh<=0;
864
+ if_sb<=0;
865
+ dataOp<=3'b000;
866
+ multdivOp<=2'b00;
867
+ start<=0;
868
+ if_mthi<=0;
869
+ if_mtlo<=0;
870
+ if_mfhi<=0;
871
+ if_mflo<=0;
872
+ if_1<=0;
873
+ if_2<=0;
874
+ if_3<=0;
875
+ cp0WE<=0;
876
+ case(rt)
877
+ 5'b00001:begin//bgez
878
+ if_bgez<=1;
879
+ if_bltz<=0;
880
+ end
881
+ 5'b00000:begin//bltz
882
+ if_bgez<=0;
883
+ if_bltz<=1;
884
+ end
885
+ default:begin
886
+ if_bgez<=0;
887
+ if_bltz<=0;
888
+ end
889
+ endcase
890
+ end
891
+
892
+ 6'b001111://lui
893
+ begin
894
+ RegDst<=2'b00;
895
+ ALUASrc<=0;
896
+ ALUBSrc<=1;
897
+ RegWrite<=1;
898
+ MemRead<=0;
899
+ MemWrite<=0;
900
+ MemtoReg<=3'b000;
901
+ ExtOp<=2'b10;
902
+ if_beq<=0;
903
+ if_bne<=0;
904
+ if_bgtz<=0;
905
+ if_blez<=0;
906
+ if_bgez<=0;
907
+ if_bltz<=0;
908
+ ALUCtrl<=4'b0010;
909
+ if_j<=0;
910
+ PCsel<=2'b00;
911
+ if_sh<=0;
912
+ if_sb<=0;
913
+ dataOp<=3'b000;
914
+ multdivOp<=2'b00;
915
+ start<=0;
916
+ if_mthi<=0;
917
+ if_mtlo<=0;
918
+ if_mfhi<=0;
919
+ if_mflo<=0;
920
+ if_1<=0;
921
+ if_2<=0;
922
+ if_3<=0;
923
+ cp0WE<=0;
924
+ end
925
+
926
+ 6'b001101://ori
927
+ begin
928
+ RegDst<=2'b00;
929
+ ALUASrc<=0;
930
+ ALUBSrc<=1;
931
+ RegWrite<=1;
932
+ MemRead<=0;
933
+ MemWrite<=0;
934
+ MemtoReg<=3'b000;
935
+ ExtOp<=2'b01;
936
+ if_beq<=0;
937
+ if_bne<=0;
938
+ if_bgtz<=0;
939
+ if_blez<=0;
940
+ if_bgez<=0;
941
+ if_bltz<=0;
942
+ ALUCtrl<=4'b0001;
943
+ if_j<=0;
944
+ PCsel<=2'b00;
945
+ if_sh<=0;
946
+ if_sb<=0;
947
+ dataOp<=3'b000;
948
+ multdivOp<=2'b00;
949
+ start<=0;
950
+ if_mthi<=0;
951
+ if_mtlo<=0;
952
+ if_mfhi<=0;
953
+ if_mflo<=0;
954
+ if_1<=0;
955
+ if_2<=0;
956
+ if_3<=0;
957
+ cp0WE<=0;
958
+ end
959
+
960
+ 6'b001010://slti
961
+ begin
962
+ RegDst<=2'b00;
963
+ ALUASrc<=0;
964
+ ALUBSrc<=1;
965
+ RegWrite<=1;
966
+ MemRead<=0;
967
+ MemWrite<=0;
968
+ MemtoReg<=3'b000;
969
+ ExtOp<=2'b00;
970
+ if_beq<=0;
971
+ if_bne<=0;
972
+ if_bgtz<=0;
973
+ if_blez<=0;
974
+ if_bgez<=0;
975
+ if_bltz<=0;
976
+ ALUCtrl<=4'b1001;
977
+ if_j<=0;
978
+ PCsel<=2'b00;
979
+ if_sh<=0;
980
+ if_sb<=0;
981
+ dataOp<=3'b000;
982
+ multdivOp<=2'b00;
983
+ start<=0;
984
+ if_mthi<=0;
985
+ if_mtlo<=0;
986
+ if_mfhi<=0;
987
+ if_mflo<=0;
988
+ if_1<=0;
989
+ if_2<=0;
990
+ if_3<=0;
991
+ cp0WE<=0;
992
+ end
993
+
994
+ 6'b001011://sltiu
995
+ begin
996
+ RegDst<=2'b00;
997
+ ALUASrc<=0;
998
+ ALUBSrc<=1;
999
+ RegWrite<=1;
1000
+ MemRead<=0;
1001
+ MemWrite<=0;
1002
+ MemtoReg<=3'b000;
1003
+ ExtOp<=2'b00;
1004
+ if_beq<=0;
1005
+ if_bne<=0;
1006
+ if_bgtz<=0;
1007
+ if_blez<=0;
1008
+ if_bgez<=0;
1009
+ if_bltz<=0;
1010
+ ALUCtrl<=4'b1010;
1011
+ if_j<=0;
1012
+ PCsel<=2'b00;
1013
+ if_sh<=0;
1014
+ if_sb<=0;
1015
+ dataOp<=3'b000;
1016
+ multdivOp<=2'b00;
1017
+ start<=0;
1018
+ if_mthi<=0;
1019
+ if_mtlo<=0;
1020
+ if_mfhi<=0;
1021
+ if_mflo<=0;
1022
+ if_1<=0;
1023
+ if_2<=0;
1024
+ if_3<=0;
1025
+ cp0WE<=0;
1026
+ end
1027
+
1028
+ 6'b001100://andi
1029
+ begin
1030
+ RegDst<=2'b00;
1031
+ ALUASrc<=0;
1032
+ ALUBSrc<=1;
1033
+ RegWrite<=1;
1034
+ MemRead<=0;
1035
+ MemWrite<=0;
1036
+ MemtoReg<=3'b000;
1037
+ ExtOp<=2'b01;
1038
+ if_beq<=0;
1039
+ if_bne<=0;
1040
+ if_bgtz<=0;
1041
+ if_blez<=0;
1042
+ if_bgez<=0;
1043
+ if_bltz<=0;
1044
+ ALUCtrl<=4'b0000;
1045
+ if_j<=0;
1046
+ PCsel<=2'b00;
1047
+ if_sh<=0;
1048
+ if_sb<=0;
1049
+ dataOp<=3'b000;
1050
+ multdivOp<=2'b00;
1051
+ start<=0;
1052
+ if_mthi<=0;
1053
+ if_mtlo<=0;
1054
+ if_mfhi<=0;
1055
+ if_mflo<=0;
1056
+ if_1<=0;
1057
+ if_2<=0;
1058
+ if_3<=0;
1059
+ cp0WE<=0;
1060
+ end
1061
+
1062
+ 6'b001110://xori
1063
+ begin
1064
+ RegDst<=2'b00;
1065
+ ALUASrc<=0;
1066
+ ALUBSrc<=1;
1067
+ RegWrite<=1;
1068
+ MemRead<=0;
1069
+ MemWrite<=0;
1070
+ MemtoReg<=3'b000;
1071
+ ExtOp<=2'b01;
1072
+ if_beq<=0;
1073
+ if_bne<=0;
1074
+ if_bgtz<=0;
1075
+ if_blez<=0;
1076
+ if_bgez<=0;
1077
+ if_bltz<=0;
1078
+ ALUCtrl<=4'b0100;
1079
+ if_j<=0;
1080
+ PCsel<=2'b00;
1081
+ if_sh<=0;
1082
+ if_sb<=0;
1083
+ dataOp<=3'b000;
1084
+ multdivOp<=2'b00;
1085
+ start<=0;
1086
+ if_mthi<=0;
1087
+ if_mtlo<=0;
1088
+ if_mfhi<=0;
1089
+ if_mflo<=0;
1090
+ if_1<=0;
1091
+ if_2<=0;
1092
+ if_3<=0;
1093
+ cp0WE<=0;
1094
+ end
1095
+
1096
+ 6'b001001://addiu
1097
+ begin
1098
+ RegDst<=2'b00;
1099
+ ALUASrc<=0;
1100
+ ALUBSrc<=1;
1101
+ RegWrite<=1;
1102
+ MemRead<=0;
1103
+ MemWrite<=0;
1104
+ MemtoReg<=3'b000;
1105
+ ExtOp<=2'b00;
1106
+ if_beq<=0;
1107
+ if_bne<=0;
1108
+ if_bgtz<=0;
1109
+ if_blez<=0;
1110
+ if_bgez<=0;
1111
+ if_bltz<=0;
1112
+ ALUCtrl<=4'b0010;
1113
+ if_j<=0;
1114
+ PCsel<=2'b00;
1115
+ if_sh<=0;
1116
+ if_sb<=0;
1117
+ dataOp<=3'b000;
1118
+ multdivOp<=2'b00;
1119
+ start<=0;
1120
+ if_mthi<=0;
1121
+ if_mtlo<=0;
1122
+ if_mfhi<=0;
1123
+ if_mflo<=0;
1124
+ if_1<=0;
1125
+ if_2<=0;
1126
+ if_3<=0;
1127
+ cp0WE<=0;
1128
+ end
1129
+
1130
+ 6'b001000://addi
1131
+ begin
1132
+ RegDst<=2'b00;
1133
+ ALUASrc<=0;
1134
+ ALUBSrc<=1;
1135
+ RegWrite<=1;
1136
+ MemRead<=0;
1137
+ MemWrite<=0;
1138
+ MemtoReg<=3'b000;
1139
+ ExtOp<=2'b00;
1140
+ if_beq<=0;
1141
+ if_bne<=0;
1142
+ if_bgtz<=0;
1143
+ if_blez<=0;
1144
+ if_bgez<=0;
1145
+ if_bltz<=0;
1146
+ ALUCtrl<=4'b0010;
1147
+ if_j<=0;
1148
+ PCsel<=2'b00;
1149
+ if_sh<=0;
1150
+ if_sb<=0;
1151
+ dataOp<=3'b000;
1152
+ multdivOp<=2'b00;
1153
+ start<=0;
1154
+ if_mthi<=0;
1155
+ if_mtlo<=0;
1156
+ if_mfhi<=0;
1157
+ if_mflo<=0;
1158
+ if_1<=0;
1159
+ if_2<=0;
1160
+ if_3<=0;
1161
+ cp0WE<=0;
1162
+ end
1163
+
1164
+ 6'b000011://jal
1165
+ begin
1166
+ RegDst<=2'b10;
1167
+ ALUASrc<=0;
1168
+ ALUBSrc<=0;
1169
+ RegWrite<=1;
1170
+ MemRead<=0;
1171
+ MemWrite<=0;
1172
+ MemtoReg<=3'b010;
1173
+ ExtOp<=2'b00;
1174
+ if_beq<=0;
1175
+ if_bne<=0;
1176
+ if_bgtz<=0;
1177
+ if_blez<=0;
1178
+ if_bgez<=0;
1179
+ if_bltz<=0;
1180
+ ALUCtrl<=4'b0000;
1181
+ if_j<=1;
1182
+ PCsel<=2'b10;
1183
+ if_sh<=0;
1184
+ if_sb<=0;
1185
+ dataOp<=3'b000;
1186
+ multdivOp<=2'b00;
1187
+ start<=0;
1188
+ if_mthi<=0;
1189
+ if_mtlo<=0;
1190
+ if_mfhi<=0;
1191
+ if_mflo<=0;
1192
+ if_1<=0;
1193
+ if_2<=0;
1194
+ if_3<=0;
1195
+ cp0WE<=0;
1196
+ end
1197
+
1198
+ 6'b000010://j
1199
+ begin
1200
+ RegDst<=2'b00;
1201
+ ALUASrc<=0;
1202
+ ALUBSrc<=0;
1203
+ RegWrite<=0;
1204
+ MemRead<=0;
1205
+ MemWrite<=0;
1206
+ MemtoReg<=3'b000;
1207
+ ExtOp<=2'b00;
1208
+ if_beq<=0;
1209
+ if_bne<=0;
1210
+ if_bgtz<=0;
1211
+ if_blez<=0;
1212
+ if_bgez<=0;
1213
+ if_bltz<=0;
1214
+ ALUCtrl<=4'b0000;
1215
+ if_j<=1;
1216
+ PCsel<=2'b10;
1217
+ if_sh<=0;
1218
+ if_sb<=0;
1219
+ dataOp<=3'b000;
1220
+ multdivOp<=2'b00;
1221
+ start<=0;
1222
+ if_mthi<=0;
1223
+ if_mtlo<=0;
1224
+ if_mfhi<=0;
1225
+ if_mflo<=0;
1226
+ if_1<=0;
1227
+ if_2<=0;
1228
+ if_3<=0;
1229
+ cp0WE<=0;
1230
+ end
1231
+
1232
+ 6'b010000:
1233
+ begin
1234
+ if_beq<=0;
1235
+ if_bne<=0;
1236
+ if_bgtz<=0;
1237
+ if_blez<=0;
1238
+ if_bgez<=0;
1239
+ if_bltz<=0;
1240
+ ExtOp<=2'b00;
1241
+ ALUCtrl<=4'b0000;
1242
+ ALUASrc<=0;
1243
+ ALUBSrc<=0;
1244
+ if_j<=0;
1245
+ MemRead<=0;
1246
+ MemWrite<=0;
1247
+ RegDst<=2'b00;
1248
+ if_sh<=0;
1249
+ if_sb<=0;
1250
+ dataOp<=3'b000;
1251
+ multdivOp<=2'b00;
1252
+ start<=0;
1253
+ if_mthi<=0;
1254
+ if_mtlo<=0;
1255
+ if_mfhi<=0;
1256
+ if_mflo<=0;
1257
+ if_1<=0;
1258
+ if_2<=0;
1259
+ if_3<=0;
1260
+ case(rs)
1261
+ 5'b00100: //mtc0
1262
+ begin
1263
+ RegWrite<=0;
1264
+ MemtoReg<=3'b000;
1265
+ PCsel<=2'b00;
1266
+ cp0WE<=1;
1267
+ end
1268
+ 5'b00000: //mfc0
1269
+ begin
1270
+ RegWrite<=1;
1271
+ MemtoReg<=3'b100;
1272
+ PCsel<=2'b00;
1273
+ cp0WE<=0;
1274
+ end
1275
+ default:
1276
+ begin
1277
+
1278
+ end
1279
+ endcase
1280
+ case(func)
1281
+ 6'b011000://eret
1282
+ begin
1283
+ RegWrite<=0;
1284
+ MemtoReg<=3'b000;
1285
+ PCsel<=2'b11;
1286
+ cp0WE<=0;
1287
+ end
1288
+ default: begin
1289
+
1290
+ end
1291
+ endcase
1292
+ end
1293
+
1294
+ default:
1295
+ begin
1296
+ RegDst<=2'b00;
1297
+ ALUASrc<=0;
1298
+ ALUBSrc<=0;
1299
+ RegWrite<=0;
1300
+ MemRead<=0;
1301
+ MemWrite<=0;
1302
+ MemtoReg<=3'b000;
1303
+ ExtOp<=2'b00;
1304
+ if_beq<=0;
1305
+ if_bne<=0;
1306
+ if_bgtz<=0;
1307
+ if_blez<=0;
1308
+ if_bgez<=0;
1309
+ if_bltz<=0;
1310
+ ALUCtrl<=4'b0000;
1311
+ if_j<=0;
1312
+ PCsel<=2'b00;
1313
+ if_sh<=0;
1314
+ if_sb<=0;
1315
+ dataOp<=3'b000;
1316
+ multdivOp<=2'b00;
1317
+ start<=0;
1318
+ if_mthi<=0;
1319
+ if_mtlo<=0;
1320
+ if_mfhi<=0;
1321
+ if_mflo<=0;
1322
+ if_1<=0;
1323
+ if_2<=0;
1324
+ if_3<=0;
1325
+ cp0WE<=0;
1326
+ end
1327
+ endcase
1328
+ end
1329
+
1330
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/dm.v ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:57:57 11/23/2018
7
+ // Design Name:
8
+ // Module Name: dm
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module dm(
22
+ input clk,
23
+ input reset,
24
+ input MemWrite,
25
+ input MemRead,
26
+ input if_sh,
27
+ input if_sb,
28
+ input [31:0] MemAddr,
29
+ input [31:0] WD,
30
+ input [31:0] PC,
31
+ output [31:0] RD
32
+ );
33
+
34
+ reg[31:0] dm[0:4095];
35
+ integer i;
36
+ initial begin
37
+ for(i=0;i<4096;i=i+1) begin //1024
38
+ dm[i]<=0;
39
+ end
40
+ end
41
+
42
+ assign RD = MemRead ? dm[MemAddr[13:2]] : 0;
43
+
44
+ always@(posedge clk) begin
45
+ if(reset) begin
46
+ for(i=0;i<4096;i=i+1) begin//1024
47
+ dm[i]<=0;
48
+ end
49
+ end
50
+ else begin
51
+ if(MemWrite) begin
52
+ if(if_sh) begin
53
+ if(MemAddr[1]==1'b1) begin
54
+ dm[MemAddr[13:2]][31:16] <= WD[15:0];
55
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{WD[15:0],dm[MemAddr[13:2]][15:0]});
56
+ end
57
+ else if(MemAddr[1]==1'b0) begin
58
+ dm[MemAddr[13:2]][15:0] <= WD[15:0];
59
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{dm[MemAddr[13:2]][31:16],WD[15:0]});
60
+ end
61
+ end
62
+ else if(if_sb) begin
63
+ case(MemAddr[1:0])
64
+ 2'b00: begin
65
+ dm[MemAddr[13:2]][7:0] <= WD[7:0];
66
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{dm[MemAddr[13:2]][31:8],WD[7:0]});
67
+ end
68
+ 2'b01: begin
69
+ dm[MemAddr[13:2]][15:8] <= WD[7:0];
70
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{dm[MemAddr[13:2]][31:16],WD[7:0],dm[MemAddr[13:2]][7:0]});
71
+ end
72
+ 2'b10: begin
73
+ dm[MemAddr[13:2]][23:16] <= WD[7:0];
74
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{dm[MemAddr[13:2]][31:24],WD[7:0],dm[MemAddr[13:2]][15:0]});
75
+ end
76
+ 2'b11: begin
77
+ dm[MemAddr[13:2]][31:24] <= WD[7:0];
78
+ $display("%d@%h: *%h <= %h",$time,PC,{MemAddr[31:2],2'b00},{WD[7:0],dm[MemAddr[13:2]][23:0]});
79
+ end
80
+ endcase
81
+ end
82
+ else begin
83
+ dm[MemAddr[13:2]] <= WD;
84
+ $display("%d@%h: *%h <= %h",$time,PC,MemAddr,WD);
85
+ end
86
+ end
87
+ end
88
+ end
89
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/ext.v ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:59:19 11/23/2018
7
+ // Design Name:
8
+ // Module Name: ext
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module ext(
22
+ input [15:0] in,
23
+ input [1:0] ExtOp,
24
+ output reg [31:0] out
25
+ );
26
+
27
+ always@(*) begin
28
+ case(ExtOp)
29
+ 2'b00: out <= {{16{in[15]}},in};
30
+ 2'b01: out <= {{16{1'b0}},in};
31
+ 2'b10: out <= {in,{16{1'b0}}};
32
+ 2'b11: out <= 0;
33
+ endcase
34
+ end
35
+
36
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/forward_mux.v ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:57:34 11/24/2018
7
+ // Design Name:
8
+ // Module Name: forward_mux
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module forward_mux(
22
+ input [31:0] RS_E,
23
+ input [31:0] RT_E,
24
+ input [31:0] RT_M,
25
+ input [31:0] WD,
26
+ input [31:0] AO_M,
27
+ input [31:0] MDO_M,
28
+ input [31:0] CP0_M,
29
+ input [31:0] MD_out,
30
+ input [31:0] PC8_E,
31
+ input [31:0] PC8_M,
32
+ input [31:0] PC8_W,
33
+ input [31:0] RF_RD1,
34
+ input [31:0] RF_RD2,
35
+ input [31:0] EPCout,
36
+ input [2:0] ForwardRSD,
37
+ input [2:0] ForwardRTD,
38
+ input [2:0] ForwardRSE,
39
+ input [2:0] ForwardRTE,
40
+ input [2:0] ForwardRTM,
41
+ input [2:0] ForwardERET,
42
+ output reg[31:0] MFRSD,
43
+ output reg[31:0] MFRTD,
44
+ output reg[31:0] MFRSE,
45
+ output reg[31:0] MFRTE,
46
+ output reg[31:0] MFRTM,
47
+ output reg[31:0] MFERET
48
+ );
49
+
50
+ always@(*) begin
51
+ case(ForwardRSD)
52
+ 3'b000 : MFRSD <= RF_RD1;
53
+ 3'b001 : MFRSD <= AO_M;
54
+ 3'b010 : MFRSD <= WD;
55
+ 3'b011 : MFRSD <= PC8_E;
56
+ 3'b100 : MFRSD <= PC8_M;
57
+ 3'b101 : MFRSD <= CP0_M;
58
+ 3'b110 : MFRSD <= MD_out;
59
+ 3'b111 : MFRSD <= MDO_M;
60
+ default : MFRSD <= 0;
61
+ endcase
62
+
63
+ case(ForwardRTD)
64
+ 0: MFRTD <= RF_RD2;
65
+ 1: MFRTD <= AO_M;
66
+ 2: MFRTD <= WD;
67
+ 3: MFRTD <= PC8_E;
68
+ 4: MFRTD <= PC8_M;
69
+ 5: MFRTD <= CP0_M;
70
+ 6: MFRTD <= MD_out;
71
+ 7: MFRTD <= MDO_M;
72
+ default: MFRTD <= 0;
73
+ endcase
74
+
75
+ case(ForwardRSE)
76
+ 0:MFRSE <= RS_E;
77
+ 1:MFRSE <= AO_M;
78
+ 2:MFRSE <= WD;
79
+ 3:MFRSE <= 0;
80
+ 4:MFRSE <= PC8_M;
81
+ 5:MFRSE <= CP0_M;
82
+ 6:MFRSE <= 0;
83
+ 7:MFRSE <= MDO_M;
84
+ default:MFRSE <= 0;
85
+ endcase
86
+
87
+ case(ForwardRTE)
88
+ 0:MFRTE <= RT_E;
89
+ 1:MFRTE <= AO_M;
90
+ 2:MFRTE <= WD;
91
+ 3:MFRTE <= 0;
92
+ 4:MFRTE <= PC8_M;
93
+ 5:MFRTE <= CP0_M;
94
+ 6:MFRTE <= 0;
95
+ 7:MFRTE <= MDO_M;
96
+ default:MFRTE <= 0;
97
+ endcase
98
+
99
+ case(ForwardRTM)
100
+ 0:MFRTM <= RT_M;
101
+ 1:MFRTM <= 0;
102
+ 2:MFRTM <= WD;
103
+ 3:MFRTM <= 0;
104
+ 4:MFRTM <= 0;
105
+ 5:MFRTM <= 0;
106
+ 6:MFRTM <= 0;
107
+ 7:MFRTM <= 0;
108
+ default:MFRTM <= 0;
109
+ endcase
110
+
111
+ case(ForwardERET)
112
+ 0:MFERET <= EPCout;
113
+ 1:MFERET <= MFRTM;
114
+ default:MFERET <= 0;
115
+ endcase
116
+ end
117
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/grf.v ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:02:24 11/23/2018
7
+ // Design Name:
8
+ // Module Name: grf
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module grf(
22
+ input clk,
23
+ input reset,
24
+ input RegWrite,
25
+ input [4:0] RA1,
26
+ input [4:0] RA2,
27
+ input [4:0] WA,
28
+ input [31:0] WD,
29
+ input [31:0] PC,
30
+ output [31:0] RD1,
31
+ output [31:0] RD2
32
+ );
33
+
34
+ reg[31:0] register[31:0];
35
+ integer i;
36
+ initial begin
37
+ for(i=0;i<32;i=i+1) begin
38
+ register[i] <= 0;
39
+ end
40
+ end
41
+
42
+ assign RD1 = register[RA1];
43
+ assign RD2 = register[RA2];
44
+
45
+ always@(posedge clk) begin
46
+ if(reset) begin
47
+ for(i=0;i<32;i=i+1) begin
48
+ register[i] <= 0;
49
+ end
50
+ end
51
+ else begin
52
+ if(RegWrite&&WA!=5'b00000) begin
53
+ register[WA] <= WD;
54
+ //$display("@%h: $%d <= %h", PC, WA,WD);
55
+ $display("%d@%h: $%d <= %h",$time,PC,WA,WD);
56
+ end
57
+ end
58
+ end
59
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/hazardUnit.v ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 12:55:59 11/24/2018
7
+ // Design Name:
8
+ // Module Name: hazardUnit
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module hazardUnit(
22
+ input [31:0] IR_D,
23
+ input [31:0] IR_E,
24
+ input [31:0] IR_M,
25
+ input [31:0] IR_W,
26
+ input Busy,
27
+ input start,
28
+ output IR_D_en,
29
+ output IR_E_clr,
30
+ output PC_en,
31
+ output [2:0]ForwardRSD,
32
+ output [2:0]ForwardRTD,
33
+ output [2:0]ForwardRSE,
34
+ output [2:0]ForwardRTE,
35
+ output [2:0]ForwardRTM,
36
+ output [2:0]ForwardERET
37
+ );
38
+
39
+ `define op 31:26
40
+ `define rs 25:21
41
+ `define rt 20:16
42
+ `define rd 15:11
43
+ `define shamt 10:6
44
+ `define func 5:0
45
+ `define imm16 15:0
46
+
47
+ `define R 6'b000000
48
+ `define lw 6'b100011
49
+ `define lb 6'b100000
50
+ `define lbu 6'b100100
51
+ `define lh 6'b100001
52
+ `define lhu 6'b100101
53
+ `define sw 6'b101011
54
+ `define sb 6'b101000
55
+ `define sh 6'b101001
56
+ `define lui 6'b001111
57
+ `define ori 6'b001101
58
+ `define andi 6'b001100
59
+ `define xori 6'b001110
60
+ `define addi 6'b001000
61
+ `define addiu 6'b001001
62
+ `define slti 6'b001010
63
+ `define sltiu 6'b001011
64
+ `define beq 6'b000100
65
+ `define bne 6'b000101
66
+ `define bgtz 6'b000111
67
+ `define blez 6'b000110
68
+ `define jal 6'b000011
69
+ `define addu 6'b100001
70
+ `define add 6'b100000
71
+ `define subu 6'b100011
72
+ `define sub 6'b100010
73
+ `define and_ 6'b100100
74
+ `define or_ 6'b100101
75
+ `define xor_ 6'b100110
76
+ `define nor_ 6'b100111
77
+ `define sll 6'b000000
78
+ `define srl 6'b000010
79
+ `define sra 6'b000011
80
+ `define sllv 6'b000100
81
+ `define srlv 6'b000110
82
+ `define srav 6'b000111
83
+ `define jr 6'b001000
84
+ `define jalr 6'b001001
85
+ `define mult 6'b011000
86
+ `define multu 6'b011001
87
+ `define div 6'b011010
88
+ `define divu 6'b011011
89
+ `define mfhi 6'b010000
90
+ `define mflo 6'b010010
91
+ `define mthi 6'b010001
92
+ `define mtlo 6'b010011
93
+ `define slt 6'b101010
94
+ `define sltu 6'b101011
95
+
96
+ `define cal_r_D (IR_D[`op]==`R&&IR_D[`func]!=`jalr&&IR_D[`func]!=`jr&&IR_D[`func]!=`mfhi&&IR_D[`func]!=`mflo&&IR_D!=0)
97
+ `define cal_r_E (IR_E[`op]==`R&&IR_E[`func]!=`jalr&&IR_E[`func]!=`jr&&IR_E[`func]!=`mfhi&&IR_E[`func]!=`mflo&&IR_E!=0)
98
+ `define cal_r_M (IR_M[`op]==`R&&IR_M[`func]!=`jalr&&IR_M[`func]!=`jr&&IR_M[`func]!=`mfhi&&IR_M[`func]!=`mflo&&IR_M!=0)
99
+ `define cal_r_W (IR_W[`op]==`R&&IR_W[`func]!=`jalr&&IR_W[`func]!=`jr&&IR_W[`func]!=`mfhi&&IR_W[`func]!=`mflo&&IR_W!=0)
100
+
101
+ `define cal_i_D (IR_D[`op]==`lui||IR_D[`op]==`ori||IR_D[`op]==`addi||IR_D[`op]==`addiu||IR_D[`op]==`andi||IR_D[`op]==`xori||IR_D[`op]==`slti||IR_D[`op]==`sltiu)
102
+ `define cal_i_E (IR_E[`op]==`lui||IR_E[`op]==`ori||IR_E[`op]==`addi||IR_E[`op]==`addiu||IR_E[`op]==`andi||IR_E[`op]==`xori||IR_E[`op]==`slti||IR_E[`op]==`sltiu)
103
+ `define cal_i_M (IR_M[`op]==`lui||IR_M[`op]==`ori||IR_M[`op]==`addi||IR_M[`op]==`addiu||IR_M[`op]==`andi||IR_M[`op]==`xori||IR_M[`op]==`slti||IR_M[`op]==`sltiu)
104
+ `define cal_i_W (IR_W[`op]==`lui||IR_W[`op]==`ori||IR_W[`op]==`addi||IR_W[`op]==`addiu||IR_W[`op]==`andi||IR_W[`op]==`xori||IR_W[`op]==`slti||IR_W[`op]==`sltiu)
105
+
106
+ `define load_D (IR_D[`op]==`lw||IR_D[`op]==`lb||IR_D[`op]==`lbu||IR_D[`op]==`lh||IR_D[`op]==`lhu)
107
+ `define load_E (IR_E[`op]==`lw||IR_E[`op]==`lb||IR_E[`op]==`lbu||IR_E[`op]==`lh||IR_E[`op]==`lhu)
108
+ `define load_M (IR_M[`op]==`lw||IR_M[`op]==`lb||IR_M[`op]==`lbu||IR_M[`op]==`lh||IR_M[`op]==`lhu)
109
+ `define load_W (IR_W[`op]==`lw||IR_W[`op]==`lb||IR_W[`op]==`lbu||IR_W[`op]==`lh||IR_W[`op]==`lhu)
110
+
111
+ `define store_D (IR_D[`op]==`sw||IR_D[`op]==`sh||IR_D[`op]==`sb)
112
+ `define store_E (IR_E[`op]==`sw||IR_E[`op]==`sh||IR_E[`op]==`sb)
113
+ `define store_M (IR_M[`op]==`sw||IR_M[`op]==`sh||IR_M[`op]==`sb)
114
+ `define store_W (IR_W[`op]==`sw||IR_W[`op]==`sh||IR_W[`op]==`sb)
115
+
116
+ `define beq_D (IR_D[`op]==`beq||IR_D[`op]==`bne||IR_D[`op]==`bgtz||IR_D[`op]==`blez||(IR_D[`op]==6'b000001&&(IR_D[`rt]==5'b00000||IR_D[`rt]==5'b00001)))
117
+ `define beq_E (IR_E[`op]==`beq||IR_E[`op]==`bne||IR_E[`op]==`bgtz||IR_E[`op]==`blez||(IR_E[`op]==6'b000001&&(IR_E[`rt]==5'b00000||IR_E[`rt]==5'b00001)))
118
+ `define beq_M (IR_M[`op]==`beq||IR_M[`op]==`bne||IR_M[`op]==`bgtz||IR_M[`op]==`blez||(IR_M[`op]==6'b000001&&(IR_M[`rt]==5'b00000||IR_M[`rt]==5'b00001)))
119
+ `define beq_W (IR_W[`op]==`beq||IR_W[`op]==`bne||IR_W[`op]==`bgtz||IR_W[`op]==`blez||(IR_W[`op]==6'b000001&&(IR_W[`rt]==5'b00000||IR_W[`rt]==5'b00001)))
120
+
121
+ `define jal_D (IR_D[`op]==`jal)
122
+ `define jal_E (IR_E[`op]==`jal)
123
+ `define jal_M (IR_M[`op]==`jal)
124
+ `define jal_W (IR_W[`op]==`jal)
125
+
126
+ `define jalr_D (IR_D[`op]==`R&&IR_D[`func]==`jalr)
127
+ `define jalr_E (IR_E[`op]==`R&&IR_E[`func]==`jalr)
128
+ `define jalr_M (IR_M[`op]==`R&&IR_M[`func]==`jalr)
129
+ `define jalr_W (IR_W[`op]==`R&&IR_W[`func]==`jalr)
130
+
131
+ `define jr_D (IR_D[`op]==`R&&IR_D[`func]==`jr)
132
+ `define jr_E (IR_E[`op]==`R&&IR_E[`func]==`jr)
133
+ `define jr_M (IR_M[`op]==`R&&IR_M[`func]==`jr)
134
+ `define jr_W (IR_W[`op]==`R&&IR_W[`func]==`jr)
135
+
136
+ `define mfhi_D (IR_D[`op]==`R&&IR_D[`func]==`mfhi)
137
+ `define mfhi_E (IR_E[`op]==`R&&IR_E[`func]==`mfhi)
138
+ `define mfhi_M (IR_M[`op]==`R&&IR_M[`func]==`mfhi)
139
+ `define mfhi_W (IR_W[`op]==`R&&IR_W[`func]==`mfhi)
140
+
141
+ `define mflo_D (IR_D[`op]==`R&&IR_D[`func]==`mflo)
142
+ `define mflo_E (IR_E[`op]==`R&&IR_E[`func]==`mflo)
143
+ `define mflo_M (IR_M[`op]==`R&&IR_M[`func]==`mflo)
144
+ `define mflo_W (IR_W[`op]==`R&&IR_W[`func]==`mflo)
145
+
146
+ `define mf_D (`mfhi_D||`mflo_D)
147
+ `define mf_E (`mfhi_E||`mflo_E)
148
+ `define mf_M (`mfhi_M||`mflo_M)
149
+ `define mf_W (`mfhi_W||`mflo_W)
150
+
151
+ `define mfc0_D (IR_D[`op]==6'b010000&&IR_D[`rs]==5'b00000)
152
+ `define mfc0_E (IR_E[`op]==6'b010000&&IR_E[`rs]==5'b00000)
153
+ `define mfc0_M (IR_M[`op]==6'b010000&&IR_M[`rs]==5'b00000)
154
+ `define mfc0_W (IR_W[`op]==6'b010000&&IR_W[`rs]==5'b00000)
155
+
156
+ `define mtc0_D (IR_D[`op]==6'b010000&&IR_D[`rs]==5'b00100)
157
+ `define mtc0_E (IR_E[`op]==6'b010000&&IR_E[`rs]==5'b00100)
158
+ `define mtc0_M (IR_M[`op]==6'b010000&&IR_M[`rs]==5'b00100)
159
+ `define mtc0_W (IR_W[`op]==6'b010000&&IR_W[`rs]==5'b00100)
160
+
161
+ reg stall=0;
162
+ wire stall_b,stall_cal_r,stall_cal_i,stall_load,stall_store,stall_jr,stall_jalr,stall_busy,stall_mfmt,stall_eret;
163
+
164
+ assign stall_b = (`beq_D & `cal_r_E & (IR_D[`rs]==IR_E[`rd]&IR_E[`rd]!=0||IR_D[`rt]==IR_E[`rd]&IR_E[`rd]!=0))||
165
+ (`beq_D & `cal_i_E & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0||IR_D[`rt]==IR_E[`rt]&IR_E[`rt]!=0))||
166
+ (`beq_D & `load_E & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0||IR_D[`rt]==IR_E[`rt]&IR_E[`rt]!=0))||
167
+ (`beq_D & `load_M & (IR_D[`rs]==IR_M[`rt]&IR_M[`rt]!=0||IR_D[`rt]==IR_M[`rt]&IR_M[`rt]!=0));
168
+ assign stall_cal_r = (`cal_r_D) && (`load_E) && (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0||IR_D[`rt]==IR_E[`rt]&IR_E[`rt]!=0);
169
+ assign stall_cal_i = (`cal_i_D) && (`load_E) && (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0);
170
+ assign stall_load = (`load_D) && (`load_E) && (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0);
171
+ assign stall_store = (`store_D) && (`load_E) && (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0);
172
+ assign stall_jr = (`jr_D & (`cal_r_E) & (IR_D[`rs]==IR_E[`rd]&IR_E[`rd]!=0))||
173
+ (`jr_D & (`cal_i_E) & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0))||
174
+ (`jr_D & (`load_E) & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0))||
175
+ (`jr_D & (`load_M) & (IR_D[`rs]==IR_M[`rt]&IR_M[`rt]!=0));
176
+ assign stall_jalr = (`jalr_D & (`cal_r_E) & (IR_D[`rs]==IR_E[`rd]&IR_E[`rd]!=0))||
177
+ (`jalr_D & (`cal_i_E) & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0))||
178
+ (`jalr_D & (`load_E) & (IR_D[`rs]==IR_E[`rt]&IR_E[`rt]!=0))||
179
+ (`jalr_D & (`load_M) & (IR_D[`rs]==IR_M[`rt]&IR_M[`rt]!=0));
180
+ assign stall_busy = (IR_D[`op]==`R&(IR_D[`func]==`mult||IR_D[`func]==`multu||IR_D[`func]==`div||IR_D[`func]==`divu||
181
+ IR_D[`func]==`mflo||IR_D[`func]==`mfhi||IR_D[`func]==`mthi||IR_D[`func]==`mtlo)) & (Busy||start);
182
+ //assign stall_mfmt = (IR_D[`op]==`R&(IR_D[`func]==`mult||IR_D[`func]==`multu||IR_D[`func]==`div||IR_D[`func]==`divu||
183
+ // IR_D[`func]==`mflo||IR_D[`func]==`mfhi||IR_D[`func]==`mthi||IR_D[`func]==`mtlo))
184
+ // &(IR_E[`op]==`R&(IR_E[`func]==`mult||IR_E[`func]==`multu||IR_E[`func]==`div||IR_E[`func]==`divu));
185
+ assign stall_eret = (IR_E[`op]==6'b010000&&IR_E[`rs]==5'b00100&&IR_E[`rd]==14)&&IR_D==32'h42000018;
186
+ always@(*) begin
187
+ stall <= stall_b||stall_cal_r||stall_cal_i||stall_load||stall_store||stall_jr||stall_jalr||stall_busy||stall_eret;
188
+ end
189
+
190
+ assign IR_D_en = !stall;
191
+ assign IR_E_clr = stall;
192
+ assign PC_en = !stall;
193
+
194
+ `define RSD (`cal_r_D||`cal_i_D||`load_D||`store_D||`beq_D||`jr_D||`jalr_D)
195
+ `define RTD (`cal_r_D||`store_D||`beq_D||`mtc0_D)
196
+ `define RSE (`cal_r_E||`cal_i_E||`load_E||`store_E)
197
+ `define RTE (`cal_r_E||`store_E||`mtc0_E)
198
+ `define RTM (`store_M||`mtc0_M)
199
+
200
+ assign ForwardRSD = (`RSD & `jal_E & IR_D[`rs]==31 & IR_D[`rs]!=0) ? 3 :
201
+ (`RSD & `jalr_E & IR_D[`rs]==IR_E[`rd] & IR_D[`rs]!=0) ? 3 :
202
+ (`RSD & `mf_E & IR_D[`rs]==IR_E[`rd] & IR_D[`rs]!=0) ? 6 :
203
+ (`RSD & `cal_r_M & IR_D[`rs]==IR_M[`rd] & IR_D[`rs]!=0) ? 1 :
204
+ (`RSD & `cal_i_M & IR_D[`rs]==IR_M[`rt] & IR_D[`rs]!=0) ? 1 :
205
+ (`RSD & `jal_M & IR_D[`rs]==31 & IR_D[`rs]!=0) ? 4 :
206
+ (`RSD & `jalr_M & IR_D[`rs]==IR_M[`rd] & IR_D[`rs]!=0) ? 4 :
207
+ (`RSD & `mf_M & IR_D[`rs]==IR_M[`rd] & IR_D[`rs]!=0) ? 5 :
208
+ (`RSD & `mfc0_M & IR_D[`rs]==IR_M[`rt] & IR_D[`rs]!=0) ? 7 :
209
+ (`RSD & `cal_r_W & IR_D[`rs]==IR_W[`rd] & IR_D[`rs]!=0) ? 2 :
210
+ (`RSD & `cal_i_W & IR_D[`rs]==IR_W[`rt] & IR_D[`rs]!=0) ? 2 :
211
+ (`RSD & `load_W & IR_D[`rs]==IR_W[`rt] & IR_D[`rs]!=0) ? 2 :
212
+ (`RSD & `mf_W & IR_D[`rs]==IR_W[`rd] & IR_D[`rs]!=0) ? 2 :
213
+ (`RSD & `mfc0_W & IR_D[`rs]==IR_W[`rt] & IR_D[`rs]!=0) ? 2 :
214
+ (`RSD & `jal_W & IR_D[`rs]==31 & IR_D[`rs]!=0) ? 2 :
215
+ (`RSD & `jalr_W & IR_D[`rs]==IR_W[`rd] & IR_D[`rs]!=0) ? 2 : 0 ;
216
+
217
+ assign ForwardRTD = (`RTD & `jal_E & IR_D[`rt]==31 & IR_D[`rt]!=0) ? 3 :
218
+ (`RTD & `jalr_E & IR_D[`rt]==IR_E[`rd] & IR_D[`rt]!=0) ? 3 :
219
+ (`RTD & `mf_E & IR_D[`rt]==IR_E[`rd] & IR_D[`rt]!=0) ? 6 :
220
+ (`RTD & `cal_r_M & IR_D[`rt]==IR_M[`rd] & IR_D[`rt]!=0) ? 1 :
221
+ (`RTD & `cal_i_M & IR_D[`rt]==IR_M[`rt] & IR_D[`rt]!=0) ? 1 :
222
+ (`RTD & `jal_M & IR_D[`rt]==31 & IR_D[`rt]!=0) ? 4 :
223
+ (`RTD & `jalr_M & IR_D[`rt]==IR_M[`rd] & IR_D[`rt]!=0) ? 4 :
224
+ (`RTD & `mf_M & IR_D[`rt]==IR_M[`rd] & IR_D[`rt]!=0) ? 7 :
225
+ (`RTD & `mfc0_M & IR_D[`rt]==IR_M[`rt] & IR_D[`rt]!=0) ? 5 :
226
+ (`RTD & `cal_r_W & IR_D[`rt]==IR_W[`rd] & IR_D[`rt]!=0) ? 2 :
227
+ (`RTD & `cal_i_W & IR_D[`rt]==IR_W[`rt] & IR_D[`rt]!=0) ? 2 :
228
+ (`RTD & `load_W & IR_D[`rt]==IR_W[`rt] & IR_D[`rt]!=0) ? 2 :
229
+ (`RTD & `mf_W & IR_D[`rt]==IR_W[`rd] & IR_D[`rt]!=0) ? 2 :
230
+ (`RTD & `mfc0_W & IR_D[`rt]==IR_W[`rt] & IR_D[`rt]!=0) ? 2 :
231
+ (`RTD & `jal_W & IR_D[`rt]==31 & IR_D[`rt]!=0) ? 2 :
232
+ (`RTD & `jalr_W & IR_D[`rt]==IR_W[`rd] & IR_D[`rt]!=0) ? 2 : 0;
233
+
234
+ assign ForwardRSE = (`RSE & `cal_r_M & (IR_E[`rs]==IR_M[`rd]) & IR_E[`rs]!=0) ? 1 :
235
+ (`RSE & `cal_i_M & (IR_E[`rs]==IR_M[`rt]) & IR_E[`rs]!=0) ? 1 :
236
+ (`RSE & `jal_M & (IR_E[`rs]==31) & IR_E[`rs]!=0) ? 4 :
237
+ (`RSE & `jalr_M & (IR_E[`rs]==IR_M[`rd]) & IR_E[`rs]!=0) ? 4 :
238
+ (`RSE & `mf_M & (IR_E[`rs]==IR_M[`rd]) & IR_E[`rs]!=0) ? 7 :
239
+ (`RSE & `mfc0_M & (IR_E[`rs]==IR_M[`rt]) & IR_E[`rs]!=0) ? 5 :
240
+ (`RSE & `cal_r_W & (IR_E[`rs]==IR_W[`rd]) & IR_E[`rs]!=0) ? 2 :
241
+ (`RSE & `cal_i_W & (IR_E[`rs]==IR_W[`rt]) & IR_E[`rs]!=0) ? 2 :
242
+ (`RSE & `load_W & (IR_E[`rs]==IR_W[`rt]) & IR_E[`rs]!=0) ? 2 :
243
+ (`RSE & `mf_W & (IR_E[`rs]==IR_W[`rd]) & IR_E[`rs]!=0) ? 2 :
244
+ (`RSE & `mfc0_W & (IR_E[`rs]==IR_W[`rt]) & IR_E[`rs]!=0) ? 2 :
245
+ (`RSE & `jal_W & (IR_E[`rs]==31) & IR_E[`rs]!=0) ? 2 :
246
+ (`RSE & `jalr_W & (IR_E[`rs]==IR_W[`rd]) & IR_E[`rs]!=0) ? 2 : 0;
247
+
248
+ assign ForwardRTE = (`RTE & `cal_r_M & (IR_E[`rt]==IR_M[`rd]) & IR_E[`rt]!=0) ? 1 :
249
+ (`RTE & `cal_i_M & (IR_E[`rt]==IR_M[`rt]) & IR_E[`rt]!=0) ? 1 :
250
+ (`RTE & `jal_M & (IR_E[`rt]==31) & IR_E[`rt]!=0) ? 4 :
251
+ (`RTE & `jalr_M & (IR_E[`rt]==IR_M[`rd]) & IR_E[`rt]!=0) ? 4 :
252
+ (`RTE & `mf_M & (IR_E[`rt]==IR_M[`rd]) & IR_E[`rt]!=0) ? 7 :
253
+ (`RTE & `mfc0_M & (IR_E[`rt]==IR_M[`rt]) & IR_E[`rt]!=0) ? 5 :
254
+ (`RTE & `cal_r_W & (IR_E[`rt]==IR_W[`rd]) & IR_E[`rt]!=0) ? 2 :
255
+ (`RTE & `cal_i_W & (IR_E[`rt]==IR_W[`rt]) & IR_E[`rt]!=0) ? 2 :
256
+ (`RTE & `load_W & (IR_E[`rt]==IR_W[`rt]) & IR_E[`rt]!=0) ? 2 :
257
+ (`RTE & `mf_W & (IR_E[`rt]==IR_W[`rd]) & IR_E[`rt]!=0) ? 2 :
258
+ (`RTE & `mfc0_W & (IR_E[`rt]==IR_W[`rt]) & IR_E[`rt]!=0) ? 2 :
259
+ (`RTE & `jal_W & (IR_E[`rt]==31) & IR_E[`rt]!=0) ? 2 :
260
+ (`RTE & `jalr_W & (IR_E[`rt]==IR_W[`rd]) & IR_E[`rt]!=0) ? 2 : 0;
261
+
262
+ assign ForwardRTM = (`RTM & `cal_r_W & (IR_M[`rt]==IR_W[`rd]) & IR_M[`rt]!=0) ? 2 :
263
+ (`RTM & `cal_i_W & (IR_M[`rt]==IR_W[`rt]) & IR_M[`rt]!=0) ? 2 :
264
+ (`RTM & `load_W & (IR_M[`rt]==IR_W[`rt]) & IR_M[`rt]!=0) ? 2 :
265
+ (`RTM & `mf_W & (IR_M[`rt]==IR_W[`rd]) & IR_M[`rt]!=0) ? 2 :
266
+ (`RTM & `mfc0_W & (IR_M[`rt]==IR_W[`rt]) & IR_M[`rt]!=0) ? 2 :
267
+ (`RTM & `jal_W & (IR_M[`rt]==31) & IR_M[`rt]!=0) ? 2 :
268
+ (`RTM & `jalr_W & (IR_M[`rt]==IR_W[`rd]) & IR_M[`rt]!=0) ? 2 : 0;
269
+
270
+ assign ForwardERET = ((IR_D==32'h42000018)&&(IR_M[`op]==6'b010000&&IR_M[`rs]==5'b00100&&IR_M[`rd]==14)) ? 1 : 0;
271
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/im.v ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:57:01 11/23/2018
7
+ // Design Name:
8
+ // Module Name: im
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module im(
22
+ input [31:0] PC,
23
+ output[31:0] instruction
24
+ );
25
+
26
+ reg[31:0] im[0:4095];
27
+ initial begin
28
+ $readmemh("code.txt",im);
29
+ $readmemh("code_handler.txt",im,1120,2047);
30
+ end
31
+
32
+ wire[31:0] pc=PC-32'h00003000;
33
+ assign instruction = im[pc[13:2]];
34
+
35
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/mips.v ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 16:46:09 11/23/2018
7
+ // Design Name:
8
+ // Module Name: mips
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module mips(
22
+ input clk,
23
+ input reset
24
+ );
25
+
26
+ wire [31:0] PrRD;
27
+ wire [6:2] ExcCode;
28
+ wire [31:0] PrAddr;
29
+ wire [31:0] PrWD;
30
+ wire PrWe;
31
+ wire [31:0] DEV_Addr;
32
+ wire [31:0] DEV_WD;
33
+ wire [31:0] DEV0_RD;
34
+ wire [31:0] DEV1_RD;
35
+ wire WeDEV0;
36
+ wire WeDEV1;
37
+ wire IRQ0;
38
+ wire IRQ1;
39
+
40
+ CPU my_CPU(.clk(clk),.reset(reset),.HWInt({4'b0,IRQ1,IRQ0}),.PrRD(PrRD),.ExcCode(ExcCode),.PrAddr(PrAddr),.PrWD(PrWD),.PrWe(PrWe));
41
+ Bridge my_Bridge(PrAddr,PrWe,PrRD,PrWD,DEV_Addr,DEV_WD,DEV0_RD,DEV1_RD,WeDEV0,WeDEV1);
42
+ DEV0 my_time0(clk,reset,DEV_Addr,WeDEV0,DEV_WD,DEV0_RD,IRQ0);
43
+ DEV1 my_time1(clk,reset,DEV_Addr,WeDEV1,DEV_WD,DEV1_RD,IRQ1);
44
+
45
+ assign ExcCode = (IRQ0||IRQ1) ? 5'b00000 : 5'b11111;
46
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/mux.v ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:31:53 11/23/2018
7
+ // Design Name:
8
+ // Module Name: mux
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module mux(
22
+ input [31:0] EXT_E,
23
+ input [31:0] IR_E,
24
+ input [31:0] IR_W,
25
+ input [31:0] DR_Wnew,
26
+ input [31:0] AO_W,
27
+ input [31:0] MDO_W,
28
+ input [31:0] PC8_W,
29
+ input [31:0] CP0_W,
30
+ input [31:0] MFRSE,
31
+ input [31:0] MFRTE,
32
+ input [31:0] High,
33
+ input [31:0] Low,
34
+ input ALUasel,
35
+ input ALUbsel,
36
+ input if_mfhi,
37
+ input if_mflo,
38
+ input [1:0] RegDst,
39
+ input [2:0] MemtoReg,
40
+ output reg[31:0] ALU_A,
41
+ output reg[31:0] ALU_B,
42
+ output reg[4:0] MUX_A3,
43
+ output reg[31:0] MUX_WD,
44
+ output reg[31:0] MD_out
45
+ );
46
+
47
+ always@(*) begin
48
+ case(ALUasel)
49
+ 1'b0: ALU_A<=MFRSE;
50
+ 1'b1: ALU_A<={27'b0,IR_E[10:6]};
51
+ endcase
52
+
53
+ case(ALUbsel)
54
+ 1'b0: ALU_B<=MFRTE;
55
+ 1'b1: ALU_B<=EXT_E;
56
+ endcase
57
+
58
+ case(RegDst)
59
+ 2'b00: MUX_A3<=IR_W[20:16];
60
+ 2'b01: MUX_A3<=IR_W[15:11];
61
+ 2'b10: MUX_A3<=32'h1f;
62
+ 2'b11: MUX_A3<=0;
63
+ endcase
64
+
65
+ case(MemtoReg)
66
+ 3'b000: MUX_WD<=AO_W;
67
+ 3'b001: MUX_WD<=DR_Wnew;
68
+ 3'b010: MUX_WD<=PC8_W;
69
+ 3'b011: MUX_WD<=MDO_W;
70
+ 3'b100: MUX_WD<=CP0_W;
71
+ endcase
72
+
73
+ if(if_mfhi) MD_out<=High;
74
+ else if(if_mflo) MD_out<=Low;
75
+ else MD_out<=0;
76
+ end
77
+
78
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/npc.v ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:13:22 11/23/2018
7
+ // Design Name:
8
+ // Module Name: npc
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module npc(
22
+ input [31:0] PC4,
23
+ input [31:0] PC4D,
24
+ input [25:0] I26,
25
+ input [31:0] MFRSD,
26
+ input [31:0] EPC,
27
+ input Zero,
28
+ input more,
29
+ input less,
30
+ input if_beq,
31
+ input if_bne,
32
+ input if_bgtz,
33
+ input if_blez,
34
+ input if_bgez,
35
+ input if_bltz,
36
+ input if_j,//j��jal
37
+ input[1:0] PC_sel,
38
+ input Interrupt,
39
+ output reg[31:0] next_pc
40
+ );
41
+ reg[31:0] NPC;
42
+ always@(*) begin
43
+ if((if_beq&&Zero)||(if_bne&&!Zero)||(if_bgtz&&more)||(if_blez&&!more)||(if_bgez&&!less)||(if_bltz&&less)) begin
44
+ NPC<=PC4D+{{14{I26[15]}},I26[15:0],2'b00};
45
+ end
46
+ else if(if_j) begin
47
+ NPC<={PC4D[31:28],I26[25:0],2'b00};
48
+ end
49
+ else begin
50
+ NPC<=PC4D+4;
51
+ end
52
+ case(PC_sel)
53
+ 2'b00: next_pc<=PC4;
54
+ 2'b01: next_pc<=MFRSD;
55
+ 2'b10: next_pc<=NPC;
56
+ 2'b11: next_pc<=EPC;
57
+ endcase
58
+ if(Interrupt) begin
59
+ next_pc<=32'h00004180;
60
+ end
61
+ end
62
+
63
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/pc.v ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 17:02:00 11/23/2018
7
+ // Design Name:
8
+ // Module Name: pc
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module pc(
22
+ input clk,
23
+ input reset,
24
+ input en,
25
+ input[31:0] next_pc,
26
+ output reg[31:0] pc,
27
+ output [4:0] excode_F
28
+ );
29
+
30
+ initial begin
31
+ pc<=32'h00003000;
32
+ end
33
+
34
+ assign excode_F = ((pc[1:0]!=2'b00)||(pc < 32'h00003000)||(pc > 32'h00004ffc)) ? 4 : 0;
35
+
36
+ always@(posedge clk) begin
37
+ if(reset) begin
38
+ pc <= 32'h00003000;
39
+ end
40
+ else if(en) begin
41
+ pc <= next_pc;
42
+ end
43
+ else begin
44
+ pc <= pc;
45
+ end
46
+ end
47
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_7/test.v ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////
4
+ // Company:
5
+ // Engineer:
6
+ //
7
+ // Create Date: 23:05:00 11/25/2018
8
+ // Design Name: mips
9
+ // Module Name: E:/Verilog/p5/test.v
10
+ // Project Name: p5
11
+ // Target Device:
12
+ // Tool versions:
13
+ // Description:
14
+ //
15
+ // Verilog Test Fixture created by ISE for module: mips
16
+ //
17
+ // Dependencies:
18
+ //
19
+ // Revision:
20
+ // Revision 0.01 - File Created
21
+ // Additional Comments:
22
+ //
23
+ ////////////////////////////////////////////////////////////////////////////////
24
+
25
+ module test;
26
+
27
+ // Inputs
28
+ reg clk;
29
+ reg reset;
30
+
31
+ // Instantiate the Unit Under Test (UUT)
32
+ mips uut (
33
+ .clk(clk),
34
+ .reset(reset)
35
+ );
36
+
37
+ initial begin
38
+ clk = 0;
39
+ reset = 1;
40
+ #12 reset = 0;
41
+ end
42
+
43
+ always #10 clk = ~clk;
44
+
45
+
46
+ endmodule
47
+
48
+
aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/Bridge.v ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 23:05:11 12/12/2018
7
+ // Design Name:
8
+ // Module Name: Bridge
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module Bridge(
22
+ input [31:0] PrAddr,
23
+ input PrWE,
24
+ output [31:0] PrRD,
25
+ input [31:0] PrWD,
26
+ output [31:0] DEV_Addr,
27
+ output [31:0] DEV_WD,
28
+ input [31:0] DEV0_RD,
29
+ input [31:0] DEV1_RD,
30
+ output WeDEV0,
31
+ output WeDEV1
32
+ );
33
+
34
+ `define DEBUG_DEV_DATA 32'b0
35
+
36
+ //DEV0 00007F00-00007F0B
37
+ //DEV1 00007F10-00007F1B
38
+ wire HitDEV0,HitDEV1;
39
+ assign HitDEV0 = (PrAddr[31:4]==28'h00007F0);
40
+ assign HitDEV1 = (PrAddr[31:4]==28'h00007F1);
41
+
42
+ assign PrRD = (HitDEV0) ? DEV0_RD :
43
+ (HitDEV1) ? DEV1_RD :
44
+ `DEBUG_DEV_DATA;
45
+
46
+ assign WeDEV0 = PrWE&&HitDEV0;
47
+ assign WeDEV1 = PrWE&&HitDEV1;
48
+
49
+ assign DEV_Addr = PrAddr;
50
+ assign DEV_WD = PrWD;
51
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/CP0.v ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 15:46:14 12/13/2018
7
+ // Design Name:
8
+ // Module Name: CP0
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module CP0(
22
+ input[4:0] A1,//��CP0�Ĵ������ ִ��MFC0ָ��ʱ����
23
+ input[4:0] A2,//дCP0�Ĵ������ ִ��MTC0ָ��ʱ����
24
+ input[31:0] DIn,//CP0�Ĵ�����д������ ִ��MTC0ָ��ʱ���� ��������GPR
25
+ input[31:0] PC,//�ж�/�쳣ʱ��PC
26
+ input[31:0] IR_M,//ָ��
27
+ input Zero,
28
+ input more,
29
+ input less,
30
+ input if_bd,
31
+ input[6:2] ExcCode,//�ж�/�쳣������
32
+ input[5:0] HWInt,//6���豸�ж�
33
+ input We,//CP0дʹ�� ִ��MTC0ָ��ʱ����
34
+ input EXLSet,//������λSR��EXL(EXLΪ1) ��ˮ����M�׶β���
35
+ input EXLClr,//�������SR��EXL(EXLΪ0) ִ��ERETָ��ʱ����
36
+ input clk,
37
+ input reset,
38
+ output Interrupt,//�жϺ��쳣 ��HWInt/IM/EXL/IE�ĺ���
39
+ output[31:0] EPC,//EPC�Ĵ��������NPC
40
+ output[31:0] DOut //CP0�Ĵ������������ ִ��MFC0ָ��ʱ���������������GRF
41
+ );
42
+
43
+ `define SR 12
44
+ `define CAUSE 13
45
+ `define EPC 14
46
+ `define PRID 15
47
+
48
+ `define R 6'b000000
49
+ `define beq 6'b000100
50
+ `define bne 6'b000101
51
+ `define bgtz 6'b000111
52
+ `define blez 6'b000110
53
+ `define jal 6'b000011
54
+ `define jr 6'b001000
55
+ `define j 6'b000010
56
+ `define jalr 6'b001001
57
+
58
+ //SR {16'b0, im, 8'b0, exl, ie}
59
+ reg[15:10] im; //6λ�ж�����λ���ֱ��Ӧ6���ⲿ�ж� 1-�����жϣ�0-��ֹ�ж�
60
+ reg exl;//EXL���쳣�� 1-�����쳣�����������жϣ�0-�����ж�
61
+ reg ie;//ȫ���ж�ʹ�� 1-�����жϣ�0-��ֹ�ж�
62
+ //CAUSE {bd, 15'b0, hwint_pend[15:10], 3'b0, exccode[6:2], 2'b0}
63
+ reg bd;//�쳣�������ӳٲ۵�ָ��Ļ� bd=1
64
+ reg[6:2] exccode;
65
+ reg[15:10] hwint_pend;//��clock�����ز��ϵı����ⲿ6���ж�(HWInt[5:0])
66
+ //EPC
67
+ reg[31:0] epc;
68
+ //PRID
69
+ reg[31:0] prid = 32'h12345678;
70
+ wire Exception;//�쳣
71
+ wire IntReq;//�ж�
72
+ assign EPC = epc;
73
+ //�� ����SR/Cause/EPC/PRId�⣬���õļĴ���һ�����0
74
+ assign DOut = (A1==`SR) ? {16'b0, im, 8'b0, exl, ie} :
75
+ (A1==`CAUSE) ? {bd, 15'b0, hwint_pend[15:10], 3'b0, exccode[6:2], 2'b0} :
76
+ (A1==`EPC) ? epc :
77
+ (A1==`PRID) ? prid : 0;
78
+ assign IntReq = (|(HWInt[5:0] & im[15:10])) & ie & !exl ;
79
+ assign Exception = (ExcCode>0) ? 1'b1 : 1'b0;
80
+ assign Interrupt = IntReq||Exception;
81
+
82
+ integer i;
83
+ always@(posedge clk) begin
84
+ hwint_pend <= HWInt;//��clock�����ز��ϵı����ⲿ6���ж�(HWInt[5:0])
85
+ if(reset) begin
86
+ im<=6'b0;//SR
87
+ exl<=1'b0;
88
+ ie<=1'b0;
89
+ hwint_pend<=6'b0;//CAUSE
90
+ bd<=0;
91
+ exccode<=0;
92
+ epc<=0;//EPC
93
+ end
94
+ else begin
95
+ epc <= (Interrupt&&bd) ? {PC[31:2], 2'b00} - 4 :
96
+ (Interrupt&&!bd) ? {PC[31:2], 2'b00} :
97
+ epc;
98
+ if(bd==1'b0) begin
99
+ bd <= (((IR_M[31:26]==`j)||(IR_M[31:26]==`jal)||(IR_M[31:26]==`beq&&Zero)||(IR_M[31:26]==`bne&&!Zero)||(IR_M[31:26]==`blez&&!more)||(IR_M[31:26]==`bgtz&&more)
100
+ ||(IR_M[31:26]==`R&&(IR_M[5:0]==`jr||IR_M[5:0]==`jalr))||(IR_M[31:26]==6'b000001&&IR_M[20:16]==5'b00000&&less)||(IR_M[31:26]==6'b000001&&IR_M[20:16]==5'b00001&&!less)));
101
+ end
102
+ else begin
103
+ if(exl==1'b0&&Interrupt==1'b0) bd<=1'b0;
104
+ end
105
+ if(We) begin //�
106
+ case(A2)
107
+ `SR: begin
108
+ {im, exl, ie} <= {DIn[15:10], DIn[1], DIn[0]};
109
+ end
110
+ `CAUSE: begin
111
+ hwint_pend <= DIn[15:10];
112
+ end
113
+ `EPC: begin
114
+ epc <= DIn;
115
+ end
116
+ `PRID: begin
117
+ prid <= DIn;
118
+ end
119
+ default: begin
120
+ end
121
+ endcase
122
+ end
123
+ if(EXLSet||Interrupt) begin
124
+ exl<=1'b1;
125
+ exccode<=ExcCode;
126
+ end
127
+ if(EXLClr) begin
128
+ exl<=1'b0;
129
+ bd<=1'b0;
130
+ end
131
+ //if(IntReq) begin
132
+ // bd<=1'b0;
133
+ //end
134
+ end
135
+ end
136
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/CPU.v ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 11:30:40 12/15/2018
7
+ // Design Name:
8
+ // Module Name: CPU
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module CPU(
22
+ input clk,
23
+ input reset,
24
+ input [7:2] HWInt,
25
+ input [31:0] PrRD,
26
+ input [6:2] ExcCode,
27
+ output [31:0] PrAddr,
28
+ output [31:0] PrWD,
29
+ output PrWe
30
+ );
31
+
32
+ `define op 31:26
33
+ `define rs 25:21
34
+ `define rt 20:16
35
+ `define rd 15:11
36
+ `define shamt 10:6
37
+ `define func 5:0
38
+ `define imm16 15:0
39
+ `define imm26 25:0
40
+
41
+ wire PC_en;
42
+ wire[1:0] PC_sel;
43
+ wire[31:0] PC;
44
+ wire[31:0] PC4;
45
+ wire[31:0] Instr;
46
+ wire ID_en;
47
+ wire EX_en;
48
+ wire MEM_en;
49
+ wire WB_en;
50
+ wire ID_reset;
51
+ wire EX_reset;
52
+ wire MEM_reset;
53
+ wire WB_reset;
54
+ wire[31:0] IR_D;
55
+ wire[31:0] PC_D;
56
+ wire[31:0] PC4_D;
57
+ wire[31:0] PC8_D;
58
+ wire[31:0] IR_E;
59
+ wire[31:0] PC_E;
60
+ wire[31:0] PC4_E;
61
+ wire[31:0] PC8_E;
62
+ wire[31:0] IR_M;
63
+ wire[31:0] PC_M;
64
+ wire[31:0] PC4_M;
65
+ wire[31:0] PC8_M;
66
+ wire[31:0] IR_W;
67
+ wire[31:0] PC_W;
68
+ wire[31:0] PC4_W;
69
+ wire[31:0] PC8_W;
70
+ wire Zero;
71
+ wire Zero_E;
72
+ wire Zero_M;
73
+ wire more;
74
+ wire less;
75
+ wire more_E;
76
+ wire less_E;
77
+ wire more_M;
78
+ wire less_M;
79
+ wire if_beq;
80
+ wire if_bne;
81
+ wire if_blez;
82
+ wire if_bgtz;
83
+ wire if_bltz;
84
+ wire if_bgez;
85
+ wire if_j;
86
+ wire[1:0] EXTop;
87
+ wire if_sh;
88
+ wire if_sb;
89
+
90
+ wire[3:0] ALUCtrl;
91
+ wire[1:0] RegDst;
92
+ wire ALUASrc;
93
+ wire ALUBSrc;
94
+ wire RegWrite;
95
+ wire MemRead;
96
+ wire MemWrite;
97
+ wire [2:0] MemtoReg;
98
+ wire [2:0] dataOp;
99
+ wire [1:0] multdivOp;
100
+ wire if_mthi;
101
+ wire if_mtlo;
102
+ wire if_mfhi;
103
+ wire if_mflo;
104
+ wire start;
105
+ wire Busy;
106
+ wire[31:0] High;
107
+ wire[31:0] Low;
108
+
109
+ wire [31:0] RS_E;
110
+ wire [31:0] RT_E;
111
+ wire [31:0] EXT_E;
112
+ wire [31:0] AO_M;
113
+ wire [31:0] MDO_M;
114
+ wire [31:0] RT_M;
115
+ wire [31:0] DR_W;
116
+ wire [31:0] DR_Wnew;
117
+ wire [31:0] AO_W;
118
+ wire [31:0] MDO_W;
119
+
120
+ wire[4:0] MUX_A3;
121
+ wire[31:0] MUX_WD;
122
+ wire[31:0] RF_RD1;
123
+ wire[31:0] RF_RD2;
124
+ wire[31:0] MFRSD;
125
+ wire[31:0] MFRTD;
126
+ wire[31:0] MFRSE;
127
+ wire[31:0] MFRTE;
128
+ wire[31:0] MFRTM;
129
+ wire[31:0] MFERET;
130
+ wire[31:0] EXT_out;
131
+ wire[31:0] ALU_A;
132
+ wire[31:0] ALU_B;
133
+ wire[31:0] ALU_out;
134
+ wire[31:0] MD_out;
135
+ wire[31:0] DM_out;
136
+ wire[31:0] next_pc;
137
+ wire[2:0] ForwardRSD;
138
+ wire[2:0] ForwardRTD;
139
+ wire[2:0] ForwardRSE;
140
+ wire[2:0] ForwardRTE;
141
+ wire[2:0] ForwardRTM;
142
+ wire[2:0] ForwardERET;
143
+
144
+ assign EX_en = 1;
145
+ assign MEM_en = 1;
146
+ assign WB_en = 1;
147
+ assign ID_reset = 0;
148
+ assign MEM_reset = 0;
149
+ assign WB_reset = 0;
150
+ assign PC4 = PC + 4;
151
+
152
+ wire[6:2] excode_F,excode_D,excode_E,excode_M;
153
+ wire[6:2] excode_outF,excode_outD,excode_outE,excode_outM;
154
+ wire over;
155
+ wire cp0WE;
156
+ wire[31:0] CP0_M;
157
+ wire[31:0] CP0_W;
158
+ wire[31:0] EPCout;
159
+ wire Interrupt;
160
+ wire EXLSet;
161
+ wire EXLClr;
162
+ wire[31:0] DMDEV;
163
+ wire if_eret;
164
+
165
+ assign PrAddr = AO_M;
166
+ assign PrWD = MFRTM;
167
+ assign PrWe = (MemWrite&&(~Interrupt));//û�����쳣�ſ���д
168
+ assign EXLSet = Interrupt;
169
+ assign EXLClr = (IR_M[`op]==6'b010000&&IR_M[`func]==6'b011000); //eret
170
+
171
+ pc my_pc(clk,reset,PC_en||Interrupt,next_pc,PC,excode_F);
172
+ im my_im(PC,Instr);
173
+
174
+ ID my_ID(clk,(ID_reset||reset||Interrupt||(IR_D==32'h42000018&&ID_en)),ID_en,Instr,PC,IR_D,PC_D,PC4_D,PC8_D,excode_D);
175
+ ExcCode my_exc_D(clk,ID_reset||reset||Interrupt,ID_en,excode_F,excode_outD);
176
+ controller my_controllerD(.op(IR_D[`op]),.func(IR_D[`func]),.rs(IR_D[`rs]),.rt(IR_D[`rt]),.ExtOp(EXTop),.if_beq(if_beq),.if_bne(if_bne),
177
+ .if_blez(if_blez),.if_bgtz(if_bgtz),.if_bgez(if_bgez),.if_bltz(if_bltz),.if_j(if_j),.PCsel(PC_sel));
178
+ grf my_grf(clk,reset,RegWrite,IR_D[`rs],IR_D[`rt],MUX_A3,MUX_WD,PC_W,RF_RD1,RF_RD2);
179
+ cmp my_cmp(MFRSD,MFRTD,Zero,more,less);
180
+ ext my_ext(IR_D[`imm16],EXTop,EXT_out);
181
+ npc my_npc(PC4,PC4_D,IR_D[`imm26],MFRSD,MFERET,Zero,more,less,if_beq,if_bne,if_bgtz,if_blez,if_bgez,if_bltz,if_j,PC_sel,Interrupt,next_pc);
182
+
183
+ EX my_EX(clk,EX_reset||reset||Interrupt,EX_en,over,IR_D,PC_D,PC4_D,PC8_D,MFRSD,MFRTD,EXT_out,Zero,more,less,IR_E,PC_E,PC4_E,PC8_E,RS_E,RT_E,EXT_E,excode_E,Zero_E,more_E,less_E);
184
+ ExcCode my_exc_E(clk,EX_reset||reset||Interrupt,EX_en,(excode_outD==0?excode_D:excode_outD),excode_outE);
185
+ controller my_controllerE(.op(IR_E[`op]),.func(IR_E[`func]),.rs(IR_E[`rs]),.rt(IR_E[`rt]),.ALUCtrl(ALUCtrl),.ALUASrc(ALUASrc),.ALUBSrc(ALUBSrc),
186
+ .multdivOp(multdivOp),.start(start),.if_mthi(if_mthi),.if_mtlo(if_mtlo),.if_mfhi(if_mfhi),.if_mflo(if_mflo));
187
+ alu my_alu(ALU_A,ALU_B,ALUCtrl,ALU_out,over);
188
+ Mult_Div my_Mult_Div(clk,reset,MFRSE,MFRTE,multdivOp,start,if_mthi,if_mtlo,Busy,High,Low);
189
+
190
+ MEM my_MEM(clk,MEM_reset||reset||Interrupt,MEM_en,IR_E,PC_E,PC4_E,PC8_E,ALU_out,MD_out,MFRTE,Zero_E,more_E,less_E,IR_M,PC_M,PC4_M,PC8_M,AO_M,MDO_M,RT_M,excode_M,Zero_M,more_M,less_M);
191
+ ExcCode my_exc_M(clk,MEM_reset||reset||Interrupt,MEM_en,(excode_outE==0?excode_E:excode_outE),excode_outM);
192
+ controller my_controllerM(.op(IR_M[`op]),.func(IR_M[`func]),.rs(IR_M[`rs]),.rt(IR_M[`rt]),.MemRead(MemRead),.MemWrite(MemWrite),.if_sh(if_sh),.if_sb(if_sb),.cp0WE(cp0WE));
193
+ dm my_dm(clk,reset,(MemWrite&&~Interrupt),MemRead,if_sh,if_sb,AO_M,MFRTM,PC_M,DM_out);
194
+
195
+ //�ж����ȼ���
196
+ CP0 my_CP0(.A1(IR_M[`rd]),.A2(IR_M[`rd]),.DIn(MFRTM),.PC(PC_M),.IR_M(IR_M),.Zero(Zero_M),.more(more_M),.less(less_M),.if_bd(excode_E>0||excode_outE>0),
197
+ .ExcCode(((|HWInt)? ExcCode : (excode_outM==0) ? excode_M : excode_outM)),.HWInt(HWInt),.We(cp0WE),.EXLSet(EXLSet),.EXLClr(EXLClr),
198
+ .clk(clk),.reset(reset),.Interrupt(Interrupt),.EPC(EPCout),.DOut(CP0_M));
199
+
200
+ WB my_WB(clk,WB_reset||reset||Interrupt,WB_en,IR_M,PC_M,PC4_M,PC8_M,AO_M,MDO_M,DM_out,CP0_M,IR_W,PC_W,PC4_W,PC8_W,AO_W,MDO_W,DR_W,CP0_W);
201
+ controller my_controllerW(.op(IR_W[`op]),.func(IR_W[`func]),.rs(IR_W[`rs]),.rt(IR_W[`rt]),.RegDst(RegDst),.RegWrite(RegWrite),.MemtoReg(MemtoReg),.dataOp(dataOp));
202
+ DataExt my_DataExt(DR_W,dataOp,AO_W[1:0],DR_Wnew);
203
+
204
+ assign DMDEV = (AO_W>=32'h00000000&&AO_W<=32'h00002ffc) ? DR_Wnew : PrRD;//��dm����dev
205
+
206
+ mux my_mux(EXT_E,IR_E,IR_W,DMDEV,AO_W,MDO_W,PC8_W,CP0_W,MFRSE,MFRTE,High,Low,ALUASrc,ALUBSrc,if_mfhi,if_mflo,RegDst,MemtoReg,ALU_A,ALU_B,MUX_A3,MUX_WD,MD_out);
207
+ forward_mux my_forward(RS_E,RT_E,RT_M,MUX_WD,AO_M,MDO_M,CP0_M,MD_out,PC8_E,PC8_M,PC8_W,RF_RD1,RF_RD2,EPCout,
208
+ ForwardRSD,ForwardRTD,ForwardRSE,ForwardRTE,ForwardRTM,ForwardERET,MFRSD,MFRTD,MFRSE,MFRTE,MFRTM,MFERET);
209
+ hazardUnit my_hazard(IR_D,IR_E,IR_M,IR_W,Busy,start,ID_en,EX_reset,PC_en,ForwardRSD,ForwardRTD,ForwardRSE,ForwardRTE,ForwardRTM,ForwardERET);
210
+
211
+ endmodule
212
+
aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/DEV0.v ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 22:05:26 12/12/2018
7
+ // Design Name:
8
+ // Module Name: DEV0
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module DEV0(
22
+ input clk,
23
+ input reset,
24
+ input [31:0] Addr,
25
+ input WE,
26
+ input [31:0] DataIn,
27
+ output reg [31:0] DataOut,
28
+ output reg IRQ
29
+ );
30
+
31
+ `define Idle 2'b00
32
+ `define Counting 2'b01
33
+ `define Interrupt 2'b10
34
+
35
+ reg[1:0] state;
36
+ reg[31:0] PRESET;
37
+ reg[31:0] COUNT;
38
+ reg[2:1] Mode;//ģʽѡ��
39
+ reg IM;//�����
40
+ reg Enable;//������ʹ��
41
+ reg irq;
42
+
43
+ always@(posedge clk) begin
44
+ if(reset) begin
45
+ PRESET<=0;
46
+ COUNT<=0;
47
+ state<=`Idle;
48
+ IM<=0;
49
+ Enable<=0;
50
+ irq<=0;
51
+ end
52
+ if(WE) begin
53
+ case(Addr)
54
+ 32'h0000_7f00: begin //ctrl
55
+ {IM,Mode,Enable} <= DataIn[3:0];
56
+ //������������ʱ����������ʹ�ܱ� store ��ָ���޸�Ϊ 0 ��ֹͣ����
57
+ if(state == `Counting && DataIn[0]==1'b0) begin
58
+ COUNT<=PRESET;
59
+ end
60
+ end
61
+ 32'h0000_7f04: begin
62
+ PRESET<=DataIn;
63
+ end
64
+ 32'h0000_7f08: begin
65
+ //COUNT���ᱻд��
66
+ end
67
+ default: begin
68
+ //ʲô������
69
+ end
70
+ endcase
71
+ end
72
+ else begin
73
+ case(state)
74
+ `Idle: begin
75
+ if(Enable==1'b1) begin
76
+ state<=`Counting;
77
+ COUNT<=PRESET;
78
+ irq<=0;
79
+ end
80
+ else begin
81
+ state<=`Idle;
82
+ end
83
+ end
84
+
85
+ `Counting: begin
86
+ if(Enable==1'b0) begin
87
+ state<=`Idle;
88
+ end
89
+ else if(Enable==1'b1&&COUNT<=1) begin
90
+ state<=`Interrupt;
91
+ irq<=0;
92
+ end
93
+ else if(Enable==1'b1) begin
94
+ state<=`Counting;
95
+ COUNT<=COUNT-1;
96
+ irq<=0;
97
+ end
98
+ end
99
+
100
+ `Interrupt: begin
101
+ if(Enable==1'b1) begin
102
+ irq<=1;
103
+ if(Mode==2'b00) begin
104
+ Enable<=0;
105
+ state<=`Idle;
106
+ end
107
+ else if(Mode==2'b01) begin
108
+ state<=`Counting;
109
+ COUNT<=PRESET;
110
+ end
111
+ end
112
+ end
113
+ endcase
114
+ end
115
+ //IRQ<=IM&&(Mode==2'b0)&&(COUNT==0);
116
+ IRQ<=IM&&irq;
117
+ case(Addr[3:2])
118
+ 2'b00: begin
119
+ DataOut<={28'b0,IM,Mode,Enable};
120
+ end
121
+ 2'b01: begin
122
+ DataOut<=PRESET;
123
+ end
124
+ 2'b10: begin
125
+ DataOut<=COUNT;
126
+ end
127
+ 2'b11: begin
128
+ DataOut<=0;
129
+ end
130
+ endcase
131
+ end
132
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/DEV1.v ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 22:40:05 12/12/2018
7
+ // Design Name:
8
+ // Module Name: DEV1
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module DEV1(
22
+ input clk,
23
+ input reset,
24
+ input [31:0] Addr,
25
+ input WE,
26
+ input [31:0] DataIn,
27
+ output reg [31:0] DataOut,
28
+ output reg IRQ
29
+ );
30
+
31
+ `define Idle 2'b00
32
+ `define Counting 2'b01
33
+ `define Interrupt 2'b10
34
+
35
+ reg[1:0] state;
36
+ reg[31:0] PRESET;
37
+ reg[31:0] COUNT;
38
+ reg[2:1] Mode;
39
+ reg IM;
40
+ reg Enable;
41
+ reg irq;
42
+
43
+ always@(posedge clk) begin
44
+ if(reset) begin
45
+ PRESET<=0;
46
+ COUNT<=0;
47
+ state<=`Idle;
48
+ IM<=0;
49
+ Enable<=0;
50
+ irq<=0;
51
+ end
52
+ if(WE) begin
53
+ case(Addr)
54
+ 32'h0000_7f10: begin //ctrl
55
+ {IM,Mode,Enable} <= DataIn[3:0];
56
+ //������������ʱ����������ʹ�ܱ� store ��ָ���޸�Ϊ 0 ��ֹͣ����
57
+ if(state == `Counting && DataIn[0]==1'b0) begin
58
+ COUNT<=PRESET;
59
+ end
60
+ end
61
+ 32'h0000_7f14: begin
62
+ PRESET<=DataIn;
63
+ end
64
+ 32'h0000_7f18: begin
65
+
66
+ end
67
+ default: begin
68
+
69
+ end
70
+ endcase
71
+ end
72
+ else begin
73
+ case(state)
74
+ `Idle: begin
75
+ if(Enable==1'b1) begin
76
+ state<=`Counting;
77
+ COUNT<=PRESET;
78
+ irq<=0;
79
+ end
80
+ else begin
81
+ state<=`Idle;
82
+ end
83
+ end
84
+
85
+ `Counting: begin
86
+ if(Enable==1'b0) begin
87
+ state<=`Idle;
88
+ end
89
+ else if(Enable==1'b1&&COUNT<=1) begin
90
+ state<=`Interrupt;
91
+ irq<=0;
92
+ end
93
+ else if(Enable==1'b1) begin
94
+ state<=`Counting;
95
+ COUNT<=COUNT-1;
96
+ irq<=0;
97
+ end
98
+ end
99
+
100
+ `Interrupt: begin
101
+ if(Enable==1'b1) begin
102
+ irq<=1;
103
+ if(Mode==2'b00) begin
104
+ Enable<=0;
105
+ state<=`Idle;
106
+ end
107
+ else if(Mode==2'b01) begin
108
+ state<=`Counting;
109
+ COUNT<=PRESET;
110
+ end
111
+ end
112
+ end
113
+ endcase
114
+ end
115
+ IRQ<=irq&&IM;
116
+ case({WE,Addr[3:2]})
117
+ 3'b000: begin
118
+ DataOut<={28'b0,IM,Mode,Enable};
119
+ end
120
+ 3'b001: begin
121
+ DataOut<=PRESET;
122
+ end
123
+ 3'b010: begin
124
+ DataOut<=COUNT;
125
+ end
126
+ 3'b011: begin
127
+ DataOut<=0;
128
+ end
129
+ endcase
130
+ end
131
+ endmodule
aptx1231_BUAA_CO/P7/p7_旧/p7_8改bd/DataExt.v ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns / 1ps
2
+ //////////////////////////////////////////////////////////////////////////////////
3
+ // Company:
4
+ // Engineer:
5
+ //
6
+ // Create Date: 11:13:10 12/01/2018
7
+ // Design Name:
8
+ // Module Name: DataExt
9
+ // Project Name:
10
+ // Target Devices:
11
+ // Tool versions:
12
+ // Description:
13
+ //
14
+ // Dependencies:
15
+ //
16
+ // Revision:
17
+ // Revision 0.01 - File Created
18
+ // Additional Comments:
19
+ //
20
+ //////////////////////////////////////////////////////////////////////////////////
21
+ module DataExt(
22
+ input [31:0] Din,
23
+ input [2:0] dataOp,
24
+ input [1:0] Addr,
25
+ output reg[31:0] Dout
26
+ );
27
+
28
+ always@(*) begin
29
+ case(dataOp)
30
+ 3'b000: Dout<=Din;
31
+ 3'b001: begin//lbu
32
+ case(Addr)
33
+ 2'b00: Dout<={24'b0,Din[7:0]};
34
+ 2'b01: Dout<={24'b0,Din[15:8]};
35
+ 2'b10: Dout<={24'b0,Din[23:16]};
36
+ 2'b11: Dout<={24'b0,Din[31:24]};
37
+ endcase
38
+ end
39
+ 3'b010: begin//lb
40
+ case(Addr)
41
+ 2'b00: Dout<={{24{Din[7]}},Din[7:0]};
42
+ 2'b01: Dout<={{24{Din[15]}},Din[15:8]};
43
+ 2'b10: Dout<={{24{Din[23]}},Din[23:16]};
44
+ 2'b11: Dout<={{24{Din[31]}},Din[31:24]};
45
+ endcase
46
+ end
47
+ 3'b011: begin//lhu
48
+ case(Addr[1])
49
+ 1'b0: Dout<={24'b0,Din[15:0]};
50
+ 1'b1: Dout<={24'b0,Din[31:16]};
51
+ endcase
52
+ end
53
+ 3'b100: begin//lh
54
+ case(Addr[1])
55
+ 1'b0: Dout<={{16{Din[15]}},Din[15:0]};
56
+ 1'b1: Dout<={{16{Din[31]}},Din[31:16]};
57
+ endcase
58
+ end
59
+ endcase
60
+ end
61
+ endmodule