Example:
input clk_125mhz,
input data_clock,
input data_readback,
inout [7:0] data_bus,
input [7:0] rctl_1,
input [7:0] rctl_2,
output reg [7:0] wctl_1,
output reg [7:0] wctl_2
reg [7:0] address; // A store of a string of bytes which will be our command.
reg oe_data = 0;
red [7:0[ data_out = 0;
assign data_bus = oe_data ? data_out : 8'bzzzzzzzz ; // Generate the bi-directional 8bit IO port.
reg prev_data_clock = 0;
always @(posedge clk_125mhz) begin
prev_data_clock <= data_clock ;
if (!prev_data_clock && data_clock ) begin // Load address on the detected rising transition of the data_clock.
address <= data_in_bus ;
end
if (prev_data_clock && !data_clock ) begin // Load data on the detected falling transition of the data_clock.
if (address = 8'h01) wctl_1<=data_bus;
if (address = 8'h02) wctl_2<=data_bus;
end
// readback control
if (data_readback && data_clock ) begin // When the data_readback is high and only while the data_clock is kept high.
oe_data <=1 ; // output enable 8 bit bus.
if (address = 8'h81) data_out <= rctl_1 ;
if (address = 8'h82) data_out <= rctl_2 ;
end else oe_data <=0 ; // input enable 8 bit bus.
end
This code is missing a 'reset' at the beginning and you may add a check for output enable by looking at the stored high address bits to help protect your IOs.
To transmit data:
Make sure the 'data_clock' & 'data_readback' are low.
output enable the MCU 8bit data port
TX Address on the 'data_bus'.
Make 'data_clock' high
TX Control byte on the 'data_bus'.
Make 'data_clock' low.
Done.
To read data:
Make sure the 'data_clock' & 'data_readback' are low.
output enable the MCU 8bit data port
TX Address on the 'data_bus'.
Make 'data_clock' high
Switch your MCU 8-bit bus to input mode and then make 'data_readback' high.
wait 1 nop
read 8 bit data bus. (You can pause here, or continuously read here if you want to monitor a time sensitive status flag)
Make the 'data_clock' & 'data_readback' low.
Done.