move functions and reorganize lib for toy & helper
This commit is contained in:
parent
6b5c6ecceb
commit
2be60bb718
11
src/helper.c
11
src/helper.c
@ -4,6 +4,17 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "toy.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
|
//assumes little endian
|
||||||
//stackoverflow.com/questions/111928
|
//stackoverflow.com/questions/111928
|
||||||
void fprintBits(size_t const size, void const * const ptr, FILE *file_pointer)
|
void fprintBits(size_t const size, void const * const ptr, FILE *file_pointer)
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
#ifndef DEBUG_H
|
#ifndef DEBUG_H
|
||||||
#define 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 fprintBits(size_t const size, void const * const ptr, FILE *file_pointer);
|
||||||
void makeHexDump(bool base_2, uint16_t ram[]);
|
void makeHexDump(bool base_2, uint16_t ram[]);
|
||||||
|
|
||||||
|
19
src/toy.c
19
src/toy.c
@ -1,4 +1,4 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -122,28 +122,18 @@ uint16_t get_data(uint16_t instruction)
|
|||||||
return operand;
|
return operand;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get2compl(uint16_t value)
|
static uint16_t accu;
|
||||||
{
|
const uint16_t * const ACCU = &accu;
|
||||||
int32_t 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 if true
|
bool execute(uint8_t op_code, int data_addr, uint16_t *ram) // jump if true
|
||||||
{
|
{
|
||||||
static uint16_t accu;
|
|
||||||
bool jump=false;
|
bool jump=false;
|
||||||
|
|
||||||
switch(op_code)
|
switch(op_code)
|
||||||
{
|
{
|
||||||
case 0: ram[data_addr] = accu; break; //STORE
|
case 0: ram[data_addr] = accu; break; //STORE
|
||||||
case 1: accu = ram[data_addr]; break; //LOAD
|
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 3: accu = accu + ram[data_addr]; break; //ADD
|
||||||
case 4: accu = accu - ram[data_addr]; break; //SUB
|
case 4: accu = accu - ram[data_addr]; break; //SUB
|
||||||
case 5: accu = accu | ram[data_addr]; break; //OR
|
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 14: ; break; //NOP
|
||||||
case 15: ; break; //NOP
|
case 15: ; break; //NOP
|
||||||
}
|
}
|
||||||
printf("ACCU: %d\n",get2compl(accu)); // not good place for it !
|
|
||||||
return jump;
|
return jump;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
#define CPU_WORD_SIZE 16
|
#define CPU_WORD_SIZE 16
|
||||||
|
|
||||||
|
|
||||||
|
extern const uint16_t * const ACCU; //read only access to accu
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* print_instructionSet(): print the cpu instruction set
|
* print_instructionSet(): print the cpu instruction set
|
||||||
* This is a user help function, can be activated via
|
* 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);
|
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
|
* execute(): execute the toy-CPU instruction
|
||||||
* This function implements the CPU instruction set,
|
* This function implements the CPU instruction set,
|
||||||
|
Loading…
Reference in New Issue
Block a user