From 4c6c501955acbdf5d3c1af7888f04251669e65fb Mon Sep 17 00:00:00 2001
From: xamidev <121681048+xamidev@users.noreply.github.com>
Date: Fri, 18 Oct 2024 21:18:14 +0200
Subject: [PATCH] first commit: MOV(?), ADD(?), HLT
---
.gitignore | 1 +
README.md | 20 +++++
cpu.c | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++++
makefile | 5 ++
program.asm | 3 +
5 files changed, 255 insertions(+)
create mode 100644 .gitignore
create mode 100644 README.md
create mode 100644 cpu.c
create mode 100644 makefile
create mode 100644 program.asm
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f47cb20
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.out
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..16c7933
--- /dev/null
+++ b/README.md
@@ -0,0 +1,20 @@
+# UnnamedCPU
+
+Continuing my exploration of the lower-level world...
+
+This project is the implementation of a CPU in a high-level language, C. It aims to have a reduced (minimalist) instruction set and still be Turing complete.
+
+# How to test
+
+You can run a test program like that. I'll try making a developer's manual so one can make its own programs using the custom assembly here.
+
+```
+make
+./a.out program.asm
+```
+
+# What I'd like to do
+
+- Finish the instruction set implementation
+- Implement input/output devices (like a serial output that could be redirected to the host terminal)
+- Re-do the CPU in a Hardware Description Language, or even better: with transistors IRL! (i probably won't waste my life doing that tho)
diff --git a/cpu.c b/cpu.c
new file mode 100644
index 0000000..bf4040c
--- /dev/null
+++ b/cpu.c
@@ -0,0 +1,226 @@
+/*
+ * Unnamed 8-bit processing unit
+ * Made by github.com/xamidev
+ *
+ * This is free and unencumbered software released into the public domain.
+ * For more information, please refer to
+*/
+
+#include
+#include
+#include
+#include
+#include
+
+#define MEM_SIZE 256
+#define NUM_REGISTERS 4
+
+/*
+ * Instruction set
+ * Here, we're making a RISC (reduced instruction set computer)
+ * so we're staying minimalistic.
+*/
+
+typedef enum
+{
+ MOV = 1,
+ ADD,
+ SUB,
+ LOAD,
+ STORE,
+ JMP,
+ JEQ,
+ HLT = 0xFF
+} instruction_set_t;
+
+/*
+ * CPU structure definition
+ * Contains 4 8-bit registers, memory, a program counter, and a halt switch.
+*/
+
+typedef struct
+{
+ uint8_t reg[NUM_REGISTERS];
+ uint8_t memory[MEM_SIZE];
+ uint16_t pc;
+ bool halted;
+} CPU_t;
+
+CPU_t cpu;
+
+/*
+ * Initializing the CPU: program counter and registers to zero, halted flag to false.
+*/
+
+void cpu_init()
+{
+ cpu.pc = 0;
+ cpu.halted = false;
+ for (size_t i=0; i\n", argv[0]);
+ return -1;
+ }
+
+ assemble(argv[1]);
+
+ // Dumping our program
+ mem_dump();
+ cpu_run();
+
+ // Post-mortem analysis
+ cpu_dump();
+
+ return 0;
+}
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..59003f9
--- /dev/null
+++ b/makefile
@@ -0,0 +1,5 @@
+all:
+ gcc -g *.c
+
+clean:
+ rm a.out
diff --git a/program.asm b/program.asm
new file mode 100644
index 0000000..019e078
--- /dev/null
+++ b/program.asm
@@ -0,0 +1,3 @@
+
+MOV 1 2
+HLT