summaryrefslogtreecommitdiff
path: root/main.c
blob: 4a5135c3c565a378dc146d9fbf12c76f7a337828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// AVR gamecube controller interface
// Copyright (C) 2014 Florian Jung
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.



/* Setup:

   lfuse: 0xe0
   hfuse: 0xd9

   attach a 12MHz quartz to the atmega8, using the appropriate XTAL pins.

   supply 3.3V to the 3.3V line of the gamecube controller.
   supply 5V to the 5V line of the gamecube controller.
   connect the GND and SHIELD lines of the controller to the common GND.
   connect the DATA line of the gamecube controller to the C5 pin.

   !!! ensure that the PORTC has bit 0 cleared at any time. !!!
   !!! otherwise, +5V is supplied to the pin; the gamecube  !!!
   !!! controller, however, may shortcut the line down to   !!!
   !!! GND at any time, leading to HARDWARE DAMAGE.         !!!
*/

#include <avr/io.h>
#include <avr/delay.h>

static char buffer[300]; // contains the exploded bits, i.e. each bit sent/received occupies one byte here.

#define OUT0 "sbi %2, 5 \n"  /* pull line to GND */
#define OUT1 "cbi %2, 5 \n"  /* tristate the line */

#define WAIT10 "ldi r16, 1 \n rcall delay_loop \n"
#define WAIT34 "ldi r16, 9 \n rcall delay_loop \n"
#define WAIT10MINUS10 ""
#define WAIT34MINUS10 "ldi r16,5 \n rcall delay_loop \n nop \n nop \n"

/* send the first "len" bytes stored in "bytes" to the controller,
 * then receive the reply and store it into the global "buffer". */
int send_recv_gc(char* bytes, int len)
{
	/* Phase 1: Send the data.
	 * Phase 1.1: Explode the bits into buffer. buffer[0..7] will contain the bits of bytes[0],
	 *            buffer[8..15] contains the bits of bytes[1], and so on. MSB first.
	 * Phase 1.2: Actually send the data.
	 *
	 * intermediate Phase: wait for data line to become high again.
	 *
	 * Phase 2: Receive the reply.
	 * Phase 2.1: Busy-loop until the line is pulled down for the first time.
	 * Phase 2.2: Actually receive now:
	 *            A counter is set to 0x80.
	 *            Busy-loop until the line becomes high. Increment the counter in each iteration.
	 *            (The line is now high.)
	 *            Busy-loop until the line becomes low again. Decrement the counter each iteration.
	 *            (Done receiving the bit)
	 *            Write out the counter to buffer[], and proceed with the next bit.
	 *
	 *            If the counter over- or underflows, stop receiving, because the line
	 *            seems to be idle again (i.e. data transfer is finished). That's a timeout
	 *            of ca. 53 us. (when running at 12 MHz)
	 *
	 * buffer[] now contains the counter values of the bits.
	 * if (buffer[42] > 0x80), i.e. (buffer[42] & 0x80), then the line was longer LOW than HIGH -> bit42 = 0
	 * otherwise, the line was longer HIGH than LOW -> bit42 = 1.
	 */

	// The NOPs are there because of symmetry reasons.
	// the "// 2" comments after the assembly directives are the number of cycles this
	// instruction will take to execute.

	char* buf_ptr = buffer;

	/****** SEND PART ******/

	int k=0;
	for (int i=0; i<len; i++)
		for (int j=0x80; j!=0; j=j>>1, k++)
			buffer[k] = ((bytes[i] & j)!=0)?1:0 ;
	
	len=len*8+1;
	
	asm volatile(
		"push r31   ; save Z\n"
		"push r30 \n\n"

		";;;;;;;; SEND PART ;;;;;;;;\n\n"

		"send_loop:   \n"
		"sbiw %3, 1 \n" // 2
		"breq send_done \n" // 1 if not done, 2 if done
		"ld r16, z+ \n" // 2
		"tst r16 \n" // 1
		"brne send_one \n\n" // 1 if zero, 2 if nonzero

		"; otherwise, send zero \n"
		"nop \n" // 1
		OUT0
		WAIT34
		OUT1
		WAIT10MINUS10
		"rjmp send_loop \n\n" // 2

		"send_one: \n"
		OUT0
		WAIT10
		OUT1
		WAIT34MINUS10
		"rjmp send_loop \n\n"

		"delay_loop:\n"
		"; this costs 7 + 3*(r16) cycles \n"
		"dec r16\n"
		"brne delay_loop\n"
		"ret\n\n"

		"send_done: \n"
		"; now send the stop bit and release the line \n"
		"nop \n nop \n nop \n"
		OUT0
		"; instead of WAIT10, do sensible work \n"
		"pop r30    ; restore Z \n"          // 2
		"pop r31 \n"                         // 2
		"clr r16 \n"                         // 1
		"nop \n nop \n nop \n nop \n nop \n" // 5
		OUT1
		"; done :) \n\n\n"


		"; now the final thing is to wait for DATA become high again (should be immediately anyway) \n"
		"send_final_loop: \n"
		"inc r16 \n"
		"breq timeout \n"
		"sbis %1, 5 \n"
		"rjmp send_final_loop \n\n\n"



		";;;;;;;; RECEIVE PART ;;;;;;;;\n\n"


		"clr r16 \n"
		"recv_wait_low_initial_loop: \n"
		"inc r16 \n"
		"breq timeout \n"
		"sbic %1, 5 \n"
		"rjmp recv_wait_low_initial_loop \n" // from low to the start of counting: 6 cycles = 0.5us
		"nop \n"
		"nop \n"
		"nop \n"
		"nop \n"


		"recv_loop: \n"
		"recv_low_begin: \n"
		"ldi r16, 128 \n" // 1
		"recv_low_loop: \n"
		"inc r16 \n"      // 1
		"breq timeout \n" // 1 if no timeout
		"sbis %1, 5 \n"   // 1               // von high auf dec: 6
		"rjmp recv_low_loop \n\n" // 2 if executed, 1 if skipped.
		"nop \n"          // to account for the rjmp recv_loop below.
		"nop \n"
		"nop \n"
		"nop \n"

		"recv_high_begin: \n"
		"nop \n" // to account for the ldi in recv_low_begin.
		"recv_high_loop: \n"
		"dec r16 \n" // 1
		"breq timeout \n" // 1 if no timeout
		"sbic %1, 5 \n" // 1                 // von low auf inc: 6
		"rjmp recv_high_loop \n\n" // 2 if executed, 1 if skipped

		"st z+, r16 \n" // 2
		"rjmp recv_loop \n\n" // 2

		"timeout: \n"
		:	"+z" ((unsigned char volatile*) buf_ptr)
		:	"I" (_SFR_IO_ADDR(PINC)), 
			"I" (_SFR_IO_ADDR(DDRC)),
			"w" (len)
		: "r16", "memory"
		);

		return buf_ptr-buffer;

		// a value of >=128 means "0", <127 means "1" bit.
}


int main (void)
{
	DDRC=0x00;
	PORTC=0x00;

	DDRB=0xFF;
	DDRD=0x00;
	PORTB=0x55;


	int temp=0;
	int n_received;

	while(1)
	{
		temp++;
		if (!(PIND & 0x08)) // check if uC is hung up
		{
			//PORTB=~(1 << ((temp>>9)%6));
			PORTB=~temp;
		}
		else if (!(PIND & 0x20)) // debug num_received
		{
			PORTB=~n_received>>3;
		}
		else if (!(PIND & 0x40)) // clear debug output
		{
			PORTB=~0x00;
		}
		else
		{
			// decode "buffer" and write button states to PORTB

			//PORTB=~buffer[4];
			unsigned char tmp2=0;
			for (int i=0;i<8;i++)
				tmp2|= ( (buffer[55-i]&0x80)?0:(1<<i) );

			char tmp=0;
			for (int i=1;i<8;i++)
				tmp|= ( (buffer[i]&0x80)?0:(1<<i) );

			if (  (temp & 0xFF) > tmp2 )
				tmp|=1;

			PORTB=~tmp;
		}
               
		_delay_ms(0.3);

	char foo[] = { 0x40, 0x03, 0x02 };

	if (!(PIND & 0x10))
		foo[2]=0x03;

	n_received=send_recv_gc(foo, 3);


	}


	return 0; // never reached
}