Add 2-complement interpretation for ALU

This commit is contained in:
lux 2017-08-14 22:37:03 +02:00
parent 82aba5e133
commit d8b1d230da
3 changed files with 30 additions and 14 deletions

View File

@ -18,28 +18,34 @@ int main(int argc, char *argv[])
int quit='n'; int quit='n';
bool run = true; bool run = true;
printf("\n+++++++++++++++++++++++++++++++++++++++++++\n+Boot: "CPU_TYPE printf("\n+++++++++++++++++++++++++++++++++++++++++++++++\n+Boot: "CPU_TYPE
" wite %zu x %zu RAM.+\n+++++++++++++++++++++++++++++++++++++++++++\n\n" " wite %zu x %zu Bit RAM.+\n+++++++++++++++++++++++++++++++++++++++++++++++\n\n"
,(sizeof(ram)/sizeof(ram[0])),sizeof(ram[0])*8); ,(sizeof(ram)/sizeof(ram[0])),sizeof(ram[0])*8);
if(initialise_ram(ram,argc,argv)==-1) return 1; if(initialise_ram(ram,argc,argv)==-1) return 1;
while(run && pc<(RAM_SIZE-1)) while(run && pc<(RAM_SIZE-1))
{ {
ir = ram[pc]; ir = ram[pc];
op_code = get_opcode(ir); op_code = get_opcode(ir);
data_addr=find_data(ir); data_addr=find_data(ir);
printf("Inst: "); //Handele user output
printf("\ninstruction: ");
fprintBits(sizeof(*ram), ram+pc,stdout); fprintBits(sizeof(*ram), ram+pc,stdout);
printf("OP Code: %"PRIu8" ",op_code); printf("OP Code: %"PRIu8"\t",op_code);
printf("Adresse: %d\n",data_addr); printf("Adresse: %u\n",data_addr);
printf("\ninstruction result:\n");
if(execute(op_code,data_addr,ram)) //CPU control flow
pc=find_data(ram[pc]); //jump if ACCU is ZERO if(execute(op_code,data_addr,ram)) //jump if ALU says
pc=find_data(ram[pc]);
else 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) while((quit = getchar()) != '\n' && quit != EOF)
{ {
if(quit == 'c') makeHexDump(true,ram); if(quit == 'c') makeHexDump(true,ram);
@ -47,8 +53,5 @@ int main(int argc, char *argv[])
else if(quit == 'q') run = false; else if(quit == 'q') run = false;
} }
} }
return 0; return 0;
} }

View File

@ -65,6 +65,18 @@ int find_data(uint16_t instruction)
return instr; 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 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 14: ; break; //NOP
case 15: ; break; //NOP case 15: ; break; //NOP
} }
printf("ACCU: %"PRIu16 "\n",accu); printf("ACCU: %d\n",get2compl(accu)); // not good place for it !
return jump; return jump;
} }

View File

@ -14,6 +14,7 @@
int initialise_ram(uint16_t *ram, int argc, char **argv ); int initialise_ram(uint16_t *ram, int argc, char **argv );
uint8_t get_opcode(uint16_t instruction); uint8_t get_opcode(uint16_t instruction);
int find_data(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); bool execute(uint8_t op_code, int data_addr,uint16_t *ram);
#endif #endif