* Contribute CGEN simulator build support code.
[binutils-gdb.git] / gdb / rdi-share / msgbuild.c
blob63263e75676fb7b2246d71299f7794546cf722ef
1 /*
2 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
3 *
4 * This software may be freely used, copied, modified, and distributed
5 * provided that the above copyright notice is preserved in all copies of the
6 * software.
7 */
9 /* -*-C-*-
11 * $Revision$
12 * $Date$
15 * msgbuild.c - utilities for assembling and interpreting ADP messages
19 #include <stdarg.h> /* ANSI varargs support */
21 #ifdef TARGET
22 # include "angel.h"
23 # include "devconf.h"
24 #else
25 # include "host.h"
26 # include "hostchan.h"
27 #endif
29 #include "channels.h"
30 #include "buffers.h"
31 #include "angel_endian.h" /* Endianness support macros */
32 #include "msgbuild.h" /* Header file for this source code */
34 #ifndef UNUSED
35 # define UNUSED(x) ((x)=(x))
36 #endif
38 #ifndef TARGET
40 extern unsigned int Armsd_BufferSize;
42 #endif /* ndef TARGET */
45 unsigned int vmsgbuild(unsigned char *buffer, const char *format, va_list args)
47 unsigned int blen = 0;
48 int ch;
50 /* Step through the format string */
51 while ((ch = *format++) != '\0')
53 if (ch != '%')
55 if (buffer != NULL)
56 *buffer++ = (unsigned char)ch;
58 blen++;
60 else
62 switch (ch = *format++)
64 case 'w':
65 case 'W':
66 /* 32bit pointer */
67 case 'p':
68 case 'P':
70 /* 32bit word / 32bit pointer */
71 unsigned int na = va_arg(args, unsigned int);
73 if (buffer != NULL)
75 PUT32LE(buffer, na);
76 buffer += sizeof(unsigned int);
79 blen += sizeof(unsigned int);
81 break;
84 case 'h':
85 case 'H':
87 /* 16bit value */
88 unsigned int na = va_arg(args, unsigned int);
90 if (buffer != NULL)
92 PUT16LE(buffer, na);
93 buffer += sizeof(unsigned short);
96 blen += sizeof(unsigned short);
98 break;
101 case 'c':
102 case 'C':
103 case 'b':
104 case 'B':
105 /* 8bit character / 8bit byte */
106 ch = va_arg(args, int);
109 * XXX
111 * fall through to the normal character processing
114 case '%':
115 default:
116 /* normal '%' character, or a different normal character */
117 if (buffer != NULL)
118 *buffer++ = (unsigned char)ch;
120 blen++;
121 break;
126 return blen;
130 * msgbuild
131 * --------
132 * Simple routine to aid in construction of Angel messages. See the
133 * "msgbuild.h" header file for a detailed description of the operation
134 * of this routine.
136 unsigned int msgbuild(unsigned char *buffer, const char *format, ...)
138 va_list args;
139 unsigned int blen;
141 va_start(args, format);
142 blen = vmsgbuild(buffer, format, args);
143 va_end(args);
145 return blen;
148 #if !defined(JTAG_ADP_SUPPORTED) && !defined(MSG_UTILS_ONLY)
150 * This routine allocates a buffer, puts the data supplied as
151 * parameters into the buffer and sends the message. It does *NOT*
152 * wait for a reply.
154 extern int msgsend(ChannelID chan, const char *format,...)
156 unsigned int length;
157 p_Buffer buffer;
158 va_list args;
159 # ifndef TARGET
160 Packet *packet;
162 packet = DevSW_AllocatePacket(Armsd_BufferSize);
163 buffer = packet->pk_buffer;
164 # else
165 buffer = angel_ChannelAllocBuffer(Angel_ChanBuffSize);
166 # endif
168 if (buffer != NULL)
170 va_start(args, format);
172 length = vmsgbuild(BUFFERDATA(buffer), format, args);
174 # ifdef TARGET
175 angel_ChannelSend(CH_DEFAULT_DEV, chan, buffer, length);
176 # else
177 packet->pk_length = length;
178 Adp_ChannelWrite(chan, packet);
179 # endif
181 va_end(args);
182 return 0;
184 else
185 return -1;
188 #endif /* ndef JTAG_ADP_SUPPORTED && ndef MSG_UTILS_ONLY */
191 * unpack_message
192 * --------------
194 extern unsigned int unpack_message(unsigned char *buffer, const char *format, ...)
196 va_list args;
197 unsigned int blen = 0;
198 int ch;
199 char *chp = NULL;
201 va_start(args, format);
203 /* Step through the format string. */
204 while ((ch = *format++) != '\0')
206 if (ch != '%')
208 if (buffer != NULL)
209 ch = (unsigned char)*buffer++;
211 blen++;
213 else
215 switch (ch = *format++)
217 case 'w':
218 case 'W':
220 /* 32bit word. */
221 unsigned int *nap = va_arg(args, unsigned int*);
223 if (buffer != NULL)
225 *nap = PREAD32(LE, buffer);
226 buffer += sizeof(unsigned int);
229 blen += sizeof(unsigned int);
231 break;
234 case 'h':
235 case 'H':
237 /* 16bit value. */
238 unsigned int *nap = va_arg(args, unsigned int*);
240 if (buffer != NULL)
242 *nap = PREAD16(LE,buffer);
243 buffer += sizeof(unsigned short);
246 blen += sizeof(unsigned short);
248 break;
251 case 'c':
252 case 'C':
253 case 'b':
254 case 'B':
255 /* 8-bit character, or 8-bit byte */
256 chp = va_arg(args, char*);
259 * XXX
261 * fall through to the normal character processing.
264 case '%':
265 default:
266 /* normal '%' character, or a different normal character */
267 if (buffer != NULL)
268 *chp = (unsigned char)*buffer++;
270 blen++;
272 break;
277 va_end(args);
278 return(blen);
282 /* EOF msgbuild.c */