[388] | 1 | /*
|
---|
| 2 | * Copyright (C) 2008-2017 by TOPPERS Project
|
---|
| 3 | *
|
---|
| 4 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 5 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 6 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 7 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 8 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 9 | * スコード中に含まれていること.
|
---|
| 10 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 11 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 12 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 13 | * の無保証規定を掲載すること.
|
---|
| 14 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 15 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 16 | * と.
|
---|
| 17 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 18 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 19 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 20 | * 報告すること.
|
---|
| 21 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 22 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 23 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 24 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 25 | * 免責すること.
|
---|
| 26 | *
|
---|
| 27 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 28 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 29 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 30 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 31 | * の責任を負わない.
|
---|
| 32 | *
|
---|
| 33 | * @(#) $Id$
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | /* #[<PREAMBLE>]#
|
---|
| 37 | * #[<...>]# から #[</...>]# で囲まれたコメントは編集しないでください
|
---|
| 38 | * tecsmerge によるマージに使用されます
|
---|
| 39 | * 呼び口関数 #_TCPF_#
|
---|
| 40 | * call port : cSerialPort signature: sSerialPort context: task
|
---|
| 41 | * ER cSerialPort_open( );
|
---|
| 42 | * ER cSerialPort_close( );
|
---|
| 43 | * ER_UINT cSerialPort_read( char_t* buffer, uint_t length );
|
---|
| 44 | * ER_UINT cSerialPort_write( const char_t* buffer, uint_t length );
|
---|
| 45 | * ER cSerialPort_control( uint_t ioControl );
|
---|
| 46 | * ER cSerialPort_refer( T_SERIAL_RPOR* pk_rpor );
|
---|
| 47 | *
|
---|
| 48 | * #[</PREAMBLE>]# */
|
---|
| 49 |
|
---|
| 50 | /* プロトタイプ宣言や変数の定義をここに書きます #_PAC_# */
|
---|
| 51 | #include "nMruby_tMruby_tecsgen.h"
|
---|
| 52 | #include <string.h>
|
---|
| 53 |
|
---|
| 54 | #include <mruby.h>
|
---|
| 55 | #include <mruby/proc.h>
|
---|
| 56 | #include <mruby/data.h>
|
---|
| 57 | #include <mruby/compile.h>
|
---|
| 58 |
|
---|
| 59 | #ifndef E_OK
|
---|
| 60 | #define E_OK 0 /* success */
|
---|
| 61 | #define E_ID (-18) /* illegal ID */
|
---|
| 62 | #endif
|
---|
| 63 |
|
---|
| 64 | #define MEM_SIZE 1024*1024 // 1MB
|
---|
| 65 | //#define VM_EVAL
|
---|
| 66 | //#define MALLOC_EVAL
|
---|
| 67 |
|
---|
| 68 | #include <t_syslog.h>
|
---|
| 69 |
|
---|
| 70 | void mrb_init_mrb(CELLCB *p_cellcb, mrb_state *mrb);
|
---|
| 71 | /* 受け口関数 #_TEPF_# */
|
---|
| 72 | /* #[<ENTRY_PORT>]# eMrubyBody
|
---|
| 73 | * entry port: eMrubyBody
|
---|
| 74 | * signature: sMrubyBody
|
---|
| 75 | * context: task
|
---|
| 76 | * #[</ENTRY_PORT>]# */
|
---|
| 77 |
|
---|
| 78 | /* #[<ENTRY_FUNC>]# eMrubyBody_main
|
---|
| 79 | * name: eMrubyBody_main
|
---|
| 80 | * global_name: nMruby_tMruby_eMrubyBody_main
|
---|
| 81 | * oneway: false
|
---|
| 82 | * #[</ENTRY_FUNC>]# */
|
---|
| 83 | void
|
---|
| 84 | eMrubyBody_main(CELLIDX idx)
|
---|
| 85 | {
|
---|
| 86 | #ifdef VM_EVAL
|
---|
| 87 | SYSUTM tstart, tend;
|
---|
| 88 | #endif
|
---|
| 89 |
|
---|
| 90 | CELLCB *p_cellcb;
|
---|
| 91 | mrb_state *mrb;
|
---|
| 92 |
|
---|
| 93 | if (VALID_IDX(idx)) {
|
---|
| 94 | p_cellcb = GET_CELLCB(idx);
|
---|
| 95 | }
|
---|
| 96 | #ifdef VM_EVAL
|
---|
| 97 | get_utm(&tstart);
|
---|
| 98 | #endif
|
---|
| 99 |
|
---|
| 100 | /* new interpreter instance */
|
---|
| 101 | mrb = mrb_open();
|
---|
| 102 | if (mrb == NULL) {
|
---|
| 103 | syslog(LOG_EMERG, "Invalid mrb_state, exiting test driver");
|
---|
| 104 | // cSerialPort_write("Invalid mrb_state, exiting test driver\n", 39);
|
---|
| 105 | return;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | #ifdef VM_EVAL
|
---|
| 109 | get_utm(&tend);
|
---|
| 110 | syslog(LOG_EMERG, "mrb_open time = %d micro sec", tend - tstart);
|
---|
| 111 | #endif
|
---|
| 112 | if(is_cInit_joined()){
|
---|
| 113 | // TECS Bridge
|
---|
| 114 | cInit_initializeBridge( mrb );
|
---|
| 115 | }
|
---|
| 116 | mrb_init_mrb(p_cellcb, mrb);
|
---|
| 117 | syslog(LOG_EMERG, "end of mruby program");
|
---|
| 118 | #ifdef VM_EVAL
|
---|
| 119 | get_utm(&tstart);
|
---|
| 120 | #endif
|
---|
| 121 | mrb_close(mrb);
|
---|
| 122 | #ifdef VM_EVAL
|
---|
| 123 | get_utm(&tend);
|
---|
| 124 | syslog(LOG_EMERG, "mrb_close time = %d micro sec", tend - tstart);
|
---|
| 125 | #endif
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | /* #[<POSTAMBLE>]#
|
---|
| 130 | * これより下に非受け口関数を書きます
|
---|
| 131 | * #[</POSTAMBLE>]#*/
|
---|
| 132 |
|
---|
| 133 | void
|
---|
| 134 | mrb_init_mrb(CELLCB *p_cellcb, mrb_state *mrb)
|
---|
| 135 | {
|
---|
| 136 | mrb_irep *irep = mrb_read_irep(mrb, ATTR_irep);
|
---|
| 137 |
|
---|
| 138 | mrb_run(mrb, mrb_proc_new(mrb, irep), mrb_top_self(mrb));
|
---|
| 139 | if (mrb->exc) {
|
---|
| 140 | mrb_p(mrb, mrb_obj_value(mrb->exc));
|
---|
| 141 | exit(0);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /*
|
---|
| 146 | extern void debug_print(char * str);
|
---|
| 147 | extern void debug_print_address(char * str, int address);
|
---|
| 148 | extern void debug_print_int(char * str, int data);
|
---|
| 149 | #include <errno.h>
|
---|
| 150 | void *
|
---|
| 151 | _sbrk(size_t incr)
|
---|
| 152 | {
|
---|
| 153 | // TODO: 正しいヒープサイズを指定すること (EV3はRAM 64MB)
|
---|
| 154 | extern char __heap_start;
|
---|
| 155 | static uint32_t cs3_heap_end = &__heap_start + MEM_SIZE; // ヒープの最後アドレス
|
---|
| 156 | static char *heap_end = &__heap_start;
|
---|
| 157 | char *prev_heap_end;
|
---|
| 158 | uint16_t incr2;
|
---|
| 159 |
|
---|
| 160 | char str[20];
|
---|
| 161 |
|
---|
| 162 | #ifdef MALLOC_EVAL
|
---|
| 163 | syslog(LOG_EMERG, "heap_start = %x", &__heap_start);
|
---|
| 164 | #endif
|
---|
| 165 |
|
---|
| 166 | prev_heap_end = heap_end;
|
---|
| 167 | incr2 = (uint16_t)incr;
|
---|
| 168 |
|
---|
| 169 | if (heap_end + incr2 > cs3_heap_end) {
|
---|
| 170 | errno = ENOMEM;
|
---|
| 171 | return (void *)-1;
|
---|
| 172 | }
|
---|
| 173 | heap_end += incr2;
|
---|
| 174 |
|
---|
| 175 | #ifdef MALLOC_EVAL
|
---|
| 176 | syslog(LOG_EMERG, "heap_end = %x", heap_end);
|
---|
| 177 | #endif
|
---|
| 178 |
|
---|
| 179 | #ifdef DEBUG_MALLOC
|
---|
| 180 | debug_print_int("incr",incr);
|
---|
| 181 | debug_print_address("prev_heap_end",(int) prev_heap_end);
|
---|
| 182 | dly_tsk(50);
|
---|
| 183 | #endif
|
---|
| 184 | return (void *) prev_heap_end;
|
---|
| 185 | }
|
---|
| 186 | */
|
---|
| 187 | extern intptr_t bt_snd_chr(intptr_t c);
|
---|
| 188 | extern intptr_t uart_snd_chr(intptr_t c) ;
|
---|
| 189 | extern void initialize_lcd_dri();
|
---|
| 190 | #define UART1 (*(volatile struct st_uart *)0x01D0C000)
|
---|
| 191 | size_t fwrite(const void *buf, size_t size, size_t n, FILE *fp){
|
---|
| 192 | intptr_t intp;
|
---|
| 193 | char *cp;
|
---|
| 194 | cp = (char*)buf;
|
---|
| 195 | /* シリアルポートが結合されている場合のみ呼び出す
|
---|
| 196 | if(is_cSerialPort_joined()){
|
---|
| 197 | //cSerialPort_write(buf, size);
|
---|
| 198 |
|
---|
| 199 | }*/
|
---|
| 200 |
|
---|
| 201 | for(int i = 0;i < size; i++){
|
---|
| 202 | intp = *cp++;
|
---|
| 203 | if (intp == '\n') {
|
---|
| 204 | bt_snd_chr('\r');
|
---|
| 205 | while(!uart_send(&UART1, '\r'));
|
---|
| 206 | }
|
---|
| 207 | bt_snd_chr(intp);
|
---|
| 208 | while(!uart_send(&UART1, intp));
|
---|
| 209 |
|
---|
| 210 | }
|
---|
| 211 | return size;
|
---|
| 212 | };
|
---|
| 213 |
|
---|
| 214 | #ifndef EV3_SOUND
|
---|
| 215 | void _write(){}
|
---|
| 216 | void _close(){}
|
---|
| 217 | void _lseek(){}
|
---|
| 218 | void _read(){}
|
---|
| 219 | //void _fstat(){}
|
---|
| 220 | //void _isatty(){}
|
---|
| 221 | #endif
|
---|
| 222 |
|
---|
| 223 | //void _exit(){}
|
---|
| 224 | //void _getpid(){}
|
---|
| 225 | //void _kill(){}
|
---|
| 226 | void _gettimeofday(){}
|
---|
| 227 | void _fini(){}
|
---|
| 228 |
|
---|