Rev 8331 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8327 | maxcodehac | 1 | #include "SoC.h" |
2 | |||
3 | #include |
||
4 | #include |
||
5 | #include |
||
6 | #include |
||
7 | #include |
||
8 | #include |
||
9 | #include |
||
10 | #include |
||
11 | //#include |
||
12 | #include |
||
13 | //#include |
||
8518 | maxcodehac | 14 | #include |
8327 | maxcodehac | 15 | |
16 | #define getch2 con_getch |
||
17 | #define cprintf printf |
||
18 | |||
19 | #define off64_t off_t |
||
20 | unsigned char* readFile(const char* name, UInt32* lenP){ |
||
21 | |||
22 | long len = 0; |
||
23 | unsigned char *r = NULL; |
||
24 | int i; |
||
25 | FILE* f; |
||
26 | |||
27 | f = fopen(name, "r"); |
||
28 | if(!f){ |
||
29 | perror("cannot open file"); |
||
30 | return NULL; |
||
31 | } |
||
32 | |||
33 | i = fseek(f, 0, SEEK_END); |
||
34 | if(i){ |
||
35 | return NULL; |
||
36 | perror("cannot seek to end"); |
||
37 | } |
||
38 | |||
39 | len = ftell(f); |
||
40 | if(len < 0){ |
||
41 | perror("cannot get position"); |
||
42 | return NULL; |
||
43 | } |
||
44 | |||
45 | i = fseek(f, 0, SEEK_SET); |
||
46 | if(i){ |
||
47 | return NULL; |
||
48 | perror("cannot seek to start"); |
||
49 | } |
||
50 | |||
51 | |||
52 | r = malloc(len); |
||
53 | if(!r){ |
||
54 | perror("cannot alloc memory"); |
||
55 | return NULL; |
||
56 | } |
||
57 | |||
58 | if(len != (long)fread(r, 1, len, f)){ |
||
59 | perror("canot read file"); |
||
60 | free(r); |
||
61 | return NULL; |
||
62 | } |
||
63 | |||
64 | *lenP = len; |
||
65 | return r; |
||
66 | } |
||
67 | |||
68 | |||
69 | |||
70 | static int ctlCSeen = 0; |
||
71 | |||
72 | static int readchar(void){ |
||
73 | |||
74 | struct timeval tv; |
||
75 | fd_set set; |
||
76 | char c; |
||
77 | char tmp[20]; |
||
78 | int ret = CHAR_NONE; |
||
79 | |||
80 | // if(ctlCSeen){ |
||
81 | //ctlCSeen = 0; |
||
82 | //return 0x03; |
||
83 | //} |
||
84 | |||
85 | //tv.tv_sec = 0; |
||
86 | //tv.tv_usec = 0; |
||
87 | |||
88 | //FD_ZERO(&set); |
||
89 | //FD_SET(0, &set); |
||
90 | |||
91 | //i = 1; //(1, &set, NULL, NULL, &tv); |
||
92 | if(con_kbhit()){ |
||
93 | |||
94 | ret = getch2(); |
||
95 | if (ret==0xD) {ret=0xA;} |
||
96 | } |
||
97 | |||
98 | |||
99 | |||
100 | return ret; |
||
101 | } |
||
102 | |||
103 | static void writechar(int chr){ |
||
104 | |||
105 | if(!(chr & 0xFF00)){ |
||
106 | |||
107 | cprintf("%c", chr); |
||
108 | } |
||
109 | else{ |
||
110 | cprintf("<<~~ EC_0x%x ~~>>", chr); |
||
111 | } |
||
112 | fflush(stdout); |
||
113 | } |
||
114 | |||
115 | void ctl_cHandler(_UNUSED_ int v){ //handle SIGTERM |
||
116 | |||
117 | // exit(-1); |
||
118 | ctlCSeen = 1; |
||
119 | } |
||
120 | |||
121 | int rootOps(void* userData, UInt32 sector, void* buf, UInt8 op){ |
||
122 | |||
123 | FILE* root = userData; |
||
124 | int i; |
||
125 | |||
126 | switch(op){ |
||
127 | case BLK_OP_SIZE: |
||
128 | |||
129 | if(sector == 0){ //num blocks |
||
130 | |||
131 | if(root){ |
||
132 | |||
133 | i = fseek(root, 0, SEEK_END); |
||
134 | if(i) return false; |
||
135 | |||
136 | *(unsigned long*)buf = (off64_t)ftell(root) / (off64_t)BLK_DEV_BLK_SZ; |
||
137 | } |
||
138 | else{ |
||
139 | |||
140 | *(unsigned long*)buf = 0; |
||
141 | } |
||
142 | } |
||
143 | else if(sector == 1){ //block size |
||
144 | |||
145 | *(unsigned long*)buf = BLK_DEV_BLK_SZ; |
||
146 | } |
||
147 | else return 0; |
||
148 | return 1; |
||
149 | |||
150 | case BLK_OP_READ: |
||
151 | |||
152 | i = fseek(root, (off64_t)sector * (off64_t)BLK_DEV_BLK_SZ, SEEK_SET); |
||
153 | if(i) return false; |
||
154 | return fread(buf, 1, BLK_DEV_BLK_SZ, root) == BLK_DEV_BLK_SZ; |
||
155 | |||
156 | case BLK_OP_WRITE: |
||
157 | |||
158 | i = fseek(root, (off64_t)sector * (off64_t)BLK_DEV_BLK_SZ, SEEK_SET); |
||
159 | if(i) return false; |
||
160 | return fwrite(buf, 1, BLK_DEV_BLK_SZ, root) == BLK_DEV_BLK_SZ; |
||
161 | } |
||
162 | return 0; |
||
163 | } |
||
164 | |||
165 | SoC soc; |
||
166 | |||
167 | int main(int argc, char** argv){ |
||
168 | load_console(); |
||
169 | con_set_title("uARM"); |
||
170 | //CONSOLE_INIT("CONSOLE"); |
||
171 | |||
172 | /* |
||
173 | struct termios cfg, old; |
||
174 | */ |
||
175 | FILE* root = NULL; |
||
176 | int gdbPort = 0; |
||
177 | |||
178 | if(argc != 3 && argc != 2){ |
||
8331 | maxcodehac | 179 | fprintf(stdout,"usage: %s path_to_disk [gdbPort]\n", argv[0]); |
180 | return 0; |
||
8327 | maxcodehac | 181 | } |
182 | |||
183 | //setup the terminal |
||
184 | { |
||
185 | int ret; |
||
186 | |||
187 | ret = 0; //tcgetattr(0, &old); |
||
188 | //cfg = old; |
||
189 | if(ret) perror("cannot get term attrs"); |
||
190 | |||
191 | /* #ifndef _DARWIN_ |
||
192 | |||
193 | cfg.c_iflag &=~ (INLCR | INPCK | ISTRIP | IUCLC | IXANY | IXOFF | IXON); |
||
194 | cfg.c_oflag &=~ (OPOST | OLCUC | ONLCR | OCRNL | ONOCR | ONLRET); |
||
195 | cfg.c_lflag &=~ (ECHO | ECHOE | ECHONL | ICANON | IEXTEN | XCASE); |
||
196 | #else */ |
||
197 | // cfmakeraw(&cfg); |
||
198 | //#endif |
||
199 | |||
200 | ret = 0; //tcsetattr(0, TCSANOW, &cfg); |
||
201 | if(ret) perror("cannot set term attrs"); |
||
202 | } |
||
203 | |||
204 | root = fopen(argv[1], "r+b"); |
||
205 | if(!root){ |
||
206 | fprintf(stderr,"Failed to open root device\n"); |
||
207 | exit(-1); |
||
208 | } |
||
209 | |||
210 | fprintf(stderr,"Stderror ready\n"); |
||
211 | |||
212 | if(argc >= 3) gdbPort = atoi(argv[2]); |
||
213 | |||
214 | socInit(&soc, socRamModeAlloc, NULL, readchar, writechar, rootOps, root); |
||
215 | //(SIGINT, &ctl_cHandler); |
||
216 | socRun(&soc, gdbPort); |
||
217 | |||
218 | fclose(root); |
||
219 | //tcsetattr(0, TCSANOW, &old); |
||
220 | |||
221 | return 0; |
||
222 | } |
||
223 | |||
224 | |||
225 | //////// runtime things |
||
226 | |||
227 | void* emu_alloc(UInt32 size){ |
||
228 | |||
229 | return calloc(size,1); |
||
230 | } |
||
231 | |||
232 | void emu_free(void* ptr){ |
||
233 | |||
234 | free(ptr); |
||
235 | } |
||
236 | |||
237 | UInt32 rtcCurTime(void){ |
||
238 | |||
239 | struct timeval tv; |
||
240 | |||
241 | gettimeofday(&tv, NULL); |
||
242 | |||
243 | return tv.tv_sec; |
||
244 | } |
||
245 | |||
246 | void err_str(const char* str){ |
||
247 | |||
248 | printf( "%s", str); |
||
249 | }~~><~~>> |
||
250 |