module ledblink (CLKin, CLKout, SI, SO, CS, LED); input CLKin; output CLKout; reg CLKout; reg [2:0] CNT; // Counter for counting to 8 reg [15:0] ADDRESS; // Address reg [3:0] COUNTER; reg [7:0] DATA; // Data to write reg [7:0] DATAIN; reg [2:0] STATE; // State, 1 = Instruction, 2 + 3 = 16 bit Address, 4 = Byte to write reg WRBIT; // 1 for write, 0 for read reg [2:0] COUNTCLK; output SI; // Data to sram reg SI; output CS; // Chip select reg CS; input SO; output LED; reg LED; initial CNT = 1'b0; initial STATE = 1'b0; initial SI = 1'b1; initial ADDRESS = 16'b0000000000001110; initial COUNTER = 4'd15; initial DATA = 8'b10101010; initial CS = 1'b1; initial DATAIN = 8'b00000000; initial WRBIT = 1'b1; initial LED = 1'b0; initial CLKout = 1'b0; initial COUNTCLK = 1'b0; always @ (posedge CLKout) begin CNT = CNT + 1'b1; if (STATE == 0) begin CS = 1'b0; CNT = 3'd7; STATE = STATE + 1'b1; SI = 1'b0; end else if (STATE == 1) begin // Write Instruction if (CNT == 0) SI = 1'b0; if (CNT == 6) SI = 1'b1; if (CNT == 7) begin if (WRBIT == 1) SI = 1'b0; STATE = STATE + 1'b1; end end else if (STATE == 2) begin // 16 bit Address SI = ADDRESS[COUNTER]; COUNTER = COUNTER - 1'b1; if (COUNTER == 15) begin STATE = STATE + 1'b1; COUNTER = 7; // Re-assign address count to data counter end end else if (STATE == 3) begin if (WRBIT == 1) SI = DATA[COUNTER]; if (WRBIT == 0) DATAIN[COUNTER] = SO; COUNTER = COUNTER - 1'b1; if (CNT == 7) begin STATE = STATE + 1'b1; end end else if (STATE == 4) begin SI = 1'b1; CS = 1'b1; STATE = 1'b0; WRBIT = WRBIT + 1'b1; if (DATAIN == DATA) LED = 1'b1; else LED = 1'b0; end else STATE = STATE + 1'b1; end always @ (posedge CLKin) begin COUNTCLK = COUNTCLK + 1'b1; if (COUNTCLK == 7) CLKout = ~CLKout; end endmodule