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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [orpmon/] [common/] [common.c] - Blame information for rev 816

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

powered by: WebSVN 2.1.0

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