-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrca110_cpu.v
More file actions
294 lines (256 loc) · 6.45 KB
/
Copy pathrca110_cpu.v
File metadata and controls
294 lines (256 loc) · 6.45 KB
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
//
// Copyright (c) 2015 Jan Adelsbach <jan@janadelsbach.com>.
// All Rights Reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
`include "rca110_defs.v"
module rca110_memory(i_clk, i_rst, mm_adr, mm_we, mm_idat, mm_odat);
input i_clk;
input i_rst;
input [11:0] mm_adr;
input mm_we;
output reg [23:0] mm_idat;
input [23:0] mm_odat;
reg [24:0] m_memory [4095:0];
integer i;
always @(posedge i_clk) begin
if(i_rst) begin
mm_idat <= 0;
for(i = 0; i < 4095; i = i + 1)
m_memory[i] = 0;
m_memory[64] = {9'b011010000, 3'b010, 12'b000000000010}; // STP
m_memory[65] = {9'o400, 15'h0};
m_memory[66] = {12'b0, 12'b100000000000};
end
else begin
if(mm_we)
m_memory[mm_adr] <= mm_odat;
else
mm_idat <= m_memory[mm_adr];
end
end
endmodule // rca110_memory
module rca110(i_clk, i_rst,
mm_adr, mm_we, mm_idat, mm_odat);
input i_clk;
input i_rst;
output reg [11:0] mm_adr;
output reg mm_we;
input [23:0] mm_idat;
output reg [23:0] mm_odat;
// Instruction decode
reg [23:0] r_inst;
wire [8:0] w_inst_OP;
wire [2:0] w_inst_T;
wire [10:0] w_inst_Y;
assign w_inst_OP = r_inst[23:15];
assign w_inst_T = r_inst[14:12];
assign w_inst_Y = r_inst[11:0];
// Registers
reg [23:0] r_L;
reg [23:0] r_R;
reg [11:0] r_PC;
reg [7:0] r_JR;
reg [7:0] r_JS;
reg [11:0] r_XR [6:0];
integer i;
wire w_do_Y_offset;
reg [11:0] w_Y_wrap;
// assign w_Y_wrap = (|w_inst_T & w_do_Y_offset) ?
// w_inst_Y - m_XR[w_inst_T-1] : w_inst_Y;
assign w_do_Y_offset = (w_inst_OP == 9'o400) ? 1'b0 : 1'b1;
wire [23:0] w_store1_data;
assign w_store1_data = (w_inst_OP == `RCA110_OP_STZ) ? 24'h00 :
(w_inst_OP == `RCA110_OP_STL) ? r_L :
(w_inst_OP == `RCA110_OP_STR) ? r_R : 24'h00;
reg [2:0] r_state;
reg [2:0] r_state_next;
localparam SFETCH1 = 3'b000; // Fetch I
localparam SFETCH2 = 3'b001; // Decode
localparam SEXEC1 = 3'b010; // Execute single operations
localparam SEXEC2 = 3'b011; // Memory Writeback
// Index register update
always @(posedge i_clk) begin
if(i_rst) begin
for(i = 0; i < 7; i = i + 1)
r_XR[i] <= 0;
end
else begin
if(mm_we && (mm_adr >= 12'o0001 && mm_adr <= 12'o0007)) begin
$display("r_XR[%d] <= %h", mm_adr, mm_odat);
r_XR[mm_adr[2:0] - 1] = mm_odat;
end
end
end
always @(posedge i_clk) begin
if(i_rst) begin
mm_adr <= 0;
mm_we <= 0;
mm_odat <= 0;
r_L <= 0;
r_R <= 0;
r_PC <= 64;
r_JR <= 0;
r_JS <= 0;
r_state <= SFETCH1;
r_state_next <= SFETCH1;
r_inst <= 0;
end
else begin
case(r_state)
SFETCH1:
begin
mm_adr <= r_PC;
mm_we <= 0;
mm_odat <= 0;
r_state <= SFETCH2;
r_PC = r_PC+1;
end
SFETCH2:
begin
r_inst <= mm_idat;
r_state <= SEXEC1;
if(|mm_idat[14:12])
w_Y_wrap <= mm_idat[11:0] - r_XR[mm_idat[14:12]-1];
else
w_Y_wrap <= mm_idat[11:0];
end
SEXEC1:
begin
r_state = SFETCH1;
case(w_inst_OP)
// Load & Store
`RCA110_OP_STP:
begin
mm_adr <= w_Y_wrap;
mm_we <= 1'b1;
mm_odat[11:0] <= ~(r_PC-1);
mm_odat[23:12] <= 0;
end
`RCA110_OP_LDZ:
begin
r_L <= 0;
end
`RCA110_OP_LDL,
`RCA110_OP_LDR,
`RCA110_OP_LDB,
`RCA110_OP_ADD,
`RCA110_OP_LDA:
begin
mm_adr <= w_Y_wrap;
mm_we <= 0;
r_state = SEXEC2;
r_state_next = SEXEC2; // Used as flag
end
`RCA110_OP_STZ,
`RCA110_OP_STL,
`RCA110_OP_STR:
begin
mm_adr <= w_Y_wrap;
mm_odat <= w_store1_data;
mm_we <= 1;
r_state <= SFETCH1;
end
`RCA110_OP_STB:
begin
mm_adr <= w_Y_wrap;
mm_odat <= r_R;
mm_we <= 1;
r_state <= SEXEC2;
end
`RCA110_OP_STA:
// First fetch than save since it only affects bit 11:0
begin
mm_adr <= w_Y_wrap;
r_state <= SEXEC2;
end
endcase // case (w_inst_OP)
end // case: SEXEC1
SEXEC2:
begin
case(w_inst_OP)
`RCA110_OP_LDL:
begin
r_L <= mm_idat;
end
`RCA110_OP_LDR:
begin
r_R <= mm_idat;
end
`RCA110_OP_LDB:
begin
if(r_state_next == SEXEC2) begin
r_R <= mm_idat;
mm_adr <= w_Y_wrap+1;
r_state_next <= SFETCH1;
end
else begin
r_L <= mm_idat;
r_state <= SFETCH1;
end
end // case: `RCA110_OP_LDB
`RCA110_OP_LDA:
begin
r_L[11:0] <= mm_idat[11:0];
r_state <= SFETCH1;
end
`RCA110_OP_STB:
begin
mm_adr <= w_Y_wrap+1;
mm_odat <= r_L;
r_state <= SFETCH1;
end
`RCA110_OP_STA:
begin
mm_adr <= w_Y_wrap;
mm_odat[23:12] <= mm_idat[23:12];
mm_odat[11:0] <= r_L[11:0];
mm_we <= 1;
end
`RCA110_OP_ADD,
`RCA110_OP_ADR:
begin
r_L = r_L + mm_idat;
mm_odat <= r_L + mm_idat;
mm_we <= 1'b1;
mm_adr <= w_Y_wrap;
end
endcase // case (w_inst_OP)
end
endcase // case (r_state)
end
end
endmodule // rca110
module tb;
reg clk = 0;
reg rst = 1;
wire [11:0] mm_adr;
wire mm_we;
wire [23:0] mm_idat;
wire [23:0] mm_odat;
rca110_memory mem(clk, rst, mm_adr, mm_we, mm_idat, mm_odat);
rca110 cpu(clk, rst,
mm_adr, mm_we, mm_idat, mm_odat);
always #10 clk = ~clk;
always @(posedge clk) begin
rst <= 0;
end
initial begin
$dumpfile("rca110.vcd");
$dumpvars(0, mem);
$dumpvars(0, cpu);
#500 $finish();
end
endmodule // tb