-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.v
162 lines (128 loc) · 4.16 KB
/
processor.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
module processor;
reg [31:0] pc; //32-bit prograom counter
reg clk; //clock
reg [7:0] datmem[0:31],mem[0:31]; //32-size data and instruction memory (8 bit(1 byte) for each location)
reg statusn;
reg statusz;
wire [31:0]
dataa, //Read data 1 output of Register File
datab, //Read data 2 output of Register File
out2, //Output of mux with ALUSrc control-mult2
out3, //Output of mux with MemToReg control-mult3
out4, //Output of mux with (Branch&ALUZero) control-mult4
sum, //ALU result
extad, //Output of sign-extend unit
adder1out, //Output of adder which adds PC and 4-add1
adder2out, //Output of adder which adds PC+4 and 2 shifted sign-extend result-add2
sextad; //Output of shift left 2 unit
wire [5:0] inst31_26; //31-26 bits of instruction
wire [4:0]
inst25_21, //25-21 bits of instruction
inst20_16, //20-16 bits of instruction
inst15_11, //15-11 bits of instruction
out1; //Write data input of Register File
wire [15:0] inst15_0; //15-0 bits of instruction
wire [31:0] instruc, //current instruction
dpack; //Read data output of memory (data read from memory)
wire [2:0] gout; //Output of ALU control unit
wire zout, //Zero output of ALU
nout,
brnout,
pcsrc, //Output of AND gate with Branch and ZeroOut inputs
//Control signals
regdest,alusrc,memtoreg,regwrite,memread,memwrite,branch,aluop1,aluop0;
//32-size register file (32 bit(1 word) for each register)
reg [31:0] registerfile[0:31];
integer i;
// datamemory connections
always @(posedge clk)
//write data to memory
if (memwrite)
begin
//sum stores address,datab stores the value to be written
datmem[sum[4:0]+3]=datab[7:0];
datmem[sum[4:0]+2]=datab[15:8];
datmem[sum[4:0]+1]=datab[23:16];
datmem[sum[4:0]]=datab[31:24];
end
//instruction memory
//4-byte instruction
assign instruc={mem[pc[4:0]],mem[pc[4:0]+1],mem[pc[4:0]+2],mem[pc[4:0]+3]};
assign inst31_26=instruc[31:26];
assign inst25_21=instruc[25:21];
assign inst20_16=instruc[20:16];
assign inst15_11=instruc[15:11];
assign inst15_0=instruc[15:0];
// registers
assign dataa=registerfile[inst25_21];//Read register 1
assign datab=registerfile[inst20_16];//Read register 2
always @(posedge clk)
begin
registerfile[out1]= (regwrite & ~brnout) ? out3:registerfile[out1];//Write data to register
end
//read data from memory, sum stores address
assign dpack={datmem[sum[5:0]],datmem[sum[5:0]+1],datmem[sum[5:0]+2],datmem[sum[5:0]+3]};
//multiplexers
//mux with RegDst control
mult2_to_1_5 mult1(out1, instruc[20:16],instruc[15:11],regdest);
//mux with ALUSrc control
mult2_to_1_32 mult2(out2, datab,extad,alusrc);
//mux with MemToReg control
mult2_to_1_32 mult3(out3, sum,dpack,memtoreg);
//mux with (Branch&ALUZero) control
mult2_to_1_32 mult4(out4, adder1out,adder2out,pcsrc);
// load pc
always @(negedge clk)
begin
pc=out4;
if (brnout & statusn)
pc=sum;
statusn= nout;
statusz= zout;
end
// alu, adder and control logic connections
//ALU unit
alu32 alu1(sum,dataa,out2,zout,nout,gout);
//adder which adds PC and 4
adder add1(pc,32'h4,adder1out);
//adder which adds PC+4 and 2 shifted sign-extend result
adder add2(adder1out,sextad,adder2out);
//Control unit
control cont(instruc[31:26],regdest,alusrc,memtoreg,regwrite,memread,memwrite,branch,
aluop1,aluop0);
//Sign extend unit
signext sext(instruc[15:0],extad);
//ALU control unit
alucont acont(aluop1,aluop0,instruc[5],instruc[4], instruc[3],instruc[2], instruc[1], instruc[0] ,gout,brnout);
//Shift-left 2 unit
shift shift2(sextad,extad);
//AND gate
assign pcsrc=branch && zout;
//initialize datamemory,instruction memory and registers
//read initial data from files given in hex
initial
begin
$readmemh("initDm.dat",datmem); //read Data Memory
$readmemh("initIM.dat",mem);//read Instruction Memory
$readmemh("initReg.dat",registerfile);//read Register File
for(i=0; i<31; i=i+1)
$display("Instruction Memory[%0d]= %h ",i,mem[i],"Data Memory[%0d]= %h ",i,datmem[i],
"Register[%0d]= %h",i,registerfile[i]);
end
initial
begin
pc=0;
#400 $finish;
end
initial
begin
clk=0;
//40 time unit for each cycle
forever #20 clk=~clk;
end
initial
begin
$monitor($time,"PC %h",pc," SUM %h",sum," INST %h",instruc[31:0],
" REGISTER %h %h %h %h ",registerfile[4],registerfile[5], registerfile[6],registerfile[1] );
end
endmodule