//-------------------------------------------------------------------------------------------- bool_t ui_widgetRemoveMask( ui_Widget_t * pw, BIT_FIELD mbits ) { if ( NULL == pw ) return bfalse; UNSET_BIT( pw->mask, mbits ); UNSET_BIT( pw->state, mbits ); return btrue; }
int CHttpClient::Read(char * szBuffer, int iLen) { // Set the socket to non blocking #ifdef _WIN32 u_long sockopt = 1; ioctlsocket(m_iSocket, FIONBIO, &sockopt); #else int flags = fcntl(m_iSocket, F_GETFL, 0); UNSET_BIT(flags, O_NONBLOCK); fcntl(m_iSocket, F_SETFL, flags); #endif // Try to receive int iBytesRead = recv(m_iSocket, szBuffer, iLen, 0); // Set the socket to blocking #ifdef _WIN32 sockopt = 0; ioctlsocket(m_iSocket, FIONBIO, &sockopt); #else SET_BIT(flags, O_NONBLOCK); fcntl(m_iSocket, F_SETFL, flags); #endif return iBytesRead; }
//-------------------------------------------------------------------------------------------- BIT_FIELD BIT_FIELD_unset_bits( BIT_FIELD val, BIT_FIELD bits ) { BIT_FIELD new_val = val; UNSET_BIT( new_val, bits ); return new_val; }
//-------------------------------------------------------------------------------------------- BIT_FIELD BIT_FIELD_clear_all_bits( BIT_FIELD val, BIT_FIELD bits ) { BIT_FIELD new_val = val; UNSET_BIT( new_val, bits ); return new_val; }
/*===========================================================================* * alloc_pages * *===========================================================================*/ static phys_bytes alloc_pages(int pages, int memflags) { phys_bytes boundary16 = 16 * 1024 * 1024 / VM_PAGE_SIZE; phys_bytes boundary1 = 1 * 1024 * 1024 / VM_PAGE_SIZE; phys_bytes mem = NO_MEM, i; /* page number */ int maxpage = NUMBER_PHYSICAL_PAGES - 1; static int lastscan = -1; int startscan, run_length; if(memflags & PAF_LOWER16MB) maxpage = boundary16 - 1; else if(memflags & PAF_LOWER1MB) maxpage = boundary1 - 1; else { /* no position restrictions: check page cache */ if(pages == 1) { while(free_page_cache_size > 0) { i = free_page_cache[free_page_cache_size-1]; if(page_isfree(i)) { free_page_cache_size--; mem = i; assert(mem != NO_MEM); run_length = 1; break; } free_page_cache_size--; } } } if(lastscan < maxpage && lastscan >= 0) startscan = lastscan; else startscan = maxpage; if(mem == NO_MEM) mem = findbit(0, startscan, pages, memflags, &run_length); if(mem == NO_MEM) mem = findbit(0, maxpage, pages, memflags, &run_length); if(mem == NO_MEM) return NO_MEM; /* remember for next time */ lastscan = mem; for(i = mem; i < mem + pages; i++) { UNSET_BIT(free_pages_bitmap, i); } if(memflags & PAF_CLEAR) { int s; if ((s= sys_memset(NONE, 0, CLICK_SIZE*mem, VM_PAGE_SIZE*pages)) != OK) panic("alloc_mem: sys_memset failed: %d", s); } return mem; }
int main(void) { char x = 0x0F, i; for (i = 0; i < 8; i++) { printf("%d : %d\n", i, CHECK_BIT(x, i) > 0); if (!CHECK_BIT(x, i)) SET_BIT(x, i); else UNSET_BIT(x, i); } printf("x: %x\n", x); }
int main(void) { unsigned int *bitarray = NULL; unsigned int bitarray_size; unsigned int i, j, n; double s; if (sizeof(unsigned int) != BYTES_PER_INT) { fprintf(stderr, "size of 'unsigned int' is expected to be %d instead of %d\n", BYTES_PER_INT, sizeof(unsigned int)); return 1; } /* this is how many 'unsigned int's needed to store PRIMES_UPPER_BOUND bits */ bitarray_size = PRIMES_UPPER_BOUND / BITS_PER_INT + ((PRIMES_UPPER_BOUND % BITS_PER_INT) == 0 ? 0 : 1); /* printf("PRIMES_UPPER_BOUND=%d\n", PRIMES_UPPER_BOUND); */ /* printf("BITS_PER_INT=%d\n", BITS_PER_INT); */ /* printf("bitarray_size=%d\n", bitarray_size); */ /* allocate bitarray and turn all the bits on */ bitarray = (unsigned int*)malloc(sizeof(unsigned int)*bitarray_size); for (i = 0; i < bitarray_size; i++) bitarray[i] = ALL_ON; /* determine prime numbers by Sieve of Eratosthenes */ for (i = 2; i <= PRIMES_UPPER_BOUND; i++) { if (! IS_BIT_SET(bitarray, i)) /* i is not a prime */ continue; /* i is prime: remove from the list all the numbers * divisible by i except itself */ /* printf("prime %6d\n", i); */ for (n = 2; ; n++) { j = i*n; if (j >= PRIMES_UPPER_BOUND) break; UNSET_BIT(bitarray, j); } } /* compute sum of all the primes */ s = 0; for (i = 2; i < PRIMES_UPPER_BOUND; i++) if (IS_BIT_SET(bitarray, i)) s += i; printf("%.0f\n", s); free(bitarray); return 0; }
void CIVVehicle::SetCanBeVisiblyDamaged(bool bState) { // Do we have a valid vehicle pointer? IVVehicle * pVehicle = GetVehicle(); if(pVehicle) { if(bState) SET_BIT(pVehicle->m_byteFlags9, 4); else UNSET_BIT(pVehicle->m_byteFlags9, 4); } }
/*===========================================================================* * lin_lin_copy * *===========================================================================*/ static int lin_lin_copy(struct proc *srcproc, vir_bytes srclinaddr, struct proc *dstproc, vir_bytes dstlinaddr, vir_bytes bytes) { u32_t addr; proc_nr_t procslot; assert(vm_running); assert(nfreepdes >= MAX_FREEPDES); assert(get_cpulocal_var(ptproc)); assert(get_cpulocal_var(proc_ptr)); assert(read_cr3() == get_cpulocal_var(ptproc)->p_seg.p_cr3); procslot = get_cpulocal_var(ptproc)->p_nr; assert(procslot >= 0 && procslot < I386_VM_DIR_ENTRIES); if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE)); if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE)); assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE)); assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v); if(srcproc) assert(!RTS_ISSET(srcproc, RTS_VMINHIBIT)); if(dstproc) assert(!RTS_ISSET(dstproc, RTS_VMINHIBIT)); while(bytes > 0) { phys_bytes srcptr, dstptr; vir_bytes chunk = bytes; int changed = 0; #ifdef CONFIG_SMP unsigned cpu = cpuid; if (GET_BIT(srcproc->p_stale_tlb, cpu)) { changed = 1; UNSET_BIT(srcproc->p_stale_tlb, cpu); } if (GET_BIT(dstproc->p_stale_tlb, cpu)) { changed = 1; UNSET_BIT(dstproc->p_stale_tlb, cpu); } #endif /* Set up 4MB ranges. */ srcptr = createpde(srcproc, srclinaddr, &chunk, 0, &changed); dstptr = createpde(dstproc, dstlinaddr, &chunk, 1, &changed); if(changed) reload_cr3(); /* Copy pages. */ PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr); if(addr) { /* If addr is nonzero, a page fault was caught. */ if(addr >= srcptr && addr < (srcptr + chunk)) { return EFAULT_SRC; } if(addr >= dstptr && addr < (dstptr + chunk)) { return EFAULT_DST; } panic("lin_lin_copy fault out of range"); /* Not reached. */ return EFAULT; } /* Update counter and addresses for next iteration, if any. */ bytes -= chunk; srclinaddr += chunk; dstlinaddr += chunk; } if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE)); if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE)); assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE)); assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v); return OK; }
//-------------------------------------------------------------------------------------------- bool Inventory::add_item( ObjectRef iowner, ObjectRef iitem, uint8_t inventorySlot, bool ignoreKurse ) { // Are owner and item valid? if (!_currentModule->getObjectHandler().exists(iowner) || !_currentModule->getObjectHandler().exists(iitem)) { return false; } Object *powner = _currentModule->getObjectHandler().get(iowner); const std::shared_ptr<Object> &pitem = _currentModule->getObjectHandler()[iitem]; // Does the owner have free slot in her inventory? if (inventorySlot >= powner->getInventory().getMaxItems()) { return false; } // If there is an item in the slot, do nothing. if (powner->getInventory().getItem(inventorySlot)) { return false; } // Don't allow sub-inventories. if (pitem->isInsideInventory()) { return false; } // Kursed? if (pitem->iskursed && !ignoreKurse) { // Flag the item as not put away. SET_BIT(pitem->ai.alert, ALERTIF_NOTPUTAWAY); if (powner->isPlayer()) DisplayMsg_printf("%s is sticky...", pitem->getName().c_str()); return false; } // too big item? if (pitem->getProfile()->isBigItem()) { SET_BIT(pitem->ai.alert, ALERTIF_NOTPUTAWAY); if (powner->isPlayer()) DisplayMsg_printf("%s is too big to be put away...", pitem->getName().c_str()); return false; } // Check if item can be stacked on other items. ObjectRef stack = Inventory::hasStack(iitem, iowner); if ( _currentModule->getObjectHandler().exists( stack ) ) { // We found a similar, stackable item in the inventory. Object *pstack = _currentModule->getObjectHandler().get( stack ); // Reveal the name of the item or the stack. if (pitem->nameknown || pstack->getProfile()->isNameKnown()) { pitem->nameknown = true; pstack->nameknown = true; } // Reveal the usage of the item or the stack. if (pitem->getProfile()->isUsageKnown() || pstack->getProfile()->isUsageKnown()) { pitem->getProfile()->makeUsageKnown(); pstack->getProfile()->makeUsageKnown(); } // Add the item ammo to the stack. int newammo = pitem->ammo + pstack->ammo; if (newammo <= pstack->ammomax) { // All transfered, so kill the in hand item pstack->ammo = newammo; pitem->requestTerminate(); return true; } else { // Only some were transfered, pitem->ammo = pitem->ammo + pstack->ammo - pstack->ammomax; pstack->ammo = pstack->ammomax; SET_BIT( powner->ai.alert, ALERTIF_TOOMUCHBAGGAGE ); } } else { //@todo: implement weight check here // Make sure we have room for another item //if ( pchr_pack->count >= Inventory::MAXNUMINPACK ) // { // SET_BIT( powner->ai.alert, ALERTIF_TOOMUCHBAGGAGE ); // return false; //} // Take the item out of hand pitem->detatchFromHolder(true, false); // clear the dropped flag UNSET_BIT( pitem->ai.alert, ALERTIF_DROPPED ); //Do not trigger dismount logic on putting items into inventory pitem->dismount_object = ObjectRef::Invalid; pitem->dismount_timer = 0; //now put the item into the inventory pitem->attachedto = ObjectRef::Invalid; pitem->inwhich_inventory = iowner; powner->getInventory()._items[inventorySlot] = pitem; // fix the flags if (pitem->getProfile()->isEquipment()) { SET_BIT(pitem->ai.alert, ALERTIF_PUTAWAY); // same as ALERTIF_ATLASTWAYPOINT; } //@todo: add in the equipment code here } return true; }
bool Inventory::swap_item( ObjectRef iobj, uint8_t inventory_slot, const slot_t grip_off, const bool ignorekurse ) { //valid character? const std::shared_ptr<Object> &pobj = _currentModule->getObjectHandler()[iobj]; if(!pobj) { return false; } //Validate slot number if(inventory_slot >= pobj->getInventory().getMaxItems()) { return false; } // Make sure everything is hunkydori if (pobj->isItem() || pobj->isInsideInventory()) return false; const std::shared_ptr<Object> &inventory_item = pobj->getInventory().getItem(inventory_slot); const std::shared_ptr<Object> &item = _currentModule->getObjectHandler()[pobj->holdingwhich[grip_off]]; //Nothing to do? if(!item && !inventory_item) { return true; } //Check if either item is kursed first if(!ignorekurse) { if(item && item->iskursed) { // Flag the last found_item as not put away SET_BIT( item->ai.alert, ALERTIF_NOTPUTAWAY ); // Same as ALERTIF_NOTTAKENOUT if ( pobj->isPlayer() ) DisplayMsg_printf("%s is sticky...", item->getName().c_str()); return false; } if(inventory_item && inventory_item->iskursed) { // Flag the last found_item as not removed SET_BIT( inventory_item->ai.alert, ALERTIF_NOTTAKENOUT ); // Same as ALERTIF_NOTPUTAWAY if ( pobj->isPlayer() ) DisplayMsg_printf( "%s won't go out!", inventory_item->getName().c_str() ); return false; } } //remove existing item from inventory and into the character's hand if (inventory_item) { pobj->getInventory().removeItem(inventory_item, ignorekurse); inventory_item->getObjectPhysics().attachToObject(pobj, grip_off == SLOT_RIGHT ? GRIP_RIGHT : GRIP_LEFT); //fix flags UNSET_BIT(inventory_item->ai.alert, ALERTIF_GRABBED); SET_BIT(inventory_item->ai.alert, ALERTIF_TAKENOUT); } //put the new item in the inventory if (item) { add_item(pobj->getObjRef(), item->getObjRef(), inventory_slot, ignorekurse); } return true; }
/*===========================================================================* * lin_lin_copy * *===========================================================================*/ static int lin_lin_copy(struct proc *srcproc, vir_bytes srclinaddr, struct proc *dstproc, vir_bytes dstlinaddr, vir_bytes bytes) { u32_t addr; proc_nr_t procslot; assert(get_cpulocal_var(ptproc)); assert(get_cpulocal_var(proc_ptr)); assert(read_ttbr0() == get_cpulocal_var(ptproc)->p_seg.p_ttbr); procslot = get_cpulocal_var(ptproc)->p_nr; assert(procslot >= 0 && procslot < ARM_VM_DIR_ENTRIES); if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE)); if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE)); assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE)); assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v); if(srcproc) assert(!RTS_ISSET(srcproc, RTS_VMINHIBIT)); if(dstproc) assert(!RTS_ISSET(dstproc, RTS_VMINHIBIT)); while(bytes > 0) { phys_bytes srcptr, dstptr; vir_bytes chunk = bytes; int changed = 0; #ifdef CONFIG_SMP unsigned cpu = cpuid; if (srcproc && GET_BIT(srcproc->p_stale_tlb, cpu)) { changed = 1; UNSET_BIT(srcproc->p_stale_tlb, cpu); } if (dstproc && GET_BIT(dstproc->p_stale_tlb, cpu)) { changed = 1; UNSET_BIT(dstproc->p_stale_tlb, cpu); } #endif /* Set up 1MB ranges. */ srcptr = createpde(srcproc, srclinaddr, &chunk, 0, &changed); dstptr = createpde(dstproc, dstlinaddr, &chunk, 1, &changed); if(changed) { reload_ttbr0(); } /* Copy pages. */ PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr); if(addr) { /* If addr is nonzero, a page fault was caught. * * phys_copy does all memory accesses word-aligned (rounded * down), so pagefaults can occur at a lower address than * the specified offsets. compute the lower bounds for sanity * check use. */ vir_bytes src_aligned = srcptr & ~0x3, dst_aligned = dstptr & ~0x3; if(addr >= src_aligned && addr < (srcptr + chunk)) { return EFAULT_SRC; } if(addr >= dst_aligned && addr < (dstptr + chunk)) { return EFAULT_DST; } panic("lin_lin_copy fault out of range"); /* Not reached. */ return EFAULT; } /* Update counter and addresses for next iteration, if any. */ bytes -= chunk; srclinaddr += chunk; dstlinaddr += chunk; } if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE)); if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE)); assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE)); assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v); return OK; }
void CIVVehicle::SetHeadlights(bool bSwitch) { IVVehicle * pVehicle = GetVehicle(); if(pVehicle) bSwitch ? SET_BIT(pVehicle->m_byteFlags6, 2) : UNSET_BIT(pVehicle->m_byteFlags6, 2); }
void runIns(struct Instruction i) { switch (i.op) { case NOP: /* * no realiza ninguna operacion. */ { UNSET_BIT(ZERO_BIT_FLAGS); break; } case MOV: /* * mueve un valor de/hacia registro/memoria/constante. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } case MEM: { if(i.src.val < 0 || i.src.val > (MEM_SIZE - 1) / 4) { printf("%d: Not valid memory index for source.\n", machine.reg[PC]); abort(); } else src_value = machine.memory[i.src.val * 4]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else machine.reg[i.dst.val] = src_value; break; } case MEM: { if(i.dst.val < 0 || i.dst.val > (MEM_SIZE - 1) / 4) { printf("%d: Not valid memory index for destination.\n", machine.reg[PC]); abort(); } else machine.memory[i.dst.val * 4] = src_value; break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } UNSET_BIT(ZERO_BIT_FLAGS); break; } case LW: /* * carga en dst el valor de la memoria apuntada por src. */ { int src_value; switch(i.src.type){ case MEM: { if(i.src.val < 0 || i.src.val > (MEM_SIZE - 1) / 4) { printf("%d: Not valid memory index for source.\n", machine.reg[PC]); abort(); } else src_value = machine.memory[i.src.val * 4]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] = src_value; break; } case MEM: { if(i.dst.val < 0 || i.dst.val > (MEM_SIZE - 1) / 4) { printf("%d: Not valid memory index for destination.\n", machine.reg[PC]); abort(); } else machine.memory[i.dst.val * 4] = src_value; break; } default: { printf("%d Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } UNSET_BIT(ZERO_BIT_FLAGS); break; } case SW: /* * guarda el valor de src en la direccion de memoria dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } case MEM: { if(i.src.val < 0 || i.src.val > (MEM_SIZE - 1) / 4) { printf("%d: Not valid memory index for source.\n", machine.reg[PC]); abort(); } else src_value = machine.memory[i.src.val * 4]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case MEM: { if(i.dst.val < 0 || i.dst.val > (MEM_SIZE - 1) / 4) { printf("%d: Not valid memory index for destination.\n", machine.reg[PC]); abort(); } else machine.memory[i.dst.val * 4] = src_value; break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } UNSET_BIT(ZERO_BIT_FLAGS); break; } case PUSH: /* * escribe el argumento src en el tope de la pila, * decrementando el SP por 4. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.dst.val == PC) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } if(machine.reg[SP] <= 0) { printf("%d: No free space in the stack.\n", machine.reg[PC]); abort(); } else { machine.reg[SP] -= 4; machine.memory[machine.reg[SP]] = src_value; } UNSET_BIT(ZERO_BIT_FLAGS); break; } case POP: /* * escribe en el argumento dst el tope de la pila, * incrementando el SP por 4. */ { int stack_value; if(machine.reg[SP] >= MEM_SIZE) { printf("%d: The stack is empty.\n", machine.reg[PC]); abort(); } else { stack_value = machine.memory[machine.reg[SP]]; machine.reg[SP] += 4; } switch(i.src.type) { case REG: { if(i.src.val == ZERO || i.src.val == FLAGS || i.src.val == SP || i.src.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else machine.reg[i.src.val] = stack_value; break; } case MEM: { if(i.src.val < 0 || i.src.val > (MEM_SIZE - 1) / 4) { printf("%d: Not valid memory index for destination.\n", machine.reg[PC]); abort(); } else machine.memory[i.src.val * 4] = stack_value; break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } UNSET_BIT(ZERO_BIT_FLAGS); break; } case PRINT: /* * imprime por pantalla el entero descripto en el argumento src. */ { switch(i.src.type){ case IMM: printf("%d\n", i.src.val); break; case REG: printf("%d\n", machine.reg[i.src.val]); break; case MEM: printf("%d\n", machine.memory[i.src.val / 4]); break; default: printf("%d: Operand type not valid.\n", machine.reg[PC]); abort(); } UNSET_BIT(ZERO_BIT_FLAGS); break; } case READ: /* * lee un entero y lo guarda en el argumento destino. */ { switch(i.src.type){ case REG: { if(i.src.val == ZERO || i.src.val == FLAGS || i.src.val == SP || i.src.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else{ int value; scanf("%d", &value); machine.reg[i.src.val] = value; } break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } UNSET_BIT(ZERO_BIT_FLAGS); break; } case ADD: /* * suma los argumentos src y dst y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] += src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case SUB: /* * resta los argumentos src y dst y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] -= src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case MUL: /* * multiplica los argumentos src y dst y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] *= src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case DIV: /* * divide los argumentos src y dst y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] /= src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case CMP: /* * compara src y dst y setea los bits correspondiente en el registro FLAGS. */ { int src_value, dst_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.dst.val == SP || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case IMM: { dst_value = i.dst.val; } case REG: { if(i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else dst_value = machine.reg[i.dst.val]; break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } if(dst_value == src_value){ SET_BIT(EQUAL_BIT_FLAGS); UNSET_BIT(LOWER_BIT_FLAGS); } else if(dst_value > src_value) { SET_BIT(LOWER_BIT_FLAGS); UNSET_BIT(EQUAL_BIT_FLAGS); } else { UNSET_BIT(EQUAL_BIT_FLAGS); UNSET_BIT(LOWER_BIT_FLAGS); } UNSET_BIT(ZERO_BIT_FLAGS); break; } case JMP: /* * saltan a la instruccion numero dst de la lista de instrucciones. */ { machine.reg[PC] = i.src.val-1; UNSET_BIT(ZERO_BIT_FLAGS); break; } case JMPE: /* * saltan a la instruccion numero dst si la bandera de igualdad esta seteada. */ { if(ISSET_EQUAL) machine.reg[PC] = i.src.val-1; UNSET_BIT(ZERO_BIT_FLAGS); break; } case JMPL: /* * saltan a la instruccion numero dst si la bandera de menor esta seteada. */ { if(ISSET_LOWER) machine.reg[PC] = i.src.val-1; UNSET_BIT(ZERO_BIT_FLAGS); break; } case AND: /* * realiza un bitwise and entre los dos operandos y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] &= src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case OR: /* * realiza un bitwise or entre los dos operandos y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] |= src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case XOR: /* * realiza un bitwise xor entre los dos operandos y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] ^= src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case LSH: /* * realiza un corrimiento de src bits a izquierda de dst y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] <<= src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case RSH: /* * realiza un corrimiento de src bits a derecha de dst y lo guarda en dst. */ { int src_value; switch(i.src.type){ case IMM: { src_value = i.src.val; break; } case REG: { if(i.src.val == PC || i.src.val == FLAGS) { printf("%d: Not valid register for source: %s\n", machine.reg[PC], regname[i.src.val]); abort(); } else src_value = machine.reg[i.src.val]; break; } default: { printf("%d: Not valid operand type for source.\n", machine.reg[PC]); abort(); } } switch(i.dst.type){ case REG: { if(i.dst.val == ZERO || i.dst.val == FLAGS || i.dst.val == SP || i.dst.val == PC) { printf("%d: Not valid register for destination: %s\n", machine.reg[PC], regname[i.dst.val]); abort(); } else machine.reg[i.dst.val] >>= src_value; if (!machine.reg[i.dst.val]) SET_BIT(ZERO_BIT_FLAGS); else UNSET_BIT(ZERO_BIT_FLAGS); break; } default: { printf("%d: Not valid operand type for destination.\n", machine.reg[PC]); abort(); } } break; } case CALL: /* * guarda el registro contador de programa en el stack y salta al label apuntado por src */ { int src_value = machine.reg[PC]; if(machine.reg[SP] <= 0) { printf("%d: No free space in the stack.\n", machine.reg[PC]); abort(); } else { machine.reg[SP] -= 4; machine.memory[machine.reg[SP]] = src_value; } machine.reg[PC] = i.src.val - 1; UNSET_BIT(ZERO_BIT_FLAGS); break; } case RET: /* * borra el registro contador de programa del stack y salta a la dirección apuntada por él. */ { int stack_value; if(machine.reg[SP] >= MEM_SIZE) { printf("%d: The stack is empty.\n", machine.reg[PC]); abort(); } else { stack_value = machine.memory[machine.reg[SP]]; machine.reg[SP] += 4; } machine.reg[PC] = stack_value; UNSET_BIT(ZERO_BIT_FLAGS); break; } case HLT: /* * termina el programa. */ { exit(1); break; } default: printf("Instruction not implemented\n"); abort(); } }