Ejemplo n.º 1
0
	void instr_LEA (CPU *cpu)	{
		int dst = bitshift(cpu->instr_reg,11,9,0);		
		int pcoffset = (int)bitshift(cpu->instr_reg,8,0,1);
		cpu->reg[dst] = cpu->pgm_counter+pcoffset;
		printf("LEA R%d %x %x ; Reg[%x] <- %x + %x = %x \n",dst,cpu->pgm_counter,pcoffset,dst,cpu->pgm_counter,pcoffset,cpu->pgm_counter+pcoffset);
		set_condition_code(cpu,cpu->reg[dst]);
	}
Ejemplo n.º 2
0
	void instr_STR (CPU *cpu)	{
		int dst = bitshift(cpu->instr_reg,11,9,0);
		int base = bitshift(cpu->instr_reg,8,6,0);
		int offset = bitshift(cpu->instr_reg,5,0,1);
		cpu->memory[base+offset] = cpu->reg[dst];
		printf("STR R%d R%d %x ; memory[%x+%x] = reg[%x]= %x\n",dst,base,offset,offset,cpu->pgm_counter,dst,cpu->reg[dst]);
	}
Ejemplo n.º 3
0
	void instr_NOT (CPU *cpu)	{
		int dst = bitshift(cpu->instr_reg,11,9,0);
		int src = bitshift(cpu->instr_reg,8,6,0);
		cpu->reg[dst] = ~cpu->reg[src];
		printf("NOT R%d R%d ; reg[%x]<-~reg[%x] = \n",dst,src,dst,src,~cpu->reg[src]);
		set_condition_code(cpu,cpu->reg[dst]);
	}
Ejemplo n.º 4
0
	void instr_LDI (CPU *cpu)	{
		int dst = bitshift(cpu->instr_reg,11,9,0);
		int pcoffset = (int)bitshift(cpu->instr_reg,8,0,1);
		cpu->reg[dst] = cpu->memory[cpu->memory[pcoffset+cpu->pgm_counter]];
		printf("LDI R%d %x %x ; reg[%x] <- memory[memory[%x+%x]] = %x \n",dst,pcoffset,cpu->pgm_counter,dst,pcoffset,cpu->pgm_counter,cpu->memory[cpu->memory[pcoffset+cpu->pgm_counter]]);
		set_condition_code(cpu,cpu->reg[dst]);
	}
        void instr_ST  (CPU *cpu)        {
                int src = bitshift(cpu->instr_reg,11,9,0);
                int pcoffset = bitshift(cpu->instr_reg,8,0,1);
                cpu->memory[cpu->pgm_counter+pcoffset] = cpu->reg[src];

                printf("ST R%d %x %x ; memory[%x] <-reg[%x] = %x,\n",src,cpu->pgm_counter,pcoffset,src,cpu->pgm_counter+pcoffset,cpu->reg[src]);
        }
Ejemplo n.º 6
0
	void instr_LD  (CPU *cpu)	{
		int dst = bitshift(cpu->instr_reg,11,9,0);
		int pcoffset = bitshift(cpu->instr_reg,8,0,1);
		cpu->reg[dst] = cpu->memory[cpu->pgm_counter+pcoffset];
		printf("LD R%d %d ; reg[%x]<- memory[%x] = %x ,\n",dst,pcoffset,dst,cpu->pgm_counter+pcoffset,cpu->memory[cpu->pgm_counter+pcoffset]);
		set_condition_code(cpu,cpu->reg[dst]);
	}
Ejemplo n.º 7
0
	void instr_LDR (CPU *cpu)	{
		int dst = bitshift(cpu->instr_reg,11,9,0);
		int base = bitshift(cpu->instr_reg,8,6,0);
		int offset = (int)bitshift(cpu->instr_reg,5,0,1);
		cpu->reg[dst] = cpu->memory[cpu->reg[base]+offset];
		printf("LDR R%d %x %x ; reg[%x] <-memory[reg[%x]+%x] = %x\n",dst,base,offset,dst,base,offset,cpu->memory[cpu->reg[base]+offset]);
		set_condition_code(cpu,cpu->reg[dst]);
	}
Ejemplo n.º 8
0
  void instr_AND (CPU *cpu) {
		int type = bitshift(cpu->instr_reg,5,5,0);
		int dst = bitshift(cpu->instr_reg,11,9,0);
		int src1 = bitshift(cpu->instr_reg,8,6,0);
		if(type==1){
			int src2 = bitshift(cpu->instr_reg,2,0,0);
			cpu->reg[dst] = cpu->reg[src1]&cpu->reg[src2];
			printf("AND R%d R%d 0 R%d; reg[%x]<-reg[%x]&&reg[%x] = %x\n",dst,src1,src2,dst,src1,src2,cpu->reg[src1]&cpu->reg[src2]);
		}else{
			int imm5 =bitshift(cpu->instr_reg,4,0,0);
			cpu->reg[dst] = cpu->reg[src1]&imm5;
			printf("AND R%d R%d 1 %x;  reg[%x]<-reg[%x]&&%x = %x\n",dst,src1,imm5,dst,src1,imm5,cpu->reg[src1]&imm5);
		}
		set_condition_code(cpu,cpu->reg[dst]);
	}
Ejemplo n.º 9
0
/* /\* } *\/ */
	void instr_ADD (CPU *cpu)	{
		int type = bitshift(cpu->instr_reg,5,5,0);
		int dst = bitshift(cpu->instr_reg,11,9,0);
		int src1 = bitshift(cpu->instr_reg,8,6,0);
		if(type==0){
			int src2 = bitshift(cpu->instr_reg,2,0,0);
			cpu->reg[dst] = cpu->reg[src1]+cpu->reg[src2];
			printf("ADD  R%d R%d 0 R%d;reg[%d] = %x+%x = %x\n",dst,src1,src2,dst,cpu->reg[src1],cpu->reg[src2],cpu->reg[src1]+cpu->reg[src2]);
		}else{
			int imm5 =bitshift(cpu->instr_reg,4,0,1);
			cpu->reg[dst] = cpu->reg[src1]+imm5;
			printf("ADD R%d R%d 1 %d; reg[%d] = %x +%x\n",dst,src1,imm5,dst,cpu->reg[src1],imm5,cpu->reg[src1]+imm5);
		}
		set_condition_code(cpu,cpu->reg[dst]);
	}
Ejemplo n.º 10
0
	void instr_BR  (CPU *cpu)	{
		int mask = bitshift(cpu->instr_reg,11,9,0);
		char maskcode[30];
		switch(mask){
			case 0 :
				strcpy(maskcode,"NOP");

				break;
			case 1 :
				strcpy(maskcode,"NOP");

				break;
			case 2 :
				strcpy(maskcode,"Z '=0'");

				break;
			case 3 :
				strcpy(maskcode,"PZ '>=0'");

				break;
			case 4 :
				strcpy(maskcode,"N '<0'");

				break;
			case 5 :
				strcpy(maskcode,"NP '!=0'");

				break;
			case 6 :
				strcpy(maskcode,"NZ '<=0'");

				break;
			case 7 :
				strcpy(maskcode,"BR 'unconditional'");

				break;
		}
		int pcoffset = bitshift(cpu->instr_reg,8,0,1);
		if(mask&get_condition_code(cpu)!=0){
			int oldcounter = cpu->pgm_counter;
			cpu->pgm_counter += pcoffset;
			printf("BR number:%d %s ;condition satisify! New pgm_counter = %x + %x = %x\n",cpu->condition_number,maskcode,oldcounter,pcoffset,cpu->pgm_counter);
		}else{		
			printf("BR number:%d %s ;condition NOT satisify!\n",cpu->condition_number,maskcode);
		}
	}
Ejemplo n.º 11
0
/**
 * Evaluates the comparison expressions: greater than (a > b), less than (a < b),
 * greater than or equal to (a >= b), less than or equal to (a <= b),
 * which have the same operator precedence, by recursively calling the
 * functions for evaluating expressions with higher precedence.
 *
 * @param[out] ret: the integer value the logical and expression evaluates to
 *
 * @return: whether at the current location the expression can be evaluated
 */
bool
MacroParser::comparison(int64_t *ret)
{
	int64_t term2 = 0;
	if (bitshift(ret)) {
		for (;;) {
			if (_scanner.atSymbol(GT_EQ)) {
				if (bitshift(&term2)) {
					*ret = *ret >= term2;
				} else {
					return false;
				}
			} else if (_scanner.atSymbol(LT_EQ)) {
				if (bitshift(&term2)) {
					*ret = *ret <= term2;
				} else {
					return false;
				}
			} else if (_scanner.atSymbol(GT)) {
				if (bitshift(&term2)) {
					*ret = *ret > term2;
				} else {
					return false;
				}
			} else if (_scanner.atSymbol(LT)) {
				if (bitshift(&term2)) {
					*ret = *ret < term2;
				} else {
					return false;
				}
			} else {
				return true;
			}
		}
	}
	return false;
}
Ejemplo n.º 12
0
	void instr_TRAP(CPU *cpu)	{
		int vectors = bitshift(cpu->instr_reg,7,0,0);
		Word pointer;
		char temp;
		switch(vectors){
			case 32:
				printf("input a char:");
				scanf("%c",&temp);
				cpu->reg[0] = cpu->reg[0]&0;
				cpu->reg[0] =temp;
		    printf("read a char '%c'\n",temp);
				break;
			case 33:
				temp = cpu->reg[0];
				printf("%c",temp);
				break;
			case 34:

				pointer = cpu->reg[0];
				while(cpu->memory[pointer]){
					printf("%c",cpu->memory[pointer]);
					pointer++;
				}
				break;
			case 35:
				printf("Input a char:");
				scanf("%c",&temp);
				cpu->reg[0] = cpu->reg[0]&0;
				cpu->reg[0] =temp;
		    printf("read a char '%c'\n",temp);
				break;
			case 37:
				cpu->running = 0;
				break;
		}
		printf("\n");
	}
Ejemplo n.º 13
0
	void instr_STI (CPU *cpu)	{
		int src = bitshift(cpu->instr_reg,11,9,0);
		int pcoffset = bitshift(cpu->instr_reg,8,0,1);
		cpu->memory[cpu->memory[pcoffset+cpu->pgm_counter]] = cpu->reg[src];
		printf("STI R%d %x ; memory[memory[%x+%x]]<-reg[%x] = %x \n",src,pcoffset,cpu->pgm_counter,pcoffset,src,cpu->reg[src]);
	}
Ejemplo n.º 14
0
	void instr_JMP (CPU *cpu)	{
		int base = bitshift(cpu->instr_reg,8,6,0);
		cpu->pgm_counter = cpu->reg[base];
		printf("JMP New pgm_counter = reg[%d] = %x\n",base,cpu->reg[base]);
	}
Ejemplo n.º 15
0
static int fillbmpheader(FILE *file,struct bmpheader *head) {
    int i;

    memset(head,'\0',sizeof(*head));
    if ( getc(file)!='B' || getc(file)!='M' )
return 0;			/* Bad format */
    head->size = getl(file);
    head->mbz1 = getshort(file);
    head->mbz2 = getshort(file);
    head->offset = getl(file);
    head->headersize = getl(file);
    if ( head->headersize==12 ) {	/* Windows 2.0 format, also OS/2 */
	head->width = getshort(file);
	head->height = getshort(file);
	head->planes = getshort(file);
	head->bitsperpixel = getshort(file);
	head->colorsused = 0;
	head->compression = 0;
    } else {
	head->width = getl(file);
	head->height = getl(file);
	head->planes = getshort(file);
	head->bitsperpixel = getshort(file);
	head->compression = getl(file);
	head->imagesize = getl(file);
	head->ignore1 = getl(file);
	head->ignore2 = getl(file);
	head->colorsused = getl(file);
	head->colorsimportant = getl(file);
    }
    if ( head->height<0 )
	head->height = -head->height;
    else
	head->invert = true;
    if ( head->bitsperpixel!=1 && head->bitsperpixel!=4 && head->bitsperpixel!=8 &&
	    head->bitsperpixel!=16 && head->bitsperpixel!=24 && head->bitsperpixel!=32 )
return( 0 );
    if ( head->compression==3 && ( head->bitsperpixel==16 || head->bitsperpixel==32 ))
	/* Good */;
    else if ( head->compression==0 && ( head->bitsperpixel<=8 || head->bitsperpixel==24 || head->bitsperpixel==32 ))
	/* Good */;
    else if ( head->compression==1 && head->bitsperpixel==8 )
	/* Good */;
    else if ( head->compression==2 && head->bitsperpixel==4 )
	/* Good */;
    else
return( 0 );
    if ( head->colorsused==0 )
	head->colorsused = 1<<head->bitsperpixel;
    if ( head->bitsperpixel>=16 )
	head->colorsused = 0;
    if ( head->colorsused>(1<<head->bitsperpixel) )
return( 0 );
    for ( i=0; i<head->colorsused; ++i ) {
	int b = getc(file), g = getc(file), r=getc(file);
	head->clut[i] = COLOR_CREATE(r,g,b);
	if ( head->headersize!=12 )
	    getc(file);
    }
    if ( head->compression==3 || head->headersize==108 ) {
	head->red_mask = getl(file);
	head->green_mask = getl(file);
	head->blue_mask = getl(file);
	head->red_shift = bitshift(head->red_mask);
	head->green_shift = bitshift(head->green_mask);
	head->blue_shift = bitshift(head->blue_mask);
    }
    if ( head->headersize==108 ) {
	getl(file);		/* alpha_mask */
	getl(file);		/* color space type */
	getl(file);		/* redx */
	getl(file);		/* redy */
	getl(file);		/* redz */
	getl(file);		/* greenx */
	getl(file);		/* greeny */
	getl(file);		/* greenz */
	getl(file);		/* bluex */
	getl(file);		/* bluey */
	getl(file);		/* bluez */
	getl(file);		/* gammared */
	getl(file);		/* gammagreen */
	getl(file);		/* gammablue */
    }
return( 1 );
}
Ejemplo n.º 16
0
static int fillbmpheader(FILE *fp,struct bmpheader *head) {
/* Get BMPheader info. Return 0 if read the header okay, -1 if read error */
    int i;
    long temp;

    if ( fgetc(fp)!='B' || getc(fp)!='M' ||	/* Bad format */ \
	 getlong(fp,&head->size)	|| \
	 (head->mbz1=getshort(fp))<0	|| \
	 (head->mbz2=getshort(fp))<0	|| \
	 getlong(fp,&head->offset)	|| \
	 getlong(fp,&head->headersize)	)
	return( -1 );

    if ( head->headersize==12 ) {	/* Windows 2.0 format, also OS/2 */
	if ( (head->width=getshort(fp))<0	|| \
	     (head->height=getshort(fp))<0	|| \
	     (head->planes=getshort(fp))<0	|| \
	     (head->bitsperpixel=getshort(fp))<0 )
	    return( -1 );
	//head->colorsused=0;
	//head->compression=0;
    } else {
	if ( getlong(fp,&head->width)		|| \
	     getlong(fp,&head->height)		|| \
	     (head->planes=getshort(fp))<0	|| \
	     (head->bitsperpixel=getshort(fp))<0 || \
	     getlong(fp,&head->compression)	|| \
	     getlong(fp,&head->imagesize)	|| \
	     getlong(fp,&head->ignore1)		|| \
	     getlong(fp,&head->ignore2)		|| \
	     getlong(fp,&head->colorsused)	|| \
	     getlong(fp,&head->colorsimportant) )
	    return( -1 );
    }
    if ( head->height<0 )
	head->height = -head->height;
    else
	head->invert = true;

    if ( head->bitsperpixel!=1 && head->bitsperpixel!=4 && head->bitsperpixel!=8 &&
	    head->bitsperpixel!=16 && head->bitsperpixel!=24 && head->bitsperpixel!=32 )
	return( -1 );

    if ( head->compression==3 && ( head->bitsperpixel==16 || head->bitsperpixel==32 ) )
	/* Good */;
    else if ( head->compression==0 && ( head->bitsperpixel<=8 || head->bitsperpixel==24 || head->bitsperpixel==32 ) )
	/* Good */;
    else if ( head->compression==1 && head->bitsperpixel==8 )
	/* Good */;
    else if ( head->compression==2 && head->bitsperpixel==4 )
	/* Good */;
    else
	return( -1 );

    if ( head->colorsused==0 )
	head->colorsused = 1<<head->bitsperpixel;
    if ( head->bitsperpixel>=16 )
	head->colorsused = 0;
    if ( head->colorsused>(1<<head->bitsperpixel) )
	return( -1 );

    for ( i=0; i<head->colorsused; ++i ) {
	int b,g,r;
	if ( (b=fgetc(fp))<0 || (g=fgetc(fp))<0 || (r=fgetc(fp))<0 )
	    return( -1 );
	head->clut[i]=COLOR_CREATE(r,g,b);
	if ( head->headersize!=12 && fgetc(fp)<0 )
	    return( -1 );
    }
    if ( head->compression==3 || head->headersize==108 ) {
	if ( getlong(fp,&head->red_mask)	|| \
	     getlong(fp,&head->green_mask)	|| \
	     getlong(fp,&head->blue_mask) )
	    return( -1 );
	head->red_shift = bitshift(head->red_mask);
	head->green_shift = bitshift(head->green_mask);
	head->blue_shift = bitshift(head->blue_mask);
    }

    if ( head->headersize==108 && (
	 getlong(fp,&temp) ||	/* alpha_mask */ \
	 getlong(fp,&temp) ||	/* color space type */ \
	 getlong(fp,&temp) ||	/* redx */ \
	 getlong(fp,&temp) ||	/* redy */ \
	 getlong(fp,&temp) ||	/* redz */ \
	 getlong(fp,&temp) ||	/* greenx */ \
	 getlong(fp,&temp) ||	/* greeny */ \
	 getlong(fp,&temp) ||	/* greenz */ \
	 getlong(fp,&temp) ||	/* bluex */ \
	 getlong(fp,&temp) ||	/* bluey */ \
	 getlong(fp,&temp) ||	/* bluez */ \
	 getlong(fp,&temp) ||	/* gammared */ \
	 getlong(fp,&temp) ||	/* gammagreen */ \
	 getlong(fp,&temp) )	/* gammablue */ )
	return( -1 );

    return( 0 );
}