static inline VOID RpspClientPrintMove ( IN INT myTaskId, IN RPS_MOVE move ) { switch(move) { case RockMove: bwprintf(BWCOM2, "Client %d: I choose rock \r\n", myTaskId); break; case PaperMove: bwprintf(BWCOM2, "Client %d: I choose paper \r\n", myTaskId); break; case ScissorsMove: bwprintf(BWCOM2, "Client %d: I choose scissors \r\n", myTaskId); break; default: ASSERT(FALSE); break; } }
static VOID RpspServerHandleOnePlayerQuitting ( IN INT quittingPlayer, IN INT otherPlayer ) { RPS_SERVER_REQUEST otherPlayerRequest; RPS_SERVER_RESPONSE otherPlayerResponse = PartnerQuitResponse; bwprintf(BWCOM2, "Server: Client %d is quitting \r\n", quittingPlayer); // Acknowledge quitting player, and inform other player Reply(quittingPlayer, NULL, 0); Reply(otherPlayer, &otherPlayerResponse, sizeof(otherPlayerResponse)); // Wait for other player to quit Receive(&otherPlayer, &otherPlayerRequest, sizeof(otherPlayerRequest)); ASSERT(QuitRequest == otherPlayerRequest.type); bwprintf(BWCOM2, "Server: Client %d is quitting \r\n", otherPlayer); // Acknowledge player 2 quitting Reply(otherPlayer, NULL, 0); }
static inline VOID RpspClientPrintResult ( IN INT myTaskId, IN RPS_SERVER_RESPONSE response ) { if(PartnerQuitResponse == response) { bwprintf(BWCOM2, "Client %d: My partner quit! \r\n", myTaskId); } else if(WinResponse == response) { bwprintf(BWCOM2, "Client %d: I won the match! \r\n", myTaskId); } else if(TieResponse == response) { bwprintf(BWCOM2, "Client %d: We tied that match \r\n", myTaskId); } else { bwprintf(BWCOM2, "Client %d: I lost the match :( \r\n", myTaskId); } }
void sr() { int ret = -1; int ret2; int tid; char rmsg[5]; char replymsg[5]; replymsg[0] = 'm'; replymsg[1] = 'i'; replymsg[2] = 'd'; replymsg[3] = '\0'; char msg[9]; msg[0] = 'm'; msg[1] = 'i'; msg[2] = 'd'; msg[3] = 's'; msg[4] = 'e'; msg[5] = 'n'; msg[6] = 'd'; msg[7] = '\0'; char reply[9]; while (1) { bwprintf(COM2, "sr call receive\n"); ret = Receive(&tid, rmsg, 5); if (ret > 0) { bwprintf(COM2, "sr receive: %s with origin %d char\n", rmsg, ret); bwprintf(COM2, "sr reply to %d\n", tid); ret2 = Reply(tid, replymsg, 5); bwprintf(COM2, "sr ret value: %d\n", ret2); ret = Send(1, msg, 9, reply, 2); bwprintf(COM2, "sr receive reply: %s with origin %d char\n", reply, ret); } } }
void other_task() { int mytid = MyTid(); int myparenttid = MyParentTid(); bwprintf( COM2, "My tid: %d\tMy parent tid:%d\n\r", mytid, myparenttid); Pass(); bwprintf( COM2, "My tid: %d\tMy parent tid:%d\n\r", mytid, myparenttid); Exit(); }
void other_user_task() { bwprintf(COM2, "My TID is %d and my parent's TID is %d.\n", MyTid(), MyParentTid()); Pass(); bwprintf(COM2, "My TID is %d and my parent's TID is %d.\n", MyTid(), MyParentTid()); Exit(); }
void fuckit() { int i; int tid = 0xB00B; char blah[20]; bwputstr(COM2, "FUCKIT: Going to recieve\r\n"); i = Receive(&tid, blah, 20); bwprintf(COM2, "FUCKIT:recv'd(%d) %s\r\n", i, blah); i = Reply(tid, "EAT MY ASS", 11); bwprintf(COM2, "FUCKIT: replyed %d. exiting. \r\n", i); Exit(); }
void user_program() { int tid = -1; tid = Create(7, receiver); bwprintf(COM2, "Created: %d\n", tid); tid = Create(4, sr); bwprintf(COM2, "Created: %d\n", tid); tid = Create(3, sender); bwprintf(COM2, "First: exiting\n"); Exit(); }
void assert(int exp, char* exp_str, char* format, ...) { if (!exp) { reset_interrupts(); bwprintf(COM2, "Assertion Failed! (%s) ", exp_str); va_list args; va_start(args, format); bwformat(COM2, format, args); va_end(args); bwprintf(COM2, "\n"); } }
void time_test_receiver_64(){ char msg[64]; int tid = 0; int msglen = sizeof(msg); bwprintf(COM2, "\t[TIMING_TEST_RECVR] CALLING RECEIVE\n"); Receive( &tid, msg, msglen); Reply( tid, msg, msglen ); bwprintf(COM2, "\t[TIMING_TEST_RECVR] EXITING\n"); Exit(); }
static VOID RpsServerHandleBothPlayersQuitting ( IN INT player1, IN INT player2 ) { bwprintf(BWCOM2, "Server: Client %d is quitting \r\n", player1); bwprintf(BWCOM2, "Server: Client %d is quitting \r\n", player2); Reply(player1, NULL, 0); Reply(player2, NULL, 0); }
void sender() { int ret = -1; char msg[9]; msg[0] = 'h'; msg[1] = 'e'; msg[2] = 'l'; msg[3] = 'l'; msg[4] = 'o'; msg[5] = '\0'; char reply[9]; bwprintf(COM2, "Sender call send\n"); ret = Send(2, msg, 9, reply, 5); bwprintf(COM2, "Sender receive reply: %s with origin %d char\n", reply, ret); Exit(); }
void task_test_clock_accuracy() { //int i = 0; int tid = 0; tid = Create(5,&task_system_nameserver); //assert(tid == NAMESERVER_TID,"tid == NAMESERVER_TID"); tid = Create(5,&task_system_clock_server); tid = Create(9,&task_system_clock_notifier); int print_interval = 100; int time = -1; int time2 = -1; int time3 = -1; while(1) { time2 = Time(); assert(time2 <= time3 + 1,"time2 <= time3 + 1"); time3 = time2; if(time2 == time + print_interval){ time = time2; bwprintf(COM2, "%d\n", time); } } Exit(); }
int Sendt(int a, char *b, int c, char *d, int e) { start = TIMERV; Send(a, b, c, d, e); end = TIMERV; /// this doesn't help, it's still not going to time right bwprintf(COM2, "KJS: It took %d 200mses\r\n", start - end); }
void lol() { int i; bwprintf(COM2, "HAHA! I'm a new user task and my tid is '%d'\n", MyTid()); for(i = 0; i < 50000; ++i); Exit(); }
int math_sqrt( int x ) { if( x < 0 ) { bwprintf(COM2, "\x1B[2J\x1B[HX IN SQRT [%d]", x); while(1); assert( 0, "TRIED TO SQUARE ROOT A NEGATIVE NUMBER" ); } if( x == 0 ) { return 0; } int top = 1; int bottom = 0; //gallop search to find the an interval containing the sqrt while( top * top < x ) { bottom = top; top = top * 2; //make sure the square won't overflow if( top > 46340 ) { //if it will overflow, don't let it top = 46340; break; } } //if it would have caused overflow, just return the top //should almost never happen if( x >= top * top ) { return top; } //loop until we have zeroed in while( top - bottom > 1 ) { int mid = ((top - bottom) / 2) + bottom; int sqr = mid * mid; if( sqr == x ) { return mid; } else if( sqr < x ) { bottom = mid; } else { top = mid; } } int topdiff = x - (top * top); topdiff = ABS( topdiff ); int botdiff = x - (bottom * bottom); botdiff = ABS( botdiff ); if( topdiff < botdiff ) { return top; } return bottom; }
/* Will kind of suck without zero-padding */ void PrintBits(unsigned char *bits, int len) { int i; for(i = 0; i < len; ++i) { bwprintf(COM2, "%x-", bits[i]); } bwputstr(COM2, "\r\n"); }
void clockTick() { unsigned int time = 0; int tid = MyTid(); while(1) { AwaitEvent(EVENT_TIME_ELAP, NULL, 0); time++; bwprintf(COM2, "%d: %ds\n", tid, time); } }
void a1() { int tid; tid = Create( 5, other_task); bwprintf( COM2, "Created: %d.\n\r", tid); tid = Create( 5, other_task); bwprintf( COM2, "Created: %d.\n\r", tid); tid = Create( 12, other_task); bwprintf( COM2, "Created: %d.\n\r", tid); tid = Create( 15, other_task); bwprintf( COM2, "Created: %d.\n\r", tid); bwprintf( COM2, "First: exiting.\n\r"); Exit(); }
void printtrap (struct trapframe *t) { bwprintf (COM2, "User trap frame:\n"); bwprintf (COM2, "\tr0 = %x\n" "\tr1 = %x\n" "\tr2 = %x\n" "\tr3 = %x\n" "\tr4 = %x\n" "\tr5 = %x\n" "\tr6 = %x\n" "\tr7 = %x\n" "\tr8 = %x\n" "\tr9 = %x\n" "\tr10 = %x\n" "\tr11 = %x\n" "\tr12 = %x\n" "\tr13 = %x\n" "\tr14 = %x\n\n", t->r0,t->r1,t->r2,t->r3,t->r4,t->r5,t->r6,t->r7,t->r8,t->r9,t->r10,t->r11,t->r12,t->r13,t->r14); }
void error(char* format, ...) { reset_interrupts(); bwprintf(COM2, "ERROR: "); va_list args; va_start(args, format); bwformat(COM2, format, args); va_end(args); }
// ----------------------------------------------------------------------------------------------------------------------------------------------------- // Command Methods // ----------------------------------------------------------------------------------------------------------------------------------------------------- void send_train_move_command( int tr_speed, int delay_time, Train_status *train_status, Train_server_data *server_data ){ // First, get the command server tid int cmd_server_tid = server_data->tasks_tids[TR_CMD_SERVER_TID_INDEX]; // If it's the same speed ignore this request. if ( tr_speed == train_status->motion_data.train_speed ) return; // Is velocity enabled? if ( tr_speed != TRAIN_STOP_CMD_SPEED && !server_data->calibration_data.speed_data[ tr_speed - 1 ].velocity_enabled ) bwassert( 0, "TRAIN_MOTION: Velocity not enabled. [ speed: %d distance_type: %d ]", tr_speed, train_status->motion_data.distance_type ); // Send the command to the server bwdebug( DBG_USR, TRAIN_SRV_DEBUG_AREA, "TRAIN_SERVER: Sending TR command. [ train_id: %d speed: %d delay_until: %d]", train_status->train_id, tr_speed, delay_time ); int current_time = TimeInMs(); int new_time = 0; if ( delay_time > 0 && delay_time > current_time ){ // Prepare the message to send Train_cmd train_cmd; train_cmd.delay_until = delay_time; // Add the cmd that will be sent after the delay ends train_cmd.request.type = ADD_CMD_REQUEST; train_cmd.request.cmd.cmd_type = TRAIN_CMD_TYPE; train_cmd.request.cmd.element_id = train_status->train_id; train_cmd.request.cmd.param = tr_speed; // Push the result into the queue (if the notifier is sleeping it will be awaken at the end of this cycle) train_cmd_queue_push( train_cmd, &server_data->train_cmd_queue ); new_time = delay_time; } else{ bwprintf( COM2, "TRAIN_MOTION: Sending cmd\n - [ Ticks: %d Ms: %d Train: %d Speed: %d ]", current_time / 10, current_time, train_status->train_id, tr_speed ); send_command( TRAIN_CMD_TYPE, train_status->train_id, tr_speed, cmd_server_tid ); // TODO: Do we need to add a "CMD" delay. new_time = current_time; } //bwprintf( COM2, "SENDING TR [ TRAIN_ID: %d SPEED: %d DELAY: %d ]\n", train_status->train_id, tr_speed, delay_time ); if ( tr_speed == TRAIN_STOP_CMD_SPEED ){ train_status->motion_state = TRAIN_DEACCELERATING; train_status->motion_data.time_since_deacceleration = new_time; } else { train_status->motion_state = TRAIN_ACCELERATING; train_status->motion_data.train_speed = tr_speed; train_status->distance_since_speed_change = 0; train_status->time_speed_change = new_time; } }
void receiver() { int ret = -1; int ret2; int tid; char msg[5]; char replymsg[5]; replymsg[0] = 'c'; replymsg[1] = 'a'; replymsg[2] = 'o'; replymsg[3] = '\0'; while (1) { bwprintf(COM2, "Receiver call receive\n"); ret = Receive(&tid, msg, 5); if (ret > 0) { bwprintf(COM2, "Receive msg: %s with origin %d char\n", msg, ret); bwprintf(COM2, "Receiver reply to %d\n", tid); ret2 = Reply(tid, replymsg, 5); bwprintf(COM2, "Reply ret value: %d\n", ret2); } } }
/** * Handles a request for a task. * */ int handle(Task *task, Request *req) { // Handle Interrupts. if (req == 0) { int data; int event = process_interrupt(&data); kevent(event, data); make_ready(task); return 0; } // Handle System Calls. switch (req->request) { case MY_TID: kmytid(task); break; case CREATE: kcreate(task, (int)req->args[0] /* priority */, req->args[1] /* code */, (int)req->args[2] /* args */); break; case MY_PARENT_TID: kmy_parent_tid(task); break; case PASS: make_ready(task); break; case EXIT: kexit(task); break; case SEND: ksend(task, (int)req->args[0], (char *)req->args[1], (int)req->args[2], (char *)req->args[3], (int)req->args[4]); break; case RECEIVE: krecieve(task, (int *)req->args[0], (char *)req->args[1], (int)req->args[2]); break; case REPLY: kreply(task, (int)req->args[0], (char *)req->args[1], (int)req->args[2]); break; case AWAIT_EVENT: kawait(task, (int)req->args[0]); break; case WAIT_TID: kwait_tid(task, (int)req->args[0]); break; case SHUTDOWN: return -1; default: bwprintf(COM2, "Undefined request number %u\n", req->request); break; } return 0; }
void rps_server() { int q[50]; int start = 0; int end = 0; int p1=0, p2=0; char a1, a2; char m[2]; m[1] = 0; int tid; int nump = 0; bwprintf (COM2, "RPS server started.\r\n"); RegisterAs ("RPS"); // bwprintf (COM2, "I AM REGISTERED??? WhoIs (\"RPS\") = %d\r\n",WhoIs ("RPS")); while (1) { Receive (&tid, m, 1); // bwprintf (COM2, "dicks"); switch (m[0]) { case 'J': bwprintf (COM2, "RPS_SERVER: %d joined.\r\n", tid); q[end]=tid; end++; break; case 'R': case 'P': case 'S': if (p1 == 0 || p2 == 0) { m[0] = 'Q'; Reply (tid, m, 1); } if (p1 == tid) { bwprintf (COM2, "RPS_SERVER: Player 1 says %c.\r\n", m[0]); a1 = m[0]; } else { bwprintf (COM2, "RPS_SERVER: Player 2 says %c.\r\n", m[0]); a2 = m[0]; } break; case 'Q': bwprintf (COM2, "RPS_SERVER: %d quit.", tid); if (tid == p1) p1 = 0; else p2 = 0; p1=0;p2=0; break; } if (p1 == p2 && p1 == 0 && end-start >= 2) { p1 = q[start]; p2 = q[start+1]; start += 2; bwprintf (COM2, "\r\n RPS_SERVER: Start game: %d vs. %d.\r\n", p1, p2); nump -= 2; a1 = a2 = 0; Reply (p1, 0, 0); Reply (p2, 0, 0); } if (a1 != 0 && a2 != 0) { if (a1 == 'R' && a2 == 'R') { m[0] = 'T'; Reply (p1, m, 1); m[0] = 'T'; Reply (p2, m, 1); } else if (a1 == 'R' && a2 == 'P') { m[0] = 'L'; Reply (p1, m, 1); m[0] = 'W'; Reply (p2, m, 1); } else if (a1 == 'R' && a2 == 'S') { m[0] = 'W'; Reply (p1, m, 1); m[0] = 'L'; Reply (p2, m, 1); } else if (a1 == 'P' && a2 == 'R') { m[0] = 'W'; Reply (p1, m, 1); m[0] = 'L'; Reply (p2, m, 1); } else if (a1 == 'P' && a2 == 'P') { m[0] = 'T'; Reply (p1, m, 1); m[0] = 'T'; Reply (p2, m, 1); } else if (a1 == 'P' && a2 == 'S') { m[0] = 'L'; Reply (p1, m, 1); m[0] = 'W'; Reply (p2, m, 1); } else if (a1 == 'S' && a2 == 'R') { m[0] = 'L'; Reply (p1, m, 1); m[0] = 'W'; Reply (p2, m, 1); } else if (a1 == 'S' && a2 == 'P') { m[0] = 'W'; Reply (p1, m, 1); m[0] = 'L'; Reply (p2, m, 1); } else if (a1 == 'S' && a2 == 'S') { m[0] = 'T'; Reply (p1, m, 1); m[0] = 'T'; Reply (p2, m, 1); } a1 = a2 = 0; } } }
void first() { bwprintf (COM2, "I AM FIRST USER.\n\tMODE IS "); print_mode (); bwputstr (COM2, ".\n\tCREATE???\n"); //int z = Create (0xABCDEF01, (void*)0x10FEDCBA); int z = Create (3, second); int i=0,j=0,k=0; while (1) { bwprintf (COM2, "I AM FIRST USER.\n\tKERNEL SAID %d\n\tMODE IS ",z); print_mode (); bwputstr (COM2, ".\n\tPASS??\n"); i++; if (i>10) j++; if (j>10) k++; i %= 11; j %= 11; // bwprintf (COM2, "(i,j,k) = (%d,%d,%d)\n",i,j,k); Pass(); bwprintf (COM2, "I AM FIRST USER.\n\tMODE IS "); print_mode (); bwputstr (COM2, ".\n\tEXIT????\n"); Exit(); } // int i = 0xFFFFF; int r; while (i--) { bwprintf(COM2, "Hey, I'm a user(%d)!\n", i); r = swtch(i); bwprintf(COM2, "And the kernel gave me %d!\n", r); for (r = 0; r < 500000; ++r); } bwputstr (COM2, "CPU Mode: "); print_mode(); bwputstr (COM2, "\n\n"); int x = 42; int b[5]; b[0] = 0xDEADBEEF; b[1] = 0xDEADBEEF; b[2] = 0xDEADBEEF; b[3] = 0xDEADBEEF; b[4] = 0xDEADBEEF; bwprintf (COM2, "x is %d\n", x); i = 5; while (i--) { bwprintf (COM2, "b[%d] = ", i); bwputr(COM2, b[i]); bwputstr (COM2, "\n");} b[2] -= x; b[3]--; bwputstr (COM2, "now b[2] = b[2] - x and b[3] = b[3] - 1...\n"); i = 5; while (i--) { bwprintf (COM2, "b[%d] = ", i); bwputr(COM2, b[i]); bwputstr (COM2, "\n");} }
void bootstrap (struct td *tds, void (*f)(), int *stacks) { bwprintf (COM2, "[s[2J[2;1000r[;H[45mThis is a taskbar." "[K[m[u"); bwprintf(COM2, "[?25l[2;1H"); bwputstr(COM2, "Welcome to\r\n" "+--------------------------------------------------+\r\n" "| ______ __ ______ ____ |\r\n" "| / __/ /________ __ _ / / ___ / / __ \\/ __/ |\r\n" "| _\\ \\/ __/ __/ _ \\/ ' \\/ _ \\/ _ \\/ / /_/ /\\ \\ |\r\n" "| /___/\\__/_/ \\___/_/_/_/_.__/\\___/_/\\____/___/ |\r\n" "| v0.0.3 (Techno Fitness) |\r\n" "+--------------------------------------------------+\r\n\r\n"); DPRINTFUNC ("bootstrap"); install_handler (); DPRINTOK ("Interrupt handler installed.\n"); enable_timer (); enable_interrupts (); _kCreate(tds, IDLE, f, 0, 0, stacks); DPRINTOK ("First user task created.\n"); DPRINTOK ("Booting complete.\n"); }
int aput2c( int channel, char a, char b ) { static int com1TxServer = -1; static int com2TxServer = -1; int serverTid; if (channel == COM1) { if (com1TxServer < 0) { if ( (com1TxServer = WhoIs( "UART1_TX_SERVER" )) < 0 ) { bwprintf(COM2, "aputc got bad WhoIs result: %d\n\r", com1TxServer); Halt(); } } serverTid = com1TxServer; } else if (channel == COM2) { if (com2TxServer < 0) { if ( (com2TxServer = WhoIs( "UART2_TX_SERVER" )) < 0 ) { bwprintf(COM2, "aputc got bad WhoIs result: %d\n\r", com2TxServer); Halt(); } } serverTid = com2TxServer; } else { // else fail with a bad channel return -1; } // create and populate the message structure that we're going to send to the server MsgUartServer msg; msg.type = MSG_TX_CLIENT; msg.data[0] = a; msg.data[1] = b; msg.length = 2; int ret = Send(serverTid, (char*) &msg, sizeof(MsgUartServer), NULL, 0); if(ret < 0) return ret; return 0; }
int agetc( int channel ) { static int rxservers[2]; rxservers[0] = -1; rxservers[1] = -1; // fail with a bad channel if(channel < 0 || channel >= 2) return -1; while(rxservers[channel] < 0) { rxservers[channel] = WhoIs( channel ? "UART2_RX_SERVER" : "UART1_RX_SERVER" ); SDEBUG( bwprintf(COM1, "Got a tid of %d for the channel %d RX server.\n\r", rxservers[channel], channel); ) } // create and populate the message structure that we're going to send to the server MsgUartServer msg; msg.type = MSG_RX_CLIENT; msg.length = 1; int ret = Send(rxservers[channel], (char*) &msg, sizeof(MsgUartServer), (char*) &msg, sizeof(MsgUartServer)); if(ret < 0) { SDEBUG( bwprintf(COM1, "Shit is broken: Send returned %d\n\r", ret); ) return ret;
// ----------------------------------------------------------------------------------------------------------------------------------------------------- // Landmark Methods // ----------------------------------------------------------------------------------------------------------------------------------------------------- int has_train_arrived( Train_status *train_status ){ if ( train_status->train_state == TRAIN_STATE_MOVE_TO_GOAL ){ Train_position current_position = train_status->current_position; Train_position goal_position = train_status->current_goal; if ( current_position.landmark == goal_position.landmark && current_position.offset >= ( goal_position.offset - 2 ) ) { bwprintf( COM2, "ARRIVED! Total Distance: %d", train_status->distance_since_speed_change ); return 1; } } return 0; }