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

Comparing vt102/vt102 (file contents):
Revision 1.10 by root, Wed Dec 3 02:07:44 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
28if ($ARGV[0] =~ /^-?-vt100$/) { 27shift, ($VT102 = 0), ($AVO = 0) if $ARGV[0] =~ /^-?-vt100$/;
29 shift; $VT102 = 0; $AVO = 0; 28shift, ($VT102 = 0) if $ARGV[0] =~ /^-?-vt100\+avo$/;
30} 29shift if $ARGV[0] =~ /^-?-vt102$/;
30shift, ($VT131 = 1) if $ARGV[0] =~ /^-?-vt131$/;
31 31
32if ($ARGV[0] =~ /^-?-vt100\+avo$/) { 32# vt100 wps = word processing roms
33 shift; $VT102 = 0; $AVO = 1; 33# vt101 = vt102 - avo, but custom rom? really?
34} 34# vt103 = vt100 + tu58 tape drive
35 35# vt125 = vt100 + gpo graphics processor
36if ($ARGV[0] =~ /^-?-vt102$/) { 36# vt132 = vt100 + avo, stp
37 shift; # default 37# vt180 = vt100 + z80 cp/m
38}
39
40if ($ARGV[0] =~ /^-?-vt131$/) {
41 shift; $VT131 = 1;
42}
43 38
44if ($ARGV[0] =~ /^-/) { 39if ($ARGV[0] =~ /^-/) {
45 die <<EOF; 40 die <<EOF;
46 41
47VT102, A VT100/101/102/131 SIMULATOR 42VT102, A VT100/102/131 SIMULATOR
48 43
49Usage: 44Usage:
50 45
51 $0 [option] [program [args]] 46 $0 [option] [program [args]]
52 47
84} 79}
85 80
86############################################################################# 81#############################################################################
87# ROM/hardware init 82# ROM/hardware init
88 83
84my $PTY; # the pty we allocated, if any
85my $KBD = 1;
86
89my $ROMS = do { 87my $ROMS = do {
90 binmode DATA; 88 binmode DATA;
91 local $/; 89 local $/;
92 <DATA> 90 <DATA>
93}; 91};
94 92
950x6801 == length $ROMS or die "corrupted rom image"; 930x6801 == length $ROMS or die "corrupted rom image";
96
97binmode STDOUT;
98 94
99my @M = (0xff) x 65536; # main memory, = (0xff) x 65536; 95my @M = (0xff) x 65536; # main memory, = (0xff) x 65536;
100 96
101# populate mem with rom contents 97# populate mem with rom contents
102if ($VT102) { 98if ($VT102) {
108} 104}
109 105
110############################################################################# 106#############################################################################
111# 8085 CPU registers and I/O support 107# 8085 CPU registers and I/O support
112 108
113my $PTY; # the pty we allocated, if any
114
115# 8080/8085 registers 109# 8080/8085 registers
116# b, c, d, e, h, l, a 110my ($A, $B, $C, $D, $E, $H, $L); # 8 bit general purpose
117my ($A, $B, $C, $D, $E, $H, $L, $A); 111my ($PC, $SP, $IFF); # program counter, stack pointer, interrupt flag
118my ($PC, $SP, $IFF, $FA, $FZ, $FS, $FP, $FC); 112my ($FA, $FZ, $FS, $FP, $FC); # condition codes (psw)
119 113
120my $RST = 0; # 8080 pending interrupts 114my $RST = 0; # pending interrupts (external interrupt logic)
121my $INTMASK = 7; # 8085 half interrupts 115my $INTMASK = 7; # 8085 half interrupt mask
122my $INTPEND = 0; # 8085 half interrupts 116my $INTPEND = 0; # 8085 half interrupts pending
123 117
124my $x; # dummy temp for instructions 118my $CLK; # rather inexact clock, counts extended basic blocks
125
126my $CLK; # rather inexact clock
127 119
128############################################################################# 120#############################################################################
129# 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
130 122
131# nvram 123# nvram
143 sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase 135 sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase
144 sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read 136 sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read
145 sub { }, # 7 standby 137 sub { }, # 7 standby
146); 138);
147 139
148my @bitidx; 140my @NVR_BITIDX; $NVR_BITIDX[1 << $_] = 9 - $_ for 0..9;
149$bitidx[1 << $_] = 9 - $_ for 0..9;
150 141
151# the nvr1400 state machine. what a monster 142# the nvr1400 state machine. what a monster
152sub nvr() { 143sub nvr() {
153 my $a1 = $bitidx[(~$NVRADDR ) & 0x3ff]; 144 my $a1 = $NVR_BITIDX[(~$NVRADDR ) & 0x3ff];
154 my $a0 = $bitidx[(~$NVRADDR >> 10) & 0x3ff]; 145 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 146
158 $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) 147 $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1)
159} 148}
160 149
161############################################################################# 150#############################################################################
162# I/O ports - output 151# I/O ports - output
163 152
164my $DC11_REVERSE = 0; 153my $DC11_REVERSE = 0; # light background?
165 154
166my $XON = 1; # false if terminal wants us to pause 155my $XON = 1; # false if terminal wants us to pause
167my $PUSARTCMD; 156my $PUSARTCMD;
168 157
158my $KSTATUS; # keyboard status (click + scan flag + leds)
169my @KXMIT; # current scan queue 159my @KXMIT; # current scan queue
170my %KXMIT; # currently pressed keys 160my %KXMIT; # currently pressed keys
171my @KQUEUE; # key event queue 161my @KQUEUE; # key event queue
172my $KXCNT; # count for debouncew 162my $KXCNT; # count for debouncew
173my @PUSARTRECV; 163
174my $KSTATUS; 164my @PUSARTRECV; # serial input (to terminal) queue
175 165
176sub out_00 { # pusartdata 166sub out_00 { # pusartdata
177 # handle xon/xoff, but also pass it through 167 # handle xon/xoff, but also pass it through
178 if ($_[0] == 0x13) { 168 if ($_[0] == 0x13) {
179 $XON = 0; 169 $XON = 0;
186 syswrite $PTY, chr $_[0]; 176 syswrite $PTY, chr $_[0];
187 177
188 $INTPEND |= 1; 178 $INTPEND |= 1;
189} 179}
190 180
191sub out_01 { 181sub out_01 { # pusartcmd
192 $PUSARTCMD = shift; 182 $PUSARTCMD = shift;
193 183
194 $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy 184 $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy
195 $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
196} 186}
197 187
198sub out_02 { } # baudrate generator 188sub out_02 { } # baudrate generator
199 189
200sub out_23 { } # unknown 190sub out_23 { } # vt102 unknown
201sub out_27 { } # unknown 191sub out_27 { } # vt102 unknown
202sub out_2f { } # unknown, connected to in 0f 192sub out_2f { } # vt102 unknown, connected to in 0f
203 193
204sub out_42 { } # brightness 194sub out_42 { } # brightness
205 195
206sub out_62 { 196sub out_62 { # nvr latch register (4 bits)
207 $NVRLATCH = shift; 197 $NVRLATCH = shift;
208} 198}
209 199
210sub out_a2 { 200sub out_a2 { # device control 011
211 my $dc11 = 0x0f & shift; 201 my $dc11 = 0x0f & shift;
212 202
213 $DC11_REVERSE = 1 if $dc11 == 0b1010; 203 $DC11_REVERSE = 1 if $dc11 == 0b1010;
214 $DC11_REVERSE = 0 if $dc11 == 0b1011; 204 $DC11_REVERSE = 0 if $dc11 == 0b1011;
215} 205}
216 206
217sub out_c2 { } # unknown 207sub out_c2 { } # unknown
218sub out_d2 { } # 0..3 == 80c/132c/60hz/50hz 208sub out_d2 { } # device control 012, 0..3 == 80c/132c/60hz/50hz
219 209
220sub out_82 { 210sub out_82 { # keyboard txmit
221 # keyboard 211 # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4 (vt100)
222
223 # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 212 # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 (vt102)
224 # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4
225 $KSTATUS = $_[0]; 213 $KSTATUS = $_[0];
226 214
227 # start new scan unless scan in progress 215 # start new scan unless scan is in progress
228 if (($_[0] & 0x40) && !@KXMIT) { 216 if (($_[0] & 0x40) && !@KXMIT) {
229 # do not reply with keys in locked mode 217 # do not reply with keys in locked mode
230 # or during post (0xff), 218 # or during post (0xff),
231 # mostly to skip init and not fail POST, 219 # mostly to skip init and not fail POST,
232 # and to send startup keys only when terminal is ready 220 # and to send startup keys only when terminal is ready
254} 242}
255 243
256############################################################################# 244#############################################################################
257# I/O ports - input 245# I/O ports - input
258 246
259my $NVRBIT; 247my $NVRBIT; # the current nvr data bit
260my $LBA6; # twice the frequenxy of LBA7 248my $LBA6; # twice the frequenxy of LBA7
261 249
262sub in_00 { # pusart data 250sub in_00 { # pusart data
263 # interrupt not generated here, because infinite 251 # interrupt not generated here, because infinite
264 # speed does not go well with the vt102. 252 # speed does not go well with the vt102.
265 253
269sub in_01 { # pusart status 257sub in_01 { # pusart status
270 # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY 258 # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY
271 0x85 + (@PUSARTRECV && 0x02) 259 0x85 + (@PUSARTRECV && 0x02)
272} 260}
273 261
274sub in_22 { # modem buffer(?) 262sub in_22 { # modem buffer
275 # wild guess: -CTS -SPDI -RI -CD 0 0 0 0 263 # wild guess: -CTS -SPDI -RI -CD 0 0 0 0
276 0x20 264 0x20
277} 265}
278 266
279sub in_0f { 0xff } # vt102 unknown, connected to out 2f 267sub in_0f { 0xff } # vt102 unknown, connected to out 2f
306sub in_1b { 0xff } # vt102 unknown 294sub in_1b { 0xff } # vt102 unknown
307 295
308############################################################################# 296#############################################################################
309# 8085 cpu opcodes and flag handling 297# 8085 cpu opcodes and flag handling
310 298
299my $x; # dummy scratchpad for opcodes
300
311sub sf { # set flags (ZSC - AP not implemented) 301sub sf { # set flags, full version (ZSC - AP not implemented)
312 $FS = $_[0] & 0x080; 302 $FS = $_[0] & 0x080;
313 $FZ = !($_[0] & 0x0ff); 303 $FZ = !($_[0] & 0x0ff);
314 $FC = $_[0] & 0x100; 304 $FC = $_[0] & 0x100;
315 305
316 $_[0] &= 0xff; 306 $_[0] &= 0xff;
317} 307}
318 308
319sub sf8 { # set flags (ZSC - AP not implemented) 309sub sf8 { # set flags, for 8-bit results (ZSC - AP not implemented)
320 $FS = $_[0] & 0x080; 310 $FS = $_[0] & 0x080;
321 $FZ = !($_[0] & 0x0ff); 311 $FZ = !($_[0] & 0x0ff);
322 $FC = 0; 312 $FC = 0;
323} 313}
324 314
325sub sf_nc { # set flags except carry 315sub sf_nc { # set flags, except carry
326 $FS = $_[0] & 0x080; 316 $FS = $_[0] & 0x080;
327 $FZ = ($_[0] & 0x0ff) == 0; 317 $FZ = ($_[0] & 0x0ff) == 0;
328 318
329 $_[0] &= 0xff; 319 $_[0] &= 0xff;
330} 320}
331 321
322# opcode table
332my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff; 323my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff;
333 324
334my @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
335my @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
336 327
337$op[0x00] = ''; 328$op[0x00] = ''; # nop
338 329
339# mov r,r / r,M / M,r 330# mov r,r / r,M / M,r
340for my $s (0..7) { 331for my $s (0..7) {
341 for my $d (0..7) { 332 for my $d (0..7) {
342 $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov 333 $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov
382$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr 373$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr
383$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr 374$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr
384 375
385$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
386$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
387
388$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc 378$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc
389$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar 379$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar
390
391$op[0x2f] = '$A ^= 0xff'; # cma
392 380
393# 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)
394# 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
395sub dad { 383sub dad {
396 $x = $H * 256 + $L + $_[0]; 384 $x = $H * 256 + $L + $_[0];
401 389
402$op[0x09] = 'dad $B * 256 + $C'; # dad 390$op[0x09] = 'dad $B * 256 + $C'; # dad
403$op[0x19] = 'dad $D * 256 + $E'; # dad 391$op[0x19] = 'dad $D * 256 + $E'; # dad
404$op[0x29] = 'dad $H * 256 + $L'; # dad 392$op[0x29] = 'dad $H * 256 + $L'; # dad
405$op[0x39] = 'dad $SP '; # dad 393$op[0x39] = 'dad $SP '; # dad
394
395$op[0x2f] = '$A ^= 0xff'; # cma
406 396
407$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add 397$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add
408$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc 398$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc
409$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub 399$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub
410$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb 400$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb
411$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana 401$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana
412$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra 402$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra
413$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora 403$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora
414$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp 404$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp
415# possible todo: optimize ora a, maybe xra a 405# possible todo: optimize ora a, maybe xra a, possibly ana
416 406
417$op[0xc6] = 'sf $A += IMM8'; # adi 407$op[0xc6] = 'sf $A += IMM8'; # adi
418# ce ADI NYI
419$op[0xd6] = 'sf $A -= IMM8'; # sui 408$op[0xd6] = 'sf $A -= IMM8'; # sui
420# de SBI NYI
421$op[0xe6] = 'sf8 $A &= IMM8'; # ani 409$op[0xe6] = 'sf8 $A &= IMM8'; # ani
422$op[0xee] = 'sf8 $A ^= IMM8'; # xri 410$op[0xee] = 'sf8 $A ^= IMM8'; # xri
423$op[0xf6] = 'sf8 $A |= IMM8'; # ori 411$op[0xf6] = 'sf8 $A |= IMM8'; # ori
424$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
425 415
426$op[0xc5] = 'PUSH $B; PUSH $C'; 416$op[0xc5] = 'PUSH $B; PUSH $C';
427$op[0xd5] = 'PUSH $D; PUSH $E'; 417$op[0xd5] = 'PUSH $D; PUSH $E';
428$op[0xe5] = 'PUSH $H; PUSH $L'; 418$op[0xe5] = 'PUSH $H; PUSH $L';
429$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
431$op[0xc1] = '($C, $B) = (POP, POP)'; # pop 421$op[0xc1] = '($C, $B) = (POP, POP)'; # pop
432$op[0xd1] = '($E, $D) = (POP, POP)'; # pop 422$op[0xd1] = '($E, $D) = (POP, POP)'; # pop
433$op[0xe1] = '($L, $H) = (POP, POP)'; # pop 423$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 424$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw
435 425
436$op[0xc2 + $_ * 8] = 'BRA IMM16 ' . $cc[$_] for 0..7; # jcc 426$op[0xc2 + $_ * 8] = 'BRA IMM16 if ' . $cc[$_] for 0..7; # jcc
437$op[0xc3] = 'JMP IMM16'; # jmp 427$op[0xc3] = 'JMP IMM16'; # jmp
438 428
439$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
440$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call 430$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call
441 431
442$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
443$op[0xc9] = 'JMP POP + POP * 256'; # ret 433$op[0xc9] = 'JMP POP + POP * 256'; # ret
444 434
445$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst 435$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst
446 436
447$op[0xe9] = 'JMP $H * 256 + $L'; # pchl 437$op[0xe9] = 'JMP $H * 256 + $L'; # pchl
448# f9 SPHL NYI 438# f9 SPHL NYI, apparently unused
449 439
450$op[0x37] = '$FC = 1 '; # stc 440$op[0x37] = '$FC = 1 '; # stc
451$op[0x3f] = '$FC = !$FC'; # cmc 441$op[0x3f] = '$FC = !$FC'; # cmc
452 442
453$op[0xd3] = 'OUT'; # out 443$op[0xd3] = 'OUT'; # out
454$op[0xdb] = 'IN'; # in 444$op[0xdb] = 'IN'; # in
455 445
456$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg 446$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg
457 447
458# e3 xthl NYI # @ 917b, hl <-> (sp) 448# e3 xthl NYI # @ 917b in e69, hl <-> (sp)
459 449
460$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (incomplete) 450$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (8085, incomplete)
461$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (incomplete) 451$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (8085, incomplete)
462 452
463$op[0xf3] = '$IFF = 0'; # DI 453$op[0xf3] = '$IFF = 0'; # di
464$op[0xfb] = '$IFF = 1'; # EI 454$op[0xfb] = '$IFF = 1'; # ei
465 455
466# yeah, the fucking setup screens actually use daa... 456# yeah, the fucking setup screen actually uses daa...
467$op[0x27] = ' 457$op[0x27] = '
468 my ($h, $l); 458 my ($h, $l);
469 459
470 ($h, $l) = ($A >> 4, $A & 15); 460 ($h, $l) = ($A >> 4, $A & 15);
471 461
479 $A = ($h * 16 + $l) & 0xff; 469 $A = ($h * 16 + $l) & 0xff;
480 } 470 }
481'; # daa, almost certainly borked, also, acarry not set by sf 471'; # daa, almost certainly borked, also, acarry not set by sf
482 472
483############################################################################# 473#############################################################################
484# print cpu status for debugging purposes 474# debug
485 475
486# print cpu status, for debugging 476# print cpu status, for debugging
487sub status { 477sub status {
488 my $PC = shift || $PC; 478 my $PC = shift || $PC;
489 479
499} 489}
500 490
501############################################################################# 491#############################################################################
502# video emulation 492# video emulation
503 493
504my @CHARMAP = ( 494binmode STDOUT;
495
496my @CHARMAP = ( # acschars / chars 0..31
505 " " , "\x{29eb}", "\x{2592}", "\x{2409}", 497 " " , "\x{29eb}", "\x{2592}", "\x{2409}",
506 "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", 498 "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}",
507 "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", 499 "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}",
508 "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", 500 "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}",
509 "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", 501 "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}",
630$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift 622$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift
631 623
632my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; 624my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP;
633$KEYMATCH = qr{^($KEYMATCH)}s; 625$KEYMATCH = qr{^($KEYMATCH)}s;
634 626
635my %KMOD; 627my %KMOD; # currently pressed modifier keys
636 628
637sub key { 629sub key {
638 my ($key) = @_; 630 my ($key) = @_;
639 631
640 push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up 632 push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up
695my $slave = $PTY->slave; 687my $slave = $PTY->slave;
696 688
697$PTY->set_winsize (24, 80); 689$PTY->set_winsize (24, 80);
698 690
699unless (fork) { 691unless (fork) {
692 $ENV{LC_ALL} = "C";
700 $ENV{TERM} = $VT102 ? "vt102" : "vt100"; 693 $ENV{TERM} = $VT102 ? "vt102" : "vt100";
701 694
702 close $PTY; 695 close $PTY;
703 696
704 open STDIN , "<&", $slave; 697 open STDIN , "<&", $slave;
717$PTY->close_slave; 710$PTY->close_slave;
718 711
719############################################################################# 712#############################################################################
720# the actual hardware simulator 713# the actual hardware simulator
721 714
722my @ICACHE; # compiled instruction cache 715my @ICACHE; # compiled instruction/basic block cache
723 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.
724while () { 725while () {
725 # execute extended basic blocks 726 # execute extended basic blocks
726 $PC = ($ICACHE[$PC] ||= do { 727 $PC = ($ICACHE[$PC] ||= do {
727 my $pc = $PC; 728 my $pc = $PC;
728 729
742 743
743 s/\bPC\b/$pc/ge; # PC at end of insn 744 s/\bPC\b/$pc/ge; # PC at end of insn
744 s/\bBRA\b/return/g; # conditional jump 745 s/\bBRA\b/return/g; # conditional jump
745 s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump 746 s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump
746 747
747 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
748 s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; 749 s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; # out likewise
749 } 750 }
750 751
751 $insn .= "$op;\n"; 752 $insn .= "$op;\n";
752 } 753 }
753 754
754 755
755 $insn .= "$pc"; 756 $insn .= $pc;
756 $insn =~ s/\x00.*$//s; 757 $insn =~ s/\x00.*$//s;
757 758
758 eval "use integer; sub { $insn }" or die "$insn: $@" 759 eval "use integer; sub { $insn }" or die "$insn: $@"
759 })->(); 760 })->();
760 761
761 ++$CLK; 762 ++$CLK;
762 763
763 # things we do from time too time only 764 # things we do from time to time only
764 unless ($CLK & 0xf) { 765 unless ($CLK & 0xf) {
765 # do I/O 766 # do I/O
766 767
767 unless ($CLK & 0xfff) { 768 unless ($CLK & 0xfff) {
769 if (select $x = $RIN, undef, undef, $POWERSAVE < 100 ? 0 : 0.2) {
768 770
769 # pty/serial I/O 771 # pty/serial I/O
770 unless ((@PUSARTRECV >= 128) || @KQUEUE || !$PTY) { 772 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; 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.
775 push @PUSARTRECV, unpack "C*", $buf; 777 push @PUSARTRECV, grep { ($_ & 0x7f) != 0x13 } unpack "C*", $buf;
776 } 778 }
777 }
778 779
779 # keyboard input 780 # keyboard input
780 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.
781 while (select my $rin = "\x01", undef, undef, 0) { 784 while (select my $rin = "\x01", undef, undef, 0) {
782 sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF 785 sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF
783 or last; 786 or last;
787 }
788
789 stdin_parse if length $STDIN_BUF;
784 } 790 }
785 791
786 stdin_parse if length $STDIN_BUF; 792 $POWERSAVE = 0; # activity
793 } elsif (@PUSARTRECV || @KQUEUE) {
794 $POWERSAVE = 0;
795 } else {
796 ++$POWERSAVE;
787 } 797 }
788 } 798 }
789 799
790 # kick off various interrupts 800 # 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) 801 $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 802 #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy
794 803
795 # kick off vertical retrace form time to time 804 # kick off vertical retrace interrupt from time to time
796 unless ($CLK & 0x1ff) { 805 unless ($CLK & 0x1ff) {
797 $RST |= 4; # vertical retrace 806 $RST |= 4; # vertical retrace
798 } 807 }
799 808
800 # handle video hardware 809 # handle video hardware
801 unless ($CLK & 0x3fff) { 810 unless ($CLK & 0x3fff) {
802 display; 811 display;
803 } 812 }
804 } 813 }
805 814
806 # 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.
807 if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) { 818 if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) {
808 # rst 1 kbd data available 819 # rst 1 kbd data available
809 # rst 2 pusart xmit+recv flag 820 # rst 2 pusart xmit+recv flag
810 # rst 4 vertical retrace 821 # rst 4 vertical retrace
811 # 5.5 vt125 mb7 trans ready (serial send?) 822 # 5.5 vt125 mb7 trans ready (serial send?)
812 # 6.5 vt125 mb7 read ready (something modem?) 823 # 6.5 vt125 mb7 read ready (something modem?)
813 # 7.5 vt125 mb7 vblank h(?) 824 # 7.5 vt125 mb7 vblank h(?)
814 # trap vt125 mbi init h(?) 825 # trap vt125 mbi init h(?)
815 my $vec; 826 my $vec;
816 827
817 $x = $INTPEND & ~$INTMASK; 828 my $pend = $INTPEND & ~$INTMASK;
818 829
819 if ($x & 1) { $vec = 0x2c; $INTPEND &= ~1; 830 if ($pend & 1) { $vec = 0x2c; $INTPEND &= ~1;
820 } elsif ($x & 2) { $vec = 0x34; $INTPEND &= ~2; 831 } elsif ($pend & 2) { $vec = 0x34; $INTPEND &= ~2;
821 } elsif ($x & 4) { $vec = 0x3c; $INTPEND &= ~4; 832 } elsif ($pend & 4) { $vec = 0x3c; $INTPEND &= ~4;
822# } 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
823 } 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
824 } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; 835 } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2;
825 } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; 836 } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4;
826 } else { 837 } else {
827 die; 838 die;
828 } 839 }
829 840
841 # jump to the interrupt vector
830 $M[--$SP] = $PC >> 8; 842 $M[--$SP] = $PC >> 8;
831 $M[--$SP] = $PC & 0xff; 843 $M[--$SP] = $PC & 0xff;
832 $PC = $vec; 844 $PC = $vec;
833 845
834 $IFF = 0; 846 $IFF = 0;
841# vt100 @ 0x0000+0x0800 23-032E2 853# vt100 @ 0x0000+0x0800 23-032E2
842# vt100 @ 0x0800+0x0800 23-061E2 854# vt100 @ 0x0800+0x0800 23-061E2
843# vt100 @ 0x1000+0x0800 23-033E2 855# vt100 @ 0x1000+0x0800 23-033E2
844# vt100 @ 0x1800+0x0800 23-034E2 856# vt100 @ 0x1800+0x0800 23-034E2
845# 857#
846# vt102 @ 0x0000+0x8000 23-226E4 858# vt102 @ 0x0000+0x2000 23-226E4
847# vt102 @ 0x8000+0x8000 23-225E4 859# vt102 @ 0x8000+0x2000 23-225E4
848# 860#
849# vt131 @ 0xa000+0x0800 23-280E2 861# vt131 @ 0xa000+0x0800 23-280E2
850# 862#
851 863
852__DATA__ 864__DATA__

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines