OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [oc/] [orpmon/] [common/] [common.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 809 simons
#include "common.h"
2
#include "uart.h"
3
#include "screen.h"
4
#include "support.h"
5
 
6
#define MAX_COMMANDS  100
7
 
8
bd_t bd;
9
 
10
int num_commands = 0;
11
 
12
struct command_struct {
13
  const char *name;
14
  const char *params;
15
  const char *help;
16
  int (*func)(int argc, char *argv[]);
17
} command[MAX_COMMANDS];
18
 
19
unsigned long strtoul(char *s)
20
{
21
  int base = 10;
22
  char *ptmp;
23
  unsigned long val = 0;
24
  unsigned long digit = 1;
25
 
26
  ptmp = s;
27
  while (*ptmp != '\0')
28
    if (*ptmp++ == 'x')
29
      base = 16;
30
 
31
  while (ptmp-- != s)
32
    if ((*ptmp >= '0') &&
33
        (*ptmp <= '9'))
34
    {
35
        val += (*ptmp - '0') * digit;
36
        digit *= base;
37
    }
38
    else
39
    if ((*ptmp >= 'a') &&
40
        (*ptmp <= 'f'))
41
    {
42
        val += (*ptmp - 'a' + 10) * digit;
43
        digit *= base;
44
    }
45
 
46
  return val;
47
}
48
 
49
void putc (const char c)
50
{
51
  debug ("getc %i, %i = %c\n", bd.bi_console_type, c, c);
52
  switch (bd.bi_console_type) {
53
  case CT_NONE:
54
    break;
55
  case CT_UART:
56
    uart_putc (c);
57
    break;
58
  case CT_CRT:
59
    screen_putc (c);
60
    break;
61
  case CT_SIM:
62
    __printf ("%c", c);
63
    break;
64
  }
65
}
66
 
67
int getc ()
68
{
69
  debug ("getc %i\n", bd.bi_console_type);
70
  switch (bd.bi_console_type) {
71
  case CT_NONE:
72
  case CT_CRT:
73
  case CT_SIM:
74
    return -1;
75
  case CT_UART:
76
    return uart_getc ();
77
  }
78
  return -1;
79
}
80
 
81
int testc ()
82
{
83
  debug ("testc %i\n", bd.bi_console_type);
84
  switch (bd.bi_console_type) {
85
  case CT_NONE:
86
  case CT_CRT:
87
  case CT_SIM:
88
    return -1;
89
  case CT_UART:
90
    return uart_testc ();
91
  }
92
  return -1;
93
}
94
 
95
int ctrlc ()
96
{
97
  if (testc ()) {
98
    switch (getc ()) {
99
      case 0x03:    /* ^C - Control C */
100
        return 1;
101
      default:
102
        break;
103
    }
104
  }
105
  return 0;
106
}
107
 
108
void change_console_type (enum bi_console_type_t con_type)
109
{
110
  debug ("Console change %i -> %i\n", bd.bi_console_type, con_type);
111
  /* Close previous */
112
  switch (bd.bi_console_type) {
113
  case CT_NONE:
114
  case CT_UART:
115
  case CT_CRT:
116
  case CT_SIM:
117
    break;
118
  }
119
  bd.bi_console_type = con_type;
120
  /* Initialize new */
121
  switch (bd.bi_console_type) {
122
  case CT_NONE:
123
    break;
124
  case CT_UART:
125
    uart_init ();
126
    break;
127
  case CT_CRT:
128
    screen_init ();
129
    break;
130
  case CT_SIM:
131
    break;
132
  }
133
}
134
 
135
void register_command_func (const char *name, const char *params, const char *help, int (*func)(int argc, char *argv[]))
136
{
137
  debug ("register_command '%s'\n", name);
138
  if (num_commands < MAX_COMMANDS) {
139
    command[num_commands].name = name;
140
    command[num_commands].params = params;
141
    command[num_commands].help = help;
142
    command[num_commands].func = func;
143
    num_commands++;
144
  } else printf ("Command '%s' ignored; MAX_COMMANDS limit reached\n", name);
145
}
146
 
147
/* Process command and arguments by executing
148
   specific function. */
149
void mon_command(void)
150
{
151
  char c = '\0';
152
  char str[1000];
153
  char *pstr = str;
154
  char *command_str;
155
  char *argv[20];
156
  int argc = 0;
157
  int end = 0;
158
 
159
  /* Show prompt */
160
#ifdef XESS
161
  printf ("\norp-xsv> ");
162
#else
163
  printf ("\nbender> ");
164
#endif
165
 
166
  /* Get characters from UART */
167
  c = getc();
168
  while (c != '\r' && c != '\f' && c != '\n')
169
  {
170
    if (c == '\b')
171
      pstr--;
172
    else
173
      *pstr++ = c;
174
    putc(c);
175
    c = getc();
176
  }
177
  *pstr = '\0';
178
  printf ("\n");
179
 
180
  /* Skip leading blanks */
181
  pstr = str;
182
  while (*pstr == ' ' && *pstr != '\0') pstr++;
183
 
184
  /* Get command from the string */
185
  command_str = pstr;
186
  while (*pstr != '\0' && *pstr != ' ') pstr++;
187
  if (*pstr == '\0') end = 1;
188
  *pstr = '\0';
189
 
190
  while (!end) {
191
    /* Go to next argument */
192
    while (*pstr == ' ' && *pstr != '\0') pstr++;
193
    if (*pstr) argv[argc++] = pstr;
194
    else end = 1;
195
  }
196
 
197
  {
198
    int i, found = 0;
199
    for (i = 0; i < num_commands; i++)
200
      if (strcmp (command_str, command[i].name) == 0) {
201
        switch (command[i].func (argc, &argv[0])) {
202
        case -1:
203
          printf ("Missing/wrong parameters, usage: %s %s\n", command[i].name, command[i].params);
204
          break;
205
        }
206
        found = 1;
207
        break;
208
      }
209
    if (!found) printf ("Unknown command. Type 'help' for help.\n");
210
  }
211
}
212
 
213
#if HELP_ENABLED
214
/* Displays help screen */
215
int help_cmd (int argc, char *argv[])
216
{
217
  int i;
218
  for (i = 0; i < num_commands; i++)
219
    printf ("%-10s %-20s - %s\n", command[i].name, command[i].params, command[i].help);
220
  return 0;
221
}
222
#endif /* HELP_ENABLED */
223
 
224
void module_cpu_init (void);
225
void module_memory_init (void);
226
void module_eth_init (void);
227
//void module_dhry_init (void);
228
void module_camera_init (void);
229
void module_tftp_init (void);
230
void tick_init(void);
231
 
232
/* List of all initializations */
233
void mon_init (void)
234
{
235
  module_cpu_init ();
236
  module_memory_init ();
237
  module_eth_init ();
238
//  module_dhry_init ();
239
  module_camera_init ();
240
  module_tftp_init ();
241
 
242
//  tick_init();
243
}
244
 
245
/* Main shell loop */
246
int main(int argc, char **argv)
247
{
248
  /* Initialize controller */
249
  change_console_type (CT_UART);
250
  mon_init ();
251
 
252
  if (HELP_ENABLED) register_command ("help", "", "shows this help", help_cmd);
253
 
254
#ifdef XESS
255
  printf ("\nORP-XSV Monitor (type 'help' for help)\n");
256
#else
257
  printf ("\nBender Monitor (type 'help' for help)\n");
258
#endif
259
 
260
  while(1) mon_command();
261
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.