hp-saturn/saturn_regs_pc_rstk.v

270 lines
8.1 KiB
Coq
Raw Normal View History

/*
(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,
2019-03-03 15:19:07 +01:00
i_clk_en,
i_reset,
i_phases,
i_phase,
i_cycle_ctr,
i_bus_busy,
i_exec_unit_busy,
i_nibble,
2019-03-03 20:48:56 +01:00
i_jump_instr,
i_jump_length,
2019-03-05 05:39:34 +01:00
i_block_0x,
2019-03-04 11:52:05 +01:00
i_push_pc,
o_current_pc,
2019-03-04 11:52:05 +01:00
o_reload_pc,
/* debugger access */
i_dbg_rstk_ptr,
o_dbg_rstk_val,
o_reg_rstk_ptr
);
input wire [0:0] i_clk;
2019-03-03 15:19:07 +01:00
input wire [0:0] i_clk_en;
input wire [0:0] i_reset;
input wire [3:0] i_phases;
2019-03-15 13:31:37 +01:00
// 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;
2019-03-03 20:48:56 +01:00
input wire [0:0] i_jump_instr;
input wire [2:0] i_jump_length;
2019-03-05 05:39:34 +01:00
input wire [0:0] i_block_0x;
2019-03-04 11:52:05 +01:00
input wire [0:0] i_push_pc;
output wire [19:0] o_current_pc;
output reg [0:0] o_reload_pc;
2019-03-04 11:52:05 +01:00
input wire [2:0] i_dbg_rstk_ptr;
output wire [19:0] o_dbg_rstk_val;
output wire [2:0] o_reg_rstk_ptr;
2019-03-04 11:52:05 +01:00
assign o_dbg_rstk_val = reg_RSTK[i_dbg_rstk_ptr];
assign o_reg_rstk_ptr = reg_rstk_ptr;
2019-03-04 11:52:05 +01:00
/**************************************************************************************************
*
* pc and rstk handling module
*
*************************************************************************************************/
2019-03-03 20:48:56 +01:00
wire [0:0] do_jump_instr = !just_reset && i_jump_instr;
/*
* local variables
*/
reg [0:0] just_reset;
2019-03-04 11:52:05 +01:00
reg [2:0] init_counter;
2019-03-03 20:48:56 +01:00
reg [0:0] jump_decode;
reg [2:0] jump_counter;
reg [19:0] jump_base;
2019-03-15 13:31:37 +01:00
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);
2019-03-15 13:31:37 +01:00
//wire [0:0] jump_abs5 = i_jump_instr && (i_jump_length == 3'd4);
wire [0:0] jump_relative = jump_rel2 || jump_rel3 || jump_rel4;
2019-03-15 07:13:38 +01:00
/* this appears to be SLOW */
wire [0:0] is_rtn = i_phases[2] && i_block_0x && !i_nibble[3] && !i_nibble[2];
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
2019-03-04 11:52:05 +01:00
reg [19:0] reg_PC;
reg [2:0] reg_rstk_ptr;
reg [19:0] reg_RSTK[0:7];
reg [2:0] rstk_ptr_to_push_at;
reg [19:0] addr_to_return_to;
reg [2:0] rstk_ptr_after_pop;
2019-03-04 11:52:05 +01:00
assign o_current_pc = reg_PC;
initial begin
o_reload_pc = 1'b0;
2019-03-03 20:48:56 +01:00
just_reset = 1'b1;
2019-03-04 11:52:05 +01:00
init_counter = 3'd0;
2019-03-03 20:48:56 +01:00
jump_decode = 1'b0;
jump_counter = 3'd0;
2019-03-04 11:52:05 +01:00
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;
end
/*
* the process
*/
always @(posedge i_clk) begin
2019-03-04 11:52:05 +01:00
/* initialize RSTK */
if (just_reset || (init_counter != 0)) begin
2019-03-14 17:52:03 +01:00
`ifdef SIM
2019-03-04 11:52:05 +01:00
$display("PC_RSTK %0d: [%d] initializing RSTK[%0d]", i_phase, i_cycle_ctr, init_counter);
2019-03-14 17:52:03 +01:00
`endif
2019-03-04 11:52:05 +01:00
reg_RSTK[init_counter] <= 20'h00000;
init_counter <= init_counter + 3'd1;
2019-03-14 17:52:03 +01:00
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
2019-03-04 11:52:05 +01:00
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
2019-03-14 17:52:03 +01:00
// 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
2019-03-04 11:52:05 +01:00
$display("PC_RSTK %0d: [%d] inc_pc %5h => %5h", i_phase, i_cycle_ctr, reg_PC, reg_PC + 20'h00001);
reg_PC <= reg_PC + 20'h00001;
end
2019-03-03 20:48:56 +01:00
/*
* 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
2019-03-15 10:21:02 +01:00
$display("PC_RSTK %0d: [%d] start decode jump %0d | jump_base %5h", i_phase, i_cycle_ctr,
i_jump_length, i_push_pc? reg_PC + {{17{1'b0}},(i_jump_length + 3'd1)} : reg_PC);
`endif
jump_counter <= 3'd0;
2019-03-15 10:21:02 +01:00
jump_base <= i_push_pc? reg_PC + {{17{1'b0}},(i_jump_length + 3'd1)} : 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) */
2019-03-03 20:48:56 +01:00
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);
2019-03-15 13:31:37 +01:00
jump_offset <= jump_next_offset[15:0];
2019-03-03 20:48:56 +01:00
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);
2019-03-03 20:48:56 +01:00
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");
2019-03-03 20:48:56 +01:00
end
end
end
2019-03-03 20:48:56 +01:00
2019-03-05 05:39:34 +01:00
/*
* RTN instruction
*/
if (i_clk_en && !i_bus_busy && !i_exec_unit_busy) begin
2019-03-05 05:39:34 +01:00
/* 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 (is_rtn) begin
2019-03-05 05:39:34 +01:00
/* 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
2019-03-05 05:39:34 +01:00
$write("PC_RSTK %0d: [%d] RTN", i_phase, i_cycle_ctr);
case (i_nibble)
4'h0: $display("SXM");
2019-03-15 21:38:28 +01:00
4'h1: $write("\n");
2019-03-05 05:39:34 +01:00
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
2019-03-05 05:39:34 +01:00
end
end
if (i_reset) begin
o_reload_pc <= 1'b0;
2019-03-03 20:48:56 +01:00
just_reset <= 1'b1;
2019-03-04 11:52:05 +01:00
init_counter <= 3'd0;
2019-03-03 20:48:56 +01:00
jump_decode <= 1'b0;
jump_counter <= 3'd0;
2019-03-04 11:52:05 +01:00
reg_PC <= 20'h00000;
reg_rstk_ptr <= 3'd7;
end
end
endmodule