Skip to content

Commit acea34b

Browse files
committed
feat: add docs
1 parent 6cf91cd commit acea34b

9 files changed

Lines changed: 72 additions & 10 deletions

File tree

pkg/chip8/audio.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,29 @@ const (
1313
AudioXOChip // XO-Chip 16-byte pattern
1414
)
1515

16+
// Audio represents the CHIP-8 / XO-CHIP sound generator state.
17+
//
18+
// It models the programmable audio pattern, pitch, and playback
19+
// position used by the virtual machine. The Audio type contains
20+
// no host-specific audio output logic and is controlled by the sound timer.
1621
type Audio struct {
17-
pattern [16]byte // 128 bits
18-
pitch byte // default in xochip is 64 meaning 4000 Hz
19-
st byte
20-
phase float64 // position inside pattern
21-
mode AudioMode
22+
// pattern is a 128-bit (16-byte) audio pattern buffer.
23+
// Each bit represents one step of the waveform.
24+
pattern [16]byte
25+
26+
// pitch controls the playback frequency of the pattern.
27+
// In XO-CHIP, the default value is 64, corresponding to 4000 Hz.
28+
pitch byte
29+
30+
// st is the sound timer. While ST > 0, audio playback is active.
31+
st byte
32+
33+
// phase represents the current playback position within the pattern.
34+
// It is expressed as a fractional index to allow sub-step advancement.
35+
phase float64
36+
37+
// mode selects the active audio mode (e.g. CHIP-8 or XO-CHIP).
38+
mode AudioMode
2239
}
2340

2441
func NewAudio() Audio {

pkg/chip8/cpu.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
const opSize = 2
88

9+
// CPU represents the CHIP-8 processor state.
910
type CPU struct {
1011
v [16]byte
1112
i uint16
@@ -52,7 +53,11 @@ func (c *CPU) fetch(memory *Memory) uint16 {
5253
return opcode
5354
}
5455

55-
func (c *CPU) execute(op uint16, memory *Memory, display *Display, keypad *Keypad, audio *Audio) {
56+
// Execute executes a single instruction.
57+
//
58+
// It fetches the opcode at PC, decodes it, executes it,
59+
// and updates timers as specified by the specification.
60+
func (c *CPU) Execute(op uint16, memory *Memory, display *Display, keypad *Keypad, audio *Audio) {
5661
switch op & 0xF000 {
5762
case 0x0000:
5863
switch op & 0x00FF {

pkg/chip8/display.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ func (s Size) Area() int {
1919
return s.Width * s.Height
2020
}
2121

22+
// Display represents the CHIP-8 / XO-CHIP video state.
23+
//
24+
// It maintains one or more bitplanes, the active resolution mode,
25+
// and internal flags used to coordinate drawing and vertical blanking.
26+
// The Display type contains no rendering or host-specific output logic.
2227
type Display struct {
28+
// Planes contains the video bitplanes.
29+
// CHIP-8 uses a single plane; XO-CHIP supports up to four planes.
2330
Planes [4][]byte
2431
size Size
2532
dirty bool

pkg/chip8/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Package chip8 implements a pure CHIP-8 virtual machine.
2+
//
3+
// It implements all core components of the system, including the CPU,
4+
// memory, timers, display, sound, and input state. The package is fully
5+
// platform-agnostic and contains no host- or UI-specific code.
6+
package chip8

pkg/chip8/memory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var fontSets = [240]byte{
4747
0xFE, 0x66, 0x62, 0x64, 0x7C, 0x64, 0x60, 0x60, 0xF0, 0x00, // F
4848
}
4949

50+
// Memory represents the CHIP-8 address space.
5051
type Memory struct {
5152
bytes [MemorySize]byte
5253
}

pkg/chip8/vm.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ import (
55
"time"
66
)
77

8+
const (
9+
// Timer decrements at 60 Hz independent of CPU frequency.
10+
TimerHz = 60
11+
)
12+
813
type FrameState struct {
914
Dirty bool
1015
Beep bool
1116
}
1217

18+
// VM represents a complete CHIP-8 virtual machine instance.
19+
//
20+
// It aggregates all core subsystems required for execution, including
21+
// CPU, memory, display, input, and audio. A VM instance owns its internal
22+
// timing state and is responsible for coordinating CPU execution and
23+
// timer updates.
1324
type VM struct {
1425
CPU CPU
1526
Memory Memory
@@ -18,7 +29,6 @@ type VM struct {
1829
Audio Audio
1930
romSize int
2031
cpuHz float64
21-
timerHz float64
2232
cycleAccum float64
2333
timerAccum float64
2434
}
@@ -30,7 +40,6 @@ func NewVM() *VM {
3040
Display: NewDisplay(),
3141
Keypad: NewKeypad(),
3242
Audio: NewAudio(),
33-
timerHz: 60.0,
3443
cpuHz: DefaultConf.CPUHz(),
3544
}
3645
}
@@ -68,7 +77,7 @@ func (vm *VM) Reset() {
6877
func (vm *VM) Step() {
6978
if !vm.Display.pendingVBlank || !vm.CPU.Quirks.WaitVBlank {
7079
opcode := vm.CPU.fetch(&vm.Memory)
71-
vm.CPU.execute(opcode, &vm.Memory, &vm.Display, &vm.Keypad, &vm.Audio)
80+
vm.CPU.Execute(opcode, &vm.Memory, &vm.Display, &vm.Keypad, &vm.Audio)
7281
}
7382
}
7483

@@ -82,7 +91,7 @@ func (vm *VM) RunFrame(frameDelta time.Duration) FrameState {
8291
vm.Step()
8392
}
8493

85-
vm.timerAccum += vm.timerHz * dt
94+
vm.timerAccum += TimerHz * dt
8695

8796
for vm.timerAccum >= 1 {
8897
vm.timerAccum -= 1

pkg/db/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package db stores metadata about programs, ROMs and platforms.
2+
//
3+
// The package contains no emulator or application logic and is limited to data definitions and querying.
4+
package db

pkg/host/doc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Package host provides reusable, platform-facing infrastructure
2+
// shared across host applications.
3+
//
4+
// It contains code that integrates the CHIP-8 virtual machine with
5+
// concrete runtime environments (CLI, desktop, mobile, or web) and
6+
// may depend on pkg/chip8 and pkg/db.
7+
package host

pkg/host/emu.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
"github.com/mxmgorin/ch8go/pkg/db"
1111
)
1212

13+
// Emu represents a host-level emulator instance.
14+
//
15+
// It binds a CHIP-8 virtual machine to host-provided services such as
16+
// metadata lookup, palette configuration, and framebuffer management.
17+
// Emu owns execution control state (pause, timing) but does not contain
18+
// core emulation logic.
1319
type Emu struct {
1420
VM *chip8.VM
1521
MetaDB *db.MetaDB

0 commit comments

Comments
 (0)