From 12c42d956354c70e2aa36d03696560eb3172e40a Mon Sep 17 00:00:00 2001 From: Jason Hilder Date: Thu, 16 Apr 2026 07:34:21 +0200 Subject: [PATCH] Initial commit for basics need a refresher. --- cmd/asm/main.go | 7 ++++ cmd/sim/main.go | 26 ++++++++++++++ go.mod | 3 ++ internal/bus/bus.go | 20 +++++++++++ internal/cpu/cpu.go | 85 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 cmd/asm/main.go create mode 100644 cmd/sim/main.go create mode 100644 go.mod create mode 100644 internal/bus/bus.go create mode 100644 internal/cpu/cpu.go diff --git a/cmd/asm/main.go b/cmd/asm/main.go new file mode 100644 index 0000000..7bd7bfd --- /dev/null +++ b/cmd/asm/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hi compiler") +} diff --git a/cmd/sim/main.go b/cmd/sim/main.go new file mode 100644 index 0000000..13a706d --- /dev/null +++ b/cmd/sim/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + + "jh/gomos/internal/bus" + "jh/gomos/internal/cpu" +) + +func main() { + fmt.Println("--- 6502 Simulator Starting ---") + + // Initialize the Bus + b := bus.New() + + // Initialize CPU and "plug in" the Bus + c := &cpu.CPU{Bus: b} + c.Reset() + + // Run for 10 cycles to verify the "Fetch" works + for range 10 { + c.Step() + } + + fmt.Println("--- Simulation Complete ---") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f728866 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module jh/gomos + +go 1.26.1 diff --git a/internal/bus/bus.go b/internal/bus/bus.go new file mode 100644 index 0000000..864b7d4 --- /dev/null +++ b/internal/bus/bus.go @@ -0,0 +1,20 @@ +package bus + +type MainBus struct { + // add ram etc here +} + +func New() *MainBus { + return &MainBus{} +} + +// Just a test read for now, need to figure wtf is happening... +func (b *MainBus) Read(address uint16) uint8 { + // Just testing with NOPs + return 0xEA +} + +func (b *MainBus) Write(address uint16, data uint8) { + // Need to write data to the actual ram. +} + diff --git a/internal/cpu/cpu.go b/internal/cpu/cpu.go new file mode 100644 index 0000000..9bb6795 --- /dev/null +++ b/internal/cpu/cpu.go @@ -0,0 +1,85 @@ +package cpu + +import "fmt" + +const ARCH = "MOS6502" + +// bits for the status register +const ( + FlagCarry = 1 << 0 // Bit 0 + FlagZero = 1 << 1 // Bit 1 + FlagInterrupt = 1 << 2 // Bit 2 + FlagDecimal = 1 << 3 // Bit 3 + FlagBreak = 1 << 4 // Bit 4 + FlagUnused = 1 << 5 // Bit 5 + FlagOverflow = 1 << 6 // Bit 6 + FlagNegative = 1 << 7 // Bit 7 +) + +type Bus interface { + Read(address uint16) uint8 +} + +type CPU struct { + A uint8 // Accumulator + X uint8 // X Register + Y uint8 // Y Register + PC uint16 // Program Counter + SP uint8 // Stack Pointer + Status uint8 // Status Flags + Bus Bus // Connection to the Bus +} + +func (c *CPU) Reset() { + c.A = 0 + c.X = 0 + c.Y = 0 + c.SP = 0xFD + c.Status = 0x24 + c.PC = 0x0000 +} + +// Small helper func to flip the status register bits +func (c *CPU) setFlag(flag uint8, set bool) { + if set { + c.Status |= flag + } else { + c.Status &= ^flag + } +} + +// fetch -> decode -> execute cycle +func (c *CPU) Step() { + // fetch with program counter and bus memory + opcode := c.Bus.Read(c.PC) + + fmt.Printf("Executing at PC: %04X | Opcode: %02X\n", c.PC, opcode) + + // decode + exe need to split this out to handle all instructions etc + // Look at : https://www.ahl27.com/posts/2023/01/6502-emu1/ + switch opcode { + case 0xEA: //NOP + // does nothing its a no op + default: + fmt.Printf("Unknown opcode: %02X\n", opcode) + } + + // Move the Program counter + c.PC++ +} + + + + + + + + + + + + + + + +