main.c (18907B)
1 #define _GNU_SOURCE 2 #ifdef _WIN32 3 #include <windows.h> 4 #endif 5 #include <GLFW/glfw3.h> 6 #include <GL/glu.h> 7 #include <stdint.h> 8 #include <string.h> 9 #include <stdlib.h> 10 #include <stdio.h> 11 #include "debuglog.h" 12 #ifdef _WIN32 13 #include "win32_compat.h" // cdecl wrappers for stdcall GL, mikmod stubs, crash log, usleep 14 #else 15 #include <mikmod.h> 16 #include <ucontext.h> 17 #include <alloca.h> 18 #include <signal.h> 19 #include <sys/time.h> 20 #include <unistd.h> 21 #define GLF(x) (&x) 22 #endif 23 24 //#include <pthread.h> 25 //#include <mikmod.h> 26 27 28 /* SPECS 29 32bit compiler 30 Simplified launch 31 auto frees malloc when COLD called 32 */ 33 34 // Sound 35 MODULE* module; 36 SAMPLE* samples[4]; 37 int voices[4]; 38 int modplaying = 1; 39 40 // threading 41 //pthread_t THREADS[4]; 42 43 44 // OGL 45 GLFWwindow* window; 46 typedef unsigned char uchar; 47 typedef unsigned int uint; 48 typedef unsigned short ushort; 49 50 uint IWIDTH = 640; 51 uint IHEIGHT = 480; 52 float SCALE = 4096.0f; 53 #define ISCALE (1.0f/SCALE) 54 55 // MACROS ---- 56 // #define STDC __attribute__((stdcall)) 57 #define SRCSZE (1440*2880)>>2 58 #define BLKNME "data.blk" 59 #define WW extern uint* 60 #define XX uint* STK 61 62 // MEM ---- 63 extern uint* DATABLK; 64 extern uint* CDATA; // 0-9 INPUT 10...29 C func 30... GL func 65 extern ushort WIDTH; 66 extern ushort HEIGHT; 67 68 uint MALLOC_ARRAY[512]; 69 uint* CMALLOC = &MALLOC_ARRAY; 70 void freemalloc() { 71 for(int i = 0; i < 512; i++) 72 if(MALLOC_ARRAY[i]) { free(MALLOC_ARRAY[i]); MALLOC_ARRAY[i] = 0; } 73 CMALLOC = &MALLOC_ARRAY; 74 } 75 76 uint* blkread() { 77 FILE* fs = fopen(BLKNME, "rb"); 78 if(!fs) return NULL; 79 uint* mem = (uint*)&DATABLK; 80 fread((char*)mem, 4, SRCSZE, fs); 81 fclose(fs); 82 printf("-- memory @: %X\n", mem); 83 return DATABLK; 84 } 85 86 void blkwrite() { 87 FILE* fs = fopen(BLKNME, "wb"); fseek(fs, 0, SEEK_SET); 88 fwrite(DATABLK, 4, SRCSZE, fs); 89 fflush(fs); fclose(fs); 90 } 91 92 // fps 93 char* fps_msg[32]; 94 double fps_elapsed = 0, fps_lastime = 0; 95 uint fps_framecnt = 0; 96 void fps() { 97 double ctime = glfwGetTime(); 98 double res = ctime-fps_lastime; 99 fps_elapsed += ctime - fps_lastime; 100 fps_lastime = ctime; 101 ++fps_framecnt; 102 if(fps_elapsed >= 1.0) { 103 sprintf((char*)fps_msg,"PC32 FPS: %d %d", fps_framecnt, (int)(glfwGetTime()*1000000)); 104 glfwSetWindowTitle(window, (const char*)fps_msg); 105 fps_framecnt = 0; fps_elapsed = 0; 106 } 107 } 108 109 // CALLBACKS -------------------------------------------------------------- 110 111 // log what the window and GL actually have right now 112 static void dbg_gl(const char* what, ...) { 113 if(g_dbg == 0) return; 114 char tag[96]; va_list a; va_start(a, what); vsnprintf(tag, sizeof tag, what, a); va_end(a); 115 int ww = 0, wh = 0, fw = 0, fh = 0; GLint vp[4] = {0,0,0,0}; GLfloat zx = 0, zy = 0; 116 glfwGetWindowSize(window, &ww, &wh); 117 glfwGetFramebufferSize(window, &fw, &fh); 118 glGetIntegerv(GL_VIEWPORT, vp); glGetFloatv(GL_ZOOM_X, &zx); glGetFloatv(GL_ZOOM_Y, &zy); 119 dbg("%s: window %dx%d fb %dx%d | IWIDTH/IHEIGHT %ux%u WIDTH/HEIGHT %ux%u | GL viewport %d,%d %dx%d zoom %.3f,%.3f\n", 120 tag, ww, wh, fw, fh, IWIDTH, IHEIGHT, (unsigned)WIDTH, (unsigned)HEIGHT, vp[0], vp[1], vp[2], vp[3], zx, zy); 121 } 122 123 void resize(GLFWwindow* window, int W, int H) { 124 WIDTH = W; HEIGHT = H; (&CDATA)[0] = 1; 125 //glPixelZoom((float)W/1280.0f, (float)H/1024.0f); 126 glPixelZoom((float)W/(float)IWIDTH, (float)H/(float)IHEIGHT); 127 glViewport(0, 0, IWIDTH, IHEIGHT); 128 dbg_gl("resize(%d,%d)", W, H); 129 } 130 131 #ifdef _WIN32 132 // Linux: the first "window resized" event comes from the window manager AFTER the VM is running, 133 // and the Forth side reacts to the CDATA[0] flag it sets (re-reads WX/WY, calls winscale -> viewport). 134 // Windows: the window is sized before the VM starts and the VM clears that flag at startup, so the 135 // event would be lost. The VM calls this instead of glfwPollEvents and, for its first few polls, 136 // re-sends the current window size. 137 static void win_pollevents(void) { 138 static int resends = 5; 139 glfwPollEvents(); 140 if(resends > 0) { 141 resends--; 142 int W, H; 143 glfwGetWindowSize(window, &W, &H); 144 resize(window, W, H); 145 } 146 } 147 #define POLLEVENTS (&win_pollevents) 148 #else 149 #define POLLEVENTS (&glfwPollEvents) 150 #endif 151 152 void key_cb(GLFWwindow* window, int key, int scancode, int action, int mods) { 153 if(!action || key < 200) { (&CDATA)[1] = 0; return; } 154 (&CDATA)[2] = key; 155 (&CDATA)[3] = mods; 156 //printf("key: %d scan: %d action: %d mods: %d \n", key, scancode, action, mods); 157 } 158 159 void char_cb(GLFWwindow* window, uint unicode) { 160 (&CDATA)[1] = unicode; 161 //printf("char: %d\n", unicode); 162 } 163 164 void mousepos_cb(GLFWwindow* window, double x, double y) { 165 int mx = ((int)(((float)x/(float)WIDTH)*((float)IWIDTH))); 166 int my = (int)(((float)y/(float)HEIGHT)*((float)IHEIGHT)); 167 (&CDATA)[4] = ((ushort)my)<<16 | (ushort)mx; } 168 169 void mousebot_cb(GLFWwindow* window, int button, int action, int mods) { 170 (&CDATA)[5] = ((button+1)<<4)|action&0xF; } 171 172 void mousescr_cb(GLFWwindow* window, double x, double y) 173 { (&CDATA)[6] = (int32_t)y; } 174 175 // glfwGetJoystickHats only exists in GLFW >= 3.3; weak so older libs still link 176 extern const unsigned char* glfwGetJoystickHats(int, int*) __attribute__((weak)); 177 void cjoy(uint X) { 178 int acnt = 0, bcnt = 0, hcnt = 0; 179 float* axes = glfwGetJoystickAxes(X, &acnt); 180 char* butn = glfwGetJoystickButtons(X, &bcnt); 181 char* hats = glfwGetJoystickHats ? (char*)glfwGetJoystickHats(X, &hcnt) : 0; 182 (&CDATA)[7] = acnt; 183 (&CDATA)[8] = bcnt; 184 (&CDATA)[9] = hcnt; 185 } 186 187 188 // C FNS 189 extern uint cprint(char* msg) { 190 for(int i = 0; i < 16; i++) 191 printf("%c", msg[i]); 192 printf("?\n"); 193 return -1; 194 } 195 196 uint cdebug(XX) { 197 printf("called from C! \n"); 198 return 0xAABB; 199 } 200 201 char* cdot(char* X) { 202 int len = X[-1]&15; 203 printf("P:"); 204 for(int i = 0; i < len; i++) 205 printf("%c", X[i]); 206 printf("\n"); 207 return X; 208 } 209 210 char* cpystr(char* from, char* to) { 211 int len = from[-1]&15; 212 for(int i = 0; i < len; i++) 213 to[i] = from[i]; 214 to[len] = 0; 215 return to; 216 } 217 218 void cblk_write(XX) { blkwrite(); return STK; } 219 void csleep(uint X) { if(X) usleep(X); } 220 uint copen(char* path, char* mode) { 221 char p[256]; char m[256]; 222 cpystr(path, p); cpystr(mode, m); 223 FILE* han = fopen(p, m); 224 if(!han) { 225 printf("OPEN FAILED! | CREATING FILE: %s\n", p); 226 han = fopen(p, "w+"); 227 } 228 return han; 229 return fopen(p, m); 230 } 231 void cclose(uint handle) { fclose(handle); } 232 uint cflush(uint handle) { fflush(handle); return handle; } 233 uint cwrite(uint from, uint to, uint count) { 234 int sze = count>>30 ? 4 : 1; 235 fwrite(from, sze, count&0x0FFFFFFF, to); return to; 236 } 237 uint cread(uint from, uint to, uint count) { 238 int sze = count>>30 ? 4 : 1; 239 fread(to, sze, count&0x0FFFFFFF, from); return to; 240 } 241 uint ctell(uint handle) { return ftell(handle); } 242 uint cseek(uint pos, uint handle) { fseek(handle, pos, SEEK_SET); return handle; } 243 uint csize(uint handle) { 244 fseek(handle, 0, SEEK_END); 245 int sze = ftell(handle); 246 fseek(handle, 0, SEEK_SET); 247 return sze; 248 } 249 uint ctimeu(void) { 250 if(!(uint)(glfwGetTime()*1000000)) glfwSetTime(0); 251 return (uint)(glfwGetTime()*1000000); 252 253 } 254 255 256 uint csystem(char* X) { 257 char cmd[32]; cpystr(X, cmd); 258 printf("SYSTEM %s \n", cmd); system(cmd); return X; } 259 260 // GL FNS 261 void glbg(uint color) { 262 float r = ((float)(color>>16))/255.0f; 263 float g = ((float)((color>>8)&0xFF))/255.0f; 264 float b = ((float)(color&0xFF))/255.0f; 265 glClearColor(r, g, b, 1.0f); 266 } 267 268 uint glwindow() { return window; } 269 270 void TEST(uint X, uint Y, uint type, uint packing, uint* data) { 271 272 uint xtype = GL_RGB; 273 uint xpack = GL_UNSIGNED_SHORT_5_6_5; 274 glDrawPixels(X, Y, type, packing, data); 275 int u = 10; 276 } 277 278 279 void cresetmatrix() { 280 float X = ISCALE; 281 //glMatrixMode(GL_PROJECTION); 282 //glLoadIdentity(); 283 //glScalef(X, X, X); 284 glMatrixMode(GL_TEXTURE); 285 glLoadIdentity(); 286 glScalef(X, X, X); 287 glMatrixMode(GL_MODELVIEW); 288 glLoadIdentity(); 289 glScalef(X, X, X); 290 } 291 292 void ctranslate(int z, int y, int x) {glTranslatef((float)x, (float)y, (float)z); } 293 void cscale(int z, int y, int x) { glScalef((float)x, (float)y, (float)z); } 294 void crotate(int deg, int flag) { glRotatef((float)deg/SCALE, flag&0xFF, flag&0xFF00, flag&0xFF0000); } 295 void ctexture(int ifmt, int extfmt, int datatype, int sze, int* data) { 296 int sx = sze&0xFFFF; 297 int sy = (sze>>16)&0xFFFF; 298 int u = (int)GL_RGBA; 299 int t = (int)GL_UNSIGNED_SHORT_5_5_5_1; 300 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, data); 301 glTexImage2D(GL_TEXTURE_2D, 0, ifmt, sx, sy, 0, 302 extfmt, datatype, data); 303 } 304 305 void cortho(int x, int y) { 306 glOrtho( 307 (float)(((short)x>>16)&0xFFFF)/SCALE, 308 (float)((short)x&0xFFFF)/SCALE, 309 (float)(((short)y>>16)&0xFFFF)/SCALE, 310 (float)((short)y&0xFFFF)/SCALE, 311 0.01f, 10000000.0f); 312 } 313 void cpersp(int fov, int aspect) { 314 float f = (float)fov*ISCALE; 315 float a = (float)aspect* ISCALE; 316 gluPerspective(f, a, 0.01f, 10000000.0f); } 317 318 void cuv(int x, int y) { 319 glTexCoord2i(x, y); 320 } 321 322 void cvert(int x, int y, int z) { 323 glVertex3i(x, y, z); 324 } 325 326 void ccolor(int c) { 327 float a = (float)(c&0xFF)/255.0f; 328 c = c>>8; 329 float b = (float)(c&0xFF)/255.0f; 330 float g = (float)((c>>8)&0xFF)/255.0f; 331 float r = (float)((c>>16)&0xFF)/255.0f; 332 glColor4f(r, g, b, a); 333 } 334 void cnormal(int c) { glNormal3b(c&0xFF, (c>>8)&0xFF, (c>>16)&0xFF); } 335 void cscpy(int fmt, int type, int pos, int sze, int* data) { 336 glReadPixels(pos&0xFFFF, (pos>>16)&0xFFFF, sze&0xFFFF, (sze>>16)&0xFFFF, fmt, type, data); 337 } 338 339 int csqrt(int X) { 340 float number = (float)X * ISCALE; 341 int i; 342 float x2, y; 343 const float threehalfs = 1.5F; 344 x2 = number * 0.5F; 345 y = number; 346 i = * ( long * ) &y; 347 i = 0x5f3759df - ( i >> 1 ); 348 y = * ( float * ) &i; 349 y = y * ( threehalfs - ( x2 * y * y ) ); 350 return (int)(y*4096.0f); 351 } 352 353 void lpsze(int x) { 354 if(x < 0) glLineWidth(abs(x)*ISCALE); 355 glPointSize(x*ISCALE); 356 } 357 358 void gldummy() { 359 360 } 361 362 void gltest() { 363 364 } 365 366 void cwinscale(uint x, uint y) { 367 IWIDTH = x; IHEIGHT = y; 368 int W; int H; 369 glfwGetWindowSize(window, &W, &H); 370 glPixelZoom((float)W/(float)IWIDTH, (float)H/(float)IHEIGHT); 371 glViewport(0, 0, IWIDTH, IHEIGHT); 372 dbg_gl("cwinscale(%u,%u)", x, y); 373 } 374 375 // MikMod 376 377 uint sndmodplayfile(char* path) { 378 /* 379 char p[256]; 380 cpystr(path, p); 381 module = Player_Load(p, 64, 0); 382 if(module) { Player_Start(module); return module; } 383 fprintf(stderr, "could not load module: %s \n", 384 MikMod_strerror(MikMod_errno)); 385 */ 386 return 0; 387 } 388 389 uint sndmodplay(char* addr, uint length) { 390 /* 391 module = Player_LoadMem(addr, length, 64, 0); 392 if(module) { modplaying = 1; Player_Start(module); return module; } 393 fprintf(stderr, "could not load module: %s \n", 394 MikMod_strerror(MikMod_errno)); 395 */ 396 return 0; 397 } 398 399 void sndmodflag(uint val, uint flag) { 400 if(!module) return; 401 switch(flag) { 402 case(0): { module->volume = val&0x7F; break; } 403 case(1): { module->wrap = val&1; break; } 404 case(2): { module->loop = val&1; break; } 405 case(3): { module->fadeout = val&1; break; } 406 } 407 } 408 409 void sndmodstop() { 410 if(!module) return; 411 Player_Stop(); Player_Free(module); 412 } 413 414 int sndupdate() { 415 416 } 417 418 uint sndloadsample(char* addr, uint length, uint idx) { 419 /* 420 SAMPLE* sample = samples[idx&3]; 421 if(sample) Sample_Free(sample); 422 sample = Sample_LoadMem(addr, length); 423 samples[idx&3] = sample; 424 if(sample) return sample; 425 fprintf(stderr, "could not load sample: %s \n", 426 MikMod_strerror(MikMod_errno)); 427 */ 428 return 0; 429 } 430 431 void sndplaysample(uint idx) { 432 /* 433 SAMPLE* sample = samples[idx&3]; 434 if(sample) { 435 int voice = Sample_Play(sample, 0, 0); 436 Voice_SetPanning(voice, PAN_CENTER); 437 voices[idx] = voice; 438 } 439 */ 440 } 441 442 uint sndsamplestop(uint idx) { 443 //Sample_Free(samples[idx&3]); 444 } 445 446 void sndsampleflag(uint val, uint prop, uint idx) { 447 /* 448 SAMPLE* sample = samples[idx&3]; 449 switch(prop) { 450 case(0): sample->panning = val; break; 451 case(1): sample->speed = val; break; 452 case(2): sample->volume = val; break; 453 } 454 */ 455 } 456 457 void sound_thread() { 458 /* 459 printf("sound thread active\n"); 460 while(!glfwWindowShouldClose(window)) { 461 MikMod_Update(); 462 usleep(10000); 463 } 464 */ 465 } 466 467 void initsound() { 468 /* 469 MikMod_RegisterAllDrivers(); 470 MikMod_RegisterAllLoaders(); 471 md_mode |= DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX; 472 if(MikMod_Init("")) { 473 fprintf(stderr, "mikmod couldnt start: %s\n", 474 MikMod_strerror(MikMod_errno)); 475 return; 476 } 477 MikMod_SetNumVoices(-1, 4); 478 MikMod_EnableOutput(); 479 pthread_t thread_id; 480 pthread_create(&thread_id, NULL, sound_thread, NULL); 481 */ 482 } 483 484 // threading 485 void* thread_mem[4] = {0, 0, 0, 0}; 486 void thread_wait0() { 487 printf("threads waiting \n"); 488 while(!glfwWindowShouldClose(window)) { 489 if(thread_mem[0]) { 490 void* fn = thread_mem[0]; 491 thread_mem[0] = 0; 492 ((void(*)()) fn)(); 493 } 494 usleep(10000); 495 } 496 } 497 498 //void thread_store(uint fn, uint idx) { thread_mem[idx&3] = fn; } 499 500 501 // MAIN 502 extern uint comp(void); 503 #define INTERN(X) ANS[0] = (uint)X; ANS++ 504 void FORTH() { 505 blkread(); 506 // C intern 30 fns max 507 uint* ANS = &(CDATA)+10; 508 INTERN(&csleep); 509 INTERN(&copen); 510 INTERN(&cclose); 511 INTERN(&cflush); 512 INTERN(&cwrite); 513 INTERN(&cread); 514 INTERN(&ctell); 515 INTERN(&cseek); 516 INTERN(&csize); 517 INTERN(&ctimeu); 518 INTERN(&csystem); 519 520 // GL intern 226 fns max 521 ANS = &(CDATA)+30; 522 INTERN(&glbg); 523 INTERN(&glwindow); 524 INTERN(POLLEVENTS); 525 INTERN(&glfwWindowShouldClose); 526 INTERN(GLF(glClear)); 527 INTERN(GLF(glDrawPixels)); 528 INTERN(&glfwSwapBuffers); 529 INTERN(GLF(glEnable)); 530 INTERN(GLF(glDisable)); 531 INTERN(GLF(glBlendFunc)); 532 INTERN(GLF(glBindTexture)); 533 534 // GLFW hints & window 535 INTERN(&cwinscale); 536 537 // GL 3D 538 ANS = &(CDATA)+80; 539 INTERN(GLF(glBegin)); 540 INTERN(GLF(glEnd)); 541 INTERN(GLF(glGenTextures)); 542 INTERN(&ctexture); 543 INTERN(GLF(glTexParameteri)); 544 INTERN(GLF(glMatrixMode)); 545 INTERN(GLF(glPushMatrix)); 546 INTERN(GLF(glPopMatrix)); 547 INTERN(GLF(glLoadIdentity)); 548 INTERN(&cresetmatrix); 549 INTERN(&ctranslate); 550 INTERN(&crotate); 551 INTERN(&cscale); 552 INTERN(&cortho); 553 INTERN(&cpersp); 554 INTERN(GLF(glVertex3i)); // vert pos 555 INTERN(GLF(glTexCoord2i)); // vert uv 556 INTERN(&ccolor); // vert color 557 INTERN(&cnormal); // vert normal 558 INTERN(&cscpy); 559 INTERN(&csqrt); 560 INTERN(&lpsze); 561 INTERN(&gldummy); 562 563 // Sound Modfiles 564 ANS = &(CDATA)+180; 565 INTERN(&sndmodplay); 566 INTERN(&sndmodplayfile); 567 INTERN(&sndmodflag); 568 INTERN(&sndmodstop); 569 INTERN(&sndupdate); 570 INTERN(&Player_ToggleMute); 571 INTERN(&Player_TogglePause); 572 INTERN(&Player_SetVolume); 573 574 // Sound Samples 575 INTERN(&sndloadsample); 576 INTERN(&sndplaysample); 577 INTERN(&sndsamplestop); 578 INTERN(&sndsampleflag); 579 580 581 582 583 //glEnable(GL_DEPTH_TEST); 584 glClearDepth(1.0f); 585 glEnable(GL_TEXTURE_2D); 586 glShadeModel(GL_SMOOTH); 587 glCullFace(GL_BACK); 588 glPointSize(8.0f); 589 glLineWidth(3.0f); 590 cresetmatrix(); 591 //glColor4f(1.0f, 0, 0, 1.0f); 592 593 // The VM puts its data stack at esp+512 (cf.s: COLD), i.e. in the 512 bytes ABOVE comp's 594 // frame. Without padding that is the callers' frames and, when they are small, the 595 // process's argv/envp, which the VM then overwrites (getenv() in libGL crashed). 596 // Reserve scratch space so that range lies inside it. Must be allocated right here 597 // (alloca) so it sits directly above comp's return address. 598 volatile char* vmpad = (volatile char*)alloca(2048); 599 vmpad[0] = 0; 600 uint* res = comp(); 601 printf("%X\n", res[0]); 602 printf("%X\n", res[1]); 603 printf("%X\n", res[2]); 604 printf("OK\n"); 605 } 606 607 #ifndef _WIN32 608 // crash report (works without gdb/dmesg): written to stderr AND to PC32_crash.log in the 609 // working directory (for the AppImage: the folder next to it) 610 static int g_argc; static char** g_argv; 611 static void crash_report(FILE* o, int sig, siginfo_t* si, ucontext_t* uc) { 612 greg_t* r = uc->uc_mcontext.gregs; 613 unsigned eip = r[REG_EIP], esp = r[REG_ESP]; 614 fprintf(o, "PC32 CRASH: signal %d, fault addr %p, eip %08x, esp %08x (esp&15 = %u)\n", 615 sig, si->si_addr, eip, esp, esp & 15); 616 fprintf(o, "regs: eax %08x ebx %08x ecx %08x edx %08x esi %08x edi %08x ebp %08x\n", 617 (unsigned)r[REG_EAX], (unsigned)r[REG_EBX], (unsigned)r[REG_ECX], (unsigned)r[REG_EDX], 618 (unsigned)r[REG_ESI], (unsigned)r[REG_EDI], (unsigned)r[REG_EBP]); 619 if(eip > 0x1000) { 620 unsigned char* p = (unsigned char*)eip; 621 fprintf(o, "bytes at eip: %02x %02x %02x %02x %02x %02x %02x %02x\n", 622 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 623 } 624 fprintf(o, "argc %d:", g_argc); 625 for(int i = 0; i < g_argc; i++) fprintf(o, " [%s]", g_argv[i]); 626 fprintf(o, "\n"); 627 const char* names[] = { "APPDIR", "APPIMAGE", "OWD", "PC32_DIR", "LIBGL_DRIVERS_PATH", 628 "LIBGL_ALWAYS_SOFTWARE", "DISPLAY", "LD_LIBRARY_PATH", "XDG_SESSION_TYPE", 0 }; 629 for(int i = 0; names[i]; i++) fprintf(o, "env %s=%s\n", names[i], getenv(names[i]) ? getenv(names[i]) : "(unset)"); 630 extern char** environ; int ne = 0; while(environ && environ[ne]) ne++; 631 fprintf(o, "environ entries: %d (environ=%p)\n", ne, (void*)environ); 632 fprintf(o, "stack at esp:"); 633 if(esp > 0x1000) for(int i = 0; i < 32; i++) fprintf(o, "%s%08x", i % 8 ? " " : "\n ", ((unsigned*)esp)[i]); 634 fprintf(o, "\n"); 635 FILE* f = fopen("/proc/self/maps", "r"); 636 char line[512]; 637 fprintf(o, "-- memory map (* = contains eip) --\n"); 638 while(f && fgets(line, sizeof line, f)) { 639 unsigned lo, hi; 640 int hit = sscanf(line, "%x-%x", &lo, &hi) == 2 && eip >= lo && eip < hi; 641 fprintf(o, "%s%s", hit ? "* " : " ", line); 642 } 643 if(f) fclose(f); 644 } 645 646 static void crash_handler(int sig, siginfo_t* si, void* ctx) { 647 fprintf(stderr, "\n"); 648 crash_report(stderr, sig, si, (ucontext_t*)ctx); 649 fflush(stderr); 650 FILE* lf = fopen("PC32_crash.log", "w"); 651 if(lf) { 652 crash_report(lf, sig, si, (ucontext_t*)ctx); 653 fclose(lf); 654 fprintf(stderr, "PC32: crash report written to PC32_crash.log\n"); 655 } else fprintf(stderr, "PC32: could not write PC32_crash.log in the current directory\n"); 656 _exit(139); 657 } 658 659 static void install_crash_handler() { 660 static char altstack[65536]; 661 stack_t ss = { altstack, 0, sizeof altstack }; 662 sigaltstack(&ss, NULL); 663 struct sigaction sa; 664 memset(&sa, 0, sizeof sa); 665 sa.sa_sigaction = crash_handler; 666 sa.sa_flags = SA_SIGINFO | SA_ONSTACK; 667 sigaction(SIGSEGV, &sa, NULL); 668 sigaction(SIGILL, &sa, NULL); 669 sigaction(SIGBUS, &sa, NULL); 670 sigaction(SIGFPE, &sa, NULL); 671 } 672 #endif 673 674 int main(int argc, char** argv) { 675 g_argc = argc; g_argv = argv; 676 #ifdef _WIN32 677 win_startup(); 678 #endif 679 install_crash_handler(); 680 glfwInit(); 681 682 glfwDefaultWindowHints(); 683 if(argc == 1) { 684 #ifndef _WIN32 // Windows: win_fit_window() instead; WS_MAXIMIZE would block resizing 685 glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); 686 #endif 687 glfwWindowHint(GLFW_DECORATED, NULL); 688 } 689 window = glfwCreateWindow(IWIDTH, IHEIGHT, "PC32", NULL, NULL); 690 glfwMakeContextCurrent(window); 691 692 glfwSetCursorPosCallback(window, mousepos_cb); 693 glfwSetMouseButtonCallback(window, mousebot_cb); 694 glfwSetScrollCallback(window, mousescr_cb); 695 glfwSetKeyCallback(window, key_cb); 696 glfwSetCharCallback(window, char_cb); 697 glfwSetWindowSizeCallback(window, resize); 698 #ifdef _WIN32 699 if(argc == 1) win_fit_window(window); // instead of GLFW_MAXIMIZED, see win32_compat.h 700 #endif 701 702 glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_FALSE); 703 704 //sound init 705 initsound(); 706 707 FORTH(); 708 glfwSetWindowShouldClose(window, 1); 709 glfwTerminate(); 710 //MikMod_Exit(); 711 // pthread_join(thread_id, NULL); 712 } 713 714 715 716 717 718 719