Skip to content

Instantly share code, notes, and snippets.

@Eiyeron
Last active September 17, 2018 12:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eiyeron/9394041 to your computer and use it in GitHub Desktop.
Save Eiyeron/9394041 to your computer and use it in GitHub Desktop.
Mode 7
#include "fixed.h"
char* fixtostr (fix f, char* string) {
int n;
int i;
int cpt;
int start = 0;
n = UNFIX(f);
if (n<0) {
start=1;
string[0] = '-';
n *= -1;
}
for (i = 1, cpt = 1; n / i >= 10; i *= 10, cpt++);
for (cpt = start; i; cpt++, i /= 10) string[cpt] = (n / i) % 10 + '0';
string[cpt] = '\0';
return string;
}
fix ftofix(float f) {
return f*(1<<DB);
}
float fixtof(fix f) {
return ((float)f)/(1<<DB);
}
fix fdiv(fix x, fix y) {
if (y>(1<<(2*DB-2))) return x/(y>>DB);
return fmul(x, ((1<<(2*DB))/y));
// return mult(x, (0x40000000/y)>>(30-2*precision));
}
fix fmul(fix x, fix y) {
int d1, d2, e1, e2;
e1 = x >> DB;
e2 = y >> DB;
d1 = x & (0xFFFFFFFF>>(32-DB));
d2 = y & (0xFFFFFFFF>>(32-DB));
return ((e1*e2)<<DB) + e1*d2 + e2*d1 + ((d1*d2)>>DB);
}
// Precomputed table of sinus value, between 0 and 90째 inclued with step of 0.1째.
// Values are in fixed 30 bits of decimal.
int precalc_sin[901] = {
0, 1874032, 3748058, 5622073, 7496071, 9370046, 11243993,
13117905, 14991777, 16865604, 18739378, 20613096, 22486752, 24360338,
26233852, 28107284, 29980632, 31853888, 33727044, 35600100, 37473048,
39345884, 41218596, 43091184, 44963640, 46835960, 48708136, 50580168,
52452040, 54323756, 56195304, 58066684, 59937884, 61808904, 63679732,
65550372, 67420808, 69291040, 71161056, 73030864, 74900440, 76769800,
78638912, 80507792, 82376432, 84244816, 86112936, 87980800, 89848400,
91715720, 93582768, 95449528, 97315992, 99182160, 101048032, 102913592,
104778840, 106643768, 108508376, 110372648, 112236584, 114100176, 115963424,
117826320, 119688856, 121551024, 123412824, 125274248, 127135296, 128995952,
130856208, 132716072, 134575536, 136434592, 138293216, 140151424, 142009216,
143866576, 145723488, 147579952, 149435984, 151291552, 153146656, 155001296,
156855456, 158709152, 160562352, 162415072, 164267296, 166119008, 167970224,
169820928, 171671120, 173520784, 175369920, 177218512, 179066576, 180914096,
182761056, 184607472, 186453312, 188298592, 190143296, 191987424, 193830960,
195673904, 197516256, 199358016, 201199152, 203039680, 204879600, 206718880,
208557552, 210395568, 212232960, 214069696, 215905776, 217741200, 219575968,
221410064, 223243472, 225076224, 226908272, 228739632, 230570304, 232400272,
234229520, 236058064, 237885888, 239712992, 241539360, 243364992, 245189872,
247014032, 248837424, 250660048, 252481920, 254303024, 256123344, 257942896,
259761664, 261579632, 263396800, 265213168, 267028736, 268843488, 270657408,
272470528, 274282784, 276094240, 277904832, 279714592, 281523488, 283331520,
285138720, 286945024, 288750464, 290555008, 292358688, 294161472, 295963360,
297764352, 299564416, 301363584, 303161824, 304959168, 306755552, 308551008,
310345536, 312139104, 313931712, 315723392, 317514112, 319303840, 321092608,
322880384, 324667200, 326453024, 328237824, 330021664, 331804480, 333586272,
335367072, 337146848, 338925568, 340703296, 342479936, 344255584, 346030144,
347803680, 349576160, 351347552, 353117888, 354887136, 356655296, 358422400,
360188384, 361953280, 363717088, 365479744, 367241344, 369001792, 370761120,
372519328, 374276384, 376032320, 377787104, 379540736, 381293184, 383044512,
384794656, 386543616, 388291424, 390038048, 391783456, 393527680, 395270720,
397012544, 398753184, 400492576, 402230752, 403967712, 405703456, 407437952,
409171200, 410903200, 412633952, 414363456, 416091680, 417818656, 419544352,
421268768, 422991904, 424713760, 426434304, 428153568, 429871488, 431588128,
433303456, 435017472, 436730144, 438441504, 440151520, 441860192, 443567488,
445273472, 446978112, 448681344, 450383232, 452083776, 453782912, 455480672,
457177024, 458872000, 460565600, 462257760, 463948544, 465637888, 467325824,
469012352, 470697440, 472381088, 474063296, 475744064, 477423392, 479101248,
480777664, 482452608, 484126080, 485798080, 487468576, 489137632, 490805152,
492471200, 494135744, 495798784, 497460320, 499120352, 500778848, 502435808,
504091264, 505745152, 507397536, 509048352, 510697600, 512345312, 513991456,
515636064, 517279072, 518920512, 520560352, 522198624, 523835328, 525470400,
527103904, 528735776, 530366048, 531994688, 533621728, 535247136, 536870912,
538493056, 540113536, 541732416, 543349632, 544965184, 546579072, 548191296,
549801856, 551410752, 553017920, 554623424, 556227264, 557829376, 559429824,
561028544, 562625600, 564220864, 565814464, 567406336, 568996480, 570584896,
572171520, 573756480, 575339648, 576921088, 578500736, 580078656, 581654784,
583229120, 584801728, 586372544, 587941504, 589508736, 591074176, 592637824,
594199680, 595759680, 597317888, 598874240, 600428800, 601981568, 603532416,
605081472, 606628672, 608174080, 609717568, 611259264, 612799040, 614336960,
615873024, 617407168, 618939456, 620469888, 621998400, 623525056, 625049792,
626572608, 628093504, 629612544, 631129600, 632644800, 634158016, 635669312,
637178688, 638686080, 640191616, 641695104, 643196672, 644696320, 646193984,
647689664, 649183360, 650675072, 652164864, 653652608, 655138368, 656622144,
658103936, 659583680, 661061504, 662537216, 664010944, 665482688, 666952320,
668420032, 669885632, 671349184, 672810688, 674270208, 675727616, 677182976,
678636288, 680087552, 681536704, 682983808, 684428800, 685871744, 687312576,
688751296, 690187968, 691622464, 693054912, 694485248, 695913472, 697339520,
698763520, 700185344, 701605056, 703022592, 704438016, 705851264, 707262400,
708671424, 710078208, 711482880, 712885312, 714285632, 715683776, 717079744,
718473536, 719865088, 721254464, 722641664, 724026688, 725409472, 726790016,
728168384, 729544576, 730918464, 732290176, 733659648, 735026880, 736391872,
737754624, 739115072, 740473344, 741829312, 743183104, 744534528, 745883776,
747230656, 748575360, 749917696, 751257792, 752595584, 753931072, 755264320,
756595200, 757923840, 759250112, 760574080, 761895808, 763215104, 764532160,
765846848, 767159168, 768469248, 769776896, 771082240, 772385216, 773685888,
774984128, 776280064, 777573632, 778864832, 780153600, 781440064, 782724096,
784005760, 785285056, 786561920, 787836416, 789108544, 790378240, 791645504,
792910400, 794172864, 795432896, 796690496, 797945664, 799198464, 800448768,
801696640, 802942080, 804185088, 805425600, 806663744, 807899328, 809132544,
810363264, 811591488, 812817280, 814040576, 815261376, 816479680, 817695552,
818908864, 820119744, 821328128, 822533952, 823737280, 824938176, 826136512,
827332288, 828525568, 829716352, 830904576, 832090304, 833273472, 834454144,
835632192, 836807744, 837980800, 839151232, 840319104, 841484416, 842647232,
843807424, 844965056, 846120128, 847272576, 848422464, 849569792, 850714496,
851856640, 852996224, 854133120, 855267456, 856399232, 857528320, 858654848,
859778752, 860900032, 862018688, 863134720, 864248128, 865358912, 866467008,
867572544, 868675392, 869775616, 870873152, 871968064, 873060288, 874149888,
875236800, 876321088, 877402624, 878481600, 879557824, 880631360, 881702272,
882770432, 883835968, 884898752, 885958848, 887016320, 888070976, 889123008,
890172288, 891218880, 892262784, 893303936, 894342400, 895378112, 896411072,
897441344, 898468864, 899493632, 900515648, 901534976, 902551488, 903565312,
904576384, 905584640, 906590208, 907592960, 908593024, 909590208, 910584704,
911576384, 912565312, 913551488, 914534848, 915515392, 916493184, 917468160,
918440384, 919409792, 920376384, 921340160, 922301184, 923259328, 924214720,
925167296, 926116992, 927063936, 928008000, 928949248, 929887680, 930823296,
931756032, 932686016, 933613056, 934537344, 935458688, 936377280, 937292928,
938205760, 939115776, 940022848, 940927104, 941828544, 942727040, 943622720,
944515456, 945405376, 946292352, 947176512, 948057728, 948936128, 949811584,
950684160, 951553856, 952420608, 953284480, 954145472, 955003584, 955858752,
956710976, 957560320, 958406720, 959250240, 960090816, 960928448, 961763200,
962594944, 963423808, 964249792, 965072768, 965892800, 966709888, 967524096,
968335296, 969143552, 969948864, 970751232, 971550656, 972347072, 973140608,
973931072, 974718656, 975503232, 976284864, 977063488, 977839104, 978611840,
979381504, 980148224, 980911936, 981672704, 982430464, 983185216, 983936960,
984685760, 985431552, 986174272, 986914048, 987650816, 988384576, 989115328,
989843008, 990567744, 991289408, 992008064, 992723776, 993436352, 994145984,
994852544, 995556096, 996256576, 996954048, 997648512, 998339904, 999028288,
999713600, 1000395840, 1001075072, 1001751232, 1002424320, 1003094400, 1003761408,
1004425408, 1005086272, 1005744128, 1006398848, 1007050560, 1007699200, 1008344768,
1008987264, 1009626688, 1010263040, 1010896320, 1011526464, 1012153600, 1012777600,
1013398528, 1014016384, 1014631168, 1015242816, 1015851392, 1016456896, 1017059264,
1017658560, 1018254784, 1018847872, 1019437824, 1020024704, 1020608512, 1021189184,
1021766720, 1022341120, 1022912448, 1023480704, 1024045760, 1024607744, 1025166592,
1025722304, 1026274944, 1026824384, 1027370752, 1027913984, 1028454080, 1028991040,
1029524864, 1030055552, 1030583104, 1031107520, 1031628736, 1032146880, 1032661888,
1033173696, 1033682368, 1034187904, 1034690304, 1035189568, 1035685632, 1036178560,
1036668352, 1037154944, 1037638400, 1038118720, 1038595840, 1039069824, 1039540608,
1040008256, 1040472704, 1040934016, 1041392128, 1041847104, 1042298880, 1042747520,
1043192896, 1043635200, 1044074240, 1044510144, 1044942848, 1045372416, 1045798720,
1046221888, 1046641856, 1047058624, 1047472256, 1047882624, 1048289856, 1048693888,
1049094720, 1049492352, 1049886784, 1050278016, 1050666048, 1051050880, 1051432512,
1051810944, 1052186112, 1052558144, 1052926976, 1053292608, 1053654976, 1054014144,
1054370112, 1054722880, 1055072448, 1055418816, 1055761920, 1056101824, 1056438528,
1056771968, 1057102272, 1057429248, 1057753088, 1058073664, 1058391040, 1058705152,
1059016128, 1059323776, 1059628288, 1059929472, 1060227520, 1060522304, 1060813824,
1061102144, 1061387200, 1061669056, 1061947712, 1062223104, 1062495232, 1062764160,
1063029824, 1063292224, 1063551424, 1063807424, 1064060096, 1064309568, 1064555840,
1064798784, 1065038528, 1065275072, 1065508288, 1065738304, 1065965056, 1066188608,
1066408896, 1066625856, 1066839680, 1067050176, 1067257472, 1067461440, 1067662208,
1067859776, 1068054016, 1068245056, 1068432768, 1068617280, 1068798528, 1068976512,
1069151232, 1069322752, 1069490944, 1069655936, 1069817600, 1069976064, 1070131264,
1070283200, 1070431808, 1070577216, 1070719360, 1070858240, 1070993856, 1071126272,
1071255360, 1071381184, 1071503744, 1071623040, 1071739072, 1071851840, 1071961344,
1072067584, 1072170560, 1072270272, 1072366720, 1072459904, 1072549824, 1072636480,
1072719872, 1072800000, 1072876800, 1072950400, 1073020672, 1073087744, 1073151488,
1073211968, 1073269248, 1073323200, 1073373888, 1073421312, 1073465472, 1073506304,
1073543936, 1073578304, 1073609344, 1073637184, 1073661696, 1073682944, 1073700928,
1073715648, 1073727104, 1073735296, 1073740160, 1073741824 };
fix fsin(fix a) {
int v;
if(a<0) v = 3600+UNFIX(a*10)%3600;
else v = UNFIX(a*10)%3600;
if (v<=900) return precalc_sin[v]>>(30-DB);
if (v<=1800) return precalc_sin[1800-v]>>(30-DB);
if (v<=2700) return -(precalc_sin[v-1800]>>(30-DB));
return -(precalc_sin[3600-v]>>(30-DB));
}
fix fcos(fix a) {
return fsin(FIX(90)-a);
}
fix ftan(fix a) {
return fdiv(fsin(a), fcos(a));
}
#ifndef FIXED_H
#define FIXED_H
// Bit lenght of the decimal part, change it to change the precision :
#define DB 15
#define MOD(x, y) ((x)<0 ? (y)+(x)%(y) : (x)%(y))
typedef int fix;
// OOOOOOO
// Fixed dot manipulations functions and macros :
char* fixtostr (fix n, char* string);
fix ftofix(float f);
float fixtof(fix f);
#define FIX(x) ((x)<<DB)
#define UNFIX(x) ((x)>>DB)
fix fdiv(fix x, fix y);
fix fmul(fix x, fix y);
fix fsin(fix a);
fix fcos(fix a);
fix ftan(fix a);
#endif // FIXED_H
/*****************************************************************/
/* */
/* CASIO fx-9860G SDK Library */
/* */
/* File name : [ProjectName].c */
/* */
/* Copyright (c) 2006 CASIO COMPUTER CO., LTD. */
/* */
/*****************************************************************/
#include "fxlib.h"
#include "MonochromeLib.h"
#include "fixed.h"
void fixM7() {
unsigned char sprite[16][16] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0},
{0,0,0,0,1,1,1,1,0,1,1,1,0,0,0,0},
{0,0,0,0,1,0,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,1,1,1,1,1,1,0,1,0,0,0,0},
{0,0,0,0,1,1,0,1,1,0,1,1,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
const fix s = FIX(64);
const fix horizon = FIX(32);
const fix fov = FIX(64);
int time = 0;
unsigned rx, ry,rufx,rufy,spx, spy;
unsigned i, j;
fix sx, sy;
while(1){
fix angle = fdiv(FIX(time), FIX(1));
unsigned ca = fcos(angle), sa = fsin(angle);
ML_clear_vram();
for(i = UNFIX(horizon) + 1; i< 64; i++) {
fix pz = FIX(i) - horizon;
fix py = pz - fov;
//if(pz <= 0) continue;
sy = fdiv(fmul(fdiv(py, pz), s), FIX(10));
time++;
for(j = 0; j < 128; j++) {
sx = fdiv(fmul(fdiv(FIX(j-64), pz), s), FIX(10));
rx = fmul(sx, ca) - fmul(sy, sa);
ry = fmul(sx, sa) + fmul(sy, ca);
rufx = UNFIX(rx);
rufy = UNFIX(ry);
spx = rufx&15;
spy = rufy&15;
if(sprite[spy][spx])
ML_pixel(j,i, 1);
}
}
ML_display_vram();
}
}
int AddIn_main(int isAppli, unsigned short OptionNum)
{
fixM7();
return 1;
}
//****************************************************************************
//************** ****************
//************** Notice! ****************
//************** ****************
//************** Please do not change the following source. ****************
//************** ****************
//****************************************************************************
#pragma section _BR_Size
unsigned long BR_Size;
#pragma section
#pragma section _TOP
//****************************************************************************
// InitializeSystem
//
// param : isAppli : 1 = Application / 0 = eActivity
// OptionNum : Option Number (only eActivity)
//
// retval : 1 = No error / 0 = Error
//
//****************************************************************************
int InitializeSystem(int isAppli, unsigned short OptionNum)
{
return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
}
#pragma section
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment