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