Initialer Commit

This commit is contained in:
lux 2017-08-02 20:10:36 +02:00
commit 168a8b9bcf
7 changed files with 179 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.o
*.toy
/coredump*
toy_cpu

25
makefile Normal file
View File

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

54
src/debug/debug.c Normal file
View File

@ -0,0 +1,54 @@
#include "debug.h"
#include "../toy.h"
#include <time.h>
//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<RAM_SIZE; i++) fprintBits(sizeof(uint16_t),ram+i,fp);
}
else
for(int i=1; i<=RAM_SIZE; i++) fprintf(fp,"%X\n",ram[i]);
fclose(fp);
}

10
src/debug/debug.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef DEBUG_H
#define DEBUG_H
#include <stdbool.h>
#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

12
src/main.c Normal file
View File

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

59
src/toy.c Normal file
View File

@ -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<CPU_WORD_SIZE)
{
fprintf(stderr,"%s","input file corrupted\n");
return -1;
}
}
tempS[CPU_WORD_SIZE]='\0';
ram[j]=strtoul(tempS,&end,2);
j++;
}while(tempS[0]!=EOF);
fclose(fp);
return j-1;
}
uint8_t get_opcode(uint16_t instruction)
{
uint8_t opcode;
opcode = instruction>>12;
return opcode;
}

15
src/toy.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef TOY_H
#define TOY_H
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#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