rpi_sdl_test.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #include "jit_arm_raw.c"
  5. #include <stdint.h>
  6. #include <sys/stat.h>
  7. #include <stdlib.h>
  8. #include <sys/mman.h>
  9. #include "SDL/SDL.h"
  10. #define CODESZ 1024*4
  11. typedef uint32_t (*funcptr)();
  12. void hello() {
  13. printf("hello!\n");
  14. }
  15. SDL_Surface* screen;
  16. void setup_sdl() {
  17. SDL_Init(SDL_INIT_VIDEO);
  18. screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  19. }
  20. int main() {
  21. setup_sdl();
  22. code = mmap(0, CODESZ, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
  23. memset(code, 0, CODESZ);
  24. cpool_idx = 128; // 128 ops gap
  25. uint32_t* fb = (uint32_t*)screen->pixels;
  26. jit_movi(1,0);
  27. jit_movi(5,0);
  28. jit_movi(4,4);
  29. jit_movi(6,1);
  30. jit_movi(7,640*480);
  31. jit_movi(2,0xff00ff);
  32. jit_lea(3,fb);
  33. jit_label("loop");
  34. jit_strw(2);
  35. jit_addr(3,4);
  36. jit_addr(5,6);
  37. jit_addr(2,6);
  38. jit_cmpr(5,7);
  39. jit_jne("loop");
  40. jit_ret();
  41. jit_ret();
  42. FILE* f = fopen("/tmp/test","w");
  43. fwrite(code, CODESZ, 1, f);
  44. fclose(f);
  45. int mp_res = mprotect(code, CODESZ, PROT_EXEC|PROT_READ);
  46. funcptr fn = (funcptr)code;
  47. uint32_t res = fn();
  48. printf("asm result: %lx\n",res);
  49. SDL_UpdateRect(screen, 0, 0, 640, 480);
  50. printf("done\n");
  51. char* foo=malloc(64);
  52. gets(foo);
  53. //free(code);
  54. //munmap(code);
  55. }