main_imx233.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "imx233/imx233.h"
  5. #include "sledge/machine.h"
  6. #include "sledge/minilisp.h"
  7. #include "sledge/alloc.h"
  8. #include "sledge/blit.h"
  9. #include <lightning.h>
  10. void main();
  11. extern uint32_t _bss_end;
  12. uint8_t* heap_end;
  13. void _cstartup(unsigned int r0, unsigned int r1, unsigned int r2)
  14. {
  15. int i;
  16. for (i=0;i<2000;i++) {
  17. while (*((volatile uint32_t*)(0x80070018)) & 32) {}; // UART_PL01x_FR_TXFF
  18. *((volatile uint32_t*)0x80070000) = '@';
  19. }
  20. uart_puts("[interim] _cstartup\n");
  21. heap_end = (uint8_t*)0x42300000; // 0x300000 reserved for kernel binary + stack
  22. //memset(heap_end,0,1024*1024*16); // clear 16 MB of memory
  23. main();
  24. while(1) {};
  25. }
  26. // GPIO Register set
  27. volatile unsigned int* gpio;
  28. COLOR_TYPE* FB;
  29. COLOR_TYPE* FB_MEM;
  30. char buf[128];
  31. void enable_mmu(void);
  32. extern void* _get_stack_pointer();
  33. void uart_repl();
  34. //extern void libfs_init();
  35. //extern void uspi_keypress_handler(const char *str);
  36. static int have_eth = 0;
  37. uint8_t* eth_rx_buffer;
  38. void init_mini_ip(Cell* buffer_cell);
  39. void main()
  40. {
  41. //enable_mmu();
  42. //arm_invalidate_data_caches();
  43. //uart_init(); // gpio setup also affects emmc TODO: document
  44. uart_puts("-- BOMBERJACKET/IMX233 kernel_main entered.\r\n");
  45. setbuf(stdout, NULL);
  46. //libfs_init();
  47. //init_rpi_qpu();
  48. //uart_puts("-- QPU enabled.\r\n");
  49. FB = (COLOR_TYPE*)0x40000000;
  50. FB_MEM = FB;
  51. init_blitter(FB);
  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, 0x88, SCREEN_W*SCREEN_H*SCREEN_BPP);
  59. //eth_rx_buffer=malloc(64*1024);
  60. uart_repl();
  61. }
  62. //static struct fs* fat_fs;
  63. /*void vfs_register(struct fs *fs) {
  64. printf("~~ vfs_register: %s/%s block_size: %d\r\n",fs->parent->device_name,fs->fs_name,fs->block_size);
  65. printf("~~ read_directory: %p fopen: %p\r\n",fs->read_directory,fs->fopen);
  66. //char* name = "/";
  67. //struct dirent* dir = fs->read_directory(fs,&name);
  68. //printf("~~ dirent: %p name: %s\r\n",dir,dir->name);
  69. fat_fs = fs;
  70. }*/
  71. void printhex(uint32_t num) {
  72. char buf[9];
  73. buf[8] = 0;
  74. for (int i=7; i>=0; i--) {
  75. int d = num&0xf;
  76. if (d<10) buf[i]='0'+d;
  77. else buf[i]='a'+d-10;
  78. num=num>>4;
  79. }
  80. uart_puts(buf);
  81. }
  82. void printhex_signed(int32_t num) {
  83. char buf[9];
  84. buf[8] = 0;
  85. if (num<0) {
  86. uart_putc('-');
  87. num=-num;
  88. }
  89. for (int i=7; i>=0; i--) {
  90. int d = num&0xf;
  91. if (d<10) buf[i]='0'+d;
  92. else buf[i]='a'+d-10;
  93. num=num/16;
  94. }
  95. uart_puts(buf);
  96. }
  97. #include "libc_glue.c"
  98. int machine_video_set_pixel(uint32_t x, uint32_t y, COLOR_TYPE color) {
  99. if (x>=SCREEN_W || y>=SCREEN_H) return 0;
  100. FB_MEM[y*1920+x] = color;
  101. return 0;
  102. }
  103. int machine_video_rect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, COLOR_TYPE color) {
  104. uint32_t y1=y;
  105. uint32_t y2=y+h;
  106. uint32_t x2=x+w;
  107. uint32_t off = y1*SCREEN_W;
  108. // FIXME: clip!
  109. for (; y1<y2; y1++) {
  110. for (uint32_t x1=x; x1<x2; x1++) {
  111. FB_MEM[off+x1] = color;
  112. }
  113. off+=SCREEN_W;
  114. }
  115. return 0;
  116. }
  117. Cell* lookup_global_symbol(char* name);
  118. void memdump(jit_word_t start,uint32_t len,int raw);
  119. int ethernet_rx(uint8_t* packet) {
  120. int frame_len = 0;
  121. if (have_eth) {
  122. //USPiReceiveFrame(packet, &frame_len);
  123. if (frame_len) {
  124. printf("[eth] frame received! len: %d\r\n",frame_len);
  125. memdump((uint32_t)packet,frame_len,0);
  126. }
  127. }
  128. return frame_len;
  129. }
  130. void ethernet_tx(uint8_t* packet, int len) {
  131. //USPiSendFrame(packet, len);
  132. printf("[eth] frame sent (%d)\r\n",len);
  133. }
  134. int machine_video_flip() {
  135. //memset(FB_MEM, 0xffffff, 1920*1080*4);
  136. return 0;
  137. }
  138. static char usb_key_in = 0;
  139. static int usb_keyboard_enabled = 0;
  140. int machine_get_key(int modifiers) {
  141. if (modifiers) return 0;
  142. int k = 0;
  143. if (usb_keyboard_enabled) {
  144. k = usb_key_in;
  145. usb_key_in = 0;
  146. } else {
  147. k = uart_getc();
  148. if (k==27) {
  149. k = uart_getc();
  150. if (k==91) {
  151. k = uart_getc();
  152. if (k==27) {
  153. // fast repetition
  154. k = uart_getc();
  155. k = uart_getc();
  156. }
  157. if (k==68) return 130;
  158. if (k==67) return 131;
  159. if (k==65) return 132;
  160. if (k==66) return 133;
  161. printf("~~ inkey unknown sequence: 91,%d\r\n",k);
  162. return 0;
  163. }
  164. }
  165. }
  166. return k;
  167. }
  168. Cell* machine_save_file(Cell* cell, char* path) {
  169. return alloc_int(0);
  170. }
  171. static char sysfs_tmp[1024];
  172. Cell* machine_load_file(char* path) {
  173. // sysfs
  174. if (!strcmp(path,"/sys/mem")) {
  175. MemStats* mst = alloc_stats();
  176. sprintf(sysfs_tmp, "(%d %d %d %d)", mst->byte_heap_used, mst->byte_heap_max, mst->cells_used, mst->cells_max);
  177. return read_string(sysfs_tmp);
  178. }
  179. Cell* result_cell = alloc_int(0);
  180. return result_cell;
  181. }
  182. typedef jit_word_t (*funcptr)();
  183. static jit_state_t *_jit;
  184. static jit_state_t *_jit_saved;
  185. static void *stack_ptr, *stack_base;
  186. #include "sledge/compiler.c"
  187. void insert_rootfs_symbols() {
  188. // until we have a file system, inject binaries that are compiled in the kernel
  189. // into the environment
  190. /*extern uint8_t _binary_bjos_rootfs_unifont_start;
  191. extern uint32_t _binary_bjos_rootfs_unifont_size;
  192. Cell* unif = alloc_bytes(16);
  193. unif->addr = &_binary_bjos_rootfs_unifont_start;
  194. unif->size = _binary_bjos_rootfs_unifont_size;
  195. printf("~~ unifont is at %p\r\n",unif->addr);
  196. extern uint8_t* blitter_speedtest(uint8_t* font);
  197. unif->addr = blitter_speedtest(unif->addr);
  198. insert_symbol(alloc_sym("unifont"), unif, &global_env);
  199. */
  200. /*extern uint8_t _binary_bjos_rootfs_editor_l_start;
  201. extern uint32_t _binary_bjos_rootfs_editor_l_size;
  202. Cell* editor = alloc_string("boot");
  203. editor->addr = &_binary_bjos_rootfs_editor_l_start;
  204. editor->size = read_word((uint8_t*)&_binary_bjos_rootfs_editor_l_size,0); //_binary_bjos_rootfs_editor_l_size;
  205. printf("~~ editor-source is at %p, size %d\r\n",editor->addr,editor->size);
  206. insert_symbol(alloc_sym("editor-source"), editor, &global_env);*/
  207. //Cell* boot = alloc_string("(eval (load \"/sd/boot.l\"))");
  208. //insert_symbol(alloc_sym("boot-source"), boot, &global_env);
  209. //Cell* udp_cell = alloc_num_bytes(65535);
  210. //insert_symbol(alloc_sym("network-input"), udp_cell, &global_env);
  211. //init_mini_ip(udp_cell);
  212. }
  213. void uart_repl() {
  214. uart_puts("~~ trying to malloc repl buffers\r\n");
  215. char* out_buf = malloc(1024*10);
  216. char* in_line = malloc(1024*2);
  217. char* in_buf = malloc(1024*10);
  218. uart_puts("\r\n\r\n++ welcome to sledge arm/32 (c)2015 mntmn.\r\n");
  219. init_compiler();
  220. insert_rootfs_symbols();
  221. uart_puts("\r\n~~ compiler initialized.\r\n");
  222. memset(out_buf,0,1024*10);
  223. memset(in_line,0,1024*2);
  224. memset(in_buf,0,1024*10);
  225. // jit stack
  226. stack_ptr = stack_base = malloc(4096 * sizeof(jit_word_t));
  227. long count = 0;
  228. int fullscreen = 0;
  229. int in_offset = 0;
  230. int parens = 0;
  231. int linec = 0;
  232. Cell* expr;
  233. char c = 13;
  234. //strcpy(in_line,"(eval (load \"/sd/boot.l\"))\n");
  235. init_jit(NULL);
  236. uart_puts("\r\n\r\n~~ JIT initialized.\r\n");
  237. while (1) {
  238. expr = NULL;
  239. uart_puts("sledge> ");
  240. int i = 0;
  241. while (c!=13) {
  242. c = uart_getc();
  243. uart_putc(c);
  244. in_line[i++] = c;
  245. in_line[i] = 0;
  246. }
  247. c = 0;
  248. int len = strlen(in_line);
  249. // recognize parens
  250. for (i=0; i<len; i++) {
  251. if (in_line[i] == '(') {
  252. parens++;
  253. } else if (in_line[i] == ')') {
  254. parens--;
  255. }
  256. }
  257. //printf("parens: %d in_offset: %d\n",parens,in_offset);
  258. if (len>1) {
  259. strncpy(in_buf+in_offset, in_line, len-1);
  260. in_buf[in_offset+len-1] = 0;
  261. //printf("line: '%s' (%d)\n",in_buf,strlen(in_buf));
  262. linec++;
  263. //if (linec>10) while (1) {};
  264. }
  265. printf("\r\n[%s]\r\n",in_buf);
  266. if (parens>0) {
  267. printf("\r\n...\r\n");
  268. in_offset+=len-1;
  269. } else {
  270. if (len>1) {
  271. expr = read_string(in_buf);
  272. in_offset=0;
  273. }
  274. }
  275. //printf("parens: %d offset: %d\n",parens,in_offset);
  276. jit_node_t *in;
  277. //funcptr compiled;
  278. if (expr) {
  279. _jit = jit_new_state();
  280. //jit_prolog();
  281. int success = compile_arg(JIT_R0, expr, TAG_ANY);
  282. //jit_movi(JIT_R0, 0);
  283. jit_retr(JIT_R0);
  284. //int success = 1;
  285. if (success) {
  286. funcptr compiled = jit_emit();
  287. //jit_disassemble();
  288. //start_clock();
  289. /*uint32_t compiled[] = {
  290. 0xe3a03005, // mov r3, #5
  291. 0xe1a00003, // mov r0, r3
  292. 0xe12fff1e // bx lr
  293. };*/
  294. printf("-- compiled: %p\r\n",compiled);
  295. memdump((uint32_t)compiled,200,1);
  296. printf("-- jumping to code…");
  297. //Cell* res = expr;
  298. Cell* res = (Cell*)((funcptr)compiled)();
  299. printf("-- res at: %p\r\n",res);
  300. // TODO: move to write op
  301. if (!res || res<heap_end) {
  302. uart_puts("null\n");
  303. } else {
  304. lisp_write(res, out_buf, 1024*10);
  305. uart_puts(out_buf);
  306. }
  307. }
  308. uart_puts("\r\n");
  309. jit_clear_state();
  310. jit_destroy_state();
  311. }
  312. }
  313. }