VerilogCPU/cpu_controller.sv

67 lines
1.5 KiB
Systemverilog
Raw Permalink Normal View History

2018-06-05 23:42:20 -07:00
/**
* Controller to interface CPU with the outside world.
* The clk and reset inputs work as expected.
* Inputs are fed in from the various input sources,
* and given directly to CPU.
* spi_clk, spi_ss and spi_mosi are SPI connections used to program the CPU.
* Outputs displayed from the CPU disp instruction.
*/
2018-06-05 22:57:01 -07:00
module cpu_controller(input logic clk, reset,
input logic [11:0] inputs,
input logic spi_clk, spi_ss, spi_mosi,
output logic [11:0] outputs);
logic [31:0] inst;
logic [7:0] addr;
logic [19:0] the_void;
logic prog;
logic en;
2018-06-05 23:42:20 -07:00
2018-06-05 22:57:01 -07:00
logic inst_ready;
logic inst_done;
logic inst_ready_edge;
2018-06-05 23:42:20 -07:00
2018-06-05 22:57:01 -07:00
logic cpu_clk;
2018-06-05 23:42:20 -07:00
2018-06-05 22:57:01 -07:00
edge_detector inst_ready_detector(
.in(inst_ready),
.clk(clk),
2018-06-07 21:26:46 -07:00
.reset(reset),
.pos_edge(inst_ready_edge));
2018-06-05 23:42:20 -07:00
2018-06-05 22:57:01 -07:00
logic prog_forward_clk;
2018-06-05 23:42:20 -07:00
2018-06-05 22:57:01 -07:00
assign prog_forward_clk = inst_ready_edge & ~inst_done & prog;
assign cpu_clk = reset | (en ? clk : prog_forward_clk);
2018-06-05 23:42:20 -07:00
2018-06-05 22:57:01 -07:00
spi_slave prog_slave(
.clk(clk),
.reset(reset),
.master_clk(spi_clk),
.ss(spi_ss),
.mosi(spi_mosi),
.ready(inst_ready),
.done(inst_done),
.data(inst));
2018-06-05 23:42:20 -07:00
2018-06-05 22:57:01 -07:00
cpu cpu_unit(
.clk(cpu_clk),
.inputs({4'b0, inputs}),
.reset(reset),
.prog(prog),
.pinst(inst),
.paddr(addr),
.disp({the_void, outputs}));
2018-06-05 23:42:20 -07:00
2018-06-05 22:57:01 -07:00
always_ff@(posedge clk)
if (reset) begin
prog <= 0;
en <= 0;
addr <= 0;
end else begin
en <= inst_done;
prog <= (prog & ~inst_done) | (inst_ready_edge & (inst == 32'hCAFEBABE));
addr <= addr + prog_forward_clk;
end
2018-06-05 23:42:20 -07:00
endmodule