BigNum BigNum::operator *(const BigNum& operand) const { std::vector<uint32_t> ret(data.size() + operand.data.size() - 1, 0); std::vector<uint8_t> u8ret; for (int i = 0; i < data.size(); i++) for (int j = 0; j < operand.data.size(); j++) { //std::cout << "j: " << j << ". operand.data.size(): " << operand.data.size() << std::endl; ret[i + j] += (uint16_t) data[i] * operand.data[j]; } uint32_t carry(0); for (std::vector<uint32_t>::iterator it = ret.begin(); it != ret.end(); it++) { *it += carry; carry = *it >> 8; u8ret.push_back((*it) bitand ((1 << 8) - 1)); } while (carry > 0) { u8ret.push_back(carry bitand ((1 << 8) - 1)); carry >>= 8; } //redudant because u8ret will be refined in BigNum construction but for sake of logical purpose // while(not u8ret.empty() and *u8ret.rbegin() == 0) // u8ret.pop_back(); BigNum bb(0); bb.data = u8ret; bb.negative = negative xor operand.negative; bb.refine(); return bb; }
Bignum::Bignum(const char * number) { init(); bool symbol = false; if (number[0] == '-') { number += 1; symbol = true; } int len = strlen(number), tmp_i = 0; for (int i = bignum_len - 1; len > 0 && i >= 0; i--) { tmp_i++; if (len >= LONG_LEN) { bignum[i] += str_to_int64(number + (len - LONG_LEN), LONG_LEN); carry(i); } else { bignum[i] += str_to_int64(number, len); break; } len -= LONG_LEN; } if (symbol) { bignum[bignum_len - tmp_i] *= -1; } }
/* * FC 0x01 Read Coil Status respond / FC 0x02 Read Input Status */ int tcp_build_resp_read_status(struct tcp_frm_rsp *tx_buf, struct thread_pack *tpack, unsigned char fc) { int byte; int txlen; unsigned short msglen; unsigned short len; len = tpack->tmpara->len; byte = carry((int)len, 8); txlen = byte + 9; msglen = byte + 3; tpack->tsfpara->msglen = msglen; tx_buf->transID = htons(tpack->tsfpara->transID); tx_buf->potoID = htons(tpack->tsfpara->potoID); tx_buf->msglen = htons(tpack->tsfpara->msglen); tx_buf->unitID = tpack->tsfpara->unitID; tx_buf->fc = fc; tx_buf->byte = (unsigned char)byte; memset(tx_buf+1, 0, byte); // tx_buf+1 shift a size of sturct "tcp_frm_rsp" (9 byte) // printf("<Modbus TCP Slave> respond Read %s Status\n", fc==READCOILSTATUS?"Coil":"Input"); return txlen; }
void check_carry(void) { /* Check cost of carry for a put */ double S = 500.0, X = 490.0, r = 0.08, b = 0.03, v = 0.15, T = 3.0/12; assert_equal(carry(0, S, X, T, r, b, v), -42.2254); assert_equal(carry_put(S, X, T, r, b, v), -42.2254); }
ListNode* sumList(ListNode* A, ListNode* B, int c) { ListNode* m = nullptr; ListNode* t = nullptr; ListNode* e = nullptr; while( (A!=nullptr || B!=nullptr || c!=0) ) { if(A==nullptr && B==nullptr) { t = new ListNode(0 + 0); carry(c, t->val); } else if(A==nullptr) { t = new ListNode(0 + B->val); carry(c,t->val); B = B->next; } else if(B==nullptr) { t = new ListNode(A->val + 0); carry(c,t->val); A = A->next; } else { t = new ListNode( A->val + B->val) ; carry(c,t->val); A = A->next; B = B->next; } if(m==nullptr) { m = t; e = t; } else { e->next = t; e = e->next; } } return m; }
/*===========================================================================* * do_rdwt * *===========================================================================*/ static int do_rdwt(int flag_rw) { size_t size, size_ret; u64_t pos; int r; pos = make64(m_in.BDEV_POS_LO, m_in.BDEV_POS_HI); size = m_in.BDEV_COUNT; if (rem64u(pos, SECTOR_SIZE) != 0 || size % SECTOR_SIZE != 0) { printf("Filter: unaligned request from caller!\n"); return EINVAL; } buffer = flt_malloc(size, buf_array, BUF_SIZE); if(flag_rw == FLT_WRITE) carry(size, flag_rw); reset_kills(); for (;;) { size_ret = size; r = transfer(pos, buffer, &size_ret, flag_rw); if(r != RET_REDO) break; #if DEBUG printf("Filter: transfer yielded RET_REDO, checking drivers\n"); #endif if((r = check_driver(DRIVER_MAIN)) != OK) break; if((r = check_driver(DRIVER_BACKUP)) != OK) break; } if(r == OK && flag_rw == FLT_READ) carry(size_ret, flag_rw); flt_free(buffer, size, buf_array); if (r != OK) return r; return size_ret; }
/* Routine to move an object */ void move(int obj, int where) { int from; from = (obj<MAXOBJ) ? place[obj] : fixed[obj]; if (from>0 && from<=300) carry(obj, from); drop(obj, where); }
Bignum::Bignum(const int64 number) { init(); bignum[bignum_len - 1] = number; for (int i = bignum_len - 1; i >= 0; i--) { if (bignum[i] == 0) { break; } carry(i); } }
RCP<const UnivariateIntPolynomial> mul_poly(const UnivariateIntPolynomial &a, const UnivariateIntPolynomial &b) { RCP<const Symbol> var = symbol(""); if (a.get_var()->get_name() == "") { var = b.get_var(); } else if (b.get_var()->get_name() == "") { var = a.get_var(); } else if (!(a.get_var()->__eq__(*b.get_var()))) { throw std::runtime_error("Error: variables must agree."); } else { var = a.get_var(); } bool neg = false; if ((--(a.get_dict().end()))->second < 0) neg = not neg; if ((--(b.get_dict().end()))->second < 0) neg = not neg; unsigned int N = bit_length(std::min(a.get_degree() + 1, b.get_degree() + 1)) + bit_length(a.max_abs_coef()) + bit_length(b.max_abs_coef()); integer_class a1(1), b1; a1 <<= N; integer_class a2 = a1 / 2; integer_class mask = a1 - 1; integer_class a_val(a.eval_bit(N)), b_val(b.eval_bit(N)); integer_class s_val(a_val * b_val); integer_class r = mp_abs(s_val); std::vector<integer_class> v; integer_class carry(0); while (r != 0 or carry != 0) { mp_and(b1, r, mask); if (b1 < a2) { v.push_back(b1 + carry); carry = 0; } else { v.push_back(b1 - a1 + carry); carry = 1; } r >>= N; } if (neg) return neg_poly(*UnivariateIntPolynomial::from_vec(var, v)); else return UnivariateIntPolynomial::from_vec(var, v); }
void move(int object, int where) { int from; if (object <= 100) from = place[object]; else from = fixed[object - 100]; if (from > 0 && from <= 300) carry(object, from); drop(object, where); }
literalt bv_utilst::carry_out( const bvt &op0, const bvt &op1, literalt carry_in) { assert(op0.size()==op1.size()); literalt carry_out=carry_in; for(std::size_t i=0; i<op0.size(); i++) carry_out=carry(op0[i], op1[i], carry_out); return carry_out; }
/* * FC 0x01 Read Coil Status respond / FC 0x02 Read Input Status */ int ser_build_resp_read_status(unsigned char *tx_buf, struct frm_para *sfpara, unsigned char fc) { int byte; int src_len; unsigned char slvID; unsigned char src[FRMLEN]; slvID = (unsigned char)sfpara->slvID; byte = (int)carry(sfpara->len, 8); // printf("<modbus serial slave> respond read %s status\n", fc==READCOILSTATUS?"coil":"input"); src_len = _build_frame(src, "cccD", slvID, fc, (unsigned char)byte, byte); build_rtu_frm(tx_buf, src, src_len); src_len += 2; // CRC 2 byte return src_len; }
vector<int> plusOne(vector<int>& digits) { if(allNines(digits)) { digits.push_back(0); digits[0] = 1; for(int i=1;i<digits.size()-1;i++) { digits[i] = 0; } } else { digits[digits.size()-1]++; } carry(digits); return digits; }
void Agent::addCarried(AgentRef a) { assert(a); // TODO: muh, vehicle drop needs more thought if (a->invehicle) { Vehicle *v = dynamic_cast<Vehicle *>(a->invehicle.get()); assert(v); v->dropCarried(a); } carry(a); // TODO: this doesn't reorder children or anything.. a->setZOrder(a->zorder); // fire 'Got Carried Agent' if (engine.version >= 3) queueScript(124, a); // TODO: is this the correct param? }
Integer& Integer::operator*= (Integer b) { Integer result; std::vector<bool> vec_a = bits_; std::vector<bool> vec_b = b.bits_; std::vector<Integer> carry(vec_a.size()); //Using binary method of addition, multiply individually by each digit in multiplier for (int i = static_cast<int>(vec_a.size()) - 1; i >= 0; --i) { //Iterate through multiplicand vector and multiply every digit by current working digit for (int m = static_cast<int>(vec_b.size()) - 1; m >= 0; --m) { //1 and 1 = 1 //0 and 1 = 0 //1 and 0 = 0 carry[i].bits_.insert(carry[i].bits_.begin(), (vec_b[m] && vec_a[i])); } //Add a zero for each 2^k for (int l = static_cast<int>(vec_a.size()) - 1; l > i; --l) { carry[i].bits_.push_back(false); } } //Add each individual multiplication operation together into final vector for (int i = static_cast<int>(carry.size() - 1); i >= 0; --i) { result += carry[i]; } //Set current vector equal to result this->bits_ = result.bits_; //std::cout << this->bits_[0]; return *this; }
void search() { VariableValue left, right; std::vector<VariableValue> currentPoint(_meshCentre.size()); std::vector<double> carry(_variables.size()); std::vector<window> windows(_variables.size()); long long nodesInCurrentRow; long long nodesPerRow; long long numNodes; /* set up some "helper" functions. They're not on the class because * wtf why would they be there? They're not at all useful to * anything else */ auto nodesInRow = [&] (int i) { return (_variables[i].getType() == VariableType::Discrete ? std::min(nodesPerRow, _variables[i].rangeEnd.ll - _variables[i].rangeStart.ll + 1ll) : nodesPerRow); }; auto getWindowForVariable = [&] (int idx) { nodesInCurrentRow = nodesInRow(idx); window w; if (_variables[idx].getType() == VariableType::Continuous) { w.left.d = std::max(_variables[idx].rangeStart.d, (double)_meshCentre[idx] - _sideWidth/2.0); w.right.d = std::min(_variables[idx].rangeEnd.d, (double)_meshCentre[idx] + _sideWidth/2.0); } else if (_variables[idx].getType() == VariableType::Discrete) { w.left.ll = std::max(_variables[idx].rangeStart.ll, (long long)_meshCentre[idx] - nodesInCurrentRow / 2); w.right.ll = std::min(_variables[idx].rangeEnd.ll, (long long)_meshCentre[idx] + nodesInCurrentRow / 2); } return w; }; auto incrementVariable = [&](int i) { if (_variables[i].getType() == VariableType::Discrete) { currentPoint[i].ll += (long long)((windows[i].right.ll - windows[i].left.ll) / (double) (nodesInCurrentRow - 1)); // For discrete vars, add with carry to deal with possible // windows-size problems (right - left > numNodes). // This logic could be horribly horribly f****d. double _ = (double)currentPoint[i].ll; _ += carry[i]; // I don't want to round() this, I want to floor + deal with // potential double-precision problems if (floor(_ + EPS) != floor(_)) { carry[i] = 0.0; currentPoint[i].ll = (long long)ceil(_); } else { carry[i] = _ - floor(_); currentPoint[i].ll = (long long)floor(_); } } else { currentPoint[i].d += ((windows[i].right.d - windows[i].left.d) / (double) (nodesInCurrentRow - 1)); } }; for (; ; _constrictions++) { std::fill(carry.begin(), carry.end(), 0.0); nodesPerRow = pow((1/_constrictFactor), _constrictions) + 1; numNodes = 1; for (int i = 0; i < _variables.size(); i++) { windows[i] = getWindowForVariable(i); numNodes *= nodesInCurrentRow; currentPoint[i] = windows[i].left; } std::cout << numNodes << std::endl; // This is stupid, but I'm doing it anyway to fix some // stupidity: VariableValue _ = currentPoint[0]; nodesInCurrentRow = nodesInRow(0); incrementVariable(0); // TODO Fix this later //if (currentPoint[0] - _ <= _lengthTolerance) return; if (_variables[0].getType() == VariableType::Continuous) currentPoint[0].d = _.d - (currentPoint[0].d - _.d); else currentPoint[0].ll = _.ll - (currentPoint[0].ll - _.ll); for (long long i = 0; i < numNodes; i++) { nodesInCurrentRow = nodesInRow(0); incrementVariable(0); for (int j = 0; j < _variables.size(); j++) { //nodesInCurrentRow = nodesInRow(j); // TODO hack if ((_variables[j].getType() == VariableType::Continuous && currentPoint[j].d > windows[j].right.d) || (_variables[j].getType() == VariableType::Discrete && currentPoint[j].ll > windows[j].right.ll)) { currentPoint[j] = windows[j].left; if (j < _variables.size() - 1) { nodesInCurrentRow = nodesInRow(j+1); incrementVariable(j+1); } } else break; } std::cout << "("; for (int j = 0; j < _variables.size(); j++) std::cout << (_variables[j].getType() == VariableType::Discrete ? currentPoint[j].ll : currentPoint[j].d) << (j == _variables.size() - 1 ? "" : ",\t\t"); std::cout << ")\n"; } } }
/* CARRY TAKE etc. */ vtake() { int msg; int i; if (toting(object)) { actspk(verb); return; } /* special case objects and fixed objects */ msg = 25; if (object == PLANT && prop[PLANT] <= 0) msg = 115; if (object == BEAR && prop[BEAR] == 1) msg = 169; if (object == CHAIN && prop[BEAR] != 0) msg = 170; if (fixed[object]) { rspeak(msg); return; } /* special case for liquids */ if (object == WATER || object == OIL) { if (!here(BOTTLE) || liq() != object) { object = BOTTLE; if (toting(BOTTLE)&&prop[BOTTLE] == 1){ vfill(); return; } if (prop[BOTTLE] != 1) msg = 105; if (!toting(BOTTLE)) msg = 104; rspeak(msg); return; } object = BOTTLE; } if (holding >= 7) { rspeak(92); return; } /* special case for bird. */ if (object == BIRD && prop[BIRD] == 0) { if (toting(ROD)) { rspeak(26); return; } if (!toting(CAGE)) { rspeak(27); return; } prop[BIRD] = 1; } if ( (object == BIRD || object == CAGE) && prop[BIRD] != 0) carry((BIRD+CAGE)-object,loc); carry(object,loc); /* handle liquid in bottle */ i = liq(); if (object == BOTTLE && i != 0) place[i] = -1; rspeak(54); }
// Specialisation for two operands. Result bvAddBothWays(FixedBits& x, FixedBits& y, FixedBits& output) { const int bitWidth = output.getWidth(); FixedBits carry(bitWidth + 1, false); carry.setFixed(0, true); carry.setValue(0, false); //cerr << "input" << x << y << output << endl; for (int i = 0; i < bitWidth; i++) { //cerr << i << ":"<< x[i] << y[i] << carry[i] << "=" << output[i]<< endl; int lb = (x[i] == '1' ? 1 : 0) + (y[i] == '1' ? 1 : 0) + (carry[i] == '1' ? 1 : 0); int ub = (x[i] == '0' ? 0 : 1) + (y[i] == '0' ? 0 : 1) + (carry[i] == '0' ? 0 : 1); const int lb_initial = lb; const int ub_initial = ub; if (carry[i+1] == '1') // carry out is true. lb = std::max(2, lb); if (carry[i+1] == '0') // carry out is false. ub = std::min(1, ub); const char output_i = output[i]; if (output_i == '1' && (lb % 2 == 0)) lb++; if (output_i == '0' && (lb % 2 == 1)) lb++; if (output_i == '1' && (ub % 2 == 0)) ub--; if (output_i == '0' && (ub % 2 == 1)) ub--; if (lb >= 2) setValue(carry, i+1, true); if (ub <= 1) setValue(carry, i+1, false); if (ub < lb) return CONFLICT; if (lb == ub) { setValue(output, i, ((lb % 2) == 1)); if (lb_initial ==lb) { if (!x.isFixed(i)) setValue(x, i, false); if (!y.isFixed(i)) setValue(y, i, false); if (!carry.isFixed(i)) { setValue(carry, i, false); i = std::max(i-2,-1); // go back to the prior column. continue; } } if (ub_initial ==lb) { if (!x.isFixed(i)) setValue(x, i, true); if (!y.isFixed(i)) setValue(y, i, true); if (!carry.isFixed(i)) { setValue(carry, i, true); i = std::max(i-2,-1); // go back to the prior column. continue; } } } //cerr << i << "[" << ub << ":" << lb << "]" << endl; } return NOT_IMPLEMENTED; }
/* called by GUI */ Icon::Icon(User *user, void *d) { char *infos = (char *) d; ifile = NULL; ofile = NULL; char *action = NULL; vref = NULL; char icon[URL_LEN]; *icon = 0; /* parameters transmission */ for (char *pt = strtok(infos, "&"); pt ; pt = strtok(NULL, "&")) { if (! stringcmp(pt, "<url=")) { pt = getParam(pt); strcpy(names.url, pt); taken = true; } else if (! stringcmp(pt, "<file=")) { pt = getParam(pt); ifile = strdup(pt); } else if (! stringcmp(pt, "<ofile=")) { pt = getParam(pt); strcpy(names.url, pt); } else if (! stringcmp(pt, "<name=")) { pt = getParam(pt); strcpy(names.named, pt); } else if (! stringcmp(pt, "<icon=")) { pt = getParam(pt); strcpy(icon, pt); } else if (! stringcmp(pt, "<action=")) { pt = getParam(pt); action = strdup(pt); } else if (! stringcmp(pt, "<vref=")) { pt = strchr(pt, '='); pt++; char *p2; if ((p2 = strchr(pt, '>'))) *p2 = 0; vref = strdup(pt); } } if (vref) { // local saved document exists parser(vref); // get the last loaded texture int texid = Texture::getIdByUrl(names.url); Texture *tclast = Texture::getEntryById(texid); if (! tclast) { tex = new char[sizeof(ICO_DEF) + 1]; strcpy(tex, ICO_DEF); // default texture } else { tex = new char[strlen(tclast->url) + 1]; strcpy(tex, tclast->url); } taken = false; } else { // new document named interactively by hand /* position */ float off = 0.4; pos.x = user->pos.x + off * Cos(user->pos.az); pos.y = user->pos.y + off * Sin(user->pos.az); pos.z = user->pos.z + 0.6; // visible by eyes pos.az = user->pos.az + M_PI_2; /* texture */ if (*icon) { tex = new char[strlen(icon) + 1]; strcpy(tex, icon); } else { // default binding icon to document char ext[8] = ""; memset(ext, 0, sizeof(ext)); if (Format::getExt(names.url, ext)) { tex = new char[URL_LEN]; Format::getImgByExt(ext, tex); } else { tex = new char[sizeof(ICO_DEF) + 1]; strcpy(tex, ICO_DEF); } } if (ifile) { // private local document if (*names.url) { // public url given by user Cache::download(ifile, ofile, "inout"); } else { // build local ofile in ~/public_html/vreng/ ofile = new char[URL_LEN]; sprintf(ofile, "%s/public_html", getenv("HOME")); if (access(ofile, R_OK|W_OK|X_OK) == 0) { strcat(ofile, "/vreng/"); if (access(ofile, R_OK|W_OK|X_OK) == -1) mkdir(ofile, 0755); strcat(ofile, ifile); FILE *fin, *fout; if ((fin = File::openFile(ifile, "r")) && (fout = File::openFile(ofile, "w"))) { char buf[2]; while (fread(buf, 1, 1, fin)) fwrite(buf, 1, 1, fout); File::closeFile(fin); File::closeFile(fout); chmod(ofile, 0644); //FIXME: define local http_server sprintf(names.url, "http://%s/~%s/vreng/%s", DEF_HTTP_SERVER, getenv("USER"), ifile); } else { error("can't open %s or %s: %s (%d)", ifile, ofile, strerror(errno), errno); free(ifile); ifile = NULL; delete[] ofile; } } else { error("can't access %s", ofile); free(ifile); ifile = NULL; delete[] ofile; } } } makeSolid(); } // local creation defaults(); enableBehavior(REMOVABLE); enableBehavior(NO_ELEMENTARY_MOVE); setRenderPrior(RENDER_HIGH); initializeMobileObject(1); ttl = (taken) ? MAXFLOAT : 0; initImposedMovement(ttl); disablePermanentMovement(); // network creation createVolatileNetObject(PROPS); // document's owner setOwner(); trace(DBG_WO, "Icon: url=%s icon=%s name=%s owner=%s", urlName(), tex, getInstance(), ownerName()); if (action) { if (! stringcmp(action, "pin")) pin(this, NULL, 0L, 0L); else if (! stringcmp(action, "push")) push(this, NULL, 0L, 0L); else if (! stringcmp(action, "carry")) carry(this, NULL, 0L, 0L); } }
static void set_para(mbus_cmd_info *cmd) { mb_printf("Enter Slave ID : "); scanf("%hhu", &cmd->info.unitID); printf("Function code :\n"); printf("%hhu\t\tRead Coil Status\n", READCOILSTATUS); printf("%hhu\t\tRead Input Status\n", READINPUTSTATUS); printf("%hhu\t\tRead Holding Registers\n", READHOLDINGREGS); printf("%hhu\t\tRead Input Registers\n", READINPUTREGS); printf("%hhu\t\tForce Single Coil\n", FORCESINGLECOIL); printf("%hhu\t\tPreset Single Register\n", PRESETSINGLEREG); printf("%hhu\t\tForce multi Coil\n", FORCEMULTICOILS); printf("%hhu\t\tPreset multi Register\n", PRESETMULTIREGS); printf("Enter Function code : "); scanf("%hhu", &cmd->info.fc); switch(cmd->info.fc){ case READCOILSTATUS: case READINPUTSTATUS: case READHOLDINGREGS: case READINPUTREGS: printf("Setting Start addr (start from 0): "); scanf("%hu", &cmd->query.addr); printf("Setting Data Length: "); scanf("%hu", &cmd->query.len); break; case FORCESINGLECOIL: printf("Setting Start addr (start from 0): "); scanf("%hu", &cmd->swrite.addr); cmd->swrite.data = htobe16(0); break; case PRESETSINGLEREG: printf("Setting Start addr (start from 0): "); scanf("%hu", &cmd->swrite.addr); cmd->swrite.data = htobe16(0); break; case FORCEMULTICOILS: printf("Setting Start addr (start from 0): "); scanf("%hu", &cmd->mwrite.addr); printf("Setting Data Length: "); scanf("%hu", &cmd->mwrite.len); bytecnt = carry(cmd->mwrite.len, 8); cmd->mwrite.bcnt = bytecnt; wdata = (uint8_t *)malloc(bytecnt); assert(wdata); memset(wdata, 0, bytecnt); cmd->mwrite.data = wdata; break; case PRESETMULTIREGS: printf("Setting Start addr (start from 0): "); scanf("%hu", &cmd->mwrite.addr); printf("Setting Data Length: "); scanf("%hu", &cmd->mwrite.len); bytecnt = cmd->mwrite.len * sizeof(short); cmd->mwrite.bcnt = bytecnt; wdata = (uint8_t *)malloc(bytecnt); assert(wdata); memset(wdata, 0, bytecnt); cmd->mwrite.data = wdata; break; default: printf("unknown function code"); exit(0); } printf("Setup delay between each command (seconds): "); scanf("%u", &delay); }
void InvocationCounter::print() { tty->print_cr("invocation count: up = %d, limit = %d, carry = %s, state = %s", count(), limit(), carry() ? "true" : "false", state_as_string(state())); }
void InvocationCounter::print_short() { tty->print(" [%d%s;%s]", count(), carry()?"+carry":"", state_as_short_string(state())); }
/* * Analyze modbus TCP respond */ int tcp_resp_parser(unsigned char *rx_buf, struct tcp_frm_para *tmfpara, int rlen) { // int i; int act_byte; unsigned short tmp16; unsigned char qfc, rfc; unsigned short qact, ract; unsigned short qlen; // unsigned short raddr; unsigned short rrlen; /* char *s[EXCPMSGTOTAL] = {"<Modbus TCP Master> Read Coil Status (FC=01) exception !!", "<Modbus TCP Master> Read Input Status (FC=02) exception !!", "<Modbus TCP Master> Read Holding Registers (FC=03) exception !!", "<Modbus TCP Master> Read Input Registers (FC=04) exception !!", "<Modbus TCP Master> Force Single Coil (FC=05) exception !!", "<Modbus TCP Master> Preset Single Register (FC=06) exception !!" }; */ qfc = tmfpara->fc; rfc = *(rx_buf+7); qlen = tmfpara->len; rrlen = *(rx_buf+8); if(qfc != rfc){ if(rfc > PRESETEXCPSTATUS_EXCP || rfc < READCOILSTATUS_EXCP){ printf("<Modbus TCP Master> unknown respond function code : %x !!\n", rfc); return -1; } // printf("%s\n", s[rfc - READCOILSTATUS_EXCP]); return -1; } if(!(rfc ^ READCOILSTATUS) || !(rfc ^ READINPUTSTATUS)){ // fc = 0x01/0x02, detect data len act_byte = carry((int)qlen, 8); if(rrlen != act_byte){ printf("<Modbus TCP Master> recv respond length wrong (rlen = %d | qlen = %d)\n", rrlen, act_byte); return -1; } /* printf("<Modbus TCP Master> Data : "); for(i = 9; i < rlen; i++){ printf(" %x |", *(rx_buf+i)); } printf("\n"); */ }else if(!(rfc ^ READHOLDINGREGS) || !(rfc ^ READINPUTREGS)){ // fc = 0x03/0x04, detect data byte if(rrlen != qlen << 1){ printf("<Modbus TCP Master> recv respond byte wrong !!\n"); return -1; } /* printf("<Modbus TCP Master> Data : "); for(i = 9; i < rlen; i+=2){ printf(" %x%x |", *(rx_buf+i), *(rx_buf+i+1)); } printf("\n"); */ }else if(!(rfc ^ FORCESIGLEREGS)){ // fc = 0x05, get write on/off status // memcpy(&tmp16, rx_buf+8, sizeof(tmp16)); // raddr = ntohs(tmp16); ract = *(rx_buf+10); if(ract == 255){ // printf("<Modbus TCP Master> addr : %x The status to wirte on (FC:0x05)\n", raddr); }else if(!ract){ // printf("<Modbus TCP Master> addr : %x The status to wirte off (FC:0x05)\n", raddr); }else{ printf("<Modbus TCP Master> Unknown status (FC:0x04)\n"); return -1; } }else if(!(rfc ^ PRESETEXCPSTATUS)){ // fc = 0x06, get status on register qact = tmfpara->act; memcpy(&tmp16, rx_buf+8, sizeof(tmp16)); // raddr = ntohs(tmp16); memcpy(&tmp16, rx_buf+10, sizeof(tmp16)); ract = ntohs(tmp16); if(qact != ract){ printf("<Modbus TCP Master> Action fail (FC:0x06) "); printf("Query action : %x | Respond action : %x\n", qact, ract); return -1; } // printf("<Modbus TCP Master> addr : %x Action code : %x\n", raddr, ract); }else{ // fc = Unknown printf("<Modbus TCP Master> Unknown Function code %x !!\n", rfc); return -1; } return 0; }
literalt bv_utilst::full_adder( const literalt a, const literalt b, const literalt carry_in, literalt &carry_out) { #ifdef OPTIMAL_FULL_ADDER if(prop.has_set_to() && prop.cnf_handled_well()) { literalt x; literalt y; int constantProp = -1; if(a.is_constant()) { x = b; y = carry_in; constantProp = (a.is_true()) ? 1 : 0; } else if(b.is_constant()) { x = a; y = carry_in; constantProp = (b.is_true()) ? 1 : 0; } else if(carry_in.is_constant()) { x = a; y = b; constantProp = (carry_in.is_true()) ? 1 : 0; } literalt sum; // Rely on prop.l* to do further constant propagation if(constantProp == 1) { // At least one input bit is 1 carry_out = prop.lor(x, y); sum = prop.lequal(x, y); } else if(constantProp == 0) { // At least one input bit is 0 carry_out = prop.land(x, y); sum = prop.lxor(x, y); } else { carry_out = prop.new_variable(); sum = prop.new_variable(); // Any two inputs 1 will set the carry_out to 1 prop.lcnf(!a, !b, carry_out); prop.lcnf(!a, !carry_in, carry_out); prop.lcnf(!b, !carry_in, carry_out); // Any two inputs 0 will set the carry_out to 0 prop.lcnf(a, b, !carry_out); prop.lcnf(a, carry_in, !carry_out); prop.lcnf(b, carry_in, !carry_out); // If both carry out and sum are 1 then all inputs are 1 prop.lcnf(a, !sum, !carry_out); prop.lcnf(b, !sum, !carry_out); prop.lcnf(carry_in, !sum, !carry_out); // If both carry out and sum are 0 then all inputs are 0 prop.lcnf(!a, sum, carry_out); prop.lcnf(!b, sum, carry_out); prop.lcnf(!carry_in, sum, carry_out); // If all of the inputs are 1 or all are 0 it sets the sum prop.lcnf(!a, !b, !carry_in, sum); prop.lcnf(a, b, carry_in, !sum); } return sum; } else // NOLINT(readability/braces) #endif // OPTIMAL_FULL_ADDER { // trivial encoding carry_out=carry(a, b, carry_in); return prop.lxor(prop.lxor(a, b), carry_in); } }
/* pick the context menu options appropiate for the item */ int context_menu_object(const object_type *o_ptr) { menu_type *m; rect_region r; int selected; int location = 0; char *labels; char header[120]; s16b *list; m = menu_dynamic_new(); if (!m || !o_ptr) { return 0; } object_desc(header, o_ptr, TRUE, 2, sizeof(header)); list = look_up_list((object_type*)o_ptr); if (list) { if (list == &(p_ptr->inventory)) { location = USE_INVEN; } else if (list == &(area(p_ptr->px, p_ptr->py)->o_idx)) { location = USE_FLOOR; } else { /* check if in a container */ location = USE_INVEN; } } else if (GET_ARRAY_INDEX(p_ptr->equipment, o_ptr) >= EQUIP_WIELD) { location = USE_EQUIP; } labels = (char*)string_make(lower_case); m->selections = labels; menu_dynamic_add_label(m, "Inspect", 'I', 1, labels); if (item_tester_hook_is_book(o_ptr)) { if (player_can_cast_from(o_ptr)) { if (player_can_cast()) { menu_dynamic_add_label(m, "Cast", 'm', 8, labels); } else { menu_dynamic_add_label(m, "$Cast", 'm', 8, labels); } if (player_can_study()) { menu_dynamic_add_label(m, "Study", 'G', 10, labels); } else { menu_dynamic_add_label(m, "$Study", 'G', 10, labels); } } if (player_is_caster() && player_can_read()) { menu_dynamic_add_label(m, "Browse", 'b', 9, labels); } } else if (item_tester_hook_useable(o_ptr)) { if (obj_is_wand(o_ptr)) { if (obj_has_charges(o_ptr)) { menu_dynamic_add_label(m, "Aim", 'a', 8, labels); } else { menu_dynamic_add_label(m, "$Aim", 'a', 8, labels); } } else if (obj_is_rod(o_ptr)) { if (obj_can_zap(o_ptr)) { menu_dynamic_add_label(m, "Zap", 'z', 8, labels); } else { menu_dynamic_add_label(m, "$Zap", 'z', 8, labels); } } else if (obj_is_staff(o_ptr)) { if (obj_has_charges(o_ptr)) { menu_dynamic_add_label(m, "Use", 'u', 8, labels); } else { menu_dynamic_add_label(m, "$Use", 'u', 8, labels); } } else if (obj_is_scroll(o_ptr)) { if (player_can_read()) { menu_dynamic_add_label(m, "Read", 'r', 8, labels); } else { menu_dynamic_add_label(m, "$Read", 'r', 8, labels); } } else if (obj_is_potion(o_ptr)) { menu_dynamic_add_label(m, "Quaff", 'q', 8, labels); } else if (obj_is_food(o_ptr)) { menu_dynamic_add_label(m, "Eat", 'E', 8, labels); } else if (item_tester_hook_activate(o_ptr)) { if (obj_is_activatable(o_ptr)) { menu_dynamic_add_label(m, "Activate", 'A', 8, labels); } else { menu_dynamic_add_label(m, "$Activate", 'A', 8, labels); } } else { menu_dynamic_add_label(m, "Use", 'U', 8, labels); } } else if (item_tester_hook_ammo(o_ptr)) { if (obj_can_fire(o_ptr)) { menu_dynamic_add_label(m, "Fire", 'f', 15, labels); } else { menu_dynamic_add_label(m, "$Fire", 'f', 15, labels); } } if (obj_can_refill(o_ptr)) { menu_dynamic_add_label(m, "Refill", 'F', 11, labels); } if (item_tester_hook_wear(o_ptr)) { if (location == USE_EQUIP) { menu_dynamic_add_label(m, "Take off", 't', 3, labels); } else if (location == USE_INVEN) { if (item_tester_hook_armour(o_ptr)) { menu_dynamic_add_label(m, "Wear", 'w', 2, labels); } else { menu_dynamic_add_label(m, "Wield", 'w', 2, labels); } /*menu_dynamic_add_label(m, "Equip", 'w', 2, labels);*/ } } if ((location == USE_INVEN) || (location == USE_EQUIP)) { menu_dynamic_add_label(m, "Drop", 'd', 6, labels); if (o_ptr->number > 1) { menu_dynamic_add_label(m, "Drop All", 'd', 13, labels); } } else if (location == USE_FLOOR) { if (inven_carry_okay(o_ptr)) { menu_dynamic_add_label(m, "Pickup", 'g', 7, labels); } else { menu_dynamic_add_label(m, "$Pickup", 'g', 7, labels); } } menu_dynamic_add_label(m, "Throw", 'v', 12, labels); /*if (obj_has_inscrip(o_ptr)) {*/ if (o_ptr->inscription) { menu_dynamic_add_label(m, "Uninscribe", '}', 5, labels); } else { menu_dynamic_add_label(m, "Inscribe", '{', 4, labels); } menu_dynamic_add_label(m, "Destroy", 'k', 14, labels); #if 0 if (object_is_squelched(o_ptr)) { menu_dynamic_add_label(m, "Unignore", 'k', 14, labels); } else { menu_dynamic_add_label(m, "Ignore", 'k', 14, labels); } #endif /* work out display region */ r.width = menu_dynamic_longest_entry(m) + 3 + 2; /* +3 for tag, 2 for pad */ r.col = Term->wid - r.width - 1; r.row = 1; r.page_rows = m->count; /* Hack -- no flush needed */ msg_flag = FALSE; screen_save(); button_backup_all(TRUE); /* Recall object */ roff_set_width(r.col); roff_obj_aux(o_ptr); roff_set_width(0); menu_layout(m, &r); rect_region_erase_bordered(&r); prtf(0, 0, "($UEnter to select$Y\n$V, $UESC$Y%c$V) Command for %s:", ESCAPE, header); selected = menu_dynamic_select(m); menu_dynamic_free(m); string_free(labels); button_restore(); screen_load(); if (selected == 1) { /* inspect it */ identify_fully_aux(o_ptr); return 2; } else if (selected == 2) { /* wield the item */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = 'w'; do_cmd_wield(); /*cmd_insert(CMD_WIELD); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } else if (selected == 3) { /* take the item off */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = 't'; do_cmd_takeoff(); /*cmd_insert(CMD_TAKEOFF); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } else if (selected == 4) { /* inscribe the item */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = '{'; do_cmd_inscribe(); /*cmd_insert(CMD_INSCRIBE); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } else if (selected == 5) { /* uninscribe the item */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = '}'; do_cmd_uninscribe(); /*cmd_insert(CMD_UNINSCRIBE); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } else if (selected == 6) { /* drop the item */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = 'd'; do_cmd_drop(); /*cmd_insert(CMD_DROP); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } else if (selected == 7) { /* pick the item up */ p_ptr->cmd.cmd = 'g'; if (inven_carry_okay(o_ptr)) { py_pickup_aux((object_type*)o_ptr); } else { carry(TRUE); } /*cmd_insert(CMD_PICKUP); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } else if (selected == 8) { /* use the item */ bool full = item_tester_full; item_tester_full = FALSE; if (player_can_cast_from(o_ptr)) { set_get_item_object(o_ptr); p_ptr->cmd.cmd = 'm'; do_cmd_cast(); } else { set_get_item_object(o_ptr); p_ptr->cmd.cmd = 'u'; repeat_check(); do_cmd_use(); /*cmd_insert(CMD_USE_ANY); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } item_tester_full = full; } else if (selected == 9) { /* browse a spellbook */ p_ptr->cmd.cmd = 'b'; do_cmd_browse_aux(o_ptr); /* copied from textui_spell_browse */ /*textui_book_browse(o_ptr);*/ return 2; } else if (selected == 10) { /* study a spell book */ p_ptr->cmd.cmd = 'G'; do_cmd_study(FALSE, (object_type*)o_ptr); } else if (selected == 11) { /* use the item to refill a light source */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = 'F'; do_cmd_refill(); /*cmd_insert(CMD_REFILL); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } else if (selected == 12) { /* throw the item */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = 'v'; repeat_check(); do_cmd_throw(); /*cmd_insert(CMD_THROW); cmd_set_arg_item(cmd_get_top(), 0, slot);*/ } else if (selected == 13) { /* drop all of the item stack */ if (get_check(format("Drop %s? ", header))) { set_get_item_object(o_ptr); p_ptr->cmd.arg = o_ptr->number; p_ptr->cmd.cmd = 'd'; do_cmd_drop(); /*cmd_insert(CMD_DROP); cmd_set_arg_item(cmd_get_top(), 0, slot); cmd_set_arg_number(cmd_get_top(), 1, o_ptr->number);*/ } } else if (selected == 14) { /* squelch or unsquelch the item */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = 'k'; do_cmd_destroy(); /*textui_cmd_destroy_menu(slot);*/ } else if (selected == 15) { /* fire some ammo */ set_get_item_object(o_ptr); p_ptr->cmd.cmd = 'f'; repeat_check(); do_cmd_fire(); } else if (selected == -1) { /* this menu was canceled, tell whatever called us to display its menu again */ return 3; } return 1; }
int context_menu_player(int mx, int my) { menu_type *m; rect_region r; int selected; char *labels; cave_type *c_ptr = area(p_ptr->px,p_ptr->py); pcave_type *pc_ptr = parea(p_ptr->px,p_ptr->py); feature_type *feat; m = menu_dynamic_new(); if (!m) { return 0; } labels = (char*)string_make(lower_case); m->selections = labels; feat = &(f_info[c_ptr->feat]); menu_dynamic_add_label(m, "Use Item", 'u', 1, labels); /* if player can cast, add casting option */ if (player_is_caster()) { if (player_can_cast()) { menu_dynamic_add_label(m, "Cast", 'm', 2, labels); } else { menu_dynamic_add_label(m, "$Cast", 'm', 2, labels); } } /* if player can use racial powers or mutations, add option */ if (player_has_power()) { if (player_can_use_power()) { menu_dynamic_add_label(m, "Use Power", 'U', 16, labels); } else { menu_dynamic_add_label(m, "$Use Power", 'U', 16, labels); } } /* if player is on stairs add option to use them */ if (feat->flags & FF_EXIT_UP) { menu_dynamic_add_label(m, "Go Up", '<', 11, labels); } if (feat->flags & FF_EXIT_DOWN) { menu_dynamic_add_label(m, "Go Down", '>', 12, labels); } menu_dynamic_add_label(m, "Search", 's', 3, labels); menu_dynamic_add_label(m, "Look", 'l', 6, labels); menu_dynamic_add_label(m, "Rest", 'R', 4, labels); menu_dynamic_add_label(m, "Inventory", 'i', 5, labels); /* if object under player add pickup option */ if (c_ptr->o_idx) { object_type *o_ptr = &(o_list[c_ptr->o_idx]); //if (!squelch_item_ok(o_ptr)) { menu_dynamic_add_label(m, "Floor", 'i', 13, labels); if (inven_carry_okay(o_ptr)) { menu_dynamic_add_label(m, "Pickup", 'g', 14, labels); } else { menu_dynamic_add_label(m, "$Pickup", 'g', 14, labels); } //} } menu_dynamic_add_label(m, "Character", 'C', 7, labels); /* XXX Don't show the keymap line until the keymap list is implemented, to * avoid confusion as to what should be there */ /*menu_dynamic_add(m, "Keymaps", 10);*/ if (!OPT(center_player)) { menu_dynamic_add_label(m, "^Center Map", 'L', 15, labels); } menu_dynamic_add_label(m, "Other", ' ', 9, labels); /* work out display region */ r.width = menu_dynamic_longest_entry(m) + 3 + 2; /* +3 for tag, 2 for pad */ if (mx > Term->wid - r.width - 1) { r.col = Term->wid - r.width - 1; } else { r.col = mx + 1; } r.page_rows = m->count; if (my > Term->hgt - r.page_rows - 1) { if (my - r.page_rows - 1 <= 0) { /* menu has too many items, so put in upper right corner */ r.row = 1; r.col = Term->wid - r.width - 1; } else { r.row = Term->hgt - r.page_rows - 1; } } else { r.row = my + 1; } /* Hack -- no flush needed */ msg_flag = FALSE; screen_save(); button_backup_all(TRUE); menu_layout(m, &r); rect_region_erase_bordered(&r); prtf(0, 0, "($UEnter to select$Y\n$V, $UESC$Y%c$V) Command:", ESCAPE); selected = menu_dynamic_select(m); menu_dynamic_free(m); string_free(labels); button_restore(); screen_load(); switch(selected) { case 1: { /* use an item */ p_ptr->cmd.cmd = 'u'; repeat_check(); do_cmd_use(); } break; case 2: { /* Cast a spell */ p_ptr->cmd.cmd = 'm'; repeat_check(); do_cmd_cast_wrapper(); } break; case 3: { /* search */ p_ptr->cmd.cmd = 's'; repeat_check(); do_cmd_search(); } break; case 4: { /* rest */ p_ptr->cmd.cmd = 'R'; repeat_check(); do_cmd_rest(); } break; case 5: { /* show inventory screen */ Term_keypress('i');//,0); } break; case 6: { /* look mode */ if (target_set(TARGET_LOOK)) { //if (target_set_interactive(TARGET_LOOK, p_ptr->px, p_ptr->py)) { msgf("Target Selected."); } } break; case 7: { /* show character screen */ do_cmd_character(); } break; case 9: { /* show another layer of menu options screen */ int res; while ((res = context_menu_player_2(mx,my)) == 3); if (res == 2) return 3; } break; case 10: { /* show the commands */ int res; while ((res = context_menu_command(mx,my)) == 3); if (res == 2) return 3; } break; case 11: { /* go up stairs */ p_ptr->cmd.cmd = '<'; repeat_check(); do_cmd_go_up(); } break; case 12: { /* go down stairs */ p_ptr->cmd.cmd = '>'; repeat_check(); do_cmd_go_down(); } break; case 13: { if (c_ptr->o_idx) { object_type *o_ptr = &(o_list[c_ptr->o_idx]); /* there is an item on the floor, show the inventory screen starting * from the floor */ if (o_ptr->next_o_idx) { do_cmd_inven_floor(); } else { /* if we only have one item, show the context menu directly */ if (o_ptr->k_idx) { /* Track the object kind */ object_kind_track(o_ptr->k_idx); while (context_menu_object(o_ptr) == 2); } } } } break; case 14: { /* pick the item up */ //cmd_insert(CMD_PICKUP); //cmd_set_arg_item(cmd_get_top(), 0, -1); carry(TRUE); } break; case 15: { /* center the map on the player */ /*panel_center(p_ptr->px, p_ptr->py);*/ do_cmd_center_map(); } break; case 16: { /* use character powers */ p_ptr->cmd.cmd = 'U'; repeat_check(); do_cmd_racial_power(); } break; } return 1; }
int ser_resp_parser(unsigned char *rx_buf, struct frm_para *mfpara, int rlen) { // int i; unsigned int act_byte; unsigned char qfc; unsigned int qact; unsigned int qlen; // unsigned int raddr; unsigned char rfc; unsigned int rrlen; unsigned int ract; /* char *s[EXCPMSGTOTAL] = { "<Modbus Serial Master> Read Coil Status (FC=01) exception !!", "<Modbus Serial Master> Read Input Status (FC=02) exception !!", "<Modbus Serial Master> Read Holding Registers (FC=03) exception !!", "<Modbus Serial Master> Read Input Registers (FC=04) exception !!", "<Modbus Serial Master> Force Single Coil (FC=05) exception !!", "<Modbus Serial Master> Preset Single Register (FC=06) exception !!" }; */ qfc = mfpara->fc; qlen = mfpara->len; rfc = *(rx_buf+1); rrlen = *(rx_buf+2); if(qfc ^ rfc){ if(rfc > PRESETEXCPSTATUS_EXCP || rfc < READCOILSTATUS_EXCP){ printf("<Modbus Serial Master> Uknow respond function code : %x !!\n", rfc); return -1; } // printf("%s\n", s[rfc-READCOILSTATUS_EXCP]); return -1; } if(!(rfc ^ READCOILSTATUS) || !(rfc ^ READINPUTSTATUS)){ // fc = 0x01/0x02, get data len act_byte = carry((int)qlen, 8); if(rrlen != act_byte){ printf("<Modbus Serial Master> length fault !!\n"); return -1; } /* printf("<Modbus Serial Master> Data :"); for(i = 3; i < rlen-2; i++){ printf(" %x |", *(rx_buf+i)); } printf("\n"); */ }else if(!(rfc ^ READHOLDINGREGS) || !(rfc ^ READINPUTREGS)){ //fc = 0x03/0x04, get data byte if(rrlen != qlen<<1){ printf("<Modbus Serial Master> byte fault !!\n"); return -1; } /* printf("<Modbus Serial Master> Data : "); for(i = 3; i < rlen-2; i+=2){ printf(" %x%x", *(rx_buf+i), *(rx_buf+i+1)); printf("|"); } printf("\n"); */ }else if(!(rfc ^ FORCESIGLEREGS)){ //fc = 0x05, get action ract = *(rx_buf+4); // raddr = *(rx_buf+2)<<8 | *(rx_buf+3); if(ract == 255){ // printf("<Modbus Serial Master> addr : %x The status to wirte on (FC:0x05)\n", raddr); }else if(!ract){ // printf("<Modbus Serial Master> addr : %x The status to wirte off (FC:0x05)\n", raddr); }else{ printf("<Modbus Serial Master> Unknow action !!\n"); return -1; } }else if(!(rfc ^ PRESETEXCPSTATUS)){ // fc = 0x06, get action qact = mfpara->act; // raddr = *(rx_buf+2)<<8 | *(rx_buf+3); ract = *(rx_buf+4)<<8 | *(rx_buf+5); if(qact != ract){ printf("<Modbus Serial Master> Action wrong !!\n"); return -1; } // printf("<Modbus Serial Master> addr : %x Action code = %x\n", raddr, ract); }else{ printf("<Modbus Serial Master> Unknow respond function code = %x\n", rfc); return -1; } return 0; }