commit b4c2cdeb6ea062424dd96abcc7a1d952a49381c2
Author: Gabriel G. de Brito <gabrielgbrito@icloud.com>
Date: Fri, 17 Nov 2023 00:45:22 -0300
In the beginning there was darkness
Diffstat:
6 files changed, 128 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+/egg
diff --git a/go.mod b/go.mod
@@ -0,0 +1,3 @@
+module github.com/gboncoffee/egg
+
+go 1.19
diff --git a/machine.go b/machine.go
@@ -0,0 +1,73 @@
+package main
+
+type page struct {
+ min uint64
+ next *page
+ mem []byte
+}
+
+type Memory struct {
+ first *page
+ pagesize uint64
+}
+
+type Machine interface {
+ CreateMachine() *Machine
+ NextInstruction() error
+ GetMemory(uint64) []byte
+ SetMemory(uint64, []byte)
+ GetRegister(uint64) []byte
+ SetRegister(uint64, []byte)
+}
+
+func newPage(min uint64, size uint64, next *page) *page {
+ p := page{min: min, next: next, mem: make([]byte, size)}
+
+ return &p
+}
+
+func NewMemory(pagesize uint64) *Memory {
+ mem := Memory{first: newPage(0, pagesize, nil), pagesize: pagesize}
+
+ return &mem
+}
+
+// We need to convert everything to uint64 and it becomes a messy type
+// confusion.
+func fetchBytes(addr uint64, idx uint64, arr *[]byte, p *page) {
+ if idx >= uint64(len(*arr)) {
+ return
+ }
+
+ var i uint64 = 0
+ for p != nil && i < uint64(len(*arr)) && p.min < addr+uint64(len(*arr)) {
+ if (addr-p.min)+i < 0 {
+ return
+ }
+
+ (*arr)[i] = p.mem[((addr + i) - p.min)]
+
+ i++
+ if addr+i > p.min+uint64(len(p.mem)) {
+ p = p.next
+ i += p.min - (addr + i)
+ }
+ }
+}
+
+func (m *Memory) Get(n_bytes uint64, addr uint64) []byte {
+ arr := make([]byte, n_bytes)
+ p := m.first
+ for p != nil {
+ if addr >= p.min && addr < uint64(len(p.mem))+p.min {
+ fetchBytes(addr, 0, &arr, p)
+ return arr
+ }
+ p = p.next
+ }
+
+ return arr
+}
+
+func (m *Memory) Set(n_bytes uint64, addr uint64, mem []byte) {
+}
diff --git a/main.go b/main.go
@@ -0,0 +1,17 @@
+package main
+
+import (
+ "fmt"
+
+ "github.com/gboncoffee/egg/riscv"
+)
+
+func main() {
+ fmt.Println("Hello, World!")
+
+ if riscv.CreateMachine() {
+ fmt.Println("yes")
+ } else {
+ fmt.Println("no")
+ }
+}
diff --git a/main_test.go b/main_test.go
@@ -0,0 +1,26 @@
+package main
+
+import "testing"
+
+func TestCreateMemory(t *testing.T) {
+ mem := NewMemory(1024)
+ if mem.pagesize != 1024 {
+ t.Fatalf("Incorrect pagesize")
+ }
+
+ if mem.first == nil {
+ t.Fatalf("mem.first is nil")
+ }
+
+ if mem.first.next != nil {
+ t.Fatalf("mem.first.next points somewhere")
+ }
+
+ if mem.first.min != 0 {
+ t.Fatalf("Memory was not iniciated to the beggining")
+ }
+
+ if len(mem.first.mem) != 1024 {
+ t.Fatalf("Memory buffer was not iniciated with 1024 positions")
+ }
+}
diff --git a/riscv/riscv.go b/riscv/riscv.go
@@ -0,0 +1,8 @@
+package riscv
+
+import "fmt"
+
+func CreateMachine() bool {
+ fmt.Println("create")
+ return true
+}