From 0f27ef741adbeedf85960affad573404da6a2a9d Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sun, 25 Dec 2016 03:52:38 +0100 Subject: [PATCH] Initial code --- .gitignore | 1 + .idea/misc.xml | 14 ++++++++++++++ .idea/modules.xml | 8 ++++++++ .idea/synacorvm.iml | 34 ++++++++++++++++++++++++++++++++++ CMakeLists.txt | 7 +++++++ binary_decoder.c | 5 +++++ binary_decoder.h | 8 ++++++++ cpu.c | 16 ++++++++++++++++ cpu.h | 10 ++++++++++ main.c | 6 ++++++ mem.h | 10 ++++++++++ registers.h | 19 +++++++++++++++++++ stack.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ stack.h | 14 ++++++++++++++ 14 files changed, 199 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/synacorvm.iml create mode 100644 CMakeLists.txt create mode 100644 binary_decoder.c create mode 100644 binary_decoder.h create mode 100644 cpu.c create mode 100644 cpu.h create mode 100644 main.c create mode 100644 mem.h create mode 100644 registers.h create mode 100644 stack.c create mode 100644 stack.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1cc4572 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3eb495b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f1d385a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/synacorvm.iml b/.idea/synacorvm.iml new file mode 100644 index 0000000..cda13ed --- /dev/null +++ b/.idea/synacorvm.iml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..93f9a87 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.5) +project(synacorvm) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES main.c cpu.c cpu.h binary_decoder.c binary_decoder.h registers.h mem.h stack.c stack.h) +add_executable(synacorvm ${SOURCE_FILES}) \ No newline at end of file diff --git a/binary_decoder.c b/binary_decoder.c new file mode 100644 index 0000000..30c3b89 --- /dev/null +++ b/binary_decoder.c @@ -0,0 +1,5 @@ +// +// Created by imanol on 12/25/16. +// + +#include "binary_decoder.h" diff --git a/binary_decoder.h b/binary_decoder.h new file mode 100644 index 0000000..cd04ac3 --- /dev/null +++ b/binary_decoder.h @@ -0,0 +1,8 @@ +// +// Created by imanol on 12/25/16. +// + +#ifndef SYNACORVM_BINARY_DECODER_H +#define SYNACORVM_BINARY_DECODER_H + +#endif //SYNACORVM_BINARY_DECODER_H diff --git a/cpu.c b/cpu.c new file mode 100644 index 0000000..25f79ab --- /dev/null +++ b/cpu.c @@ -0,0 +1,16 @@ +// +// Created by imanol on 12/25/16. +// + +#include "cpu.h" + +void nop() +{ + return; +} + +void out(unsigned char a) +{ + printf("%c",a); +} + diff --git a/cpu.h b/cpu.h new file mode 100644 index 0000000..a14d8a1 --- /dev/null +++ b/cpu.h @@ -0,0 +1,10 @@ +// +// Created by imanol on 12/25/16. +// + +#ifndef SYNACORVM_CPU_H +#define SYNACORVM_CPU_H + +#include + +#endif //SYNACORVM_CPU_H diff --git a/main.c b/main.c new file mode 100644 index 0000000..7a69bbb --- /dev/null +++ b/main.c @@ -0,0 +1,6 @@ +#include + +int main(int argc, char** argv) +{ + return 0; +} \ No newline at end of file diff --git a/mem.h b/mem.h new file mode 100644 index 0000000..562584a --- /dev/null +++ b/mem.h @@ -0,0 +1,10 @@ +// +// Created by imanol on 12/25/16. +// + +#ifndef SYNACORVM_MEM_H +#define SYNACORVM_MEM_H + +short mem[32768]; //64KiB RAM + +#endif //SYNACORVM_MEM_H diff --git a/registers.h b/registers.h new file mode 100644 index 0000000..255d65f --- /dev/null +++ b/registers.h @@ -0,0 +1,19 @@ +// +// Created by imanol on 12/25/16. +// + +#ifndef SYNACORVM_REGISTERS_H +#define SYNACORVM_REGISTERS_H + +short r0 = 0; +short r1 = 0; +short r2 = 0; +short r3 = 0; +short r4 = 0; +short r5 = 0; +short r6 = 0; +short r7 = 0; + +short pc = 0; + +#endif //SYNACORVM_REGISTERS_H diff --git a/stack.c b/stack.c new file mode 100644 index 0000000..50a4cae --- /dev/null +++ b/stack.c @@ -0,0 +1,47 @@ +// +// Created by imanol on 12/25/16. +// + +#include +#include "stack.h" + +#define EXTEND_SIZE 10 + +uint32_t stack_size = EXTEND_SIZE; +uint32_t stack_pos = 0; +short *stack; + +void initialize_stack() +{ + stack = calloc(EXTEND_SIZE,sizeof(short)); +} + +void extend_stack() +{ + stack_size += EXTEND_SIZE; + realloc(stack,stack_size*sizeof(short)); +} + +void shrink_stack() +{ + stack_size -= EXTEND_SIZE; + realloc(stack,stack_size*sizeof(short)); +} + +void push(short value) +{ + stack[stack_pos++] = value; + if(stack_pos == stack_size-1) + { + extend_stack(); + } +} +short pop() +{ + short value = stack[stack_pos--]; + if(stack_size - stack_pos == 2*EXTEND_SIZE) + { + shrink_stack(); + } + return value; +} \ No newline at end of file diff --git a/stack.h b/stack.h new file mode 100644 index 0000000..0261eb7 --- /dev/null +++ b/stack.h @@ -0,0 +1,14 @@ +// +// Created by imanol on 12/25/16. +// + +#ifndef SYNACORVM_STACK_H +#define SYNACORVM_STACK_H + +#include + +void initialize_stack(); +void push(short value); +short pop(); + +#endif //SYNACORVM_STACK_H -- libgit2 0.22.2