SummerCart64/fw/rtl/cpu/cpu_usb.sv

130 lines
3.3 KiB
Systemverilog
Raw Normal View History

2021-08-12 21:07:47 +02:00
module cpu_usb (
2021-08-18 13:54:07 +02:00
if_system sys,
2021-08-15 21:49:02 +02:00
if_cpu_bus bus,
2021-08-21 02:53:28 +02:00
if_dma.cpu dma,
2021-08-12 21:07:47 +02:00
output usb_clk,
output usb_cs,
input usb_miso,
2021-08-16 00:23:10 +02:00
inout [3:0] usb_miosi,
2021-08-18 13:54:07 +02:00
input usb_pwren
2021-08-12 21:07:47 +02:00
);
2021-08-18 13:54:07 +02:00
logic rx_flush;
logic rx_empty;
logic rx_read;
logic [7:0] rx_rdata;
2021-08-12 21:07:47 +02:00
2021-08-18 13:54:07 +02:00
logic tx_flush;
logic tx_full;
logic tx_write;
logic [7:0] tx_wdata;
2021-08-12 21:07:47 +02:00
2021-08-21 02:53:28 +02:00
// always_ff @(posedge sys.clk) begin
// bus.ack <= 1'b0;
// if (bus.request) begin
// bus.ack <= 1'b1;
// end
// end
2021-08-20 19:51:55 +02:00
2021-08-21 02:53:28 +02:00
// always_comb begin
// bus.rdata = 32'd0;
// if (bus.ack) begin
// case (bus.address[2:2])
// 0: bus.rdata = {30'd0, ~tx_full, ~rx_empty};
// 1: bus.rdata = {24'd0, rx_rdata};
// default: bus.rdata = 32'd0;
// endcase
// end
// end
// always_ff @(posedge sys.clk) begin
// rx_flush <= 1'b0;
// rx_read <= 1'b0;
// tx_flush <= 1'b0;
// tx_write <= 1'b0;
// if (bus.request) begin
// case (bus.address[2:2])
// 2'd0: if (bus.wmask[0]) begin
// {tx_flush, rx_flush} <= bus.wdata[3:2];
// end
// 2'd1: if (bus.wmask[0]) begin
// if (!tx_full) begin
// tx_write <= 1'b1;
// tx_wdata <= bus.wdata[7:0];
// end
// end else begin
// rx_read <= 1'b1;
// end
// endcase
// end
// end
typedef enum bit [0:0] {
S_IDLE,
S_WAIT
} e_state;
e_state state;
logic byte_counter;
2021-08-12 21:07:47 +02:00
2021-08-20 19:51:55 +02:00
always_ff @(posedge sys.clk) begin
2021-08-15 21:49:02 +02:00
rx_read <= 1'b0;
2021-08-12 21:07:47 +02:00
2021-08-21 02:53:28 +02:00
if (sys.reset) begin
dma.request <= 1'b0;
dma.write <= 1'b1;
dma.address <= 32'd0;
state <= S_IDLE;
byte_counter <= 1'b0;
end else begin
case (state)
S_IDLE: begin
if (!rx_empty && !rx_read) begin
byte_counter <= ~byte_counter;
rx_read <= 1'b1;
dma.wdata <= {dma.wdata[7:0], rx_rdata};
if (byte_counter) begin
dma.request <= 1'b1;
state <= S_WAIT;
end
end
2021-08-15 21:49:02 +02:00
end
2021-08-21 02:53:28 +02:00
S_WAIT: begin
if (dma.ack) begin
dma.address <= dma.address + 2'd2;
dma.request <= 1'b0;
state <= S_IDLE;
2021-08-15 21:49:02 +02:00
end
end
endcase
2021-08-12 21:07:47 +02:00
end
end
usb_ft1248 usb_ft1248_inst (
2021-08-18 13:54:07 +02:00
.sys(sys),
2021-08-12 21:07:47 +02:00
.usb_clk(usb_clk),
.usb_cs(usb_cs),
.usb_miso(usb_miso),
.usb_miosi(usb_miosi),
2021-08-18 13:54:07 +02:00
.usb_pwren(usb_pwren),
2021-08-12 21:07:47 +02:00
2021-08-15 21:49:02 +02:00
.rx_flush(rx_flush),
.rx_empty(rx_empty),
.rx_read(rx_read),
.rx_rdata(rx_rdata),
2021-08-21 02:53:28 +02:00
// .tx_flush(tx_flush),
// .tx_full(tx_full),
// .tx_write(tx_write),
// .tx_wdata(tx_wdata)
2021-08-12 21:07:47 +02:00
);
endmodule