move functions and reorganize lib for toy & helper

This commit is contained in:
lux 2017-10-01 21:03:00 +02:00
parent 6b5c6ecceb
commit 2be60bb718
4 changed files with 24 additions and 22 deletions

View File

@ -4,6 +4,17 @@
#include <time.h>
#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)

View File

@ -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[]);

View File

@ -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;
}

View File

@ -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,