… | |
… | |
15 | # |
15 | # |
16 | |
16 | |
17 | # If this file contains embedded ROMs, the above copyright notice does |
17 | # If this file contains embedded ROMs, the above copyright notice does |
18 | # not apply to them. |
18 | # not apply to them. |
19 | |
19 | |
|
|
20 | use 5.010; |
20 | use strict; |
21 | use strict; |
|
|
22 | use integer; |
21 | #use common::sense; |
23 | #use common::sense; |
22 | |
24 | |
23 | my $VT102 = 1; |
25 | my $VT102 = 1; |
24 | my $VT131 = 0; |
26 | my $VT131 = 0; |
25 | my $AVO = 1; |
27 | my $AVO = 1; |
26 | my $KBD = 1; |
|
|
27 | |
28 | |
28 | if ($ARGV[0] =~ /^-?-vt100$/) { |
29 | shift, ($VT102 = 0), ($AVO = 0) if $ARGV[0] =~ /^-?-vt100$/; |
29 | shift; $VT102 = 0; $AVO = 0; |
30 | shift, ($VT102 = 0) if $ARGV[0] =~ /^-?-vt100\+avo$/; |
30 | } |
31 | shift if $ARGV[0] =~ /^-?-vt102$/; |
|
|
32 | shift, ($VT131 = 1) if $ARGV[0] =~ /^-?-vt131$/; |
31 | |
33 | |
32 | if ($ARGV[0] =~ /^-?-vt100\+avo$/) { |
34 | # vt100 wps = word processing roms |
33 | shift; $VT102 = 0; $AVO = 1; |
35 | # vt101 = vt102 - avo, but custom rom? really? |
34 | } |
36 | # vt103 = vt100 + tu58 tape drive |
35 | |
37 | # vt125 = vt100 + gpo graphics processor |
36 | if ($ARGV[0] =~ /^-?-vt102$/) { |
38 | # vt132 = vt100 + avo, stp |
37 | shift; # default |
39 | # vt180 = vt100 + z80 cp/m |
38 | } |
|
|
39 | |
|
|
40 | if ($ARGV[0] =~ /^-?-vt131$/) { |
|
|
41 | shift; $VT131 = 1; |
|
|
42 | } |
|
|
43 | |
40 | |
44 | if ($ARGV[0] =~ /^-/) { |
41 | if ($ARGV[0] =~ /^-/) { |
45 | die <<EOF; |
42 | die <<EOF; |
46 | |
43 | |
47 | VT102, A VT100/101/102/131 SIMULATOR |
44 | VT102, A VT100/102/131 SIMULATOR |
48 | |
45 | |
49 | Usage: |
46 | Usage: |
50 | |
47 | |
51 | $0 [option] [program [args]] |
48 | $0 [option] [program [args]] |
52 | |
49 | |
… | |
… | |
69 | SET UP Home |
66 | SET UP Home |
70 | BACKSPACE Rubout |
67 | BACKSPACE Rubout |
71 | CAPS LOCK Prior/PgUp |
68 | CAPS LOCK Prior/PgUp |
72 | NO SCROLL Next/PgDown |
69 | NO SCROLL Next/PgDown |
73 | BREAK End |
70 | BREAK End |
|
|
71 | CTRL-C Insert |
74 | |
72 | |
75 | Set-Up Guide: |
73 | Set-Up Guide: |
76 | |
74 | |
77 | http://vt100.net/docs/vt102-ug/chapter3.html#S3.6 |
75 | http://vt100.net/docs/vt102-ug/chapter3.html#S3.6 |
78 | |
76 | |
… | |
… | |
83 | EOF |
81 | EOF |
84 | } |
82 | } |
85 | |
83 | |
86 | ############################################################################# |
84 | ############################################################################# |
87 | # ROM/hardware init |
85 | # ROM/hardware init |
|
|
86 | |
|
|
87 | my $PTY; # the pty we allocated, if any |
|
|
88 | my $KBD = 1; |
88 | |
89 | |
89 | my $ROMS = do { |
90 | my $ROMS = do { |
90 | binmode DATA; |
91 | binmode DATA; |
91 | local $/; |
92 | local $/; |
92 | <DATA> |
93 | <DATA> |
93 | }; |
94 | }; |
94 | |
95 | |
95 | 0x6801 == length $ROMS or die "corrupted rom image"; |
96 | 0x6801 == length $ROMS or die "corrupted rom image"; |
96 | |
97 | |
97 | binmode STDOUT; |
|
|
98 | |
|
|
99 | my @M = (0xff) x 65536; # main memory, = (0xff) x 65536; |
98 | my @M = (0xff) x 65536; # main memory |
100 | |
99 | |
101 | # populate mem with rom contents |
100 | # populate mem with rom contents |
102 | if ($VT102) { |
101 | if ($VT102) { |
103 | @M[0x0000 .. 0x1fff] = unpack "C*", substr $ROMS, 0x2000, 0x2000; |
102 | @M[0x0000 .. 0x1fff] = unpack "C*", substr $ROMS, 0x2000, 0x2000; |
104 | @M[0x8000 .. 0x9fff] = unpack "C*", substr $ROMS, 0x4000, 0x2000; |
103 | @M[0x8000 .. 0x9fff] = unpack "C*", substr $ROMS, 0x4000, 0x2000; |
… | |
… | |
108 | } |
107 | } |
109 | |
108 | |
110 | ############################################################################# |
109 | ############################################################################# |
111 | # 8085 CPU registers and I/O support |
110 | # 8085 CPU registers and I/O support |
112 | |
111 | |
113 | my $PTY; # the pty we allocated, if any |
|
|
114 | |
|
|
115 | # 8080/8085 registers |
112 | # 8080/8085 registers |
116 | # b, c, d, e, h, l, a |
113 | my ($A, $B, $C, $D, $E, $H, $L); # 8 bit general purpose |
117 | my ($A, $B, $C, $D, $E, $H, $L, $A); |
114 | my ($PC, $SP, $IFF); # program counter, stack pointer, interrupt flag |
118 | my ($PC, $SP, $IFF, $FA, $FZ, $FS, $FP, $FC); |
115 | my ($FA, $FZ, $FS, $FP, $FC); # condition codes (psw) |
119 | |
116 | |
120 | my $RST = 0; # 8080 pending interrupts |
117 | my $RST = 0; # pending interrupts (external interrupt logic) |
121 | my $INTMASK = 7; # 8085 half interrupts |
118 | my $INTMASK = 7; # 8085 half interrupt mask |
122 | my $INTPEND = 0; # 8085 half interrupts |
119 | my $INTPEND = 0; # 8085 half interrupts pending |
123 | |
120 | |
124 | my $x; # dummy temp for instructions |
121 | my $CLK; # rather inexact clock, counts extended basic blocks |
125 | |
|
|
126 | my $CLK; # rather inexact clock |
|
|
127 | |
122 | |
128 | ############################################################################# |
123 | ############################################################################# |
129 | # the dreaded NVR1400 chip. not needed to get it going, but provided anyway |
124 | # the dreaded NVR1400 chip. not needed to get it going, but provided anyway |
130 | |
125 | |
131 | # nvram |
126 | # nvram |
… | |
… | |
143 | sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase |
138 | sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase |
144 | sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read |
139 | sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read |
145 | sub { }, # 7 standby |
140 | sub { }, # 7 standby |
146 | ); |
141 | ); |
147 | |
142 | |
148 | my @bitidx; |
143 | my @NVR_BITIDX; $NVR_BITIDX[1 << $_] = 9 - $_ for 0..9; |
149 | $bitidx[1 << $_] = 9 - $_ for 0..9; |
|
|
150 | |
144 | |
151 | # the nvr1400 state machine. what a monster |
145 | # the nvr1400 state machine. what a monster |
152 | sub nvr() { |
146 | sub nvr() { |
153 | my $a1 = $bitidx[(~$NVRADDR ) & 0x3ff]; |
147 | my $a1 = $NVR_BITIDX[(~$NVRADDR ) & 0x3ff]; |
154 | my $a0 = $bitidx[(~$NVRADDR >> 10) & 0x3ff]; |
148 | my $a0 = $NVR_BITIDX[(~$NVRADDR >> 10) & 0x3ff]; |
155 | |
|
|
156 | # printf "NVR %02x A %020b %d %d D %02x\n", $NVRLATCH, $NVRADDR & 0xfffff, $a1, $a0, $NVRDATA; |
|
|
157 | |
149 | |
158 | $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) |
150 | $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) |
159 | } |
151 | } |
160 | |
152 | |
161 | ############################################################################# |
153 | ############################################################################# |
162 | # I/O ports - output |
154 | # I/O ports - output |
163 | |
155 | |
164 | my $DC11_REVERSE = 0; |
156 | my $DC11_REVERSE = 0; # light background? |
165 | |
157 | |
166 | my $XON = 1; # false if terminal wants us to pause |
158 | my $XON = 1; # false if terminal wants us to pause |
167 | my $PUSARTCMD; |
159 | my $PUSARTCMD; |
168 | |
160 | |
|
|
161 | my $KSTATUS; # keyboard status (click + scan flag + leds) |
169 | my @KXMIT; # current scan queue |
162 | my @KXMIT; # current scan queue |
170 | my %KXMIT; # currently pressed keys |
163 | my %KXMIT; # currently pressed keys |
171 | my @KQUEUE; # key event queue |
164 | my @KQUEUE; # key event queue |
172 | my $KXCNT; # count for debouncew |
165 | my $KXCNT; # count for debouncew |
173 | my @PUSARTRECV; |
166 | |
174 | my $KSTATUS; |
167 | my @PUSARTRECV; # serial input (to terminal) queue |
175 | |
168 | |
176 | sub out_00 { # pusartdata |
169 | sub out_00 { # pusartdata |
177 | # handle xon/xoff, but also pass it through |
170 | # handle xon/xoff, but also pass it through |
178 | if ($_[0] == 0x13) { |
171 | if ($_[0] == 0x13) { |
179 | $XON = 0; |
172 | $XON = 0; |
… | |
… | |
186 | syswrite $PTY, chr $_[0]; |
179 | syswrite $PTY, chr $_[0]; |
187 | |
180 | |
188 | $INTPEND |= 1; |
181 | $INTPEND |= 1; |
189 | } |
182 | } |
190 | |
183 | |
191 | sub out_01 { |
184 | sub out_01 { # pusartcmd |
192 | $PUSARTCMD = shift; |
185 | $PUSARTCMD = shift; |
193 | |
186 | |
194 | $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy |
187 | $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy |
195 | $INTPEND |= 2 if $PUSARTCMD & 0x04 && !@PUSARTRECV; # VT102, 6.5 rxrdy, needed for some reason |
188 | $INTPEND |= 2 if $PUSARTCMD & 0x04 && !@PUSARTRECV; # VT102, 6.5 rxrdy, needed for some reason |
196 | } |
189 | } |
197 | |
190 | |
198 | sub out_02 { } # baudrate generator |
191 | sub out_02 { } # baudrate generator |
199 | |
192 | |
200 | sub out_23 { } # unknown |
193 | sub out_23 { } # vt102 unknown |
201 | sub out_27 { } # unknown |
194 | sub out_27 { } # vt102 unknown |
202 | sub out_2f { } # unknown, connected to in 0f |
195 | sub out_2f { } # vt102 unknown, connected to in 0f |
203 | |
196 | |
204 | sub out_42 { } # brightness |
197 | sub out_42 { } # brightness |
205 | |
198 | |
206 | sub out_62 { |
199 | sub out_62 { # nvr latch register (4 bits) |
207 | $NVRLATCH = shift; |
200 | $NVRLATCH = shift; |
208 | } |
201 | } |
209 | |
202 | |
210 | sub out_a2 { |
203 | sub out_a2 { # device control 011 |
211 | my $dc11 = 0x0f & shift; |
204 | my $dc11 = 0x0f & shift; |
212 | |
205 | |
213 | $DC11_REVERSE = 1 if $dc11 == 0b1010; |
206 | $DC11_REVERSE = 1 if $dc11 == 0b1010; |
214 | $DC11_REVERSE = 0 if $dc11 == 0b1011; |
207 | $DC11_REVERSE = 0 if $dc11 == 0b1011; |
215 | } |
208 | } |
216 | |
209 | |
217 | sub out_c2 { } # unknown |
210 | sub out_c2 { } # unknown |
218 | sub out_d2 { } # 0..3 == 80c/132c/60hz/50hz |
211 | sub out_d2 { } # device control 012, 0..3 == 80c/132c/60hz/50hz |
219 | |
212 | |
220 | sub out_82 { |
213 | sub out_82 { # keyboard txmit |
221 | # keyboard |
214 | # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4 (vt100) |
222 | |
|
|
223 | # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 |
215 | # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 (vt102) |
224 | # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4 |
|
|
225 | $KSTATUS = $_[0]; |
216 | $KSTATUS = $_[0]; |
226 | |
217 | |
227 | # start new scan unless scan in progress |
218 | # start new scan unless scan is in progress |
228 | if (($_[0] & 0x40) && !@KXMIT) { |
219 | if (($_[0] & 0x40) && !@KXMIT) { |
229 | # do not reply with keys in locked mode |
220 | # do not reply with keys in locked mode |
230 | # or during post (0xff), |
221 | # or during post (0xff), |
231 | # mostly to skip init and not fail POST, |
222 | # mostly to skip init and not fail POST, |
232 | # and to send startup keys only when terminal is ready |
223 | # and to send startup keys only when terminal is ready |
… | |
… | |
254 | } |
245 | } |
255 | |
246 | |
256 | ############################################################################# |
247 | ############################################################################# |
257 | # I/O ports - input |
248 | # I/O ports - input |
258 | |
249 | |
259 | my $NVRBIT; |
250 | my $NVRBIT; # the current nvr data bit |
260 | my $LBA6; # twice the frequenxy of LBA7 |
251 | my $LBA6; # twice the frequenxy of LBA7 |
261 | |
252 | |
262 | sub in_00 { # pusart data |
253 | sub in_00 { # pusart data |
263 | # interrupt not generated here, because infinite |
254 | # interrupt not generated here, because infinite |
264 | # speed does not go well with the vt102. |
255 | # speed does not go well with the vt102. |
265 | |
256 | |
… | |
… | |
269 | sub in_01 { # pusart status |
260 | sub in_01 { # pusart status |
270 | # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY |
261 | # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY |
271 | 0x85 + (@PUSARTRECV && 0x02) |
262 | 0x85 + (@PUSARTRECV && 0x02) |
272 | } |
263 | } |
273 | |
264 | |
274 | sub in_22 { # modem buffer(?) |
265 | sub in_22 { # modem buffer |
275 | # wild guess: -CTS -SPDI -RI -CD 0 0 0 0 |
266 | # wild guess: -CTS -SPDI -RI -CD 0 0 0 0 |
276 | 0x20 |
267 | 0x20 |
277 | } |
268 | } |
278 | |
269 | |
279 | sub in_0f { 0xff } # vt102 unknown, connected to out 2f |
270 | sub in_0f { 0xff } # vt102 unknown, connected to out 2f |
… | |
… | |
306 | sub in_1b { 0xff } # vt102 unknown |
297 | sub in_1b { 0xff } # vt102 unknown |
307 | |
298 | |
308 | ############################################################################# |
299 | ############################################################################# |
309 | # 8085 cpu opcodes and flag handling |
300 | # 8085 cpu opcodes and flag handling |
310 | |
301 | |
|
|
302 | my $x; # dummy scratchpad for opcodes |
|
|
303 | |
311 | sub sf { # set flags (ZSC - AP not implemented) |
304 | sub sf { # set flags, full version (ZSC - AP not implemented) |
312 | $FS = $_[0] & 0x080; |
305 | $FS = $_[0] & 0x080; |
313 | $FZ = !($_[0] & 0x0ff); |
306 | $FZ = !($_[0] & 0x0ff); |
314 | $FC = $_[0] & 0x100; |
307 | $FC = $_[0] & 0x100; |
315 | |
308 | |
316 | $_[0] &= 0xff; |
309 | $_[0] &= 0xff; |
317 | } |
310 | } |
318 | |
311 | |
319 | sub sf8 { # set flags (ZSC - AP not implemented) |
312 | sub sf8 { # set flags, for 8-bit results (ZSC - AP not implemented) |
320 | $FS = $_[0] & 0x080; |
313 | $FS = $_[0] & 0x080; |
321 | $FZ = !($_[0] & 0x0ff); |
314 | $FZ = !($_[0] & 0x0ff); |
322 | $FC = 0; |
315 | $FC = 0; |
323 | } |
316 | } |
324 | |
317 | |
325 | sub sf_nc { # set flags except carry |
318 | sub sf_nc { # set flags, except carry |
326 | $FS = $_[0] & 0x080; |
319 | $FS = $_[0] & 0x080; |
327 | $FZ = ($_[0] & 0x0ff) == 0; |
320 | $FZ = ($_[0] & 0x0ff) == 0; |
328 | |
321 | |
329 | $_[0] &= 0xff; |
322 | $_[0] &= 0xff; |
330 | } |
323 | } |
331 | |
324 | |
|
|
325 | # opcode table |
332 | my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff; |
326 | my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff; |
333 | |
327 | |
334 | my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); |
328 | my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); # r/m encoding |
335 | my @cc = ('if !$FZ', 'if $FZ', 'if !$FC', 'if $FC', ';die', ';die', 'if !$FS', 'if $FS'); # die == unimplemented $FP parity |
329 | my @cc = ('!$FZ', '$FZ', '!$FC', '$FC', 'die;', 'die;', '!$FS', '$FS'); # cc encoding. die == unimplemented $FP parity |
336 | |
330 | |
337 | $op[0x00] = ''; |
331 | $op[0x00] = ''; # nop |
338 | |
332 | |
339 | # mov r,r / r,M / M,r |
333 | # mov r,r / r,M / M,r |
340 | for my $s (0..7) { |
334 | for my $s (0..7) { |
341 | for my $d (0..7) { |
335 | for my $d (0..7) { |
342 | $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov |
336 | $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov |
… | |
… | |
382 | $op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr |
376 | $op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr |
383 | $op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr |
377 | $op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr |
384 | |
378 | |
385 | $op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc |
379 | $op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc |
386 | $op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral |
380 | $op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral |
387 | |
|
|
388 | $op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc |
381 | $op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc |
389 | $op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar |
382 | $op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar |
390 | |
|
|
391 | $op[0x2f] = '$A ^= 0xff'; # cma |
|
|
392 | |
383 | |
393 | # getting this insn wrong (its the only 16 bit insn to modify flags) |
384 | # getting this insn wrong (its the only 16 bit insn to modify flags) |
394 | # wasted three of my best days with mindless vt102 rom reverse engineering |
385 | # wasted three of my best days with mindless vt102 rom reverse engineering |
395 | sub dad { |
386 | sub dad { |
396 | $x = $H * 256 + $L + $_[0]; |
387 | $x = $H * 256 + $L + $_[0]; |
… | |
… | |
401 | |
392 | |
402 | $op[0x09] = 'dad $B * 256 + $C'; # dad |
393 | $op[0x09] = 'dad $B * 256 + $C'; # dad |
403 | $op[0x19] = 'dad $D * 256 + $E'; # dad |
394 | $op[0x19] = 'dad $D * 256 + $E'; # dad |
404 | $op[0x29] = 'dad $H * 256 + $L'; # dad |
395 | $op[0x29] = 'dad $H * 256 + $L'; # dad |
405 | $op[0x39] = 'dad $SP '; # dad |
396 | $op[0x39] = 'dad $SP '; # dad |
|
|
397 | |
|
|
398 | $op[0x2f] = '$A ^= 0xff'; # cma |
406 | |
399 | |
407 | $op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add |
400 | $op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add |
408 | $op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc |
401 | $op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc |
409 | $op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub |
402 | $op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub |
410 | $op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb |
403 | $op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb |
411 | $op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana |
404 | $op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana |
412 | $op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra |
405 | $op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra |
413 | $op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora |
406 | $op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora |
414 | $op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp |
407 | $op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp |
415 | # possible todo: optimize ora a, maybe xra a |
408 | # possible todo: optimize ora a, maybe xra a, possibly ana |
416 | |
409 | |
417 | $op[0xc6] = 'sf $A += IMM8'; # adi |
410 | $op[0xc6] = 'sf $A += IMM8'; # adi |
418 | # ce ADI NYI |
|
|
419 | $op[0xd6] = 'sf $A -= IMM8'; # sui |
411 | $op[0xd6] = 'sf $A -= IMM8'; # sui |
420 | # de SBI NYI |
|
|
421 | $op[0xe6] = 'sf8 $A &= IMM8'; # ani |
412 | $op[0xe6] = 'sf8 $A &= IMM8'; # ani |
422 | $op[0xee] = 'sf8 $A ^= IMM8'; # xri |
413 | $op[0xee] = 'sf8 $A ^= IMM8'; # xri |
423 | $op[0xf6] = 'sf8 $A |= IMM8'; # ori |
414 | $op[0xf6] = 'sf8 $A |= IMM8'; # ori |
424 | $op[0xfe] = 'sf $A - IMM8'; # cpi |
415 | $op[0xfe] = 'sf $A - IMM8'; # cpi |
|
|
416 | # ce ACI NYI, apparently unused |
|
|
417 | # de SBI NYI, apparently unused |
425 | |
418 | |
426 | $op[0xc5] = 'PUSH $B; PUSH $C'; |
419 | $op[0xc5] = 'PUSH $B; PUSH $C'; |
427 | $op[0xd5] = 'PUSH $D; PUSH $E'; |
420 | $op[0xd5] = 'PUSH $D; PUSH $E'; |
428 | $op[0xe5] = 'PUSH $H; PUSH $L'; |
421 | $op[0xe5] = 'PUSH $H; PUSH $L'; |
429 | $op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw |
422 | $op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw |
… | |
… | |
431 | $op[0xc1] = '($C, $B) = (POP, POP)'; # pop |
424 | $op[0xc1] = '($C, $B) = (POP, POP)'; # pop |
432 | $op[0xd1] = '($E, $D) = (POP, POP)'; # pop |
425 | $op[0xd1] = '($E, $D) = (POP, POP)'; # pop |
433 | $op[0xe1] = '($L, $H) = (POP, POP)'; # pop |
426 | $op[0xe1] = '($L, $H) = (POP, POP)'; # pop |
434 | $op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw |
427 | $op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw |
435 | |
428 | |
436 | $op[0xc2 + $_ * 8] = 'BRA IMM16 ' . $cc[$_] for 0..7; # jcc |
429 | $op[0xc2 + $_ * 8] = 'BRA IMM16 if ' . $cc[$_] for 0..7; # jcc |
437 | $op[0xc3] = 'JMP IMM16'; # jmp |
430 | $op[0xc3] = 'JMP IMM16'; # jmp |
438 | |
431 | |
439 | $op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) ' . $cc[$_] for 0..7; # ccc |
432 | $op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) if ' . $cc[$_] for 0..7; # ccc |
440 | $op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call |
433 | $op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call |
441 | |
434 | |
442 | $op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 ' . $cc[$_] for 0..7; # rcc |
435 | $op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 if ' . $cc[$_] for 0..7; # rcc |
443 | $op[0xc9] = 'JMP POP + POP * 256'; # ret |
436 | $op[0xc9] = 'JMP POP + POP * 256'; # ret |
444 | |
437 | |
445 | $op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst |
438 | $op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst |
446 | |
439 | |
447 | $op[0xe9] = 'JMP $H * 256 + $L'; # pchl |
440 | $op[0xe9] = 'JMP $H * 256 + $L'; # pchl |
448 | # f9 SPHL NYI |
441 | # f9 SPHL NYI, apparently unused |
449 | |
442 | |
450 | $op[0x37] = '$FC = 1 '; # stc |
443 | $op[0x37] = '$FC = 1 '; # stc |
451 | $op[0x3f] = '$FC = !$FC'; # cmc |
444 | $op[0x3f] = '$FC = !$FC'; # cmc |
452 | |
445 | |
453 | $op[0xd3] = 'OUT'; # out |
446 | $op[0xd3] = 'OUT'; # out |
454 | $op[0xdb] = 'IN'; # in |
447 | $op[0xdb] = 'IN'; # in |
455 | |
448 | |
456 | $op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg |
449 | $op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg |
457 | |
450 | |
458 | # e3 xthl NYI # @ 917b, hl <-> (sp) |
451 | # e3 xthl NYI # @ 917b in e69, hl <-> (sp) |
459 | |
452 | |
460 | $op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (incomplete) |
453 | $op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (8085, incomplete) |
461 | $op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (incomplete) |
454 | $op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (8085, incomplete) |
462 | |
455 | |
463 | $op[0xf3] = '$IFF = 0'; # DI |
456 | $op[0xf3] = '$IFF = 0'; # di |
464 | $op[0xfb] = '$IFF = 1'; # EI |
457 | $op[0xfb] = '$IFF = 1'; # ei |
465 | |
458 | |
466 | # yeah, the fucking setup screens actually use daa... |
459 | # yeah, the fucking setup screen actually uses daa... |
467 | $op[0x27] = ' |
460 | $op[0x27] = ' |
468 | my ($h, $l); |
461 | my ($h, $l); |
469 | |
462 | |
470 | ($h, $l) = ($A >> 4, $A & 15); |
463 | ($h, $l) = ($A >> 4, $A & 15); |
471 | |
464 | |
… | |
… | |
479 | $A = ($h * 16 + $l) & 0xff; |
472 | $A = ($h * 16 + $l) & 0xff; |
480 | } |
473 | } |
481 | '; # daa, almost certainly borked, also, acarry not set by sf |
474 | '; # daa, almost certainly borked, also, acarry not set by sf |
482 | |
475 | |
483 | ############################################################################# |
476 | ############################################################################# |
484 | # print cpu status for debugging purposes |
477 | # debug |
485 | |
478 | |
486 | # print cpu status, for debugging |
479 | # print cpu status, for debugging |
487 | sub status { |
480 | sub status { |
488 | my $PC = shift || $PC; |
481 | my $PC = shift || $PC; |
489 | |
482 | |
… | |
… | |
499 | } |
492 | } |
500 | |
493 | |
501 | ############################################################################# |
494 | ############################################################################# |
502 | # video emulation |
495 | # video emulation |
503 | |
496 | |
504 | my @CHARMAP = ( |
497 | binmode STDOUT; |
|
|
498 | |
|
|
499 | my @CHARMAP = ( # acschars / chars 0..31 |
505 | " " , "\x{29eb}", "\x{2592}", "\x{2409}", |
500 | " " , "\x{29eb}", "\x{2592}", "\x{2409}", |
506 | "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", |
501 | "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", |
507 | "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", |
502 | "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", |
508 | "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", |
503 | "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", |
509 | "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", |
504 | "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", |
… | |
… | |
535 | |
530 | |
536 | my @LED = $VT102 |
531 | my @LED = $VT102 |
537 | ? qw(L1 INSERT DSR CTS LOCKED LOCAL SCAN BEEP) |
532 | ? qw(L1 INSERT DSR CTS LOCKED LOCAL SCAN BEEP) |
538 | : qw(L4 L3 L2 L1 LOCKED LOCAL SCAN BEEP); |
533 | : qw(L4 L3 L2 L1 LOCKED LOCAL SCAN BEEP); |
539 | |
534 | |
|
|
535 | my $CURSOR_IS_ON; |
|
|
536 | |
540 | # display screen |
537 | # display screen |
541 | sub display { |
538 | sub display { |
|
|
539 | # this is for the powersave mode - check whether the cursor is on here, |
|
|
540 | # and only allow powersave later when it was on the last display time |
|
|
541 | $CURSOR_IS_ON = $M[$VT102 ? 0x207b : 0x21ba]; |
|
|
542 | |
|
|
543 | my $leds = join " ", map $KSTATUS & 2**$_ ? "\e[7m$LED[$_]\e[m" : "$LED[$_]", reverse 0 .. $#LED; |
|
|
544 | |
|
|
545 | my $scr = sprintf "\e[H--- LED [ %s ] CLK %d\e[K\n", $leds, $CLK; |
|
|
546 | |
|
|
547 | $scr .= "\e[?5" . ($DC11_REVERSE ? "h" : "l"); |
|
|
548 | |
542 | my $i = 0x2000; |
549 | my $i = 0x2000; |
543 | |
|
|
544 | my $leds = join " ", map $KSTATUS & 2**$_ ? "\e[7m$LED[$_]\e[m" : "$LED[$_]", reverse 0 .. $#LED; |
|
|
545 | |
|
|
546 | my $scr = sprintf "\e[H--- LED [ %s ] CLK %d\e[K\n", $leds, $CLK; |
|
|
547 | |
|
|
548 | $scr .= "\e[?5" . ($DC11_REVERSE ? "h" : "l"); |
|
|
549 | |
550 | |
550 | line: |
551 | line: |
551 | for my $y (0 .. 25) { # ntsc, two vblank delay lines, up to 24 text lines |
552 | for my $y (0 .. 25) { # ntsc, two vblank delay lines, up to 24 text lines |
552 | my $prev_sgr; |
553 | my $prev_attr; |
|
|
554 | my ($c, $attr); # declare here for speedup |
553 | |
555 | |
554 | $scr .= sprintf "%2d \xe2\x94\x82", $y; |
556 | $scr .= sprintf "%2d \xe2\x94\x82", $y; |
555 | |
557 | |
556 | for (0..139) { |
558 | for (0..139) { |
557 | my $c = $M[$i]; |
559 | $c = $M[$i]; |
558 | |
560 | |
559 | if ($c == 0x7f) { # also 0xff, but the firmware avoids that |
561 | if ($c == 0x7f) { # also 0xff, but the firmware avoids that |
560 | $scr .= "\e[m\xe2\x94\x82\e[K\n"; |
562 | $scr .= "\e[m\xe2\x94\x82\e[K\n"; |
561 | |
563 | |
562 | my $a1 = $M[$i + 1]; |
564 | my $a1 = $M[$i + 1]; |
… | |
… | |
565 | $i = 0x2000 + (($a1 * 256 + $a0) & 0xfff); |
567 | $i = 0x2000 + (($a1 * 256 + $a0) & 0xfff); |
566 | |
568 | |
567 | next line; |
569 | next line; |
568 | } |
570 | } |
569 | |
571 | |
|
|
572 | $scr .= $SGR[$prev_attr = $attr] |
570 | my $sgr = $SGR[ ($M[$i++ + 0x1000] & 15) | ($c & 0x80)]; |
573 | if $prev_attr != ($attr = ($M[$i++ + 0x1000] & 15) | ($c & 0x80)); |
571 | |
|
|
572 | $scr .= $prev_sgr = $sgr if $sgr ne $prev_sgr; |
|
|
573 | |
574 | |
574 | $scr .= $CHARMAP[$c & 0x7f]; |
575 | $scr .= $CHARMAP[$c & 0x7f]; |
575 | } |
576 | } |
576 | |
577 | |
577 | $scr .= "\e[K\nvideo overflow\e[K\n"; |
578 | $scr .= "\e[K\nvideo overflow\e[K\n"; |
… | |
… | |
598 | "\x1e" => 0x24 | 0x100, # CTRL-~ |
599 | "\x1e" => 0x24 | 0x100, # CTRL-~ |
599 | "\x1f" => 0x75 | 0x100, # CTRL-? |
600 | "\x1f" => 0x75 | 0x100, # CTRL-? |
600 | |
601 | |
601 | # hardcoded rxvt keys |
602 | # hardcoded rxvt keys |
602 | "\e" => 0x2a, # ESC |
603 | "\e" => 0x2a, # ESC |
|
|
604 | "\e[2~" => 0x79 | 0x100, # CTRL-C (insert) |
603 | "\e[3~" => 0x03, # DC |
605 | "\e[3~" => 0x03, # DC |
604 | "\e[5~" => 0x7e, # CAPS LOCK (prior) |
606 | "\e[5~" => 0x7e, # CAPS LOCK (prior) |
605 | "\e[6~" => 0x6a, # NO SCROLL (next) |
607 | "\e[6~" => 0x6a, # NO SCROLL (next) |
606 | "\e[A" => 0x30, # UP |
608 | "\e[A" => 0x30, # UP |
607 | "\e[B" => 0x22, # DOWN |
609 | "\e[B" => 0x22, # DOWN |
… | |
… | |
614 | "\e[7~" => 0x7b, # SETUP (home) |
616 | "\e[7~" => 0x7b, # SETUP (home) |
615 | "\e[8~" => 0x23, # BREAK (end) |
617 | "\e[8~" => 0x23, # BREAK (end) |
616 | "\e[8\$" => 0x23 | 0x080, # SHIFT BREAK / DISCONNECT (shift-end) |
618 | "\e[8\$" => 0x23 | 0x080, # SHIFT BREAK / DISCONNECT (shift-end) |
617 | "\x7f" => 0x33, # BACKSPACE |
619 | "\x7f" => 0x33, # BACKSPACE |
618 | |
620 | |
619 | "\e[11~" => 0x32, # F1 |
621 | "\e[11~" => 0x32, # PF1 |
620 | "\e[11~" => 0x42, # F2 |
622 | "\e[12~" => 0x42, # PF2 |
621 | "\e[11~" => 0x31, # F3 |
623 | "\e[13~" => 0x31, # PF3 |
622 | "\e[11~" => 0x41, # F4 |
624 | "\e[14~" => 0x41, # PF4 |
623 | ); |
625 | ); |
624 | |
626 | |
625 | @KEYMAP{map chr, 0x20 .. 0x40, 0x5b .. 0x7e} = unpack "C*", pack "H*", |
627 | @KEYMAP{map chr, 0x20 .. 0x40, 0x5b .. 0x7e} = unpack "C*", pack "H*", |
626 | "779ad5a9a8b8a755a6b5b6b466256575" . "351a3929283837273626d656e634e5f5" . "b9" # 20..40 |
628 | "779ad5a9a8b8a755a6b5b6b466256575" . "351a3929283837273626d656e634e5f5" . "b9" # 20..40 |
627 | . "154514b7a5" . "244a6879591949485816574746766706" . "050a185a0817780969077a95c594a4"; # 5b..7e |
629 | . "154514b7a5" . "244a6879591949485816574746766706" . "050a185a0817780969077a95c594a4"; # 5b..7e |
… | |
… | |
630 | $KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift |
632 | $KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift |
631 | |
633 | |
632 | my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; |
634 | my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; |
633 | $KEYMATCH = qr{^($KEYMATCH)}s; |
635 | $KEYMATCH = qr{^($KEYMATCH)}s; |
634 | |
636 | |
635 | my %KMOD; |
637 | my %KMOD; # currently pressed modifier keys |
636 | |
638 | |
637 | sub key { |
639 | sub key { |
638 | my ($key) = @_; |
640 | my ($key) = @_; |
639 | |
641 | |
640 | push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up |
642 | push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up |
… | |
… | |
656 | # skip input we can't decipher |
658 | # skip input we can't decipher |
657 | substr $STDIN_BUF, 0, 1, ""; |
659 | substr $STDIN_BUF, 0, 1, ""; |
658 | } |
660 | } |
659 | |
661 | |
660 | if ($KBD) { |
662 | if ($KBD) { |
661 | system "stty -icanon -icrnl -inlcr -echo min 1 time 0"; |
663 | system "stty -icanon -icrnl -inlcr -echo min 1 time 0"; # -isig |
662 | eval q{ sub END { system "stty sane" } }; |
664 | eval q{ sub END { system "stty sane" } }; |
663 | $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { exit 1 }; |
665 | $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { exit 1 }; |
664 | } |
666 | } |
665 | |
667 | |
666 | ############################################################################# |
668 | ############################################################################# |
667 | # initial key input, to set up online mode etc. |
669 | # initial key input, to set up online mode etc. |
668 | # could be done via nvram defaults |
670 | # could be done via nvram defaults |
669 | |
671 | |
670 | @KQUEUE = ( |
672 | @KQUEUE = ( |
671 | 0x7b, -0x7b, # setup |
673 | 0x7b, -0x7b, # setup |
672 | 0, # delay |
674 | 0, # delay |
673 | 0x28, -0x28, # 4, toggle local/online |
675 | 0x28, -0x28, # 4, toggle local/online |
674 | 0x38, -0x38, # 5, setup b |
676 | 0x38, -0x38, # 5, setup b |
675 | 0, # delay |
677 | 0, # delay |
676 | (0x10, -0x10) x 2, # cursor right |
678 | (0x10, -0x10) x 2, # cursor right |
677 | 0x37, -0x37, # 6 toggle soft scroll |
679 | 0x37, -0x37, # 6 toggle soft scroll |
678 | (0x10, -0x10) x 1, # cursor right |
680 | (0x10, -0x10) x 1, # cursor right |
679 | 0x37, -0x37, # 6 toggle autorepeat off |
681 | 0x37, -0x37, # 6 toggle autorepeat off |
680 | (0x10, -0x10) x 8, # cursor right |
682 | (0x10, -0x10) x 8, # cursor right |
681 | 0x37, -0x37, # 6 toggle keyclick |
683 | 0x37, -0x37, # 6 toggle keyclick |
682 | (0x10, -0x10) x 1, # cursor right |
684 | (0x10, -0x10) x 1, # cursor right |
683 | $VT102 ? () : (0x37, -0x37), # 6 toggle ansi/vt52 |
685 | $VT102 ? () : (0x37, -0x37), # 6 toggle ansi/vt52 |
684 | (0x10, -0x10) x 7, # cursor right |
686 | (0x10, -0x10) x 7, # cursor right |
685 | 0x37, -0x37, # 6 toggle wrap around |
687 | 0x37, -0x37, # 6 toggle wrap around |
686 | 0x7b, -0x7b, # leave setup |
688 | 0x7b, -0x7b, # leave setup |
687 | ); |
689 | ); |
688 | |
690 | |
689 | ############################################################################# |
691 | ############################################################################# |
690 | # process/pty management |
692 | # process/pty management |
691 | |
693 | |
|
|
694 | if (1) { |
692 | require IO::Pty; |
695 | require IO::Pty; |
693 | $PTY = IO::Pty->new; |
696 | $PTY = IO::Pty->new; |
694 | |
697 | |
695 | my $slave = $PTY->slave; |
698 | my $slave = $PTY->slave; |
696 | |
699 | |
697 | $PTY->set_winsize (24, 80); |
700 | $PTY->set_winsize (24, 80); |
698 | |
701 | |
699 | unless (fork) { |
702 | unless (fork) { |
|
|
703 | $ENV{LC_ALL} = "C"; |
700 | $ENV{TERM} = $VT102 ? "vt102" : "vt100"; |
704 | $ENV{TERM} = $VT102 ? "vt102" : "vt100"; |
701 | |
705 | |
702 | close $PTY; |
706 | close $PTY; |
703 | |
707 | |
704 | open STDIN , "<&", $slave; |
708 | open STDIN , "<&", $slave; |
705 | open STDOUT, ">&", $slave; |
709 | open STDOUT, ">&", $slave; |
706 | open STDERR, ">&", $slave; |
710 | open STDERR, ">&", $slave; |
707 | |
711 | |
708 | system "stty ixoff erase ^H"; |
712 | system "stty ixoff erase ^H"; |
709 | |
713 | |
710 | $PTY->make_slave_controlling_terminal; |
714 | $PTY->make_slave_controlling_terminal; |
|
|
715 | $PTY->close_slave; |
|
|
716 | |
|
|
717 | @ARGV = "sh" unless @ARGV; |
|
|
718 | exec @ARGV; |
|
|
719 | } |
|
|
720 | |
711 | $PTY->close_slave; |
721 | $PTY->close_slave; |
712 | |
722 | } else { |
713 | @ARGV = "sh" unless @ARGV; |
723 | open $PTY, "+</dev/null" |
714 | exec @ARGV; |
724 | or die "/dev/null: $!"; |
715 | } |
725 | } |
716 | |
|
|
717 | $PTY->close_slave; |
|
|
718 | |
726 | |
719 | ############################################################################# |
727 | ############################################################################# |
720 | # the actual hardware simulator |
728 | # the actual hardware simulator |
721 | |
729 | |
722 | my @ICACHE; # compiled instruction cache |
730 | my @ICACHE; # compiled instruction/basic block cache |
723 | |
731 | |
|
|
732 | my $POWERSAVE; # powersave counter |
|
|
733 | |
|
|
734 | my $RIN; # libev for the less well-off |
|
|
735 | |
|
|
736 | (vec $RIN, 0, 1) = 1 if $KBD; |
|
|
737 | (vec $RIN, fileno $PTY, 1) = 1 if $PTY; |
|
|
738 | |
|
|
739 | # the cpu. |
724 | while () { |
740 | while () { |
725 | # execute extended basic blocks |
741 | # execute an extended basic block |
726 | $PC = ($ICACHE[$PC] ||= do { |
742 | $PC = ($ICACHE[$PC] ||= do { |
727 | my $pc = $PC; |
743 | my $pc = $PC; |
728 | |
744 | |
729 | my $insn = ""; |
745 | my $insn = ""; |
730 | |
746 | |
… | |
… | |
742 | |
758 | |
743 | s/\bPC\b/$pc/ge; # PC at end of insn |
759 | s/\bPC\b/$pc/ge; # PC at end of insn |
744 | s/\bBRA\b/return/g; # conditional jump |
760 | s/\bBRA\b/return/g; # conditional jump |
745 | s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump |
761 | s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump |
746 | |
762 | |
747 | s/\bIN\b/ sprintf "\$A = in_%02x", $M[$pc++]/xge; |
763 | s/\bIN\b/ sprintf "\$A = in_%02x", $M[$pc++]/xge; # in insns call in_HEX |
748 | s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; |
764 | s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; # out likewise |
749 | } |
765 | } |
750 | |
766 | |
751 | $insn .= "$op;\n"; |
767 | $insn .= "$op;\n"; |
752 | } |
768 | } |
753 | |
769 | |
754 | |
|
|
755 | $insn .= $pc; |
770 | $insn .= $pc; |
756 | $insn =~ s/\x00.*$//s; |
771 | $insn =~ s/\x00.*$//s; |
757 | |
772 | |
758 | eval "use integer; sub { $insn }" or die "$insn: $@" |
773 | eval "sub { $insn }" or die "$insn: $@" |
759 | })->(); |
774 | })->(); |
760 | |
775 | |
761 | ++$CLK; |
776 | ++$CLK; |
762 | |
777 | |
763 | # things we do from time too time only |
778 | # things we do from time to time only |
764 | unless ($CLK & 0xf) { |
779 | unless ($CLK & 0xf) { |
765 | # do I/O |
780 | # do I/O |
766 | |
781 | |
767 | unless ($CLK & 0xfff) { |
782 | unless ($CLK & 0xfff) { |
|
|
783 | if (select $x = $RIN, undef, undef, $POWERSAVE < 10 ? 0 : $CURSOR_IS_ON && 3600) { |
768 | |
784 | |
769 | # pty/serial I/O |
785 | # pty/serial I/O |
770 | unless ((@PUSARTRECV >= 128) || @KQUEUE || !$PTY) { |
786 | if ($PTY && (vec $x, fileno $PTY, 1) && (@PUSARTRECV < 128) && !@KQUEUE) { |
771 | my $rin = ""; (vec $rin, fileno $PTY, 1) = 1; |
|
|
772 | |
|
|
773 | if (select $rin, undef, undef, 0) { |
|
|
774 | sysread $PTY, my $buf, 256; |
787 | sysread $PTY, my $buf, 256; |
|
|
788 | |
|
|
789 | # linux don't do cs7 and/or parity anymore, so we need to filter |
|
|
790 | # out xoff characters to avoid freezes. |
775 | push @PUSARTRECV, unpack "C*", $buf; |
791 | push @PUSARTRECV, grep { ($_ & 0x7f) != 0x13 } unpack "C*", $buf; |
776 | } |
792 | } |
777 | } |
|
|
778 | |
793 | |
779 | # keyboard input |
794 | # keyboard input |
780 | if ($KBD) { |
795 | if ($KBD && (vec $x, 0, 1)) { |
|
|
796 | # to avoid non-blocking mode on stdin (and stty min 0), we |
|
|
797 | # just read byte-by-byte after a select says there is data. |
781 | while (select my $rin = "\x01", undef, undef, 0) { |
798 | while (select my $rin = "\x01", undef, undef, 0) { |
782 | sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF |
799 | sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF |
783 | or last; |
800 | or last; |
|
|
801 | } |
|
|
802 | |
|
|
803 | stdin_parse if length $STDIN_BUF; |
784 | } |
804 | } |
785 | |
805 | |
786 | stdin_parse if length $STDIN_BUF; |
806 | $POWERSAVE = 0; # activity |
|
|
807 | } elsif (@PUSARTRECV || @KQUEUE) { |
|
|
808 | $POWERSAVE = 0; |
|
|
809 | } else { |
|
|
810 | ++$POWERSAVE; |
787 | } |
811 | } |
788 | } |
812 | } |
789 | |
813 | |
790 | # kick off various interrupts |
814 | # kick off serial input interrupt quite often |
791 | |
|
|
792 | $RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though) |
815 | $RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though) |
793 | #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy |
816 | #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy |
794 | |
817 | |
795 | # kick off vertical retrace form time to time |
818 | # kick off vertical retrace interrupt from time to time |
796 | unless ($CLK & 0x1ff) { |
819 | unless ($CLK & 0x1ff) { |
797 | $RST |= 4; # vertical retrace |
820 | $RST |= 4; # vertical retrace |
798 | } |
821 | } |
799 | |
822 | |
800 | # handle video hardware |
823 | # handle video hardware |
801 | unless ($CLK & 0x3fff) { |
824 | unless ($CLK & 0x3fff) { |
802 | display; |
825 | display; |
803 | } |
826 | } |
804 | } |
827 | } |
805 | |
828 | |
806 | # the interrupt logic |
829 | # the interrupt logic - we only interrupt after basic blocks |
|
|
830 | # which, as a side effect, ensures that we don't interrupt |
|
|
831 | # "ei; ret" sequences and thus reduce the risk of stack overflows. |
807 | if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) { |
832 | if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) { |
808 | # rst 1 kbd data available |
833 | # rst 1 kbd data available |
809 | # rst 2 pusart xmit+recv flag |
834 | # rst 2 pusart xmit+recv flag |
810 | # rst 4 vertical retrace |
835 | # rst 4 vertical retrace |
811 | # 5.5 vt125 mb7 trans ready (serial send?) |
836 | # 5.5 vt125 mb7 trans ready (serial send?) |
812 | # 6.5 vt125 mb7 read ready (something modem?) |
837 | # 6.5 vt125 mb7 read ready (something modem?) |
813 | # 7.5 vt125 mb7 vblank h(?) |
838 | # 7.5 vt125 mb7 vblank h(?) |
814 | # trap vt125 mbi init h(?) |
839 | # trap vt125 mbi init h(?) |
815 | my $vec; |
840 | my $vec; |
816 | |
841 | |
817 | $x = $INTPEND & ~$INTMASK; |
842 | my $pend = $INTPEND & ~$INTMASK; |
818 | |
843 | |
819 | if ($x & 1) { $vec = 0x2c; $INTPEND &= ~1; |
844 | if ($pend & 1) { $vec = 0x2c; $INTPEND &= ~1; |
820 | } elsif ($x & 2) { $vec = 0x34; $INTPEND &= ~2; |
845 | } elsif ($pend & 2) { $vec = 0x34; $INTPEND &= ~2; |
821 | } elsif ($x & 4) { $vec = 0x3c; $INTPEND &= ~4; |
846 | } elsif ($pend & 4) { $vec = 0x3c; $INTPEND &= ~4; |
822 | # } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # the vt102 firmware doesn't like combined interrupts |
847 | # } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # the vt102 firmware doesn't like combined interrupts |
823 | } elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102 |
848 | } elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102 |
824 | } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; |
849 | } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; |
825 | } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; |
850 | } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; |
826 | } else { |
851 | } else { |
827 | die; |
852 | die; |
828 | } |
853 | } |
829 | |
854 | |
|
|
855 | # jump to the interrupt vector |
830 | $M[--$SP] = $PC >> 8; |
856 | $M[--$SP] = $PC >> 8; |
831 | $M[--$SP] = $PC & 0xff; |
857 | $M[--$SP] = $PC & 0xff; |
832 | $PC = $vec; |
858 | $PC = $vec; |
833 | |
859 | |
834 | $IFF = 0; |
860 | $IFF = 0; |