Now, you need to explain to me why I made these changes and where the pitfalls are:
module host_handler (
// input
input wire reset, // GPU reset signal
input wire GPU_CLK, // GPU clock (125 MHz)
//input wire host_CLK, // Microcom clock signal (8 MHz)
input wire host_M1, // Z80 M1 - active LOW
input wire host_MREQ, // Z80 MREQ - active LOW
input wire host_WR, // Z80 WR - active LOW
input wire host_RD, // Z80 RD - active LOW
input wire [21:0] host_addr, // Microcom 22-bit address bus
input wire [7:0] host_wr_data, // Z80 DATA bus to pass incoming data to GPU RAM
input wire [7:0] gpu_r_data,
// output
//output wire [7:0] h_rd_data, // Z80 DATA bus to return data from GPU RAM to Z80
//output reg h_rd_req, //
output reg gpu_wr_ena, // flag HIGH when writing to GPU RAM
output reg data_dir, // control level converter direction for data flow
output reg [19:0] gpu_addr, // connect to host_addr in vid_osd_generator to address GPU RAM
output reg [7:0] gpu_wdata, // 8-bit data bus to GPU RAM in vid_osd_generator
output reg [7:0] host_data,
output reg host_data_ena // flag HIGH to write data back to Z80
);
parameter MEMORY_RANGE = 3'b011; // host_addr[21:19] == 3'b011 targets the 512KB 'window' at 0x180000-0x1FFFFF
wire mem_window, Z80_mreq, Z80_write, Z80_read, Write_GPU_RAM, Read_GPU_RAM, GPU_data_oe;
reg last_Z80_WR = 1'b0; // keep these low on power-up, otherwise a read or write pulse may be triggered
reg last_Z80_RD = 1'b0;
assign mem_window = (host_addr[21:19] == MEMORY_RANGE); // Define an active memory range
assign Z80_mreq = ~host_MREQ && host_M1 ; // Define a bus memory access state
assign Z80_write = ~host_WR && last_Z80_WR ; // Isolate a single write transaction
assign Z80_read = ~host_RD && last_Z80_RD ; // Isolate a single read transaction
assign Write_GPU_RAM = mem_window && Z80_mreq && Z80_write; // Define a GPU Write action
assign Read_GPU_RAM = mem_window && Z80_mreq && Z80_read ; // Define a GPU Read action
assign GPU_data_oe = mem_window && Z80_mreq && ~host_RD ; // Define the time the GPU ouputs data onto the Z80 data bus
always @ (posedge GPU_CLK)
begin
// is this the start of a WR memory cycle being executed within the GPU's memory window?
if (Write_GPU_RAM) // these compares are finalised on the next GPU_CLK
begin
// yes - pass the address (limited to 512 KB) and data through
gpu_wdata <= host_wr_data;
gpu_addr <= host_addr[18:0];
gpu_wr_ena <= 1'b1;
end // memory cycle
else begin
gpu_wr_ena <= 1'b0;
end
if (Read_GPU_RAM)
begin
gpu_addr <= host_addr[18:0] ; // pass address to GPU RAM
gpu_rd_req <= 1'b1 ; // Send a read request to GPU.
end else if (gpu_rd_rdy)
begin
gpu_rd_req <= 1'b0; // End the read request once the read is ready
host_data[7:0] <= gpu_r_data[7:0]; // Latch the GPU ram read into the output register for the Z80
end
data_dir <= GPU_data_oe ;
host_data_ena <= GPU_data_oe ;
last_Z80_WR <= host_WR;
last_Z80_RD <= host_RD;
end
endmodule