aboutsummaryrefslogtreecommitdiff
path: root/gdb/remote-array.c
blob: 498c21820274c72ae046dad0f33df85dcf12b858 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
/* Remote debugging interface for Array Tech RAID controller..
   Copyright 90, 91, 92, 93, 94, 1995, 1998  Free Software Foundation, Inc.
   Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.

   This module talks to a debug monitor called 'MONITOR', which
   We communicate with MONITOR via either a direct serial line, or a TCP
   (or possibly TELNET) stream to a terminal multiplexor,
   which in turn talks to the target board.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
 */

#include "defs.h"
#include "gdbcore.h"
#include "target.h"
#include "gdb_wait.h"
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include "gdb_string.h"
#include "command.h"
#include "serial.h"
#include "monitor.h"
#include "remote-utils.h"
#include "inferior.h"
#include "version.h"

extern int baud_rate;

#define ARRAY_PROMPT ">> "

#define SWAP_TARGET_AND_HOST(buffer,len) 				\
  do									\
    {									\
      if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER)				\
	{								\
	  char tmp;							\
	  char *p = (char *)(buffer);					\
	  char *q = ((char *)(buffer)) + len - 1;		   	\
	  for (; p < q; p++, q--)				 	\
	    {								\
	      tmp = *q;							\
	      *q = *p;							\
	      *p = tmp;							\
	    }								\
	}								\
    }									\
  while (0)

static void debuglogs (int, char *, ...);
static void array_open ();
static void array_close ();
static void array_detach ();
static void array_attach ();
static void array_resume ();
static void array_fetch_register ();
static void array_store_register ();
static void array_fetch_registers ();
static void array_store_registers ();
static void array_prepare_to_store ();
static void array_files_info ();
static void array_kill ();
static void array_create_inferior ();
static void array_mourn_inferior ();
static void make_gdb_packet ();
static int array_xfer_memory ();
static int array_wait ();
static int array_insert_breakpoint ();
static int array_remove_breakpoint ();
static int tohex ();
static int to_hex ();
static int from_hex ();
static int array_send_packet ();
static int array_get_packet ();
static unsigned long ascii2hexword ();
static void hexword2ascii ();

#define LOG_FILE "monitor.log"
#if defined (LOG_FILE)
FILE *log_file;
#endif

static int timeout = 30;
/* Having this larger than 400 causes us to be incompatible with m68k-stub.c
   and i386-stub.c.  Normally, no one would notice because it only matters
   for writing large chunks of memory (e.g. in downloads).  Also, this needs
   to be more than 400 if required to hold the registers (see below, where
   we round it up based on REGISTER_BYTES).  */
#define PBUFSIZ 400

/* 
 * Descriptor for I/O to remote machine.  Initialize it to NULL so that
 * array_open knows that we don't have a file open when the program starts.
 */
serial_t array_desc = NULL;

/*
 * this array of registers need to match the indexes used by GDB. The
 * whole reason this exists is cause the various ROM monitors use
 * different strings than GDB does, and doesn't support all the
 * registers either. So, typing "info reg sp" becomes a "r30".
 */
extern char *tmp_mips_processor_type;
extern int mips_set_processor_type ();

static struct target_ops array_ops;

static void
init_array_ops (void)
{
  array_ops.to_shortname = "array";
  array_ops.to_longname =
    "Debug using the standard GDB remote protocol for the Array Tech target.",
    array_ops.to_doc =
    "Debug using the standard GDB remote protocol for the Array Tech target.\n\
Specify the serial device it is connected to (e.g. /dev/ttya).";
  array_ops.to_open = array_open;
  array_ops.to_close = array_close;
  array_ops.to_attach = NULL;
  array_ops.to_post_attach = NULL;
  array_ops.to_require_attach = NULL;
  array_ops.to_detach = array_detach;
  array_ops.to_require_detach = NULL;
  array_ops.to_resume = array_resume;
  array_ops.to_wait = array_wait;
  array_ops.to_post_wait = NULL;
  array_ops.to_fetch_registers = array_fetch_registers;
  array_ops.to_store_registers = array_store_registers;
  array_ops.to_prepare_to_store = array_prepare_to_store;
  array_ops.to_xfer_memory = array_xfer_memory;
  array_ops.to_files_info = array_files_info;
  array_ops.to_insert_breakpoint = array_insert_breakpoint;
  array_ops.to_remove_breakpoint = array_remove_breakpoint;
  array_ops.to_terminal_init = 0;
  array_ops.to_terminal_inferior = 0;
  array_ops.to_terminal_ours_for_output = 0;
  array_ops.to_terminal_ours = 0;
  array_ops.to_terminal_info = 0;
  array_ops.to_kill = array_kill;
  array_ops.to_load = 0;
  array_ops.to_lookup_symbol = 0;
  array_ops.to_create_inferior = array_create_inferior;
  array_ops.to_post_startup_inferior = NULL;
  array_ops.to_acknowledge_created_inferior = NULL;
  array_ops.to_clone_and_follow_inferior = NULL;
  array_ops.to_post_follow_inferior_by_clone = NULL;
  array_ops.to_insert_fork_catchpoint = NULL;
  array_ops.to_remove_fork_catchpoint = NULL;
  array_ops.to_insert_vfork_catchpoint = NULL;
  array_ops.to_remove_vfork_catchpoint = NULL;
  array_ops.to_has_forked = NULL;
  array_ops.to_has_vforked = NULL;
  array_ops.to_can_follow_vfork_prior_to_exec = NULL;
  array_ops.to_post_follow_vfork = NULL;
  array_ops.to_insert_exec_catchpoint = NULL;
  array_ops.to_remove_exec_catchpoint = NULL;
  array_ops.to_has_execd = NULL;
  array_ops.to_reported_exec_events_per_exec_call = NULL;
  array_ops.to_has_exited = NULL;
  array_ops.to_mourn_inferior = array_mourn_inferior;
  array_ops.to_can_run = 0;
  array_ops.to_notice_signals = 0;
  array_ops.to_thread_alive = 0;
  array_ops.to_stop = 0;
  array_ops.to_pid_to_exec_file = NULL;
  array_ops.to_core_file_to_sym_file = NULL;
  array_ops.to_stratum = process_stratum;
  array_ops.DONT_USE = 0;
  array_ops.to_has_all_memory = 1;
  array_ops.to_has_memory = 1;
  array_ops.to_has_stack = 1;
  array_ops.to_has_registers = 1;
  array_ops.to_has_execution = 1;
  array_ops.to_sections = 0;
  array_ops.to_sections_end = 0;
  array_ops.to_magic = OPS_MAGIC;
};

/*
 * printf_monitor -- send data to monitor.  Works just like printf.
 */
static void
printf_monitor (char *pattern,...)
{
  va_list args;
  char buf[PBUFSIZ];
  int i;

  va_start (args, pattern);

  vsprintf (buf, pattern, args);

  debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf);

  if (strlen (buf) > PBUFSIZ)
    error ("printf_monitor(): string too long");
  if (SERIAL_WRITE (array_desc, buf, strlen (buf)))
    fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
}
/*
 * write_monitor -- send raw data to monitor.
 */
static void
write_monitor (data, len)
     char data[];
     int len;
{
  if (SERIAL_WRITE (array_desc, data, len))
    fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));

  *(data + len + 1) = '\0';
  debuglogs (1, "write_monitor(), Sending: \"%s\".", data);

}

/*
 * debuglogs -- deal with debugging info to multiple sources. This takes
 *      two real args, the first one is the level to be compared against 
 *      the sr_get_debug() value, the second arg is a printf buffer and args
 *      to be formatted and printed. A CR is added after each string is printed.
 */
static void
debuglogs (int level, char *pattern,...)
{
  va_list args;
  char *p;
  unsigned char buf[PBUFSIZ];
  char newbuf[PBUFSIZ];
  int i;

  va_start (args, pattern);

  if ((level < 0) || (level > 100))
    {
      error ("Bad argument passed to debuglogs(), needs debug level");
      return;
    }

  vsprintf (buf, pattern, args);	/* format the string */

  /* convert some characters so it'll look right in the log */
  p = newbuf;
  for (i = 0; buf[i] != '\0'; i++)
    {
      if (i > PBUFSIZ)
	error ("Debug message too long");
      switch (buf[i])
	{
	case '\n':		/* newlines */
	  *p++ = '\\';
	  *p++ = 'n';
	  continue;
	case '\r':		/* carriage returns */
	  *p++ = '\\';
	  *p++ = 'r';
	  continue;
	case '\033':		/* escape */
	  *p++ = '\\';
	  *p++ = 'e';
	  continue;
	case '\t':		/* tab */
	  *p++ = '\\';
	  *p++ = 't';
	  continue;
	case '\b':		/* backspace */
	  *p++ = '\\';
	  *p++ = 'b';
	  continue;
	default:		/* no change */
	  *p++ = buf[i];
	}

      if (buf[i] < 26)
	{			/* modify control characters */
	  *p++ = '^';
	  *p++ = buf[i] + 'A';
	  continue;
	}
      if (buf[i] >= 128)
	{			/* modify control characters */
	  *p++ = '!';
	  *p++ = buf[i] + 'A';
	  continue;
	}
    }
  *p = '\0';			/* terminate the string */

  if (sr_get_debug () > level)
    printf_unfiltered ("%s\n", newbuf);

#ifdef LOG_FILE			/* write to the monitor log */
  if (log_file != 0x0)
    {
      fputs (newbuf, log_file);
      fputc ('\n', log_file);
      fflush (log_file);
    }
#endif
}

/* readchar -- read a character from the remote system, doing all the fancy
 *    timeout stuff.
 */
static int
readchar (int timeout)
{
  int c;

  c = SERIAL_READCHAR (array_desc, abs (timeout));

  if (sr_get_debug () > 5)
    {
      putchar (c & 0x7f);
      debuglogs (5, "readchar: timeout = %d\n", timeout);
    }

#ifdef LOG_FILE
  if (isascii (c))
    putc (c & 0x7f, log_file);
#endif

  if (c >= 0)
    return c & 0x7f;

  if (c == SERIAL_TIMEOUT)
    {
      if (timeout <= 0)
	return c;		/* Polls shouldn't generate timeout errors */
      error ("Timeout reading from remote system.");
#ifdef LOG_FILE
      fputs ("ERROR: Timeout reading from remote system", log_file);
#endif
    }
  perror_with_name ("readchar");
}

/* 
 * expect --  scan input from the remote system, until STRING is found.
 *      If DISCARD is non-zero, then discard non-matching input, else print
 *      it out. Let the user break out immediately.
 */
static void
expect (char *string, int discard)
{
  char *p = string;
  int c;


  debuglogs (1, "Expecting \"%s\".", string);

  immediate_quit = 1;
  while (1)
    {
      c = readchar (timeout);
      if (!isascii (c))
	continue;
      if (c == *p++)
	{
	  if (*p == '\0')
	    {
	      immediate_quit = 0;
	      debuglogs (4, "Matched");
	      return;
	    }
	}
      else
	{
	  if (!discard)
	    {
	      fputc_unfiltered (c, gdb_stdout);
	    }
	  p = string;
	}
    }
}

/* Keep discarding input until we see the MONITOR array_cmds->prompt.

   The convention for dealing with the expect_prompt is that you
   o give your command
   o *then* wait for the expect_prompt.

   Thus the last thing that a procedure does with the serial line
   will be an expect_prompt().  Exception:  array_resume does not
   wait for the expect_prompt, because the terminal is being handed over
   to the inferior.  However, the next thing which happens after that
   is a array_wait which does wait for the expect_prompt.
   Note that this includes abnormal exit, e.g. error().  This is
   necessary to prevent getting into states from which we can't
   recover.  */
static void
expect_prompt (int discard)
{
  expect (ARRAY_PROMPT, discard);
}

/*
 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
 */
static int
junk (char ch)
{
  switch (ch)
    {
    case '\0':
    case ' ':
    case '-':
    case '\t':
    case '\r':
    case '\n':
      if (sr_get_debug () > 5)
	debuglogs (5, "Ignoring \'%c\'.", ch);
      return 1;
    default:
      if (sr_get_debug () > 5)
	debuglogs (5, "Accepting \'%c\'.", ch);
      return 0;
    }
}

/* 
 *  get_hex_digit -- Get a hex digit from the remote system & return its value.
 *              If ignore is nonzero, ignore spaces, newline & tabs.
 */
static int
get_hex_digit (int ignore)
{
  static int ch;
  while (1)
    {
      ch = readchar (timeout);
      if (junk (ch))
	continue;
      if (sr_get_debug () > 4)
	{
	  debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch);
	}
      else
	{
#ifdef LOG_FILE			/* write to the monitor log */
	  if (log_file != 0x0)
	    {
	      fputs ("get_hex_digit() got a 0x", log_file);
	      fputc (ch, log_file);
	      fputc ('\n', log_file);
	      fflush (log_file);
	    }
#endif
	}

      if (ch >= '0' && ch <= '9')
	return ch - '0';
      else if (ch >= 'A' && ch <= 'F')
	return ch - 'A' + 10;
      else if (ch >= 'a' && ch <= 'f')
	return ch - 'a' + 10;
      else if (ch == ' ' && ignore)
	;
      else
	{
	  expect_prompt (1);
	  debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch);
	  error ("Invalid hex digit from remote system. (0x%x)", ch);
	}
    }
}

/* get_hex_byte -- Get a byte from monitor and put it in *BYT. 
 *    Accept any number leading spaces.
 */
static void
get_hex_byte (char *byt)
{
  int val;

  val = get_hex_digit (1) << 4;
  debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val);

  val |= get_hex_digit (0);
  debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val);
  *byt = val;

  debuglogs (4, "get_hex_byte() -- Read a 0x%x", val);
}

/* 
 * get_hex_word --  Get N 32-bit words from remote, each preceded by a space,
 *      and put them in registers starting at REGNO.
 */
static int
get_hex_word (void)
{
  long val, newval;
  int i;

  val = 0;

#if 0
  if (HOST_BYTE_ORDER == BIG_ENDIAN)
    {
#endif
      for (i = 0; i < 8; i++)
	val = (val << 4) + get_hex_digit (i == 0);
#if 0
    }
  else
    {
      for (i = 7; i >= 0; i--)
	val = (val << 4) + get_hex_digit (i == 0);
    }
#endif

  debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val, (HOST_BYTE_ORDER == BIG_ENDIAN) ? "big endian" : "little endian");

  return val;
}

/* This is called not only when we first attach, but also when the
   user types "run" after having attached.  */
static void
array_create_inferior (char *execfile, char *args, char **env)
{
  int entry_pt;

  if (args && *args)
    error ("Can't pass arguments to remote MONITOR process");

  if (execfile == 0 || exec_bfd == 0)
    error ("No executable file specified");

  entry_pt = (int) bfd_get_start_address (exec_bfd);

/* The "process" (board) is already stopped awaiting our commands, and
   the program is already downloaded.  We just set its PC and go.  */

  clear_proceed_status ();

  /* Tell wait_for_inferior that we've started a new process.  */
  init_wait_for_inferior ();

  /* Set up the "saved terminal modes" of the inferior
     based on what modes we are starting it with.  */
  target_terminal_init ();

  /* Install inferior's terminal modes.  */
  target_terminal_inferior ();

  /* insert_step_breakpoint ();  FIXME, do we need this?  */

  /* Let 'er rip... */
  proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0);
}

/*
 * array_open -- open a connection to a remote debugger.
 *      NAME is the filename used for communication.
 */
static int baudrate = 9600;
static char dev_name[100];

static void
array_open (char *args, char *name, int from_tty)
{
  char packet[PBUFSIZ];

  if (args == NULL)
    error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
`target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);

/*  if (is_open) */
  array_close (0);

  target_preopen (from_tty);
  unpush_target (&array_ops);

  tmp_mips_processor_type = "lsi33k";	/* change the default from r3051 */
  mips_set_processor_type_command ("lsi33k", 0);

  strcpy (dev_name, args);
  array_desc = SERIAL_OPEN (dev_name);

  if (array_desc == NULL)
    perror_with_name (dev_name);

  if (baud_rate != -1)
    {
      if (SERIAL_SETBAUDRATE (array_desc, baud_rate))
	{
	  SERIAL_CLOSE (array_desc);
	  perror_with_name (name);
	}
    }

  SERIAL_RAW (array_desc);

#if defined (LOG_FILE)
  log_file = fopen (LOG_FILE, "w");
  if (log_file == NULL)
    perror_with_name (LOG_FILE);
  fprintf (log_file, "GDB %s (%s", version, host_name);
  fprintf (log_file, " --target %s)\n", array_ops.to_shortname);
  fprintf (log_file, "Remote target %s connected to %s\n\n", array_ops.to_shortname, dev_name);
#endif

  /* see if the target is alive. For a ROM monitor, we can just try to force the
     expect_prompt to print a few times. For the GDB remote protocol, the application
     being debugged is sitting at a breakpoint and waiting for GDB to initialize
     the connection. We force it to give us an empty packet to see if it's alive.
   */
  debuglogs (3, "Trying to ACK the target's debug stub");
  /* unless your are on the new hardware, the old board won't initialize
     because the '@' doesn't flush output like it does on the new ROMS.
   */
  printf_monitor ("@");		/* ask for the last signal */
  expect_prompt (1);		/* See if we get a expect_prompt */
#ifdef TEST_ARRAY		/* skip packet for testing */
  make_gdb_packet (packet, "?");	/* ask for a bogus packet */
  if (array_send_packet (packet) == 0)
    error ("Couldn't transmit packet\n");
  printf_monitor ("@\n");	/* force it to flush stdout */
  expect_prompt (1);		/* See if we get a expect_prompt */
#endif
  push_target (&array_ops);
  if (from_tty)
    printf ("Remote target %s connected to %s\n", array_ops.to_shortname, dev_name);
}

/*
 * array_close -- Close out all files and local state before this
 *      target loses control.
 */

static void
array_close (int quitting)
{
  SERIAL_CLOSE (array_desc);
  array_desc = NULL;

  debuglogs (1, "array_close (quitting=%d)", quitting);

#if defined (LOG_FILE)
  if (log_file)
    {
      if (ferror (log_file))
	printf_filtered ("Error writing log file.\n");
      if (fclose (log_file) != 0)
	printf_filtered ("Error closing log file.\n");
    }
#endif
}

/* 
 * array_detach -- terminate the open connection to the remote
 *      debugger. Use this when you want to detach and do something
 *      else with your gdb.
 */
static void
array_detach (int from_tty)
{

  debuglogs (1, "array_detach ()");

  pop_target ();		/* calls array_close to do the real work */
  if (from_tty)
    printf ("Ending remote %s debugging\n", target_shortname);
}

/*
 * array_attach -- attach GDB to the target.
 */
static void
array_attach (char *args, int from_tty)
{
  if (from_tty)
    printf ("Starting remote %s debugging\n", target_shortname);

  debuglogs (1, "array_attach (args=%s)", args);

  printf_monitor ("go %x\n");
  /* swallow the echo.  */
  expect ("go %x\n", 1);
}

/*
 * array_resume -- Tell the remote machine to resume.
 */
static void
array_resume (int pid, int step, enum target_signal sig)
{
  debuglogs (1, "array_resume (step=%d, sig=%d)", step, sig);

  if (step)
    {
      printf_monitor ("s\n");
    }
  else
    {
      printf_monitor ("go\n");
    }
}

#define TMPBUFSIZ 5

/*
 * array_wait -- Wait until the remote machine stops, then return,
 *          storing status in status just as `wait' would.
 */
static int
array_wait (int pid, struct target_waitstatus *status)
{
  int old_timeout = timeout;
  int result, i;
  char c;
  serial_t tty_desc;
  serial_ttystate ttystate;

  debuglogs (1, "array_wait (), printing extraneous text.");

  status->kind = TARGET_WAITKIND_EXITED;
  status->value.integer = 0;

  timeout = 0;			/* Don't time out -- user program is running. */

#if !defined(__GO32__) && !defined(__MSDOS__) && !defined(_WIN32)
  tty_desc = SERIAL_FDOPEN (0);
  ttystate = SERIAL_GET_TTY_STATE (tty_desc);
  SERIAL_RAW (tty_desc);

  i = 0;
  /* poll on the serial port and the keyboard. */
  while (1)
    {
      c = readchar (timeout);
      if (c > 0)
	{
	  if (c == *(ARRAY_PROMPT + i))
	    {
	      if (++i >= strlen (ARRAY_PROMPT))
		{		/* matched the prompt */
		  debuglogs (4, "array_wait(), got the expect_prompt.");
		  break;
		}
	    }
	  else
	    {			/* not the prompt */
	      i = 0;
	    }
	  fputc_unfiltered (c, gdb_stdout);
	  gdb_flush (gdb_stdout);
	}
      c = SERIAL_READCHAR (tty_desc, timeout);
      if (c > 0)
	{
	  SERIAL_WRITE (array_desc, &c, 1);
	  /* do this so it looks like there's keyboard echo */
	  if (c == 3)		/* exit on Control-C */
	    break;
#if 0
	  fputc_unfiltered (c, gdb_stdout);
	  gdb_flush (gdb_stdout);
#endif
	}
    }
  SERIAL_SET_TTY_STATE (tty_desc, ttystate);
#else
  expect_prompt (1);
  debuglogs (4, "array_wait(), got the expect_prompt.");
#endif

  status->kind = TARGET_WAITKIND_STOPPED;
  status->value.sig = TARGET_SIGNAL_TRAP;

  timeout = old_timeout;

  return 0;
}

/*
 * array_fetch_registers -- read the remote registers into the
 *      block regs.
 */
static void
array_fetch_registers (int ignored)
{
  int regno, i;
  char *p;
  unsigned char packet[PBUFSIZ];
  char regs[REGISTER_BYTES];

  debuglogs (1, "array_fetch_registers (ignored=%d)\n", ignored);

  memset (packet, 0, PBUFSIZ);
  /* Unimplemented registers read as all bits zero.  */
  memset (regs, 0, REGISTER_BYTES);
  make_gdb_packet (packet, "g");
  if (array_send_packet (packet) == 0)
    error ("Couldn't transmit packet\n");
  if (array_get_packet (packet) == 0)
    error ("Couldn't receive packet\n");
  /* FIXME: read bytes from packet */
  debuglogs (4, "array_fetch_registers: Got a \"%s\" back\n", packet);
  for (regno = 0; regno <= PC_REGNUM + 4; regno++)
    {
      /* supply register stores in target byte order, so swap here */
      /* FIXME: convert from ASCII hex to raw bytes */
      i = ascii2hexword (packet + (regno * 8));
      debuglogs (5, "Adding register %d = %x\n", regno, i);
      SWAP_TARGET_AND_HOST (&i, 4);
      supply_register (regno, (char *) &i);
    }
}

/* 
 * This is unused by targets like this one that use a
 * protocol based on GDB's remote protocol.
 */
static void
array_fetch_register (int ignored)
{
  array_fetch_registers ();
}

/*
 * Get all the registers from the targets. They come back in a large array.
 */
static void
array_store_registers (int ignored)
{
  int regno;
  unsigned long i;
  char packet[PBUFSIZ];
  char buf[PBUFSIZ];
  char num[9];

  debuglogs (1, "array_store_registers()");

  memset (packet, 0, PBUFSIZ);
  memset (buf, 0, PBUFSIZ);
  buf[0] = 'G';

  /* Unimplemented registers read as all bits zero.  */
  /* FIXME: read bytes from packet */
  for (regno = 0; regno < 41; regno++)
    {				/* FIXME */
      /* supply register stores in target byte order, so swap here */
      /* FIXME: convert from ASCII hex to raw bytes */
      i = (unsigned long) read_register (regno);
      hexword2ascii (num, i);
      strcpy (buf + (regno * 8) + 1, num);
    }
  *(buf + (regno * 8) + 2) = 0;
  make_gdb_packet (packet, buf);
  if (array_send_packet (packet) == 0)
    error ("Couldn't transmit packet\n");
  if (array_get_packet (packet) == 0)
    error ("Couldn't receive packet\n");

  registers_changed ();
}

/* 
 * This is unused by targets like this one that use a
 * protocol based on GDB's remote protocol.
 */
static void
array_store_register (int ignored)
{
  array_store_registers ();
}

/* Get ready to modify the registers array.  On machines which store
   individual registers, this doesn't need to do anything.  On machines
   which store all the registers in one fell swoop, this makes sure
   that registers contains all the registers from the program being
   debugged.  */

static void
array_prepare_to_store (void)
{
  /* Do nothing, since we can store individual regs */
}

static void
array_files_info (void)
{
  printf ("\tAttached to %s at %d baud.\n",
	  dev_name, baudrate);
}

/*
 * array_write_inferior_memory -- Copy LEN bytes of data from debugger
 *      memory at MYADDR to inferior's memory at MEMADDR.  Returns length moved.
 */
static int
array_write_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
{
  unsigned long i;
  int j;
  char packet[PBUFSIZ];
  char buf[PBUFSIZ];
  char num[9];
  char *p;

  debuglogs (1, "array_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
  memset (buf, '\0', PBUFSIZ);	/* this also sets the string terminator */
  p = buf;

  *p++ = 'M';			/* The command to write memory */
  hexword2ascii (num, memaddr);	/* convert the address */
  strcpy (p, num);		/* copy the address */
  p += 8;
  *p++ = ',';			/* add comma delimeter */
  hexword2ascii (num, len);	/* Get the length as a 4 digit number */
  *p++ = num[4];
  *p++ = num[5];
  *p++ = num[6];
  *p++ = num[7];
  *p++ = ':';			/* add the colon delimeter */
  for (j = 0; j < len; j++)
    {				/* copy the data in after converting it */
      *p++ = tohex ((myaddr[j] >> 4) & 0xf);
      *p++ = tohex (myaddr[j] & 0xf);
    }

  make_gdb_packet (packet, buf);
  if (array_send_packet (packet) == 0)
    error ("Couldn't transmit packet\n");
  if (array_get_packet (packet) == 0)
    error ("Couldn't receive packet\n");

  return len;
}

/*
 * array_read_inferior_memory -- read LEN bytes from inferior memory
 *      at MEMADDR.  Put the result at debugger address MYADDR.  Returns
 *      length moved.
 */
static int
array_read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
{
  int j;
  char buf[20];
  char packet[PBUFSIZ];
  int count;			/* Number of bytes read so far.  */
  unsigned long startaddr;	/* Starting address of this pass.  */
  int len_this_pass;		/* Number of bytes to read in this pass.  */

  debuglogs (1, "array_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);

  /* Note that this code works correctly if startaddr is just less
     than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
     thing).  That is, something like
     array_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
     works--it never adds len To memaddr and gets 0.  */
  /* However, something like
     array_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
     doesn't need to work.  Detect it and give up if there's an attempt
     to do that.  */
  if (((memaddr - 1) + len) < memaddr)
    {
      errno = EIO;
      return 0;
    }

  for (count = 0, startaddr = memaddr; count < len; startaddr += len_this_pass)
    {
      /* Try to align to 16 byte boundry (why?) */
      len_this_pass = 16;
      if ((startaddr % 16) != 0)
	{
	  len_this_pass -= startaddr % 16;
	}
      /* Only transfer bytes we need */
      if (len_this_pass > (len - count))
	{
	  len_this_pass = (len - count);
	}
      /* Fetch the bytes */
      debuglogs (3, "read %d bytes from inferior address %x", len_this_pass,
		 startaddr);
      sprintf (buf, "m%08lx,%04x", startaddr, len_this_pass);
      make_gdb_packet (packet, buf);
      if (array_send_packet (packet) == 0)
	{
	  error ("Couldn't transmit packet\n");
	}
      if (array_get_packet (packet) == 0)
	{
	  error ("Couldn't receive packet\n");
	}
      if (*packet == 0)
	{
	  error ("Got no data in the GDB packet\n");
	}
      /* Pick packet apart and xfer bytes to myaddr */
      debuglogs (4, "array_read_inferior_memory: Got a \"%s\" back\n", packet);
      for (j = 0; j < len_this_pass; j++)
	{
	  /* extract the byte values */
	  myaddr[count++] = from_hex (*(packet + (j * 2))) * 16 + from_hex (*(packet + (j * 2) + 1));
	  debuglogs (5, "myaddr[%d] set to %x\n", count - 1, myaddr[count - 1]);
	}
    }
  return (count);
}

/* FIXME-someday!  merge these two.  */
static int
array_xfer_memory (memaddr, myaddr, len, write, target)
     CORE_ADDR memaddr;
     char *myaddr;
     int len;
     int write;
     struct target_ops *target;	/* ignored */
{
  if (write)
    return array_write_inferior_memory (memaddr, myaddr, len);
  else
    return array_read_inferior_memory (memaddr, myaddr, len);
}

static void
array_kill (char *args, int from_tty)
{
  return;			/* ignore attempts to kill target system */
}

/* Clean up when a program exits.
   The program actually lives on in the remote processor's RAM, and may be
   run again without a download.  Don't leave it full of breakpoint
   instructions.  */

static void
array_mourn_inferior (void)
{
  remove_breakpoints ();
  generic_mourn_inferior ();	/* Do all the proper things now */
}

#define MAX_ARRAY_BREAKPOINTS 16

static CORE_ADDR breakaddr[MAX_ARRAY_BREAKPOINTS] =
{0};

/*
 * array_insert_breakpoint -- add a breakpoint
 */
static int
array_insert_breakpoint (CORE_ADDR addr, char *shadow)
{
  int i;
  int bp_size = 0;
  CORE_ADDR bp_addr = addr;

  debuglogs (1, "array_insert_breakpoint() addr = 0x%x", addr);
  BREAKPOINT_FROM_PC (&bp_addr, &bp_size);

  for (i = 0; i <= MAX_ARRAY_BREAKPOINTS; i++)
    {
      if (breakaddr[i] == 0)
	{
	  breakaddr[i] = addr;
	  if (sr_get_debug () > 4)
	    printf ("Breakpoint at %s\n", paddr_nz (addr));
	  array_read_inferior_memory (bp_addr, shadow, bp_size);
	  printf_monitor ("b 0x%x\n", addr);
	  expect_prompt (1);
	  return 0;
	}
    }

  fprintf (stderr, "Too many breakpoints (> 16) for monitor\n");
  return 1;
}

/*
 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
 */
static int
array_remove_breakpoint (CORE_ADDR addr, char *shadow)
{
  int i;

  debuglogs (1, "array_remove_breakpoint() addr = 0x%x", addr);

  for (i = 0; i < MAX_ARRAY_BREAKPOINTS; i++)
    {
      if (breakaddr[i] == addr)
	{
	  breakaddr[i] = 0;
	  /* some monitors remove breakpoints based on the address */
	  printf_monitor ("bd %x\n", i);
	  expect_prompt (1);
	  return 0;
	}
    }
  fprintf (stderr, "Can't find breakpoint associated with 0x%s\n",
	   paddr_nz (addr));
  return 1;
}

static void
array_stop (void)
{
  debuglogs (1, "array_stop()");
  printf_monitor ("\003");
  expect_prompt (1);
}

/* 
 * array_command -- put a command string, in args, out to MONITOR.
 *      Output from MONITOR is placed on the users terminal until the
 *      expect_prompt is seen. FIXME
 */
static void
monitor_command (char *args, int fromtty)
{
  debuglogs (1, "monitor_command (args=%s)", args);

  if (array_desc == NULL)
    error ("monitor target not open.");

  if (!args)
    error ("Missing command.");

  printf_monitor ("%s\n", args);
  expect_prompt (0);
}

/*
 * make_gdb_packet -- make a GDB packet. The data is always ASCII.
 *       A debug packet whose contents are <data>
 *       is encapsulated for transmission in the form:
 *
 *              $ <data> # CSUM1 CSUM2
 *
 *       <data> must be ASCII alphanumeric and cannot include characters
 *       '$' or '#'.  If <data> starts with two characters followed by
 *       ':', then the existing stubs interpret this as a sequence number.
 *
 *       CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
 *       checksum of <data>, the most significant nibble is sent first.
 *       the hex digits 0-9,a-f are used.
 *
 */
static void
make_gdb_packet (char *buf, char *data)
{
  int i;
  unsigned char csum = 0;
  int cnt;
  char *p;

  debuglogs (3, "make_gdb_packet(%s)\n", data);
  cnt = strlen (data);
  if (cnt > PBUFSIZ)
    error ("make_gdb_packet(): to much data\n");

  /* start with the packet header */
  p = buf;
  *p++ = '$';

  /* calculate the checksum */
  for (i = 0; i < cnt; i++)
    {
      csum += data[i];
      *p++ = data[i];
    }

  /* terminate the data with a '#' */
  *p++ = '#';

  /* add the checksum as two ascii digits */
  *p++ = tohex ((csum >> 4) & 0xf);
  *p++ = tohex (csum & 0xf);
  *p = 0x0;			/* Null terminator on string */
}

/*
 * array_send_packet -- send a GDB packet to the target with error handling. We
 *              get a '+' (ACK) back if the packet is received and the checksum
 *              matches. Otherwise a '-' (NAK) is returned. It returns a 1 for a
 *              successful transmition, or a 0 for a failure.
 */
static int
array_send_packet (char *packet)
{
  int c, retries, i;
  char junk[PBUFSIZ];

  retries = 0;

#if 0
  /* scan the packet to make sure it only contains valid characters.
     this may sound silly, but sometimes a garbled packet will hang
     the target board. We scan the whole thing, then print the error
     message.
   */
  for (i = 0; i < strlen (packet); i++)
    {
      debuglogs (5, "array_send_packet(): Scanning \'%c\'\n", packet[i]);
      /* legit hex numbers or command */
      if ((isxdigit (packet[i])) || (isalpha (packet[i])))
	continue;
      switch (packet[i])
	{
	case '+':		/* ACK */
	case '-':		/* NAK */
	case '#':		/* end of packet */
	case '$':		/* start of packet */
	  continue;
	default:		/* bogus character */
	  retries++;
	  debuglogs (4, "array_send_packet(): Found a non-ascii digit \'%c\' in the packet.\n", packet[i]);
	}
    }
#endif

  if (retries > 0)
    error ("Can't send packet, found %d non-ascii characters", retries);

  /* ok, try to send the packet */
  retries = 0;
  while (retries++ <= 10)
    {
      printf_monitor ("%s", packet);

      /* read until either a timeout occurs (-2) or '+' is read */
      while (retries <= 10)
	{
	  c = readchar (-timeout);
	  debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c);
	  switch (c)
	    {
	    case '+':
	      debuglogs (3, "Got Ack\n");
	      return 1;
	    case SERIAL_TIMEOUT:
	      debuglogs (3, "Timed out reading serial port\n");
	      printf_monitor ("@");	/* resync with the monitor */
	      expect_prompt (1);	/* See if we get a expect_prompt */
	      break;		/* Retransmit buffer */
	    case '-':
	      debuglogs (3, "Got NAK\n");
	      printf_monitor ("@");	/* resync with the monitor */
	      expect_prompt (1);	/* See if we get a expect_prompt */
	      break;
	    case '$':
	      /* it's probably an old response, or the echo of our command.
	       * just gobble up the packet and ignore it.
	       */
	      debuglogs (3, "Got a junk packet\n");
	      i = 0;
	      do
		{
		  c = readchar (timeout);
		  junk[i++] = c;
		}
	      while (c != '#');
	      c = readchar (timeout);
	      junk[i++] = c;
	      c = readchar (timeout);
	      junk[i++] = c;
	      junk[i++] = '\0';
	      debuglogs (3, "Reading a junk packet, got a \"%s\"\n", junk);
	      continue;		/* Now, go look for next packet */
	    default:
	      continue;
	    }
	  retries++;
	  debuglogs (3, "Retransmitting packet \"%s\"\n", packet);
	  break;		/* Here to retransmit */
	}
    }				/* outer while */
  return 0;
}

/*
 * array_get_packet -- get a GDB packet from the target. Basically we read till we
 *              see a '#', then check the checksum. It returns a 1 if it's gotten a
 *              packet, or a 0 it the packet wasn't transmitted correctly.
 */
static int
array_get_packet (char *packet)
{
  int c;
  int retries;
  unsigned char csum;
  unsigned char pktcsum;
  char *bp;

  csum = 0;
  bp = packet;

  memset (packet, 1, PBUFSIZ);
  retries = 0;
  while (retries <= 10)
    {
      do
	{
	  c = readchar (timeout);
	  if (c == SERIAL_TIMEOUT)
	    {
	      debuglogs (3, "array_get_packet: got time out from serial port.\n");
	    }
	  debuglogs (3, "Waiting for a '$', got a %c\n", c);
	}
      while (c != '$');

      retries = 0;
      while (retries <= 10)
	{
	  c = readchar (timeout);
	  debuglogs (3, "array_get_packet: got a '%c'\n", c);
	  switch (c)
	    {
	    case SERIAL_TIMEOUT:
	      debuglogs (3, "Timeout in mid-packet, retrying\n");
	      return 0;
	    case '$':
	      debuglogs (3, "Saw new packet start in middle of old one\n");
	      return 0;		/* Start a new packet, count retries */
	    case '#':
	      *bp = '\0';
	      pktcsum = from_hex (readchar (timeout)) << 4;
	      pktcsum |= from_hex (readchar (timeout));
	      if (csum == 0)
		debuglogs (3, "\nGDB packet checksum zero, must be a bogus packet\n");
	      if (csum == pktcsum)
		{
		  debuglogs (3, "\nGDB packet checksum correct, packet data is \"%s\",\n", packet);
		  printf_monitor ("@");
		  expect_prompt (1);
		  return 1;
		}
	      debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum, csum);
	      return 0;
	    case '*':		/* Run length encoding */
	      debuglogs (5, "Run length encoding in packet\n");
	      csum += c;
	      c = readchar (timeout);
	      csum += c;
	      c = c - ' ' + 3;	/* Compute repeat count */

	      if (c > 0 && c < 255 && bp + c - 1 < packet + PBUFSIZ - 1)
		{
		  memset (bp, *(bp - 1), c);
		  bp += c;
		  continue;
		}
	      *bp = '\0';
	      printf_filtered ("Repeat count %d too large for buffer.\n", c);
	      return 0;

	    default:
	      if ((!isxdigit (c)) && (!ispunct (c)))
		debuglogs (4, "Got a non-ascii digit \'%c\'.\\n", c);
	      if (bp < packet + PBUFSIZ - 1)
		{
		  *bp++ = c;
		  csum += c;
		  continue;
		}

	      *bp = '\0';
	      puts_filtered ("Remote packet too long.\n");
	      return 0;
	    }
	}
    }
  return 0;			/* exceeded retries */
}

/*
 * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value.
 */
static unsigned long
ascii2hexword (unsigned char *mem)
{
  unsigned long val;
  int i;
  char buf[9];

  val = 0;
  for (i = 0; i < 8; i++)
    {
      val <<= 4;
      if (mem[i] >= 'A' && mem[i] <= 'F')
	val = val + mem[i] - 'A' + 10;
      if (mem[i] >= 'a' && mem[i] <= 'f')
	val = val + mem[i] - 'a' + 10;
      if (mem[i] >= '0' && mem[i] <= '9')
	val = val + mem[i] - '0';
      buf[i] = mem[i];
    }
  buf[8] = '\0';
  debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val, buf, mem);
  return val;
}

/*
 * ascii2hexword -- convert a hex value to an ascii number represented by 8
 *      digits.
 */
static void
hexword2ascii (unsigned char *mem, unsigned long num)
{
  int i;
  unsigned char ch;

  debuglogs (4, "hexword2ascii() converting %x ", num);
  for (i = 7; i >= 0; i--)
    {
      mem[i] = tohex ((num >> 4) & 0xf);
      mem[i] = tohex (num & 0xf);
      num = num >> 4;
    }
  mem[8] = '\0';
  debuglogs (4, "\tto a %s", mem);
}

/* Convert hex digit A to a number.  */
static int
from_hex (int a)
{
  if (a == 0)
    return 0;

  debuglogs (4, "from_hex got a 0x%x(%c)\n", a, a);
  if (a >= '0' && a <= '9')
    return a - '0';
  if (a >= 'a' && a <= 'f')
    return a - 'a' + 10;
  if (a >= 'A' && a <= 'F')
    return a - 'A' + 10;
  else
    {
      error ("Reply contains invalid hex digit 0x%x", a);
    }
}

/* Convert number NIB to a hex digit.  */
static int
tohex (int nib)
{
  if (nib < 10)
    return '0' + nib;
  else
    return 'a' + nib - 10;
}

/*
 * _initialize_remote_monitors -- setup a few addtitional commands that
 *              are usually only used by monitors.
 */
void
_initialize_remote_monitors (void)
{
  /* generic monitor command */
  add_com ("monitor", class_obscure, monitor_command,
	   "Send a command to the debug monitor.");

}

/*
 * _initialize_array -- do any special init stuff for the target.
 */
void
_initialize_array (void)
{
  init_array_ops ();
  add_target (&array_ops);
}