-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaturn_regs_pc_rstk.v
285 lines (234 loc) · 8.99 KB
/
saturn_regs_pc_rstk.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
/*
(c) Raphaël Jacquot 2019
This file is part of hp_saturn.
hp_saturn 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
any later version.
hp_saturn 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 Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
`default_nettype none
module saturn_regs_pc_rstk (
i_clk,
i_clk_en,
i_reset,
i_phases,
i_phase,
i_cycle_ctr,
i_bus_busy,
i_exec_unit_busy,
i_nibble,
i_jump_instr,
i_jump_length,
i_block_0x,
i_push_pc,
i_test_jump,
o_current_pc,
o_reload_pc,
/* debugger access */
i_dbg_rstk_ptr,
o_dbg_rstk_val,
o_reg_rstk_ptr
);
input wire [0:0] i_clk;
input wire [0:0] i_clk_en;
input wire [0:0] i_reset;
input wire [3:0] i_phases;
// Verilator lint_off UNUSED
wire [0:0] unused = { i_phases[0] };
// Verilator lint_on UNUSED
input wire [1:0] i_phase;
input wire [31:0] i_cycle_ctr;
input wire [0:0] i_bus_busy;
input wire [0:0] i_exec_unit_busy;
input wire [3:0] i_nibble;
input wire [0:0] i_jump_instr;
input wire [2:0] i_jump_length;
input wire [0:0] i_block_0x;
input wire [0:0] i_push_pc;
input wire [0:0] i_test_jump;
output wire [19:0] o_current_pc;
output reg [0:0] o_reload_pc;
input wire [2:0] i_dbg_rstk_ptr;
output wire [19:0] o_dbg_rstk_val;
output wire [2:0] o_reg_rstk_ptr;
assign o_dbg_rstk_val = reg_RSTK[i_dbg_rstk_ptr];
assign o_reg_rstk_ptr = reg_rstk_ptr;
/**************************************************************************************************
*
* pc and rstk handling module
*
*************************************************************************************************/
wire [0:0] do_jump_instr = !just_reset && (i_jump_instr || i_test_jump) ;
/*
* local variables
*/
reg [0:0] just_reset;
reg [2:0] init_counter;
reg [0:0] jump_decode;
reg [2:0] jump_counter;
reg [19:0] jump_base;
reg [15:0] jump_offset;
wire [0:0] jump_rel2 = i_jump_instr && (i_jump_length == 3'd1);
wire [0:0] jump_rel3 = i_jump_instr && (i_jump_length == 3'd2);
wire [0:0] jump_rel4 = i_jump_instr && (i_jump_length == 3'd3);
//wire [0:0] jump_abs5 = i_jump_instr && (i_jump_length == 3'd4);
wire [0:0] jump_relative = jump_rel2 || jump_rel3 || jump_rel4;
/* this appears to be SLOW */
wire [0:0] is_inst_rtn = !i_test_jump && i_block_0x && !i_nibble[3] && !i_nibble[2];
wire [0:0] is_rtnyes = i_test_jump && !(| jump_next_offset[7:0]);
wire [0:0] do_rtn = i_phases[2] && is_inst_rtn;
wire [0:0] do_rtnyes = i_phases[2] && is_rtnyes;
reg [19:0] jump_next_offset;
always @(*) begin
case (jump_counter)
3'd0: jump_next_offset = { {16{1'b0}}, i_nibble};
3'd1: jump_next_offset = { {12{jump_rel2?i_nibble[3]:1'b0}} , i_nibble, jump_offset[ 3:0]};
3'd2: jump_next_offset = { { 8{jump_rel3?i_nibble[3]:1'b0}} , i_nibble, jump_offset[ 7:0]};
3'd3: jump_next_offset = { { 4{jump_rel4?i_nibble[3]:1'b0}} , i_nibble, jump_offset[11:0]};
3'd4: jump_next_offset = { i_nibble, jump_offset[15:0]};
default: jump_next_offset = 20'h00000;
endcase
end
reg [19:0] reg_PC;
reg [2:0] reg_rstk_ptr;
reg [19:0] reg_RSTK[0:7];
reg [19:0] prev_PC;
reg [2:0] rstk_ptr_to_push_at;
reg [19:0] addr_to_return_to;
reg [2:0] rstk_ptr_after_pop;
assign o_current_pc = reg_PC;
initial begin
o_reload_pc = 1'b0;
just_reset = 1'b1;
init_counter = 3'd0;
jump_decode = 1'b0;
jump_counter = 3'd0;
reg_PC = 20'h00000;
reg_rstk_ptr = 3'd7;
addr_to_return_to = 20'b0;
rstk_ptr_after_pop = 3'd0;
rstk_ptr_to_push_at = 3'd0;
$monitor ("PC_RSTK %0d: [%d] bus_busy %b | exec_unit_busy %b | j_cnt %h | i_jump %b | do_jmp %b | is_rtnyes %b | block_0x %b | nibble %h | test_jmp %b | offset[7:0] %h",
i_phase, i_cycle_ctr, i_bus_busy, i_exec_unit_busy,
jump_counter, i_jump_instr, do_jump_instr, is_rtnyes,
i_block_0x, i_nibble, i_test_jump, jump_next_offset[7:0]);
end
/*
* the process
*/
always @(posedge i_clk) begin
/* initialize RSTK */
if (just_reset || (init_counter != 0)) begin
`ifdef SIM
$display("PC_RSTK %0d: [%d] initializing RSTK[%0d]", i_phase, i_cycle_ctr, init_counter);
`endif
reg_RSTK[init_counter] <= 20'h00000;
init_counter <= init_counter + 3'd1;
if (init_counter == 3'd7) begin
`ifdef SIM
$display("PC_RSTK %0d: [%d] exit from reset mode", i_phase, i_cycle_ctr);
`endif
just_reset <= 1'b0;
end
end
/*
* only do something when nothing is busy doing some other tasks
* either talking to the bus, or debugging something
*/
// if (!i_debug_cycle)
// $display("PC_RSTK %0d: [%d] !i_bus_busy %b", i_phase, i_cycle_ctr, !i_bus_busy);
if (i_clk_en && !i_bus_busy && !i_exec_unit_busy) begin
// if (i_phases[3] && just_reset) begin
// $display("PC_RSTK %0d: [%d] exit from reset mode", i_phase, i_cycle_ctr);
// just_reset <= 1'b0;
// end
if (i_phases[1] && !just_reset) begin
$display("PC_RSTK %0d: [%d] inc_pc %5h => %5h", i_phase, i_cycle_ctr, reg_PC, reg_PC + 20'h00001);
prev_PC <= reg_PC;
reg_PC <= reg_PC + 20'h00001;
end
/*
* jump instruction calculations
*/
/* start the jump instruction
* the jump base is:
* address of first nibble of the offset when goto
* address of nibble after the offset when gosub
*/
if (i_phases[3] && do_jump_instr && !jump_decode) begin
`ifdef SIM
$display("PC_RSTK %0d: [%d] start decode jump %0d | jump_base %5h (i_test_jump %b)", i_phase, i_cycle_ctr,
i_jump_length, i_push_pc? reg_PC + {{17{1'b0}},(i_jump_length + 3'd1)} : (i_test_jump ? prev_PC : reg_PC), i_test_jump);
`endif
jump_counter <= 3'd0;
/* may not be correct for test_jump */
jump_base <= i_push_pc? reg_PC + {{17{1'b0}},(i_jump_length + 3'd1)} : (i_test_jump ? prev_PC : reg_PC);
jump_decode <= 1'b1;
rstk_ptr_to_push_at <= (reg_rstk_ptr + 3'o1) & 3'o7;
end
/* one step of the calculation (one nibble of data came in) */
if (i_phases[2] && do_jump_instr && jump_decode) begin
$display("PC_RSTK %0d: [%d] decode jump %0d/%0d %h %5h", i_phase, i_cycle_ctr, i_jump_length, jump_counter, i_nibble, jump_next_offset);
jump_offset <= jump_next_offset[15:0];
jump_counter <= jump_counter + 3'd1;
if (jump_counter == i_jump_length) begin
$write("PC_RSTK %0d: [%d] execute jump(%0d) jump_base %h jump_next_offset %h", i_phase, i_cycle_ctr, i_jump_length, jump_base, jump_next_offset);
jump_decode <= 1'b0;
// jump_exec <= 1'b1;
// o_reload_pc <= 1'b1;
reg_PC <= jump_relative ? jump_next_offset + jump_base : jump_next_offset;
if (i_push_pc) begin
$write(" ( push %5h => RSTK[%0d] )", reg_PC, rstk_ptr_to_push_at);
reg_RSTK[rstk_ptr_to_push_at] <= reg_PC;
reg_rstk_ptr <= rstk_ptr_to_push_at;
end
$write("\n");
end
end
end
/*
* RTN instruction
*/
if (i_clk_en && !i_bus_busy && !i_exec_unit_busy) begin
/* this happens at the same time in the decoder */
if (i_phases[1]) begin
addr_to_return_to <= reg_RSTK[reg_rstk_ptr];
rstk_ptr_after_pop <= (reg_rstk_ptr - 3'o1) & 3'o7;
end
if (do_rtn || do_rtnyes) begin
/* this is an RTN */
reg_PC <= addr_to_return_to;
reg_RSTK[reg_rstk_ptr] <= 20'h00000;
reg_rstk_ptr <= rstk_ptr_after_pop;
`ifdef SIM
$write("PC_RSTK %0d: [%d] RTN", i_phase, i_cycle_ctr);
case (i_nibble)
4'h0:
if (i_test_jump) $display("YES");
else $display("SXM");
4'h1: $write("\n");
4'h2: $display("SC");
4'h3: $display("CC");
default: begin end
endcase
$display("PC_RSTK %0d: [%d] execute RTN back to %5h", i_phase, i_cycle_ctr, addr_to_return_to);
`endif
end
end
if (i_reset) begin
o_reload_pc <= 1'b0;
just_reset <= 1'b1;
init_counter <= 3'd0;
jump_decode <= 1'b0;
jump_counter <= 3'd0;
reg_PC <= 20'h00000;
reg_rstk_ptr <= 3'd7;
end
end
endmodule