void main_game_loop(nstate *state) { wait_for_start(state); while(state->state != STATE_EXIT) { if(state->state == STATE_RUNNING) run_game(state); if(state->state == STATE_STOPPED) wait_for_start(state); if(state->state != STATE_EXIT) new_game(state); } }
void play_game(void) { // Identify myself fprintf(stdout, "#name %s\n", name); fflush(stdout); // Wait for start of game wait_for_start(); // Main game loop for (;;) { if (current_player == my_player) { // My turn // Check if game is over (optional) // Determine next move next_move(); // Apply it to local state // Tell the world print_and_recv_echo(next_move_cache); // It is the opponents turn current_player = (current_player == player1) ? player2 : player1; } else { // Wait for move from other player // Get server's next instruction read_msg_and_tokenize(); if (tokens_size == 6 && strcmp(tokens[0], "MOVE") == 0) { // Translate to local coordinates and update our local state // It is now my turn current_player = (current_player == player1) ? player2 : player1; } else if (tokens_size == 4 && strcmp(tokens[0], "FINAL") == 0 && strcmp(tokens[2], "BEATS") == 0) { // Game over if (strcmp(tokens[1], name) == 0 && strcmp(tokens[3], opp_name) == 0) { fprintf(stderr, "I, %s, have won!\n", name); fflush(stderr); } else if (strcmp(tokens[3], name) == 0&& strcmp(tokens[1], opp_name) == 0) { fprintf(stderr, "I, %s, have lost.\n", name); fflush(stderr); } else { fprintf(stderr, "Did not find expected players in FINAL command.\n"); fprintf(stderr, "Found '%s' and '%s'.\n", tokens[1], tokens[3]); fprintf(stderr, "Expected '%s' and '%s'.\n", name, opp_name); fprintf(stderr, "Received message '%s'\n", message); fflush(stderr); } break; } else { // Unknown command fprintf(stderr, "Unknown command of '%s' from the server\n", message); fflush(stderr); } } } }
static void do_pio(int fd, u32 runs, struct benchmark *bench, const void *opts) { if (iopl(3) == -1) err(errno, "iopl"); send_ack(fd); if (wait_for_start(fd)) { u32 i; for (i = 0; i < runs; i++) outb(0xFF, 0x512); send_ack(fd); } }
static void do_context_switch(int fd, u32 runs, struct benchmark *b, const void *opts) { char c = 1; int fds1[2], fds2[2], child; if (pipe(fds1) != 0 || pipe(fds2) != 0) err(1, "Creating pipe"); child = fork(); if (child == -1) err(1, "forking"); if (child > 0) { close(fds1[0]); close(fds2[1]); send_ack(fd); if (wait_for_start(fd)) { while ((int)runs > 0) { write(fds1[1], &c, 1); read(fds2[0], &c, 1); runs -= 2; } if (runs == 0) send_ack(fd); } else kill(child, SIGTERM); waitpid(child, NULL, 0); close(fds1[1]); close(fds2[0]); } else { close(fds2[0]); close(fds1[1]); while ((int)runs > 0) { read(fds1[0], &c, 1); write(fds2[1], &c, 1); runs -= 2; } if ((int)runs == -1) send_ack(fd); exit(0); } }
void roi_start_multiprogram() { sem_t *wait = sem_open(waiting_sem_name, 0, 0644, 0); if (wait == SEM_FAILED) { if (errno == ENOENT) { perror("Didn't find running multiprogram coordinator, proceeding as usual"); roi_start(); } } else { sem_trywait(wait); sem_t *go = sem_open(go_sem_name, 0, 0644, 0); if (go == SEM_FAILED) { perror("'go' semaphore failed"); abort(); } wait_for_start(go); } }
static void do_bandwidth_bench(int fd, u32 runs, struct benchmark *bench, const void *opts) { char *mem = malloc(NET_BANDWIDTH_SIZE); if (!mem) err(1, "allocating %i bytes", NET_BANDWIDTH_SIZE); send_ack(fd); if (wait_for_start(fd)) { u32 i; /* We always send 1MB of warmup, to open TCP window. */ send_data(fd, mem, NET_WARMUP_BYTES); for (i = 0; i < runs; i++) send_data(fd, mem, NET_BANDWIDTH_SIZE); send_ack(fd); } free(mem); }
int a2dp_write(a2dpData d, const void* buffer, int count) { struct bluetooth_data* data = (struct bluetooth_data*)d; uint8_t* src = (uint8_t *)buffer; int codesize; int err, ret = 0; long frames_left = count; int encoded; unsigned int written; const char *buff; int did_configure = 0; #ifdef ENABLE_TIMING uint64_t begin, end; DBG("********** a2dp_write **********"); begin = get_microseconds(); #endif err = wait_for_start(data, WRITE_TIMEOUT); if (err < 0) return err; codesize = data->codesize; while (frames_left >= codesize) { /* Enough data to encode (sbc wants 512 byte blocks) */ encoded = sbc_encode(&(data->sbc), src, codesize, data->buffer + data->count, sizeof(data->buffer) - data->count, &written); if (encoded <= 0) { ERR("Encoding error %d", encoded); goto done; } VDBG("sbc_encode returned %d, codesize: %d, written: %d\n", encoded, codesize, written); src += encoded; data->count += written; data->frame_count++; data->samples += encoded; data->nsamples += encoded; /* No space left for another frame then send */ if ((data->count + written >= data->link_mtu) || (data->count + written >= BUFFER_SIZE)) { VDBG("sending packet %d, count %d, link_mtu %u", data->seq_num, data->count, data->link_mtu); err = avdtp_write(data); if (err < 0) return err; } ret += encoded; frames_left -= encoded; } if (frames_left > 0) ERR("%ld bytes left at end of a2dp_write\n", frames_left); done: #ifdef ENABLE_TIMING end = get_microseconds(); print_time("a2dp_write total", begin, end); #endif return ret; }
void Client::play_game() { // Identify myself std::cout << "#name " << name << std::endl; // Wait for start of game wait_for_start(); // Main game loop for (;;) { if (current_player == my_player) { // My turn // Check if game is over /* if (gs.game_over()) { std::cerr << "I, " << name << ", have lost" << std::endl; switch_current_player(); continue; } */ // Determine next move const Move m = next_move(); // Apply it locally // gs.apply_move(m); // Tell the world print_and_recv_echo(m); // It is the opponents turn switch_current_player(); } else { // Wait for move from other player // Get server's next instruction std::string server_msg = read_msg(); const std::vector<std::string> tokens = tokenize_msg(server_msg); if (tokens.size() == 5 && tokens[0] == "MOVE") { // Translate to local coordinates and update our local state // const Move m = gs.translate_to_local(tokens); // gs.apply_move(m); // It is now my turn switch_current_player(); } else if (tokens.size() == 4 && tokens[0] == "FINAL" && tokens[2] == "BEATS") { // Game over if (tokens[1] == name && tokens[3] == opp_name) std::cerr << "I, " << name << ", have won!" << std::endl; else if (tokens[3] == name && tokens[1] == opp_name) std::cerr << "I, " << name << ", have lost." << std::endl; else std::cerr << "Did not find expected players in FINAL command.\n" << "Found '" << tokens[1] << "' and '" << tokens[3] << "'. " << "Expected '" << name << "' and '" << opp_name << "'.\n" << "Received message '" << server_msg << "'" << std::endl; break; } else { // Unknown command std::cerr << "Unknown command of '" << server_msg << "' from the server" << std::endl; } } } }
/* * ======== bridge_brd_start ======== * purpose: * Initializes DSP MMU and Starts DSP. * * Preconditions: * a) DSP domain is 'ACTIVE'. * b) DSP_RST1 is asserted. * b) DSP_RST2 is released. */ static int bridge_brd_start(struct bridge_dev_context *dev_ctxt, u32 dsp_addr) { int status = 0; struct bridge_dev_context *dev_context = dev_ctxt; struct iommu *mmu = NULL; struct shm_segs *sm_sg; int l4_i = 0, tlb_i = 0; u32 sg0_da = 0, sg1_da = 0; struct bridge_ioctl_extproc *tlb = dev_context->atlb_entry; u32 dw_sync_addr = 0; u32 ul_shm_base; /* Gpp Phys SM base addr(byte) */ u32 ul_shm_base_virt; /* Dsp Virt SM base addr */ u32 ul_tlb_base_virt; /* Base of MMU TLB entry */ /* Offset of shm_base_virt from tlb_base_virt */ u32 ul_shm_offset_virt; struct cfg_hostres *resources = NULL; u32 temp; u32 ul_dsp_clk_rate; u32 ul_dsp_clk_addr; u32 ul_bios_gp_timer; u32 clk_cmd; struct io_mgr *hio_mgr; u32 ul_load_monitor_timer; struct omap_dsp_platform_data *pdata = omap_dspbridge_dev->dev.platform_data; /* The device context contains all the mmu setup info from when the * last dsp base image was loaded. The first entry is always * SHMMEM base. */ /* Get SHM_BEG - convert to byte address */ (void)dev_get_symbol(dev_context->hdev_obj, SHMBASENAME, &ul_shm_base_virt); ul_shm_base_virt *= DSPWORDSIZE; DBC_ASSERT(ul_shm_base_virt != 0); /* DSP Virtual address */ ul_tlb_base_virt = dev_context->sh_s.seg0_da; DBC_ASSERT(ul_tlb_base_virt <= ul_shm_base_virt); ul_shm_offset_virt = ul_shm_base_virt - (ul_tlb_base_virt * DSPWORDSIZE); /* Kernel logical address */ ul_shm_base = dev_context->sh_s.seg0_va + ul_shm_offset_virt; DBC_ASSERT(ul_shm_base != 0); /* 2nd wd is used as sync field */ dw_sync_addr = ul_shm_base + SHMSYNCOFFSET; /* Write a signature into the shm base + offset; this will * get cleared when the DSP program starts. */ if ((ul_shm_base_virt == 0) || (ul_shm_base == 0)) { pr_err("%s: Illegal SM base\n", __func__); status = -EPERM; } else __raw_writel(0xffffffff, dw_sync_addr); if (!status) { resources = dev_context->resources; if (!resources) status = -EPERM; /* Assert RST1 i.e only the RST only for DSP megacell */ if (!status) { (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST1_IVA2_MASK, OMAP3430_RST1_IVA2_MASK, OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL); /* Mask address with 1K for compatibility */ __raw_writel(dsp_addr & OMAP3_IVA2_BOOTADDR_MASK, OMAP343X_CTRL_REGADDR( OMAP343X_CONTROL_IVA2_BOOTADDR)); /* * Set bootmode to self loop if dsp_debug flag is true */ __raw_writel((dsp_debug) ? OMAP3_IVA2_BOOTMOD_IDLE : 0, OMAP343X_CTRL_REGADDR( OMAP343X_CONTROL_IVA2_BOOTMOD)); } } if (!status) { (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST2_IVA2_MASK, 0, OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL); mmu = dev_context->dsp_mmu; if (mmu) dsp_mmu_exit(mmu); mmu = dsp_mmu_init(); if (IS_ERR(mmu)) { dev_err(bridge, "dsp_mmu_init failed!\n"); dev_context->dsp_mmu = NULL; status = (int)mmu; } } if (!status) { dev_context->dsp_mmu = mmu; sm_sg = &dev_context->sh_s; sg0_da = iommu_kmap(mmu, sm_sg->seg0_da, sm_sg->seg0_pa, sm_sg->seg0_size, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32); if (IS_ERR_VALUE(sg0_da)) { status = (int)sg0_da; sg0_da = 0; } } if (!status) { sg1_da = iommu_kmap(mmu, sm_sg->seg1_da, sm_sg->seg1_pa, sm_sg->seg1_size, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32); if (IS_ERR_VALUE(sg1_da)) { status = (int)sg1_da; sg1_da = 0; } } if (!status) { u32 da; for (tlb_i = 0; tlb_i < BRDIOCTL_NUMOFMMUTLB; tlb_i++) { if (!tlb[tlb_i].ul_gpp_pa) continue; dev_dbg(bridge, "IOMMU %d GppPa: 0x%x DspVa 0x%x Size" " 0x%x\n", tlb_i, tlb[tlb_i].ul_gpp_pa, tlb[tlb_i].ul_dsp_va, tlb[tlb_i].ul_size); da = iommu_kmap(mmu, tlb[tlb_i].ul_dsp_va, tlb[tlb_i].ul_gpp_pa, PAGE_SIZE, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32); if (IS_ERR_VALUE(da)) { status = (int)da; break; } } } if (!status) { u32 da; l4_i = 0; while (l4_peripheral_table[l4_i].phys_addr) { da = iommu_kmap(mmu, l4_peripheral_table[l4_i]. dsp_virt_addr, l4_peripheral_table[l4_i]. phys_addr, PAGE_SIZE, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32); if (IS_ERR_VALUE(da)) { status = (int)da; break; } l4_i++; } } /* Lock the above TLB entries and get the BIOS and load monitor timer * information */ if (!status) { /* Enable the BIOS clock */ (void)dev_get_symbol(dev_context->hdev_obj, BRIDGEINIT_BIOSGPTIMER, &ul_bios_gp_timer); (void)dev_get_symbol(dev_context->hdev_obj, BRIDGEINIT_LOADMON_GPTIMER, &ul_load_monitor_timer); if (ul_load_monitor_timer != 0xFFFF) { clk_cmd = (BPWR_ENABLE_CLOCK << MBX_PM_CLK_CMDSHIFT) | ul_load_monitor_timer; dsp_peripheral_clk_ctrl(dev_context, &clk_cmd); } else { dev_dbg(bridge, "Not able to get the symbol for Load " "Monitor Timer\n"); } if (ul_bios_gp_timer != 0xFFFF) { clk_cmd = (BPWR_ENABLE_CLOCK << MBX_PM_CLK_CMDSHIFT) | ul_bios_gp_timer; dsp_peripheral_clk_ctrl(dev_context, &clk_cmd); } else { dev_dbg(bridge, "Not able to get the symbol for BIOS Timer\n"); } /* Set the DSP clock rate */ (void)dev_get_symbol(dev_context->hdev_obj, "_BRIDGEINIT_DSP_FREQ", &ul_dsp_clk_addr); /*Set Autoidle Mode for IVA2 PLL */ (*pdata->dsp_cm_write)(1 << OMAP3430_AUTO_IVA2_DPLL_SHIFT, OMAP3430_IVA2_MOD, OMAP3430_CM_AUTOIDLE_PLL); if ((unsigned int *)ul_dsp_clk_addr != NULL) { /* Get the clock rate */ ul_dsp_clk_rate = dsp_clk_get_iva2_rate(); dev_dbg(bridge, "%s: DSP clock rate (KHZ): 0x%x \n", __func__, ul_dsp_clk_rate); (void)bridge_brd_write(dev_context, (u8 *) &ul_dsp_clk_rate, ul_dsp_clk_addr, sizeof(u32), 0); } /* * Enable Mailbox events and also drain any pending * stale messages. */ dev_context->mbox = omap_mbox_get("dsp"); if (IS_ERR(dev_context->mbox)) { dev_context->mbox = NULL; pr_err("%s: Failed to get dsp mailbox handle\n", __func__); status = -EPERM; } } if (!status) { dev_context->mbox->rxq->callback = (int (*)(void *))io_mbox_msg; /*PM_IVA2GRPSEL_PER = 0xC0;*/ temp = readl(resources->dw_per_pm_base + 0xA8); temp = (temp & 0xFFFFFF30) | 0xC0; writel(temp, resources->dw_per_pm_base + 0xA8); /*PM_MPUGRPSEL_PER &= 0xFFFFFF3F; */ temp = readl(resources->dw_per_pm_base + 0xA4); temp = (temp & 0xFFFFFF3F); writel(temp, resources->dw_per_pm_base + 0xA4); /*CM_SLEEPDEP_PER |= 0x04; */ temp = readl(resources->dw_per_base + 0x44); temp = (temp & 0xFFFFFFFB) | 0x04; writel(temp, resources->dw_per_base + 0x44); /*CM_CLKSTCTRL_IVA2 = 0x00000003 -To Allow automatic transitions */ (*pdata->dsp_cm_write)(OMAP34XX_CLKSTCTRL_ENABLE_AUTO, OMAP3430_IVA2_MOD, OMAP2_CM_CLKSTCTRL); /* Let DSP go */ dev_dbg(bridge, "%s Unreset\n", __func__); /* release the RST1, DSP starts executing now .. */ (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST1_IVA2_MASK, 0, OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL); dev_dbg(bridge, "Waiting for Sync @ 0x%x\n", dw_sync_addr); dev_dbg(bridge, "DSP c_int00 Address = 0x%x\n", dsp_addr); if (dsp_debug) while (__raw_readw(dw_sync_addr)) ;; /* Wait for DSP to clear word in shared memory */ /* Read the Location */ if (!wait_for_start(dev_context, dw_sync_addr)) status = -ETIMEDOUT; /* Start wdt */ dsp_wdt_sm_set((void *)ul_shm_base); dsp_wdt_enable(true); status = dev_get_io_mgr(dev_context->hdev_obj, &hio_mgr); if (hio_mgr) { io_sh_msetting(hio_mgr, SHM_OPPINFO, NULL); /* Write the synchronization bit to indicate the * completion of OPP table update to DSP */ __raw_writel(0XCAFECAFE, dw_sync_addr); /* update board state */ dev_context->dw_brd_state = BRD_RUNNING; return 0; } else { dev_context->dw_brd_state = BRD_UNKNOWN; } } while (tlb_i--) { if (!tlb[tlb_i].ul_gpp_pa) continue; iommu_kunmap(mmu, tlb[tlb_i].ul_gpp_va); } while (l4_i--) iommu_kunmap(mmu, l4_peripheral_table[l4_i].dsp_virt_addr); if (sg0_da) iommu_kunmap(mmu, sg0_da); if (sg1_da) iommu_kunmap(mmu, sg1_da); return status; }
int main() { // PC connection Serial pc(0, 9600); // Turn all leds on while waiting Bus leds(4, LED4, LED3, LED2, LED1); leds.mode(Output); leds.write_all(1); // Config motors Motor motorB (P22, P21, P23); Motor motorA (P25, P24, P26); // Turn lights on FarolA = 1; FarolB = 1; // The interface with the remote control Serial cmdLink(1, 38400); // Read until the control says START wait_for_start(cmdLink); // Seteo velocidad y direccion del motor A motorA.set_sentido(ADELANTE); motorA.Velocidad = MARCHA_IZQ; // Seteo velocidad y direccion del motor B motorB.set_sentido(ADELANTE); motorB.Velocidad = MARCHA_DER; while (1) { // encendemos los leds de acuerdo con los sensores //estado_sensores(); unsigned char cmd = get_command(cmdLink); leds.write(cmd); // Stop makes the car stop until the next start if (cmd == STOP) { motorA.set_sentido(DETENIDO); motorB.set_sentido(DETENIDO); wait_for_start(cmdLink); } // Go faster else if (cmd == START) { motorA.Velocidad = 0.9; motorB.Velocidad = 0.9; } // Enter line_follower mode else if (cmd == LINE_FOLLOWER) { line_follower(motorA, motorB); } // Go forward else if (cmd == (CENTER+FORWARD)) { motorA.set_sentido(ADELANTE); motorA.Velocidad = MARCHA_IZQ; motorB.set_sentido(ADELANTE); motorB.Velocidad = MARCHA_DER; } // Go backward else if (cmd == (CENTER+BACKWARD)) { motorA.set_sentido(ATRAS); motorA.Velocidad = MARCHA_IZQ; motorB.set_sentido(ATRAS); motorB.Velocidad = MARCHA_DER; } // Go left else if (cmd > LEFT && cmd < RIGHT) { motorB.set_sentido(ADELANTE); float f = (cmd - LEFT) / 10.; if (f > 0.7) { motorA.set_sentido(ATRAS); motorA.Velocidad = MARCHA_IZQ*(f-0.2); motorB.Velocidad = MARCHA_DER*(f-0.2); } else { motorA.set_sentido(ADELANTE); motorA.Velocidad = MARCHA_IZQ*(0.7-f); motorB.Velocidad = MARCHA_DER*(1+f); } } // Go right else if (cmd > RIGHT && cmd < BACKWARD) { motorA.set_sentido(ADELANTE); float f = (cmd - RIGHT) / 10.; if (f > 0.7) { motorB.set_sentido(ATRAS); motorA.Velocidad = MARCHA_IZQ*(f-0.2); motorB.Velocidad = MARCHA_DER*(f-0.2); } else { motorB.set_sentido(ADELANTE); motorA.Velocidad = MARCHA_IZQ*(1+f); motorB.Velocidad = MARCHA_DER*(0.7-f); } } else { cmd -= BACKWARD; if (cmd > LEFT && cmd < RIGHT) { float f = (cmd - LEFT) / 10.; motorB.set_sentido(ATRAS); if (f > 0.7) { motorA.set_sentido(ADELANTE); motorA.Velocidad = MARCHA_IZQ*(f-0.2); motorB.Velocidad = MARCHA_DER*(f-0.2); } else { motorA.set_sentido(ATRAS); motorA.Velocidad = MARCHA_IZQ*(0.9-f); motorB.Velocidad = MARCHA_DER*(1+f); } } else { float f = (cmd - RIGHT) / 10.; motorA.set_sentido(ATRAS); if (f > 0.7) { motorB.set_sentido(ADELANTE); motorA.Velocidad = MARCHA_IZQ*(f-0.2); motorB.Velocidad = MARCHA_DER*(f-0.2); } else { motorB.set_sentido(ATRAS); motorA.Velocidad = MARCHA_IZQ*(1+f); motorB.Velocidad = MARCHA_DER*(0.9-f); } } } } }