about summary refs log tree commit diff stats
path: root/source/link.c
blob: c6270398f5339690dba9399da81ec777b1a20960 (plain) (blame)
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
/*
 * Copyright (C) 2017 hatkirby
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */
#include "link.h"
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>

//from my tests 50us seems to be the lowest
//safe si transfer delay in between calls
#define SI_TRANS_DELAY 500

static u8* resbuf;
static u8* cmdbuf;

void initLink()
{
  cmdbuf = memalign(32,32);
  resbuf = memalign(32,32);
}

static volatile u32 transval = 0;
void transcb(s32 chan, u32 ret)
{
  transval = 1;
}

static volatile u32 resval = 0;
void acb(s32 res, u32 val)
{
  resval = val;
}

void doreset()
{
  cmdbuf[0] = 0xFF; //reset
  transval = 0;
  SI_Transfer(1, cmdbuf, 1, resbuf, 3, transcb, SI_TRANS_DELAY);

  while (transval == 0);
}

void getstatus()
{
  cmdbuf[0] = 0; //status
  transval = 0;
  SI_Transfer(1, cmdbuf, 1, resbuf, 3, transcb, SI_TRANS_DELAY);

  while (transval == 0);
}

u32 recv()
{
  memset(resbuf,0,32);
  cmdbuf[0]=0x14; //read
  transval = 0;
  SI_Transfer(1, cmdbuf, 1, resbuf, 5, transcb, SI_TRANS_DELAY);

  while (transval == 0);
  //printf("%08lx\n", *(vu32*)resbuf);
  return *(vu32*)resbuf;
}

void send(u32 msg)
{
  cmdbuf[0] = 0x15;
  cmdbuf[1] = (msg >> 0) & 0xFF;
  cmdbuf[2] = (msg >> 8) & 0xFF;
  cmdbuf[3] = (msg >> 16) & 0xFF;
  cmdbuf[4] = (msg >> 24) & 0xFF;

  transval = 0;
  resbuf[0] = 0;
  SI_Transfer(1, cmdbuf, 5, resbuf, 1, transcb, SI_TRANS_DELAY);

  while (transval == 0);
}

u32 getMsg()
{
  u32 val = __builtin_bswap32(recv());
  send(0);

  return val;
}

void getMsgArr(u32* arr, int len)
{
  for (int i=0; i<len; i++)
  {
    *(vu32*)(arr+i) = recv();
  }
}

void sendMsg(u32 msg)
{
  send(msg);
}

void waitForGBA()
{
  resval = 0;

  do
  {
    SI_GetTypeAsync(1, acb);

    for (;;)
    {
      if (resval)
      {
        if (resval == 0x80 || resval & 8)
        {
          resval = 0;
          SI_GetTypeAsync(1, acb);
        } else if (resval)
        {
          break;
        }
      }
    }
  } while (!(resval & SI_GBA));
}

void waitForBIOS()
{
  resbuf[2]=0;

  do
  {
    doreset();
  } while (!(resbuf[1] > 4));
}

void waitForGame()
{
  do
  {
    doreset();
  } while ((resbuf[0] != 0) || !(resbuf[2] & 0x10));
}

void waitForAck()
{
  while (recv() != 0) {sleep(1);}
}