[317] | 1 | /* #[<PREAMBLE>]#
|
---|
| 2 | * #[<...>]# から #[</...>]# で囲まれたコメントは編集しないでください
|
---|
| 3 | * tecsmerge によるマージに使用されます
|
---|
| 4 | * 呼び口関数 #_TCPF_#
|
---|
| 5 | * call port : cSerialPort signature: sSerialPort context: task
|
---|
| 6 | * ER cSerialPort_open( );
|
---|
| 7 | * ER cSerialPort_close( );
|
---|
| 8 | * ER_UINT cSerialPort_read( char_t* buffer, uint_t length );
|
---|
| 9 | * ER_UINT cSerialPort_write( const char_t* buffer, uint_t length );
|
---|
| 10 | * ER cSerialPort_control( uint_t ioControl );
|
---|
| 11 | * ER cSerialPort_refer( T_SERIAL_RPOR* pk_rpor );
|
---|
| 12 | *
|
---|
| 13 | * #[</PREAMBLE>]# */
|
---|
| 14 |
|
---|
| 15 | /* プロトタイプ宣言や変数の定義をここに書きます #_PAC_# */
|
---|
| 16 | #include "nMruby_tMruby_tecsgen.h"
|
---|
| 17 | #include <string.h>
|
---|
| 18 |
|
---|
| 19 | #include <mruby.h>
|
---|
| 20 | #include <mruby/proc.h>
|
---|
| 21 | #include <mruby/data.h>
|
---|
| 22 | #include <mruby/compile.h>
|
---|
| 23 |
|
---|
| 24 | #ifndef E_OK
|
---|
| 25 | #define E_OK 0 /* success */
|
---|
| 26 | #define E_ID (-18) /* illegal ID */
|
---|
| 27 | #endif
|
---|
| 28 |
|
---|
| 29 | #define MEM_SIZE 1024*1024 // 1MB
|
---|
| 30 | //#define VM_EVAL
|
---|
| 31 | //#define MALLOC_EVAL
|
---|
| 32 |
|
---|
| 33 | #include <t_syslog.h>
|
---|
| 34 |
|
---|
| 35 | void mrb_init_mrb(CELLCB *p_cellcb, mrb_state *mrb);
|
---|
| 36 | /* 受け口関数 #_TEPF_# */
|
---|
| 37 | /* #[<ENTRY_PORT>]# eMrubyBody
|
---|
| 38 | * entry port: eMrubyBody
|
---|
| 39 | * signature: sMrubyBody
|
---|
| 40 | * context: task
|
---|
| 41 | * #[</ENTRY_PORT>]# */
|
---|
| 42 |
|
---|
| 43 | /* #[<ENTRY_FUNC>]# eMrubyBody_main
|
---|
| 44 | * name: eMrubyBody_main
|
---|
| 45 | * global_name: nMruby_tMruby_eMrubyBody_main
|
---|
| 46 | * oneway: false
|
---|
| 47 | * #[</ENTRY_FUNC>]# */
|
---|
| 48 | void
|
---|
| 49 | eMrubyBody_main(CELLIDX idx)
|
---|
| 50 | {
|
---|
| 51 | #ifdef VM_EVAL
|
---|
| 52 | SYSUTM tstart, tend;
|
---|
| 53 | #endif
|
---|
| 54 |
|
---|
| 55 | CELLCB *p_cellcb;
|
---|
| 56 | mrb_state *mrb;
|
---|
| 57 |
|
---|
| 58 | if (VALID_IDX(idx)) {
|
---|
| 59 | p_cellcb = GET_CELLCB(idx);
|
---|
| 60 | }
|
---|
| 61 | #ifdef VM_EVAL
|
---|
| 62 | get_utm(&tstart);
|
---|
| 63 | #endif
|
---|
| 64 |
|
---|
| 65 | /* new interpreter instance */
|
---|
| 66 | mrb = mrb_open();
|
---|
| 67 | if (mrb == NULL) {
|
---|
| 68 | syslog(LOG_EMERG, "Invalid mrb_state, exiting test driver");
|
---|
| 69 | // cSerialPort_write("Invalid mrb_state, exiting test driver\n", 39);
|
---|
| 70 | return;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | #ifdef VM_EVAL
|
---|
| 74 | get_utm(&tend);
|
---|
| 75 | syslog(LOG_EMERG, "mrb_open time = %d micro sec", tend - tstart);
|
---|
| 76 | #endif
|
---|
| 77 | if(is_cInit_joined()){
|
---|
| 78 | // TECS Bridge
|
---|
| 79 | cInit_initializeBridge( mrb );
|
---|
| 80 | }
|
---|
| 81 | mrb_init_mrb(p_cellcb, mrb);
|
---|
| 82 | syslog(LOG_EMERG, "end of mruby program");
|
---|
| 83 | #ifdef VM_EVAL
|
---|
| 84 | get_utm(&tstart);
|
---|
| 85 | #endif
|
---|
| 86 | mrb_close(mrb);
|
---|
| 87 | #ifdef VM_EVAL
|
---|
| 88 | get_utm(&tend);
|
---|
| 89 | syslog(LOG_EMERG, "mrb_close time = %d micro sec", tend - tstart);
|
---|
| 90 | #endif
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | /* #[<POSTAMBLE>]#
|
---|
| 95 | * これより下に非受け口関数を書きます
|
---|
| 96 | * #[</POSTAMBLE>]#*/
|
---|
| 97 |
|
---|
| 98 | void
|
---|
| 99 | mrb_init_mrb(CELLCB *p_cellcb, mrb_state *mrb)
|
---|
| 100 | {
|
---|
| 101 | mrb_irep *irep = mrb_read_irep(mrb, ATTR_irep);
|
---|
| 102 |
|
---|
| 103 | mrb_run(mrb, mrb_proc_new(mrb, irep), mrb_top_self(mrb));
|
---|
| 104 | if (mrb->exc) {
|
---|
| 105 | mrb_p(mrb, mrb_obj_value(mrb->exc));
|
---|
| 106 | exit(0);
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /*
|
---|
| 111 | extern void debug_print(char * str);
|
---|
| 112 | extern void debug_print_address(char * str, int address);
|
---|
| 113 | extern void debug_print_int(char * str, int data);
|
---|
| 114 | #include <errno.h>
|
---|
| 115 | void *
|
---|
| 116 | _sbrk(size_t incr)
|
---|
| 117 | {
|
---|
| 118 | // TODO: 正しいヒープサイズを指定すること (EV3はRAM 64MB)
|
---|
| 119 | extern char __heap_start;
|
---|
| 120 | static uint32_t cs3_heap_end = &__heap_start + MEM_SIZE; // ヒープの最後アドレス
|
---|
| 121 | static char *heap_end = &__heap_start;
|
---|
| 122 | char *prev_heap_end;
|
---|
| 123 | uint16_t incr2;
|
---|
| 124 |
|
---|
| 125 | char str[20];
|
---|
| 126 |
|
---|
| 127 | #ifdef MALLOC_EVAL
|
---|
| 128 | syslog(LOG_EMERG, "heap_start = %x", &__heap_start);
|
---|
| 129 | #endif
|
---|
| 130 |
|
---|
| 131 | prev_heap_end = heap_end;
|
---|
| 132 | incr2 = (uint16_t)incr;
|
---|
| 133 |
|
---|
| 134 | if (heap_end + incr2 > cs3_heap_end) {
|
---|
| 135 | errno = ENOMEM;
|
---|
| 136 | return (void *)-1;
|
---|
| 137 | }
|
---|
| 138 | heap_end += incr2;
|
---|
| 139 |
|
---|
| 140 | #ifdef MALLOC_EVAL
|
---|
| 141 | syslog(LOG_EMERG, "heap_end = %x", heap_end);
|
---|
| 142 | #endif
|
---|
| 143 |
|
---|
| 144 | #ifdef DEBUG_MALLOC
|
---|
| 145 | debug_print_int("incr",incr);
|
---|
| 146 | debug_print_address("prev_heap_end",(int) prev_heap_end);
|
---|
| 147 | dly_tsk(50);
|
---|
| 148 | #endif
|
---|
| 149 | return (void *) prev_heap_end;
|
---|
| 150 | }
|
---|
| 151 | */
|
---|
| 152 | extern intptr_t bt_snd_chr(intptr_t c);
|
---|
| 153 | extern intptr_t uart_snd_chr(intptr_t c) ;
|
---|
| 154 | extern void initialize_lcd_dri();
|
---|
| 155 | #define UART1 (*(volatile struct st_uart *)0x01D0C000)
|
---|
| 156 | size_t fwrite(const void *buf, size_t size, size_t n, FILE *fp){
|
---|
| 157 | intptr_t intp;
|
---|
| 158 | char *cp;
|
---|
| 159 | cp = (char*)buf;
|
---|
| 160 | /* シリアルポートが結合されている場合のみ呼び出す
|
---|
| 161 | if(is_cSerialPort_joined()){
|
---|
| 162 | //cSerialPort_write(buf, size);
|
---|
| 163 |
|
---|
| 164 | }*/
|
---|
| 165 |
|
---|
| 166 | for(int i = 0;i < size; i++){
|
---|
| 167 | intp = *cp++;
|
---|
| 168 | if (intp == '\n') {
|
---|
| 169 | bt_snd_chr('\r');
|
---|
| 170 | while(!uart_send(&UART1, '\r'));
|
---|
| 171 | }
|
---|
| 172 | bt_snd_chr(intp);
|
---|
| 173 | while(!uart_send(&UART1, intp));
|
---|
| 174 |
|
---|
| 175 | }
|
---|
| 176 | return size;
|
---|
| 177 | };
|
---|
| 178 |
|
---|
| 179 | #ifndef EV3_SOUND
|
---|
| 180 | void _write(){}
|
---|
| 181 | void _close(){}
|
---|
| 182 | void _lseek(){}
|
---|
| 183 | void _read(){}
|
---|
| 184 | //void _fstat(){}
|
---|
| 185 | //void _isatty(){}
|
---|
| 186 | #endif
|
---|
| 187 |
|
---|
| 188 | //void _exit(){}
|
---|
| 189 | //void _getpid(){}
|
---|
| 190 | //void _kill(){}
|
---|
| 191 | void _gettimeofday(){}
|
---|
| 192 | void _fini(){}
|
---|
| 193 |
|
---|