From da7317ced2a41763d4717d7c81abc87851796fd0 Mon Sep 17 00:00:00 2001 From: Rose Apollo <55228370+AUnicornWithNoLife@users.noreply.github.com> Date: Fri, 19 Apr 2024 09:05:10 +0000 Subject: [PATCH] even more --- .gitignore | 2 ++ README.md | 20 ++++++++++---------- build.sh | 2 ++ emu/cpu/ram.c | 28 ++++++++++++++++++++++++++++ emu/cpu/ram.h | 18 ++++++++++++++++++ emu/cpu/register.c | 37 +++++++++++++++++++++++++++++++++++++ emu/cpu/register.h | 10 ++++++++++ emu/cpu/stack.c | 13 +++++++++++++ emu/cpu/stack.h | 12 ++++++++++++ emu/cpu_const.h | 8 +++++--- emu/main.c | 14 ++++++++++++++ 11 files changed, 151 insertions(+), 13 deletions(-) create mode 100755 build.sh create mode 100644 emu/cpu/ram.c create mode 100644 emu/cpu/ram.h create mode 100644 emu/cpu/register.c create mode 100644 emu/cpu/register.h create mode 100644 emu/cpu/stack.c create mode 100644 emu/cpu/stack.h create mode 100644 emu/main.c diff --git a/.gitignore b/.gitignore index c6127b3..dfb8efe 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,5 @@ modules.order Module.symvers Mkfile.old dkms.conf + +bin \ No newline at end of file diff --git a/README.md b/README.md index 5a08f53..bb518a6 100644 --- a/README.md +++ b/README.md @@ -38,17 +38,17 @@ | 0x5 | PC | Program Counter | | 0x6 | SP | Stack Pointer | -| Area of RAM | reserved | usage | -|:-------------:|:--------:|-------------------| -| 00000 - 00064 | YES | system header | -| 00064 - 65263 | NO | executable | -| 65263 - 65519 | YES | interupt pointers | -| 65519 - 65535 | YES | stack | +| Area of RAM | reserved | Writable | usage | +|:-------------:|:--------:|:--------:|-------------------| +| 00000 - 00064 | YES | NO | system header | +| 00064 - 65023 | NO | YES | executable | +| 65023 - 65279 | YES | YES | interupt pointers | +| 65279 - 65535 | YES | YES | stack | -| BYTE 1 | | | BYTE 2 | BYTE 2 | -|------------|-------------|-------------|------------|------------| -| OPCODE | arg 1 info | arg 2 info | ARGUMENT 1 | ARGUMENT 2 | -| bit 0 - 12 | bit 12 - 14 | bit 14 - 16 | bit 0 - 16 | bit 0 - 16 | +| BYTE 1 | | | | BYTE 2 | BYTE 2 | +|--------|--------|------------|------------|------------|------------| +| OPCODE | UNUSED | ARG 1 INFO | ARG 2 INFO | ARGUMENT 1 | ARGUMENT 2 | +| 8 bits | 4 bits | 2 bits | 2 bits | 16 bits | 16 bits | | State | Argument Interpretation | |------:|---------------------------------------------| diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..59b9853 --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +mkdir ./bin +clang -o ./bin/r16e ./emu/main.c ./emu/cpu/ram.c ./lib/carg-parse/carg-parse.c \ No newline at end of file diff --git a/emu/cpu/ram.c b/emu/cpu/ram.c new file mode 100644 index 0000000..b5eb0a0 --- /dev/null +++ b/emu/cpu/ram.c @@ -0,0 +1,28 @@ +#include "ram.h" + +#include + +r16_int* ram = NULL; + +void InitRam() +{ + ram = (r16_int*)malloc(sizeof(r16_int) * UINT16_MAX); +} + +r16_int GetValue(r16_int address) +{ + #ifdef RAM_DO_NULL_CHECK + if (ram == NULL) { InitRam(); } + #endif + + return ram[address.u]; +} + +void SetValue(r16_int address, r16_int value) +{ + #ifdef RAM_DO_NULL_CHECK + if (ram == NULL) { InitRam(); } + #endif + + ram[address.u] = value; +} diff --git a/emu/cpu/ram.h b/emu/cpu/ram.h new file mode 100644 index 0000000..21d23fb --- /dev/null +++ b/emu/cpu/ram.h @@ -0,0 +1,18 @@ +#ifndef EMU_CPU_RAM_H +#define EMU_CPU_RAM_H + +#define RAM_DO_NULL_CHECK + +#include + +typedef union +{ + int16_t s; + uint16_t u; +} r16_int; + +void InitRam(); +void SetValue(r16_int address, r16_int value); +r16_int GetValue(r16_int address); + +#endif \ No newline at end of file diff --git a/emu/cpu/register.c b/emu/cpu/register.c new file mode 100644 index 0000000..911da2d --- /dev/null +++ b/emu/cpu/register.c @@ -0,0 +1,37 @@ +#include "./register.h" +#include + +r16_int r0, r1, r2, r3, acu, pc, sp; + +r16_int *GetRegPoint(CpuRegisters reg) +{ + switch (reg) + { + case R0: + return &r0; + case R1: + return &r1; + case R2: + return &r2; + case R3: + return &r3; + case ACU: + return &acu; + case PC: + return &pc; + case SP: + return &sp; + } + + return NULL; +} + +r16_int GetRegister(CpuRegisters reg) +{ + return *(GetRegPoint(reg)); +} + +void SetRegister(CpuRegisters reg, r16_int value) +{ + *(GetRegPoint(reg)) = value; +} \ No newline at end of file diff --git a/emu/cpu/register.h b/emu/cpu/register.h new file mode 100644 index 0000000..bc213db --- /dev/null +++ b/emu/cpu/register.h @@ -0,0 +1,10 @@ +#ifndef EMU_CPU_REGISTER_H +#define EMU_CPU_REGISTER_H + +#include "../cpu_const.h" +#include "./ram.h" + +r16_int GetRegister(CpuRegisters reg); +void SetRegister(CpuRegisters reg, r16_int value); + +#endif \ No newline at end of file diff --git a/emu/cpu/stack.c b/emu/cpu/stack.c new file mode 100644 index 0000000..3da01d0 --- /dev/null +++ b/emu/cpu/stack.c @@ -0,0 +1,13 @@ +#include "./stack.h" + +r16_int pop() +{ + #ifdef DO_STACK_CHECKS + if () + #endif +} + +void push(r16_int value) +{ + +} diff --git a/emu/cpu/stack.h b/emu/cpu/stack.h new file mode 100644 index 0000000..28e4111 --- /dev/null +++ b/emu/cpu/stack.h @@ -0,0 +1,12 @@ +#ifndef EMU_CPU_STACK_H +#define EMU_CPU_STACK_H + +#include "./ram.h" +#include "./register.h" + +#define STACK_SIZE 256 +#define STACK_LOCATION (UINT16_MAX - STACK_SIZE) + +#define DO_STACK_CHECKS + +#endif \ No newline at end of file diff --git a/emu/cpu_const.h b/emu/cpu_const.h index 23dabc3..b22a2e2 100644 --- a/emu/cpu_const.h +++ b/emu/cpu_const.h @@ -1,6 +1,8 @@ #ifndef EMU_CPU_CONST_H #define EMU_CPU_CONST_H +#include + typedef enum { HLT = 0x00, @@ -44,12 +46,12 @@ typedef enum typedef enum { value = 0x0, - register = 0x1, - ram = 0x2, + valueInRegister = 0x1, + valueInRam = 0x2, pointerInRegister = 0x3 } ArgumentInfo; -#define INSTRUCTION_BIT_MASK 0b1111111111110000 +#define INSTRUCTION_BIT_MASK 0b1111111100000000 #define INSTRUCTION_ARG_1_INFO_MASK 0b0000000000001100 #define INSTRUCTION_ARG_2_INFO_MASK 0b0000000000000011 diff --git a/emu/main.c b/emu/main.c new file mode 100644 index 0000000..e0162f5 --- /dev/null +++ b/emu/main.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +#include "../lib/carg-parse/carg-parse.h" +#include "./cpu_const.h" + +int main(int argc, char **argv) +{ + carg_parse_data* args = carg_parse(argc, argv); + + carg_parse_free(args); +}