ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/vt102/vt102
(Generate patch)

Comparing vt102/vt102 (file contents):
Revision 1.14 by root, Wed Dec 3 02:13:41 2014 UTC vs.
Revision 1.25 by root, Thu Dec 4 04:36:11 2014 UTC

21#use common::sense; 21#use common::sense;
22 22
23my $VT102 = 1; 23my $VT102 = 1;
24my $VT131 = 0; 24my $VT131 = 0;
25my $AVO = 1; 25my $AVO = 1;
26my $KBD = 1;
27 26
28shift, ($VT102 = 0), ($AVO = 0) if $ARGV[0] =~ /^-?-vt100$/; 27shift, ($VT102 = 0), ($AVO = 0) if $ARGV[0] =~ /^-?-vt100$/;
29shift, ($VT102 = 0) if $ARGV[0] =~ /^-?-vt100\+avo$/; 28shift, ($VT102 = 0) if $ARGV[0] =~ /^-?-vt100\+avo$/;
30shift if $ARGV[0] =~ /^-?-vt102$/; 29shift if $ARGV[0] =~ /^-?-vt102$/;
31shift, ($VT131 = 1) if $ARGV[0] =~ /^-?-vt131$/; 30shift, ($VT131 = 1) if $ARGV[0] =~ /^-?-vt131$/;
32 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
33if ($ARGV[0] =~ /^-/) { 39if ($ARGV[0] =~ /^-/) {
34 die <<EOF; 40 die <<EOF;
35 41
36VT102, A VT100/101/102/131 SIMULATOR 42VT102, A VT100/102/131 SIMULATOR
37 43
38Usage: 44Usage:
39 45
40 $0 [option] [program [args]] 46 $0 [option] [program [args]]
41 47
72EOF 78EOF
73} 79}
74 80
75############################################################################# 81#############################################################################
76# ROM/hardware init 82# ROM/hardware init
83
84my $PTY; # the pty we allocated, if any
85my $KBD = 1;
77 86
78my $ROMS = do { 87my $ROMS = do {
79 binmode DATA; 88 binmode DATA;
80 local $/; 89 local $/;
81 <DATA> 90 <DATA>
95} 104}
96 105
97############################################################################# 106#############################################################################
98# 8085 CPU registers and I/O support 107# 8085 CPU registers and I/O support
99 108
100my $PTY; # the pty we allocated, if any
101
102# 8080/8085 registers 109# 8080/8085 registers
103# b, c, d, e, h, l, a 110my ($A, $B, $C, $D, $E, $H, $L); # 8 bit general purpose
104my ($A, $B, $C, $D, $E, $H, $L, $A); 111my ($PC, $SP, $IFF); # program counter, stack pointer, interrupt flag
105my ($PC, $SP, $IFF, $FA, $FZ, $FS, $FP, $FC); 112my ($FA, $FZ, $FS, $FP, $FC); # condition codes (psw)
106 113
107my $RST = 0; # 8080 pending interrupts 114my $RST = 0; # pending interrupts (external interrupt logic)
108my $INTMASK = 7; # 8085 half interrupts 115my $INTMASK = 7; # 8085 half interrupt mask
109my $INTPEND = 0; # 8085 half interrupts 116my $INTPEND = 0; # 8085 half interrupts pending
110 117
111my $x; # dummy temp for instructions 118my $CLK; # rather inexact clock, counts extended basic blocks
112
113my $CLK; # rather inexact clock
114 119
115############################################################################# 120#############################################################################
116# the dreaded NVR1400 chip. not needed to get it going, but provided anyway 121# the dreaded NVR1400 chip. not needed to get it going, but provided anyway
117 122
118# nvram 123# nvram
130 sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase 135 sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase
131 sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read 136 sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read
132 sub { }, # 7 standby 137 sub { }, # 7 standby
133); 138);
134 139
135my @bitidx; 140my @NVR_BITIDX; $NVR_BITIDX[1 << $_] = 9 - $_ for 0..9;
136$bitidx[1 << $_] = 9 - $_ for 0..9;
137 141
138# the nvr1400 state machine. what a monster 142# the nvr1400 state machine. what a monster
139sub nvr() { 143sub nvr() {
140 my $a1 = $bitidx[(~$NVRADDR ) & 0x3ff]; 144 my $a1 = $NVR_BITIDX[(~$NVRADDR ) & 0x3ff];
141 my $a0 = $bitidx[(~$NVRADDR >> 10) & 0x3ff]; 145 my $a0 = $NVR_BITIDX[(~$NVRADDR >> 10) & 0x3ff];
142
143# printf "NVR %02x A %020b %d %d D %02x\n", $NVRLATCH, $NVRADDR & 0xfffff, $a1, $a0, $NVRDATA;
144 146
145 $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) 147 $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1)
146} 148}
147 149
148############################################################################# 150#############################################################################
149# I/O ports - output 151# I/O ports - output
150 152
151my $DC11_REVERSE = 0; 153my $DC11_REVERSE = 0; # light background?
152 154
153my $XON = 1; # false if terminal wants us to pause 155my $XON = 1; # false if terminal wants us to pause
154my $PUSARTCMD; 156my $PUSARTCMD;
155 157
158my $KSTATUS; # keyboard status (click + scan flag + leds)
156my @KXMIT; # current scan queue 159my @KXMIT; # current scan queue
157my %KXMIT; # currently pressed keys 160my %KXMIT; # currently pressed keys
158my @KQUEUE; # key event queue 161my @KQUEUE; # key event queue
159my $KXCNT; # count for debouncew 162my $KXCNT; # count for debouncew
160my @PUSARTRECV; 163
161my $KSTATUS; 164my @PUSARTRECV; # serial input (to terminal) queue
162 165
163sub out_00 { # pusartdata 166sub out_00 { # pusartdata
164 # handle xon/xoff, but also pass it through 167 # handle xon/xoff, but also pass it through
165 if ($_[0] == 0x13) { 168 if ($_[0] == 0x13) {
166 $XON = 0; 169 $XON = 0;
173 syswrite $PTY, chr $_[0]; 176 syswrite $PTY, chr $_[0];
174 177
175 $INTPEND |= 1; 178 $INTPEND |= 1;
176} 179}
177 180
178sub out_01 { 181sub out_01 { # pusartcmd
179 $PUSARTCMD = shift; 182 $PUSARTCMD = shift;
180 183
181 $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy 184 $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy
182 $INTPEND |= 2 if $PUSARTCMD & 0x04 && !@PUSARTRECV; # VT102, 6.5 rxrdy, needed for some reason 185 $INTPEND |= 2 if $PUSARTCMD & 0x04 && !@PUSARTRECV; # VT102, 6.5 rxrdy, needed for some reason
183} 186}
184 187
185sub out_02 { } # baudrate generator 188sub out_02 { } # baudrate generator
186 189
187sub out_23 { } # unknown 190sub out_23 { } # vt102 unknown
188sub out_27 { } # unknown 191sub out_27 { } # vt102 unknown
189sub out_2f { } # unknown, connected to in 0f 192sub out_2f { } # vt102 unknown, connected to in 0f
190 193
191sub out_42 { } # brightness 194sub out_42 { } # brightness
192 195
193sub out_62 { 196sub out_62 { # nvr latch register (4 bits)
194 $NVRLATCH = shift; 197 $NVRLATCH = shift;
195} 198}
196 199
197sub out_a2 { 200sub out_a2 { # device control 011
198 my $dc11 = 0x0f & shift; 201 my $dc11 = 0x0f & shift;
199 202
200 $DC11_REVERSE = 1 if $dc11 == 0b1010; 203 $DC11_REVERSE = 1 if $dc11 == 0b1010;
201 $DC11_REVERSE = 0 if $dc11 == 0b1011; 204 $DC11_REVERSE = 0 if $dc11 == 0b1011;
202} 205}
203 206
204sub out_c2 { } # unknown 207sub out_c2 { } # unknown
205sub out_d2 { } # 0..3 == 80c/132c/60hz/50hz 208sub out_d2 { } # device control 012, 0..3 == 80c/132c/60hz/50hz
206 209
207sub out_82 { 210sub out_82 { # keyboard txmit
208 # keyboard 211 # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4 (vt100)
209
210 # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 212 # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 (vt102)
211 # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4
212 $KSTATUS = $_[0]; 213 $KSTATUS = $_[0];
213 214
214 # start new scan unless scan in progress 215 # start new scan unless scan is in progress
215 if (($_[0] & 0x40) && !@KXMIT) { 216 if (($_[0] & 0x40) && !@KXMIT) {
216 # do not reply with keys in locked mode 217 # do not reply with keys in locked mode
217 # or during post (0xff), 218 # or during post (0xff),
218 # mostly to skip init and not fail POST, 219 # mostly to skip init and not fail POST,
219 # and to send startup keys only when terminal is ready 220 # and to send startup keys only when terminal is ready
241} 242}
242 243
243############################################################################# 244#############################################################################
244# I/O ports - input 245# I/O ports - input
245 246
246my $NVRBIT; 247my $NVRBIT; # the current nvr data bit
247my $LBA6; # twice the frequenxy of LBA7 248my $LBA6; # twice the frequenxy of LBA7
248 249
249sub in_00 { # pusart data 250sub in_00 { # pusart data
250 # interrupt not generated here, because infinite 251 # interrupt not generated here, because infinite
251 # speed does not go well with the vt102. 252 # speed does not go well with the vt102.
252 253
256sub in_01 { # pusart status 257sub in_01 { # pusart status
257 # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY 258 # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY
258 0x85 + (@PUSARTRECV && 0x02) 259 0x85 + (@PUSARTRECV && 0x02)
259} 260}
260 261
261sub in_22 { # modem buffer(?) 262sub in_22 { # modem buffer
262 # wild guess: -CTS -SPDI -RI -CD 0 0 0 0 263 # wild guess: -CTS -SPDI -RI -CD 0 0 0 0
263 0x20 264 0x20
264} 265}
265 266
266sub in_0f { 0xff } # vt102 unknown, connected to out 2f 267sub in_0f { 0xff } # vt102 unknown, connected to out 2f
293sub in_1b { 0xff } # vt102 unknown 294sub in_1b { 0xff } # vt102 unknown
294 295
295############################################################################# 296#############################################################################
296# 8085 cpu opcodes and flag handling 297# 8085 cpu opcodes and flag handling
297 298
299my $x; # dummy scratchpad for opcodes
300
298sub sf { # set flags (ZSC - AP not implemented) 301sub sf { # set flags, full version (ZSC - AP not implemented)
299 $FS = $_[0] & 0x080; 302 $FS = $_[0] & 0x080;
300 $FZ = !($_[0] & 0x0ff); 303 $FZ = !($_[0] & 0x0ff);
301 $FC = $_[0] & 0x100; 304 $FC = $_[0] & 0x100;
302 305
303 $_[0] &= 0xff; 306 $_[0] &= 0xff;
304} 307}
305 308
306sub sf8 { # set flags (ZSC - AP not implemented) 309sub sf8 { # set flags, for 8-bit results (ZSC - AP not implemented)
307 $FS = $_[0] & 0x080; 310 $FS = $_[0] & 0x080;
308 $FZ = !($_[0] & 0x0ff); 311 $FZ = !($_[0] & 0x0ff);
309 $FC = 0; 312 $FC = 0;
310} 313}
311 314
312sub sf_nc { # set flags except carry 315sub sf_nc { # set flags, except carry
313 $FS = $_[0] & 0x080; 316 $FS = $_[0] & 0x080;
314 $FZ = ($_[0] & 0x0ff) == 0; 317 $FZ = ($_[0] & 0x0ff) == 0;
315 318
316 $_[0] &= 0xff; 319 $_[0] &= 0xff;
317} 320}
318 321
322# opcode table
319my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff; 323my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff;
320 324
321my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); 325my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); # r/m encoding
322my @cc = ('if !$FZ', 'if $FZ', 'if !$FC', 'if $FC', ';die', ';die', 'if !$FS', 'if $FS'); # die == unimplemented $FP parity 326my @cc = ('!$FZ', '$FZ', '!$FC', '$FC', 'die;', 'die;', '!$FS', '$FS'); # cc encoding. die == unimplemented $FP parity
323 327
324$op[0x00] = ''; 328$op[0x00] = ''; # nop
325 329
326# mov r,r / r,M / M,r 330# mov r,r / r,M / M,r
327for my $s (0..7) { 331for my $s (0..7) {
328 for my $d (0..7) { 332 for my $d (0..7) {
329 $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov 333 $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov
369$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr 373$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr
370$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr 374$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr
371 375
372$op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc 376$op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc
373$op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral 377$op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral
374
375$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc 378$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc
376$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar 379$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar
377
378$op[0x2f] = '$A ^= 0xff'; # cma
379 380
380# getting this insn wrong (its the only 16 bit insn to modify flags) 381# getting this insn wrong (its the only 16 bit insn to modify flags)
381# wasted three of my best days with mindless vt102 rom reverse engineering 382# wasted three of my best days with mindless vt102 rom reverse engineering
382sub dad { 383sub dad {
383 $x = $H * 256 + $L + $_[0]; 384 $x = $H * 256 + $L + $_[0];
388 389
389$op[0x09] = 'dad $B * 256 + $C'; # dad 390$op[0x09] = 'dad $B * 256 + $C'; # dad
390$op[0x19] = 'dad $D * 256 + $E'; # dad 391$op[0x19] = 'dad $D * 256 + $E'; # dad
391$op[0x29] = 'dad $H * 256 + $L'; # dad 392$op[0x29] = 'dad $H * 256 + $L'; # dad
392$op[0x39] = 'dad $SP '; # dad 393$op[0x39] = 'dad $SP '; # dad
394
395$op[0x2f] = '$A ^= 0xff'; # cma
393 396
394$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add 397$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add
395$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc 398$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc
396$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub 399$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub
397$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb 400$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb
398$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana 401$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana
399$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra 402$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra
400$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora 403$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora
401$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp 404$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp
402# possible todo: optimize ora a, maybe xra a 405# possible todo: optimize ora a, maybe xra a, possibly ana
403 406
404$op[0xc6] = 'sf $A += IMM8'; # adi 407$op[0xc6] = 'sf $A += IMM8'; # adi
405# ce ADI NYI
406$op[0xd6] = 'sf $A -= IMM8'; # sui 408$op[0xd6] = 'sf $A -= IMM8'; # sui
407# de SBI NYI
408$op[0xe6] = 'sf8 $A &= IMM8'; # ani 409$op[0xe6] = 'sf8 $A &= IMM8'; # ani
409$op[0xee] = 'sf8 $A ^= IMM8'; # xri 410$op[0xee] = 'sf8 $A ^= IMM8'; # xri
410$op[0xf6] = 'sf8 $A |= IMM8'; # ori 411$op[0xf6] = 'sf8 $A |= IMM8'; # ori
411$op[0xfe] = 'sf $A - IMM8'; # cpi 412$op[0xfe] = 'sf $A - IMM8'; # cpi
413# ce ACI NYI, apparently unused
414# de SBI NYI, apparently unused
412 415
413$op[0xc5] = 'PUSH $B; PUSH $C'; 416$op[0xc5] = 'PUSH $B; PUSH $C';
414$op[0xd5] = 'PUSH $D; PUSH $E'; 417$op[0xd5] = 'PUSH $D; PUSH $E';
415$op[0xe5] = 'PUSH $H; PUSH $L'; 418$op[0xe5] = 'PUSH $H; PUSH $L';
416$op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw 419$op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw
418$op[0xc1] = '($C, $B) = (POP, POP)'; # pop 421$op[0xc1] = '($C, $B) = (POP, POP)'; # pop
419$op[0xd1] = '($E, $D) = (POP, POP)'; # pop 422$op[0xd1] = '($E, $D) = (POP, POP)'; # pop
420$op[0xe1] = '($L, $H) = (POP, POP)'; # pop 423$op[0xe1] = '($L, $H) = (POP, POP)'; # pop
421$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw 424$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw
422 425
423$op[0xc2 + $_ * 8] = 'BRA IMM16 ' . $cc[$_] for 0..7; # jcc 426$op[0xc2 + $_ * 8] = 'BRA IMM16 if ' . $cc[$_] for 0..7; # jcc
424$op[0xc3] = 'JMP IMM16'; # jmp 427$op[0xc3] = 'JMP IMM16'; # jmp
425 428
426$op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) ' . $cc[$_] for 0..7; # ccc 429$op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) if ' . $cc[$_] for 0..7; # ccc
427$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call 430$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call
428 431
429$op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 ' . $cc[$_] for 0..7; # rcc 432$op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 if ' . $cc[$_] for 0..7; # rcc
430$op[0xc9] = 'JMP POP + POP * 256'; # ret 433$op[0xc9] = 'JMP POP + POP * 256'; # ret
431 434
432$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst 435$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst
433 436
434$op[0xe9] = 'JMP $H * 256 + $L'; # pchl 437$op[0xe9] = 'JMP $H * 256 + $L'; # pchl
435# f9 SPHL NYI 438# f9 SPHL NYI, apparently unused
436 439
437$op[0x37] = '$FC = 1 '; # stc 440$op[0x37] = '$FC = 1 '; # stc
438$op[0x3f] = '$FC = !$FC'; # cmc 441$op[0x3f] = '$FC = !$FC'; # cmc
439 442
440$op[0xd3] = 'OUT'; # out 443$op[0xd3] = 'OUT'; # out
441$op[0xdb] = 'IN'; # in 444$op[0xdb] = 'IN'; # in
442 445
443$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg 446$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg
444 447
445# e3 xthl NYI # @ 917b, hl <-> (sp) 448# e3 xthl NYI # @ 917b in e69, hl <-> (sp)
446 449
447$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (incomplete) 450$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (8085, incomplete)
448$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (incomplete) 451$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (8085, incomplete)
449 452
450$op[0xf3] = '$IFF = 0'; # DI 453$op[0xf3] = '$IFF = 0'; # di
451$op[0xfb] = '$IFF = 1'; # EI 454$op[0xfb] = '$IFF = 1'; # ei
452 455
453# yeah, the fucking setup screens actually use daa... 456# yeah, the fucking setup screen actually uses daa...
454$op[0x27] = ' 457$op[0x27] = '
455 my ($h, $l); 458 my ($h, $l);
456 459
457 ($h, $l) = ($A >> 4, $A & 15); 460 ($h, $l) = ($A >> 4, $A & 15);
458 461
466 $A = ($h * 16 + $l) & 0xff; 469 $A = ($h * 16 + $l) & 0xff;
467 } 470 }
468'; # daa, almost certainly borked, also, acarry not set by sf 471'; # daa, almost certainly borked, also, acarry not set by sf
469 472
470############################################################################# 473#############################################################################
471# print cpu status for debugging purposes 474# debug
472 475
473# print cpu status, for debugging 476# print cpu status, for debugging
474sub status { 477sub status {
475 my $PC = shift || $PC; 478 my $PC = shift || $PC;
476 479
488############################################################################# 491#############################################################################
489# video emulation 492# video emulation
490 493
491binmode STDOUT; 494binmode STDOUT;
492 495
493my @CHARMAP = ( 496my @CHARMAP = ( # acschars / chars 0..31
494 " " , "\x{29eb}", "\x{2592}", "\x{2409}", 497 " " , "\x{29eb}", "\x{2592}", "\x{2409}",
495 "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", 498 "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}",
496 "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", 499 "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}",
497 "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", 500 "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}",
498 "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", 501 "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}",
619$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift 622$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift
620 623
621my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; 624my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP;
622$KEYMATCH = qr{^($KEYMATCH)}s; 625$KEYMATCH = qr{^($KEYMATCH)}s;
623 626
624my %KMOD; 627my %KMOD; # currently pressed modifier keys
625 628
626sub key { 629sub key {
627 my ($key) = @_; 630 my ($key) = @_;
628 631
629 push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up 632 push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up
684my $slave = $PTY->slave; 687my $slave = $PTY->slave;
685 688
686$PTY->set_winsize (24, 80); 689$PTY->set_winsize (24, 80);
687 690
688unless (fork) { 691unless (fork) {
692 $ENV{LC_ALL} = "C";
689 $ENV{TERM} = $VT102 ? "vt102" : "vt100"; 693 $ENV{TERM} = $VT102 ? "vt102" : "vt100";
690 694
691 close $PTY; 695 close $PTY;
692 696
693 open STDIN , "<&", $slave; 697 open STDIN , "<&", $slave;
706$PTY->close_slave; 710$PTY->close_slave;
707 711
708############################################################################# 712#############################################################################
709# the actual hardware simulator 713# the actual hardware simulator
710 714
711my @ICACHE; # compiled instruction cache 715my @ICACHE; # compiled instruction/basic block cache
712 716
717my $POWERSAVE; # powersave counter
718
719my $RIN; # libev for the less well-off
720
721(vec $RIN, 0, 1) = 1 if $KBD;
722(vec $RIN, fileno $PTY, 1) = 1 if $PTY;
723
724# the cpu.
713while () { 725while () {
714 # execute extended basic blocks 726 # execute extended basic blocks
715 $PC = ($ICACHE[$PC] ||= do { 727 $PC = ($ICACHE[$PC] ||= do {
716 my $pc = $PC; 728 my $pc = $PC;
717 729
731 743
732 s/\bPC\b/$pc/ge; # PC at end of insn 744 s/\bPC\b/$pc/ge; # PC at end of insn
733 s/\bBRA\b/return/g; # conditional jump 745 s/\bBRA\b/return/g; # conditional jump
734 s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump 746 s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump
735 747
736 s/\bIN\b/ sprintf "\$A = in_%02x", $M[$pc++]/xge; 748 s/\bIN\b/ sprintf "\$A = in_%02x", $M[$pc++]/xge; # in insns call in_HEX
737 s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; 749 s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; # out likewise
738 } 750 }
739 751
740 $insn .= "$op;\n"; 752 $insn .= "$op;\n";
741 } 753 }
742 754
747 eval "use integer; sub { $insn }" or die "$insn: $@" 759 eval "use integer; sub { $insn }" or die "$insn: $@"
748 })->(); 760 })->();
749 761
750 ++$CLK; 762 ++$CLK;
751 763
752 # things we do from time too time only 764 # things we do from time to time only
753 unless ($CLK & 0xf) { 765 unless ($CLK & 0xf) {
754 # do I/O 766 # do I/O
755 767
756 unless ($CLK & 0xfff) { 768 unless ($CLK & 0xfff) {
769 if (select $x = $RIN, undef, undef, $POWERSAVE < 100 ? 0 : 0.2) {
757 770
758 # pty/serial I/O 771 # pty/serial I/O
759 unless ((@PUSARTRECV >= 128) || @KQUEUE || !$PTY) { 772 if ($PTY && (vec $x, fileno $PTY, 1) && (@PUSARTRECV < 128) && !@KQUEUE) {
760 my $rin = ""; (vec $rin, fileno $PTY, 1) = 1;
761
762 if (select $rin, undef, undef, 0) {
763 sysread $PTY, my $buf, 256; 773 sysread $PTY, my $buf, 256;
774
775 # linux don't do cs7 and/or parity anymore, so we need to filter
776 # out xoff characters to avoid freezes.
764 push @PUSARTRECV, unpack "C*", $buf; 777 push @PUSARTRECV, grep { ($_ & 0x7f) != 0x13 } unpack "C*", $buf;
765 } 778 }
766 }
767 779
768 # keyboard input 780 # keyboard input
769 if ($KBD) { 781 if ($KBD && (vec $x, 0, 1)) {
782 # to avoid non-blocking mode on stdin (and stty min 0), we
783 # just read byte-by-byte after a select says there is data.
770 while (select my $rin = "\x01", undef, undef, 0) { 784 while (select my $rin = "\x01", undef, undef, 0) {
771 sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF 785 sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF
772 or last; 786 or last;
787 }
788
789 stdin_parse if length $STDIN_BUF;
773 } 790 }
774 791
775 stdin_parse if length $STDIN_BUF; 792 $POWERSAVE = 0; # activity
793 } elsif (@PUSARTRECV || @KQUEUE) {
794 $POWERSAVE = 0;
795 } else {
796 ++$POWERSAVE;
776 } 797 }
777 } 798 }
778 799
779 # kick off various interrupts 800 # kick off serial input interrupt quite often
780
781 $RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though) 801 $RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though)
782 #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy 802 #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy
783 803
784 # kick off vertical retrace form time to time 804 # kick off vertical retrace interrupt from time to time
785 unless ($CLK & 0x1ff) { 805 unless ($CLK & 0x1ff) {
786 $RST |= 4; # vertical retrace 806 $RST |= 4; # vertical retrace
787 } 807 }
788 808
789 # handle video hardware 809 # handle video hardware
790 unless ($CLK & 0x3fff) { 810 unless ($CLK & 0x3fff) {
791 display; 811 display;
792 } 812 }
793 } 813 }
794 814
795 # the interrupt logic 815 # the interrupt logic - we only interrupt after basic blocks
816 # which, as a side effect, ensures that we don't interrupt
817 # "ei; ret" sequences and thus reduce the risk of stack overflows.
796 if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) { 818 if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) {
797 # rst 1 kbd data available 819 # rst 1 kbd data available
798 # rst 2 pusart xmit+recv flag 820 # rst 2 pusart xmit+recv flag
799 # rst 4 vertical retrace 821 # rst 4 vertical retrace
800 # 5.5 vt125 mb7 trans ready (serial send?) 822 # 5.5 vt125 mb7 trans ready (serial send?)
801 # 6.5 vt125 mb7 read ready (something modem?) 823 # 6.5 vt125 mb7 read ready (something modem?)
802 # 7.5 vt125 mb7 vblank h(?) 824 # 7.5 vt125 mb7 vblank h(?)
803 # trap vt125 mbi init h(?) 825 # trap vt125 mbi init h(?)
804 my $vec; 826 my $vec;
805 827
806 $x = $INTPEND & ~$INTMASK; 828 my $pend = $INTPEND & ~$INTMASK;
807 829
808 if ($x & 1) { $vec = 0x2c; $INTPEND &= ~1; 830 if ($pend & 1) { $vec = 0x2c; $INTPEND &= ~1;
809 } elsif ($x & 2) { $vec = 0x34; $INTPEND &= ~2; 831 } elsif ($pend & 2) { $vec = 0x34; $INTPEND &= ~2;
810 } elsif ($x & 4) { $vec = 0x3c; $INTPEND &= ~4; 832 } elsif ($pend & 4) { $vec = 0x3c; $INTPEND &= ~4;
811# } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # the vt102 firmware doesn't like combined interrupts 833# } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # the vt102 firmware doesn't like combined interrupts
812 } elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102 834 } elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102
813 } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; 835 } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2;
814 } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; 836 } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4;
815 } else { 837 } else {
816 die; 838 die;
817 } 839 }
818 840
841 # jump to the interrupt vector
819 $M[--$SP] = $PC >> 8; 842 $M[--$SP] = $PC >> 8;
820 $M[--$SP] = $PC & 0xff; 843 $M[--$SP] = $PC & 0xff;
821 $PC = $vec; 844 $PC = $vec;
822 845
823 $IFF = 0; 846 $IFF = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines