Add 2-complement interpretation for ALU
This commit is contained in:
		
							
								
								
									
										29
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								src/main.c
									
									
									
									
									
								
							@@ -18,28 +18,34 @@ int main(int argc, char *argv[])
 | 
			
		||||
    int         quit='n';
 | 
			
		||||
    bool        run = true;
 | 
			
		||||
 | 
			
		||||
    printf("\n+++++++++++++++++++++++++++++++++++++++++++\n+Boot: "CPU_TYPE
 | 
			
		||||
    " wite %zu x %zu RAM.+\n+++++++++++++++++++++++++++++++++++++++++++\n\n"
 | 
			
		||||
    printf("\n+++++++++++++++++++++++++++++++++++++++++++++++\n+Boot: "CPU_TYPE
 | 
			
		||||
    " wite %zu x %zu Bit RAM.+\n+++++++++++++++++++++++++++++++++++++++++++++++\n\n"
 | 
			
		||||
    ,(sizeof(ram)/sizeof(ram[0])),sizeof(ram[0])*8);
 | 
			
		||||
    
 | 
			
		||||
    if(initialise_ram(ram,argc,argv)==-1) return 1;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    while(run && pc<(RAM_SIZE-1))
 | 
			
		||||
    {
 | 
			
		||||
        ir = ram[pc];
 | 
			
		||||
        op_code = get_opcode(ir);
 | 
			
		||||
        data_addr=find_data(ir);
 | 
			
		||||
 | 
			
		||||
        printf("Inst: ");
 | 
			
		||||
        //Handele user output
 | 
			
		||||
        printf("\ninstruction: ");
 | 
			
		||||
        fprintBits(sizeof(*ram), ram+pc,stdout);
 | 
			
		||||
        printf("OP Code: %"PRIu8" ",op_code);
 | 
			
		||||
        printf("Adresse: %d\n",data_addr);
 | 
			
		||||
 | 
			
		||||
        if(execute(op_code,data_addr,ram))
 | 
			
		||||
            pc=find_data(ram[pc]);              //jump if ACCU is ZERO
 | 
			
		||||
        printf("OP Code: %"PRIu8"\t",op_code);
 | 
			
		||||
        printf("Adresse: %u\n",data_addr);
 | 
			
		||||
        printf("\ninstruction result:\n");
 | 
			
		||||
        //CPU control flow
 | 
			
		||||
        if(execute(op_code,data_addr,ram)) //jump if ALU says 
 | 
			
		||||
            pc=find_data(ram[pc]); 
 | 
			
		||||
        else pc++;
 | 
			
		||||
        printf("(n)ext step or (q)uit or (c)oredump ?\n");
 | 
			
		||||
 | 
			
		||||
        //Handele user output
 | 
			
		||||
        printf("PROGRAM COUNTER: %" PRIu16 "\n",pc);
 | 
			
		||||
        printf("\n(n)ext step or (q)uit or (c)oredump ?\n");
 | 
			
		||||
 | 
			
		||||
        //handel program flow
 | 
			
		||||
        while((quit = getchar()) != '\n' && quit != EOF)
 | 
			
		||||
        {
 | 
			
		||||
            if(quit == 'c') makeHexDump(true,ram);
 | 
			
		||||
@@ -47,8 +53,5 @@ int main(int argc, char *argv[])
 | 
			
		||||
            else if(quit == 'q') run = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								src/toy.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/toy.c
									
									
									
									
									
								
							@@ -65,6 +65,18 @@ int find_data(uint16_t instruction)
 | 
			
		||||
    return instr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int get2compl(uint16_t value)
 | 
			
		||||
{
 | 
			
		||||
    int sign_value = value;
 | 
			
		||||
    if(value>32767)
 | 
			
		||||
    {
 | 
			
		||||
        value=(~value)+1;
 | 
			
		||||
        sign_value = value*(-1);
 | 
			
		||||
    }    
 | 
			
		||||
    return sign_value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
bool execute(uint8_t op_code, int data_addr, uint16_t *ram) // jump when 1
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
@@ -92,7 +104,7 @@ bool execute(uint8_t op_code, int data_addr, uint16_t *ram) // jump when 1
 | 
			
		||||
        case 14: ;                                  break; //NOP
 | 
			
		||||
        case 15: ;                                  break; //NOP
 | 
			
		||||
    }
 | 
			
		||||
        printf("ACCU: %"PRIu16 "\n",accu);
 | 
			
		||||
        printf("ACCU: %d\n",get2compl(accu));  // not good place for it !
 | 
			
		||||
        return jump;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,7 @@
 | 
			
		||||
int initialise_ram(uint16_t *ram, int argc, char **argv ); 
 | 
			
		||||
uint8_t get_opcode(uint16_t instruction);
 | 
			
		||||
int find_data(uint16_t instruction);
 | 
			
		||||
int get2compl(uint16_t value); //not good place for something!
 | 
			
		||||
bool execute(uint8_t op_code, int data_addr,uint16_t *ram);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user