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
|
/*
Copyright (C) 2004 T. Scott Dattalo
This file is part of gpsim.
gpsim 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, or (at your option)
any later version.
gpsim 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 gpsim; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/*
Scripting
client3.cc - a program to demonstrate gpsim scripting.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "client_interface.h"
#define PORT 0x1234
pthread_t thThread1;
pthread_attr_t thAttribute1;
pthread_t thThread2;
pthread_attr_t thAttribute2;
pthread_t thThread3;
pthread_attr_t thAttribute3;
int something=1;
ClientSocketInterface *sock1=0;
ClientSocketInterface *sock2=0;
ClientSocketInterface *sock3=0;
void *thread1(void *ignored)
{
printf( "thread1 running\n");
sleep(1);
bool bPassed = true; // regression test results - assume we pass
ClientSocketInterface *sock=sock1;
if(!sock)
exit(1);
/*
Set up.
Since we are controlling the simulator entirely from a script, there really is
not much need for the command line output it provides. So we'll turn it off
by setting the simulator attribute 'sim.verbosity' to zero.
*/
sock->WriteSymbolUInt32("sim.verbosity",0);
/*
The simulator's command line parser is still available for us to use.
So we'll use it to load the source code and to set a break point.
*/
//sock->SendCmd("load s gensquares.cod\n");
unsigned int h_reg1 = sock->CreateLinkUInt32("reg1");
if(!h_reg1) {
printf("unable to create handles\n");
delete sock;
exit(1);
}
unsigned int reg1=0;
for(unsigned int i=0; i<128; i++) {
printf("t1 query link\n");
sock->WriteToLinkUInt32(h_reg1, i);
printf("t1 query link got %u\n", i);
if(! sock->QueryLinkUInt32(h_reg1,reg1) )
printf("failed to decode reg1\n");
if(i != reg1) {
printf("t1 failed expected %u, but got %u\n", i, reg1);
bPassed = false;
break;
}
}
/*
release the handle
*/
sock->RemoveLink(h_reg1);
sock->WriteSymbolUInt32("sim.verbosity",1);
delete sock;
if(bPassed)
printf("The simulation passed!\n");
else
printf("The simulation failed\n");
printf("thread 1 is complete\n");
return 0;
}
void *thread2(void *ignored)
{
printf( "thread2 running\n");
bool bPassed = true; // regression test results - assume we pass
ClientSocketInterface *sock=sock2;
if(!sock)
exit(1);
sleep(1);
/*
Set up.
Since we are controlling the simulator entirely from a script, there really is
not much need for the command line output it provides. So we'll turn it off
by setting the simulator attribute 'sim.verbosity' to zero.
*/
sock->WriteSymbolUInt32("sim.verbosity",0);
/*
The simulator's command line parser is still available for us to use.
So we'll use it to load the source code and to set a break point.
*/
//sock->SendCmd("load s gensquares.cod\n");
unsigned int h_reg2 = sock->CreateLinkUInt32("reg2");
/*
start running
*/
//sock->SendCmd("run\n");
if(!h_reg2) {
printf("unable to create handles\n");
delete sock;
exit(1);
}
unsigned int reg2=0;
for(unsigned int i=0; i<128; i++) {
printf("t2 query link\n");
sock->WriteToLinkUInt32(h_reg2, i);
printf("t2 query link got %u\n", i);
if(! sock->QueryLinkUInt32(h_reg2,reg2) )
printf("failed to decode reg2\n");
if(i != reg2) {
printf("t2 failed expected %u, but got %u\n", i, reg2);
bPassed = false;
break;
}
}
/*
release the handle
*/
sock->RemoveLink(h_reg2);
sock->WriteSymbolUInt32("sim.verbosity",1);
delete sock;
if(bPassed)
printf("The simulation passed!\n");
else
printf("The simulation failed\n");
printf("thread 2 is complete\n");
return 0;
}
void *thread3(void *ignored)
{
printf( "thread3 running\n");
ClientSocketInterface *sock=sock3;
if (!sock)
exit(1);
int i = 0;
while (i++<4)
sleep(1);
sock->SendCmd("load s gensquares.cod\n");
if (sock->CreateCallbackLink(0x1000)) {
printf("created callback link\n");
}
sock->SendCmd("break c 0x5000\n");
sock->SendCmd("run\n");
for(int i = 0; i<4; i++)
{
printf("thread 3 recieved %s\n", sock->Receive());
}
sock->SendCmd("quit\n");
delete sock;
printf("thread 3 is complete\n");
return 0;
}
void start_threads(void)
{
printf( "starting threads....\n");
pthread_attr_init(&thAttribute1);
pthread_attr_setdetachstate(&thAttribute1, PTHREAD_CREATE_JOINABLE);
pthread_create(&thThread1, &thAttribute1, thread1, (void *)&something);
pthread_attr_init(&thAttribute2);
pthread_attr_setdetachstate(&thAttribute2, PTHREAD_CREATE_JOINABLE);
pthread_create(&thThread2, &thAttribute2, thread2, (void *)&something);
pthread_attr_init(&thAttribute3);
pthread_attr_setdetachstate(&thAttribute3, PTHREAD_CREATE_JOINABLE);
pthread_create(&thThread3, &thAttribute3, thread3, (void *)&something);
printf( "started threads\n");
}
//========================================================================
//
//
//
int main(int argc, char **argv)
{
sock1 = new ClientSocketInterface(argc>2 ? argv[2] : 0, PORT);
sock2 = new ClientSocketInterface(argc>2 ? argv[2] : 0, PORT);
sock3 = new ClientSocketInterface(argc>2 ? argv[2] : 0, PORT);
start_threads();
void * status;
pthread_join(thThread3, &status);
printf("thread 3 exit %d\n", status);
return 0;
}
|