TA的每日心情 | 郁闷 2024-10-28 10:11 |
---|
签到天数: 1703 天 连续签到: 1 天 [LV.Master]伴坛终老
|
本帖最后由 nemon 于 2013-2-5 16:32 编辑
记得小学时,语文考试最后一题的作文一般是看图说话,最常出的作文题是《从XXXX想到的》,今天我们从这幅漫画开始。
这是《纽约客》漫画家Peter Steiner于1993年7月5日发表的作品,这幅漫画的标题估计尽人皆知:“在互联网上,没人知道你是一条狗”。这则漫画体现了一种对互联网的理解,强调用户能够以一种不透露个人信息的方式来发送或接受信息的能力,可以说是对互联网时代隐私的一种社会学探讨。每当我看到这幅画,就会想起这样一种测试——测试时如果一个人(代号C)使用测试对象皆理解的语言去询问两个他不能看见的对象任意一串问题。对象为:一个是正常思维的人(代号B)、一个是机器(代号A)。如果经过若干询问以后,C不能得出实质的区别来分辨A与B的不同,则此机器A通过测试。这个测试叫做……是滴,图灵测试。不过很不幸,图灵的死,却是因为当时的英国很容不下他很隐私的一些取向。(以上文字引用了wikipedia中 相 关 内容 )图灵有多强,大家都知道。关于某著名水果公司的标志是不是向图灵致敬的问题,那位已故的老大解释说"It isn't true, but God, we wish it were."今天我们就做这件连jobs都没能做的事,方式很简单,实现一个图灵机。
图灵机的定义很抽象,所以我们先来通俗的解释一下要做的到底是个啥玩意。
假如有这么一台机器,你把一条分成一格一格的写着字的纸带作为输入给它,它根据机器本身的状态(由之前的输入决定)和读写头输入纸带当前格子里的值,按照预先输入的规则来决定做什么操作(可以是改写当前纸带上的内容、向前或后移动读写头、输出一个字),那么这套机器就是一个图灵机了。我们把这个定义再具体化一点:
1】输入纸带上只有3种子符:0、1、E;
2】输出纸带上只有2种子符:0、1;
3】规则定义为以下格式
q w q1 w1 d
或q w q1 w1 d e
其中:q为输入纸带当前格子里的值,w为机器本身的状态号,q1向输入纸带当前格子里写的值,w1为机器本步执行完后的状态号,d为读写头移动的方向(+为向后、-为向前、0为不动),e为输出的值。
那么对于任意一个以E结尾的输入纸带,下面的规则库就是输出E之前的0、1组成的序列表示的二进制数的奇偶:- 1 0 1 0 +
- 1 1 1 1 +
- 1 e 2 e -
- 2 1 3 1 0 1
- 2 0 3 0 0 0
复制代码 用SAM-4S Xplained实现后,我们在串口做的控制台上实验一下:
首先显示菜单:
然后,按1选择输入一条规则:
输入第一条规则,以@作为输入结束的标识符:
照此把其余几条规则都输入:
然后按2全部显示出来,注意这里看到的已经是内部代码表示了:
然后按5,选择录入输入纸带:
输入纸带的内容我们定义成"10111e@",注意是以@结尾的6位,但其中只有5位是数字:
然后按8显示输入纸带的内容,检查一下:
这时候,按7,可以看到初始化的输出纸带,上面全是0:
然后,按6,运行程序,可以看到调试信息,也就是每一步用到的规则:
最后,再次选7,看输出纸带:
哇,10111e的个位数还真的是1,好神奇啊。
那么,这个神奇的功能之如何实现的呢?上代码!
首先是头文件:- /*
- * turing.h
- *
- * Created: 2013-2-1 18:32:23
- * Author: Nemon
- */
- #ifndef TURING_H
- #define TURING_H
- #define BUFF_IN_SIZE 4 //输入纸带长,实际可容纳的字符数是 (BUFF_IN_SIZE*sizeof(unsignd char))/2-2 (每个字符占2位,头、尾为“E”)
- #define BUFF_OT_SIZE 4 //输出纸带长
- #define PATTERN_SIZE 64 //规则库大小(由q+w决定) 1<<(sizeof(q)+sizeof(w))=1<<(4+2)=64
- #define SHEFT_NONE 0 //输入读写头不动 00
- #define SHEFT_LEFT 2 //输入读写头左移 10
- #define SHEFT_RIGHT 3 //输入读写头右移 11
- #define TOKEN_E 0 //信号“E” 00
- #define TOKEN_0 2 //信号“0” 10
- #define TOKEN_1 3 //信号“1” 11
- #define CMD_WRITE 1 // 有输出
- #define CMD_NOOUT 0 // 无输出
- //规则 q,w,q1,w1,d,e :q[4],w[2],q1[4],w1[2],d[3],e[1]=[16]
- // q : 1111 00 0000 00 000 0 : 0x0f << 12
- // w : 0000 11 0000 00 000 0 : 0x03 << 10
- // q1: 0000 00 1111 00 000 0 : 0x0f << 6
- // w1: 0000 00 0000 11 000 0 : 0x03 << 4
- // d : 0000 00 0000 00 111 0 : 0x07 << 1
- // e : 0000 00 0000 00 000 1 : 0x01 << 0
- // 规则各位的掩码
- #define MASK_PATTERN_q 0x0f << 12
- #define MASK_PATTERN_w 0x03 << 10
- #define MASK_PATTERN_q1 0x0f << 6
- #define MASK_PATTERN_w1 0x03 << 4
- #define MASK_PATTERN_d 0x07 << 1
- #define MASK_PATTERN_dh 0x03 << 2
- #define MASK_PATTERN_dl 0x01 << 1
- #define MASK_PATTERN_e 0x01 << 0
- //获取规则内容的宏
- #define GET_PATTERN_q(x) (((x)&MASK_PATTERN_q )>>12)
- #define GET_PATTERN_w(x) (((x)&MASK_PATTERN_w )>>10)
- #define GET_PATTERN_q1(x) (((x)&MASK_PATTERN_q1)>> 6)
- #define GET_PATTERN_w1(x) (((x)&MASK_PATTERN_w1)>> 4)
- #define GET_PATTERN_d(x) (((x)&MASK_PATTERN_d )>> 1)
- #define GET_PATTERN_dh(x) (((x)&MASK_PATTERN_dh)>> 2)
- #define GET_PATTERN_dl(x) (((x)&MASK_PATTERN_dl)>> 1)
- #define GET_PATTERN_e(x) (((x)&MASK_PATTERN_e )>> 0)
- //翻译为规则的宏
- #define MACRO_PATTERN(q,w,q1,w1,dh,dl,e) \
- ( (((q ) &0x0f)<<12) \
- | (((w )&0x03)<<10) \
- | (((q1)&0x0f)<< 6) \
- | (((w1)&0x03)<< 4) \
- | (((dh)&0x03)<< 2) \
- | (((dl)&0x01)<< 1) \
- | (((e )&0x01)<< 0) \
- )
- //翻译为输入纸带的宏
- #define MAKE_INPUT_TIE(c1,c2,c3,c4) (((c1)<<6)|((c2)<<4)|((c3)<<2)|((c4)<<0))
- //翻译为“无输出的规则”或“有输出的规则”的宏
- #define MACRO_PATTERN_DOIT(q,w,q1,w1,d) MACRO_PATTERN(q,w,q1,w1,d,CMD_NOOUT,0)
- #define MACRO_PATTERN_DOUT(q,w,q1,w1,d,e) MACRO_PATTERN(q,w,q1,w1,d,CMD_WRITE,e)
- //int turing();
- /*
- INPUT:
- //输入纸带
- unsigned char v_uc_input[BUFF_IN_SIZE];
- unsigned long v_ul_input_length;
- //输出纸带
- unsigned char v_uc_output[BUFF_OT_SIZE];
- unsigned long v_uc_output_length;
- //规则库 q,w,q1,w1,d,e :q[4],w[2],q1[4],w1[2],d[3],e[1]=[16]
- // q:={1~15}
- unsigned short v_ui_patterns[PATTERN_SIZE];
- unsigned long v_ui_patterns_length;
- OUPUT:
- //输出纸带的长度
- unsigned int
- */
- unsigned int RunTuring( unsigned char * v_uc_input , unsigned long v_ul_input_length , unsigned char * v_uc_output , unsigned long v_uc_output_length , unsigned short * v_ui_patterns , unsigned long v_ui_patterns_length );
- #endif //TURING_H
复制代码 然后是图灵机的执行逻辑:- #include "turing.h"
- /*
- INPUT:
- //输入纸带
- unsigned char v_uc_input[BUFF_IN_SIZE];
- unsigned long v_ul_input_length;
- //输出纸带
- unsigned char v_uc_output[BUFF_OT_SIZE];
- unsigned long v_uc_output_length;
- //规则库 q,w,q1,w1,d,e :q[4],w[2],q1[4],w1[2],d[3],e[1]=[16]
- // q:={1~15}
- unsigned short v_ui_patterns[PATTERN_SIZE];
- unsigned long v_ui_patterns_length;
- OUPUT:
- //输出纸带的长度
- unsigned int
- */
- unsigned int RunTuring ( unsigned char * v_uc_input , unsigned long v_ul_input_length , unsigned char * v_uc_output , unsigned long v_ul_output_length , unsigned short * v_ui_patterns , unsigned long v_ul_patterns_length )
- {
- //循环用通用计数器
- unsigned short ui_pointer=0;
- //输入纸带
- unsigned char uc_input[BUFF_IN_SIZE];
- //输出纸带
- //unsigned char uc_output[BUFF_OT_SIZE];
- unsigned char * uc_output;
- //规则库 q,w,q1,w1,d,e :q[4],w[2],q1[4],w1[2],d[3],e[1]=[16]
- // q:={1~15}
- unsigned short ui_patterns[PATTERN_SIZE];
- //当前输入指针
- unsigned short ui_input_pointer=1;
- //当前输出指针
- unsigned short ui_output_pointer=0;
- //当前输入
- unsigned char ui_input=0;
- //当前状态
- unsigned short ui_state=1;
- //当前规则
- unsigned short ui_pattern=0;
- //初始化规则库
- ui_patterns[0]=0;
- for(ui_pointer=1;ui_pointer<=v_ul_patterns_length && ui_pointer<PATTERN_SIZE;ui_pointer++)
- {
- ui_patterns[ui_pointer]=v_ui_patterns[ui_pointer-1];
- }
-
- //初始化当前输入指针
- ui_input_pointer=1;
- //初始化输入纸带
- for(ui_pointer=0;ui_pointer<v_ul_input_length && ui_pointer<BUFF_IN_SIZE;ui_pointer++)
- {
- uc_input[ui_pointer]=v_uc_input[ui_pointer];
- }
- //初始化当前输出指针
- ui_output_pointer=0;
- //初始化输出纸带
- uc_output=v_uc_output;
- for(ui_pointer=0;ui_pointer<v_ul_output_length;ui_pointer++)
- {
- uc_output[ui_pointer]=0;
- }
-
- //初始化当前状态
- ui_state=1;
- //执行
- while(ui_state!=0)
- {
- //获取当前输入
- ui_input=(uc_input[ui_input_pointer/4]>>(2*(3-(ui_input_pointer%4))))& 3;
- //获取当前规则
- //for(ui_pointer=PATTERN_SIZE-1;ui_pointer>0;ui_pointer--)
- for(ui_pointer=v_ul_patterns_length;ui_pointer>0;ui_pointer--)
- {
- ui_pattern=ui_patterns[ui_pointer];
- if (GET_PATTERN_q(ui_pattern)==ui_state && GET_PATTERN_w(ui_pattern)==ui_input)
- {
- break;//找到
- }
- }
- /* debug */
- printf( "{%d}[%d]=%d,(%d)=%x,q=%d,w=%d,q1=%d,w1=%d,dh=%d,dl=%d,e=%d\n"
- , ui_state
- , ui_input_pointer
- , ui_input
- , ui_pointer
- , ui_pattern
- , GET_PATTERN_q(ui_pattern)
- , GET_PATTERN_w(ui_pattern)
- , GET_PATTERN_q1(ui_pattern)
- , GET_PATTERN_w1(ui_pattern)
- , GET_PATTERN_dh(ui_pattern)
- , GET_PATTERN_dl(ui_pattern)
- , GET_PATTERN_e(ui_pattern)
- );
- /* */
- if(ui_pointer!=0)
- {
- //状态转换为q1
- ui_state = GET_PATTERN_q1(ui_pattern);
- if( GET_PATTERN_w(ui_pattern)!= GET_PATTERN_w1(ui_pattern))
- {
- //写w1
- switch(3-(ui_input_pointer%4))
- {
- case 0:
- uc_input[ui_input_pointer/4] &= 0xfc; //11111100
- break;
- case 1:
- uc_input[ui_input_pointer/4] &= 0xf3; //11110011
- break;
- case 2:
- uc_input[ui_input_pointer/4] &= 0xcf; //11001111
- break;
- case 3:
- uc_input[ui_input_pointer/4] &= 0x3f; //00111111
- break;
- default:
- //RAISE ERROR
- break;
- }
- uc_input[ui_input_pointer/4] |= ((GET_PATTERN_w1(ui_pattern))>>(3-(ui_input_pointer%4))) ;
- }
- //移动输入指针
- switch (GET_PATTERN_dh(ui_pattern))
- {
- case SHEFT_LEFT:
- if (ui_input_pointer>0)ui_input_pointer--;
- break;
- case SHEFT_RIGHT:
- //if (ui_input_pointer<BUFF_IN_SIZE*8/2-1)ui_input_pointer++;
- if (ui_input_pointer<v_ul_input_length*8/2-1)ui_input_pointer++;
- break;
- case SHEFT_NONE:
- break;
- default:
- //RAISE ERROR
- break;
- }
- //输出
- switch (GET_PATTERN_dl(ui_pattern))
- {
- case CMD_WRITE:
- //if (ui_output_pointer<BUFF_OT_SIZE*8/1){
- if (ui_output_pointer<v_ul_output_length*8/1){
- if(GET_PATTERN_e(ui_pattern))
- {
- uc_output[ui_output_pointer/8] |= 1<<(7-(ui_output_pointer%8));//写1
- }
- else
- {
- uc_output[ui_output_pointer/8] &= (0xff-(1<<(7-(ui_output_pointer%8))));//写0
- }
- //if(ui_output_pointer<BUFF_OT_SIZE*8/1-1)
- if(ui_output_pointer<v_ul_output_length*8/1-1)
- {
- ui_output_pointer++;
- }
- else
- {
- //RAISE ERROR
- }
- }
- else
- {
- //RAISE ERROR
- }
- break;
- case CMD_NOOUT:
- break;
- default:
- //RAISE ERROR
- break;
- }
- }
- else
- {
- //没找到,停机
- ui_state=0;
- }
- }
-
- //输出 输出纸带 的长度
- return ui_output_pointer;
- }
复制代码 最后是菜单项的定义和事件响应函数:- //菜单项的定义
- …………
- int i,n,iCnt;
- char console_buff[1000];
- struct stCosnoleMenu st_CosnoleMenu_buff[MENU_BUFF_SIZE];
- //Cosnole_Menu_Init(&stMenu,stCosnoleMenu,"back up",MENU_DEFAULT_SELECTED_EVENT_FUNC,MENU_DEFAULT_SELECTED_EVENT_FUNC,MENU_DEFAULT_SELECTED_EVENT_FUNC);
- Cosnole_Menu_Init(&stMenu,st_CosnoleMenu_buff,"back up",my_selected_event,my_command_event,my_submenu_event);
- iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x11,"Add a Rule" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'1');//1
- iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x12,"Show all Rules" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'2');//2
- iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x13,"Del all Rules" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'3');//3
- iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x14,"Del a Rule" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'4');//4
- iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x15,"Input the Tape" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'5');//5
- iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x16,"Run the Tape" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'6');//6
- iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x17,"Show output" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'7');//7
- iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x18,"Show Input" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'8');//8
- …………
- //事件响应函数
- void my_command_event(struct stCosnoleMenuRoot *stRt,unsigned int ui_ID)
- {
- char ac_buff[512]={0};
- unsigned int ui_l,ui_i,ui_j;
- printf("command 0x%x is be selected\r\n",ui_ID);
- switch(ui_ID)
- {
- case 0x11://iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x11,"Add a Rule" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'1');//1
- if(uig_patterns_length>=PATTERN_SIZE-1)
- {
- printf("there is %d ruls, so can't add new one.\r\n",uig_patterns_length);
- }
- else
- {
- //增加1条规则
- ui_l=inputLineFromConsole("Input a Rule.",ac_buff,512);
- //规则库 q,w,q1,w1,d,e :q[4],w[2],q1[4],w1[2],d[3],e[1]=[16]
- //MACRO_PATTERN_DOIT(q,w,q1,w1,d,e)
- //MACRO_PATTERN_DOUT(q,w,q1,w1,d)
- unsigned int uptr,u1,u2,u3,u4,u5;
- if(ui_l<9)
- {
- printf("%d is too short to be a rule",ui_l);
- }
- else
- {
- uptr=0;
- //q
- printf("%s\r\n",ac_buff);
- if(' '==ac_buff[uptr+1]){
- u1=(ac_buff[uptr]-'0');
- uptr+=2;
- }
- else
- {
- u1=(ac_buff[uptr]-'0')*10+(ac_buff[uptr+1]-'0');
- uptr+=3;
- }
- //w
- switch(ac_buff[uptr]){
- case '0':
- u2=TOKEN_0;
- break;
- case '1':
- u2=TOKEN_1;
- break;
- case 'e':
- case 'E':
- u2=TOKEN_E;
- break;
- default:
- u2=TOKEN_E;
- break;
- }
- uptr+=2;
- //q1
- if(' '==ac_buff[uptr+2]){
- u3=(ac_buff[uptr]-'0')*10+(ac_buff[uptr+1]-'0');
- uptr+=3;
- }
- else
- {
- u3=(ac_buff[uptr]-'0');
- uptr+=2;
- }
- //w1
- switch(ac_buff[uptr]){
- case '0':
- u4=TOKEN_0;
- break;
- case '1':
- u4=TOKEN_1;
- break;
- case 'e':
- case 'E':
- u4=TOKEN_E;
- break;
- default:
- u4=TOKEN_E;
- break;
- }
- uptr+=2;
- //d
- switch(ac_buff[uptr]){
- case '+':
- u5=SHEFT_RIGHT;
- break;
- case '-':
- u5=SHEFT_LEFT;
- break;
- case '0':
- default:
- u5=SHEFT_NONE;
- break;
- }
- //e
- if(uptr+1<ui_l)
- {
- uptr+=2;
- if('0'==ac_buff[uptr])
- {
- uig_patterns[uig_patterns_length]= MACRO_PATTERN_DOUT(u1,u2,u3,u4,u5,0) ;
- uig_patterns_length++;
- }else
- {
- uig_patterns[uig_patterns_length]= MACRO_PATTERN_DOUT(u1,u2,u3,u4,u5,1) ;
- uig_patterns_length++;
- }
- }
- else
- {
- uig_patterns[uig_patterns_length] = MACRO_PATTERN_DOIT(u1,u2,u3,u4,u5) ;
- uig_patterns_length++;
- }
- printf("a Rule be added, now there is %d ruls.\r\n",uig_patterns_length);
- }
- }
- /*
- * TODO
- */
- break;
- case 0x12://iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x12,"Show all Rules" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'2');//2
- //显示所有规则
- printf("Show all %d Rules:\r\n",uig_patterns_length);
- //打印规则库
- for(ui_l=0;ui_l<uig_patterns_length;ui_l++)
- {
- printf( "[%d]={%x},q=%d,w=%d,q1=%d,w1=%d,dh=%d,dl=%d,e=%d\r\n"
- , ui_l
- , uig_patterns[ui_l]
- , GET_PATTERN_q( uig_patterns[ui_l] )
- , GET_PATTERN_w( uig_patterns[ui_l] )
- , GET_PATTERN_q1( uig_patterns[ui_l] )
- , GET_PATTERN_w1( uig_patterns[ui_l] )
- , GET_PATTERN_dh( uig_patterns[ui_l] )
- , GET_PATTERN_dl( uig_patterns[ui_l] )
- , GET_PATTERN_e( uig_patterns[ui_l] )
- );
- }
- printf("the Rules be Show\r\n");
- break;
- case 0x13://iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x13,"Del all Rules" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'3');//3
- //删除所有规则
- ui_l=inputLineFromConsole("Delete all Rules?(N,y)",ac_buff,1);
- if(ui_l>0 && ( 'Y'==ac_buff[0] || 'y'==ac_buff[0] ))//需要确认
- {
- //规则条数改作0即可
- uig_patterns_length=0;
- printf("all Rules Deleted.\r\n");
- }
- else
- {
- printf("Delete cancled.\r\n");
- }
- break;
- case 0x14://iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x14,"Delete a Rule" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'4');//4
- //删除1条规则
- ui_l=inputLineFromConsole("Delete a Rule.Input Num",ac_buff,5);
- ui_j=0;
- for(ui_i=0;ui_i<ui_l;ui_i++)
- {
- if( ac_buff[ui_i]>='0' && ac_buff[ui_i]<='9' )
- {
- ui_j=ui_j*10+ac_buff[ui_i]-'0';
- }
- }
- //显示删除哪条规则
- printf("Delete the %d Rule.\r\n",ui_j);
- if(ui_j<uig_patterns_length-1)
- {
- //如果不是最后一条条规则,被删除那条后面的前移
- for(ui_i=ui_j;ui_i<uig_patterns_length-1;ui_i++)
- {
- uig_patterns[ui_i]=uig_patterns[ui_i+1];
- }
- uig_patterns_length--;
- }
- else if(ui_j==(uig_patterns_length-1))
- {
- //如果是最后一条条规则,只修改规则数量
- uig_patterns_length--;
- }
- else
- {
- printf("there isn't %d rule, check pls.\r\n",ui_j);
- }
- printf("Remove %d, Done.\r\n",ui_j);
- break;
- case 0x15://iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x15,"Input the Tape" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'5');//5
- //输入 输入纸带
- ui_l=inputLineFromConsole("Input the Tape.",ac_buff,512);
- ui_j=0;
- //初始化 输入纸带
- //for(ui_i=0;ui_i<((ui_l+1+1)/4);ui_i++)ucg_input[ui_i]=MAKE_INPUT_TIE( TOKEN_E , TOKEN_E , TOKEN_E , TOKEN_E );
- ucg_input[0]=MAKE_INPUT_TIE( TOKEN_E , TOKEN_E , TOKEN_E , TOKEN_E );
- //将0、1、E写入 输入纸带
- for(ui_i=0;ui_i<ui_l && ui_i<BUFF_IN_SIZE*4-1;ui_i++)
- {
- switch (ac_buff[ui_i])
- {
- case '0':
- ui_j++;
- ucg_input[(ui_j/4)] |= TOKEN_0<<(2*(3-ui_j%4));//TODO
- //printf("%d,%d,%d,%d,%x,%x.\r\n",ui_j,ac_buff[ui_i],(ui_j/4),(2*(3-ui_j%4)),TOKEN_0<<(2*(3-ui_j%4)),TOKEN_0);
- break;
- case '1':
- ui_j++;
- ucg_input[(ui_j/4)] |= TOKEN_1<<(2*(3-ui_j%4));//TODO
- //printf("%d,%d,%d,%d,%x,%x.\r\n",ui_j,ac_buff[ui_i],(ui_j/4),(2*(3-ui_j%4)),TOKEN_1<<(2*(3-ui_j%4)),TOKEN_1);
- break;
- case 'e':
- case 'E':
- ui_j++;
- ucg_input[(ui_j/4)] |= TOKEN_E<<(2*(3-ui_j%4));//TODO
- //printf("%d,%d,%d,%d,%x,%x.\r\n",ui_j,ac_buff[ui_i],(ui_j/4),(2*(3-ui_j%4)),TOKEN_E<<(2*(3-ui_j%4)),TOKEN_E);
- break;
- default:
- break;
- }
- }
- uig_input_length=ui_j;
- printf("the Tape has %d char.\r\n",ui_j);
- break;
- case 0x16://iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x16,"Run the Tape" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'6');//6
- //按 规则库 运行 输入纸带
- printf("the Tape is Running...\r\n");
- uig_output_length = RunTuring(ucg_input,uig_input_length,ucg_output,BUFF_OT_SIZE,uig_patterns,uig_patterns_length);
- printf("the Tape is Done\r\n");
- break;
- case 0x17://iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x17,"Show output" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'7');//7
- //显示 输出纸带
- printf("Output Tape with %d char:\r\n",uig_output_length);
- //打印输出纸带
- for(ui_l=0;ui_l<uig_output_length;ui_l++)
- {
- printf("%d",(ucg_output[ui_l/8]>>(7-(ui_l%8)))& 1);
- }
- printf("\r\n");
- printf("the OutTape shown as above.\r\n");
- break;
- case 0x18://iCnt=Cosnole_Add_A_Menu(&stMenu,0,0x18,"Show Input" ,MENU_TYPE_COMMAND,MENU_CHECK_TRUE,'8');//8
- //显示 输入纸带
- printf("Input Tape with %d char:\r\n",uig_input_length);
- //输入纸带
- for(ui_l=1;ui_l<=uig_input_length;ui_l++)
- {
- //printf("([%d]%d=%x,%d)",ui_l,ui_l/4,ucg_input[ui_l/4],(3-ui_l%4)*2);
- switch( (ucg_input[ui_l/4]>>( (3-ui_l%4)*2 ))&3 )
- {
- case TOKEN_E:printf("e");break;
- case TOKEN_0:printf("0");break;
- case TOKEN_1:printf("1");break;
- }
- }
- printf("\r\n");
- printf("the Input Tape shown as above.\r\n");
- break;
- default:
- printf("command 0x%x is be undefined.\r\n",ui_ID);
- }
- show_current_menu(&stMenu);
- }
- //获取一段控制台输入,以@结尾,返回长度
- unsigned int inputLineFromConsole(char * pc_promaptStr,char *pc_buff,unsigned int ui_buff_length)
- {
- uint8_t uc_char;
- unsigned int ui_len=0;
- for(ui_len=0;ui_len<ui_buff_length;ui_len++)pc_buff[ui_len]=0;
- ui_len=0;
- uc_char=0;
-
- printf("%s\r\n",pc_promaptStr);
- uart_read(CONSOLE_UART, &uc_char);
- while(uc_char!='@' && ui_len<ui_buff_length-1)
- {
- if(uc_char>=32 && uc_char<=126 )
- {
- pc_buff[ui_len++]=uc_char;
- uc_char=0;
- }
- uart_read(CONSOLE_UART, &uc_char);
- }
- pc_buff[ui_len]=0;
- //printf("%s",pc_buff);
- //puts("\r\n");
- return ui_len;
- }
复制代码 |
|