… | |
… | |
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 | # this hack is not considered release ready in and way, shape, or form |
20 | use strict; |
21 | # ./vt102 bash |
|
|
22 | # ./vt102 telnet towel.blinkenlights.nl |
|
|
23 | # ./vt102 curl http://artscene.textfiles.com/vt100/trekvid.vt |
|
|
24 | # ./vt102 curl http://artscene.textfiles.com/vt100/surf.vt # in 3d! |
|
|
25 | |
|
|
26 | # TODO: ctrl key map |
|
|
27 | |
|
|
28 | use common::sense; |
21 | #use common::sense; |
29 | |
|
|
30 | $| = 1; |
|
|
31 | |
22 | |
32 | my $VT102 = 1; |
23 | my $VT102 = 1; |
33 | my $AVO = $VT102 || 1; |
24 | my $VT131 = 0; |
|
|
25 | my $AVO = 1; |
|
|
26 | |
|
|
27 | shift, ($VT102 = 0), ($AVO = 0) if $ARGV[0] =~ /^-?-vt100$/; |
|
|
28 | shift, ($VT102 = 0) if $ARGV[0] =~ /^-?-vt100\+avo$/; |
|
|
29 | shift if $ARGV[0] =~ /^-?-vt102$/; |
|
|
30 | shift, ($VT131 = 1) if $ARGV[0] =~ /^-?-vt131$/; |
|
|
31 | |
|
|
32 | # vt100 wps = word processing roms |
|
|
33 | # vt101 = vt102 - avo, but custom rom? really? |
|
|
34 | # vt103 = vt100 + tu58 tape drive |
|
|
35 | # vt125 = vt100 + gpo graphics processor |
|
|
36 | # vt132 = vt100 + avo, stp |
|
|
37 | # vt180 = vt100 + z80 cp/m |
|
|
38 | |
|
|
39 | if ($ARGV[0] =~ /^-/) { |
|
|
40 | die <<EOF; |
|
|
41 | |
|
|
42 | VT102, A VT100/102/131 SIMULATOR |
|
|
43 | |
|
|
44 | Usage: |
|
|
45 | |
|
|
46 | $0 [option] [program [args]] |
|
|
47 | |
|
|
48 | Examples: |
|
|
49 | |
|
|
50 | $0 bash |
|
|
51 | $0 telnet towel.blinkenlights.nl |
|
|
52 | $0 curl http://artscene.textfiles.com/vt100/trekvid.vt |
|
|
53 | $0 curl http://artscene.textfiles.com/vt100/surf.vt # in 3d! |
|
|
54 | |
|
|
55 | Option can be one of: |
|
|
56 | |
|
|
57 | --vt100 |
|
|
58 | --vt100+avo |
|
|
59 | --vt102 |
|
|
60 | --vt131 |
|
|
61 | |
|
|
62 | Non-obvious special keys are: |
|
|
63 | |
|
|
64 | SET UP Home |
|
|
65 | BACKSPACE Rubout |
|
|
66 | CAPS LOCK Prior/PgUp |
|
|
67 | NO SCROLL Next/PgDown |
|
|
68 | BREAK End |
|
|
69 | |
|
|
70 | Set-Up Guide: |
|
|
71 | |
|
|
72 | http://vt100.net/docs/vt102-ug/chapter3.html#S3.6 |
|
|
73 | |
|
|
74 | Author: |
|
|
75 | |
|
|
76 | Marc Lehmann <vt102\@schmorp.de> |
|
|
77 | |
|
|
78 | EOF |
|
|
79 | } |
|
|
80 | |
|
|
81 | ############################################################################# |
|
|
82 | # ROM/hardware init |
|
|
83 | |
|
|
84 | my $PTY; # the pty we allocated, if any |
34 | my $KBD = 1; |
85 | my $KBD = 1; |
35 | |
86 | |
36 | ############################################################################# |
|
|
37 | # rom initialising |
|
|
38 | |
|
|
39 | my $ROM = do { |
87 | my $ROMS = do { |
40 | binmode DATA; |
88 | binmode DATA; |
41 | local $/; |
89 | local $/; |
42 | <DATA> |
90 | <DATA> |
43 | }; |
91 | }; |
44 | |
92 | |
45 | 0x6001 == length $ROM or die "corrupted rom image"; |
93 | 0x6801 == length $ROMS or die "corrupted rom image"; |
46 | |
|
|
47 | binmode STDOUT; |
|
|
48 | |
94 | |
49 | my @M = (0xff) x 65536; # main memory, = (0xff) x 65536; |
95 | my @M = (0xff) x 65536; # main memory, = (0xff) x 65536; |
50 | |
96 | |
51 | # populate mem with rom contents |
97 | # populate mem with rom contents |
52 | if ($VT102) { |
98 | if ($VT102) { |
53 | @M[0x0000 .. 0x1fff] = unpack "C*", substr $ROM, 0x2000, 0x2000; |
99 | @M[0x0000 .. 0x1fff] = unpack "C*", substr $ROMS, 0x2000, 0x2000; |
54 | @M[0x8000 .. 0x9fff] = unpack "C*", substr $ROM, 0x4000, 0x2000; |
100 | @M[0x8000 .. 0x9fff] = unpack "C*", substr $ROMS, 0x4000, 0x2000; |
|
|
101 | @M[0xa000 .. 0xa7ff] = unpack "C*", substr $ROMS, 0x6000, 0x0800 if $VT131; |
55 | } else { |
102 | } else { |
56 | @M[0x0000 .. 0x1fff] = unpack "C*", substr $ROM, 0x0000, 0x2000; |
103 | @M[0x0000 .. 0x1fff] = unpack "C*", substr $ROMS, 0x0000, 0x2000; |
57 | } |
104 | } |
58 | |
105 | |
59 | ############################################################################# |
106 | ############################################################################# |
60 | # cpu registers and I/O support |
107 | # 8085 CPU registers and I/O support |
61 | |
|
|
62 | my $PTY; # the pty we allocated, if any |
|
|
63 | my $PRSTATUS = 0; |
|
|
64 | |
108 | |
65 | # 8080/8085 registers |
109 | # 8080/8085 registers |
66 | # b, c, d, e, h, l, a |
|
|
67 | my ($A, $B, $C, $D, $E, $H, $L, $A); |
110 | my ($A, $B, $C, $D, $E, $H, $L); |
68 | my ($PC, $SP, $IFF, $FA, $FZ, $FS, $FP, $FC); |
111 | my ($PC, $SP, $IFF, $FA, $FZ, $FS, $FP, $FC); |
69 | |
112 | |
70 | my $RST = 0; # 8080 pending interrupts |
113 | my $RST = 0; # 8080 pending interrupts |
71 | my $INTMASK = 7; # 8085 half interrupts |
114 | my $INTMASK = 7; # 8085 half interrupts |
72 | my $INTPEND = 0; # 8085 half interrupts |
115 | my $INTPEND = 0; # 8085 half interrupts |
73 | |
116 | |
74 | my $x; # dummy temp for instructions |
|
|
75 | |
|
|
76 | my $CLK; # rather inexact clock |
117 | my $CLK; # rather inexact clock |
77 | |
118 | |
78 | ############################################################################# |
119 | ############################################################################# |
79 | # the dreaded nvr1400 chip. not needed to get it going, but provided for reference |
120 | # the dreaded NVR1400 chip. not needed to get it going, but provided anyway |
80 | |
121 | |
81 | # nvram |
122 | # nvram |
82 | my @NVR = (0x3fff) x 100; # vt102: 214e accum, 214f only lower 8 bit used, first 44 bytes |
123 | my @NVR = (0x3fff) x 100; # vt102: 214e accum, 214f only lower 8 bit used, first 44 bytes |
83 | my $NVRADDR; |
124 | my $NVRADDR; |
84 | my $NVRDATA; |
125 | my $NVRDATA; |
… | |
… | |
93 | sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase |
134 | sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase |
94 | sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read |
135 | sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read |
95 | sub { }, # 7 standby |
136 | sub { }, # 7 standby |
96 | ); |
137 | ); |
97 | |
138 | |
98 | my @bitidx; |
139 | my @NVR_BITIDX; |
99 | $bitidx[1 << $_] = 9 - $_ for 0..9; |
140 | $NVR_BITIDX[1 << $_] = 9 - $_ for 0..9; |
100 | |
141 | |
101 | # the nvr1400 state machine. what a monster |
142 | # the nvr1400 state machine. what a monster |
102 | sub nvr() { |
143 | sub nvr() { |
103 | my $a1 = $bitidx[(~$NVRADDR ) & 0x3ff]; |
144 | my $a1 = $NVR_BITIDX[(~$NVRADDR ) & 0x3ff]; |
104 | my $a0 = $bitidx[(~$NVRADDR >> 10) & 0x3ff]; |
145 | my $a0 = $NVR_BITIDX[(~$NVRADDR >> 10) & 0x3ff]; |
105 | |
146 | |
106 | # printf "NVR %02x A %020b %d %d D %02x\n", $NVRLATCH, $NVRADDR & 0xfffff, $a1, $a0, $NVRDATA; |
147 | # printf "NVR %02x A %020b %d %d D %02x\n", $NVRLATCH, $NVRADDR & 0xfffff, $a1, $a0, $NVRDATA; |
107 | |
148 | |
108 | $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) |
149 | $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) |
109 | } |
150 | } |
110 | |
151 | |
111 | ############################################################################# |
152 | ############################################################################# |
|
|
153 | # I/O ports - output |
112 | |
154 | |
113 | my $DC11_REVERSE = 0; |
155 | my $DC11_REVERSE = 0; |
114 | |
156 | |
115 | my $XON = 1; # false if terminal wants us to pause |
157 | my $XON = 1; # false if terminal wants us to pause |
116 | my $PUSARTCMD; |
158 | my $PUSARTCMD; |
… | |
… | |
167 | sub out_d2 { } # 0..3 == 80c/132c/60hz/50hz |
209 | sub out_d2 { } # 0..3 == 80c/132c/60hz/50hz |
168 | |
210 | |
169 | sub out_82 { |
211 | sub out_82 { |
170 | # keyboard |
212 | # keyboard |
171 | |
213 | |
172 | # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INSERT L1(?) |
214 | # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 |
173 | # CLICK STARTSCAN ONLINE LOCKED | LED1 LED2 LED3 LED4 |
215 | # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4 |
174 | $KSTATUS = $_[0]; |
216 | $KSTATUS = $_[0]; |
175 | |
217 | |
176 | # start new scan unless scan in progress |
218 | # start new scan unless scan in progress |
177 | if (($_[0] & 0x40) && !@KXMIT) { |
219 | if (($_[0] & 0x40) && !@KXMIT) { |
178 | # do not reply with keys in locked mode |
220 | # do not reply with keys in locked mode |
… | |
… | |
201 | $RST |= 1; |
243 | $RST |= 1; |
202 | } |
244 | } |
203 | } |
245 | } |
204 | |
246 | |
205 | ############################################################################# |
247 | ############################################################################# |
|
|
248 | # I/O ports - input |
206 | |
249 | |
207 | my $NVRBIT; |
250 | my $NVRBIT; |
208 | my $LBA; |
251 | my $LBA6; # twice the frequenxy of LBA7 |
209 | |
252 | |
210 | sub in_00 { # pusart data |
253 | sub in_00 { # pusart data |
211 | # interrupt not generated here, because infinite |
254 | # interrupt not generated here, because infinite |
212 | # speed does not go well with the vt102. |
255 | # speed does not go well with the vt102. |
213 | |
256 | |
… | |
… | |
222 | sub in_22 { # modem buffer(?) |
265 | sub in_22 { # modem buffer(?) |
223 | # wild guess: -CTS -SPDI -RI -CD 0 0 0 0 |
266 | # wild guess: -CTS -SPDI -RI -CD 0 0 0 0 |
224 | 0x20 |
267 | 0x20 |
225 | } |
268 | } |
226 | |
269 | |
227 | sub in_0f { } # unknown, connected to out 2f |
270 | sub in_0f { 0xff } # vt102 unknown, connected to out 2f |
228 | |
271 | |
229 | sub in_42 { # flag buffer |
272 | sub in_42 { # flag buffer |
230 | ++$LBA; |
273 | ++$LBA6; |
231 | |
274 | |
232 | $NVRBIT = nvr ? 0x20 : 0x00 if ($LBA & 0x3) == 0x2; |
275 | $NVRBIT = nvr ? 0x20 : 0x00 if ($LBA6 & 0x3) == 0x2; |
233 | |
276 | |
234 | # KBD_XMITEMPTY LBA7 NVRDATA ODDFIELD - OPTION !GFX !AVO PUSART_TXRDY |
277 | # KBD_XMITEMPTY LBA7 NVRDATA ODDFIELD - OPTION !GFX !AVO PUSART_TXRDY |
235 | |
278 | |
236 | my $f = 0x85 | $NVRBIT; |
279 | my $f = 0x85 | $NVRBIT; |
237 | |
280 | |
238 | $f |= 0x02 unless $AVO; |
281 | $f |= 0x02 unless $AVO; |
239 | $f |= 0x40 if $LBA & 0x2; |
282 | $f |= 0x40 if $LBA6 & 0x2; |
240 | |
283 | |
241 | $f |
284 | $f |
242 | } |
285 | } |
243 | |
286 | |
244 | sub in_82 { # tbmt keyboard uart |
287 | sub in_82 { # tbmt keyboard uart |
… | |
… | |
246 | |
289 | |
247 | $RST |= 1; |
290 | $RST |= 1; |
248 | shift @KXMIT |
291 | shift @KXMIT |
249 | } |
292 | } |
250 | |
293 | |
251 | sub in_03 { 0xff } # unknown, printer uart input? |
294 | sub in_03 { 0xff } # vt102 unknown, printer uart input? |
252 | sub in_0b { 0xff } # unknown |
295 | sub in_0b { 0xff } # vt102 unknown |
253 | sub in_17 { 0xff } # unknown, printer status clear by reading? |
296 | sub in_17 { 0xff } # vt102 unknown, printer status clear by reading? |
254 | sub in_1b { 0xff } # unknown |
297 | sub in_1b { 0xff } # vt102 unknown |
255 | |
298 | |
256 | ############################################################################# |
299 | ############################################################################# |
|
|
300 | # 8085 cpu opcodes and flag handling |
|
|
301 | |
|
|
302 | my $x; # dummy scratchpad for opcodes |
257 | |
303 | |
258 | sub sf { # set flags (ZSC - AP not implemented) |
304 | sub sf { # set flags (ZSC - AP not implemented) |
259 | $FS = $_[0] & 0x080; |
305 | $FS = $_[0] & 0x080; |
260 | $FZ = ($_[0] & 0x0ff) == 0; |
306 | $FZ = !($_[0] & 0x0ff); |
261 | $FC = $_[0] & 0x100; |
307 | $FC = $_[0] & 0x100; |
262 | |
308 | |
263 | $_[0] & 0xff |
309 | $_[0] &= 0xff; |
|
|
310 | } |
|
|
311 | |
|
|
312 | sub sf8 { # set flags (ZSC - AP not implemented) |
|
|
313 | $FS = $_[0] & 0x080; |
|
|
314 | $FZ = !($_[0] & 0x0ff); |
|
|
315 | $FC = 0; |
264 | } |
316 | } |
265 | |
317 | |
266 | sub sf_nc { # set flags except carry |
318 | sub sf_nc { # set flags except carry |
267 | $FS = $_[0] & 0x080; |
319 | $FS = $_[0] & 0x080; |
268 | $FZ = ($_[0] & 0x0ff) == 0; |
320 | $FZ = ($_[0] & 0x0ff) == 0; |
269 | |
321 | |
270 | $_[0] & 0xff |
322 | $_[0] &= 0xff; |
271 | } |
323 | } |
272 | |
324 | |
273 | my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0 .. 255; |
325 | my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff; |
274 | my @ops; |
|
|
275 | |
326 | |
276 | my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); |
327 | my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); |
277 | my @cc = ('if !$FZ', 'if $FZ', 'if !$FC', 'if $FC', ';die', ';die', 'if !$FS', 'if $FS'); # die == unimplemented $FP parity |
328 | my @cc = ('if !$FZ', 'if $FZ', 'if !$FC', 'if $FC', ';die', ';die', 'if !$FS', 'if $FS'); # die == unimplemented $FP parity |
|
|
329 | |
|
|
330 | $op[0x00] = ''; |
278 | |
331 | |
279 | # mov r,r / r,M / M,r |
332 | # mov r,r / r,M / M,r |
280 | for my $s (0..7) { |
333 | for my $s (0..7) { |
281 | for my $d (0..7) { |
334 | for my $d (0..7) { |
282 | $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; |
335 | $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov |
283 | } |
336 | } |
284 | } |
337 | } |
285 | |
338 | |
286 | $op[0x00] = ''; |
339 | $op[0x76] = 'die "HLT"'; # hlt (mov m,m) |
|
|
340 | |
|
|
341 | # mvi r / M |
|
|
342 | $op[0x06 + $_ * 8] = "$reg[$_] = IMM8" for 0..7; |
287 | |
343 | |
288 | $op[0x01] = '($B, $C) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
344 | $op[0x01] = '($B, $C) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
289 | $op[0x11] = '($D, $E) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
345 | $op[0x11] = '($D, $E) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
290 | $op[0x21] = '($H, $L) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
346 | $op[0x21] = '($H, $L) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
291 | $op[0x31] = '$SP = IMM16' ; # lxi #d# 0xf000 because of limited stack |
347 | $op[0x31] = '$SP = IMM16' ; # lxi |
292 | |
348 | |
293 | $op[0x02] = '$M[$B * 256 + $C] = $A'; # stax |
349 | $op[0x02] = '$M[$B * 256 + $C] = $A'; # stax |
294 | $op[0x12] = '$M[$D * 256 + $E] = $A'; # stax |
350 | $op[0x12] = '$M[$D * 256 + $E] = $A'; # stax |
295 | $op[0x32] = '$M[IMM16 ] = $A'; # sta |
351 | $op[0x32] = '$M[IMM16 ] = $A'; # sta |
296 | |
352 | |
|
|
353 | $op[0x0a] = '$A = $M[$B * 256 + $C]'; # ldax b |
|
|
354 | $op[0x1a] = '$A = $M[$D * 256 + $E]'; # ldax d |
|
|
355 | $op[0x3a] = '$A = $M[IMM16]'; # lda |
|
|
356 | |
|
|
357 | $op[0x22] = '($M[IMM16], $M[IMM16 + 1]) = ($L, $H)'; # shld |
|
|
358 | $op[0x2a] = '($L, $H) = ($M[IMM16], $M[IMM16 + 1])'; # lhld |
|
|
359 | |
297 | sub inxdcx($$$) { |
360 | sub inxdcx($$$) { |
298 | $x = ($_[0] * 256 + $_[1] + $_[2]) & 0xffff; |
361 | $x = $_[0] * 256 + $_[1] + $_[2]; |
299 | $_[0] = $x >> 8; |
362 | ($_[0], $_[1]) = (($x >> 8) & 0xff, $x & 0xff); |
300 | $_[1] = $x & 0xff; |
|
|
301 | } |
363 | } |
302 | |
364 | |
303 | $op[0x03] = 'inxdcx $B, $C, 1'; # inx |
365 | $op[0x03] = 'inxdcx $B, $C, 1'; # inx |
304 | $op[0x13] = 'inxdcx $D, $E, 1'; # inx |
366 | $op[0x13] = 'inxdcx $D, $E, 1'; # inx |
305 | $op[0x23] = 'inxdcx $H, $L, 1'; # inx |
367 | $op[0x23] = 'inxdcx $H, $L, 1'; # inx |
… | |
… | |
308 | $op[0x1b] = 'inxdcx $D, $E, -1'; # dcx |
370 | $op[0x1b] = 'inxdcx $D, $E, -1'; # dcx |
309 | $op[0x2b] = 'inxdcx $H, $L, -1'; # dcx |
371 | $op[0x2b] = 'inxdcx $H, $L, -1'; # dcx |
310 | $op[0x3b] = '--$SP' ; # dcx |
372 | $op[0x3b] = '--$SP' ; # dcx |
311 | |
373 | |
312 | # "no carry" doesn't seem to be needed for vt100 - optimize? |
374 | # "no carry" doesn't seem to be needed for vt100 - optimize? |
313 | $op[0x04 + $_ * 8] = "$reg[$_] = sf_nc $reg[$_] + 1" for 0..7; # inr |
|
|
314 | $op[0x05 + $_ * 8] = "$reg[$_] = sf_nc $reg[$_] - 1" for 0..7; # dcr |
|
|
315 | |
|
|
316 | # mvi r / M |
|
|
317 | $op[0x06 + $_ * 8] = "$reg[$_] = IMM8" for 0..7; |
375 | $op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr |
|
|
376 | $op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr |
|
|
377 | |
|
|
378 | $op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc |
|
|
379 | $op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral |
|
|
380 | |
|
|
381 | $op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc |
|
|
382 | $op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar |
|
|
383 | |
|
|
384 | $op[0x2f] = '$A ^= 0xff'; # cma |
318 | |
385 | |
319 | # getting this insn wrong (its the only 16 bit insn to modify flags) |
386 | # getting this insn wrong (its the only 16 bit insn to modify flags) |
320 | # wasted three of my best days with mindless vt102 rom reverse engineering |
387 | # wasted three of my best days with mindless vt102 rom reverse engineering |
321 | sub dad { |
388 | sub dad { |
322 | $x = $H * 256 + $L + $_[0]; |
389 | $x = $H * 256 + $L + $_[0]; |
… | |
… | |
328 | $op[0x09] = 'dad $B * 256 + $C'; # dad |
395 | $op[0x09] = 'dad $B * 256 + $C'; # dad |
329 | $op[0x19] = 'dad $D * 256 + $E'; # dad |
396 | $op[0x19] = 'dad $D * 256 + $E'; # dad |
330 | $op[0x29] = 'dad $H * 256 + $L'; # dad |
397 | $op[0x29] = 'dad $H * 256 + $L'; # dad |
331 | $op[0x39] = 'dad $SP '; # dad |
398 | $op[0x39] = 'dad $SP '; # dad |
332 | |
399 | |
333 | $op[0x07] = ' $FC = $A >> 7; $A = ($A * 2 + $FC) & 0xff '; # rlc |
400 | $op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add |
334 | $op[0x17] = ' ($FC, $A) = ($A >> 7, ($A * 2 + $FC) & 0xff)'; # ral |
401 | $op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc |
|
|
402 | $op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub |
|
|
403 | $op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb |
|
|
404 | $op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana |
|
|
405 | $op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra |
|
|
406 | $op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora |
|
|
407 | $op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp |
|
|
408 | # possible todo: optimize ora a, maybe xra a |
335 | |
409 | |
336 | $op[0x0f] = ' $FC = $A & 1; $A = ($A >> 1) | ($FC && 0x80) '; # rrc |
410 | $op[0xc6] = 'sf $A += IMM8'; # adi |
337 | $op[0x1f] = ' ($FC, $A) = ($A & 1, ($A >> 1) | ($FC && 0x80))'; # rar |
411 | # ce ACI NYI, apparently unused |
|
|
412 | $op[0xd6] = 'sf $A -= IMM8'; # sui |
|
|
413 | # de SBI NYI, apparently unused |
|
|
414 | $op[0xe6] = 'sf8 $A &= IMM8'; # ani |
|
|
415 | $op[0xee] = 'sf8 $A ^= IMM8'; # xri |
|
|
416 | $op[0xf6] = 'sf8 $A |= IMM8'; # ori |
|
|
417 | $op[0xfe] = 'sf $A - IMM8'; # cpi |
338 | |
418 | |
339 | $op[0x0a] = '$A = $M[$B * 256 + $C]'; # ldax b |
419 | $op[0xc5] = 'PUSH $B; PUSH $C'; |
340 | $op[0x1a] = '$A = $M[$D * 256 + $E]'; # ldax d |
420 | $op[0xd5] = 'PUSH $D; PUSH $E'; |
341 | $op[0x3a] = '$A = $M[IMM16]'; # lda |
421 | $op[0xe5] = 'PUSH $H; PUSH $L'; |
|
|
422 | $op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw |
|
|
423 | |
|
|
424 | $op[0xc1] = '($C, $B) = (POP, POP)'; # pop |
|
|
425 | $op[0xd1] = '($E, $D) = (POP, POP)'; # pop |
|
|
426 | $op[0xe1] = '($L, $H) = (POP, POP)'; # pop |
|
|
427 | $op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw |
|
|
428 | |
|
|
429 | $op[0xc2 + $_ * 8] = 'BRA IMM16 ' . $cc[$_] for 0..7; # jcc |
|
|
430 | $op[0xc3] = 'JMP IMM16'; # jmp |
|
|
431 | |
|
|
432 | $op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) ' . $cc[$_] for 0..7; # ccc |
|
|
433 | $op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call |
|
|
434 | |
|
|
435 | $op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 ' . $cc[$_] for 0..7; # rcc |
|
|
436 | $op[0xc9] = 'JMP POP + POP * 256'; # ret |
|
|
437 | |
|
|
438 | $op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst |
|
|
439 | |
|
|
440 | $op[0xe9] = 'JMP $H * 256 + $L'; # pchl |
|
|
441 | # f9 SPHL NYI, apparently unused |
|
|
442 | |
|
|
443 | $op[0x37] = '$FC = 1 '; # stc |
|
|
444 | $op[0x3f] = '$FC = !$FC'; # cmc |
|
|
445 | |
|
|
446 | $op[0xd3] = 'OUT'; # out |
|
|
447 | $op[0xdb] = 'IN'; # in |
|
|
448 | |
|
|
449 | $op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg |
|
|
450 | |
|
|
451 | # e3 xthl NYI # @ 917b in e69, hl <-> (sp) |
342 | |
452 | |
343 | $op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (incomplete) |
453 | $op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (incomplete) |
344 | $op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (incomplete) |
454 | $op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (incomplete) |
345 | |
455 | |
346 | $op[0x22] = '($M[IMM16], $M[IMM16 + 1]) = ($L, $H)'; # shld |
456 | $op[0xf3] = '$IFF = 0'; # DI |
347 | $op[0x2a] = '($L, $H) = ($M[IMM16], $M[IMM16 + 1])'; # lhld |
457 | $op[0xfb] = '$IFF = 1'; # EI |
348 | |
458 | |
349 | # yeah, the fucking setup screens actually use daa... |
459 | # yeah, the fucking setup screens actually use daa... |
350 | $op[0x27] = ' |
460 | $op[0x27] = ' |
351 | my ($h, $l); |
461 | my ($h, $l); |
352 | |
462 | |
353 | ($h, $l) = ($A >> 4, $A & 15); |
463 | ($h, $l) = ($A >> 4, $A & 15); |
354 | |
464 | |
355 | if ($l > 9 || $FA) { |
465 | if ($l > 9 || $FA) { |
356 | $A = sf $A + 6; |
466 | sf $A += 6; |
357 | ($h, $l) = ($A >> 4, $A & 15); |
467 | ($h, $l) = ($A >> 4, $A & 15); |
358 | } |
468 | } |
359 | |
469 | |
360 | if ($h > 9 || $FC) { |
470 | if ($h > 9 || $FC) { |
361 | $h += 6; |
471 | $h += 6; |
362 | $A = ($h * 16 + $l) & 0xff; |
472 | $A = ($h * 16 + $l) & 0xff; |
363 | } |
473 | } |
364 | '; # daa, almost certainly borked, also, acarry not set by sf |
474 | '; # daa, almost certainly borked, also, acarry not set by sf |
365 | |
475 | |
366 | $op[0x2f] = '$A ^= 0xff'; # cma |
|
|
367 | |
|
|
368 | $op[0x37] = '$FC = 1 '; # stc |
|
|
369 | $op[0x3f] = '$FC = !$FC'; # cmc |
|
|
370 | |
|
|
371 | $op[0x76] = 'die "HLT"'; # hlt |
|
|
372 | |
|
|
373 | $op[0x80 + $_] = '$A = sf $A + ' . $reg[$_] for 0..7; # add |
|
|
374 | $op[0x88 + $_] = '$A = sf $A + $FC + ' . $reg[$_] for 0..7; # adc |
|
|
375 | $op[0x90 + $_] = '$A = sf $A - ' . $reg[$_] for 0..7; # sub |
|
|
376 | $op[0x98 + $_] = '$A = sf $A - $FC - ' . $reg[$_] for 0..7; # sbb |
|
|
377 | $op[0xa0 + $_] = '$A = sf $A & ' . $reg[$_] for 0..7; # ana |
|
|
378 | $op[0xa8 + $_] = '$A = sf $A ^ ' . $reg[$_] for 0..7; # xra |
|
|
379 | $op[0xb0 + $_] = '$A = sf $A | ' . $reg[$_] for 0..7; # ora |
|
|
380 | $op[0xb8 + $_] = ' sf $A - ' . $reg[$_] for 0..7; # cmp |
|
|
381 | # possible todo: optimize ora a, maybe xra a |
|
|
382 | |
|
|
383 | $op[0xc6 + $_] = '$A = sf $A + IMM8'; # adi |
|
|
384 | $op[0xd6 + $_] = '$A = sf $A - IMM8'; # sui |
|
|
385 | $op[0xe6 + $_] = '$A = sf $A & IMM8'; # ani |
|
|
386 | $op[0xee + $_] = '$A = sf $A ^ IMM8'; # xri |
|
|
387 | $op[0xf6 + $_] = '$A = sf $A | IMM8'; # ori |
|
|
388 | $op[0xfe + $_] = ' sf $A - IMM8'; # cpi |
|
|
389 | |
|
|
390 | $op[0xc1] = '($C, $B) = (POP, POP)'; # pop |
|
|
391 | $op[0xd1] = '($E, $D) = (POP, POP)'; # pop |
|
|
392 | $op[0xe1] = '($L, $H) = (POP, POP)'; # pop |
|
|
393 | $op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = (!!($x & 0x80), !!($x & 0x40), !!($x & 0x10), !!($x & 0x04), !!($x & 0x01))'; # pop psw |
|
|
394 | |
|
|
395 | $op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg |
|
|
396 | |
|
|
397 | $op[0xc2 + $_ * 8] = 'BRA IMM16 ' . $cc[$_] for 0..7; # jcc |
|
|
398 | $op[0xc3] = 'JMP IMM16'; # jmp |
|
|
399 | |
|
|
400 | $op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) ' . $cc[$_] for 0..7; # ccc |
|
|
401 | $op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call |
|
|
402 | |
|
|
403 | $op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst |
|
|
404 | |
|
|
405 | $op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 ' . $cc[$_] for 0..7; # rcc |
|
|
406 | $op[0xc9] = 'JMP POP + POP * 256'; # ret |
|
|
407 | |
|
|
408 | $op[0xc5] = 'PUSH $B; PUSH $C'; |
|
|
409 | $op[0xd5] = 'PUSH $D; PUSH $E'; |
|
|
410 | $op[0xe5] = 'PUSH $H; PUSH $L'; |
|
|
411 | $op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw |
|
|
412 | |
|
|
413 | $op[0xd3] = 'OUT'; # out |
|
|
414 | $op[0xdb] = 'IN'; # in |
|
|
415 | |
|
|
416 | # e3 xthl @ 917b, hl <-> (sp) |
|
|
417 | |
|
|
418 | $op[0xe9] = 'JMP $H * 256 + $L'; # pchl |
|
|
419 | |
|
|
420 | $op[0xf3] = '$IFF = 0'; # DI |
|
|
421 | $op[0xfb] = '$IFF = 1'; # EI |
|
|
422 | |
|
|
423 | @ops = @op; # for debugging #d# |
|
|
424 | |
|
|
425 | ############################################################################# |
476 | ############################################################################# |
|
|
477 | # print cpu status for debugging purposes |
426 | |
478 | |
427 | # print cpu status, for debugging |
479 | # print cpu status, for debugging |
428 | sub status { |
480 | sub status { |
429 | my $PC = shift || $PC; |
481 | my $PC = shift || $PC; |
430 | |
482 | |
… | |
… | |
434 | ($FZ ? "1" : "0") |
486 | ($FZ ? "1" : "0") |
435 | . ($FS ? "1" : "0") |
487 | . ($FS ? "1" : "0") |
436 | . ($FC ? "1" : "0") |
488 | . ($FC ? "1" : "0") |
437 | . ($FA ? "1" : "0") |
489 | . ($FA ? "1" : "0") |
438 | . ($FP ? "1" : "0"), |
490 | . ($FP ? "1" : "0"), |
439 | $M[$PC], $ops[$M[$PC]]; |
491 | $M[$PC], $op[$M[$PC]]; |
440 | } |
492 | } |
441 | |
493 | |
442 | ############################################################################# |
494 | ############################################################################# |
|
|
495 | # video emulation |
443 | |
496 | |
444 | my @chr = ( |
497 | binmode STDOUT; |
|
|
498 | |
|
|
499 | my @CHARMAP = ( |
445 | " " , "\x{29eb}", "\x{2592}", "\x{2409}", |
500 | " " , "\x{29eb}", "\x{2592}", "\x{2409}", |
446 | "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", |
501 | "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", |
447 | "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", |
502 | "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", |
448 | "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", |
503 | "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", |
449 | "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", |
504 | "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", |
… | |
… | |
451 | "\x{252c}", "\x{2502}", "\x{2264}", "\x{2265}", |
506 | "\x{252c}", "\x{2502}", "\x{2264}", "\x{2265}", |
452 | "\x{03c0}", "\x{2260}", "\x{00a3}", "\x{00b7}", |
507 | "\x{03c0}", "\x{2260}", "\x{00a3}", "\x{00b7}", |
453 | (map chr, 0x020 .. 0x7e), |
508 | (map chr, 0x020 .. 0x7e), |
454 | ); |
509 | ); |
455 | |
510 | |
456 | utf8::encode $_ for @chr; |
511 | utf8::encode $_ for @CHARMAP; |
457 | |
512 | |
458 | my @sgr; # sgr sequences for attributes |
513 | my @SGR; # sgr sequences for attributes |
459 | |
514 | |
460 | for (0x00 .. 0xff) { |
515 | for (0x00 .. 0xff) { |
461 | my $sgr = ""; |
516 | my $sgr = ""; |
|
|
517 | |
|
|
518 | # ~1 sgr 5 blink |
|
|
519 | # ~2 sgr 4 underline |
|
|
520 | # ~4 sgr 1 bold |
|
|
521 | # 0x80 in attr, sgr 7, reversed |
462 | |
522 | |
463 | $sgr .= ";5" unless $_ & 0x01; |
523 | $sgr .= ";5" unless $_ & 0x01; |
464 | $sgr .= ";4" unless $_ & 0x02; |
524 | $sgr .= ";4" unless $_ & 0x02; |
465 | $sgr .= ";1" unless $_ & 0x04; |
525 | $sgr .= ";1" unless $_ & 0x04; |
466 | $sgr .= ";7" if $_ & 0x80; |
526 | $sgr .= ";7" if $_ & 0x80; |
467 | |
527 | |
468 | $sgr[$_] = "\e[${sgr}m"; |
528 | $SGR[$_] = "\e[${sgr}m"; |
469 | } |
529 | } |
470 | |
530 | |
471 | sub prscr { |
531 | my @LED = $VT102 |
|
|
532 | ? qw(L1 INSERT DSR CTS LOCKED LOCAL SCAN BEEP) |
|
|
533 | : qw(L4 L3 L2 L1 LOCKED LOCAL SCAN BEEP); |
|
|
534 | |
|
|
535 | # display screen |
|
|
536 | sub display { |
472 | my $i = 0x2000; |
537 | my $i = 0x2000; |
473 | |
538 | |
|
|
539 | my $leds = join " ", map $KSTATUS & 2**$_ ? "\e[7m$LED[$_]\e[m" : "$LED[$_]", reverse 0 .. $#LED; |
|
|
540 | |
474 | my $scr = sprintf "\e[H--- KBD %08b CLK %d\e[K\n", $KSTATUS, $CLK; |
541 | my $scr = sprintf "\e[H--- LED [ %s ] CLK %d\e[K\n", $leds, $CLK; |
475 | |
542 | |
476 | $scr .= "\e[?5" . ($DC11_REVERSE ? "h" : "l"); |
543 | $scr .= "\e[?5" . ($DC11_REVERSE ? "h" : "l"); |
477 | |
544 | |
478 | line: |
545 | line: |
479 | for my $y (0 .. 25) { # ntsc, two vblank delay lines, up to 24 text lines |
546 | for my $y (0 .. 25) { # ntsc, two vblank delay lines, up to 24 text lines |
480 | my $prev_sgr; |
547 | my $prev_sgr; |
481 | |
548 | |
482 | $scr .= sprintf "%2d |", ++$y; |
549 | $scr .= sprintf "%2d \xe2\x94\x82", $y; |
483 | |
550 | |
484 | for (0..139) { |
551 | for (0..139) { |
485 | my $c = $M[$i]; |
552 | my $c = $M[$i]; |
486 | |
553 | |
487 | if ($c == 0x7f) { # also 0xff, but the firmware avoids that |
554 | if ($c == 0x7f) { # also 0xff, but the firmware avoids that |
488 | $scr .= "\e[m|\e[K\n"; |
555 | $scr .= "\e[m\xe2\x94\x82\e[K\n"; |
489 | |
556 | |
490 | my $a1 = $M[$i + 1]; |
557 | my $a1 = $M[$i + 1]; |
491 | my $a0 = $M[$i + 2]; |
558 | my $a0 = $M[$i + 2]; |
492 | |
559 | |
493 | $i = 0x2000 + (($a1 * 256 + $a0) & 0xfff); |
560 | $i = 0x2000 + (($a1 * 256 + $a0) & 0xfff); |
494 | |
561 | |
495 | next line; |
562 | next line; |
496 | } |
563 | } |
497 | |
564 | |
498 | my $sgr = $sgr[ ($M[$i++ + 0x1000] & 15) | ($c & 0x80)]; |
565 | my $sgr = $SGR[ ($M[$i++ + 0x1000] & 15) | ($c & 0x80)]; |
499 | |
|
|
500 | # ~1 sgr 5 blink |
|
|
501 | # ~2 sgr 4 underline |
|
|
502 | # ~4 sgr 1 bold |
|
|
503 | # 0x80 in attr, sgr 7, reversed |
|
|
504 | |
566 | |
505 | $scr .= $prev_sgr = $sgr if $sgr ne $prev_sgr; |
567 | $scr .= $prev_sgr = $sgr if $sgr ne $prev_sgr; |
506 | |
568 | |
507 | $scr .= $chr[$c & 0x7f]; |
569 | $scr .= $CHARMAP[$c & 0x7f]; |
508 | } |
570 | } |
509 | |
571 | |
510 | $scr .= "\e[K\nvideo overflow\e[K\n"; |
572 | $scr .= "\e[K\nvideo overflow\e[K\n"; |
511 | last; |
573 | last; |
512 | } |
574 | } |
513 | |
575 | |
514 | $scr .= "\e[m"; |
576 | $scr .= "\e[m\e[J"; |
515 | |
|
|
516 | if (0) { |
|
|
517 | $scr .= "\e[K\n"; |
|
|
518 | for my $o (0x200 .. 0x232) { |
|
|
519 | $scr .= sprintf "%04x:", $o * 16; |
|
|
520 | for (0..15) { |
|
|
521 | $scr .= sprintf " %02x", $M[$o * 16 + $_]; |
|
|
522 | } |
|
|
523 | $scr .= "\e[K\n"; |
|
|
524 | } |
|
|
525 | } |
|
|
526 | |
|
|
527 | $scr .= "\e[J"; |
|
|
528 | |
577 | |
529 | syswrite STDOUT, $scr; |
578 | syswrite STDOUT, $scr; |
530 | } |
579 | } |
531 | |
580 | |
532 | ############################################################################# |
581 | ############################################################################# |
|
|
582 | # keyboard handling |
533 | |
583 | |
534 | if (@ARGV) { |
584 | # 0x080 shift, 0x100 ctrl |
535 | require IO::Pty; |
585 | my %KEYMAP = ( |
536 | $PTY = IO::Pty->new; |
586 | "\t" => 0x3a, |
|
|
587 | "\r" => 0x64, |
|
|
588 | "\n" => 0x44, |
|
|
589 | |
|
|
590 | "\x00" => 0x77 | 0x100, # CTRL-SPACE |
|
|
591 | "\x1c" => 0x45 | 0x100, # CTRL-\ |
|
|
592 | "\x1d" => 0x14 | 0x100, # CTRL-] |
|
|
593 | "\x1e" => 0x24 | 0x100, # CTRL-~ |
|
|
594 | "\x1f" => 0x75 | 0x100, # CTRL-? |
|
|
595 | |
|
|
596 | # hardcoded rxvt keys |
|
|
597 | "\e" => 0x2a, # ESC |
|
|
598 | "\e[3~" => 0x03, # DC |
|
|
599 | "\e[5~" => 0x7e, # CAPS LOCK (prior) |
|
|
600 | "\e[6~" => 0x6a, # NO SCROLL (next) |
|
|
601 | "\e[A" => 0x30, # UP |
|
|
602 | "\e[B" => 0x22, # DOWN |
|
|
603 | "\e[C" => 0x10, # RIGHT |
|
|
604 | "\e[D" => 0x20, # LEFT |
|
|
605 | "\e[a" => 0x30 | 0x080, # UP |
|
|
606 | "\e[b" => 0x22 | 0x080, # DOWN |
|
|
607 | "\e[c" => 0x10 | 0x080, # RIGHT |
|
|
608 | "\e[d" => 0x20 | 0x080, # LEFT |
|
|
609 | "\e[7~" => 0x7b, # SETUP (home) |
|
|
610 | "\e[8~" => 0x23, # BREAK (end) |
|
|
611 | "\e[8\$" => 0x23 | 0x080, # SHIFT BREAK / DISCONNECT (shift-end) |
|
|
612 | "\x7f" => 0x33, # BACKSPACE |
|
|
613 | |
|
|
614 | "\e[11~" => 0x32, # F1 |
|
|
615 | "\e[11~" => 0x42, # F2 |
|
|
616 | "\e[11~" => 0x31, # F3 |
|
|
617 | "\e[11~" => 0x41, # F4 |
|
|
618 | ); |
537 | |
619 | |
538 | my $slave = $PTY->slave; |
620 | @KEYMAP{map chr, 0x20 .. 0x40, 0x5b .. 0x7e} = unpack "C*", pack "H*", |
|
|
621 | "779ad5a9a8b8a755a6b5b6b466256575" . "351a3929283837273626d656e634e5f5" . "b9" # 20..40 |
|
|
622 | . "154514b7a5" . "244a6879591949485816574746766706" . "050a185a0817780969077a95c594a4"; # 5b..7e |
539 | |
623 | |
540 | $PTY->set_winsize (24, 80); |
624 | $KEYMAP{"\x1f" & $_} ||= $KEYMAP{$_} | 0x100 for "a" .. "z"; # ctrl |
|
|
625 | $KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift |
541 | |
626 | |
542 | unless (fork) { |
627 | my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; |
543 | $ENV{TERM} = $VT102 ? "vt102" : "vt100"; |
628 | $KEYMATCH = qr{^($KEYMATCH)}s; |
544 | |
629 | |
545 | close $PTY; |
630 | my %KMOD; |
546 | |
631 | |
547 | open STDIN , "<&", $slave; |
632 | sub key { |
548 | open STDOUT, ">&", $slave; |
633 | my ($key) = @_; |
549 | open STDERR, ">&", $slave; |
|
|
550 | |
634 | |
551 | system "stty ixoff erase ^H"; |
635 | push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up |
|
|
636 | push @KQUEUE, -0x7d if !($key & 0x080) && delete $KMOD{0x7d}; # shift-up |
552 | |
637 | |
553 | $PTY->make_slave_controlling_terminal; |
638 | push @KQUEUE, 0x7c if $key & 0x100 && !$KMOD{0x7c}++; # ctrl-down |
554 | $PTY->close_slave; |
639 | push @KQUEUE, 0x7d if $key & 0x080 && !$KMOD{0x7d}++; # shift-down |
555 | |
640 | |
556 | exec @ARGV; |
641 | $key &= 0x7f; |
557 | } |
642 | push @KQUEUE, $key, -$key; |
558 | |
|
|
559 | $PTY->close_slave; |
|
|
560 | |
|
|
561 | } else { |
|
|
562 | open $PTY, "</dev/null" or die;#d |
|
|
563 | } |
643 | } |
564 | |
644 | |
565 | ############################################################################# |
645 | my $STDIN_BUF; |
566 | |
646 | |
|
|
647 | sub stdin_parse { |
|
|
648 | key $KEYMAP{$1} |
|
|
649 | while $STDIN_BUF =~ s/$KEYMATCH//; |
|
|
650 | |
|
|
651 | # skip input we can't decipher |
|
|
652 | substr $STDIN_BUF, 0, 1, ""; |
|
|
653 | } |
|
|
654 | |
|
|
655 | if ($KBD) { |
|
|
656 | system "stty -icanon -icrnl -inlcr -echo min 1 time 0"; |
|
|
657 | eval q{ sub END { system "stty sane" } }; |
|
|
658 | $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { exit 1 }; |
|
|
659 | } |
|
|
660 | |
|
|
661 | ############################################################################# |
567 | # initial key input, to set up online mode etc. |
662 | # initial key input, to set up online mode etc. |
|
|
663 | # could be done via nvram defaults |
|
|
664 | |
568 | @KQUEUE = ( |
665 | @KQUEUE = ( |
569 | 0x7b, -0x7b, # setup |
666 | 0x7b, -0x7b, # setup |
570 | 0, # delay |
667 | 0, # delay |
571 | 0x28, -0x28, # 4, toggle local/online |
668 | 0x28, -0x28, # 4, toggle local/online |
572 | 0x38, -0x38, # 5, setup b |
669 | 0x38, -0x38, # 5, setup b |
… | |
… | |
583 | 0x37, -0x37, # 6 toggle wrap around |
680 | 0x37, -0x37, # 6 toggle wrap around |
584 | 0x7b, -0x7b, # leave setup |
681 | 0x7b, -0x7b, # leave setup |
585 | ); |
682 | ); |
586 | |
683 | |
587 | ############################################################################# |
684 | ############################################################################# |
|
|
685 | # process/pty management |
588 | |
686 | |
589 | # 0x080 shift, 0x100 ctrl |
687 | require IO::Pty; |
590 | my %KEYMAP = ( |
688 | $PTY = IO::Pty->new; |
591 | "\t" => 0x3a, |
|
|
592 | "\r" => 0x64, |
|
|
593 | "\n" => 0x44, |
|
|
594 | |
689 | |
595 | "\x00" => 0x77 | 0x100, # CTRL-SPACE |
690 | my $slave = $PTY->slave; |
596 | "\x1c" => 0x45 | 0x100, # CTRL-\ |
|
|
597 | "\x1d" => 0x14 | 0x100, # CTRL-] |
|
|
598 | "\x1e" => 0x24 | 0x100, # CTRL-~ |
|
|
599 | "\x1f" => 0x75 | 0x100, # CTRL-? |
|
|
600 | |
691 | |
601 | # hardcoded rxvt keys |
692 | $PTY->set_winsize (24, 80); |
602 | "\e" => 0x2a, # ESC |
|
|
603 | "\e[3~" => 0x03, # DC |
|
|
604 | "\e[5~" => 0x7e, # CAPS LOCK (prior) |
|
|
605 | "\e[6~" => 0x6a, # NO SCROLL (next) |
|
|
606 | "\e[A" => 0x30, # UP |
|
|
607 | "\e[B" => 0x22, # DOWN |
|
|
608 | "\e[C" => 0x10, # RIGHT |
|
|
609 | "\e[D" => 0x20, # LEFT |
|
|
610 | "\e[a" => 0x30 | 0x080, # UP |
|
|
611 | "\e[b" => 0x22 | 0x080, # DOWN |
|
|
612 | "\e[c" => 0x10 | 0x080, # RIGHT |
|
|
613 | "\e[d" => 0x20 | 0x080, # LEFT |
|
|
614 | "\e[7~" => 0x7b, # SETUP (home) |
|
|
615 | "\e[8~" => 0x23, # BREAK (end) |
|
|
616 | "\e[8\$" => 0x23 | 0x080, # SHIFT BREAK / DISCONNECT (shift-end) |
|
|
617 | "\x7f" => 0x33, # BACKSPACE |
|
|
618 | |
693 | |
619 | "\e[11~" => 0x32, # F1 |
694 | unless (fork) { |
620 | "\e[11~" => 0x42, # F2 |
695 | $ENV{LC_ALL} = "C"; |
621 | "\e[11~" => 0x31, # F3 |
696 | $ENV{TERM} = $VT102 ? "vt102" : "vt100"; |
622 | "\e[11~" => 0x41, # F4 |
|
|
623 | ); |
|
|
624 | |
697 | |
625 | @KEYMAP{map chr, 0x20..0x40} = unpack "C*", pack "H*", |
698 | close $PTY; |
626 | "779ad5a9a8b8a755a6b5b6b466256575" . "351a3929283837273626d656e634e5f5" . "b9"; |
|
|
627 | |
699 | |
628 | @KEYMAP{map chr, 0x5b .. 0x7e} = unpack "C*", pack "H*", |
700 | open STDIN , "<&", $slave; |
629 | "154514b7a5" . "244a6879591949485816574746766706" . "050a185a0817780969077a95c594a4"; |
701 | open STDOUT, ">&", $slave; |
|
|
702 | open STDERR, ">&", $slave; |
630 | |
703 | |
631 | $KEYMAP{"\x1f" & $_} ||= $KEYMAP{$_} | 0x100 for "a" .. "z"; # ctrl |
704 | system "stty ixoff erase ^H"; |
632 | $KEYMAP{uc $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift |
|
|
633 | |
705 | |
634 | my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; |
706 | $PTY->make_slave_controlling_terminal; |
635 | $KEYMATCH = qr{^($KEYMATCH)}s; |
707 | $PTY->close_slave; |
636 | |
708 | |
637 | sub key { |
709 | @ARGV = "sh" unless @ARGV; |
638 | my ($key) = @_; |
710 | exec @ARGV; |
639 | |
|
|
640 | state %MOD; |
|
|
641 | |
|
|
642 | push @KQUEUE, -0x7c if !($key & 0x100) && delete $MOD{0x7c}; # ctrl-up |
|
|
643 | push @KQUEUE, -0x7d if !($key & 0x080) && delete $MOD{0x7d}; # shift-up |
|
|
644 | |
|
|
645 | push @KQUEUE, 0x7c if $key & 0x100 && !$MOD{0x7c}++; # ctrl-down |
|
|
646 | push @KQUEUE, 0x7d if $key & 0x080 && !$MOD{0x7d}++; # shift-down |
|
|
647 | |
|
|
648 | $key &= 0x7f; |
|
|
649 | push @KQUEUE, $key, -$key; |
|
|
650 | } |
711 | } |
651 | |
712 | |
652 | my $STDIN_BUF; |
713 | $PTY->close_slave; |
653 | |
714 | |
654 | sub stdin_parse { |
|
|
655 | key $KEYMAP{$1} |
|
|
656 | while $STDIN_BUF =~ s/$KEYMATCH//; |
|
|
657 | |
|
|
658 | # skip input we can't decipher |
|
|
659 | substr $STDIN_BUF, 0, 1, ""; |
|
|
660 | } |
|
|
661 | |
|
|
662 | if ($KBD) { |
|
|
663 | system "stty -icanon -icrnl -inlcr -echo min 1 time 0"; |
|
|
664 | eval q{ sub END { system "stty sane" } }; |
|
|
665 | $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { exit 1 }; |
|
|
666 | } |
|
|
667 | |
|
|
668 | ############################################################################# |
715 | ############################################################################# |
|
|
716 | # the actual hardware simulator |
669 | |
717 | |
670 | my @ICACHE; # compiled instruction cache |
718 | my @ICACHE; # compiled instruction cache |
671 | |
719 | |
672 | # the cpu |
720 | my $POWERSAVE; # powersave counter |
|
|
721 | |
|
|
722 | my $RIN; # libev for the less well-off |
|
|
723 | |
|
|
724 | (vec $RIN, 0, 1) = 1 if $KBD; |
|
|
725 | (vec $RIN, fileno $PTY, 1) = 1 if $PTY; |
|
|
726 | |
673 | while () { |
727 | while () { |
674 | |
|
|
675 | # execute extended basic blocks |
728 | # execute extended basic blocks |
676 | $PC = ($ICACHE[$PC] ||= do { |
729 | $PC = ($ICACHE[$PC] ||= do { |
677 | my $pc = $PC; |
730 | my $pc = $PC; |
678 | |
731 | |
679 | my $insn = ""; |
732 | my $insn = ""; |
680 | |
733 | |
681 | # the jit compiler |
734 | # the jit compiler |
682 | for (0..15) { |
735 | for (0..31) { |
683 | |
|
|
684 | # optional tracing support |
|
|
685 | if (0) { |
|
|
686 | $insn .= qq< |
|
|
687 | if (\$PRSTATUS) { |
|
|
688 | status $pc; |
|
|
689 | die unless --\$PRSTATUS; |
|
|
690 | } |
|
|
691 | >; |
|
|
692 | } |
|
|
693 | |
|
|
694 | my $imm; |
736 | my $imm; |
695 | my $op = $op[$M[$pc++]]; |
737 | my $op = $op[$M[$pc++]]; |
696 | |
738 | |
697 | for ($op) { |
739 | for ($op) { |
698 | s/\bPUSH\b/\$M[--\$SP] =/g; # push byte to stack |
740 | s/\bPUSH\b/\$M[--\$SP] =/g; # push byte to stack |
… | |
… | |
711 | |
753 | |
712 | $insn .= "$op;\n"; |
754 | $insn .= "$op;\n"; |
713 | } |
755 | } |
714 | |
756 | |
715 | |
757 | |
716 | $insn .= "$pc"; |
758 | $insn .= $pc; |
717 | $insn =~ s/\x00.*$//s; |
759 | $insn =~ s/\x00.*$//s; |
718 | |
760 | |
719 | eval "use integer; sub { $insn }" or die "$insn: $@" |
761 | eval "use integer; sub { $insn }" or die "$insn: $@" |
720 | })->(); |
762 | })->(); |
721 | |
763 | |
722 | ++$CLK; |
764 | ++$CLK; |
723 | |
765 | |
724 | # things we do from time too time only |
766 | # things we do from time to time only |
725 | unless ($CLK & 0xf) { |
767 | unless ($CLK & 0xf) { |
726 | # do I/O |
768 | # do I/O |
727 | |
769 | |
728 | unless ($CLK & 0x7ff) { |
770 | unless ($CLK & 0xfff) { |
729 | |
771 | if (select $x = $RIN, undef, undef, $POWERSAVE < 100 ? 0 : 0.2) { |
730 | # pty/serial I/O |
772 | # pty/serial I/O |
731 | unless (@PUSARTRECV || @KQUEUE || !$PTY) { |
773 | if ($PTY && (vec $x, fileno $PTY, 1) && (@PUSARTRECV < 128) && !@KQUEUE) { |
732 | my $rin = ""; (vec $rin, fileno $PTY, 1) = 1; |
774 | my $rin = ""; (vec $rin, fileno $PTY, 1) = 1; |
733 | |
775 | |
734 | if (select $rin, undef, undef, 0) { |
776 | if (select $rin, undef, undef, 0) { |
735 | sysread $PTY, my $buf, 256; |
777 | sysread $PTY, my $buf, 256; |
|
|
778 | |
|
|
779 | # linux don't do cs7 and/or parity anymore, so we need to filter |
|
|
780 | # out xoff characters to avoid freezes. |
736 | push @PUSARTRECV, unpack "C*", $buf; |
781 | push @PUSARTRECV, grep { ($_ & 0x7f) != 0x13 } unpack "C*", $buf; |
|
|
782 | } |
737 | } |
783 | } |
738 | } |
|
|
739 | |
784 | |
740 | # keyboard input |
785 | # keyboard input |
741 | if ($KBD) { |
786 | if ($KBD && (vec $x, 0, 1)) { |
742 | while (select my $rin = "\x01", undef, undef, 0) { |
787 | while (select my $rin = "\x01", undef, undef, 0) { |
743 | sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF |
788 | sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF |
744 | or last; |
789 | or last; |
|
|
790 | } |
|
|
791 | |
|
|
792 | stdin_parse if length $STDIN_BUF; |
|
|
793 | $POWERSAVE = 0; |
745 | } |
794 | } |
746 | |
795 | |
747 | stdin_parse if length $STDIN_BUF; |
796 | $POWERSAVE = 0; |
|
|
797 | } else { |
|
|
798 | ++$POWERSAVE unless @PUSARTRECV || @KQUEUE; |
748 | } |
799 | } |
749 | } |
800 | } |
750 | |
801 | |
751 | # kick off various interrupts |
802 | # kick off various interrupts |
752 | |
803 | |
… | |
… | |
757 | unless ($CLK & 0x1ff) { |
808 | unless ($CLK & 0x1ff) { |
758 | $RST |= 4; # vertical retrace |
809 | $RST |= 4; # vertical retrace |
759 | } |
810 | } |
760 | |
811 | |
761 | # handle video hardware |
812 | # handle video hardware |
762 | |
|
|
763 | unless ($CLK & 0x1fff) { |
813 | unless ($CLK & 0x3fff) { |
764 | prscr; |
814 | display; |
765 | } |
815 | } |
766 | } |
816 | } |
767 | |
817 | |
768 | # the interrupt logic |
818 | # the interrupt logic |
769 | $x = $INTPEND & ~$INTMASK; |
819 | if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) { |
770 | if (($RST || $x) && $IFF) { |
|
|
771 | # rst 1 kbd data available |
820 | # rst 1 kbd data available |
772 | # rst 2 pusart xmit+recv flag |
821 | # rst 2 pusart xmit+recv flag |
773 | # rst 4 vertical retrace |
822 | # rst 4 vertical retrace |
774 | # 5.5 vt125 mb7 trans ready (serial send?) |
823 | # 5.5 vt125 mb7 trans ready (serial send?) |
775 | # 6.5 vt125 mb7 read ready (something modem?) |
824 | # 6.5 vt125 mb7 read ready (something modem?) |
776 | # 7.5 vt125 mb7 vblank h(?) |
825 | # 7.5 vt125 mb7 vblank h(?) |
777 | # trap vt125 mbi init h(?) |
826 | # trap vt125 mbi init h(?) |
778 | my $vec; |
827 | my $vec; |
779 | |
828 | |
|
|
829 | my $pend = $INTPEND & ~$INTMASK; |
|
|
830 | |
780 | if ($x & 1) { $vec = 0x2c; $INTPEND &= ~1; |
831 | if ($pend & 1) { $vec = 0x2c; $INTPEND &= ~1; |
781 | } elsif ($x & 2) { $vec = 0x34; $INTPEND &= ~2; |
832 | } elsif ($pend & 2) { $vec = 0x34; $INTPEND &= ~2; |
782 | } elsif ($x & 4) { $vec = 0x3c; $INTPEND &= ~4; |
833 | } elsif ($pend & 4) { $vec = 0x3c; $INTPEND &= ~4; |
783 | # } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # for some reason, this breaks vt102 |
834 | # } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # the vt102 firmware doesn't like combined interrupts |
784 | } elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102 |
835 | } elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102 |
785 | } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; |
836 | } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; |
786 | } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; |
837 | } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; |
787 | } else { |
838 | } else { |
788 | die; |
839 | die; |
… | |
… | |
793 | $PC = $vec; |
844 | $PC = $vec; |
794 | |
845 | |
795 | $IFF = 0; |
846 | $IFF = 0; |
796 | } |
847 | } |
797 | } |
848 | } |
|
|
849 | |
|
|
850 | ############################################################################# |
|
|
851 | # roms in the data section + one newline |
|
|
852 | # |
|
|
853 | # vt100 @ 0x0000+0x0800 23-032E2 |
|
|
854 | # vt100 @ 0x0800+0x0800 23-061E2 |
|
|
855 | # vt100 @ 0x1000+0x0800 23-033E2 |
|
|
856 | # vt100 @ 0x1800+0x0800 23-034E2 |
|
|
857 | # |
|
|
858 | # vt102 @ 0x0000+0x2000 23-226E4 |
|
|
859 | # vt102 @ 0x8000+0x2000 23-225E4 |
|
|
860 | # |
|
|
861 | # vt131 @ 0xa000+0x0800 23-280E2 |
|
|
862 | # |
798 | |
863 | |
799 | __DATA__ |
864 | __DATA__ |
800 | 1N ; 0 >b/BWog<Gӂ,O $
O [ xI,ڥ # yOt ͤ[ zW>/2!b>>g$>%
!h w-!h >-4!j pO:{ y:! u:x!_yA[>y
>yA[>?y@
:x!yA[P>>O[>>[Î:!ʵyA>>OlyAPÇ!:!S!h ~ ~ : O͓: Ô!20!2!!!yAG~"&=w< w:!/!!A:!Ey2!~1N ! ~eBi<2!͢:P =2S!~6ʘ!!6 |
865 | 1N ; 0 >b/BWog<Gӂ,O $
O [ xI,ڥ # yOt ͤ[ zW>/2!b>>g$>%
!h w-!h >-4!j pO:{ y:! u:x!_yA[>y
>yA[>?y@
:x!yA[P>>O[>>[Î:!ʵyA>>OlyAPÇ!:!S!h ~ ~ : O͓: Ô!20!2!!!yAG~"&=w< w:!/!!A:!Ey2!~1N ! ~eBi<2!͢:P =2S!~6ʘ!!6 |
801 | 2!0* w4ʘ> 2! ~î!N ̓/2!! "R !"" |
866 | 2!0* w4ʘ> 2! ~î!N ̓/2!! "R !"" |
802 | ! ͋!2[ 2v!!"I!>2s >2 B>2y >2!2!&l" >@:X!:!͔>2!b:!K>G:| !p ^!p" :!x:! u> |
867 | ! ͋!2[ 2v!!"I!>2s >2 B>2y >2!2!&l" >@:X!:!͔>2!b:!K>G:| !p ^!p" :!x:! u> |
… | |
… | |
906 | |