§ Ядро процессора
Процессор, основанный на анализе и повторе js-эмулятора
Gigatron.
1module gigatron
2(
3 input wire clock,
4 input wire rst_n,
5 output reg [15:0] pc,
6 input wire [15:0] rom_i,
7
8
9 output reg [15:0] addr_r,
10 output reg [15:0] addr_w,
11 input wire [ 7:0] data_i,
12 output reg [ 7:0] data_o,
13 output reg we,
14
15
16 input wire [ 7:0] inreg,
17 output reg [ 7:0] out,
18 output reg [ 7:0] outx,
19
20
21
22
23
24
25
26
27
28
29
30 output reg [ 7:0] ctrl
31);
32
33
34reg [ 7:0] ac = 0;
35reg [ 7:0] x = 0;
36reg [ 7:0] y = 0;
37reg [15:0] ir = 16'h0200;
38
39initial begin
40
41 pc = 0;
42 we = 0;
43 addr_r = 0;
44 addr_w = 0;
45 data_o = 0;
46 out = 0;
47 outx = 0;
48 ctrl = 0;
49
50end
51
52
53
54wire [ 2:0] op = ir[15:13];
55wire [ 2:0] mode = ir[12:10];
56wire [ 1:0] bus = ir[ 9:8];
57wire [ 7:0] d = ir[ 7:0];
58wire [ 7:0] zac = {~ac[7], ac[6:0]};
59wire [15:0] pcinc = pc + 1;
60
61
62
63reg [ 7:0] b;
64reg [ 7:0] alu;
65reg [ 7:0] base;
66reg cond;
67
68
69
70always @* begin
71
72 base = pc[15:8];
73
74
75 if (op == 7) addr_r = d;
76 else case (mode)
77
78 0, 4, 5, 6:
79 addr_r = d;
80 1: addr_r = x;
81 2: addr_r = {y, d};
82 3, 7: addr_r = {y, x};
83
84 endcase
85
86
87 case (bus)
88
89 2'b00: b = d;
90 2'b01: b = data_i;
91 2'b10: b = ac;
92 2'b11: b = inreg;
93
94 endcase
95
96
97 case (op)
98
99 1: alu = ac & b;
100 2: alu = ac | b;
101 3: alu = ac ^ b;
102 4: alu = ac + b;
103 5: alu = ac - b;
104 default: alu = b;
105
106 endcase
107
108
109 case (mode)
110
111 0: begin cond = 1; base = y; end
112 1: begin cond = zac > 8'h80; end
113 2: begin cond = zac < 8'h80; end
114 3: begin cond = zac != 8'h80; end
115 4: begin cond = zac == 8'h80; end
116 5: begin cond = zac >= 8'h80; end
117 6: begin cond = zac <= 8'h80; end
118 7: begin cond = 1; end
119
120 endcase
121
122end
123
124
125
126always @(posedge clock) begin
127
128 ir <= rom_i;
129 pc <= pcinc;
130 we <= 0;
131
132 case (op)
133
134
135 6: begin
136
137 addr_w <= addr_r;
138 data_o <= b;
139 we <= (bus != 1);
140
141 case (mode)
142
143 4: x <= b;
144 5: y <= b;
145 7: x <= x + 1;
146
147 endcase
148
149
150 if (bus == 1) ctrl <= d;
151
152 end
153
154
155 7: begin if (cond) pc <= {base, b}; end
156
157
158 default: case (mode)
159
160 0, 1, 2, 3: ac <= alu;
161 4: x <= alu;
162 5: y <= alu;
163 6, 7:
164 begin
165
166
167 if (mode == 7 && bus == 1) x <= x + 1;
168
169
170 if (!out[6] && alu[6]) outx <= ac;
171
172
173 out <= alu;
174
175 end
176
177 endcase
178
179 endcase
180
181end
182
183endmodule