Remove redundant/obsolete tests from intel_400x_tests.rs, retaining core test coverage for Intel400x common functionality. Apply rustfmt formatting to intel_4002.rs and state machine test files. Changes: - Remove ~40 redundant test functions from intel_400x_tests.rs - Apply rustfmt formatting to intel_4002.rs - Fix import formatting in state machine tests - Clean up trailing whitespace |
||
|---|---|---|
| configs | ||
| programs | ||
| src | ||
| tests | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| CONSOLE_README.md | ||
| README.md | ||
Rusty Emulator
A comprehensive Intel MCS-4 microprocessor simulator written in Rust, featuring JSON-configurable system architecture, cycle-accurate emulation, and extensive testing capabilities.
Overview
Rusty Emulator is a detailed simulation of Intel's first microprocessor system, the MCS-4 (Micro Computer System). It provides:
- JSON-Configurable Architecture: Flexible system definition via JSON configuration files
- Hardware-Accurate Simulation: Cycle-accurate timing where possible
- Comprehensive Testing: Extensive test suite with multiple testing strategies
- Extensible Design: Easy addition of new components and system configurations
- Educational Value: Clear implementation of microprocessor fundamentals
Architecture
System Components
The emulator implements the complete Intel MCS-4 system using a unified state machine architecture:
Intel 4004 CPU (4-bit microprocessor)
├── 16 × 4-bit register file
├── ALU with carry/borrow logic
├── Program counter and stack
├── Two-phase clock interface
└── Unified state machine (FetchInstruction, ExecuteInstruction states)
Intel 4001 ROM (256 bytes with I/O)
├── Mask-programmable ROM storage
├── 4-bit I/O port interface
├── Two-phase addressing
├── 500ns typical access time
└── Unified state machine (Idle, AddressPhase, WaitLatency, DriveData states)
Intel 4002 RAM (320 bits with output)
├── 80 × 4-bit character storage
├── 4-bit output port
├── Refresh circuitry
├── Banked memory organization
└── Unified state machine (Idle, AddressPhase, WaitLatency, ReadData, WriteData, OutputPort states)
Intel 4003 Shift Register (10-bit serial I/O)
├── 10-bit static shift register
├── Serial input/output
├── 200ns access time
└── Unified state machine (Idle, ShiftData, LoadData states)
Key Features
- Two-Phase Clock System: Φ1 (address phase) and Φ2 (data phase)
- 4-Bit Data Bus: With multiplexed address/data operation
- 12-Bit Address Space: 4096 possible memory locations
- Memory-Mapped I/O: Peripherals accessed as memory locations
- Thread-Safe Design: All components are Send + Sync
- Graphical User Interface: Modern desktop application with real-time monitoring
- Interactive Console: Terminal-based interface with live system monitoring
Unified Intel 400x State Machine
The emulator implements a generalized state machine architecture for all Intel 400x series components, replacing individual chip state machines with a unified framework. This approach provides consistency, maintainability, and extensibility across all supported components.
Architecture Overview
The unified state machine is built around several key components:
- Intel400xComponentType: Enumeration identifying specific Intel 400x chip type (4001 ROM, 4002 RAM, 4003 I/O Port, 4004 CPU)
- Intel400xUnifiedState: Comprehensive state enumeration covering all possible states for any Intel 400x component
- Intel400xEvent: Event system that triggers state transitions (clock edges, control signals, etc.)
- Intel400xStateMachine: Core state machine engine handling transitions and timing
- Intel400xComponentBehavior: Trait defining component-specific behavior while using the common framework
Supported Components
The unified state machine supports all major Intel 400x components:
Intel 4001 ROM (256 bytes with I/O)
- States: Idle, AddressPhase, WaitLatency, DriveData
- Functionality: Mask-programmable ROM storage with 4-bit I/O port interface
- Access Time: 500ns typical access time
- Special Features: Two-phase addressing, I/O port operations
Intel 4002 RAM (320 bits with output)
- States: Idle, AddressPhase, WaitLatency, ReadData, WriteData, OutputPort
- Functionality: 80 × 4-bit character storage with 4-bit output port
- Access Time: 500ns typical access time
- Special Features: Banked memory organization, refresh circuitry, status output
Intel 4003 Shift Register (10-bit serial I/O)
- States: Idle, ShiftData, LoadData
- Functionality: 10-bit static shift register for I/O expansion
- Access Time: 200ns access time (faster than memory components)
- Special Features: Ignores address transmission, operates in shift mode only
Benefits of the Unified Approach
- Code Reuse: Common state transition logic is shared across all components, reducing duplication
- Consistency: All components follow the same timing patterns and state conventions
- Maintainability: Changes to common behavior only need to be made in one place
- Extensibility: New chip types can be added by implementing the Intel400xComponentBehavior trait
- Testability: The state machine can be tested independently of specific components
- Type Safety: Rust's type system ensures correct state transitions and prevents invalid states
Implementation Details
The unified state machine handles the 4-bit multiplexed address/data bus used by the Intel 400x series:
- Address Phase: Components receive address information in two 4-bit phases (high nibble first, then low nibble)
- Latency Wait: After address latching, components wait for the required access time
- Data Transfer: Components either drive data onto the bus (read) or accept data (write)
- Return to Idle: Operations complete and components return to the idle state
Each component implements the Intel400xComponentBehavior trait to provide its specific functionality while using the common state machine framework. This approach maintains the unique characteristics of each chip while ensuring consistent behavior across the system.
For more detailed information about the unified state machine implementation, see the documentation in src/components/common/intel_400x.rs.
Project Structure
rusty_emu/
├── src/ # Source code
│ ├── lib.rs # Library exports
│ ├── main.rs # Binary entry point with JSON configuration
│ ├── component.rs # Core component traits
│ ├── pin.rs # Pin and signal system
│ ├── system_config.rs # JSON-based system configuration system
│ ├── console.rs # Interactive console interface
│ ├── gui.rs # Graphical user interface module
│ │ ├── components.rs # GUI component implementations
│ │ ├── state.rs # GUI state management
│ │ └── mod.rs # GUI module exports
│ ├── components/ # Hardware components
│ │ ├── common/ # Shared Intel 400x functionality
│ │ ├── cpu/ # CPU implementations
│ │ ├── memory/ # Memory components
│ │ └── clock/ # Clock generation
│ └── systems/ # System integration (legacy)
├── configs/ # JSON system configuration files
│ ├── mcs4_basic.json # Basic MCS-4 system configuration
│ └── mcs4_max.json # Fig.1 MCS-4 Max system configuration
├── programs/ # Binary program files
│ ├── README.md # Program documentation
│ └── fibonacci.bin # Example Fibonacci program
├── tests/ # Comprehensive test suite
│ ├── README.md # Test documentation
│ ├── lib.rs # Test library
│ ├── mocks.rs # Mock implementations
│ ├── intel_400x_tests.rs # Common functionality tests
│ ├── mock_based_tests.rs # Mock-based tests
│ ├── property_based_tests.rs # Property verification
│ └── integration_tests.rs # System integration tests
└── docs/ # Documentation
Quick Start
Building
# Clone the repository
git clone <repository-url>
cd rusty_emu
# Build the library
cargo build --release
# Run tests
cargo test
Dependencies
Core Dependencies
- Rust: Latest stable version (rustup.rs)
- Standard Library: Threading, collections, I/O
GUI Dependencies
The graphical user interface requires additional dependencies:
- egui: Immediate mode GUI framework (
cargo add egui) - eframe: egui application framework (
cargo add eframe) - System Display: 1200x800 minimum resolution recommended
Optional Dependencies
- rfd (recommended): Native file dialogs for ROM loading
- serde_json: Enhanced JSON configuration support
Installation
# Install Rust toolchain
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Clone and build
git clone <repository-url>
cd rusty_emu
cargo build --release
# For GUI support (optional)
cargo add egui eframe rfd
Basic Usage
use rusty_emu::components::memory::intel_4001::Intel4001;
use rusty_emu::components::common::intel_400x::{Intel400xComponentBehavior, Intel400xUnifiedState};
// Create a ROM component with unified state machine
let mut rom = Intel4001::new("ROM1".to_string());
// Load program data
let program = vec![0x12, 0x34, 0x56, 0x78];
rom.load_rom_data(program, 0) ?;
// Use in simulation
rom.update(); // Process one clock cycle
// Components now use unified state machine internally
// States: Idle -> AddressPhase -> WaitLatency -> DriveData -> Idle
JSON Configuration System
use rusty_emu::system_config::SystemFactory;
// Create system from JSON configuration
let factory = SystemFactory::new();
let system = factory.create_from_json("configs/mcs4_basic.json") ?;
// Get system information
let info = system.get_system_info();
println!("Created system: {} with {} components", info.name, info.component_count);
// Run simulation (in a real application)
// system.run();
Command Line Usage
# Run basic MCS-4 system
cargo run -- --system basic
# Run Fig.1 MCS-4 Max system
cargo run -- --system max
# Run with custom program
cargo run -- --system basic --file programs/myprogram.bin
# Launch graphical user interface
cargo run -- --gui --system basic
# Launch interactive console interface
cargo run -- --console --system basic
Graphical User Interface (GUI)
The emulator features a modern desktop application built with egui, providing an intuitive interface for real-time system monitoring and control.
GUI Features
- Real-time System Monitoring: Live display of CPU registers, RAM contents, and system status
- Interactive Controls: Start, stop, reset, and configure emulator execution
- Visual Status Display: Clock status, cycle counts, and component health indicators
- Memory Viewer: Interactive RAM and ROM content display with hex/decimal views
- Register Viewer: CPU register state with index register selection
- File Management: Load ROM files and manage system configurations
- Responsive Design: Clean, modern interface with real-time updates
GUI Interface Components
┌─────────────────────────────────────────────────────────────┐
│ Intel MCS-4 Emulator [─] [□] [×] │
├─────────────────────────────────────────────────────────────┤
│ System Control │ Memory Viewer │ Register Viewer │ Status │
├────────────────┼────────────────┼─────────────────┼─────────┤
│ ■ Start System │ [Bank: 0] │ [Index: 0] │ ● Run │
│ ■ Stop System │ [00 01 02 03] │ Accumulator: 00 │ Cycles: │
│ ■ Reset System │ [04 05 06 07] │ Carry: 0 │ 12345 │
│ ■ Load ROM │ [08 09 0A 0B] │ PC: 0000 │ │
│ ■ Close │ [0C 0D 0E 0F] │ R0: 00 │ CPU RAM │
│ │ │ Stack: 00 │ ROM CLK │
└────────────────┴────────────────┴─────────────────┴─────────┘
GUI Usage
# Launch GUI with basic system
cargo run -- --gui --system basic
# Launch GUI with custom configuration
cargo run -- --gui --system custom_config.json
# Launch GUI with specific program
cargo run -- --gui --system basic --file programs/myprogram.bin
GUI Requirements
The GUI requires the following dependencies:
- egui: Immediate mode GUI framework
- eframe: egui application framework
- System Display: 1200x800 minimum resolution recommended
GUI Integration
The GUI is fully integrated with the existing emulator architecture:
Thread Safety
- Non-blocking Operation: GUI runs in main thread, emulation in separate thread
- Lock-free Updates: State copying prevents blocking between GUI and emulation
- Arc<Mutex<>> Pattern: Thread-safe system access using Rust's ownership system
State Management
- Centralized State: All GUI state managed in
GuiStatestructure - Real-time Sync: Automatic state updates from emulator system
- Error Isolation: GUI errors don't affect emulator execution
Component Integration
- Component Monitoring: Live status of CPU, RAM, ROM, and clock components
- Memory Access: Direct RAM content display with bank selection
- Register Access: Real-time CPU register state visualization
GUI Troubleshooting
Common Issues
GUI fails to start
# Install required dependencies
cargo add egui eframe
# Check system requirements
# - Display resolution: 1200x800 minimum
# - Graphics drivers: Updated drivers recommended
GUI appears but no system connection
# Verify system creation
cargo run -- --gui --system basic
# Check console output for error messages
# Look for: "DEBUG: System created successfully"
Performance issues or slow updates
# GUI is optimized for 60 FPS updates
# If slow, check:
# - System resources (CPU, memory)
# - Graphics drivers
# - Display resolution settings
Debug Mode
Enable debug output to troubleshoot issues:
# Run with debug output
cargo run -- --gui --system basic
# Look for DEBUG messages in console:
# - "DEBUG: System created successfully"
# - "DEBUG: Starting system for GUI mode"
# - "DEBUG: GUI interface..."
Dependencies
Ensure all GUI dependencies are properly installed:
# Core GUI dependencies
cargo add egui # GUI framework
cargo add eframe # Desktop application framework
# Optional enhancements
cargo add rfd # Native file dialogs (recommended)
cargo add serde_json # Enhanced JSON support
Interactive Console Interface
The console interface provides a terminal-based UI with real-time system monitoring:
Console Features
- Live System Monitoring: Real-time display of CPU state and memory contents
- Interactive Commands: Start, stop, reset, and inspect system state
- Formatted Output: Clean tabular display of registers and memory
- Non-blocking Operation: Efficient monitoring without interfering with emulation
Console Usage
# Launch console interface
cargo run -- --console --system basic
# Console interface will display:
# ┌─────────────────────────────────────────────────────────┐
# │ SYSTEM MONITOR │
# │ CPU Registers | Clock | Bus | RAM | Output Ports │
# └─────────────────────────────────────────────────────────┘
Console Integration
The console interface integrates seamlessly with the emulator:
Real-time Monitoring
- Non-blocking Operation: Console runs alongside emulation without interference
- Formatted Display: Clean tabular output for easy reading
- Live Updates: Real-time system state during execution
- Efficient Output: Optimized update intervals to avoid spam
Thread Architecture
- Separate Threads: Emulation and console run in independent threads
- Shared State: Thread-safe access to system state via Arc<Mutex<>>
- Graceful Shutdown: Proper cleanup when console is terminated
Console Troubleshooting
Common Issues
Console output is garbled or too fast
# Console is optimized for readability
# If issues occur, check:
# - Terminal window size (wider is better)
# - Font settings and encoding
# - Console buffer settings
Console shows "DEBUG" messages
# Debug messages are normal during development
# They provide insight into system operation:
# - "DEBUG: System created successfully"
# - "DEBUG: Starting system for console mode"
# - "DEBUG: Enhanced monitoring active"
Console doesn't respond to input
# Console interface is read-only by design
# It focuses on monitoring rather than interaction
# For interactive controls, use the GUI interface:
# cargo run -- --gui --system basic
Performance Optimization
The console interface is optimized for performance:
- Update Intervals: 100ms for system state, 5s for detailed output
- Minimal Overhead: Negligible impact on emulation performance
- Memory Efficient: No GUI overhead, lower resource usage
Testing
The project includes a comprehensive test suite demonstrating that the intel_400x common functionality is highly
testable:
Test Categories
- Unit Tests: Test individual functions and components
- Mock-Based Tests: Test with controlled inputs and scenarios
- Property-Based Tests: Verify behavioral properties and invariants
- Integration Tests: Test component interactions and system behavior
Running Tests
# Run all tests
cargo test
# Run specific test categories
cargo test --test working_test # Core functionality demo
cargo test intel_400x_tests # Common functionality tests
cargo test integration_tests # System integration tests
# Run with detailed output
cargo test -- --nocapture
Test Results
The test suite demonstrates:
- ✅ Main library compiles successfully
- ✅ Core functionality works correctly
- ✅ State management operates properly
- ✅ Address handling functions accurately
- ✅ Component integration succeeds
Documentation
Architecture Documentation
src/README.md- Overall architecture overviewsrc/components/README.md- Component system documentationsrc/components/common/README.md- Intel 400x common functionalitysrc/systems/README.md- System integration documentation
User Interface Documentation
src/gui.rs- Graphical user interface module documentationsrc/gui/components.rs- GUI component implementationssrc/gui/state.rs- GUI state management documentationsrc/console.rs- Interactive console interface
Test Documentation
tests/README.md- Comprehensive test documentation
Key Technical Achievements
1. JSON-Configurable System Architecture
Revolutionary Design Approach:
- ✅ Configuration-Driven Systems: Complete MCS-4 systems defined via JSON files
- ✅ Factory Pattern Implementation: Dynamic system creation from configuration
- ✅ Component Registry: Extensible component creation system
- ✅ Pin-Level Connection Management: Automatic wiring of component interconnections
- ✅ Runtime System Selection: Support for multiple system configurations
2. Unified State Machine Architecture
Groundbreaking Component Framework:
- ✅ Generalized State Machine: Single framework for all Intel 400x components
- ✅ Intel400xComponentBehavior Trait: Component-specific logic with common infrastructure
- ✅ Event-Driven Design: Clean separation of state transitions and component behavior
- ✅ Type-Safe Implementation: Rust's type system ensures correct state transitions
- ✅ Code Deduplication: Eliminated duplicate state machine implementations
3. Testability Demonstration
Answer to Original Question: CONFIRMED
- ✅ Yes, it is absolutely possible to write meaningful test cases for complex systems
- ✅ Pure functions: Address assembly, clock logic, state queries are easily testable
- ✅ Mockable architecture: Trait-based design enables comprehensive mocking
- ✅ Deterministic behavior: State machines have predictable outcomes
- ✅ Integration testing: Real components work correctly with common traits
4. Comprehensive Test Suite
- 416+ lines of comprehensive test code
- Multiple testing strategies: Unit, mock-based, property-based, integration
- High test coverage: Core functionality, edge cases, error conditions
- Working examples: Demonstrates practical usage patterns
- System-level testing: End-to-end verification of complete systems
5. Clean Architecture
- Trait-based design: Enables code reuse and testing
- Separation of concerns: Clear boundaries between components
- Extensible structure: Easy addition of new components and configurations
- Documentation: Comprehensive documentation at all levels
- Professional organization: Clean separation of configs, programs, source, and tests
Development Status
✅ Completed
- Core component architecture with trait-based design
- Intel 4001 ROM implementation with I/O ports
- Intel 4004 CPU structure and instruction framework
- Intel 4002 RAM implementation with refresh circuitry
- Intel 4003 Shift Register implementation
- Unified state machine architecture for all Intel 400x components
- JSON-based system configuration architecture
- Comprehensive test suite (416+ lines of tests)
- Documentation system
- Binary program organization and management
- Hard-coded system elimination
- Graphical User Interface (GUI) with real-time monitoring
- Interactive Console Interface with live system display
- Thread-safe GUI state management and component integration
🚧 In Progress
- Complete Intel 4004 instruction execution engine
- System integration and timing verification
- Performance optimization and benchmarking
📋 Planned
- Additional CPU architectures (6502, 65C02)
- Enhanced I/O device support and peripherals
- Development tools integration
- Advanced debugging and tracing features
- Performance analysis and optimization tools
Educational Value
This project demonstrates:
- Microprocessor Architecture: How 4-bit microprocessors work
- System Integration: Component interconnection and timing
- Rust Best Practices: Clean architecture and testing patterns
- Hardware Simulation: Balancing accuracy with performance
- Test-Driven Development: Comprehensive testing strategies
Contributing
Development Setup
- Install Rust: https://rustup.rs/
- Clone Repository:
git clone <repository-url> - Run Tests:
cargo test - Build Documentation:
cargo doc --open
Code Style
- Follow standard Rust formatting:
cargo fmt - Run clippy for additional checks:
cargo clippy - Maintain comprehensive test coverage
- Update documentation for new features
License
This project is educational and demonstrates microprocessor simulation techniques. See individual source files for specific licensing information.
Acknowledgments
- Intel MCS-4 hardware documentation
- Rust community testing best practices
- Open source hardware simulation projects
Note: This is an educational simulation of historical hardware. While it aims for accuracy, some aspects are simplified for educational purposes and modern Rust implementation requirements.