tcfix.v 834 Bytes
// tcfix.v v1 Frank Berndt
// cartrige GAL fix for toshiba flash protocol;

module tcfix (
	D0, CLE, WE, RE, CEI, CEO
);
	input D0;	// data bit 0;
	input CLE;	// cmd latch enable;
	input WE;	// write enable;
	input RE;	// read enable;
	input CEI;	// CE from BB;
	output CEO;	// CE to flash;

	// snoop on read commands;
	// cmd 00 sets counter to 512+16=528;
	// cmd 01 sets counter to 256+16=272;
	// asynchronously set count;
	// decrement on each rising edge of RE;

	reg [9:0] cnt;		// cycle counter;
	wire [1:0]load;		// load counter;

	assign load[0] = CLE & ~WE & D0;
	assign load[1] = CLE & ~WE & ~D0;

	always @(posedge RE or posedge load[0] or posedge load[1])
	begin
		if(load[0])
			cnt <= 10'd272;
		else if(load[1])
			cnt <= 10'd528;
		else
			cnt <= cnt - 1;
	end

	assign CEO = ~CLE & (CEI | (cnt == 0));

endmodule