//Add with carry void adc(CPU *c, OP_CODE_INFO *o){ int8_t carry = getFlag(c,C); int8_t accum = getRegByte(c,ACCUM); int8_t operand = o->operand; int16_t sum = (0x00FF&carry) + (0x00FF&accum) + (0x00FF&operand); int8_t sumByte = sum & 0x00FF; setZero(c,sumByte); if(getFlag(c,D)){ //in decimal mode //if lower 4 bits of operands plus //the carry in are larger than 9, //then we need to apply conversions //to remain in binary coded decimal format. if((accum & 0xF) + (operand & 0xF) + carry > 9){ sum += 6; } setSign(c,sum&0xFF); setOverflow(c,accum,operand,sum&0xFF); //if the higher bits aren't in //BCD format we need to add 96 to convert. //Black magic from http://nesdev.com/6502.txt sum += sum > 0x99 ? 96 : 0; setCarryBCD(c, sum); } else { setSign(c,sumByte); setOverflow(c,accum,operand,sumByte); setCarry(c,sum); } setRegByte(c,ACCUM,sum&0xFF); }
int sub(struct NUMBER *a,struct NUMBER *b,struct NUMBER *c) { int i,f_hizero = 0,h = 0,res = 0,tmp = 1; struct NUMBER tmp_a,tmp_b; /*値を動かすので一時的に保存*/ copyNumber(a,&tmp_a); copyNumber(b,&tmp_b); if(numComp(a,b) == -1) { copyNumber(a,&tmp_b); copyNumber(b,&tmp_a);//a > bにしたいので tmp = -1; } else { copyNumber(a,&tmp_a); copyNumber(b,&tmp_b); } clearByZero(c);//cを0に if(getSign(a) == 1&&getSign(b) == 1)//a,bも+の時は通常のsub { if(numComp(&tmp_a,&tmp_b) == 0) return(0);//aとbが同じならcは0だから f_hizero = firstNotZero(&tmp_a);//aは必ず大きい値なのでf_hizeroも大きい方になる /*計算部*/ for(i = 0;i <= f_hizero;i++) { tmp_a.n[i] -= h;//繰り下がり分を引く h = (tmp_a.n[i]>=tmp_b.n[i])? 0:1; c->n[i] = h * 10 + tmp_a.n[i] - tmp_b.n[i]; } if(f_hizero == KETA - 1&&h != 0) res = -1; setSign(c,tmp); } /*絶対値を使うので保存*/ getAbs(a,&tmp_a); getAbs(b,&tmp_b); if(getSign(a) == 1 && getSign(b) == -1)//aが+、bが-の時はa+b通常のadd res = add(a,&tmp_b,c); if(getSign(a) == -1 && getSign(b) == 1 )//aが-、bが+の時は-a-b、つまり-(a+b)、addの結果を負にする { res = add(b,&tmp_a,c); setSign(c,-1); } if(getSign(a) == -1 && getSign(b) == -1)//aもbも-の時は-a + b、つまりb-aをすればいい。 res = sub(&tmp_b,&tmp_a,c); return(res); }
int sub(struct NUMBER *a,struct NUMBER *b,struct NUMBER *c) { int i,h=0,d=0; struct NUMBER temp; clearByZero(&temp); clearByZero(c); if(getSign(a)==1) { if(getSign(b)==1) { for(i=0;i<KETA;i++) { if(numComp(a,b)==1) { d=a->n[i]-b->n[i]-h; h=0; if(d<0) { d+=10; h=1; } c->n[i]=d; } else { getAbs(b,&temp); sub(&temp,a,c); setSign(c,-1); } } } else if(getSign(b)==-1) { getAbs(b,&temp); add(a,&temp,c); } } else if(getSign(a)==-1) { if(getSign(b)==1) { getAbs(a,&temp); add(b,&temp,c); setSign(c,-1); } else if(getSign(b)==-1) { getAbs(b,&temp); add(&temp,a,c); } } return 0; }
//減算 int sub(struct NUMBER *a,struct NUMBER *b,struct NUMBER *c){ int h=0;//桁下がりみてる int i; int cha,chb;//構造体が正か負か確認引数 int flag; struct NUMBER d;//絶対値をとる struct NUMBER e,f;//大小比較するためのe>f clearByZero(c); clearByZero(&e); clearByZero(&f); if(numComp(a,b)==1){ //大きいほうをe小さいほうをf copyNumber(a,&e); copyNumber(b,&f); setSign(c,1); } if(numComp(a,b)==-1){ //上に同じく copyNumber(b,&e); copyNumber(a,&f); setSign(c,-1); } if(numComp(a,b)==0){ setInt(c,0); setSign(c,1); return 1; } for(i=KETA-1;i>-1;i--){ e.n[i]=e.n[i]-h; //aiからhを引く if(e.n[i] >= f.n[i]){//ai>=biならば c->n[i]=e.n[i]-f.n[i];//ci=ai-bi h=0;//h=0 } if(e.n[i] < f.n[i]){//ai<biならば c->n[i]=10+e.n[i]-f.n[i];//ci=10+ai-bi h=1;//h=1 } } if(h != 0){ printf("/erroe\n"); return -1; } return 0; }
int multiple(struct NUMBER *a,struct NUMBER *b,struct NUMBER *c) { int i,j,h=0,tempb,tempa,e; struct NUMBER d,mai1,mai2,temp; clearByZero(&d); clearByZero(c); if(getSign(a)==1) { if(getSign(b)==1) { for(i=0;i<KETA-1;i++) { h=0; clearByZero(&d); tempb=b->n[i]; for(j=0;j<KETA;j++) { tempa=a->n[j]; e=tempa*tempb + h; if(j+i<KETA) d.n[j+i]=e%10; h=e/10; } copyNumber(c,&temp); add(&temp,&d,c); } } else if(getSign(b)==-1) { getAbs(b,&mai1); multiple(a,&mai1,c); setSign(c,-1); } } else if(getSign(a)==-1) { if(getSign(b)==1) { getAbs(a,&mai1); multiple(b,&mai1,c); setSign(c,-1); } else if(getSign(b)==-1) { getAbs(a,&mai1); getAbs(b,&mai2); multiple(&mai1,&mai2,c); } } }
int arctan(struct NUMBER *a,struct NUMBER *b) { struct NUMBER i,tmp1,tmp2; struct NUMBER apow,tenpow,mone,n;//計算に使う変数 int j,res; clearByZero(b); clearByZero(&i); clearByZero(&tmp1); clearByZero(&tmp2); clearByZero(&apow); clearByZero(&tenpow); setInt(&n,1); tenpow.n[KETA-1] = 1;//tenpowは10^(KETA-1) j = 0; while(1) { res = fastpower(a,&n,&apow);//apow = a^n if(res != 0) break; res = multiple(&n,&apow,&tmp1);//tmp1 = n * apow if(res != 0) break; res = divide(&tenpow,&tmp1,&tmp2,&apow);//tmp2 = tenpow/tmp1,apowは使わないからこれ以降使わないから if(res != 0) break; if(i.n[0]%2)//iが奇数なら,手っ取り早く偶奇を見たいからこう setSign(&tmp2,-1); else //iが偶数なら setSign(&tmp2,1); copyNumber(b,&tmp1); add(&tmp1,&tmp2,b);//b += (1/n * 1/(a^n)) * 10^(KETA-1),オーバーフローなどはないので戻り値は保存しない if(firstNotZero(&tmp2) <= KETA/10)//(1/i * 1/(a^i)) * 10^(KETA-1)がKETAの1/10程度になったら break; /*2i +1の操作*/ copyNumber(&i,&tmp1);//i → tmp increment(&tmp1,&i);//i++ copyNumber(&i,&tmp1);//i → tmp1 add(&tmp1,&i,&n);//n = 2i(n= i + i) copyNumber(&n,&tmp1);//i → tmp increment(&tmp1,&n);//i++ } return(res); }
BigInt::BigInt(int n) { // "int" constructor stringstream ss; string s; ss << n; ss >> s; if (isdigit(s[0])) { // if not signed setNumber(s); setSign(false); // +ve } else { setNumber(s.substr(1)); setSign(s[0] == '-'); } count++; }
void clearByZero(struct NUMBER *a) { int i; for(i = 0;i < KETA;i++) a->n[i] = 0; setSign(a,1); }
void and(CPU *c, OP_CODE_INFO *o){ int8_t accum = getRegByte(c,ACCUM); int8_t operand = o->operand; int8_t res = accum & operand; setSign(c,res); setZero(c,res); setRegByte(c,ACCUM,res); }
void copyNumber(struct NUMBER *a,struct NUMBER *b) { int i; clearByZero(b);//一応0クリア for(i = 0;i < KETA;i++)//最大桁までiを大きくする b->n[i] = a->n[i]; setSign(b,getSign(a)); }
void copyNumber(struct NUMBER *a,struct NUMBER *b) { int i; setSign(b,a->sign); for(i=KETA-1;i>=0;i--) { b->n[i]=a->n[i]; } }
//------------------------------------------------------------- BigInteger::BigInteger(int n) // "int" constructor { stringstream ss; string s; ss << n; ss >> s; if( isdigit(s[0]) ) // if not signed { setNumber( s ); setSign( false ); // +ve } else { setNumber( s.substr(1) ); setSign( s[0] == '-' ); } }
//表示 void dispNumber(struct NUMBER *p) { int i; setSign(p,1); for(i=0;i<KETA;i++){ printf("%d ",p->n[i]); } }
void Fe65p2PixelCfg::fromFileJson(json &j) { for (unsigned col=1; col<=n_Col; col++) { for (unsigned row=1; row<=n_Row; row++) { setSign(col, row, j["FE65-P2"]["PixelConfig"][col-1]["Sign"][row-1]); setInjEn(col, row, j["FE65-P2"]["PixelConfig"][col-1]["InjEn"][row-1]); setTDAC(col, row, j["FE65-P2"]["PixelConfig"][col-1]["TDAC"][row-1]); setPixConf(col, row, j["FE65-P2"]["PixelConfig"][col-1]["PixConf"][row-1]); } } }
//桁数分だけ表示 void kt_disp(struct NUMBER *p,int kt) { int i; setSign(p,1); for(i=KETA-kt;i<KETA-1;i++){ printf("%d ",p->n[i]); } }
int divBy10(struct NUMBER *a,struct NUMBER *b) { int i,f_hizero = firstNotZero(a),res = 0; clearByZero(b);//0で初期化する res = a->n[0];//最下位桁の値がそのままあまりになるので for(i = 0;i < f_hizero;i++)//bにi + 1が含まれるのを考慮してi = 1 b->n[i] = a->n[i + 1];//a[i + 1]の値を変更するのでf_hizero未満でiを動かす setSign(b,getSign(a));//aとbの符号をそろえる return(res); }
void clearByZero(struct NUMBER *p) { int i; for(i=0;i<KETA;i++) { p->n[i] = 0; } setSign(p,1); //符号をプラス順にセット }
//Arithmetic shift left void asl(CPU *c, OP_CODE_INFO *o){ int8_t operand = o->operand; int16_t res = (0x00FF&operand) << 1; int8_t resByte = res & 0x00FF; setCarry(c,res); setSign(c,resByte); setZero(c,resByte); if(o->mode == modeAccumulator){ setRegByte(c,ACCUM,res); } else { write(c,o->address,res); } }
int setInt(struct NUMBER *a,int x) { int i; clearByZero(a); if(x<0) { setSign(a,-1); x=x*(-1); } else if(x>0) setSign(a,1); else setSign(a,0); for(i=0;i<KETA;i++) { a->n[i]=x%10; x=x/10; if(x<1) break; } }
int add(struct NUMBER *a,struct NUMBER *b, struct NUMBER *c) { int i,e=0,d=0; int flag=0; struct NUMBER temp,temp2; clearByZero(&temp); clearByZero(&temp2); clearByZero(c); if(getSign(a)==1) { if(getSign(b)==1) { for(i=0;i<KETA;i++) { d=a->n[i]+b->n[i]+e; if(i==KETA-1 && d>=10) { printf("OverFlow\n"); clearByZero(c); flag=1; break; } c->n[i]=d%10; e=d/10; } } else if(getSign(b)==-1) { getAbs(b,&temp); sub(a,&temp,c); } } else { if(getSign(b)==1){ getAbs(a,&temp); sub(b,&temp,c); } else if(getSign(b)==-1) { getAbs(a,&temp); getAbs(b,&temp2); add(&temp,&temp2,c); setSign(c,-1); } } return flag; }
int mulBy10(struct NUMBER *a,struct NUMBER *b) { int i,f_hizero = firstNotZero(a),res = 0; clearByZero(b);//0で初期化する if(f_hizero == KETA - 1)//最初の非ゼロが最高桁なら res = -1;//フローがある else if(f_hizero == 0)//最初の非ゼロが最低桁(0)なら res = -1;//十倍しても意味がない(アンダーフロー…?) for(i = 0;i <= f_hizero;i++)//最高位の非ゼロまで b->n[i + 1] = a->n[i];//a[i]をb[i + 1]にうつす = 10倍 setSign(b,getSign(a));//aとbの符号をそろえる return(res); }
Frog::Frog(float x, float y, sf::Texture &s) : DynamicGameEntity(x, y, s){ setFrameTime(sf::seconds(0.07)); facingRight = -1; gravity = 20; moveSpeed = 50; jumpSpeed = -3; setSign(false); type = entityTypes::NPC; //boundingBox boundingBox.top = 20; boundingBox.height = 11; boundingBox.left = 7; boundingBox.width = 16; facingRight = true; }
int add(struct NUMBER *a,struct NUMBER *b,struct NUMBER *c) { int d = 0,e = 0,f_hizero = 0,i,res = 0; struct NUMBER abs_a,abs_b; /*絶対値を使う可能性があるので保存*/ getAbs(a,&abs_a); getAbs(b,&abs_b); clearByZero(c); f_hizero = (firstNotZero(a)>firstNotZero(b))?firstNotZero(a):firstNotZero(b); //f_hizeroは最初の非ゼロが現れる場所が高い方の非ゼロの場所を格納 /*計算部*/ if(getSign(a) == 1&&getSign(b) == 1)//a,bも+の時は通常のadd { for(i = 0;i <= f_hizero + 1&&i <= KETA - 1;i++) //桁上りがある場合があるのでf_hizero + 1まで、またf_hizero == KETA-1の時はKETA-1まで { d = a->n[i] + b->n[i] + e; c->n[i] = d%10; e = d / 10; } if(f_hizero == KETA - 1&&e != 0) res = -1; } if(getSign(a) == 1 && getSign(b) == -1)//aが+、bが-の時はa-b通常のsub res = sub(a,&abs_b,c); if(getSign(a) == -1 && getSign(b) == 1)//aが-、bが+の時は-a+b、つまりb-a、aとbが逆になったsub res = sub(b,&abs_a,c); if(getSign(a) == -1 && getSign(b) == -1)//aもbも-の時は-(a+b)、つまりaddの結果を負にする { res = add(&abs_a,&abs_b,c); setSign(c,-1);//cを-にする } return(res); }
int setInt(struct NUMBER *a,int x) { int tmp,hugou;//一時変数 int i,res = 0; hugou = (x >= 0)?1 : -1;//tmpを符号決定変数とする for(i = 0;i < 10||x == 0;i++) { if(i == KETA)//iが上限を超えたら { res = -1; break; } tmp = x % 10; a->n[i] = abs(tmp);//絶対値を返す x -= tmp;//10で割ったときのあまりを引く x /= 10;//10で割る } setSign(a,hugou); }
//------------------------------------------------------------- BigInteger::BigInteger(string s, bool sin) // "string" constructor { setNumber( s ); setSign( sin ); }
int main(void) { srandom(time(NULL)); struct NUMBER a,b,c,d,e; int r; int x=0,y=0,z=0; int i,flag; //setint のテスト setInt(&a,-12344); printf("a = "); dispNumber(&a); printf("\n"); //setSignのテスト copyNumber(&a,&b); setSign(&b,1); printf("b = "); dispNumber(&b); printf("\n"); //getSignのテスト r=getSign(&b); printf("getSign() = %d\n",r); //numCompのテスト r=numComp(&a,&b); printf("numComp() = %d\n",r); setInt(&b,-9996702); printf("b = "); dispNumber(&b); printf("\n"); //addのテストプログラム add(&b,&a,&c); printf("add = "); dispNumber(&c); printf("\n"); //subのテストプログラム sub(&c,&a,&d); printf("sub = "); dispNumber(&d); printf("\n"); setInt(&a,12345); setInt(&b,67894); //multipleのテスト multiple(&b,&a,&d); printf("mul = "); dispNumber(&d); printf("\n"); setInt(&a,INT_MIN); printf("a = "); dispNumber(&a); printf("\n"); putchar('\n'); return 0; }
void getAbs(struct NUMBER *a,struct NUMBER *b) { copyNumber(a,b); setSign(b,1); }
void Frog::update(sf::Time delta, float deltaTime){ if (World::checkForCollision(this, downWardForce, false)){ downWardForce = 0; touchinigGround = true; inair = false; } else{ touchinigGround = false; downWardForce += gravity * deltaTime; } if(isPositive()){ if (currentAnimation != &aGoingPos && currentAnimationState != animationState::POSITIVE){ currentAnimation = &aGoingPos; }else{ if (currentAnimation->isAnimationDone()){ currentAnimationState = animationState::POSITIVE; int turnOrNot2Turn = std::rand() % 100; // 0 - 100 if (!inair && touchinigGround){ if (turnOrNot2Turn == 50){ facingRight = !facingRight; } } if (isFacingRight()){ currentAnimation = &agroundRight; currentState = state::FACINGRIGHT; } else{ currentAnimation = &agroundLeft; currentState = state::FACINGLEFT; } int jumpOrNotToJump = std::rand() % 100; // 0 - 100 if (jumpOrNotToJump == 0 && touchinigGround){ currentState = state::JUMPING; downWardForce = jumpSpeed; touchinigGround = false; inair = true; if (isFacingRight()){ currentAnimation = &aJumpingRight; } else{ currentAnimation = &aJumpingLeft; } } if (inair){ if (isFacingRight()){ World::checkForCollision(this, moveSpeed * deltaTime, true); currentAnimation = &aJumpingRight; } else{ World::checkForCollision(this, -moveSpeed * deltaTime, true); currentAnimation = &aJumpingLeft; } } } } //check to see if it is touching a enemy for (int i = 0; i < World::getAllEnemies().size(); i++){ if (World::getAllEnemies()[i]->getBoundingBox().intersects(getBoundingBox()) && !World::getAllEnemies()[i]->isPositive()){ currentAnimationState = animationState::NEGATIVE; setSign(false); } } if (World::getPlayer()->getBoundingBox().intersects(getBoundingBox())){ if (World::getPlayer()->isPositive()) setSign(true); else setSign(false); } }else{ currentAnimation = &anegative; if(World::getPlayer()->getBoundingBox().intersects(getBoundingBox())){ if (World::getPlayer()->isPositive()) setSign(true); else setSign(false); } } play(*currentAnimation); updateAnimation(delta); }
int main(void) { char temp, i; LCD_Initialize(); DDRB = 0b00000000; PORTB = 0b00001111; DDRA = 0xFF; ADC_Init(); int value = 0; int calculations = 0; char dzialanie = 0; int digit = 0; do{ int digit = getADC(0); char sw0 = PINB & 0b00000001; char sw1 = PINB & 0b00000010; if(sw0 != 0b00000001) { state++; _delay_ms(300); } char str[15]; sprintf(str, "%15d", lastValue); LCD_GoTo(1,0); LCD_WriteText(str); switch(state){ case 0: if(sw1 != 0b00000010){ setNewValue(mappingLogToLinear(digit, digitMap, 10)); _delay_ms(300); } sprintf(str, "%15d", mappingLogToLinear(digit, digitMap,10)); LCD_GoTo(1,1); LCD_WriteText(str); break; case 1: if(sw1 != 0b00000010){ setSign(mappingLogToLinear(digit, signMap, 2)); power = 0; _delay_ms(300); } switch(mappingLogToLinear(digit, signMap,2)){ case 0: LCD_GoTo(1,1); LCD_WriteText("-"); break; case 1: LCD_GoTo(1,1); LCD_WriteText("+"); break; } break; case 2: if(sw1 != 0b00000010){ doCalculations(mappingLogToLinear(digit, expressionMap, 4)); power = 0; newValue=0; state = 0; _delay_ms(300); } switch(mappingLogToLinear(digit, expressionMap,4)){ case 0: LCD_GoTo(1,1); LCD_WriteText("+"); break; case 1: LCD_GoTo(1,1); LCD_WriteText("-"); break; case 2: LCD_GoTo(1,1); LCD_WriteText("*"); break; case 3: LCD_GoTo(1,1); LCD_WriteText("/"); break; } break; } /* char sw0 = PINB & 0b00000001; char sw1 = PINB & 0b00000010; char sw2 = PINB & 0b00000100; char sw3 = PINB & 0b00001000; if(sw0 != 0b00000001) value++; if(sw1 != 0b00000010) value--; if(sw2 != 0b00000100) { if(dzialanie == 0) calculations += value; if(dzialanie == 1) calculations -= value; if(dzialanie == 2) calculations /= value; if(dzialanie == 3) calculations *= value; value = 0; } if(sw3 != 0b00001000) { dzialanie++; dzialanie = dzialanie % 4; } char str[15]; sprintf(str, "%15d", calculations); LCD_GoTo(1,0); LCD_WriteText(str); sprintf(str, "%15d", value); LCD_GoTo(1,1); LCD_WriteText(str); _delay_ms(300); if(dzialanie == 0){ LCD_GoTo(0,0); LCD_WriteText("+"); } if(dzialanie == 1){ LCD_GoTo(0,0); LCD_WriteText("-"); } if(dzialanie == 2){ LCD_GoTo(0,0); LCD_WriteText("/"); } if(dzialanie == 3){ LCD_GoTo(0,0); LCD_WriteText("*"); }*/ }while(1); return 0; }
//------------------------------------------------------------- void BigInteger::operator = (BigInteger b) { setNumber( b.getNumber() ); setSign( b.getSign() ); }