-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunbin.php
More file actions
executable file
·170 lines (136 loc) · 5.59 KB
/
runbin.php
File metadata and controls
executable file
·170 lines (136 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use EaterEmulator\ROM;
use EaterEmulator\RAM;
use EaterEmulator\SystemBus;
use EaterEmulator\Peripherals\VIA;
use EaterEmulator\Peripherals\ACIA;
use andrewthecoder\WDC65C02\CPU;
// Register shutdown function to restore terminal
register_shutdown_function(function () {
ACIA::restoreTerminal();
echo "\n";
});
// Handle Ctrl+C gracefully
pcntl_signal(SIGINT, function () {
ACIA::restoreTerminal();
echo "\n\nInterrupted.\n";
exit(0);
});
if ($argc < 2) {
echo "Usage: {$argv[0]} <binary_file> [clock_hz]\n";
echo "\nOptions:\n";
echo " binary_file - ROM file to load\n";
echo " clock_hz - Clock speed in Hz (default: 1000, 0 = unlimited)\n";
exit(1);
}
$binaryFile = $argv[1];
$clockHz = isset($argv[2]) ? (int)$argv[2] : 1000;
if (!file_exists($binaryFile)) {
echo "Error: File not found: {$binaryFile}\n";
exit(1);
}
// Clear screen and setup display
echo "\033[2J\033[H";
echo "╔═══════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Ben Eater 65c02 Breadboard Computer Emulator ║\n";
echo "╚═══════════════════════════════════════════════════════════════════════════════╝\n";
echo "ROM: {$binaryFile}\n";
if ($clockHz > 0) {
echo "Clock: " . number_format($clockHz) . " Hz\n";
} else {
echo "Clock: Unlimited\n";
}
echo "\n";
$rom = new ROM($binaryFile);
$ram = new RAM();
$via = new VIA();
$acia = new ACIA();
$bus = new SystemBus($ram, $rom);
$bus->addPeripheral($acia); // ACIA at $5000
$bus->addPeripheral($via); // VIA at $6000
$cpu = new CPU($bus);
$bus->setCpu($cpu);
$cpu->reset();
// Display fixed header
echo "┌─ VIA LEDs ────────────────────────────────────────────────────────────────────┐\n";
echo "│ Port A: ○ ○ ○ ○ ○ ○ ○ ○ [0x00] │\n";
echo "│ Port B: ○ ○ ○ ○ ○ ○ ○ ○ [0x00] │\n";
echo "└───────────────────────────────────────────────────────────────────────────────┘\n";
echo "┌─ Serial Console (ACIA) ───────────────────────────────────────────────────────┐\n";
// Save cursor position for LED updates
$ledLineA = 8; // Line number for Port A
$ledLineB = 9; // Line number for Port B
$consoleLine = 12; // Line where console output starts
$lastPortA = 0;
$lastPortB = 0;
$cycleCount = 0;
$startTime = microtime(true);
$usecsPerInstruction = $clockHz > 0 ? (1000000 / $clockHz) : 0;
$nextInstructionTime = microtime(true);
while (true) {
// Dispatch signals for Ctrl+C handling
if (function_exists('pcntl_signal_dispatch')) {
pcntl_signal_dispatch();
}
$cpu->step();
$cycleCount++;
if ($usecsPerInstruction > 0) {
$nextInstructionTime += $usecsPerInstruction / 1000000;
$sleepTime = $nextInstructionTime - microtime(true);
if ($sleepTime > 0) {
usleep((int)($sleepTime * 1000000));
}
}
// Check for LED changes every 100 cycles
if ($cycleCount % 100 === 0) {
$currentPortA = $via->getPortAOutput();
$currentPortB = $via->getPortBOutput();
if ($currentPortA !== $lastPortA || $currentPortB !== $lastPortB) {
// Update LED display
updateLEDs($ledLineA, $ledLineB, $currentPortA, $currentPortB);
$lastPortA = $currentPortA;
$lastPortB = $currentPortB;
}
}
}
// Move to bottom of display
echo "\n";
echo "└───────────────────────────────────────────────────────────────────────────────┘\n";
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
echo "\n";
echo "Execution complete.\n";
echo "Total cycles: {$cycleCount}\n";
echo "Elapsed time: " . number_format($elapsed, 3) . " seconds\n";
if ($elapsed > 0) {
echo "Effective speed: " . number_format($cycleCount / $elapsed, 0) . " Hz\n";
}
function updateLEDs(int $lineA, int $lineB, int $portA, int $portB): void
{
// Save current cursor position
echo "\033[s";
// Update Port A
echo "\033[{$lineA};1H"; // Move to line A
echo "│ Port A: " . formatLEDs($portA) . sprintf(" [0x%02X] │", $portA);
// Update Port B
echo "\033[{$lineB};1H"; // Move to line B
echo "│ Port B: " . formatLEDs($portB) . sprintf(" [0x%02X] │", $portB);
// Restore cursor position
echo "\033[u";
flush();
}
function formatLEDs(int $value): string
{
$leds = '';
for ($bit = 7; $bit >= 0; $bit--) {
$isOn = ($value & (1 << $bit)) !== 0;
if ($isOn) {
$leds .= "\033[31m ● \033[0m";
} else {
$leds .= " ○ ";
}
}
return $leds;
}