-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpu.v
392 lines (345 loc) · 9.97 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
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
module cpu(
input clk,
input nrst,
// UI Bus
// Panel to external interfaces
//ld : out STD_LOGIC_VECTOR(7 downto 0);
//anodes : out STD_LOGIC_VECTOR(3 downto 0);
//cathodes : out STD_LOGIC_VECTOR(7 downto 0);
// Panel Bus
// Panel to CPU
input [11:0] swreg,
input [1:0] dispsel,
input run,
input loadpc,
input step,
input deposit,
// CPU to Panel
output [11:0] dispout,
output linkout,
output halt,
// IO Bus
// CPU to IOT Distributor
output bit1_cp2,
output bit2_cp3,
output [2:0] io_address,
output [7:0] dataout,
// IOT Distributor to CPU
input skip_flag,
input clearacc,
input [7:0] datain,
// UART Bus
// IOT Distributor to UART
//clear_3 : STD_LOGIC;
//load_3 : STD_LOGIC;
//dataout_3 : STD_LOGIC_VECTOR(7 downto 0);
// UART to IOT Distributor
//ready_3 : STD_LOGIC;
//clearacc_3 : STD_LOGIC;
//datain_3 : STD_LOGIC_VECTOR(7 downto 0);
// Memory Bus
// CPU to RAM
output reg [11:0] address,
output reg [11:0] write_data,
output write_enable,
output mem_load,
// RAM to CPU
input [11:0] read_data,
input mem_ready
);
`define USE_WIRE
`include "control.vh"
`undef USE_WIRE
//type word is std_logic_vector(11 downto 0);
//wire ac : word = 0;
//wire ir : word = 0;
//wire ea : word = 0;
//wire [11:0] ac = 0;
//wire link = 1'b0;
reg [12:0] link_ac = 0;
wire [11:0] ac;
wire link;
reg [11:0] ir = 0;
reg [11:0] ma = 0;
reg [11:0] md = 0;
//wire [11:0] ea = 0;
reg [11:0] ea;
reg [11:0] pc = 0;
reg skip;
localparam integer ZBit = 7;
localparam integer IBit = 8;
// OPR (111) uC bits
localparam integer UcGroup1Bit = 8;
localparam integer UcGroup2Bit = 0;
localparam integer Group1 = 2'd0;
localparam integer Group2 = 2'd1;
localparam integer Group3 = 2'd2;
`define UC_GROUP_T wire [1:0]
`UC_GROUP_T uc_group;
// uC Group 1 bits
localparam integer ClaBit = 7;
localparam integer CllBit = 6;
localparam integer CmaBit = 5;
localparam integer CmlBit = 4;
localparam integer RarBit = 3;
localparam integer RalBit = 2;
localparam integer BswBit = 1;
localparam integer IacBit = 0;
// uC Group 2 bits
//localparam integer ClaBit = 7;
localparam integer SmaBit = 6;
localparam integer SzaBit = 5;
localparam integer SnlBit = 4;
localparam integer AndBit = 3;
localparam integer OsrBit = 2;
localparam integer HltBit = 1;
// uC Group 3 bits
//localparam integer ClaBit = 7;
localparam integer MqaBit = 6;
localparam integer ScaBit = 5;
localparam integer MqlBit = 4;
// 3:1 Code bits
// uC decoded signals
wire en_cla;
wire en_cll;
wire en_cma;
wire en_cml;
reg en_rar;
reg en_ral;
reg en_rtr;
reg en_rtl;
reg en_bsw;
wire en_iac;
wire en_sma;
wire en_sza;
wire en_snl;
wire en_and;
wire [12:0] uc1_stage1;
wire [12:0] uc1_stage2;
wire [12:0] uc1_stage3;
wire [12:0] uc1_stage4;
wire [12:0] uc2_stage1;
wire uc2_skip;
reg [12:0] uc_link_ac;
reg uc_skip;
`sel_ac_t sel_ac;
`sel_pc_t sel_pc;
`sel_skip_t sel_skip;
`sel_addr_t sel_addr;
`sel_data_t sel_data;
`sel_iot_t sel_iot;
`sel_ir_t sel_ir;
`sel_ma_t sel_ma;
`sel_md_t sel_md;
wire md_clear;
wire iot_skip2;
wire [2:0] iot_bits;
wire mem_read, mem_write;
state inst_state(
.clk(clk),
.run(1'b1),
.opcode(ir[11:9]),
.indirect(ir[IBit]),
.sel_ac(sel_ac),
.sel_pc(sel_pc),
.sel_skip(sel_skip),
.sel_addr(sel_addr),
.sel_data(sel_data),
.sel_iot(sel_iot),
.sel_ir(sel_ir),
.sel_ma(sel_ma),
.sel_md(sel_md),
.mem_read(mem_read),
.mem_write(mem_write),
.mem_valid(mem_ready),
.halted(halt)
);
always @* case(sel_addr)
addr_ea: address = ea;
addr_ma: address = ma;
addr_pc: address = pc;
default: address = ea;
endcase
always @* case(sel_data)
data_ac: write_data = ac;
data_md: write_data = md;
//data_pc: write_data = pc;
data_pc1: write_data = pc+1;
default: write_data = ea;
endcase
// Address calculation
//process(posedge clk)
//variable page : std_logic_vector(11 downto 7);
//begin
// if rising_edge(clk) then
// if ir(ZBit) == 1'b1 then
// page := pc(11 downto 7);
// else
// page := (others => 1'b0);
// end if;
// ea <= page & ir(6 downto 0);
// end if;
//end process;
//always @(pc[11:7], ir[ZBit], ir[6:0])
// TODO Is this the best way to emulate a VHDL variable?
reg [11:7] page;
always @*
begin
if(ir[ZBit] == 1'b1)
page = pc[11:7];
else
page = 5'b0;
ea <= {page, ir[6:0]};
end
// Program Counter
always @(posedge clk)
begin
if(sel_pc == pc_data)
pc <= read_data;
else if(sel_pc == pc_ma)
pc <= ma;
else if(sel_pc == pc_ma1)
pc <= ma + 1;
else if(sel_pc == pc_incr) begin
if(skip == 1'b1)
pc <= pc + 12'd2;
else
pc <= pc + 12'd1;
end
end
// Accumulator and Link
always @(posedge clk)
begin
if(sel_ac == ac_and_md)
link_ac <= link_ac & {1'b1, md};
else if(sel_ac == ac_add_md)
link_ac <= link_ac + {1'b0, md};
else if(sel_ac == ac_zero)
link_ac <= {link, 12'b000000000000};
else if(sel_ac == ac_uc)
link_ac <= uc_link_ac;
else if(sel_ac == ac_iot) begin
if(clearacc)
link_ac <= {link, 4'b0000, datain};
else
link_ac <= {link, ac | {4'b0000, datain}};
end
end
// Instruction Register
always @(posedge clk)
begin
if(sel_ir == ir_data)
ir <= read_data;
end
// Memory Address
always @(posedge clk)
begin
if(sel_ma == ma_data)
ma <= read_data;
else if(sel_ma == ma_ea)
ma <= ea;
end
// Memory Data
always @(posedge clk)
begin
if(sel_md == md_data)
md <= read_data;
else if(sel_md == md_data1)
md <= read_data + 1;
end
assign md_clear = (md == 12'b0) ? 1'b1 : 1'b0;
assign iot_skip2 = (skip_flag && ir[0] == 1'b1) ? 1'b1 : 1'b0;
always @* case(sel_skip)
skip_md_clear: skip = md_clear;
skip_uc: skip = uc_skip;
skip_iot: skip = iot_skip2;
default: skip = 1'b0;
endcase
// Decoding OPR instructions
assign uc_group = (ir[UcGroup1Bit] == 1'b0) ? Group1 :
(ir[UcGroup2Bit] == 1'b0) ? Group2 :
Group3;
assign en_cla = ir[ClaBit];
assign en_cll = ir[CllBit];
assign en_cma = ir[CmaBit];
assign en_cml = ir[CmlBit];
assign en_iac = ir[IacBit];
assign en_sma = ir[SmaBit];
assign en_sza = ir[SzaBit];
assign en_snl = ir[SnlBit];
assign en_and = ir[AndBit];
always @(ir[RarBit:BswBit])
begin
en_rar <= 1'b0;
en_rtr <= 1'b0;
en_ral <= 1'b0;
en_rtl <= 1'b0;
en_bsw <= 1'b0;
case(ir[RarBit:BswBit])
3'b100: en_rar <= 1'b1;
3'b101: en_rtr <= 1'b1;
3'b010: en_ral <= 1'b1;
3'b011: en_rtl <= 1'b1;
3'b001: en_bsw <= 1'b1;
default: ;
endcase
end
// uC Group 1 processing
assign uc1_stage1[11:0] = en_cla ? 12'b0 : ac;
assign uc1_stage1[12] = en_cll ? 1'b0 : link;
assign uc1_stage2[11:0] = en_cma ? ~uc1_stage1[11:0] : uc1_stage1[11:0];
assign uc1_stage2[12] = en_cml ? ~uc1_stage1[12] : uc1_stage1[12];
assign uc1_stage3 = en_iac ? uc1_stage2 + 1 : uc1_stage2;
assign uc1_stage4 = en_ral ? {uc1_stage3[11:00], uc1_stage3[12:12]} :
en_rtl ? {uc1_stage3[10:00], uc1_stage3[12:11]} :
en_rar ? {uc1_stage3[00:00], uc1_stage3[12:01]} :
en_rtr ? {uc1_stage3[01:00], uc1_stage3[12:02]} :
en_bsw ? {uc1_stage3[12] , uc1_stage3[5:0], uc1_stage3[11:6]} :
uc1_stage3;
// uC Group 2 processing
assign uc2_stage1[11:0] = en_cla ? 12'b0 : ac;
assign uc2_stage1[12] = link;
assign uc2_skip = ((en_sma && ac[11] == 1'b1) ||
(en_sza && ac == 12'b0) ||
(en_snl && link)) ^
en_and ? 1'b1 : 1'b0;
always @(uc_group, uc1_stage4, uc2_stage1, uc2_skip, link_ac)
begin
case(uc_group)
Group1: begin
uc_link_ac <= uc1_stage4;
uc_skip <= 1'b0;
end
Group2: begin
uc_link_ac <= uc2_stage1;
uc_skip <= uc2_skip;
end
Group3: begin
uc_link_ac <= link_ac;
uc_skip <= 1'b0;
end
default: begin
uc_link_ac <= link_ac;
uc_skip <= 1'b0;
end
endcase
end
assign ac = link_ac[11:0];
assign link = link_ac[12];
// TODO Should all ac bits be supported?
//dataout <= ac;
assign dataout = ac[7:0];
// TODO Support all address bits
//io_address <= ir[8:3] when sel_iot == iot_en else 6'b000000;
assign io_address = (sel_iot == iot_en) ? ir[8:6] : 6'b000000;
assign iot_bits = ir[2:0];
assign bit1_cp2 = iot_bits[1];
assign bit2_cp3 = iot_bits[2];
assign mem_load = mem_write | mem_read;
assign write_enable = mem_write;
endmodule
//10 ... 02 01 00 12 11 RTL
//11 10 ... 02 01 00 12 RAL
//12 11 10 ... 02 01 00
//00 12 11 10 ... 02 01 RAR
//01 00 12 11 10 ... 02 RTR