Command line usage
The emulator requires a single argument:Path to the assembled binary file (4096 bytes)
Terminal mode
The emulator automatically sets the terminal to cbreak mode (character-at-a-time input with no echo). This mode is restored when the emulator exits.In cbreak mode, input is processed immediately without waiting for Enter, and typed characters are not echoed to the terminal.
Virtual devices
The emulator provides two virtual I/O devices connected via ROM ports:Keyboard device
The virtual keyboard allows programs to read user input.Port configuration
Connected to ROM ports 0-2:
- Port 0: Character high nibble
- Port 1: Character low nibble
- Port 2: Character ready flag
Monitor device
The virtual monitor allows programs to output text.Port configuration
Connected to ROM ports 3-5:
- Port 3: Character high nibble
- Port 4: Character low nibble
- Port 5: Character ready flag
Threading architecture
The emulator uses a multi-threaded design:CPU execution
The CPU executes instructions continuously via thesingle_step() method:
- Fetches instruction from program memory
- Decodes opcode
- Executes operation
- Updates program counter
Memory layout
The emulator initializes anIntel4004 structure with:
- PRAM: 4096 bytes of program memory (loaded from executable file)
- DRAM: 16 banks × 4 chips × 4 registers × 16 nibbles
- ROM ports: 16 ports for I/O operations
- Index registers: 16 registers (4 bits each)
- Program counter: 12-bit address
Port access synchronization
I/O port access is synchronized using an atomic lock:
This ensures keyboard input is not corrupted by concurrent access.
Cycle-accurate timing
The emulator implements cycle-accurate timing to match the original Intel 4004’s 740 kHz clock speed. Each instruction executes at the correct speed using busy-wait synchronization.Error handling
The emulator returns errors for:- Missing executable file argument
- File not found or cannot be opened
- Invalid binary format
- Thread spawn failures
Memory nibble access
The emulator provides a helper function to read DRAM nibbles using a 12-bit address:- Bits 8-11: Bank number (0-15)
- Bits 4-7: Chip number (0-15) and register (0-3)
- Bits 0-3: Character position (0-15)
Runtime behavior
The emulator runs until interrupted:- Press Ctrl+C to stop execution
- Terminal settings are restored on exit
- All threads terminate when main thread exits
The emulator implements cycle-accurate timing at 740 kHz, matching the original Intel 4004 clock speed. Programs run at authentic speed.