Exemplo n.º 1
0
instruction *
instruction_alloc( const char *name, int len, const char *val )
{
	instruction *i;

	if (!name || !val)
		return NULL;

	i = malloc( sizeof *i );
	if (!i)
		return NULL;

	if (strlen( name ) > MAXLEN_INSTRUCTION)
		printf( _("Warning: Instruction name too long\n") );
	strncpy( i->name, name, MAXLEN_INSTRUCTION );
	i->name[MAXLEN_INSTRUCTION] = '\0';

	i->value = register_alloc( len );
	if (!i->value) {
		free( i );
		return NULL;
	}
	i->out = register_alloc( len );
	if (!i->out) {
		free( i->value );
		free( i );
		return NULL;
	}

	register_init( i->value, val );
	i->data_register = NULL;
	i->next = NULL;

	return i;
}
Exemplo n.º 2
0
static void gic_proxy_realize(DeviceState *dev, Error **errp)
{
    GICProxy *s = XILINX_GIC_PROXY(dev);
    const char *prefix = object_get_canonical_path(OBJECT(dev));
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(gic_proxy_regs_info); ++i) {
        RegisterInfo *r = &s->regs_info[gic_proxy_regs_info[i].decode.addr/4];

        *r = (RegisterInfo) {
            .data = (uint8_t *)&s->regs[
                    gic_proxy_regs_info[i].decode.addr/4],
            .data_size = sizeof(uint32_t),
            .access = &gic_proxy_regs_info[i],
            .debug = GIC_PROXY_ERR_DEBUG,
            .prefix = prefix,
            .opaque = s,
        };
        register_init(r);
    }
}

static void gic_proxy_set_irq(void *opaque, int irq, int level)
{
    GICProxy *s = XILINX_GIC_PROXY(opaque);
    int group = irq / 32;
    int bit = irq % 32;

    if (level) {
        s->regs[GICPN_STATUS_REG(group)] |= 1 << bit;
    } else {
        s->regs[GICPN_STATUS_REG(group)] &= ~(1 << bit);
    }
    gicp_update(s, group);
}
Exemplo n.º 3
0
// Show a specified help page in US3 manual
void US_Help::show_help( const QString& helpFile )
{
  // Create a new process each time.  If show_help() is called multiple
  // times from a process, the previous process will become unavailable,
  // but we don't use it.  It's a 'memory leak', but should be minimal.
  // This is done because a QProcess can't be started multiple times
  // and we would still need a way to get the helpFile to assistant.
  // QProcess is a link to the daemon and is each program that calls
  // help needs its own Qprocess instance.

  // If we wanted to clean up, we would need to do something like the
  // following (not tested).  This seems like a lot of work for
  // little gain.
  
  // header:
  // QList< QProcess*> processes;
  
  // here:
  // assistant = new QProcess;
  // processes << assistant;
  // connect( assistant, SIGNAL(  finished  ( int, QProcess::ExitStatus ) )
  //        ( this     , SLOT  (  endProcess( ( int, QProcess::ExitStatus ) ) );
  
  // Function ( note that we don't really need status
  // void US_Help::end_process( int, QProcess::ExitStatus ) 
  // {
  //    for ( int i = processes.size() - 1; i >= 0; i-- )
  //    {
  //       if ( processes[ i ]->state() == QProcess::NotRunning )
  //       {
  //          delete processes[ i ];
  //          processes.takeAt( i );
  //       }
  //    }
  // }

  register_init();     // Register the QCH file path

  assistant = new QProcess;
  QStringList args;
  args << helpFile;

  // This us_helpdaemon will check if an instance is already running and
  // exit immediately if it is.

#ifndef Q_OS_MAC
  QString helpbin = US_Settings::appBaseDir() + "/bin/us_helpdaemon";
  assistant->start( helpbin, args );
#else
  QString helpbin = US_Settings::appBaseDir() + "/bin/us_helpdaemon";
  QString helpapp = helpbin + ".app";
  if ( QFile( helpapp ).exists() )
    assistant->start( helpapp, args );
  else
    assistant->start( helpbin, args );
#endif
  // Don't bother to wait
}
Exemplo n.º 4
0
/*
 * svf_copy_hex_to_register(hex_string, reg, len)
 *
 * Copies the contents of the hexadecimal string hex_string into the given
 * tap register.
 *
 * Parameter:
 *   hex_string : hex string to be entered in reg
 *   reg        : tap register to hold the converted hex string
 *
 * Return value:
 *   1 : all ok
 *   0 : error occurred
 */
static int
svf_copy_hex_to_register(char *hex_string, tap_register *reg)
{
  char *bit_string;

  if (!(bit_string = svf_build_bit_string(hex_string, reg->len)))
    return(0);

  register_init(reg, bit_string);

  /* free memory as we do not need the intermediate bit_string anymore */
  free(bit_string);

  return(1);
}
Exemplo n.º 5
0
void machine_initialize(_pmachine m)
{
    register_init(&m->_r_eax);
    register_init(&m->_r_ebx);
    register_init(&m->_r_ecx);
    register_init(&m->_r_edx);
    register_init(&m->_r_pc);
    register_init(&m->_r_flag);
    register_init(&m->_r_eip);

    
    stack_initialize(&m->stack);
    m->instructions = NULL;
    m->instruction_tail = NULL;
}
Exemplo n.º 6
0
RegisterInfoArray *register_init_block32(DeviceState *owner,
                                         const RegisterAccessInfo *rae,
                                         int num, RegisterInfo *ri,
                                         uint32_t *data,
                                         const MemoryRegionOps *ops,
                                         bool debug_enabled,
                                         uint64_t memory_size)
{
    const char *device_prefix = object_get_typename(OBJECT(owner));
    RegisterInfoArray *r_array = g_new0(RegisterInfoArray, 1);
    int i;

    r_array->r = g_new0(RegisterInfo *, num);
    r_array->num_elements = num;
    r_array->debug = debug_enabled;
    r_array->prefix = device_prefix;

    for (i = 0; i < num; i++) {
        int index = rae[i].addr / 4;
        RegisterInfo *r = &ri[index];

        *r = (RegisterInfo) {
            .data = &data[index],
            .data_size = sizeof(uint32_t),
            .access = &rae[i],
            .opaque = owner,
        };
        register_init(r);

        r_array->r[i] = r;
    }

    memory_region_init_io(&r_array->mem, OBJECT(owner), ops, r_array,
                          device_prefix, memory_size);

    return r_array;
}

void register_finalize_block(RegisterInfoArray *r_array)
{
    object_unparent(OBJECT(&r_array->mem));
    g_free(r_array->r);
    g_free(r_array);
}
Exemplo n.º 7
0
void plugin_init(G_GNUC_UNUSED GeanyData *gdata)
{
	GeanyKeyGroup *scope_key_group;
	char *gladefile = g_build_filename(PLUGINDATADIR, "scope.glade", NULL);
	GError *gerror = NULL;
	GtkWidget *menubar1 = find_widget(geany->main_widgets->window, "menubar1");
	guint item;
	const MenuKey *menu_key = debug_menu_keys;
	ToolItem *tool_item = toolbar_items;
	const ScopeCallback *scb;

	main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
	scope_key_group = plugin_set_key_group(geany_plugin, "scope", COUNT_KB, NULL);
	builder = gtk_builder_new();
	gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
	scp_tree_store_register_dynamic();

	if (!gtk_builder_add_from_file(builder, gladefile, &gerror))
	{
		msgwin_status_add(_("Scope: %s."), gerror->message);
		g_warning(_("Scope: %s."), gerror->message);
		g_error_free(gerror);
		g_object_unref(builder);
		builder = NULL;
	}

	g_free(gladefile);
	if (!builder)
		return;

	/* interface */
#ifndef G_OS_UNIX
	gtk_widget_hide(get_widget("terminal_show"));
#endif
	debug_item = get_widget("debug_item");
	if (menubar1)
		gtk_menu_shell_insert(GTK_MENU_SHELL(menubar1), debug_item, DEBUG_MENU_ITEM_POS);
	else
		gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), debug_item);

	menu_connect("debug_menu", &debug_menu_info, NULL);
	ui_add_document_sensitive(get_widget("scope_reset_markers"));
	ui_add_document_sensitive(get_widget("scope_cleanup_files"));

	for (item = 0; item < EVALUATE_KB; item++, menu_key++)
	{
		keybindings_set_item(scope_key_group, item, on_scope_key, 0, 0, menu_key->name,
			_(menu_key->label), debug_menu_items[item].widget);
	}

	geany_statusbar = GTK_STATUSBAR(gtk_widget_get_parent(geany->main_widgets->progressbar));
	debug_statusbar = get_widget("debug_statusbar");
	debug_state_label = GTK_LABEL(get_widget("debug_state_label"));
	gtk_box_pack_end(GTK_BOX(geany_statusbar), debug_statusbar, FALSE, FALSE, 0);

	debug_panel = get_widget("debug_panel");
	gtk_notebook_append_page(GTK_NOTEBOOK(geany->main_widgets->message_window_notebook),
		debug_panel, get_widget("debug_label"));

	/* startup */
	gtk216_init();
	program_init();
	prefs_init();
	conterm_init();
	inspect_init();
	register_init();
	parse_init();
	debug_init();
	views_init();
	thread_init();
	break_init();
	watch_init();
	stack_init();
	local_init();
	memory_init();
	menu_init();
	menu_set_popup_keybindings(scope_key_group, item);

	for (item = 0; tool_item->index != -1; item++, tool_item++)
	{
		GtkMenuItem *menu_item = GTK_MENU_ITEM(debug_menu_items[tool_item->index].widget);
		GtkToolItem *button = gtk_tool_button_new(NULL, gtk_menu_item_get_label(menu_item));

		gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(button),
			gtk_menu_item_get_use_underline(menu_item));
		g_signal_connect(button, "clicked", G_CALLBACK(on_toolbar_button_clicked),
			GINT_TO_POINTER(tool_item->index));
		g_signal_connect(button, "toolbar-reconfigured",
			G_CALLBACK(on_toolbar_reconfigured), tool_item);
		tool_item->widget = GTK_WIDGET(button);
		plugin_add_toolbar_item(geany_plugin, button);
	}

	toolbar_update_state(DS_INACTIVE);
	views_update_state(DS_INACTIVE);
	configure_toolbar();

	g_signal_connect(debug_panel, "switch-page", G_CALLBACK(on_view_changed), NULL);
	for (scb = scope_callbacks; scb->name; scb++)
		plugin_signal_connect(geany_plugin, NULL, scb->name, FALSE, scb->callback, NULL);
}
Exemplo n.º 8
0
static void rpu_realize(DeviceState *dev, Error **errp)
{
    RPU *s = XILINX_RPU(dev);
    const char *prefix = object_get_canonical_path(OBJECT(dev));
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(rpu_regs_info); ++i) {
        RegisterInfo *r = &s->regs_info[rpu_regs_info[i].decode.addr/4];

        *r = (RegisterInfo) {
            .data = (uint8_t *)&s->regs[
                    rpu_regs_info[i].decode.addr/4],
            .data_size = sizeof(uint32_t),
            .access = &rpu_regs_info[i],
            .debug = XILINX_RPU_ERR_DEBUG,
            .prefix = prefix,
            .opaque = s,
        };
        register_init(r);
        qdev_pass_all_gpios(DEVICE(r), dev);
    }

    if (!s->atcm1_for_rpu0) {
        error_set(errp, QERR_MISSING_PARAMETER, "atcm1-for-rpu0");
        return;
    }

    if (!s->btcm1_for_rpu0) {
        error_set(errp, QERR_MISSING_PARAMETER, "btcm1-for-rpu0");
        return;
    }

    if (!s->icache_for_rpu1) {
        error_set(errp, QERR_MISSING_PARAMETER, "icache-for-rpu1");
        return;
    }

    if (!s->dcache_for_rpu1) {
        error_set(errp, QERR_MISSING_PARAMETER, "dcache-for-rpu1");
        return;
    }

    if (!s->ddr) {
        error_set(errp, QERR_MISSING_PARAMETER, "ddr-mem-for-rpu");
        return;
    }

    /* RPUs starts in lockstep mode, so the rpu1 caches are not accessible. */
    memory_region_set_enabled(s->icache_for_rpu1, false);
    memory_region_set_enabled(s->dcache_for_rpu1, false);
    memory_region_set_enabled(s->ddr, false);
}

static void rpu_init(Object *obj)
{
    RPU *s = XILINX_RPU(obj);
    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);

    memory_region_init_io(&s->iomem, obj, &rpu_ops, s,
                          TYPE_XILINX_RPU, R_MAX * 4);
    sysbus_init_mmio(sbd, &s->iomem);
    sysbus_init_irq(sbd, &s->irq_rpu_1);
    sysbus_init_irq(sbd, &s->irq_rpu_0);

    /* xtcm1-for-rpu0 are the aliases for the tcm in lockstep mode.
     * This link allows to enable/disable those aliases when we are in
     * lock-step/normal mode.
     */
    object_property_add_link(obj, "atcm1-for-rpu0", TYPE_MEMORY_REGION,
                             (Object **)&s->atcm1_for_rpu0,
                             qdev_prop_allow_set_link_before_realize,
                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
                             &error_abort);
    object_property_add_link(obj, "btcm1-for-rpu0", TYPE_MEMORY_REGION,
                             (Object **)&s->btcm1_for_rpu0,
                             qdev_prop_allow_set_link_before_realize,
                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
                             &error_abort);
    object_property_add_link(obj, "rpu1-for-main-bus", TYPE_MEMORY_REGION,
                             (Object **)&s->atcm1_for_rpu0,
                             qdev_prop_allow_set_link_before_realize,
                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
                             &error_abort);

    /* This link allows to enable/disable those memory region when we are in
     * lock-step/normal mode.
     */
    object_property_add_link(obj, "icache-for-rpu1", TYPE_MEMORY_REGION,
                             (Object **)&s->icache_for_rpu1,
                             qdev_prop_allow_set_link_before_realize,
                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
                             &error_abort);
    object_property_add_link(obj, "dcache-for-rpu1", TYPE_MEMORY_REGION,
                             (Object **)&s->dcache_for_rpu1,
                             qdev_prop_allow_set_link_before_realize,
                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
                             &error_abort);

    /* Link to the second part of the DDR which is enabled in split mode and
     * disabled in lockstep mode.
     */
    object_property_add_link(obj, "ddr-mem-for-rpu", TYPE_MEMORY_REGION,
                             (Object **)&s->ddr,
                             qdev_prop_allow_set_link_before_realize,
                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
                             &error_abort);

    /* wfi_out is used to connect to PMU GPIs. */
    qdev_init_gpio_out_named(DEVICE(obj), s->wfi_out, "wfi_out", 2);
    /* wfi_in is used as input from CPUs as wfi request. */
    qdev_init_gpio_in_named(DEVICE(obj), zynqmp_rpu_0_handle_wfi, "wfi_in_0", 1);
    qdev_init_gpio_in_named(DEVICE(obj), zynqmp_rpu_1_handle_wfi, "wfi_in_1", 1);
}
Exemplo n.º 9
0
int main(int argc, char *argv[])
{
	FILE *fp,*opfp,*testf;
	int test,n,c,ln,i,m,m1,m2,b,flag=0,flag1=1,len,last,first,dig,id,errorflag=0,flen=0;
	long int madd=0000000,mm=1048575;
	label lbl[10];
	char ch,*nem[4],*str,*token,*token1,*token2,*token3,*s=" ",state,c1;

	str = (char *)malloc(sizeof(char)*50);
	token1 = (char *)malloc(sizeof(char)*10);
	token2 = (char *)malloc(sizeof(char)*20);
	token3 = (char *)malloc(sizeof(char)*20);
	//printf("file name %s\n", argv[1]);
	fp = fopen(argv[1],"r"); // opens input file which contains assembly code
	testf = fopen(argv[1],"r");
	opfp = fopen("stable.txt","w");

	if( fp == NULL )
	{
		perror("Error while opening the file.\n");
		exit(EXIT_FAILURE);
	}

	for(m1=0;m1<10;m1++)
	{
		lbl[m1].labels = (char *)malloc(sizeof(char)*10);
	}
	
	register_init();
	instr_init();

	for (c1 = getc(testf); c1 != EOF; c1 = getc(testf)){
        if (c1 == '\n') // Increment count if this character is newline
            flen++;
	}
	fclose(testf);
	//printf("--%d--",flen);
	while( fgets(str,50,fp)!=NULL) //line reading
	{
	
		state='X';
		if(flen==l){
			strcat(str,"\0");
		}
		if((str!="")&&((flen!=l)))
			str[strlen(str)-1]='\0';
		if(strcmp(str,"")==0)
			strcpy(str,"BLANK LINE");
		
		if(l==0)
		{
			if(strlen(str)!=5)
			{
				printf("error in line : %d",l+1);
				errorflag=1;
			}
			strncpy(token1,str,5);//printf( "==%s",token1 );
			if(strcmp(token1,(ins[0].nemo))!=0)
			{
				printf("error in line : %d",l+1);
				errorflag=1;
			}
		}
		
		if(l>0)
		{
			//printf( "%00000007ld\t\t%s\n",madd,str );
			madd+=32;
			state='X';
			token = strtok(str,s); // instruction name
			
			len=strlen(token);
			last=token[len-1];
			if(last==58)
			{
				token[strlen(token)-1]='\0';//printf("--%s\n",token);
				strcpy(lbl[lb].labels,token);
				lbl[lb].mad=(madd-32);
				lb++;	
				token = strtok(NULL,s);
			}

			while( token != NULL )
			{
				
				for(b=1;b<=4;b++)  // data transfer (MOV)
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='T';
						break;
					}
				}
	
				for(b=5;b<=8;b++)  // direct address
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='I';
						break;
					}
				}
				
				for(b=9;b<=15;b++) // arithmatic instruction and AND
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='A';
						id=b;
						break;
					}
				}

				for(b=16;b<=18;b++) // OR types
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						id=b;
						state='O';
						break;
					}
				}

				for(b=19;b<=20;b++)  // NOT
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='N';
						break;
					}
				}
				
				for(b=21;b<=26;b++)  // type INS r1
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='R';
						break;
					}
				}

				if(strcmp(token,(ins[27].nemo))==0) // compare CMP
				{
					state='P';
				}
				for(b=28;b<=36;b++)  // branching instruction
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='B';
						break;
					}
				}
				
				
				for(b=37;b<=40;b++) // machine control instruction
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='C';
					}
				}
		

				
				switch(state)
				{
					case 'A':
						flag1=0;
						token = strtok(NULL,s); // 1st token
						if(token!=NULL){
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{
									regs[m].used=0;
									token = strtok(NULL,s); // 2nd token
									if(token!=NULL){
										if((token[0]==91)&&(id!=13)&&(token[strlen(token)-1]==93))
										{
											flag1=1;
											token++;
											token[strlen(token)-1]='\0';
											token2 = strtok(NULL,s);
											if(token2 != NULL){
												state='X';
												break;
											}
										}
										
										for(m1=0;m1<64;m1++)
										{
											if((strcmp(token,regs[m1].regi)==0))
											{
												regs[m1].used=0;
												if(flag1==1)
													state='F';
												token = strtok(NULL,s);// 3rd token
												if((token==NULL)&&(id==13))
													state='X';
												if((token!=NULL)){
													for(m2=0;m2<64;m2++){
														if((strcmp(token,regs[m2].regi)==0)){
															regs[m2].used=0;
															state='F';
															break;
														}
														else{
															flag=0;
															state='X';
														}
													}
													token = strtok(NULL,s);
													if(token!=NULL){state='X';break;}
												}
												break;
											}
										}
									}break;
								}
							}
						}
						break;
					case 'T':
						
						token = strtok(NULL,s); // 1st token
						if(token!=NULL){
							if(token[0]==91)
							{
								if(token[strlen(token)-1]!=93)
								{
									state='X';
								}
								token++;
								token[strlen(token)-1]='\0';
							}
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{
									regs[m].used=0;
									token = strtok(NULL,s); // 2nd token
									if(token!=NULL){
										if(token[0]==91)
										{
											if(token[strlen(token)-1]!=93)
											{
												state='X';
											}
											token++;
											token[strlen(token)-1]='\0';
										}
										if(token[0]==35)
										{
											if(strlen(token)!=1)
												strcpy(lbl[lb++].labels,token);
											else{
												state='X';
												break;
											}
										}
										for(m1=0;m1<64;m1++)
										{
											
											if((strcmp(token,regs[m1].regi)==0)||(token[0]==35))
											{
												if((m1<64)&&(token[0]!=35))
												{
													regs[m1].used=0;
												}
												state='F';
												break;
											}
										}
										token = strtok(NULL,s);
										if(token!=NULL){state='X';break;}
										
									}break;
								}
							}
						}	
						break;
					case 'O':
						flag=0;
						token = strtok(NULL,s); // 1st token
						if(token!=NULL){
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{
									regs[m].used=0;
									token = strtok(NULL,s); // 2nd token
									if(token!=NULL){
										if(token[0]==91&&(token[strlen(token)-1]==93))
										{
											token++;
											token[strlen(token)-1]='\0';
											flag=2;
											token1 = strtok(NULL,s);
											if(token1 != NULL){state='X';break;}
										}
										for(m1=0;m1<64;m1++)
										{
											if((strcmp(token,regs[m1].regi)==0))
											{
												regs[m1].used=0;
												token = strtok(NULL,s);// 3rd token
												if(token!=NULL){
													for(m2=0;m2<64;m2++){
														if((strcmp(token,regs[m2].regi)==0)){
															regs[m2].used=0;
															state='F';
															break;
														}
														else{
															flag=0;
															state='X';
														}
													}
													token = strtok(NULL,s);
													if(token!=NULL){state='X';break;}
												}
												else
													state='X';
												if(flag!=0){
													state='F';
												}break;
											}
											else
											{
												if((checkHex(token)==1)){
													state='F';
													token1 = strtok(NULL,s);
													if(token1 != NULL){state='X';break;}
												}
											}
										}
									}break;
								}
							}
						}
						break;
					case 'B':
						
						token = strtok(NULL,s); // 1st token-------
						if(token!=NULL){
							state='F';
						}
						token = strtok(NULL,s);
						if(token!=NULL){state='X';}
							
						break;

					case 'P':
						token = strtok(NULL,s); // 1st token
						if(token!=NULL){
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{	
										regs[m].used=0;
										token = strtok(NULL,s); //2nd token
										if(token!=NULL){
											for(m1=0;m1<64;m1++)
											{
												if((strcmp(token,regs[m1].regi)==0))
												{
													regs[m1].used=0;
													state='F';
												}	
											}
										}
				
										break;
								}
							}
						}
						
					token = strtok(NULL,s);
					if(token!=NULL){state='X';}
					break;

					case 'I':
						
						token = strtok(NULL,s); // 1st token
						if(token!=NULL){
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{	
										regs[m].used=0;
										break;
								}
							}
						}
						token = strtok(NULL,s); // 2nd token
						if(token!=NULL){
							if(checkHex(token)==1){
								state='F';
							}
						}
						token = strtok(NULL,s);
						if(token!=NULL){state='X';}
							
						break;
					case 'R':
						
						token = strtok(NULL,s); // 1st token
						if(token!=NULL){
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{	
										regs[m].used=0;
										state='F';
										break;
								}
							}
						}
						token = strtok(NULL,s);
						if(token!=NULL){state='X';}
						break;

					case 'N':
						token = strtok(NULL,s); // 1st token
						if(token!=NULL){
							if(token[0]==91)
								{
									token++;
									token[strlen(token)-1]='\0';
								}
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{	
										regs[m].used=0;
										state='F';
										break;
								}
							}
						}
						token = strtok(NULL,s);
						if(token!=NULL){state='X';}
						break;

					case 'C':
						state='F';
						token = strtok(NULL,s);
						if(token!=NULL)
							state='X';
							
						break;
					default:
						state='X';	
				}	
				token = strtok(NULL,s);
				if(state!='F')
				{
					printf("\nerror in line : %d",l+1);
					errorflag=1;
				}
				break;
			}
		}
		l++;
	}



	for(m1=0;m1<lb;m1++)
	{
		if(lbl[m1].labels[0]==35)
		{
			lbl[m1].mad=madd;
			madd+=32;
		}
	}

	for(m1=0;m1<lb;m1++)
	{
			//printf("Labels :: %s \t address :: %ld\n",lbl[m1].labels,lbl[m1].mad);
			fprintf(opfp,"%s\t%ld\n",lbl[m1].labels,lbl[m1].mad);
	}

	fclose(fp);
	fclose(opfp);
	strcpy(token3,"pass2.exe ");
	strcat(token3,argv[1]);
	if(errorflag==0)
		system(token3);
	exit(0);
	}
Exemplo n.º 10
0
int main (int argc, char *argv[]) 
{
   int sts;
   int i;
   int buflen;
   int access;
   char buff [BUFFER_SIZE];
   sip_ticket_t ticket;

   extern char *optarg;         /* Defined in libc getopt and unistd.h */
   int ch1;
   
   char configfile[64]="siproxd";       /* basename of configfile */
   int  config_search=1;                /* search the config file */
   int  cmdline_debuglevel=0;
   char *pidfilename=NULL;
   struct sigaction act;

   log_set_stderr(1);

/*
 * setup signal handlers
 */
   act.sa_handler=sighandler;
   sigemptyset(&act.sa_mask);
   act.sa_flags=SA_RESTART;
   if (sigaction(SIGTERM, &act, NULL)) {
      ERROR("Failed to install SIGTERM handler");
   }
   if (sigaction(SIGINT, &act, NULL)) {
      ERROR("Failed to install SIGINT handler");
   }
   if (sigaction(SIGUSR2, &act, NULL)) {
      ERROR("Failed to install SIGUSR2 handler");
   }


/*
 * prepare default configuration
 */
   make_default_config();

   log_set_pattern(configuration.debuglevel);

/*
 * parse command line
 */
{
#ifdef  HAVE_GETOPT_LONG
   int option_index = 0;
   static struct option long_options[] = {
      {"help", no_argument, NULL, 'h'},
      {"config", required_argument, NULL, 'c'},
      {"debug", required_argument, NULL, 'd'},
      {"pid-file", required_argument, NULL,'p'},
      {0,0,0,0}
   };

    while ((ch1 = getopt_long(argc, argv, "hc:d:p:",
                  long_options, &option_index)) != -1) {
#else   /* ! HAVE_GETOPT_LONG */
    while ((ch1 = getopt(argc, argv, "hc:d:p:")) != -1) {
#endif
      switch (ch1) {
      case 'h': /* help */
         DEBUGC(DBCLASS_CONFIG,"option: help");
         fprintf(stderr,str_helpmsg);
         exit(0);
         break;

      case 'c': /* load config file */
         DEBUGC(DBCLASS_CONFIG,"option: config file=%s",optarg);
         i=sizeof(configfile)-1;
         strncpy(configfile,optarg,i-1);
         configfile[i]='\0';
         config_search=0;
         break; 

      case 'd': /* set debug level */
         DEBUGC(DBCLASS_CONFIG,"option: set debug level: %s",optarg);
         cmdline_debuglevel=atoi(optarg);
         log_set_pattern(cmdline_debuglevel);
         break;

      case 'p':
         pidfilename = optarg;
         break;

      default:
         DEBUGC(DBCLASS_CONFIG,"no command line options");
         break; 
      }
   }
}

/*
 * Init stuff
 */
   INFO(PACKAGE"-"VERSION"-"BUILDSTR" "UNAME" starting up");

   /* read the config file */
   if (read_config(configfile, config_search) == STS_FAILURE) exit(1);

   /* if a debug level > 0 has been given on the commandline use its
      value and not what is in the config file */
   if (cmdline_debuglevel != 0) {
      configuration.debuglevel=cmdline_debuglevel;
   }

/*
 * open a the pwfile instance, so we still have access after
 * we possibly have chroot()ed to somewhere.
 */
   if (configuration.proxy_auth_pwfile) {
      siproxd_passwordfile = fopen(configuration.proxy_auth_pwfile, "r");
   } else {
      siproxd_passwordfile = NULL;
   }

   /* set debug level as desired */
   log_set_pattern(configuration.debuglevel);
   log_set_listen_port(configuration.debugport);

   /* change user and group IDs */
   secure_enviroment();

   /* daemonize if requested to */
   if (configuration.daemonize) {
      DEBUGC(DBCLASS_CONFIG,"daemonizing");
      if (fork()!=0) exit(0);
      setsid();
      if (fork()!=0) exit(0);

      log_set_stderr(0);
      INFO("daemonized, pid=%i", getpid());
   }

   /* write PID file of main thread */
   if (pidfilename == NULL) pidfilename = configuration.pid_file;
   if (pidfilename) {
      FILE *pidfile;
      DEBUGC(DBCLASS_CONFIG,"creating PID file [%s]", pidfilename);
      sts=unlink(configuration.pid_file);
      if ((sts==0) ||(errno == ENOENT)) {
         if ((pidfile=fopen(pidfilename, "w"))) {
            fprintf(pidfile,"%i\n",(int)getpid());
            fclose(pidfile);
         } else {
            WARN("couldn't create new PID file: %s", strerror(errno));
         }
      } else {
         WARN("couldn't delete old PID file: %s", strerror(errno));
      }
   }

   /* initialize the RTP proxy */
   sts=rtpproxy_init();
   if (sts != STS_SUCCESS) {
      ERROR("unable to initialize RTP proxy - aborting"); 
      exit(1);
   }

   /* init the oSIP parser */
   parser_init();

   /* listen for incoming messages */
   sts=sipsock_listen();
   if (sts == STS_FAILURE) {
      /* failure to allocate SIP socket... */
      ERROR("unable to bind to SIP listening socket - aborting"); 
      exit(1);
   }

   /* initialize the registration facility */
   register_init();

/*
 * silence the log - if so required...
 */
   log_set_silence(configuration.silence_log);

   INFO(PACKAGE"-"VERSION"-"BUILDSTR" "UNAME" started");

/*****************************
 * Main loop
 *****************************/
   while (!exit_program) {

      DEBUGC(DBCLASS_BABBLE,"going into sipsock_wait\n");
      while (sipsock_wait()<=0) {
         /* got no input, here by timeout. do aging */
         register_agemap();

         /* TCP log: check for a connection */
         log_tcp_connect();

         /* dump memory stats if requested to do so */
         if (dmalloc_dump) {
            dmalloc_dump=0;
#ifdef DMALLOC
            INFO("SIGUSR2 - DMALLOC statistics is dumped");
            dmalloc_log_stats();
            dmalloc_log_unfreed();
#else
            INFO("SIGUSR2 - DMALLOC support is not compiled in");
#endif
         }

         if (exit_program) goto exit_prg;
      }

      /*
       * got input, process
       */
      DEBUGC(DBCLASS_BABBLE,"back from sipsock_wait");
      ticket.direction=0;

      buflen=sipsock_read(&buff, sizeof(buff)-1, &ticket.from,
                           &ticket.protocol);
      buff[buflen]='\0';

      /*
       * evaluate the access lists (IP based filter)
       */
      access=accesslist_check(ticket.from);
      if (access == 0) {
         DEBUGC(DBCLASS_ACCESS,"access for this packet was denied");
         continue; /* there are no resources to free */
      }

      /*
       * integrity checks
       */
      sts=security_check_raw(buff, buflen);
      if (sts != STS_SUCCESS) {
         DEBUGC(DBCLASS_SIP,"security check (raw) failed");
         continue; /* there are no resources to free */
      }

      /*
       * init sip_msg
       */
      sts=osip_message_init(&ticket.sipmsg);
      ticket.sipmsg->message=NULL;
      if (sts != 0) {
         ERROR("osip_message_init() failed... this is not good");
         continue; /* skip, there are no resources to free */
      }

      /*
       * RFC 3261, Section 16.3 step 1
       * Proxy Behavior - Request Validation - Reasonable Syntax
       * (parse the received message)
       */
      sts=sip_message_parse(ticket.sipmsg, buff, buflen);
      if (sts != 0) {
         ERROR("sip_message_parse() failed... this is not good");
         DUMP_BUFFER(-1, buff, buflen);
         goto end_loop; /* skip and free resources */
      }

      /*
       * integrity checks - parsed buffer
       */
      sts=security_check_sip(&ticket);
      if (sts != STS_SUCCESS) {
         ERROR("security_check_sip() failed... this is not good");
         DUMP_BUFFER(-1, buff, buflen);
         goto end_loop; /* skip and free resources */
      }

      /*
       * RFC 3261, Section 16.3 step 2
       * Proxy Behavior - Request Validation - URI scheme
       * (check request URI and refuse with 416 if not understood)
       */
      /* NOT IMPLEMENTED */

      /*
       * RFC 3261, Section 16.3 step 3
       * Proxy Behavior - Request Validation - Max-Forwards check
       * (check Max-Forwards header and refuse with 483 if too many hops)
       */
      {
         osip_header_t *max_forwards;
         int forwards_count = DEFAULT_MAXFWD;

         osip_message_get_max_forwards(ticket.sipmsg, 0, &max_forwards);
         if (max_forwards && max_forwards->hvalue) {
            forwards_count = atoi(max_forwards->hvalue);
         }

         DEBUGC(DBCLASS_PROXY,"checking Max-Forwards (=%i)",forwards_count);
         if (forwards_count <= 0) {
            DEBUGC(DBCLASS_SIP, "Forward count reached 0 -> 483 response");
            sip_gen_response(&ticket, 483 /*Too many hops*/);
            goto end_loop; /* skip and free resources */
         }
      }

      /*
       * RFC 3261, Section 16.3 step 4
       * Proxy Behavior - Request Validation - Loop Detection check
       * (check for loop and return 482 if a loop is detected)
       */
      if (check_vialoop(&ticket) == STS_TRUE) {
         /* make sure we don't end up in endless loop when detecting
          * an loop in an "loop detected" message - brrr */
         if (MSG_IS_RESPONSE(ticket.sipmsg) && 
             MSG_TEST_CODE(ticket.sipmsg, 482)) {
            DEBUGC(DBCLASS_SIP,"loop in loop-response detected, ignoring");
         } else {
            DEBUGC(DBCLASS_SIP,"via loop detected, ignoring request");
            sip_gen_response(&ticket, 482 /*Loop detected*/);
         }
         goto end_loop; /* skip and free resources */
      }

      /*
       * RFC 3261, Section 16.3 step 5
       * Proxy Behavior - Request Validation - Proxy-Require check
       * (check Proxy-Require header and return 420 if unsupported option)
       */
      /* NOT IMPLEMENTED */

      /*
       * RFC 3261, Section 16.5
       * Proxy Behavior - Determining Request Targets
       */
      /* NOT IMPLEMENTED */

      DEBUGC(DBCLASS_SIP,"received SIP type %s:%s",
             (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES",
             (MSG_IS_REQUEST(ticket.sipmsg) ?
                ((ticket.sipmsg->sip_method)?
                   ticket.sipmsg->sip_method : "NULL") :
                ((ticket.sipmsg->reason_phrase) ? 
                   ticket.sipmsg->reason_phrase : "NULL")));

      /*********************************
       * The message did pass all the
       * tests above and is now ready
       * to be proxied.
       * Before we do so, we apply some
       * additional preprocessing
       *********************************/
      /* Dial shortcuts */
      if (configuration.pi_shortdial) {
         sts = plugin_shortdial(&ticket);
         if (sts == STS_SIP_SENT) goto end_loop;
      }


      /*********************************
       * finally proxy the message.
       * This includes the masquerading
       * of the local UA and starting/
       * stopping the RTP proxy for this
       * call
       *********************************/

      /*
       * if a REQ REGISTER, check if it is directed to myself,
       * or am I just the outbound proxy but no registrar.
       * - If I'm the registrar, register & generate answer
       * - If I'm just the outbound proxy, register, rewrite & forward
       */
      if (MSG_IS_REGISTER(ticket.sipmsg) && 
          MSG_IS_REQUEST(ticket.sipmsg)) {
         if (access & ACCESSCTL_REG) {
            osip_uri_t *url;
            struct in_addr addr1, addr2, addr3;
            int dest_port;

            url = osip_message_get_uri(ticket.sipmsg);
            dest_port= (url->port)?atoi(url->port):SIP_PORT;

            if ( (get_ip_by_host(url->host, &addr1) == STS_SUCCESS) &&
                 (get_interface_ip(IF_INBOUND,&addr2) == STS_SUCCESS) &&
                 (get_interface_ip(IF_OUTBOUND,&addr3) == STS_SUCCESS)) {

               if ((configuration.sip_listen_port == dest_port) &&
                   ((memcmp(&addr1, &addr2, sizeof(addr1)) == 0) ||
                    (memcmp(&addr1, &addr3, sizeof(addr1)) == 0))) {
                  /* I'm the registrar, send response myself */
                  sts = register_client(&ticket, 0);
                  sts = register_response(&ticket, sts);
               } else {
                  /* I'm just the outbound proxy */
                  DEBUGC(DBCLASS_SIP,"proxying REGISTER request to:%s",
                         url->host);
                  sts = register_client(&ticket, 1);
                  if (sts == STS_SUCCESS) {
                     sts = proxy_request(&ticket);
                  }
               }
            } else {
               sip_gen_response(&ticket, 408 /*request timeout*/);
            }
         } else {
            WARN("non-authorized registration attempt from %s",
                 utils_inet_ntoa(ticket.from.sin_addr));
         }

      /*
       * check if outbound interface is UP.
       * If not, send back error to UA and
       * skip any proxying attempt
       */
      } else if (get_interface_ip(IF_OUTBOUND,NULL) !=
                 STS_SUCCESS) {
         DEBUGC(DBCLASS_SIP, "got a %s to proxy, but outbound interface "
                "is down", (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES");

         if (MSG_IS_REQUEST(ticket.sipmsg))
            sip_gen_response(&ticket, 408 /*request timeout*/);
      
      /*
       * MSG is a request, add current via entry,
       * do a lookup in the URLMAP table and
       * send to the final destination
       */
      } else if (MSG_IS_REQUEST(ticket.sipmsg)) {
         if (access & ACCESSCTL_SIP) {
            sts = proxy_request(&ticket);
         } else {
            INFO("non-authorized request received from %s",
                 utils_inet_ntoa(ticket.from.sin_addr));
         }

      /*
       * MSG is a response, remove current via and
       * send to the next VIA in chain
       */
      } else if (MSG_IS_RESPONSE(ticket.sipmsg)) {
         if (access & ACCESSCTL_SIP) {
            sts = proxy_response(&ticket);
         } else {
            INFO("non-authorized response received from %s",
                 utils_inet_ntoa(ticket.from.sin_addr));
         }
         
      /*
       * unsupported message
       */
      } else {
         ERROR("received unsupported SIP type %s %s",
               (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES",
               ticket.sipmsg->sip_method);
      }

      /*********************************
       * Done with proxying. Message
       * has been sent to its destination.
       *********************************/
/*
 * free the SIP message buffers
 */
      end_loop:
      osip_message_free(ticket.sipmsg);

   } /* while TRUE */
   exit_prg:

   /* save current known SIP registrations */
   register_save();
   INFO("properly terminating siproxd");

   /* remove PID file */
   if (pidfilename) {
      DEBUGC(DBCLASS_CONFIG,"deleting PID file [%s]", pidfilename);
      sts=unlink(pidfilename);
      if (sts != 0) {
         WARN("couldn't delete old PID file: %s", strerror(errno));
      }
   }

   /* END */
   return 0;
} /* main */

/*
 * Signal handler
 *
 * this one is called asynchronously whevener a registered
 * signal is applied. Just set a flag and don't do any funny
 * things here.
 */
static void sighandler(int sig) {
   if (sig==SIGTERM) exit_program=1;
   if (sig==SIGINT)  exit_program=1;
   if (sig==SIGUSR2) dmalloc_dump=1;
   return;
}
Exemplo n.º 11
0
int main(int argc, char *argv[])
{
	FILE *fp,*opfp,*omc,*mc,*testf;
	int test,n,c,ln,i,m,m1,m2,b,flag=0,len,last,digit=0,var,id=0,f,flen=0;
	long int madd=0;
	char ch,*nem[4],*str,*token,*s=" ",state,*str1,*s1="\t",*token1,*temp,*token2,c1;//*labels[10]

	str = (char *)malloc(sizeof(char)*50);
	str1 = (char *)malloc(sizeof(char)*50);
	token1 = (char *)malloc(sizeof(char)*50);
	token2 = (char *)malloc(sizeof(char)*50);
	register_init();
	instr_init();
	opfp = fopen("stable.txt","r"); 
	if(opfp == NULL) 
	{
		perror("Error while opening the file.\n");
		exit(EXIT_FAILURE);
	}

	//printf("file name %s\n", argv[1]);
	fp = fopen(argv[1],"r"); // opens file
	testf = fopen(argv[1],"r");
	omc = fopen("mcode.txt","w"); 
	mc= fopen("onlymcode.txt","w"); 

	if(fp == NULL)
	{
		perror("Error while opening the file.\n");
		exit(EXIT_FAILURE);
	}
	

	label lbl[10];

	for(m1=0;m1<10;m1++)
	{
		lbl[m1].labels = (char *)malloc(sizeof(char)*10);
	}

	for(m1=0;m1<10;m1++)
	{
		lbl[m1].mad = (char *)malloc(sizeof(char)*10);
	}
	
	while( fgets(str1,50,opfp)!=NULL) 
		{
			token=strtok(str1,s1);
			while(token!=NULL)
			{
				strcpy(lbl[lb].labels,token);
				token=strtok(NULL,s1);
				strcpy(lbl[lb].mad,token);
				token=strtok(NULL,s1);
			}++lb;	
		}

	/*for(m1=0;m1<lb;m1++)
	{
		printf("Labels :: %s \t address :: %s\n",lbl[m1].labels,lbl[m1].mad);
	}
	*/
	for (c1 = getc(testf); c1 != EOF; c1 = getc(testf)){
        if (c1 == '\n') // Increment count if this character is newline
            flen++;
	}
	fclose(testf);
	while( fgets(str,50,fp)!=NULL) //line reading
	{
	
		state='X';
		if(flen==l){
			strcat(str,"\0");
		}
		if((str!="")&&((flen!=l)))
			str[strlen(str)-1]='\0';
		
		
		if(l>0)
		{
			//printf( "\n%00000007ld\t%s",madd,str );
			fprintf(omc,"\n%00000007ld\t%s",madd,str );
			madd+=32;
			state='X';
			token = strtok(str,s); // instruction name
			
			len=strlen(token);
			last=token[len-1];
			if(last==58)
			{
				token = strtok(NULL,s);
			}

			while( token != NULL )
   			{

				for(b=1;b<=4;b++) // MOV
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='M';
						id=b;
						break;
					}
				}
				
				for(b=5;b<=8;b++)  // direct address
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='I';
						//printf("\nMCODE : %s ",ins[b].mcode);
						fprintf(omc,"\nMCODE : %s ",ins[b].mcode);
						fprintf(mc,"\n%s",ins[b].mcode);
						break;
					}
				}
				
				for(b=9;b<=10;b++)  // ADD
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='A';
						id=b;
						break;
					}
				}
				
				for(b=11;b<=13;b++)  // MUL 
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='A';
						id=b;
						break;
					}
				}
				
				for(b=14;b<=15;b++)  // AND
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='A';
						id=b;
						break;
					}
				}

				for(b=16;b<=18;b++)  // OR
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='O';
						break;
					}
				}

				for(b=19;b<=20;b++)  // NOT
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='N';
						id=b;
						break;
					}
				}

				for(b=21;b<=26;b++)  // type INS r1
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						//printf("\nMCODE : %s ",ins[b].mcode);
						fprintf(omc,"\nMCODE : %s ",ins[b].mcode);
						fprintf(mc,"\n%s",ins[b].mcode);
						state='R';
						break;
					}
				}

				if(strcmp(token,(ins[27].nemo))==0) // compare CMP
				{
					id=28;
					state='M';
				}
				for(b=28;b<=36;b++)  // branching instruction JMP
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='B';
						//printf("\nMCODE : %s ",ins[b].mcode);
						fprintf(omc,"\nMCODE : %s ",ins[b].mcode);
						fprintf(mc,"\n%s",ins[b].mcode);
						break;
					}
				}

				for(b=37;b<=40;b++) // machine control instruction
				{
					if(strcmp(token,(ins[b].nemo))==0)
					{
						state='C';
						//printf("\nMCODE : %s  ",ins[b].mcode);
						fprintf(omc,"\nMCODE : %s\n",ins[b].mcode);
						fprintf(mc,"\n%s",ins[b].mcode);
					}
				}	
				
				switch(state)
				{
	
					case 'M':
						token = strtok(NULL,s); // 1st token-------
						if(token!=NULL){
							if(token[0]==91)
							{
								token++;
								token[strlen(token)-1]='\0';
								f=4;
							}
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{	
									strcpy(token1,regs[m].address);
									strcpy(token2,regs[m].address);
									strcat(token1," ");
									token = strtok(NULL,s); // 2nd token-------
									if(token!=NULL){
										if(token[0]==91)
										{
											token++;
											token[strlen(token)-1]='\0';
											for(m1=0;m1<64;m1++)
											{
												if((strcmp(token,regs[m1].regi)==0))
												{
													strcat(token1,regs[m1].address);
													strcat(token2,regs[m1].address);
													//printf("\nMCODE : %s %s",ins[3].mcode,token1);
													fprintf(omc,"\nMCODE : %s %s\n",ins[3].mcode,token1);
													fprintf(mc,"\n%s%s",ins[3].mcode,token2);
													break;
												}
											}
											token="xx";
										}
										for(m1=0;m1<64;m1++)
										{
											if((strcmp(token,regs[m1].regi)==0))
											{
												if(f!=4)
												{
													strcat(token1,regs[m1].address);
													strcat(token2,regs[m1].address);
													//printf("\nMCODE : %s %s",ins[id].mcode,token1);
													fprintf(omc,"\nMCODE : %s %s\n",ins[id].mcode,token1);
													fprintf(mc,"\n%s%s",ins[id].mcode,token2);
													state='F';
												}
												if(f==4)
												{
													strcat(token1,regs[m1].address);
													strcat(token2,regs[m1].address);
													//printf("\nMCODE : %s %s",ins[4].mcode,token1);
													fprintf(omc,"\nMCODE : %s %s\n",ins[4].mcode,token1);
													fprintf(mc,"\n%s%s",ins[4].mcode,token2);
													f=0;
													state='F';
												}
												break;
											}
											else
											{
												if(token[0]==35)
												{
													for(var=0;var<lb;var++)
													{
														if(strcmp(lbl[var].labels,token)==0)
														{
															lbl[var].mad[strlen(lbl[var].mad)-1]='\0';
															strcat(token1,appendLeft(toBin(atoi(lbl[var].mad))));
															strcat(token2,appendLeft(toBin(atoi(lbl[var].mad))));
															//printf("\nMCODE : %s %s",ins[++id].mcode,token1);
															fprintf(omc,"\nMCODE : %s %s\n",ins[id].mcode,token1);
															fprintf(mc,"\n%s%s",ins[id].mcode,token2);
															token++;
															break;
														}
													}
												}
											}
											
										}
										token = strtok(NULL,s);
										if(token!=NULL){state='X';break;}
										
									}break;
								}
							}
						}	
						
						break;
					case 'I':
						
							token = strtok(NULL,s); // 1st token-------
							if(token!=NULL){
								for(m=0;m<64;m++)
								{
									if((strcmp(token,regs[m].regi)==0))
									{	
										//printf("%s ",regs[m].address);
										fprintf(omc,"%s ",regs[m].address);
										fprintf(mc,"%s",regs[m].address);
										break;
									}
								}
							}
							token = strtok(NULL,s); // 2nd token-------
							//printf("%s",hextoBin(token));
							fprintf(omc,"%s\n",hextoBin(token));
							fprintf(mc,"%s",hextoBin(token));
						break;
					case 'A':
							token = strtok(NULL,s);//1st token
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{
									strcpy(token1,regs[m].address);
									strcpy(token2,regs[m].address);
									strcat(token1," ");
									token = strtok(NULL,s);//2nd token
									if(token[0]==91)
									{
										token++;
										token[strlen(token)-1]='\0';
										id++;
										f=6;
									}
									for(m1=0;m1<64;m1++)
									{
										if((strcmp(token,regs[m1].regi)==0))
										{
											strcat(token1,regs[m1].address);
											strcat(token2,regs[m1].address);
											if(f==6){
												//printf("\nMCODE : %s %s",ins[id].mcode,token1);
												fprintf(omc,"\nMCODE : %s %s\n",ins[id].mcode,token1);
												fprintf(mc,"\n%s%s",ins[id].mcode,token2);
												f=0;
												break;
											}
											token = strtok(NULL,s);//3rd token
											for(m2=0;m2<64;m2++)
											{
												if((strcmp(token,regs[m2].regi)==0))
												{
													strcat(token1," ");
													strcat(token1,regs[m2].address);
													strcat(token2,regs[m2].address);
													//printf("\nMCODE : %s %s",ins[id].mcode,token1);
													fprintf(omc,"\nMCODE : %s %s\n",ins[id].mcode,token1);
													fprintf(mc,"\n%s%s",ins[id].mcode,token2);
													break;
												}
											}
											break;
										}
									}
									break;
								}
							}
						break;
					case 'O':
							token = strtok(NULL,s);//1st token
							for(m=0;m<64;m++)
							{
								if((strcmp(token,regs[m].regi)==0))
								{
									strcpy(token1,regs[m].address);
									strcpy(token2,regs[m].address);
									strcat(token1," ");
									token = strtok(NULL,s);//2nd token
									if(token[0]==91)
									{
										token++;
										token[strlen(token)-1]='\0';
										id++;
										id++;
										f=6;
									}
									if(checkHex(token)==1)	
									{
										id++;
										//printf("\nMCODE : %s %s %s",ins[id].mcode,token1,hextoBin(token));
										fprintf(omc,"\nMCODE : %s %s %s\n",ins[id].mcode,token1,hextoBin(token));
										fprintf(mc,"\n%s%s%s",ins[id].mcode,token2,hextoBin(token));
										break;
									}
									for(m1=0;m1<64;m1++)
									{
										if((strcmp(token,regs[m1].regi)==0))
										{
											strcat(token1,regs[m1].address);
											strcat(token2,regs[m1].address);
											strcat(token1," ");
											if(f==6){
												//printf("\nMCODE : %s %s",ins[id].mcode,token1);
												fprintf(omc,"\nMCODE : %s %s\n",ins[id].mcode,token1);
												fprintf(mc,"\n%s%s",ins[id].mcode,token2);
												f=0;
												break;
											}
											token = strtok(NULL,s);//3rd token
											for(m2=0;m2<64;m2++)
											{
												if((strcmp(token,regs[m2].regi)==0))
												{
													//puts(token);
													strcat(token1,regs[m2].address);//puts(token1);
													strcat(token2,regs[m2].address);
													//printf("\nMCODE : %s %s",ins[id].mcode,token1);
													fprintf(omc,"\nMCODE : %s %s\n",ins[id].mcode,token1);
													fprintf(mc,"\n%s%s",ins[id].mcode,token2);
													break;
												}
											}
											break;
										}
									}
									break;
								}
							}
						break;
					case 'N':
							token = strtok(NULL,s); // 1st token-------
							if(token!=NULL){
								if(token[0]==91)
								{
									token++;
									token[strlen(token)-1]='\0';
									id++;
								}
								for(m=0;m<64;m++)
								{
									if((strcmp(token,regs[m].regi)==0))
									{	
										//printf("\nMCODE : %s %s",ins[id].mcode,regs[m].address);
										fprintf(omc,"\nMCODE : %s %s\n",ins[id].mcode,regs[m].address);
										fprintf(mc,"\n%s%s",ins[id].mcode,regs[m].address);
										break;
									}
								}
							}
						break;
					case 'R':
							token = strtok(NULL,s); // 1st token-------
							if(token!=NULL){
								for(m=0;m<64;m++)
								{
									if((strcmp(token,regs[m].regi)==0))
									{	
										//printf("%s",regs[m].address);
										fprintf(omc,"%s\n",regs[m].address);
										fprintf(mc,"%s",regs[m].address);
										break;
									}
								}
							}
						break;
					case 'B':
						token = strtok(NULL,s); // 1st token-------
						if(token!=NULL){
							for(m1=0;m1<lb;m1++)
							{
								if(strcmp(lbl[m1].labels,token)==0)
								{
									lbl[m1].mad[strlen(lbl[m1].mad)-1]='\0';
									//printf("%s",appendLeft(toBin(atoi(lbl[m1].mad))));
									fprintf(omc,"%s\n",appendLeft(toBin(atoi(lbl[m1].mad))));
									fprintf(mc,"%s",appendLeft(toBin(atoi(lbl[m1].mad))));
									break;
								}
									if(m1==lb-1)
									{
										printf("\nError in code!!No lebel found in jump instruction at line %d !! Exit from program !!",l+1);
										exit(0);
									}
							}
							if(lb==0)
							{
								printf("\nError in code!!No lebel found in jump instruction at line %d !! Exit from program !!",l+1);
								exit(0);
							}
						}
						break;

					default:
						state='X';	
				}	
				token = strtok(NULL,s);
				break;
			}
		}
		l++;
	}
	printf("compilation successful :D ");
	fclose(fp);
	fclose(opfp);
	fclose(mc);
	append();
	return 0;
}
Exemplo n.º 12
0
/*==================================================================================================

FUNCTION: Uart_init

DESCRIPTION:

==================================================================================================*/
UartResult Uart_init(UartHandle* h,UartPortID id)
{
   UART_CONTEXT* uart_ctxt;

   UART_LOG_0(INFO,"+Uart_init");

   if (NULL == h)
   {
      UART_LOG_0(ERROR, "Calling Uart_init with NULL handle pointer.");
      return UART_ERROR;
   }

   *h = NULL;

   if(UART_ERROR == Uart_get_driver_properties())
   {
      UART_LOG_0(ERROR, "Uart_get_driver_properties failed");
      return UART_ERROR;
   }

   if (id < UART_MAX_PORTS)
   {
      uart_ctxt = &static_uart_context[id];

      // In SBL, all the drivers, that needs to log using UART, will directly call the UART APIs
      // They need to call the Uart_init function before using any of the APIs.
      // So initialize the UART only on the first call. From the second call just return the handle
      // and log it.
      if (uart_ctxt->is_port_open == TRUE)
      {
         *h = (UartHandle)uart_ctxt;
         UART_LOG_1(INFO, "Port is already open. Port: %d", id);
         return UART_SUCCESS;
      }

      if (NULL == uart_driver_props.device_names[id])
      {
         UART_LOG_1(ERROR, "Physical device is not defined in UART properties XML. Port: %d", id);
         return UART_ERROR;
      }
      uart_ctxt->port_id = uart_driver_props.device_names[id];
      uart_ctxt->is_port_open = TRUE;
      uart_ctxt->read_buf = NULL;
   }
   else
   {
      UART_LOG_1(ERROR, "Invalid Port ID. Port: %d", id);
      return UART_ERROR;
   }

   if(UART_SUCCESS != Uart_get_properties(uart_ctxt))
   {
      UART_LOG_0(ERROR, "Uart_get_properties failed.");
      return UART_ERROR;
   }
   if(UART_SUCCESS != clock_enable(uart_ctxt))
   {
      UART_LOG_0(ERROR, "clock_enable failed.");
      return UART_ERROR;
   }
   if(UART_SUCCESS != Uart_tlmm_open(uart_ctxt))
   {
      UART_LOG_0(ERROR, "Uart_tlmm_open failed.");
      Uart_deinit((UartHandle)uart_ctxt);
      return UART_ERROR;
   }
   if(UART_SUCCESS != Uart_interrupt_open(uart_ctxt,(void*)uart_isr))
   {
      UART_LOG_0(ERROR, "Uart_interrupt_open failed.");
      Uart_deinit((UartHandle)uart_ctxt);
      return UART_ERROR;
   }

   register_init(uart_ctxt);

   *h = (UartHandle)uart_ctxt;

   UART_LOG_0(INFO,"-Uart_init");
   return UART_SUCCESS;
}