-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.v
111 lines (75 loc) · 1.59 KB
/
cpu.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
module cpu(
input clk,
input n_reset,
input [7:0] instr,
output [3:0] address,
input [3:0] in,
output [3:0] out
);
wire load0;
wire load1;
wire load2;
wire load3;
reg [3:0] a_reg;
reg [3:0] b_reg;
reg [3:0] out_reg;
reg [3:0] pc_reg;
wire [3:0] op;
wire [3:0] im;
wire select_a;
wire select_b;
wire [3:0] selector_out;
wire [3:0] alu_out;
reg cf_reg;
wire c;
//Aレジスタ
always @(posedge clk) begin
if(n_reset == 1'b0 )
a_reg <= 0;
else
a_reg <= load0 ? alu_out : a_reg;
end
//Bレジスタ
always @(posedge clk) begin
if(n_reset == 1'b0 )
b_reg <= 0;
else
b_reg <= load1 ? alu_out : b_reg;
end
//出力ポート(元Cレジスタ)
always @(posedge clk) begin
if(n_reset == 1'b0 )
out_reg <= 0;
else
out_reg <= load2 ? alu_out : out_reg;
end
assign out = out_reg;
//PC(元Dレジスタ)
always @(posedge clk) begin
if(n_reset == 1'b0 )
pc_reg <= 0;
else
pc_reg <= load3 ? im : pc_reg + 1;
end
assign address = pc_reg;
//デコーダ
assign op = instr[7:4];
assign im = instr[3:0];
assign select_a = op[0] | op[3];
assign select_b = op[1];
assign load0 = !(op[2] | op[3]);
assign load1 = !(!op[2] | op[3]);
assign load2 = !op[2] & op[3];
assign load3 = (!cf_reg | op[0]) & op[2] & op[3];
//データセレクタ
data_selector ds (a_reg, b_reg, in, 4'b0000, select_a, select_b, selector_out);
//ALU
assign {c, alu_out} = selector_out + im;
//Carry Flag
always @(posedge clk) begin
if(n_reset == 1'b0 )
cf_reg <= 0;
else
cf_reg <= c;
end
endmodule