main_rpi2.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "minilisp.h"
  5. #include "alloc.h"
  6. #include "reader.h"
  7. #include "compiler_new.h"
  8. #include "devices/debug_util.h"
  9. #include "devices/rpi2/raspi.h"
  10. #include "devices/rpi2/mmu.h"
  11. #include "devices/rpi2/r3d.h"
  12. #include "devices/rpi2/rpi-boot/util.h"
  13. #include "devices/rpi2/uspi/include/uspi.h"
  14. void main();
  15. extern uint32_t _bss_end;
  16. uint8_t* heap_end;
  17. void _cstartup(unsigned int r0, unsigned int r1, unsigned int r2)
  18. {
  19. heap_end = (uint8_t*)0x1000000; // start allocating at 16MB
  20. memset(heap_end,0,1024*1024*16); // clear 16 MB of memory
  21. main();
  22. while(1) {};
  23. }
  24. uint32_t* FB;
  25. char buf[128];
  26. void enable_mmu(void);
  27. extern void* _get_stack_pointer();
  28. void uart_repl();
  29. extern void uspi_keypress_handler(const char *str);
  30. /*
  31. multicore:
  32. > write a physical ARM address to:
  33. > 0x4000008C + 0x10 * core // core := 1..3
  34. */
  35. void main()
  36. {
  37. uart_init(); // gpio setup also affects emmc TODO: document
  38. uart_puts("-- INTERIM/PI kernel_main entered.\r\n");
  39. setbuf(stdout, NULL);
  40. printf("-- init page table… --\r\n");
  41. init_page_table();
  42. printf("-- enable MMU… --\r\n");
  43. mmu_init();
  44. printf("-- MMU enabled. --\r\n");
  45. arm_dmb();
  46. arm_isb();
  47. arm_dsb();
  48. printf("-- enable QPU… -- \r\n");
  49. init_rpi_qpu();
  50. uart_puts("-- QPU enabled.\r\n");
  51. FB = init_rpi_gfx();
  52. sprintf(buf, "-- framebuffer at %p.\r\n",FB);
  53. uart_puts(buf);
  54. sprintf(buf, "-- heap starts at %p.\r\n", heap_end);
  55. uart_puts(buf);
  56. sprintf(buf, "-- stack pointer at %p.\r\n", _get_stack_pointer());
  57. uart_puts(buf);
  58. memset(FB,0xff,1920*1080*2);
  59. // uspi glue
  60. printf("[usb] uspi glue init…\r\n");
  61. extern void uspi_glue_init();
  62. uspi_glue_init();
  63. printf("[usb] uspi initialize…\r\n");
  64. int res = USPiInitialize();
  65. printf("[usb] uspi initialization: %d\r\n", res);
  66. int have_eth = USPiEthernetAvailable();
  67. printf("uu USPI has ethernet: %d\r\n", have_eth);
  68. uart_repl();
  69. }
  70. #include "devices/fbfs.c"
  71. #include "devices/rpi2/fatfs.c"
  72. #include "devices/rpi2/usbkeys.c"
  73. #include "devices/rpi2/usbmouse.c"
  74. #include "devices/rpi2/uartkeys.c"
  75. #include "devices/rpi2/dev_ethernet.c"
  76. #include "devices/rpi2/dev_sound.c"
  77. #include "devices/libc_glue.c"
  78. typedef jit_word_t (*funcptr)();
  79. Cell* platform_eval(Cell* expr); // FIXME
  80. #include "compiler_new.c"
  81. #define CODESZ 8192
  82. #define REPLBUFSZ 1024*6
  83. void fatfs_debug(); // FIXME
  84. Cell* platform_debug() {
  85. }
  86. void uart_repl() {
  87. uart_puts("~~ trying to malloc repl buffers\r\n");
  88. char* out_buf = malloc(REPLBUFSZ);
  89. char* in_line = malloc(REPLBUFSZ);
  90. char* in_buf = malloc(REPLBUFSZ);
  91. sbrk(0);
  92. uart_puts("\r\n\r\n++ welcome to sledge arm/32 (c)2015 mntmn.\r\n");
  93. init_compiler();
  94. mount_fbfs(FB);
  95. int have_kbd = USPiKeyboardAvailable();
  96. printf("[usb] has keyboard: %d\r\n", have_kbd);
  97. if (have_kbd) {
  98. USPiKeyboardRegisterKeyPressedHandler(uspi_keypress_handler);
  99. mount_usbkeys();
  100. } else {
  101. mount_uartkeys();
  102. }
  103. mount_fatfs();
  104. mount_ethernet();
  105. if (USPiMouseAvailable()) {
  106. USPiMouseRegisterStatusHandler(uspi_mouse_handler);
  107. mount_mouse();
  108. }
  109. mount_soundfs();
  110. fatfs_debug();
  111. uart_puts("\r\n~~ fs initialized.\r\n");
  112. memset(out_buf,0,REPLBUFSZ);
  113. memset(in_line,0,REPLBUFSZ);
  114. memset(in_buf,0,REPLBUFSZ);
  115. int in_offset = 0;
  116. int parens = 0;
  117. Cell* expr;
  118. char c = 0;
  119. strcpy(in_line,"(eval (read (recv (open \"/sd/shell.l\"))))\n");
  120. c=13;
  121. while (1) {
  122. expr = NULL;
  123. uart_puts("sledge> ");
  124. int i = 0;
  125. while (c!=13 && i<(REPLBUFSZ-1)) {
  126. c = uart_getc();
  127. uart_putc(c);
  128. in_line[i++] = c;
  129. in_line[i] = 0;
  130. }
  131. c = 0;
  132. int len = strnlen(in_line,REPLBUFSZ);
  133. // recognize parens
  134. for (i=0; i<len; i++) {
  135. if (in_line[i] == '(') {
  136. parens++;
  137. } else if (in_line[i] == ')') {
  138. parens--;
  139. }
  140. }
  141. if (len>1) {
  142. strncpy(in_buf+in_offset, in_line, len-1);
  143. in_buf[in_offset+len-1] = 0;
  144. }
  145. printf("\r\n[%s]\r\n",in_buf);
  146. if (parens>0) {
  147. printf("\r\n...\r\n");
  148. in_offset+=len-1;
  149. } else {
  150. if (len>1) {
  151. expr = read_string(in_buf);
  152. in_offset=0;
  153. }
  154. }
  155. if (expr) {
  156. Cell* res = platform_eval(alloc_cons(expr, NULL));
  157. if (!res) {
  158. uart_puts("null\n");
  159. } else {
  160. lisp_write(res, out_buf, REPLBUFSZ);
  161. uart_puts(out_buf);
  162. }
  163. uart_puts("\r\n");
  164. }
  165. }
  166. }
  167. Cell* platform_eval(Cell* expr) {
  168. if (!expr || expr->tag!=TAG_CONS) {
  169. printf("[platform_eval] error: no expr given.\r\n");
  170. return NULL;
  171. }
  172. char eval_buf[512];
  173. int i = 0;
  174. Cell* res = alloc_nil();
  175. Cell* c;
  176. while ((c = car(expr))) {
  177. i++;
  178. arm_dmb();
  179. arm_isb();
  180. arm_dsb();
  181. lisp_write(c, eval_buf, 512);
  182. printf("<- compiling %d: %s\r\n",i,eval_buf);
  183. code = malloc(CODESZ);
  184. memset(code, 0, CODESZ);
  185. jit_init(0x400);
  186. register void* sp asm ("sp"); // FIXME maybe unportable
  187. Frame empty_frame = {NULL, 0, 0, sp};
  188. int tag = compile_expr(c, &empty_frame, TAG_ANY);
  189. arm_dmb();
  190. arm_isb();
  191. arm_dsb();
  192. printf("compiled %d\r\n",i);
  193. if (tag) {
  194. jit_ret();
  195. funcptr fn = (funcptr)code;
  196. //printf("~~ fn at %p\r\n",fn);
  197. __asm("stmfd sp!, {r3-r12, lr}");
  198. (Cell*)fn();
  199. __asm("ldmfd sp!, {r3-r12, lr}");
  200. register Cell *retval asm ("r6");
  201. __asm("mov r6,r0");
  202. res = retval;
  203. arm_dmb();
  204. arm_isb();
  205. arm_dsb();
  206. printf("~~ expr %d res: %p\r\n",i,res);
  207. lisp_write(res, eval_buf, 512);
  208. printf("~> %s\r\n",eval_buf);
  209. } else {
  210. lisp_write(expr, eval_buf, 512);
  211. printf("[platform_eval] stopped at expression %d: '%s'\r\n",i,eval_buf);
  212. break;
  213. }
  214. // when to free the code? -> when no bound lambdas involved
  215. expr = cdr(expr);
  216. }
  217. return res;
  218. }