-
Notifications
You must be signed in to change notification settings - Fork 0
/
The Complete 8 Bit Processor.v
413 lines (248 loc) · 7.78 KB
/
The Complete 8 Bit 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
module andg(out , a , b);
input a ,b;
output reg out;
always @ (*)
begin
out <= a&b;
end
endmodule
module pro_counter(in,clk, reset, out);
input[7:0] in;
input clk;
input reset;
output reg [7:0] out;
always @(posedge clk )
begin
if(reset) out<= 8'b00000000;
else out<=in;
end
endmodule
module pc_incrmntr(pc,sum);
input [7:0] pc;
output reg [7:0] sum;
always @ (pc)
begin
sum <= pc+1;
end
endmodule
module alu(
input [7:0] a, //src1
input [7:0] b, //src2
input [2:0] alu_ctrl_in, //function sel
output reg [7:0] result, //result
output zero
);
always @(*)
begin
case(alu_ctrl_in)
3'b000: result = a + b; // add
3'b010: result = a - b; // sub
3'b100: result = a & b; // and
3'b101: result = a | b; // or
default:result = a + b; // add
endcase
end
assign zero = (result==8'd0) ? 1'b1: 1'b0;
endmodule
module alu_control(opcode , func , out);
input [3:0] opcode;
input [2:0] func;
output reg [2:0] out;
always @ (*)
begin
if(opcode == 4'b0000)
out <= func;
else if(opcode == 4'b1000)
out <= 3'b010;
else
out <= 3'b000;
end
endmodule
module CONTROL_UNIT
(
input [3:0] opcode ,
output reg reg_sel ,
output reg alu_src ,
output reg reg_w_enbl ,
output reg br ,
output reg jump ,
output reg m_r_enbl ,
output reg m_w_enbl ,
output reg mem_mux ,
output reg [3:0] to_alu_ctrl
);
always @(opcode)
begin
to_alu_ctrl <= opcode;
if(opcode == 4'b0000)
begin
reg_sel <= 1'b1; alu_src <= 1'b0; reg_w_enbl<= 1'b1; br <= 1'b0;
jump <= 1'b0; m_r_enbl <= 1'b0; m_w_enbl <= 1'b0; mem_mux <= 1'b0;
end
else if(opcode == 4'b0100)
begin
reg_sel <= 1'b1; alu_src <= 1'b1; reg_w_enbl<= 1'b1; br <= 1'b0;
jump <= 1'b0; m_r_enbl <= 1'b0; m_w_enbl <= 1'b0; mem_mux <= 1'b0;
end
else if(opcode == 4'b1011)
begin
reg_sel <= 1'b1; alu_src <= 1'b1; reg_w_enbl<= 1'b1; br <= 1'b0;
jump <= 1'b0; m_r_enbl <= 1'b1; m_w_enbl <= 1'b0; mem_mux <= 1'b1;
end
else if(opcode == 4'b1111)
begin
reg_sel <= 1'b0; alu_src <= 1'b1; reg_w_enbl<= 1'b0; br <= 1'b0;
jump <= 1'b0; m_r_enbl <= 1'b0; m_w_enbl <= 1'b1; mem_mux <= 1'b0;
end
else if(opcode == 4'b1000)
begin
reg_sel <= 1'b0; alu_src <= 1'b0; reg_w_enbl<= 1'b0; br <= 1'b1;
jump <= 1'b0; m_r_enbl <= 1'b0; m_w_enbl <= 1'b0; mem_mux <= 1'b0;
end
else if(opcode == 4'b0010)
begin
reg_sel <= 1'b1; alu_src <= 1'b0; reg_w_enbl<= 1'b0; br <= 1'b0;
jump <= 1'b1; m_r_enbl <= 1'b0; m_w_enbl <= 1'b0; mem_mux <= 1'b0;
end
else
begin
reg_sel <= 1'b0; alu_src <= 1'b0; reg_w_enbl<= 1'b0; br <= 1'b0;
jump <= 1'b0; m_r_enbl <= 1'b0; m_w_enbl <= 1'b0; mem_mux <= 1'b0;
end
end
endmodule
module dmem(input[7:0] addr,input[7:0] in,input read,input write,input clk,output[7:0] out);
reg[7:0] out;
reg[7:0] DATA[255:0];
always @(posedge clk)
begin
#1;
if(read==1)
begin
out<=DATA[addr];
end
else if(write==1) DATA[addr]<=in;
end
endmodule
module imem (input [7:0] Addr , output [15:0] rd);
reg [15:0] RAM[256:0];
initial
begin
RAM[8'b00000000]=16'b0100110000000101;
RAM[8'b00000001]=16'b0100111000001010;
RAM[8'b00000010]=16'b1111110000000000;
RAM[8'b00000011]=16'b1111111000000001;
RAM[8'b00000100]=16'b1011001000000000;
RAM[8'b00000101]=16'b1011010000000001;
RAM[8'b00000110]=16'b0000011011010000;
RAM[8'b00000111]=16'b0100001001111111;
RAM[8'b00001000]=16'b1000001000001010;
RAM[8'b00001001]=16'b0010000000000110;
end
assign rd = RAM[Addr];
endmodule
module mux_3_bit(a,b,sel,out);
input [2:0] a;
input [2:0] b;
input sel;
output reg [2:0]out;
always @(*)
begin
out <= sel ? a : b ;
end
endmodule
module mux_8_bit(a,b,sel,out);
input [7:0] a;
input [7:0] b;
input sel;
output reg [7:0]out;
always @(*)
out <= sel ? a : b ;
endmodule
module reg_file
(
input clk,
input rst,
// write port
input reg_write_en,
input [2:0] reg_write_dest,
input [7:0] reg_write_data,
//read port 1
input [2:0] reg_read_addr_1,
output [7:0] reg_read_data_1,
//read port 2
input [2:0] reg_read_addr_2,
output [7:0] reg_read_data_2
);
reg [7:0] reg_array [7:0];
always @ (posedge clk)
if(rst) begin
reg_array[0] <= 8'b00000000;
reg_array[1] <= 8'b00000000;
reg_array[2] <= 8'b00000000;
reg_array[3] <= 8'b00000000;
reg_array[4] <= 8'b00000000;
reg_array[5] <= 8'b00000000;
reg_array[6] <= 8'b00000000;
reg_array[7] <= 8'b00000000;
end
always @(negedge clk)
begin
if(reg_write_en)
begin
reg_array[reg_write_dest] <= reg_write_data;
end
end
assign reg_read_data_1 = ( reg_read_addr_1 == 0)? 8'b0 : reg_array[reg_read_addr_1];
assign reg_read_data_2 = ( reg_read_addr_2 == 0)? 8'b0 : reg_array[reg_read_addr_2];
endmodule
module sign_ext(a,out);
input [5:0] a;
output reg [7:0] out;
always @ (*)
begin
if(a[5]==1'b0)
out <= {2'b00,a};
else
out <= {2'b11,a};
end
endmodule
module cpu();
reg clk = 1'b0;
reg pc_rst , reg_rst;
wire [7:0] pc_in , pc_out , inc_pc;
wire reg_sel , alu_src , reg_w_enbl , br , jump , m_r_enbl , m_w_enbl , mem_mux_sel ;
wire [3:0] to_alu_ctrl;
wire [15:0]inst;
wire [2:0] mx_t_reg , alu_ctrl_t_alu;
wire [7:0] reg_w_data , reg_a_data , reg_b_data , alu_out , mem_out;
wire [7:0] ext_imm , mx_t_alu , imm_mux_out;
wire zero , and_t_mx;
pro_counter PC(pc_in , clk , pc_rst , pc_out);
imem I_MEM(pc_out , inst );
CONTROL_UNIT CU(inst[15:12] ,reg_sel ,alu_src ,reg_w_enbl ,br,jump , m_r_enbl , m_w_enbl , mem_mux_sel , to_alu_ctrl);
mux_3_bit REG_MUX(inst[5:3] , inst[11:9] , reg_sel , mx_t_reg);
reg_file REG_FILE(clk , reg_rst , reg_w_enbl , inst[11:9] , reg_w_data , inst[8:6] , reg_a_data , mx_t_reg , reg_b_data );
sign_ext SIGNEXT(inst[5:0] , ext_imm);
mux_8_bit ALU_MUX(ext_imm , reg_b_data , alu_src , mx_t_alu);
alu_control ALU_CTRL(to_alu_ctrl , inst[2:0] , alu_ctrl_t_alu );
alu ALU(reg_a_data , mx_t_alu , alu_ctrl_t_alu , alu_out , zero);
dmem D_MEM(alu_out , reg_b_data , m_r_enbl , m_w_enbl , clk , mem_out);
mux_8_bit MEM_MUX(mem_out , alu_out , mem_mux_sel ,reg_w_data );
pc_incrmntr PC_INCR(pc_out , inc_pc);
andg ANDG(and_t_mx ,zero ,br);
mux_8_bit IMM_MUX( ext_imm , inc_pc , and_t_mx , imm_mux_out);
mux_8_bit JUM_MUX( inst[7:0] , imm_mux_out , jump , pc_in);
always #5 clk = ~clk;
initial
begin
pc_rst = 1;
reg_rst = 1;
$monitor($time , " reg _1 %d reg_2 = %d reg_3 = %d data[0] = %d data[0] = %d", REG_FILE.reg_array[1] , REG_FILE.reg_array[2] , REG_FILE.reg_array[3] , D_MEM.DATA[0] , D_MEM.DATA[1]);
#6;
pc_rst = 0;
reg_rst = 0;
#1000;
$finish;
end
endmodule