210 lines
7.4 KiB
Verilog
210 lines
7.4 KiB
Verilog
// ROT-8 execution core. The default machine has 64 eight-bit data cells.
|
|
// The program interface accepts either packed opcodes or one opcode per byte.
|
|
// A loop back edge pops the loop entry and jumps to '[' so it is tested again.
|
|
//
|
|
// Opcode order matches the R8Forge tape format:
|
|
// 0 '>' STP 1 '<' BTP 2 '+' ROR 3 '-' FLB
|
|
// 4 '[' BIZ 5 ']' RNZ 6 '.' OUT 7 ',' INP
|
|
`default_nettype none
|
|
|
|
module rot8 #(
|
|
parameter integer WIDTH = 8, // bits per data cell
|
|
parameter integer RING = 64, // number of data cells
|
|
parameter integer IPW = 16, // instruction pointer width
|
|
parameter integer STACKW = 4, // stack pointer width; 2^STACKW - 1 usable entries
|
|
parameter integer EPROM = 0 // 0: 3 bit packed stream, 1: one instruction per byte
|
|
) (
|
|
input wire clk,
|
|
input wire rst_n,
|
|
input wire run, // 1: continuous run, 0: single-step mode
|
|
input wire step, // one-cycle request to execute one instruction
|
|
|
|
// Packed mode uses both reads. EPROM mode uses prog_data0.
|
|
output wire [IPW-1:0] prog_addr0,
|
|
output wire [IPW-1:0] prog_addr1,
|
|
input wire [7:0] prog_data0,
|
|
input wire [7:0] prog_data1,
|
|
input wire [IPW-1:0] prog_len, // instruction count; halt when ip reaches it
|
|
|
|
// input: blocks until in_valid, one byte per ','
|
|
input wire in_valid,
|
|
input wire [7:0] in_data,
|
|
output wire in_ready,
|
|
|
|
// output: one cycle pulse per '.'
|
|
output reg out_valid,
|
|
output reg [7:0] out_data,
|
|
|
|
// debug / panel
|
|
output reg [IPW-1:0] ip,
|
|
output reg [$clog2(RING)-1:0] dp,
|
|
output reg [31:0] steps,
|
|
output reg halted
|
|
);
|
|
|
|
localparam integer DPW = $clog2(RING);
|
|
|
|
localparam [2:0] OP_STP = 3'd0, OP_BTP = 3'd1, OP_ROR = 3'd2, OP_FLB = 3'd3;
|
|
localparam [2:0] OP_BIZ = 3'd4, OP_RNZ = 3'd5, OP_OUT = 3'd6, OP_INP = 3'd7;
|
|
|
|
localparam [1:0] S_FETCH = 2'd0, S_SCAN = 2'd1, S_HALT = 2'd2;
|
|
|
|
reg [1:0] state;
|
|
reg step_pending;
|
|
reg [WIDTH-1:0] cells [0:RING-1];
|
|
reg [IPW-1:0] stack [0:(1<<STACKW)-1];
|
|
reg [STACKW-1:0] sp;
|
|
|
|
// Packed opcodes can cross a byte boundary. EPROM opcodes start at bit 0.
|
|
wire [IPW+1:0] bit_index = ip * 3;
|
|
wire [IPW-1:0] byte_index = bit_index >> 3;
|
|
wire [2:0] bit_offset = bit_index[2:0];
|
|
wire [15:0] window = {prog_data1, prog_data0};
|
|
wire [2:0] op_packed = (window >> bit_offset) & 16'h0007;
|
|
|
|
wire [2:0] op_now = EPROM ? prog_data0[2:0] : op_packed;
|
|
|
|
assign prog_addr0 = EPROM ? ip : byte_index;
|
|
assign prog_addr1 = EPROM ? ip : (byte_index + 1'b1);
|
|
// Input transfers when ready and valid are both high on the clock edge.
|
|
// Ready is independent of in_valid and stays low while paused or halted.
|
|
wire in_program = ip < prog_len;
|
|
wire execute_cycle = rst_n && !halted && in_program && (run || step || step_pending);
|
|
wire execute_fetch = execute_cycle && (state == S_FETCH);
|
|
assign in_ready = execute_fetch && (op_now == OP_INP);
|
|
|
|
// Number of unmatched '[' opcodes in the forward scan.
|
|
reg [IPW-1:0] scan_depth;
|
|
|
|
integer i;
|
|
always @(posedge clk or negedge rst_n) begin
|
|
if (!rst_n) begin
|
|
state <= S_FETCH;
|
|
ip <= {IPW{1'b0}};
|
|
dp <= {DPW{1'b0}};
|
|
sp <= {STACKW{1'b0}};
|
|
steps <= 32'd0;
|
|
halted <= 1'b0;
|
|
out_valid <= 1'b0;
|
|
out_data <= 8'd0;
|
|
scan_depth <= {IPW{1'b0}};
|
|
step_pending <= 1'b0;
|
|
for (i = 0; i < RING; i = i + 1)
|
|
cells[i] <= {WIDTH{1'b0}};
|
|
end else begin
|
|
out_valid <= 1'b0;
|
|
|
|
// No instruction or scan may change state once it reaches the tape end.
|
|
if (!in_program) begin
|
|
halted <= 1'b1;
|
|
state <= S_HALT;
|
|
step_pending <= 1'b0;
|
|
end else begin
|
|
case (state)
|
|
S_FETCH: begin
|
|
if (execute_fetch) begin
|
|
// Keep the request active until a blocking input or scan completes.
|
|
step_pending <= !run && ((op_now == OP_INP && !in_valid)
|
|
|| (op_now == OP_BIZ && !cells[dp][0]));
|
|
case (op_now)
|
|
OP_STP: begin
|
|
dp <= (dp == RING - 1) ? {DPW{1'b0}} : dp + 1'b1;
|
|
ip <= ip + 1'b1;
|
|
end
|
|
OP_BTP: begin
|
|
dp <= (dp == 0) ? (RING - 1) : dp - 1'b1;
|
|
ip <= ip + 1'b1;
|
|
end
|
|
OP_ROR: begin
|
|
cells[dp] <= {cells[dp][0], cells[dp][WIDTH-1:1]};
|
|
ip <= ip + 1'b1;
|
|
end
|
|
OP_FLB: begin
|
|
cells[dp][0] <= ~cells[dp][0];
|
|
ip <= ip + 1'b1;
|
|
end
|
|
OP_BIZ: begin
|
|
if (!cells[dp][0]) begin
|
|
// skip forward to the matching ']', then past it
|
|
scan_depth <= 1;
|
|
state <= S_SCAN;
|
|
ip <= ip + 1'b1;
|
|
end else begin
|
|
// Save the loop entry unless incrementing sp would wrap.
|
|
if (sp == {STACKW{1'b1}}) begin
|
|
halted <= 1'b1;
|
|
state <= S_HALT;
|
|
end else begin
|
|
stack[sp] <= ip;
|
|
sp <= sp + 1'b1;
|
|
end
|
|
ip <= ip + 1'b1;
|
|
end
|
|
end
|
|
OP_RNZ: begin
|
|
if (cells[dp][0]) begin
|
|
// Pop before returning to '['. A successful retest pushes it again.
|
|
if (sp != 0) begin
|
|
ip <= stack[sp - 1'b1];
|
|
sp <= sp - 1'b1;
|
|
end else begin
|
|
// The requested back edge has no saved loop entry.
|
|
halted <= 1'b1;
|
|
state <= S_HALT;
|
|
end
|
|
end else begin
|
|
if (sp != 0)
|
|
sp <= sp - 1'b1; // the loop finished
|
|
ip <= ip + 1'b1;
|
|
end
|
|
end
|
|
OP_OUT: begin
|
|
out_valid <= 1'b1;
|
|
out_data <= cells[dp];
|
|
ip <= ip + 1'b1;
|
|
end
|
|
OP_INP: begin
|
|
if (in_valid) begin
|
|
cells[dp] <= in_data[WIDTH-1:0];
|
|
ip <= ip + 1'b1;
|
|
end
|
|
end
|
|
default: ip <= ip + 1'b1;
|
|
endcase
|
|
// A waiting ',' counts only when it receives a byte.
|
|
if (op_now != OP_INP || in_valid)
|
|
steps <= steps + 32'd1;
|
|
end
|
|
end
|
|
|
|
S_SCAN: begin
|
|
// Inspect one opcode per enabled cycle without incrementing steps.
|
|
if (execute_cycle) begin
|
|
if (!run && step)
|
|
step_pending <= 1'b1;
|
|
if (op_now == OP_BIZ)
|
|
scan_depth <= scan_depth + 1'b1;
|
|
else if (op_now == OP_RNZ)
|
|
scan_depth <= scan_depth - 1'b1;
|
|
ip <= ip + 1'b1;
|
|
if (op_now == OP_RNZ && scan_depth == 1) begin
|
|
state <= S_FETCH; // ip now points past the matching ']'
|
|
step_pending <= 1'b0;
|
|
end
|
|
end
|
|
end
|
|
|
|
S_HALT: begin
|
|
halted <= 1'b1;
|
|
end
|
|
|
|
default: state <= S_FETCH;
|
|
endcase
|
|
end
|
|
end
|
|
end
|
|
|
|
endmodule
|
|
|
|
`default_nettype wire
|