From 168a8b9bcfe31d640fa6e48b426fdbcfe8e0ced5 Mon Sep 17 00:00:00 2001 From: lux Date: Wed, 2 Aug 2017 20:10:36 +0200 Subject: [PATCH] Initialer Commit --- .gitignore | 4 ++++ makefile | 25 ++++++++++++++++++++ src/debug/debug.c | 54 +++++++++++++++++++++++++++++++++++++++++++ src/debug/debug.h | 10 ++++++++ src/main.c | 12 ++++++++++ src/toy.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ src/toy.h | 15 ++++++++++++ 7 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 makefile create mode 100644 src/debug/debug.c create mode 100644 src/debug/debug.h create mode 100644 src/main.c create mode 100644 src/toy.c create mode 100644 src/toy.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d10c826 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.toy +/coredump* +toy_cpu diff --git a/makefile b/makefile new file mode 100644 index 0000000..500b13a --- /dev/null +++ b/makefile @@ -0,0 +1,25 @@ +#-------------Makro-Part------------------ +CC = gcc +CFLAGS = -Wall -std=c99 +LDFLAGs = +DEBUG = + + +VPATH = src src/debug +#--------------Linker-Part----------------- +toy_cpu : main.o toy.o debug.o + echo $@ is now linked + $(CC) $(LDFLAGS) -o $@ $^ +#--------------Compiler-Part--------------- +main.o : main.c + $(CC) $(DEBUG) $(CFLAGS) -o $@ -c $< + +toy.o : toy.c toy.h + $(CC) $(DEBUG) $(CFLAGS) -o $@ -c $< + +debug.o : debug.c debug.h + $(CC) $(DEBUG) $(CFLAGS) -o $@ -c $< + +clean : + rm -f *.o + diff --git a/src/debug/debug.c b/src/debug/debug.c new file mode 100644 index 0000000..25ac685 --- /dev/null +++ b/src/debug/debug.c @@ -0,0 +1,54 @@ +#include "debug.h" +#include "../toy.h" +#include + +//assumes little endian +//stackoverflow.com/questions/111928 +void fprintBits(size_t const size, void const * const ptr, FILE *file_pointer) +{ + //Beispiel: + //printBits(sizeof(*ram), ram); + unsigned char *b = (unsigned char*) ptr; + unsigned char byte; + int i, j; + + for (i=size-1;i>=0;i--) + { + for (j=7;j>=0;j--) + { + byte = (b[i] >> j) & 1; + fprintf(file_pointer,"%u", byte); + } + } + fputs("\n",file_pointer); +} + +void makeHexDump(bool base_2, uint16_t ram[]) +{ + time_t timer; + struct tm *tm_timer; + char timestamp[32]; + FILE *fp; + + time(&timer); + tm_timer=localtime(&timer); + if(strftime(timestamp,sizeof(timestamp),"coredump_%d_%b_%y_%T.txt",tm_timer)==0) + { + fprintf(stderr,"Buffer for system-time fault! hexdump canceled\n"); + return; + } + + if(!(fp=fopen(timestamp,"wb"))) + { + fprintf(stderr,"File system access failed, hexdump canceled\n"); + return; + } + if(base_2) + { + for(int i=0; i +#include "../toy.h" + +void fprintBits(size_t const size, void const * const ptr, FILE *file_pointer); +void makeHexDump(bool base_2, uint16_t ram[]); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..5ccb7c2 --- /dev/null +++ b/src/main.c @@ -0,0 +1,12 @@ +#include "toy.h" +#include "./debug/debug.h" + +int main(int argc, char *argv[]) +{ + uint16_t ram[RAM_SIZE]; + + if(initialise_ram(ram,argc,argv)==-1) return 1; + makeHexDump(true,ram); + + return 0; +} diff --git a/src/toy.c b/src/toy.c new file mode 100644 index 0000000..9f06398 --- /dev/null +++ b/src/toy.c @@ -0,0 +1,59 @@ +#include "toy.h" + +//gibt Anzahl der erfolgreich gelesenen Maschinenworte zurück(-1 im Fehlerfall). +int initialise_ram(uint16_t *ram, int argc, char **argv ) +{ + + //open Input Stream + + FILE *fp; + int j=0; + char tempS[CPU_WORD_SIZE+1]; + char *end; + + if(argc<2) + { + fprintf(stderr,"%s","no \".toy\" input file!\n"); + return -1; + } + if(argc >2) + { + fprintf(stderr,"%s","too many input files!\n"); + return -1; + } + + if(!(fp=fopen(argv[1],"rb"))) + { + fprintf(stderr,"%s","input file corrupted\n"); + return -1; + } + // initialise Toy-RAM + + do + { + for(int i=0;i<=CPU_WORD_SIZE;i++) + { + tempS[i]=fgetc(fp); + if(tempS[0]==EOF) break; + if(tempS[i]!='1' && tempS[i]!='0' && i>12; + return opcode; +} + diff --git a/src/toy.h b/src/toy.h new file mode 100644 index 0000000..60c0f04 --- /dev/null +++ b/src/toy.h @@ -0,0 +1,15 @@ +#ifndef TOY_H +#define TOY_H + +#include +#include +#include + +#define RAM_SIZE 4096 +#define CPU_WORD_SIZE 16 + + +int initialise_ram(uint16_t *ram, int argc, char **argv ); +uint8_t get_opcode(uint16_t instruction); + +#endif