main_imx233.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "devices/imx233/imx233.h"
  5. //#include "sledge/machine.h"
  6. #include "sledge/minilisp.h"
  7. #include "sledge/alloc.h"
  8. #include "sledge/compiler_new.h"
  9. #include "os/debug_util.h"
  10. //#include "sledge/blit.h"
  11. #define COLOR_TYPE uint8_t
  12. #define SCREEN_W 640
  13. #define SCREEN_H 480
  14. #define SCREEN_BPP 1
  15. void main();
  16. extern uint32_t _bss_end;
  17. uint8_t* heap_end;
  18. void _cstartup(unsigned int r0, unsigned int r1, unsigned int r2)
  19. {
  20. int i;
  21. for (i=0;i<2000;i++) {
  22. while (*((volatile uint32_t*)(0x80070018)) & 32) {}; // UART_PL01x_FR_TXFF
  23. *((volatile uint32_t*)0x80070000) = '@';
  24. }
  25. uart_puts("[interim] _cstartup\n");
  26. heap_end = (uint8_t*)0x42300000; // 0x300000 reserved for kernel binary + stack
  27. //memset(heap_end,0,1024*1024*16); // clear 16 MB of memory
  28. main();
  29. while(1) {};
  30. }
  31. // GPIO Register set
  32. volatile unsigned int* gpio;
  33. COLOR_TYPE* FB;
  34. COLOR_TYPE* FB_MEM;
  35. char buf[128];
  36. void enable_mmu(void);
  37. extern void* _get_stack_pointer();
  38. void uart_repl();
  39. //extern void libfs_init();
  40. //extern void uspi_keypress_handler(const char *str);
  41. static int have_eth = 0;
  42. uint8_t* eth_rx_buffer;
  43. void init_mini_ip(Cell* buffer_cell);
  44. void main()
  45. {
  46. //enable_mmu();
  47. //arm_invalidate_data_caches();
  48. //uart_init(); // gpio setup also affects emmc TODO: document
  49. uart_puts("-- INTERIM/IMX233 kernel_main entered.\r\n");
  50. setbuf(stdout, NULL);
  51. //libfs_init();
  52. //init_rpi_qpu();
  53. //uart_puts("-- QPU enabled.\r\n");
  54. FB = (COLOR_TYPE*)0x40000000;
  55. FB_MEM = FB;
  56. //init_blitter(FB);
  57. sprintf(buf, "-- framebuffer at %p.\r\n",FB);
  58. uart_puts(buf);
  59. sprintf(buf, "-- heap starts at %p.\r\n", heap_end);
  60. uart_puts(buf);
  61. sprintf(buf, "-- stack pointer at %p.\r\n", _get_stack_pointer());
  62. uart_puts(buf);
  63. memset(FB, 0x88, SCREEN_W*SCREEN_H*SCREEN_BPP);
  64. //eth_rx_buffer=malloc(64*1024);
  65. uart_repl();
  66. }
  67. #include <os/libc_glue.c>
  68. int machine_video_set_pixel(uint32_t x, uint32_t y, COLOR_TYPE color) {
  69. if (x>=SCREEN_W || y>=SCREEN_H) return 0;
  70. FB_MEM[y*1920+x] = color;
  71. return 0;
  72. }
  73. int machine_video_rect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, COLOR_TYPE color) {
  74. uint32_t y1=y;
  75. uint32_t y2=y+h;
  76. uint32_t x2=x+w;
  77. uint32_t off = y1*SCREEN_W;
  78. // FIXME: clip!
  79. for (; y1<y2; y1++) {
  80. for (uint32_t x1=x; x1<x2; x1++) {
  81. FB_MEM[off+x1] = color;
  82. }
  83. off+=SCREEN_W;
  84. }
  85. return 0;
  86. }
  87. int machine_get_key(int modifiers) {
  88. if (modifiers) return 0;
  89. int k = 0;
  90. k = uart_getc();
  91. if (k==27) {
  92. k = uart_getc();
  93. if (k==91) {
  94. k = uart_getc();
  95. if (k==27) {
  96. // fast repetition
  97. k = uart_getc();
  98. k = uart_getc();
  99. }
  100. if (k==68) return 130;
  101. if (k==67) return 131;
  102. if (k==65) return 132;
  103. if (k==66) return 133;
  104. printf("~~ inkey unknown sequence: 91,%d\r\n",k);
  105. return 0;
  106. }
  107. }
  108. return k;
  109. }
  110. #include "sledge/compiler_new.c"
  111. void uart_repl() {
  112. uart_puts("~~ trying to malloc repl buffers\r\n");
  113. char* out_buf = malloc(1024*10);
  114. char* in_line = malloc(1024*2);
  115. char* in_buf = malloc(1024*10);
  116. uart_puts("\r\n\r\n++ welcome to sledge arm/32 (c)2015 mntmn.\r\n");
  117. init_compiler();
  118. uart_puts("\r\n~~ compiler initialized.\r\n");
  119. memset(out_buf,0,1024*10);
  120. memset(in_line,0,1024*2);
  121. memset(in_buf,0,1024*10);
  122. long count = 0;
  123. int fullscreen = 0;
  124. int in_offset = 0;
  125. int parens = 0;
  126. int linec = 0;
  127. Cell* expr;
  128. char c = 0; //13;
  129. //strcpy(in_line,"(eval (load \"/sd/boot.l\"))\n");
  130. while (1) {
  131. expr = NULL;
  132. uart_puts("sledge> ");
  133. int i = 0;
  134. while (c!=13) {
  135. c = uart_getc();
  136. uart_putc(c);
  137. in_line[i++] = c;
  138. in_line[i] = 0;
  139. }
  140. c = 0;
  141. int len = strlen(in_line);
  142. // recognize parens
  143. for (i=0; i<len; i++) {
  144. if (in_line[i] == '(') {
  145. parens++;
  146. } else if (in_line[i] == ')') {
  147. parens--;
  148. }
  149. }
  150. //printf("parens: %d in_offset: %d\n",parens,in_offset);
  151. if (len>1) {
  152. strncpy(in_buf+in_offset, in_line, len-1);
  153. in_buf[in_offset+len-1] = 0;
  154. //printf("line: '%s' (%d)\n",in_buf,strlen(in_buf));
  155. linec++;
  156. //if (linec>10) while (1) {};
  157. }
  158. printf("\r\n[%s]\r\n",in_buf);
  159. if (parens>0) {
  160. printf("\r\n...\r\n");
  161. in_offset+=len-1;
  162. } else {
  163. if (len>1) {
  164. expr = read_string(in_buf);
  165. in_offset=0;
  166. }
  167. }
  168. //printf("parens: %d offset: %d\n",parens,in_offset);
  169. //funcptr compiled;
  170. if (expr) {
  171. //jit_prolog();
  172. int success = 0; //compile_arg(JIT_R0, expr, TAG_ANY);
  173. if (success) {
  174. funcptr compiled = NULL; //jit_emit();
  175. //jit_disassemble();
  176. //start_clock();
  177. /*uint32_t compiled[] = {
  178. 0xe3a03005, // mov r3, #5
  179. 0xe1a00003, // mov r0, r3
  180. 0xe12fff1e // bx lr
  181. };*/
  182. printf("-- compiled: %p\r\n",compiled);
  183. memdump(compiled,200,1);
  184. printf("-- jumping to code…");
  185. //Cell* res = expr;
  186. Cell* res = (Cell*)((funcptr)compiled)();
  187. printf("-- res at: %p\r\n",res);
  188. // TODO: move to write op
  189. if (!res || res<(Cell*)heap_end) {
  190. uart_puts("null\n");
  191. } else {
  192. lisp_write(res, out_buf, 1024*10);
  193. uart_puts(out_buf);
  194. }
  195. }
  196. uart_puts("\r\n");
  197. }
  198. }
  199. }