-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.v
277 lines (261 loc) · 8.85 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
/*
* b16 core: 16 bits,
* inspired by c18 core from Chuck Moore
* (c) 2002-2011 by Bernd Paysan
*
* 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; version 2 of the License or any later.
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.
This is not the source code of the program, the source code is a LyX
literate programming style article.
*/
/*
* Instruction set:
* 1, 5, 5, 5 bits
* 0 1 2 3 4 5 6 7
* 0: nop call jmp ret jz jnz jc jnc
* /3 exec goto ret gz gnz gc gnc
* 8: xor com and or + +c *+ /-
* 10: !+ @+ @ lit c!+ c@+ c@ litc
* /1 !. @. @ lit c!. c@. c@ litc
* 18: nip drop over dup >r r>
*/
`include "b16-defines.v"
module cpu(clk, latclk, run, nreset, addr, rd, wr, data,
dataout, gwrite
`ifdef DEBUGGING,
dr, dw, daddr, din, dout, bp`endif);
parameter rstaddr=16'h3FFE, show=0,
l=16, sdep=4, rdep=4;
input clk, latclk, run, nreset, gwrite;
output `L addr;
output rd;
output [1:0] wr;
input `L data;
output `L dataout;
`ifdef DEBUGGING
input [2:0] daddr;
input dr, dw;
input `L din, bp;
output `L dout;
`endif
reg [sdep-1:0] sp;
reg [rdep-1:0] rp;
reg `L T, I, P, R;
reg [1:0] state;
reg c;
// instruction and branch target selection
wire [4:0] inst, rwinst;
reg `L jmp;
assign inst = { 4'b0000, data[15], I[14:0] }
>> (5*(3-state[1:0]));
assign rwinst = { 5'b00000, I[14:0] }
>> (5*(3-state[1:0]));
always @(state or I or P or T or data)
case(state[1:0])
2'b00: jmp = { data[14:0], 1'b0 };
2'b01: jmp = { P[15:11], I[9:0], 1'b0 };
2'b10: jmp = { P[15:6], I[4:0], 1'b0 };
2'b11: jmp = { T[15:1], 1'b0 };
endcase // casez(state)
wire `L res, toN, toR, N;
wire carry, zero;
alu #(l) alu16(.res(res), .carry(carry),
.zero(zero),
.T(T), .N(N), .c(c),
.inst(inst[2:0]));
wire `L incaddr, dataw, datas;
wire tos2n;
wire incby, bswap, addrsel, access, rd;
wire [1:0] wr;
assign incby = (rwinst[4:2] != 3'b101);
assign access = (rwinst[4:3]==2'b10);
assign addrsel = rd ?
(access & (rwinst[1:0] != 2'b11)) : |wr;
assign rd = (state==2'b00) ||
(access && (rwinst[1:0]!=2'b00));
assign wr = (access && (rwinst[1:0]==2'b00)) ?
{ ~rwinst[2] | ~T[0],
~rwinst[2] | T[0] } : 2'b00;
assign addr = addrsel ? T : P;
assign incaddr = addr + incby + 1;
assign tos2n = (!rd | (rwinst[1:0] == 2'b11));
assign toN = tos2n ? T : dataw;
assign bswap = ~incby ^ addr[0];
assign datas = bswap ? { data[7:0], data[l-1:8] }
: data;
assign dataw = incby ? datas
: { 8'h00, datas[7:0] };
assign dataout = bswap ? { N[7:0], N[l-1:8] }
: N;
reg dpush, rpush;
always @(state or inst or rd or run `ifdef DEBUGGING
or run or dw or daddr
`endif)
begin
rpush = 1'b0;
dpush = (|state[1:0] & rd) |
(inst[4] && inst[3] && inst[1]);
case(inst)
5'b00001: rpush = |state[1:0] | run;
5'b11100: rpush = 1'b1;
default ;
endcase // case(inst)
`ifdef DEBUGGING
if(!run && dw) case(daddr)
3'h0: dpush = 1;
3'h1: rpush = 1;
default ;
endcase
`endif
end
wire [sdep-1:0] spdec, spinc;
wire [rdep-1:0] rpdec, rpinc;
stack #(sdep,l) dstack(.clk(latclk),
.sp(sp),
.spdec(spdec),
.push(dpush),
.in(toN),
.out(N),
.gwrite(gwrite));
stack #(rdep,l) rstack(.clk(latclk),
.sp(rp),
.spdec(rpdec),
.push(rpush),
.in(R),
.out(toR),
.gwrite(gwrite));
assign spdec = sp-{{(sdep-1){1'b0}}, 1'b1};
assign spinc = sp+{{(sdep-1){1'b0}}, 1'b1};
assign rpdec = rp-{{(rdep-1){1'b0}}, 1'b1};
assign rpinc = rp+{{(rdep-1){1'b0}}, 1'b1};
wire [1:0] nextstate;
assign nextstate = ((~|inst) || (|inst[4:3])) ?
state[1:0] + 2'b01 : 2'b00;
`ifdef DEBUGGING
reg `L dout;
always @(daddr or dr or run or P or T or R or I or
state or sp or rp or c or N or toR or bp)
if(!dr || run) dout = 'h0;
else case(daddr)
3'h0: dout = N;
3'h1: dout = toR;
3'h2: dout = bp;
3'h3: dout = { run, 4'h0, c, state,
{4-sdep{1'b0}}, sp,
{4-rdep{1'b0}}, rp };
3'h4: dout = P;
3'h5: dout = T;
3'h6: dout = R;
3'h7: dout = I;
endcase
`endif
always @(posedge clk or negedge nreset)
if(!nreset) begin
state <= 2'b11;
P <= rstaddr;
T <= 16'h0000;
I <= 16'h0000;
R <= 16'h0000;
c <= 1'b0;
sp <= 0;
rp <= 0;
end else if(run) begin
`ifdef REPORT_VERBOSE
if(show) begin
$write("%b[%b] T=%b%x:%x[%x], ",
inst, state, c, T, N, sp);
$write("P=%x, I=%x, R=%x[%x], res=%b%x\n",
P, I, R, rp, carry, res);
end
`endif
if(~|state ||
({ inst[4:3], inst[1:0] } == 4'b1011))
P <= incaddr;
if(|state[1:0]) begin
if(rd && { inst[4:3], inst[1:0] } != 4'b1010)
sp <= spdec;
if(|wr) sp <= spinc;
end else begin
I <= data;
if(!data[15]) state[1:0] <= 2'b01;
end
state <= nextstate;
case(inst)
5'b00001: begin // call
rp <= rpdec;
R <= { ~|state ? incaddr[15:1] : P[15:1], c };
P <= jmp;
c <= 1'b0;
if(state == 2'b11) `DROP;
end // case: 5'b00001
5'b00010: begin // jmp
P <= jmp;
if(state == 2'b11) `DROP;
end
5'b00011: // ret
{ rp, c, P, R } <=
{ rpinc, R[0], R[l-1:1], 1'b0, toR };
5'b00100, 5'b00101, 5'b00110, 5'b00111:
begin // conditional jmps
if((inst[1] ? c : zero) ^ inst[0])
P <= jmp;
`DROP;
end
5'b01001: // com
{ c, T } <= { 1'b1, ~T };
5'b01110: // *+
{ T, R, c } <=
{ c ? { carry, res } : { 1'b0, T }, R };
5'b01111: // /-
{ c, T, R } <=
{ (c | carry) ? res : T, R, (c | carry) };
5'b01000, 5'b01010, 5'b01011, 5'b01100, 5'b01101:
// xor, and, or, +, +c
{ sp, c, T } <= { spinc, carry, res };
5'b10000, 5'b10001, 5'b10100, 5'b10101:
begin // !+, @+, c!+, c@+
if(nextstate != 2'b10) T <= incaddr;
sp <= rd ? spdec : spinc;
end
5'b10010, 5'b10011, 5'b10110, 5'b10111:
T <= dataw; // @, lit, c@, litc
5'b11000: sp <= spinc; // nip
5'b11001: `DROP; // drop
5'b11010: { sp, T } <= { spdec, N }; // over
5'b11011: sp <= spdec; // dup
5'b11100: begin // >r
R <= T; rp <= rpdec; `DROP;
end // case: 5'b11100
5'b11110: begin // r>
{ sp, T, R } <= { spdec, R, toR };
rp <= rpinc;
end // case: 5'b11110
default ; // noop
endcase // case(inst)
end else begin // debug
`ifdef DEBUGGING
if(dw) case(daddr)
3'h0: { sp, T } <= { spdec, din };
3'h1: { rp, R } <= { rpdec, din };
3'h3: { c, state, sp, rp } <=
{ din[10:8],
din[sdep+3:4], din[rdep-1:0] };
3'h4: P <= din;
3'h5: T <= din;
3'h6: R <= din;
3'h7: I <= din;
default ;
endcase
if(dr) case(daddr)
3'h0: sp <= spinc;
3'h1: rp <= rpinc;
default ;
endcase
`endif
end // else: !if(nreset)
endmodule // cpu