1
1
#include <stdio.h>
2
- #include <stdlib.h>
3
- #include <string.h>
4
- #include <stdarg.h>
5
- #include <crc.h>
6
-
7
2
#include <irq.h>
8
3
#include <uart.h>
9
- #include <console.h>
10
4
#include <system.h>
11
- #include <hw/common.h>
12
- #include <generated/csr.h>
13
5
6
+ #include "corecom.h"
14
7
#include "elf_loader.h"
15
-
16
- enum {
17
- MSGTYPE_REQUEST_IDENT = 0x01 ,
18
- MSGTYPE_LOAD_KERNEL = 0x02 ,
19
- MSGTYPE_KERNEL_FINISHED = 0x03 ,
20
- MSGTYPE_RPC_REQUEST = 0x04 ,
21
- };
22
-
23
- static int receive_int (void )
24
- {
25
- unsigned int r ;
26
- int i ;
27
-
28
- r = 0 ;
29
- for (i = 0 ;i < 4 ;i ++ ) {
30
- r <<= 8 ;
31
- r |= (unsigned char )uart_read ();
32
- }
33
- return r ;
34
- }
35
-
36
- static char receive_char (void )
37
- {
38
- return uart_read ();
39
- }
40
-
41
- static void send_int (int x )
42
- {
43
- int i ;
44
-
45
- for (i = 0 ;i < 4 ;i ++ ) {
46
- uart_write ((x & 0xff000000 ) >> 24 );
47
- x <<= 8 ;
48
- }
49
- }
50
-
51
- static void send_sint (short int i )
52
- {
53
- uart_write ((i >> 8 ) & 0xff );
54
- uart_write (i & 0xff );
55
- }
56
-
57
- static void send_char (char c )
58
- {
59
- uart_write (c );
60
- }
61
-
62
- static void receive_sync (void )
63
- {
64
- char c ;
65
- int recognized ;
66
-
67
- recognized = 0 ;
68
- while (recognized < 4 ) {
69
- c = uart_read ();
70
- if (c == 0x5a )
71
- recognized ++ ;
72
- else
73
- recognized = 0 ;
74
- }
75
- }
76
-
77
- static void send_sync (void )
78
- {
79
- send_int (0x5a5a5a5a );
80
- }
81
-
82
- static int ident_and_download_kernel (void * buffer , int maxlength )
83
- {
84
- int length ;
85
- unsigned int crc ;
86
- int i ;
87
- char msgtype ;
88
- unsigned char * _buffer = buffer ;
89
-
90
- while (1 ) {
91
- receive_sync ();
92
- msgtype = receive_char ();
93
- if (msgtype == MSGTYPE_REQUEST_IDENT ) {
94
- send_int (0x41524f52 ); /* "AROR" - ARTIQ runtime on OpenRISC */
95
- send_int (1000000000000LL /identifier_frequency_read ()); /* RTIO clock period in picoseconds */
96
- } else if (msgtype == MSGTYPE_LOAD_KERNEL ) {
97
- length = receive_int ();
98
- if (length > maxlength ) {
99
- send_char (0x4c ); /* Incorrect length */
100
- return -1 ;
101
- }
102
- crc = receive_int ();
103
- for (i = 0 ;i < length ;i ++ )
104
- _buffer [i ] = receive_char ();
105
- if (crc32 (buffer , length ) != crc ) {
106
- send_char (0x43 ); /* CRC failed */
107
- return -1 ;
108
- }
109
- send_char (0x4f ); /* kernel reception OK */
110
- return length ;
111
- } else
112
- return -1 ;
113
- }
114
- }
115
-
116
- static int rpc (int rpc_num , int n_args , ...)
117
- {
118
- send_sync ();
119
- send_char (MSGTYPE_RPC_REQUEST );
120
- send_sint (rpc_num );
121
- send_char (n_args );
122
-
123
- va_list args ;
124
- va_start (args , n_args );
125
- while (n_args -- )
126
- send_int (va_arg (args , int ));
127
- va_end (args );
128
-
129
- return receive_int ();
130
- }
131
-
132
- static void gpio_set (int channel , int value )
133
- {
134
- static int csr_value ;
135
-
136
- if (value )
137
- csr_value |= 1 << channel ;
138
- else
139
- csr_value &= ~(1 << channel );
140
- leds_out_write (csr_value );
141
- }
142
-
143
- static void rtio_set (long long int timestamp , int channel , int value )
144
- {
145
- rtio_reset_write (0 );
146
- rtio_chan_sel_write (channel );
147
- rtio_o_timestamp_write (timestamp );
148
- rtio_o_value_write (value );
149
- while (!rtio_o_writable_read ());
150
- rtio_o_we_write (1 );
151
- }
152
-
153
- static void rtio_sync (int channel )
154
- {
155
- rtio_chan_sel_write (channel );
156
- while (rtio_o_level_read () != 0 );
157
- }
158
-
159
- #define DDS_FTW0 0x0a
160
- #define DDS_FTW1 0x0b
161
- #define DDS_FTW2 0x0c
162
- #define DDS_FTW3 0x0d
163
- #define DDS_FUD 0x40
164
- #define DDS_GPIO 0x41
165
-
166
- #define DDS_READ (addr ) \
167
- MMPTR(0xb0000000 + (addr)*4)
168
-
169
- #define DDS_WRITE (addr , data ) \
170
- MMPTR(0xb0000000 + (addr)*4) = data
171
-
172
- static void dds_program (int channel , int ftw )
173
- {
174
- DDS_WRITE (DDS_GPIO , channel );
175
- DDS_WRITE (DDS_FTW0 , ftw & 0xff );
176
- DDS_WRITE (DDS_FTW1 , (ftw >> 8 ) & 0xff );
177
- DDS_WRITE (DDS_FTW2 , (ftw >> 16 ) & 0xff );
178
- DDS_WRITE (DDS_FTW3 , (ftw >> 24 ) & 0xff );
179
- DDS_WRITE (DDS_FUD , 0 );
180
- }
181
-
182
- static const struct symbol syscalls [] = {
183
- {"__syscall_rpc" , rpc },
184
- {"__syscall_gpio_set" , gpio_set },
185
- {"__syscall_rtio_set" , rtio_set },
186
- {"__syscall_rtio_sync" , rtio_sync },
187
- {"__syscall_dds_program" , dds_program },
188
- {NULL , NULL }
189
- };
190
-
191
- static void dds_init (void )
192
- {
193
- int i ;
194
-
195
- DDS_WRITE (DDS_GPIO , 1 << 7 );
196
-
197
- for (i = 0 ;i < 8 ;i ++ ) {
198
- DDS_WRITE (DDS_GPIO , i );
199
- DDS_WRITE (0x00 , 0x78 );
200
- DDS_WRITE (0x01 , 0x00 );
201
- DDS_WRITE (0x02 , 0x00 );
202
- DDS_WRITE (0x03 , 0x00 );
203
- DDS_WRITE (DDS_FUD , 0 );
204
- }
205
- }
8
+ #include "symbols.h"
9
+ #include "rtio.h"
10
+ #include "dds.h"
206
11
207
12
typedef void (* kernel_function )(void );
208
13
@@ -222,16 +27,15 @@ int main(void)
222
27
while (1 ) {
223
28
length = ident_and_download_kernel (kbuf , sizeof (kbuf ));
224
29
if (length > 0 ) {
225
- if (load_elf (syscalls , kbuf , length , kcode , sizeof (kcode ))) {
226
- flush_cpu_icache ();
30
+ if (load_elf (resolve_symbol , kbuf , length , kcode , sizeof (kcode ))) {
31
+ rtio_init ();
227
32
dds_init ();
33
+ flush_cpu_icache ();
228
34
k ();
229
- rtio_reset_write (1 );
230
- send_sync ();
231
- send_char (MSGTYPE_KERNEL_FINISHED );
35
+ kernel_finished ();
232
36
}
233
37
}
234
38
}
235
-
39
+
236
40
return 0 ;
237
41
}
0 commit comments