SPI BIT BANG CODE IMPLEMENTATION

In the slave everything has to happen on the clock edge from the master (either rising or falling depending on the polarity mode). So in your while loop you just want to block until you see a clock edge, then you go do your processing (read the MOSI pin and put your data onto the MISO pin). Once that is done you return to your blocking loop until you see the next clock edge, and repeat until all the bits have been processed:

void WaitForRisingEdge(void) 
{
	// Wait for clock pin to go low
	while(IN_REG & (1 << CLOCK_PIN)) {
		continue;
	}

	// Wait for clock pin to go high
	while(!(IN_REG & (1 << CLOCK_PIN)) {
		continue;
	}	
}

uint8_t SendReceiveByte(uint8_t data) {
	
	uint8_t i = 0;

	while(i < 8) {
	
		WaitForRisingEdge();

		// Send bit to master (MSB first)
		if(data & 0x80) {
			OUT_REG |= (1 << DATAOUT_PIN);
		}
		else {
			OUT_REG &= ~(1 << DATAOUT_PIN);
		}

		// Shuffle data along
		data <<= 1;

		// Read bit from master (MSB first)
		if(IN_REG & (1 << DATAIN_PIN)) {
			data |= 0x01;
		}

		// Next bit
		i++;
	}
	
	return data;
}

void main() {

	// Setup ports...
	
	// Main loop
	while(1) {
		PORTD = SendReceiveByte(40);
	}
}