diff --git a/src/helper.c b/src/helper.c index 85ec053..cb3502f 100644 --- a/src/helper.c +++ b/src/helper.c @@ -4,6 +4,17 @@ #include #include "toy.h" +int32_t get2compl(uint16_t value) +{ + int32_t sign_value = value; + if(value>32767) + { + value=(~value)+1; + sign_value = value*(-1); + } + return sign_value; +} + //assumes little endian //stackoverflow.com/questions/111928 void fprintBits(size_t const size, void const * const ptr, FILE *file_pointer) diff --git a/src/helper.h b/src/helper.h index e8a2701..d715703 100644 --- a/src/helper.h +++ b/src/helper.h @@ -1,6 +1,13 @@ #ifndef DEBUG_H #define DEBUG_H +/** +* get2compl(): interpret the transfer value as tow's complement +* Return: tow's complement value between -32768 and 32767 +*/ + +int32_t get2compl(uint16_t value); + void fprintBits(size_t const size, void const * const ptr, FILE *file_pointer); void makeHexDump(bool base_2, uint16_t ram[]); diff --git a/src/toy.c b/src/toy.c index dc9e54f..04e21a7 100644 --- a/src/toy.c +++ b/src/toy.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -122,28 +122,18 @@ uint16_t get_data(uint16_t instruction) return operand; } -int get2compl(uint16_t value) -{ - int32_t sign_value = value; - if(value>32767) - { - value=(~value)+1; - sign_value = value*(-1); - } - return sign_value; -} - +static uint16_t accu; +const uint16_t * const ACCU = &accu; bool execute(uint8_t op_code, int data_addr, uint16_t *ram) // jump if true { - static uint16_t accu; bool jump=false; switch(op_code) { case 0: ram[data_addr] = accu; break; //STORE case 1: accu = ram[data_addr]; break; //LOAD - case 2: jump = (accu == 0); break; //JMP + case 2: jump = (accu == 0); break; //JMP case 3: accu = accu + ram[data_addr]; break; //ADD case 4: accu = accu - ram[data_addr]; break; //SUB case 5: accu = accu | ram[data_addr]; break; //OR @@ -158,7 +148,6 @@ bool execute(uint8_t op_code, int data_addr, uint16_t *ram) // jump if true case 14: ; break; //NOP case 15: ; break; //NOP } - printf("ACCU: %d\n",get2compl(accu)); // not good place for it ! return jump; } diff --git a/src/toy.h b/src/toy.h index 4662ee6..b85bd20 100644 --- a/src/toy.h +++ b/src/toy.h @@ -6,6 +6,8 @@ #define CPU_WORD_SIZE 16 +extern const uint16_t * const ACCU; //read only access to accu + /** * print_instructionSet(): print the cpu instruction set * This is a user help function, can be activated via @@ -38,13 +40,6 @@ uint8_t get_opcode(uint16_t instruction); uint16_t get_data(uint16_t instruction); -/** -* get2compl(): interpret the transfer value as tow's complement -* Return: tow's complement value between -255 and 254 -*/ - -int get2compl(uint16_t value); //not good place for something! - /** * execute(): execute the toy-CPU instruction * This function implements the CPU instruction set,