Esempio n. 1
0
void fetchCycle(){
    if(isON(FETCH_FLAG)){
        fetch();
    if(isOFF(JMPR_FLAG)){
            decodeL();
            turnOFF(FETCH_FLAG);
        }else{
            decodeR(MBR);
            turnOFF(JMPR_FLAG);
        }       
    }else{
        decodeR(IBR);
        turnON(FETCH_FLAG);        
    }

}
Esempio n. 2
0
//The core function of the assembler
int parser(char *input, char *output)
{
	FILE *fin;
	char line[MAX_CHAR_PER_LINE];
	int i=0,j,k;
	char type;
	char str[MAX_LINES][MAX_CHAR_PER_LINE];
	memset(str,'\0',sizeof(str));
	fin=fopen(input,"rt");
	if(fin==NULL) 
 	{ 
		printf("Error on open input file\n"); 
		exit(1); 
 	}
	printf("Please wait ...\n");
//Delete the whitespace and comments first,
// and then store the command into the array str[].
	while(fgets(line,MAX_CHAR_PER_LINE,fin)!=NULL)
	{
		if(line[0]==13)
		continue;
		k=0;
		for(j=0;line[j]!=13;j++)
		{	
			if(line[j]==' '||line[j]=='\t')
			{
				continue;
			}
			else if(line[j]=='/')
			{
				break;
			}
			else
			str[i][k++]=line[j];
		}
		if(k!=0)
		{
			i++;
		}
	}
	fclose(fin);
	// First pass of the str[], adding the symbol into the symboltable
	int rom=0;
	for(int m=0;m<i;m++)
	{
		type=commandtype(str[m]);
		if(type=='s')
		{
			firstpass(str[m],rom);
		}
		else
		rom++;
	}
	//Second pass of the str[], 
	// then decoding the A,C,L commands into 16-bits binary
	// finally printf the 16 bits into the ".hack" file
	FILE *fout;
	fout=fopen(output,"wt");
	char code[i][17];
	memset(code,'\0',sizeof(code));
	for(int m=0;m<i;m++)
	{
		type=commandtype(str[m]);
		if(type=='a')
		decodeA(str[m],code[m]);
		if(type=='c')
		decodeC(str[m],code[m]);
		if (type=='l')
		decodeL(str[m],code[m]);
		if(type=='a'||type=='c'||type=='l')
	 	fprintf(fout, "%s\n", code[m]);
	}
	fclose(fout);
	printf("Process complete\n");
	return 0;
}