-
Notifications
You must be signed in to change notification settings - Fork 0
/
datapath.v
162 lines (122 loc) · 3.01 KB
/
datapath.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
module datapath(clk, reset, IorD, MemRead, MemWrite, MemtoReg, IRWrite,
PCSource, ALUSrcB, ALUSrcA, RegWrite, RegDst, PCSel, ALUCtrl, Op, Zero, cz);
parameter PCSTART = 0; //starting address of instruction memory
input clk;
input reset;
input IorD;
input MemWrite,MemRead,MemtoReg;
input IRWrite;
input PCSource;
input RegDst,RegWrite;
input ALUSrcA;
input [1:0] ALUSrcB;
input PCSel;
input [3:0] ALUCtrl;
output [3:0] Op;
output Zero;
output [1:0] cz;
reg [15:0]PC;
reg [15:0] ALUOut;
reg [15:0] ALUResult;
wire [15:0] OpA;
reg [15:0] OpB;
reg [15:0]A;
reg [15:0]B;
wire [15:0] address;
wire [15:0] MemData;
reg[15:0]mem[255:0];
reg [15:0]Instruction;
reg [15:0]mdr;
wire [15:0] da;//read data 1
wire [15:0] db;//read data 2
reg[15:0]registers[7:0];
initial begin
registers[0] = 16'h0000; // Initialize R0
registers[1] = 16'h0001; // Initialize R1
registers[2] = 16'h0007; // Initialize R2
registers[3] = 16'h0003; // Initialize R3
registers[4] = 16'h0004; // Initialize R4
registers[5] = 16'h0005; // Initialize R5
registers[6] = 16'h0006; // Initialize R6
registers[7] = 16'h0007; // Initialize R7
end
assign cz=Instruction[1:0];
assign Op=Instruction[15:12];
//data and instruction memory
assign address=(IorD)?ALUOut:PC;
initial
$readmemh("/home/kpvivek/verilog/mc_nitc/mem.dat", mem);
always @(posedge clk) begin
if(MemWrite)
mem[address]<=B;
end
assign
MemData =(MemRead)? mem[address]:16'bx;
//PC logic
always@ (posedge clk)begin
if(reset)
PC<=PCSTART;
else
if(PCSel)begin
case (PCSource)
1'b0: PC<=ALUResult;
1'b1: PC<=ALUOut;
endcase
end
end
//instruction register
always @(posedge clk) begin
if (IRWrite)
Instruction <= MemData;
end
//memory data register
always @(posedge clk) begin
mdr <= MemData;
end
//register file
//$r0 is always 0
assign da = (Instruction[11:9]!=0) ? registers[Instruction[11:9]] : 0;
assign db = (Instruction[8:6]!=0) ? registers[Instruction[8:6]] : 0;
always @(posedge clk) begin
if (RegWrite)begin
if (RegDst)
registers[Instruction[5:3]]<=(MemtoReg)?mdr:ALUOut;
else
registers[Instruction[8:6]]<=(MemtoReg)?mdr:ALUOut;
end
end
//A and B registers
always @(posedge clk) begin
A<=da;
end
always@(posedge clk) begin
B<=db;
end
//ALU
assign OpA=(ALUSrcA)?A:PC;
always@(ALUSrcB or B or Instruction[15:0])begin
casex(ALUSrcB)
2'b00:OpB=B;
2'b01:OpB=1;
2'b1x:OpB={{(10){Instruction[5]}},Instruction[5:0]};
endcase
end
assign Zero = (ALUResult==0);//Zero == 1 when ALUResult is 0 (for branch)
//ALU logic is mainly working in here
/*
*/
always @(ALUCtrl or OpA or OpB) begin
case(ALUCtrl)
4'b0000:ALUResult = OpA & OpB;
4'b0001:ALUResult = OpA | OpB;
4'b0010:ALUResult = OpA + OpB;
4'b0110:ALUResult = OpA - OpB;
4'b0111:ALUResult = OpA < OpB?1:0;
4'b1100:ALUResult = ~(OpA | OpB);
endcase
end
//ALUOut register
always@(posedge clk) begin
ALUOut<=ALUResult;
end
endmodule