'http://clrhome.org/table/ and move mouse over the instruction for details ' http://www.ticalc.org/archives/files/fileinfo/435/43539.html for opcodes ' see end of program for a C example Imports System.IO Imports System.Windows.Forms Imports System.IO.Ports Imports Strings = Microsoft.VisualBasic ' so can use things like left( and right( for strings Public Class Form1 ' // Main Z80 registers // Public regA As Long Public regHL As Long Public regB As Long Public regC As Long Public regDE As Long ' // Z80 Flags Public fS As Long Public fZ As Long Public f5 As Long Public fH As Long Public f3 As Long Public fPV As Long Public fN As Long Public fC As Long ' // Flag bit positions Public Const F_C As Long = 1 Public Const F_N As Long = 2 Public Const F_PV As Long = 4 Public Const F_3 As Long = 8 Public Const F_H As Long = 16 Public Const F_5 As Long = 32 Public Const F_Z As Long = 64 Public Const F_S As Long = 128 ' // Alternate registers // Public regAF_ As Long Public regHL_ As Long Public regBC_ As Long Public regDE_ As Long ' // Index registers - ID used as temp for ix/iy Public regIX As Long Public regIY As Long Public regID As Long ' // Stack pointer and program counter Public regSP As Long Public regPC As Long ' // Interrupt registers and flip-flops, and refresh registers Public intI As Long Public intR As Long Public intRTemp As Long Public intIFF1 As Long Public intIFF2 As Long Public intIM As Long ' // parity array Public Parity(256) As Long ' parity lookup table - faster than calculating it Public Z80Memory(65536) As Byte ' working memory ' /******************************************************************************* ' modZ80.bas within vbSpec.vbp ' ' Complete Z80 emulation, including (as far as I know) the ' correct emulation of bits 3 and 5 of F, and undocumented ops. ' Please mail me if you find any bugs in the emulation! ' ' Author: Chris Cowley ' ' Copyright (C)1999-2002 Grok Developments Ltd. ' http://www.grok.co.uk/ ' ' This program is free software; you can redistribute it and/or ' modify it under the terms of the GNU General Public License ' as published by the Free Software Foundation; either version 2 ' of the License, or (at your option) any later version. ' This program is distributed in the hope that it will be useful, ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ' GNU General Public License for more details. ' ' You should have received a copy of the GNU General Public License ' along with this program; if not, write to the Free Software ' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ' ' *******************************************************************************/ 'Option Explicit Private Sub adc_a(ByVal b As Long) Dim ans As Long, c As Long c = -fC ans = (regA + b + c) And &HFF fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fC = ((regA + b + c) And &H100) <> 0 fPV = ((regA Xor ((Not b) And &HFFFF)) And (regA Xor ans) And &H80) <> 0 fH = (((regA And &HF) + (b And &HF) + c) And F_H) <> 0 fN = False regA = ans End Sub Private Function adc16(ByVal a As Long, ByVal b As Long) As Long Dim c As Long, ans As Long c = -(fC) ans = (a + b + c) And &HFFFF fS = (ans And (F_S << 8)) <> 0 f3 = (ans And (F_3 << 8)) <> 0 f5 = (ans And (F_5 << 8)) <> 0 fZ = (ans = 0) fC = ((a + b + c) And &H10000) <> 0 fPV = ((a Xor ((Not b) And &HFFFF)) And (a Xor ans) And &H8000) <> 0 fH = (((a And &HFFF) + (b And &HFFF) + c) And &H1000) <> 0 fN = False adc16 = ans End Function Private Sub add_a(ByVal b As Long) Dim ans As Long ans = (regA + b) And &HFF fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fC = ((regA + b) And &H100) <> 0 fPV = ((regA Xor ((Not (b)) And &HFFFF)) And (regA Xor ans) And &H80) <> 0 fH = (((regA And &HF) + (b And &HF)) And F_H) <> 0 fN = False regA = ans End Sub Private Function add16(ByVal a As Long, ByVal b As Long) As Long Dim ans As Long ans = (a + b) And &HFFFF f3 = (ans And (F_3 << 8)) <> 0 f5 = (ans And (F_5 << 8)) <> 0 fC = ((a + b) And &H10000) <> 0 fH = (((a And &HFFF) + (b And &HFFF)) And &H1000) <> 0 fN = False add16 = ans End Function Private Sub and_a(ByVal b As Long) regA = (regA And b) fS = (regA And F_S) <> 0 f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fH = True fPV = Parity(regA) fZ = (regA = 0) fN = False fC = False End Sub Private Sub bit(ByVal b As Long, ByVal r As Long) Dim IsbitSet As Long IsbitSet = (r And b) <> 0 fN = False fH = True 'f3 = (r And F_3) <> 0 'f5 = (r And F_5) <> 0 fS = IsbitSet And (b = F_S) fZ = Not IsbitSet fPV = fZ End Sub Public Function bitRes(ByVal bit As Long, ByVal val As Long) As Long bitRes = val And (bit Xor &HFFFF) End Function Public Function bitSet(ByVal bit As Long, ByVal val As Long) As Long bitSet = val Or bit End Function Private Sub ccf() f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fH = fC fN = False fC = Not fC End Sub Public Sub cp_a(ByVal b As Long) Dim ans As Long ans = (regA - b) And &HFF fS = (ans And F_S) <> 0 f3 = (b And F_3) <> 0 f5 = (b And F_5) <> 0 fN = True fZ = (ans = 0) fC = ((regA - b) And &H100) <> 0 fH = (((regA And &HF) - (b And &HF)) And F_H) <> 0 fPV = ((regA Xor b) And (regA Xor ans) And &H80) <> 0 End Sub Private Sub cpl_a() regA = (regA Xor &HFF) And &HFF f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fH = True fN = True End Sub Private Sub daa_a() Dim incr As Long If (fH = True) Or ((regA And &HF) > &H9) Then incr = incr Or &H6 End If If (fC = True) Or (regA > &H9F) Then incr = incr Or &H60 End If If ((regA > &H8F) And ((regA And &HF) > 9)) Then incr = incr Or &H60 End If If (regA > &H99) Then fC = True If (fN = True) Then ' sub_a incr fH = (((regA And &HF) - (incr And &HF)) And F_H) <> 0 regA = (regA - incr) And &HFF Else ' // add_a incr fH = (((regA And &HF) + (incr And &HF)) And F_H) <> 0 regA = (regA + incr) And &HFF End If fS = (regA And F_S) <> 0 f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fZ = (regA = 0) fPV = Parity(regA) End Sub Private Function dec16(ByVal a As Long) As Long dec16 = (a - 1) And &HFFFF End Function Private Sub ex_af_af() Dim t As Long t = ((regA << 8) Or getF()) setAF(regAF_) regAF_ = t End Sub Private Function execute_cb() As Long Dim xxx As Long xxx = Z80Memory(regPC) ' new version regPC = regPC + 1 Select Case xxx Case 0 ' 000 RLC B regB = rlc(regB) execute_cb = 8 Case 1 ' 001 RLC C regC = rlc(regC) execute_cb = 8 Case 2 ' 002 RLC D setD(rlc(HighByte(regDE))) execute_cb = 8 Case 3 ' 003 RLC E setE(rlc(getE)) execute_cb = 8 Case 4 ' 004 RLC H setH(rlc(HighByte(regHL))) execute_cb = 8 Case 5 ' 005 RLC L setL(rlc(regHL And &HFF)) execute_cb = 8 Case 6 ' 006 RLC (HL) pokeb(regHL, rlc(peekb(regHL))) execute_cb = 15 Case 7 ' 007 RLC A regA = rlc(regA) execute_cb = 8 Case 8 ' 008 RRC B regB = rrc(regB) execute_cb = 8 Case 9 ' 009 RRC C regC = rrc(regC) execute_cb = 8 Case 10 ' 010 RRC D setD(rrc(HighByte(regDE))) execute_cb = 8 Case 11 ' 011 RRC E setE(rrc(getE)) execute_cb = 8 Case 12 ' 012 RRC H setH(rrc(HighByte(regHL))) execute_cb = 8 Case 13 ' 013 RRC L setL(rrc(regHL And &HFF)) execute_cb = 8 Case 14 ' 014 RRC (HL) pokeb(regHL, rrc(peekb(regHL))) execute_cb = 15 Case 15 ' 015 RRC A regA = rrc(regA) execute_cb = 8 Case 16 ' 016 RL B regB = rl(regB) execute_cb = 8 Case 17 ' 017 RL C regC = rl(regC) execute_cb = 8 Case 18 ' 018 RL D setD(rl(HighByte(regDE))) execute_cb = 8 Case 19 ' 019 RL E setE(rl(getE)) execute_cb = 8 Case 20 ' 020 RL H setH(rl(HighByte(regHL))) execute_cb = 8 Case 21 ' 021 RL L setL(rl(regHL And &HFF)) execute_cb = 8 Case 22 ' 022 RL (HL) pokeb(regHL, rl(peekb(regHL))) execute_cb = 15 Case 23 ' 023 RL A regA = rl(regA) execute_cb = 8 Case 24 ' 024 RR B regB = rr(regB) execute_cb = 8 Case 25 ' 025 RR C regC = rr(regC) execute_cb = 8 Case 26 ' 026 RR D setD(rr(HighByte(regDE))) execute_cb = 8 Case 27 ' 027 RR E setE(rr(getE)) execute_cb = 8 Case 28 ' 028 RR H setH(rr(HighByte(regHL))) execute_cb = 8 Case 29 ' 029 RR L setL(rr(regHL And &HFF)) execute_cb = 8 Case 30 ' 030 RR (HL) pokeb(regHL, rr(peekb(regHL))) execute_cb = 15 Case 31 ' 031 RR A regA = rr(regA) execute_cb = 8 Case 32 ' 32 SLA B regB = sla(regB) execute_cb = 8 Case 33 ' 33 SLA C regC = sla(regC) execute_cb = 8 Case 34 ' 34 SLA D setD(sla(HighByte(regDE))) execute_cb = 8 Case 35 ' 35 SLA E setE(sla(getE)) execute_cb = 8 Case 36 ' 36 SLA H setH(sla(HighByte(regHL))) execute_cb = 8 Case 37 ' 37 SLA L setL(sla(regHL And &HFF)) execute_cb = 8 Case 38 ' 38 SLA (HL) pokeb(regHL, sla(peekb(regHL))) execute_cb = 15 Case 39 ' 39 SLA A regA = sla(regA) execute_cb = 8 Case 40 ' 40 SRA B regB = sra(regB) execute_cb = 8 Case 41 ' 41 SRA C regC = sra(regC) execute_cb = 8 Case 42 ' 42 ' SRA D setD(sra(HighByte(regDE))) execute_cb = 8 Case 43 ' 43 SRA E setE(sra(getE)) execute_cb = 8 Case 44 ' 44 SRA H setH(sra(HighByte(regHL))) execute_cb = 8 Case 45 ' 45 SRA L setL(sra(regHL And &HFF)) execute_cb = 8 Case 46 ' 46 SRA (HL) pokeb(regHL, sra(peekb(regHL))) execute_cb = 15 Case 47 ' 47 SRA A regA = sra(regA) execute_cb = 8 Case 48 ' 48 SLS B regB = sls(regB) execute_cb = 8 Case 49 ' 49 SLS C regC = sls(regC) execute_cb = 8 Case 50 ' 50 SLS D setD(sls(HighByte(regDE))) execute_cb = 8 Case 51 ' 51 SLS E setE(sls(getE)) execute_cb = 8 Case 52 ' 52 SLS H setH(sls(HighByte(regHL))) execute_cb = 8 Case 53 ' 53 SLS L setL(sls(regHL And &HFF)) execute_cb = 8 Case 54 ' 54 SLS (HL) pokeb(regHL, sls(peekb(regHL))) execute_cb = 15 Case 55 ' 55 SLS A regA = sls(regA) execute_cb = 8 Case 56 ' 56 SRL B regB = srl(regB) execute_cb = 8 Case 57 ' 57 SRL C regC = srl(regC) execute_cb = 8 Case 58 ' 58 SRL D setD(srl(HighByte(regDE))) execute_cb = 8 Case 59 ' 59 SRL E setE(srl(getE)) execute_cb = 8 Case 60 ' 60 ' SRL H setH(srl(HighByte(regHL))) execute_cb = 8 Case 61 ' 61 SRL L setL(srl(regHL And &HFF)) execute_cb = 8 Case 62 ' 62 SRL (HL) pokeb(regHL, srl(peekb(regHL))) execute_cb = 15 Case 63 ' 63 SRL A regA = srl(regA) execute_cb = 8 Case 64 ' 064 BIT 0,B bit(&H1, regB) execute_cb = 8 Case 65 ' 065 BIT 0,C bit(1, regC) execute_cb = 8 Case 66 ' 066 BIT 0,D bit(1, HighByte(regDE)) execute_cb = 8 Case 67 ' 067 BIT 0,E bit(1, getE) execute_cb = 8 Case 68 ' 068 BIT 0,H bit(1, HighByte(regHL)) execute_cb = 8 Case 69 ' 069 BIT 0,L bit(1, (regHL And &HFF)) execute_cb = 8 Case 70 ' 070 BIT 0,(HL) bit(1, peekb(regHL)) execute_cb = 12 Case 71 ' 071 BIT 0,A bit(1, regA) execute_cb = 8 Case 72 ' 72 BIT 1,B bit(2, regB) execute_cb = 8 Case 73 ' 73 BIT 1,C bit(2, regC) execute_cb = 8 Case 74 ' 74 BIT 1,D bit(2, HighByte(regDE)) execute_cb = 8 Case 75 ' 75 BIT 1,E bit(2, getE) execute_cb = 8 Case 76 ' 76 BIT 1,H bit(2, HighByte(regHL)) execute_cb = 8 Case 77 ' 77 BIT 1,L bit(2, (regHL And &HFF)) execute_cb = 8 Case 78 ' 78 BIT 1,(HL) bit(2, peekb(regHL)) execute_cb = 12 Case 79 ' 79 BIT 1,A bit(2, regA) execute_cb = 8 Case 80 ' BIT 2,B bit(4, regB) execute_cb = 8 Case 81 ' BIT 2,C bit(4, regC) execute_cb = 8 Case 82 ' BIT 2,D bit(4, HighByte(regDE)) execute_cb = 8 Case 83 ' BIT 2,E bit(4, getE) execute_cb = 8 Case 84 ' BIT 2,H bit(4, HighByte(regHL)) execute_cb = 8 Case 85 ' BIT 2,L bit(4, (regHL And &HFF)) execute_cb = 8 Case 86 ' BIT 2,(HL) bit(4, peekb(regHL)) execute_cb = 12 Case 87 ' BIT 2,A bit(4, regA) execute_cb = 8 Case 88 ' BIT 3,B bit(8, regB) execute_cb = 8 Case 89 ' BIT 3,C bit(8, regC) execute_cb = 8 Case 90 ' BIT 3,D bit(8, HighByte(regDE)) execute_cb = 8 Case 91 ' BIT 3,E bit(8, getE) execute_cb = 8 Case 92 ' BIT 3,H bit(8, HighByte(regHL)) execute_cb = 8 Case 93 ' BIT 3,L bit(8, (regHL And &HFF)) execute_cb = 8 Case 94 ' BIT 3,(HL) bit(8, peekb(regHL)) execute_cb = 12 Case 95 ' BIT 3,A bit(8, regA) execute_cb = 8 Case 96 ' BIT 4,B bit(&H10, regB) execute_cb = 8 Case 97 ' BIT 4,C bit(&H10, regC) execute_cb = 8 Case 98 ' BIT 4,D bit(&H10, HighByte(regDE)) execute_cb = 8 Case 99 ' BIT 4,E bit(&H10, getE) execute_cb = 8 Case 100 ' BIT 4,H bit(&H10, HighByte(regHL)) execute_cb = 8 Case 101 ' BIT 4,L bit(&H10, (regHL And &HFF)) execute_cb = 8 Case 102 ' BIT 4,(HL) bit(&H10, peekb(regHL)) execute_cb = 12 Case 103 ' BIT 4,A bit(&H10, regA) execute_cb = 8 Case 104 ' BIT 5,B bit(&H20, regB) execute_cb = 8 Case 105 ' BIT 5,C bit(&H20, regC) execute_cb = 8 Case 106 ' BIT 5,D bit(&H20, HighByte(regDE)) execute_cb = 8 Case 107 ' BIT 5,E bit(&H20, getE) execute_cb = 8 Case 108 ' BIT 5,H bit(&H20, HighByte(regHL)) execute_cb = 8 Case 109 ' BIT 5,L bit(&H20, (regHL And &HFF)) execute_cb = 8 Case 110 ' BIT 5,(HL) bit(&H20, peekb(regHL)) execute_cb = 12 Case 111 ' BIT 5,A bit(&H20, regA) execute_cb = 8 Case 112 ' 112 BIT 6,B bit(&H40, regB) execute_cb = 8 Case 113 ' 113 BIT 6,C bit(&H40, regC) execute_cb = 8 Case 114 ' 114 BIT 6,D bit(&H40, HighByte(regDE)) execute_cb = 8 Case 115 ' 115 BIT 6,E bit(&H40, getE) execute_cb = 8 Case 116 ' 116 BIT 6,H bit(&H40, HighByte(regHL)) execute_cb = 8 Case 117 ' 117 BIT 6,L bit(&H40, (regHL And &HFF)) execute_cb = 8 Case 118 ' 118 BIT 6,(HL) bit(&H40, peekb(regHL)) execute_cb = 12 Case 119 ' 119 ' BIT 6,A bit(&H40, regA) execute_cb = 8 Case 120 ' 120 BIT 7,B bit(&H80, regB) execute_cb = 8 Case 121 ' 121 BIT 7,C bit(&H80, regC) execute_cb = 8 Case 122 ' 122 BIT 7,D bit(&H80, HighByte(regDE)) execute_cb = 8 Case 123 ' 123 BIT 7,E bit(&H80, getE) execute_cb = 8 Case 124 ' 124 BIT 7,H bit(&H80, HighByte(regHL)) execute_cb = 8 Case 125 ' 125 BIT 7,L bit(&H80, (regHL And &HFF)) execute_cb = 8 Case 126 ' 126 BIT 7,(HL) bit(&H80, peekb(regHL)) execute_cb = 12 Case 127 ' 127 BIT 7,A bit(&H80, regA) execute_cb = 8 Case 128 ' RES 0,B regB = bitRes(1, regB) execute_cb = 8 Case 129 ' RES 0,C regC = bitRes(1, regC) execute_cb = 8 Case 130 ' RES 0,D setD(bitRes(1, HighByte(regDE))) execute_cb = 8 Case 131 ' RES 0,E setE(bitRes(1, getE)) execute_cb = 8 Case 132 ' RES 0,H setH(bitRes(1, HighByte(regHL))) execute_cb = 8 Case 133 ' RES 0,L setL(bitRes(1, regHL And &HFF)) execute_cb = 8 Case 134 ' RES 0,(HL) pokeb(regHL, bitRes(&H1, peekb(regHL))) execute_cb = 15 Case 135 ' RES 0,A regA = bitRes(1, regA) execute_cb = 8 Case 136 ' RES 1,B regB = bitRes(2, regB) execute_cb = 8 Case 137 ' RES 1,C regC = bitRes(2, regC) execute_cb = 8 Case 138 ' RES 1,D setD(bitRes(2, HighByte(regDE))) execute_cb = 8 Case 139 ' RES 1,E setE(bitRes(2, getE)) execute_cb = 8 Case 140 ' RES 1,H setH(bitRes(2, HighByte(regHL))) execute_cb = 8 Case 141 ' RES 1,L setL(bitRes(2, regHL And &HFF)) execute_cb = 8 Case 142 ' RES 1,(HL) pokeb(regHL, bitRes(2, peekb(regHL))) execute_cb = 15 Case 143 ' RES 1,A regA = bitRes(2, regA) execute_cb = 8 Case 144 ' RES 2,B regB = bitRes(4, regB) execute_cb = 8 Case 145 ' RES 2,C regC = bitRes(4, regC) execute_cb = 8 Case 146 ' RES 2,D setD(bitRes(4, HighByte(regDE))) execute_cb = 8 Case 147 ' RES 2,E setE(bitRes(4, getE)) execute_cb = 8 Case 148 ' RES 2,H setH(bitRes(4, HighByte(regHL))) execute_cb = 8 Case 149 ' RES 2,L setL(bitRes(4, regHL And &HFF)) execute_cb = 8 Case 150 ' RES 2,(HL) pokeb(regHL, bitRes(4, peekb(regHL))) execute_cb = 15 Case 151 ' RES 2,A regA = bitRes(4, regA) execute_cb = 8 Case 152 ' RES 3,B regB = bitRes(8, regB) execute_cb = 8 Case 153 ' RES 3,C regC = bitRes(8, regC) execute_cb = 8 Case 154 ' RES 3,D setD(bitRes(8, HighByte(regDE))) execute_cb = 8 Case 155 ' RES 3,E setE(bitRes(8, getE)) execute_cb = 8 Case 156 ' RES 3,H setH(bitRes(8, HighByte(regHL))) execute_cb = 8 Case 157 ' RES 3,L setL(bitRes(8, regHL And &HFF)) execute_cb = 8 Case 158 ' RES 3,(HL) pokeb(regHL, bitRes(8, peekb(regHL))) execute_cb = 15 Case 159 ' RES 3,A regA = bitRes(8, regA) execute_cb = 8 Case 160 ' RES 4,B regB = bitRes(&H10, regB) execute_cb = 8 Case 161 ' RES 4,C regC = bitRes(&H10, regC) execute_cb = 8 Case 162 ' RES 4,D setD(bitRes(&H10, HighByte(regDE))) execute_cb = 8 Case 163 ' RES 4,E setE(bitRes(&H10, getE)) execute_cb = 8 Case 164 ' RES 4,H setH(bitRes(&H10, HighByte(regHL))) execute_cb = 8 Case 165 ' RES 4,L setL(bitRes(&H10, regHL And &HFF)) execute_cb = 8 Case 166 ' RES 4,(HL) pokeb(regHL, bitRes(&H10, peekb(regHL))) execute_cb = 15 Case 167 ' RES 4,A regA = bitRes(&H10, regA) execute_cb = 8 Case 168 ' 168 RES 5,B regB = bitRes(&H20, regB) execute_cb = 8 Case 169 ' 169 RES 5,C regC = bitRes(&H20, regC) execute_cb = 8 Case 170 ' 170 RES 5,D setD(bitRes(&H20, HighByte(regDE))) execute_cb = 8 Case 171 ' 171 RES 5,E setE(bitRes(&H20, getE)) execute_cb = 8 Case 172 ' RES 5,H setH(bitRes(&H20, HighByte(regHL))) execute_cb = 8 Case 173 ' RES 5,L setL(bitRes(&H20, regHL And &HFF)) execute_cb = 8 Case 174 ' RES 5,(HL) pokeb(regHL, bitRes(&H20, peekb(regHL))) execute_cb = 15 Case 175 ' RES 5,A regA = bitRes(&H20, regA) execute_cb = 8 Case 176 ' RES 6,B regB = bitRes(&H40, regB) execute_cb = 8 Case 177 ' RES 6,C regC = bitRes(&H40, regC) execute_cb = 8 Case 178 ' RES 6,D setD(bitRes(&H40, HighByte(regDE))) execute_cb = 8 Case 179 ' RES 6,E setE(bitRes(&H40, getE)) execute_cb = 8 Case 180 ' RES 6,H setH(bitRes(&H40, HighByte(regHL))) execute_cb = 8 Case 181 ' RES 6,L setL(bitRes(&H40, regHL And &HFF)) execute_cb = 8 Case 182 ' RES 6,(HL) pokeb(regHL, bitRes(&H40, peekb(regHL))) execute_cb = 15 Case 183 ' RES 6,A regA = bitRes(&H40, regA) execute_cb = 8 Case 184 ' RES 7,B regB = bitRes(&H80, regB) execute_cb = 8 Case 185 ' RES 7,C regC = bitRes(&H80, regC) execute_cb = 8 Case 186 ' RES 7,D setD(bitRes(&H80, HighByte(regDE))) execute_cb = 8 Case 187 ' RES 7,E setE(bitRes(&H80, getE)) execute_cb = 8 Case 188 ' RES 7,H setH(bitRes(&H80, HighByte(regHL))) execute_cb = 8 Case 189 ' RES 7,L setL(bitRes(&H80, regHL And &HFF)) execute_cb = 8 Case 190 ' RES 7,(HL) pokeb(regHL, bitRes(&H80, peekb(regHL))) execute_cb = 15 Case 191 ' RES 7,A regA = bitRes(&H80, regA) execute_cb = 8 Case 192 ' SET 0,B regB = bitSet(1, regB) execute_cb = 8 Case 193 ' SET 0,C regC = bitSet(1, regC) execute_cb = 8 Case 194 ' SET 0,D setD(bitSet(1, HighByte(regDE))) execute_cb = 8 Case 195 ' SET 0,E setE(bitSet(1, getE)) execute_cb = 8 Case 196 ' SET 0,H setH(bitSet(1, HighByte(regHL))) execute_cb = 8 Case 197 ' SET 0,L setL(bitSet(1, regHL And &HFF)) execute_cb = 8 Case 198 ' SET 0,(HL) pokeb(regHL, bitSet(1, peekb(regHL))) execute_cb = 15 Case 199 ' SET 0,A regA = bitSet(1, regA) execute_cb = 8 Case 200 ' SET 1,B regB = bitSet(2, regB) execute_cb = 8 Case 201 ' SET 1,C regC = bitSet(2, regC) execute_cb = 8 Case 202 ' SET 1,D setD(bitSet(2, HighByte(regDE))) execute_cb = 8 Case 203 ' SET 1,E setE(bitSet(2, getE)) execute_cb = 8 Case 204 ' SET 1,H setH(bitSet(2, HighByte(regHL))) execute_cb = 8 Case 205 ' SET 1,L setL(bitSet(2, regHL And &HFF)) execute_cb = 8 Case 206 ' SET 1,(HL) pokeb(regHL, bitSet(2, peekb(regHL))) execute_cb = 15 Case 207 ' SET 1,A regA = bitSet(2, regA) execute_cb = 8 Case 208 ' SET 2,B regB = bitSet(4, regB) execute_cb = 8 Case 209 ' SET 2,C regC = bitSet(4, regC) execute_cb = 8 Case 210 ' SET 2,D setD(bitSet(4, HighByte(regDE))) execute_cb = 8 Case 211 ' SET 2,E setE(bitSet(4, getE)) execute_cb = 8 Case 212 ' SET 2,H setH(bitSet(4, HighByte(regHL))) execute_cb = 8 Case 213 ' SET 2,L setL(bitSet(4, regHL And &HFF)) execute_cb = 8 Case 214 ' SET 2,(HL) pokeb(regHL, bitSet(&H4, peekb(regHL))) execute_cb = 15 Case 215 ' SET 2,A regA = bitSet(4, regA) execute_cb = 8 Case 216 ' SET 3,B regB = bitSet(8, regB) execute_cb = 8 Case 217 ' SET 3,C regC = bitSet(8, regC) execute_cb = 8 Case 218 ' SET 3,D setD(bitSet(8, HighByte(regDE))) execute_cb = 8 Case 219 ' SET 3,E setE(bitSet(8, getE)) execute_cb = 8 Case 220 ' SET 3,H setH(bitSet(8, HighByte(regHL))) execute_cb = 8 Case 221 ' SET 3,L setL(bitSet(8, regHL And &HFF)) execute_cb = 8 Case 222 ' SET 3,(HL) pokeb(regHL, bitSet(&H8, peekb(regHL))) execute_cb = 15 Case 223 ' SET 3,A regA = bitSet(8, regA) execute_cb = 8 Case 224 ' SET 4,B regB = bitSet(&H10, regB) execute_cb = 8 Case 225 ' SET 4,C regC = bitSet(&H10, regC) execute_cb = 8 Case 226 ' SET 4,D setD(bitSet(&H10, HighByte(regDE))) execute_cb = 8 Case 227 ' SET 4,E setE(bitSet(&H10, getE)) execute_cb = 8 Case 228 ' SET 4,H setH(bitSet(&H10, HighByte(regHL))) execute_cb = 8 Case 229 ' SET 4,L setL(bitSet(&H10, regHL And &HFF)) execute_cb = 8 Case 230 ' SET 4,(HL) pokeb(regHL, bitSet(&H10, peekb(regHL))) execute_cb = 15 Case 231 ' SET 4,A regA = bitSet(&H10, regA) execute_cb = 8 Case 232 ' SET 5,B regB = bitSet(&H20, regB) execute_cb = 8 Case 233 ' SET 5,C regC = bitSet(&H20, regC) execute_cb = 8 Case 234 ' SET 5,D setD(bitSet(&H20, HighByte(regDE))) execute_cb = 8 Case 235 ' SET 5,E setE(bitSet(&H20, getE)) execute_cb = 8 Case 236 ' SET 5,H setH(bitSet(&H20, HighByte(regHL))) execute_cb = 8 Case 237 ' SET 5,L setL(bitSet(&H20, regHL And &HFF)) execute_cb = 8 Case 238 ' SET 5,(HL) pokeb(regHL, bitSet(&H20, peekb(regHL))) execute_cb = 15 Case 239 ' SET 5,A regA = bitSet(&H20, regA) execute_cb = 8 Case 240 ' SET 6,B regB = bitSet(&H40, regB) execute_cb = 8 Case 241 ' SET 6,C regC = bitSet(&H40, regC) execute_cb = 8 Case 242 ' SET 6,D setD(bitSet(&H40, HighByte(regDE))) execute_cb = 8 Case 243 ' SET 6,E setE(bitSet(&H40, getE)) execute_cb = 8 Case 244 ' SET 6,H setH(bitSet(&H40, HighByte(regHL))) execute_cb = 8 Case 245 ' SET 6,L setL(bitSet(&H40, regHL And &HFF)) execute_cb = 8 Case 246 ' SET 6,(HL) pokeb(regHL, bitSet(&H40, peekb(regHL))) execute_cb = 15 Case 247 ' SET 6,A regA = bitSet(&H40, regA) execute_cb = 8 Case 248 ' SET 7,B regB = bitSet(&H80, regB) execute_cb = 8 Case 249 ' SET 7,C regC = bitSet(&H80, regC) execute_cb = 8 Case 250 ' SET 7,D setD(bitSet(&H80, HighByte(regDE))) execute_cb = 8 Case 251 ' SET 7,E setE(bitSet(&H80, getE)) execute_cb = 8 Case 252 ' 252 SET 7,H setH(bitSet(&H80, HighByte(regHL))) execute_cb = 8 Case 253 ' 253 SET 7,L setL(bitSet(&H80, regHL And &HFF)) execute_cb = 8 Case 254 ' 254 SET 7,(HL) pokeb(regHL, bitSet(&H80, peekb(regHL))) execute_cb = 15 Case 255 ' 255 SET 7,A regA = bitSet(&H80, regA) execute_cb = 8 Case Else execute_cb = 0 ' so the end function doesn't error, never runs this line though End Select End Function Private Function execute_ed() As Long Dim xxx As Long, count As Long, dest As Long, from As Long Dim c As Long, b As Long xxx = Z80Memory(regPC) regPC = regPC + 1 Select Case xxx Case 0 To 63 ' 000 to 063 = NOP * 2 execute_ed = 8 Case 64 ' 064 IN B,(c) regB = in_bc() execute_ed = 12 Case 65 ' 065 OUT (c),B Outb(((regB << 8) Or regC), regB) execute_ed = 12 Case 66 ' 066 SBC HL,BC regHL = sbc16(regHL, (regB << 8) Or regC) execute_ed = 15 Case 67 ' 067 LD (nn),BC pokew(nxtpcw(), ((regB << 8) Or regC)) execute_ed = 20 Case 68 ' 068 NEG neg_a() execute_ed = 8 Case 69 ' 069 RETn intIFF1 = intIFF2 poppc() execute_ed = 14 Case 70 ' 070 IM 0 intIM = 0 execute_ed = 8 Case 71 ' 071 LD I,A intI = regA execute_ed = 9 Case 72 ' 072 IN C,(c) regC = in_bc() execute_ed = 12 Case 73 ' 073 OUT (c),C Outb(((regB << 8) Or regC), regC) execute_ed = 12 Case 74 ' 074 ADC HL,BC regHL = adc16(regHL, (regB << 8) Or regC) execute_ed = 15 Case 75 ' 075 LD BC,(nn) setBC(peekw(nxtpcw())) execute_ed = 20 Case 76 ' 076 NEG neg_a() execute_ed = 8 Case 77 ' 077 RETI ' // TOCHECK: according to the official Z80 docs, IFF2 does not get ' // copied to IFF1 for RETI - but in a real Z80 it is intIFF1 = intIFF2 poppc() execute_ed = 14 Case 78 ' 078 IM 0 intIM = 0 execute_ed = 8 Case 79 ' 079 LD R,A intR = regA intRTemp = intR execute_ed = 9 Case 80 ' 080 IN D,(c) setD(in_bc()) execute_ed = 12 Case 81 ' 081 OUT (c),D Outb(((regB << 8) Or regC), HighByte(regDE)) execute_ed = 12 Case 82 ' 082 SBC HL,DE regHL = sbc16(regHL, regDE) execute_ed = 15 Case 83 ' 083 LD (nn),DE pokew(nxtpcw(), regDE) execute_ed = 20 Case 84 ' NEG neg_a() execute_ed = 8 Case 85 ' RETn intIFF1 = intIFF2 poppc() execute_ed = 14 Case 86 ' IM 1 intIM = 1 execute_ed = 8 Case 87 ' LD A,I ld_a_i() execute_ed = 9 Case 88 ' 088 IN E,(c) setE(in_bc()) execute_ed = 12 Case 89 ' 089 OUT (c),E Outb(((regB << 8) Or regC), getE) execute_ed = 12 Case 90 ' 090 ADC HL,DE regHL = adc16(regHL, regDE) execute_ed = 15 Case 91 ' 091 LD DE,(nn) regDE = peekw(nxtpcw()) execute_ed = 20 Case 92 ' NEG neg_a() execute_ed = 8 Case 93 ' RETI ' // TOCHECK: according to the official Z80 docs, IFF2 does not get ' // copied to IFF1 for RETI - but in a real Z80 it is intIFF1 = intIFF2 poppc() execute_ed = 14 Case 94 ' IM 2 intIM = 2 execute_ed = 8 Case 95 ' LD A,R ld_a_r() execute_ed = 9 Case 96 ' IN H,(c) setH(in_bc()) execute_ed = 12 Case 97 ' OUT (c),H Outb(((regB << 8) Or regC), HighByte(regHL)) execute_ed = 12 Case 98 ' SBC HL,HL regHL = sbc16(regHL, regHL) execute_ed = 15 Case 99 ' LD (nn),HL pokew(nxtpcw(), regHL) execute_ed = 20 Case 100 ' NEG neg_a() execute_ed = 8 Case 101 ' RETn intIFF1 = intIFF2 poppc() execute_ed = 14 Case 102 ' IM 0 intIM = 0 execute_ed = 8 Case 103 ' RRD rrd_a() execute_ed = 18 Case 104 ' IN L,(c) setL(in_bc()) execute_ed = 12 Case 105 ' OUT (c),L Outb(((regB << 8) Or regC), regHL And &HFF) execute_ed = 12 Case 106 ' ADC HL,HL regHL = adc16(regHL, regHL) execute_ed = 15 Case 107 ' LD HL,(nn) regHL = peekw(nxtpcw()) execute_ed = 20 Case 108 ' NEG neg_a() execute_ed = 8 Case 109 ' RETI ' // TOCHECK: according to the official Z80 docs, IFF2 does not get ' // copied to IFF1 for RETI - but in a real Z80 it is intIFF1 = intIFF2 poppc() execute_ed = 14 Case 110 ' IM 0 intIM = 0 execute_ed = 8 Case 111 ' RLD rld_a() execute_ed = 18 Case 112 ' IN (c) in_bc() execute_ed = 12 Case 113 ' OUT (c),0 Outb(((regB << 8) Or regC), 0) execute_ed = 12 Case 114 ' SBC HL,SP regHL = sbc16(regHL, regSP) execute_ed = 15 Case 115 ' LD (nn),SP pokew(nxtpcw(), regSP) execute_ed = 20 Case 116 ' NEG neg_a() execute_ed = 8 Case 117 ' RETn intIFF1 = intIFF2 poppc() execute_ed = 14 Case 118 ' IM 1 intIM = 1 execute_ed = 8 Case 119 ' // Undocumented NOP * 2 execute_ed = 8 Case 120 ' IN A,(c) regA = in_bc() execute_ed = 12 Case 121 ' OUT (c),A Outb(((regB << 8) Or regC), regA) execute_ed = 12 Case 122 ' ADC HL,SP regHL = adc16(regHL, regSP) execute_ed = 15 Case 123 ' LD SP,(nn) regSP = peekw(nxtpcw()) execute_ed = 20 Case 124 ' NEG neg_a() execute_ed = 8 Case 125 ' RETI ' // TOCHECK: according to the official Z80 docs, IFF2 does not get ' // copied to IFF1 for RETI - but in a real Z80 it is intIFF1 = intIFF2 poppc() execute_ed = 14 Case 126 ' IM 2 intIM = 2 execute_ed = 8 Case 127 ' NOP execute_ed = 8 Case 128 To 159 ' NOP * 2 (128 to 159) execute_ed = 8 ' // xxI Case 160 ' LDI pokeb(regDE, peekb(regHL)) regDE = (regDE + 1) And &HFFFF regHL = (regHL + 1) And &HFFFF setBC(dec16((regB << 8) Or regC)) fPV = (((regB << 8) Or regC) <> 0) fH = False fN = False execute_ed = 16 Case 161 ' CPI c = fC cp_a(peekb(regHL)) regHL = (regHL + 1) And &HFFFF setBC(dec16((regB << 8) Or regC)) fPV = (((regB << 8) Or regC) <> 0) fC = c execute_ed = 16 Case 162 ' INI pokeb(regHL, Inb((regB << 8) Or regC)) b = qdec8(regB) regB = b regHL = (regHL + 1) And &HFFFF fZ = (b = 0) fN = True execute_ed = 16 Case 163 ' OUTI b = qdec8(regB) regB = b Outb(((regB << 8) Or regC), peekb(regHL)) regHL = (regHL + 1) And &HFFFF fZ = (b = 0) fN = True execute_ed = 16 ' /* xxD */ Case 168 ' LDD pokeb(regDE, peekb(regHL)) regDE = dec16(regDE) regHL = dec16(regHL) setBC(dec16((regB << 8) Or regC)) fPV = (((regB << 8) Or regC) <> 0) fH = False fN = False execute_ed = 16 Case 169 ' CPD c = fC cp_a(peekb(regHL)) regHL = dec16(regHL) setBC(dec16((regB << 8) Or regC)) fPV = (((regB << 8) Or regC) <> 0) fC = c execute_ed = 16 Case 170 ' IND pokeb(regHL, Inb((regB << 8) Or regC)) b = qdec8(regB) regB = b regHL = dec16(regHL) fZ = (b = 0) fN = True execute_ed = 16 Case 171 ' OUTD count = qdec8(regB) regB = count Outb(((regB << 8) Or regC), peekb(regHL)) regHL = dec16(regHL) fZ = (count = 0) fN = True execute_ed = 16 ' // xxIR Case 176 ' LDIR b = peekb(regHL) pokeb(regDE, b) regHL = (regHL + 1) And &HFFFF regDE = (regDE + 1) And &HFFFF setBC(((regB << 8) Or regC) - 1) fH = False fN = False f3 = (b And F_3) <> 0 f5 = (b And F_5) <> 0 If ((regB << 8) Or regC) <> 0 Then regPC = regPC - 2 fPV = True execute_ed = 21 Else fPV = False execute_ed = 16 End If count = getBC() dest = regDE from = regHL Do pokeb(dest, peekb(from)) from = (from + 1) And 65535 dest = (dest + 1) And 65535 count = count - 1 Loop While count <> 0 regPC = regPC - 2 fH = False fN = False fPV = True If count = 0 Then regPC = regPC + 2 fPV = False End If regDE = dest regHL = from setBC(count) Case 177 ' CPIR c = fC b = peekb(regHL) cp_a(b) regHL = (regHL + 1) And &HFFFF setBC(dec16((regB << 8) Or regC)) fC = c f3 = (b And F_3) <> 0 f5 = (b And F_5) <> 0 c = ((regB << 8) Or regC) <> 0 fPV = c If (fPV) And (fZ = False) Then regPC = regPC - 2 execute_ed = 21 Else execute_ed = 16 End If Case 178 ' INIR pokeb(regHL, Inb((regB << 8) Or regC)) b = qdec8(regB) regB = b regHL = (regHL + 1) And &HFFFF fZ = True fN = True If (b <> 0) Then regPC = regPC - 2 execute_ed = 21 Else execute_ed = 16 End If Case 179 ' OTIR b = qdec8(regB) regB = b Outb(((regB << 8) Or regC), peekb(regHL)) regHL = (regHL + 1) And &HFFFF fZ = True fN = True If (b <> 0) Then regPC = regPC - 2 execute_ed = 21 Else execute_ed = 16 End If ' // xxDR Case 184 ' LDDR b = peekb(regHL) pokeb(regDE, b) regHL = dec16(regHL) regDE = dec16(regDE) setBC(((regB << 8) Or regC) - 1) fH = False fN = False f3 = (b And F_3) <> 0 f5 = (b And F_5) <> 0 If ((regB << 8) Or regC) <> 0 Then regPC = regPC - 2 fPV = True execute_ed = 21 Else fPV = False execute_ed = 16 End If count = getBC() dest = regDE from = regHL Do pokeb(dest, peekb(from)) from = (from - 1) And 65535 dest = (dest - 1) And 65535 count = count - 1 Loop While count <> 0 regPC = regPC - 2 fH = False fN = False fPV = True If count = 0 Then regPC = regPC + 2 fPV = False End If regDE = dest regHL = from setBC(count) Case 185 ' CPDR c = fC b = peekb(regHL) cp_a(b) regHL = dec16(regHL) setBC(dec16((regB << 8) Or regC)) fPV = ((regB << 8) Or regC) <> 0 fC = c f3 = (b And F_3) <> 0 f5 = (b And F_5) <> 0 If (fPV) And (fZ = False) Then regPC = regPC - 2 execute_ed = 21 Else execute_ed = 16 End If Case 186 ' INDR pokeb(regHL, Inb((regB << 8) Or regC)) b = qdec8(regB) regB = b regHL = dec16(regHL) fZ = True fN = True If (b <> 0) Then regPC = regPC - 2 execute_ed = 21 Else execute_ed = 16 End If Case 187 ' OTDR b = qdec8(regB) regB = b Outb(((regB << 8) Or regC), peekb(regHL)) regHL = dec16(regHL) fZ = True fN = True If (b <> 0) Then regPC = regPC - 2 execute_ed = 21 Else execute_ed = 16 End If Case 187 To 191 MsgBox("Unknown ED instruction " & xxx & " at " & regPC) execute_ed = 8 Case 192 To 255 ' // MsgBox "Unknown ED instruction " & xxx & " at " & regPC ' NOP * 2 execute_ed = 8 Case Else ' (164 To 167, 172 To 175, 180 To 183) ' NOP * 2 execute_ed = 8 End Select End Function Private Sub execute_id_cb(ByVal op As Long, ByVal z As Long) Select Case op Case 0 ' 000 RLC B op = rlc(peekb(z)) regB = op pokeb(z, op) Case 1 ' 001 RLC C op = rlc(peekb(z)) regC = op pokeb(z, op) Case 2 ' 002 RLC D op = rlc(peekb(z)) setD(op) pokeb(z, op) Case 3 ' 003 RLC E op = rlc(peekb(z)) setE(op) pokeb(z, op) Case 4 ' 004 RLC H op = rlc(peekb(z)) setH(op) pokeb(z, op) Case 5 ' 005 RLC L op = rlc(peekb(z)) setL(op) pokeb(z, op) Case 6 ' 006 RLC (HL) pokeb(z, rlc(peekb(z))) Case 7 ' 007 RLC A op = rlc(peekb(z)) regA = op pokeb(z, op) Case 8 ' 008 RRC B op = rrc(peekb(z)) regB = op pokeb(z, op) Case 9 ' 009 RRC C op = rrc(peekb(z)) regC = op pokeb(z, op) Case 10 ' 010 RRC D op = rrc(peekb(z)) setD(op) pokeb(z, op) Case 11 ' 011 RRC E op = rrc(peekb(z)) setE(op) pokeb(z, op) Case 12 ' 012 RRC H op = rrc(peekb(z)) setH(op) pokeb(z, op) Case 13 ' 013 RRC L op = rrc(peekb(z)) setL(op) pokeb(z, op) Case 14 ' 014 RRC (HL) pokeb(z, rrc(peekb(z))) Case 15 ' 015 RRC A op = rrc(peekb(z)) regA = op pokeb(z, op) Case 16 ' RL B op = rl(peekb(z)) regB = op pokeb(z, op) Case 17 ' RL C op = rl(peekb(z)) regC = op pokeb(z, op) Case 18 ' RL D op = rl(peekb(z)) setD(op) pokeb(z, op) Case 19 ' RL E op = rl(peekb(z)) setE(op) pokeb(z, op) Case 20 ' RL H op = rl(peekb(z)) setH(op) pokeb(z, op) Case 21 ' RL L op = rl(peekb(z)) setL(op) pokeb(z, op) Case 22 ' RL (HL) pokeb(z, rl(peekb(z))) Case 23 ' RL A op = rl(peekb(z)) regA = op pokeb(z, op) Case 24 ' RR B op = rr(peekb(z)) regB = op pokeb(z, op) Case 25 ' RR C op = rr(peekb(z)) regC = op pokeb(z, op) Case 26 ' RR D op = rr(peekb(z)) setD(op) pokeb(z, op) Case 27 ' RR E op = rr(peekb(z)) setE(op) pokeb(z, op) Case 28 ' RR H op = rr(peekb(z)) setH(op) pokeb(z, op) Case 29 ' RR L op = rr(peekb(z)) setL(op) pokeb(z, op) Case 30 ' RR (HL) pokeb(z, rr(peekb(z))) Case 31 ' RR A op = rr(peekb(z)) regA = op pokeb(z, op) Case 32 ' SLA B op = sla(peekb(z)) regB = op pokeb(z, op) Case 33 ' SLA C op = sla(peekb(z)) regC = op pokeb(z, op) Case 34 ' SLA D op = sla(peekb(z)) setD(op) pokeb(z, op) Case 35 ' SLA E op = sla(peekb(z)) setE(op) pokeb(z, op) Case 36 ' SLA H op = sla(peekb(z)) setH(op) pokeb(z, op) Case 37 ' SLA L op = sla(peekb(z)) setL(op) pokeb(z, op) Case 38 ' SLA (HL) pokeb(z, sla(peekb(z))) Case 39 ' SLA A op = sla(peekb(z)) regA = op pokeb(z, op) Case 40 ' SRA B op = sra(peekb(z)) regB = op pokeb(z, op) Case 41 ' SRA C op = sra(peekb(z)) regC = op pokeb(z, op) Case 42 ' SRA D op = sra(peekb(z)) setD(op) pokeb(z, op) Case 43 ' SRA E op = sra(peekb(z)) setE(op) pokeb(z, op) Case 44 ' SRA H op = sra(peekb(z)) setH(op) pokeb(z, op) Case 45 ' SRA L op = sra(peekb(z)) setL(op) pokeb(z, op) Case 46 ' SRA (HL) pokeb(z, sra(peekb(z))) Case 47 ' SRA A op = sra(peekb(z)) regA = op pokeb(z, op) Case 48 ' SLS B op = sls(peekb(z)) regB = op pokeb(z, op) Case 49 ' SLS C op = sls(peekb(z)) regC = op pokeb(z, op) Case 50 ' SLS D op = sls(peekb(z)) setD(op) pokeb(z, op) Case 51 ' SLS E op = sls(peekb(z)) setE(op) pokeb(z, op) Case 52 ' SLS H op = sls(peekb(z)) setH(op) pokeb(z, op) Case 53 ' SLS L op = sls(peekb(z)) setL(op) pokeb(z, op) Case 54 ' SLS (HL) pokeb(z, sls(peekb(z))) Case 55 ' SLS A op = sls(peekb(z)) regA = op pokeb(z, op) Case 56 ' SRL B op = srl(peekb(z)) regB = op pokeb(z, op) Case 57 ' SRL C op = srl(peekb(z)) regC = op pokeb(z, op) Case 58 ' SRL D op = srl(peekb(z)) setD(op) pokeb(z, op) Case 59 ' SRL E op = srl(peekb(z)) setE(op) pokeb(z, op) Case 60 ' SRL H op = srl(peekb(z)) setH(op) pokeb(z, op) Case 61 ' SRL L op = srl(peekb(z)) setL(op) pokeb(z, op) Case 62 ' SRL (ID) pokeb(z, srl(peekb(z))) Case 63 ' SRL A op = srl(peekb(z)) regA = op pokeb(z, op) Case 64 To 71 ' 064 to 071 BIT 0,B bit(&H1, peekb(z)) Case 72 To 79 ' 072 to 079 BIT 1,B bit(&H2, peekb(z)) Case 80 To 87 ' 080 To 087 BIT 2,B bit(&H4, peekb(z)) Case 88 To 95 ' 088 to 095 BIT 3,B bit(&H8, peekb(z)) Case 96 To 103 ' 096 to 103 BIT 4,B bit(&H10, peekb(z)) Case 104 To 111 ' 104 to 111 BIT 5,B bit(&H20, peekb(z)) Case 112 To 119 ' 112 To 119 BIT 6,B bit(&H40, peekb(z)) Case 120 To 127 ' 120 to 127 BIT 7,B bit(&H80, peekb(z)) Case 128 ' RES 0,(ID+y)->B regB = bitRes(1, peekb(z)) pokeb(z, regB) Case 129 ' RES 0,(ID+y)->C regC = bitRes(1, peekb(z)) pokeb(z, regC) Case 130 ' RES 0,(ID+y)->D setD(bitRes(1, peekb(z))) pokeb(z, HighByte(regDE)) Case 131 ' RES 0,(ID+y)->E setE(bitRes(1, peekb(z))) pokeb(z, getE) Case 132 ' RES 0,(ID+y)->H setH(bitRes(1, peekb(z))) pokeb(z, HighByte(regHL)) Case 133 ' RES 0,(ID+y)->L setL(bitRes(1, peekb(z))) pokeb(z, regHL And &HFF) Case 134 ' RES 0,(HL) pokeb(z, bitRes(&H1, peekb(z))) Case 135 ' RES 0,(ID+y)->A regA = bitRes(1, peekb(z)) pokeb(z, regA) Case 136 ' RES 1,(ID+y)->B regB = bitRes(2, peekb(z)) pokeb(z, regB) Case 137 ' RES 1,(ID+y)->C regC = bitRes(2, peekb(z)) pokeb(z, regC) Case 138 ' RES 1,(ID+y)->D setD(bitRes(2, peekb(z))) pokeb(z, HighByte(regDE)) Case 139 ' RES 1,(ID+y)->E setE(bitRes(2, peekb(z))) pokeb(z, getE) Case 140 ' RES 1,(ID+y)->H setH(bitRes(2, peekb(z))) pokeb(z, HighByte(regHL)) Case 141 ' RES 1,(ID+y)->L setL(bitRes(2, peekb(z))) pokeb(z, getL) Case 142 ' RES 1,(HL) pokeb(z, bitRes(&H2, peekb(z))) Case 143 ' RES 1,(ID+y)->A regA = bitRes(2, peekb(z)) pokeb(z, regA) Case 144 ' RES 2,(ID+y)->B regB = bitRes(4, peekb(z)) pokeb(z, regB) Case 145 ' RES 2,(ID+y)->C regC = bitRes(4, peekb(z)) pokeb(z, regC) Case 146 ' RES 2,(ID+y)->D setD(bitRes(4, peekb(z))) pokeb(z, HighByte(regDE)) Case 147 ' RES 2,(ID+y)->E setE(bitRes(4, peekb(z))) pokeb(z, getE) Case 148 ' RES 2,(ID+y)->H setH(bitRes(4, peekb(z))) pokeb(z, HighByte(regHL)) Case 149 ' RES 2,(ID+y)->L setL(bitRes(4, peekb(z))) pokeb(z, getL) Case 150 ' RES 2,(HL) pokeb(z, bitRes(&H4, peekb(z))) Case 151 ' RES 2,(ID+y)->A regA = bitRes(4, peekb(z)) pokeb(z, regA) Case 152 ' RES 3,(ID+y)->B regB = bitRes(8, peekb(z)) pokeb(z, regB) Case 153 ' RES 3,(ID+y)->C regC = bitRes(8, peekb(z)) pokeb(z, regC) Case 154 ' RES 3,(ID+y)->D setD(bitRes(8, peekb(z))) pokeb(z, HighByte(regDE)) Case 155 ' RES 3,(ID+y)->E setE(bitRes(8, peekb(z))) pokeb(z, getE) Case 156 ' RES 3,(ID+y)->H setH(bitRes(8, peekb(z))) pokeb(z, HighByte(regHL)) Case 157 ' RES 3,(ID+y)->L setL(bitRes(8, peekb(z))) pokeb(z, getL) Case 158 ' RES 3,(HL) pokeb(z, bitRes(&H8, peekb(z))) Case 159 ' RES 3,(ID+y)->A regA = bitRes(8, peekb(z)) pokeb(z, regA) Case 160 ' RES 4,(ID+y)->B regB = bitRes(&H10, peekb(z)) pokeb(z, regB) Case 161 ' RES 4,(ID+y)->C regC = bitRes(&H10, peekb(z)) pokeb(z, regC) Case 162 ' RES 4,(ID+y)->D setD(bitRes(&H10, peekb(z))) pokeb(z, HighByte(regDE)) Case 163 ' RES 4,(ID+y)->E setE(bitRes(&H10, peekb(z))) pokeb(z, getE) Case 164 ' RES 4,(ID+y)->H setH(bitRes(&H10, peekb(z))) pokeb(z, HighByte(regHL)) Case 165 ' RES 4,(ID+y)->L setL(bitRes(&H10, peekb(z))) pokeb(z, getL) Case 166 ' RES 4,(HL) pokeb(z, bitRes(&H10, peekb(z))) Case 167 ' RES 4,(ID+y)->A regA = bitRes(&H10, peekb(z)) pokeb(z, regA) Case 168 ' RES 5,(ID+y)->B regB = bitRes(&H20, peekb(z)) pokeb(z, regB) Case 169 ' RES 5,(ID+y)->C regC = bitRes(&H20, peekb(z)) pokeb(z, regC) Case 170 ' RES 5,(ID+y)->D setD(bitRes(&H20, peekb(z))) pokeb(z, HighByte(regDE)) Case 171 ' RES 5,(ID+y)->E setE(bitRes(&H20, peekb(z))) pokeb(z, getE) Case 172 ' RES 5,(ID+y)->H setH(bitRes(&H20, peekb(z))) pokeb(z, HighByte(regHL)) Case 173 ' RES 5,(ID+y)->L setL(bitRes(&H20, peekb(z))) pokeb(z, getL) Case 174 ' RES 5,(HL) pokeb(z, bitRes(&H20, peekb(z))) Case 175 ' RES 5,(ID+y)->A regA = bitRes(&H20, peekb(z)) pokeb(z, regA) Case 176 ' RES 6,(ID+y)->B regB = bitRes(&H40, peekb(z)) pokeb(z, regB) Case 177 ' RES 6,(ID+y)->C regC = bitRes(&H40, peekb(z)) pokeb(z, regC) Case 178 ' RES 6,(ID+y)->D setD(bitRes(&H40, peekb(z))) pokeb(z, HighByte(regDE)) Case 179 ' RES 6,(ID+y)->E setE(bitRes(&H40, peekb(z))) pokeb(z, getE) Case 180 ' RES 6,(ID+y)->H setH(bitRes(&H40, peekb(z))) pokeb(z, HighByte(regHL)) Case 181 ' RES 6,(ID+y)->L setL(bitRes(&H40, peekb(z))) pokeb(z, getL) Case 182 ' RES 6,(HL) pokeb(z, bitRes(&H40, peekb(z))) Case 183 ' RES 6,(ID+y)->A regA = bitRes(&H40, peekb(z)) pokeb(z, regA) Case 184 ' RES 6,(ID+y)->B regB = bitRes(&H80, peekb(z)) pokeb(z, regB) Case 185 ' RES 6,(ID+y)->C regC = bitRes(&H80, peekb(z)) pokeb(z, regC) Case 186 ' RES 6,(ID+y)->D setD(bitRes(&H80, peekb(z))) pokeb(z, HighByte(regDE)) Case 187 ' RES 6,(ID+y)->E setE(bitRes(&H80, peekb(z))) pokeb(z, getE) Case 188 ' RES 6,(ID+y)->H setH(bitRes(&H80, peekb(z))) pokeb(z, HighByte(regHL)) Case 189 ' RES 6,(ID+y)->L setL(bitRes(&H80, peekb(z))) pokeb(z, getL) Case 190 ' RES 7,(HL) pokeb(z, bitRes(&H80, peekb(z))) Case 191 ' RES 7,(ID+y)->A regA = bitRes(&H80, peekb(z)) pokeb(z, regA) Case 192 ' SET 0,(ID+y)->B regB = bitSet(1, peekb(z)) pokeb(z, regB) Case 193 ' SET 0,(ID+y)->C regC = bitSet(1, peekb(z)) pokeb(z, regC) Case 194 ' SET 0,(ID+y)->D setD(bitSet(1, peekb(z))) pokeb(z, HighByte(regDE)) Case 195 ' SET 0,(ID+y)->E setE(bitSet(1, peekb(z))) pokeb(z, getE) Case 196 ' SET 0,(ID+y)->H setH(bitSet(1, peekb(z))) pokeb(z, HighByte(regHL)) Case 197 ' SET 0,(ID+y)->L setL(bitSet(1, peekb(z))) pokeb(z, getL) Case 198 ' SET 0,(HL) pokeb(z, bitSet(&H1, peekb(z))) Case 199 ' SET 0,(ID+y)->A regA = bitSet(1, peekb(z)) pokeb(z, regA) Case 200 ' SET 1,(ID+y)->B regB = bitSet(2, peekb(z)) pokeb(z, regB) Case 201 ' SET 1,(ID+y)->C regC = bitSet(2, peekb(z)) pokeb(z, regC) Case 202 ' SET 1,(ID+y)->D setD(bitSet(2, peekb(z))) pokeb(z, HighByte(regDE)) Case 203 ' SET 1,(ID+y)->E setE(bitSet(2, peekb(z))) pokeb(z, getE) Case 204 ' SET 1,(ID+y)->H setH(bitSet(2, peekb(z))) pokeb(z, HighByte(regHL)) Case 205 ' SET 1,(ID+y)->L setL(bitSet(2, peekb(z))) pokeb(z, getL) Case 206 ' SET 1,(HL) pokeb(z, bitSet(&H2, peekb(z))) Case 207 ' SET 1,(ID+y)->A regA = bitSet(2, peekb(z)) pokeb(z, regA) Case 208 ' SET 2,(ID+y)->B regB = bitSet(4, peekb(z)) pokeb(z, regB) Case 209 ' SET 2,(ID+y)->C regC = bitSet(4, peekb(z)) pokeb(z, regC) Case 210 ' SET 2,(ID+y)->D setD(bitSet(4, peekb(z))) pokeb(z, HighByte(regDE)) Case 211 ' SET 2,(ID+y)->E setE(bitSet(4, peekb(z))) pokeb(z, getE) Case 212 ' SET 2,(ID+y)->H setH(bitSet(4, peekb(z))) pokeb(z, HighByte(regHL)) Case 213 ' SET 2,(ID+y)->L setL(bitSet(4, peekb(z))) pokeb(z, getL) Case 214 ' SET 2,(HL) pokeb(z, bitSet(&H4, peekb(z))) Case 215 ' SET 2,(ID+y)->A regA = bitSet(4, peekb(z)) pokeb(z, regA) Case 216 ' SET 3,(ID+y)->B regB = bitSet(8, peekb(z)) pokeb(z, regB) Case 217 ' SET 3,(ID+y)->C regC = bitSet(8, peekb(z)) pokeb(z, regC) Case 218 ' SET 3,(ID+y)->D setD(bitSet(8, peekb(z))) pokeb(z, HighByte(regDE)) Case 219 ' SET 3,(ID+y)->E setE(bitSet(8, peekb(z))) pokeb(z, getE) Case 220 ' SET 3,(ID+y)->H setH(bitSet(8, peekb(z))) pokeb(z, HighByte(regHL)) Case 221 ' SET 3,(ID+y)->L setL(bitSet(8, peekb(z))) pokeb(z, getL) Case 222 ' SET 3,(HL) pokeb(z, bitSet(&H8, peekb(z))) Case 223 ' SET 3,(ID+y)->A regA = bitSet(8, peekb(z)) pokeb(z, regA) Case 224 ' SET 4,(ID+y)->B regB = bitSet(&H10, peekb(z)) pokeb(z, regB) Case 225 ' SET 4,(ID+y)->C regC = bitSet(&H10, peekb(z)) pokeb(z, regC) Case 226 ' SET 4,(ID+y)->D setD(bitSet(&H10, peekb(z))) pokeb(z, HighByte(regDE)) Case 227 ' SET 4,(ID+y)->E setE(bitSet(&H10, peekb(z))) pokeb(z, getE) Case 228 ' SET 4,(ID+y)->H setH(bitSet(&H10, peekb(z))) pokeb(z, HighByte(regHL)) Case 229 ' SET 4,(ID+y)->L setL(bitSet(&H10, peekb(z))) pokeb(z, getL) Case 230 ' SET 4,(HL) pokeb(z, bitSet(&H10, peekb(z))) Case 231 ' SET 4,(ID+y)->A regA = bitSet(&H10, peekb(z)) pokeb(z, regA) Case 232 ' SET 5,(ID+y)->B regB = bitSet(&H20, peekb(z)) pokeb(z, regB) Case 233 ' SET 5,(ID+y)->C regC = bitSet(&H20, peekb(z)) pokeb(z, regC) Case 234 ' SET 5,(ID+y)->D setD(bitSet(&H20, peekb(z))) pokeb(z, HighByte(regDE)) Case 235 ' SET 5,(ID+y)->E setE(bitSet(&H20, peekb(z))) pokeb(z, getE) Case 236 ' SET 5,(ID+y)->H setH(bitSet(&H20, peekb(z))) pokeb(z, HighByte(regHL)) Case 237 ' SET 5,(ID+y)->L setL(bitSet(&H20, peekb(z))) pokeb(z, getL) Case 238 ' SET 5,(HL) pokeb(z, bitSet(&H20, peekb(z))) Case 239 ' SET 5,(ID+y)->A regA = bitSet(&H20, peekb(z)) pokeb(z, regA) Case 240 ' SET 6,(ID+y)->B regB = bitSet(&H40, peekb(z)) pokeb(z, regB) Case 241 ' SET 6,(ID+y)->C regC = bitSet(&H40, peekb(z)) pokeb(z, regC) Case 242 ' SET 6,(ID+y)->D setD(bitSet(&H40, peekb(z))) pokeb(z, HighByte(regDE)) Case 243 ' SET 6,(ID+y)->E setE(bitSet(&H40, peekb(z))) pokeb(z, getE) Case 244 ' SET 6,(ID+y)->H setH(bitSet(&H40, peekb(z))) pokeb(z, HighByte(regHL)) Case 245 ' SET 6,(ID+y)->L setL(bitSet(&H40, peekb(z))) pokeb(z, getL) Case 246 ' SET 6,(HL) pokeb(z, bitSet(&H40, peekb(z))) Case 247 ' SET 6,(ID+y)->A regA = bitSet(&H40, peekb(z)) pokeb(z, regA) Case 248 ' SET 7,(ID+y)->B regB = bitSet(&H80, peekb(z)) pokeb(z, regB) Case 249 ' SET 7,(ID+y)->C regC = bitSet(&H80, peekb(z)) pokeb(z, regC) Case 250 ' SET 7,(ID+y)->D setD(bitSet(&H80, peekb(z))) pokeb(z, HighByte(regDE)) Case 251 ' SET 7,(ID+y)->E setE(bitSet(&H80, peekb(z))) pokeb(z, getE) Case 252 ' SET 7,(ID+y)->H setH(bitSet(&H80, peekb(z))) pokeb(z, HighByte(regHL)) Case 253 ' SET 7,(ID+y)->L setL(bitSet(&H80, peekb(z))) pokeb(z, getL) Case 254 ' SET 7,(HL) pokeb(z, bitSet(&H80, peekb(z))) Case 255 ' SET 7,A regA = bitSet(&H80, peekb(z)) pokeb(z, regA) End Select End Sub Private Sub exx() Dim t As Long t = regHL regHL = regHL_ regHL_ = t t = regDE regDE = regDE_ regDE_ = t t = ((regB << 8) Or regC) setBC(regBC_) regBC_ = t End Sub Public Function getAF() As Long getAF = (regA << 8) Or getF() End Function Private Function getBC() As Long getBC = (regB << 8) Or regC End Function Private Function getD() As Long getD = HighByte(regDE) End Function Private Function getE() As Long getE = regDE And &HFF End Function Public Function getF() As Long ' // Another of Gonchucki's performance improvements ' // I've modified it slightly to use the "-(fS)" to turn ' // the boolean values into 0/1 rather than the slower ' // "Abs(fS)" method getF = (-(fS) * F_S) Or _ (-(fZ) * F_Z) Or _ (-(f5) * F_5) Or _ (-(fH) * F_H) Or _ (-(f3) * F_3) Or _ (-(fPV) * F_PV) Or _ (-(fN) * F_N) Or _ (-(fC) * F_C) ' If fS Then getF = getF Or F_S ' If fZ Then getF = getF Or F_Z ' If f5 Then getF = getF Or F_5 ' If fH Then getF = getF Or F_H ' If f3 Then getF = getF Or F_3 ' If fPV Then getF = getF Or F_PV ' If fN Then getF = getF Or F_N ' If fC Then getF = getF Or F_C End Function Private Function getH() As Long getH = HighByte(regHL) End Function Private Function getIDH() As Long getIDH = HighByte(regID) End Function Private Function getIDL() As Long getIDL = regID And &HFF End Function Private Function getL() As Long getL = regHL And &HFF End Function Private Function id_d() As Long Dim d As Long d = nxtpcb() If d And 128 Then d = -(256 - d) id_d = (regID + d) And &HFFFF End Function Private Sub ld_a_i() fS = (intI And F_S) <> 0 f3 = (intI And F_3) <> 0 f5 = (intI And F_5) <> 0 fZ = (intI = 0) fPV = intIFF2 fH = False fN = False regA = intI End Sub Private Sub ld_a_r() intRTemp = intRTemp And &H7F regA = (intR And &H80) Or intRTemp fS = (regA And F_S) <> 0 f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fZ = (regA = 0) fPV = intIFF2 fH = False fN = False End Sub Private Sub neg_a() Dim t As Long t = regA regA = 0 sub_a(t) End Sub Private Sub rld_a() Dim t As Long, q As Long t = peekb(regHL) q = t t = (t * 16) Or (regA And &HF) regA = (regA And &HF0) Or (q \ 16) pokeb(regHL, (t And &HFF)) fS = (regA And F_S) <> 0 f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fZ = (regA = 0) fPV = intIFF2 fH = False fN = False End Sub Private Sub setIDH(ByVal byteval As Long) regID = ((byteval << 8) And &HFF00) Or (regID And &HFF) End Sub Private Sub setIDL(ByVal byteval As Long) regID = (regID And &HFF00) Or (byteval And &HFF) End Sub Private Function in_bc() As Long Dim ans As Long ans = Inb((regB << 8) Or regC) fZ = (ans = 0) fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fPV = Parity(ans) fN = False fH = False in_bc = ans End Function Private Function inc8(ByVal ans As Long) As Long fPV = (ans = &H7F) fH = (((ans And &HF) + 1) And F_H) <> 0 ans = (ans + 1) And &HFF fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fN = False inc8 = ans End Function Private Function dec8(ByVal ans As Long) As Long fPV = (ans = &H80) fH = (((ans And &HF) - 1) And F_H) <> 0 ans = (ans - 1) And &HFF fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fN = True dec8 = ans End Function Function interrupt() As Long 'Dim lSleep As Long, lCounter As Long If intIFF1 = False Then interrupt = 0 Else Select Case intIM Case 0, 1 pushpc() intIFF1 = False intIFF2 = False regPC = 56 interrupt = 13 Case 2 pushpc() intIFF1 = False intIFF2 = False regPC = peekw((intI << 8) Or &HFF) interrupt = 19 Case Else interrupt = 0 ' never does this but needs this otherwise vb.net gives an error End Select End If End Function Private Sub or_a(ByVal b As Long) regA = (regA Or b) fS = (regA And F_S) <> 0 f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fH = False fPV = Parity(regA) fZ = (regA = 0) fN = False fC = False End Sub Private Function peekw(ByVal addr As Long) As Long peekw = peekb(addr) Or peekb(addr + 1) << 8 ' bit shift faster than * 256 End Function Public Sub pokeb(ByVal addr As Long, ByVal newByte As Long) Z80Memory(addr) = newByte End Sub Private Sub pokew(ByVal addr As Long, ByVal word As Long) pokeb(addr, word And &HFF) pokeb(addr + 1, HighByte((word And &HFF00))) End Sub Public Sub poppc() regPC = peekb(regSP) Or (peekb(regSP + 1) << 8) regSP = (regSP + 2 And &HFFFF) End Sub Private Function popw() As Long popw = peekb(regSP) Or (peekb(regSP + 1) << 8) regSP = (regSP + 2 And &HFFFF) End Function Public Sub pushpc() regSP = (regSP - 2) And &HFFFF pokew(regSP, regPC) End Sub Private Sub pushw(ByVal word As Long) regSP = (regSP - 2) And &HFFFF pokew(regSP, word) End Sub 'Private Sub REFRESH(ByVal t As Long) ' intRTemp = intRTemp + t 'End Sub Private Function rl(ByVal ans As Long) As Long Dim c As Long c = (ans And &H80) <> 0 rl = ((ans * 2) Or -(fC)) And &HFF fS = (rl And F_S) <> 0 f3 = (rl And F_3) <> 0 f5 = (rl And F_5) <> 0 fZ = (rl = 0) fPV = Parity(rl) fH = False fN = False fC = c End Function Private Sub rl_a() Dim c As Long c = (regA And &H80) <> 0 regA = ((regA * 2) Or -(fC)) And &HFF f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fN = False fH = False fC = c End Sub Private Function rlc(ByVal ans As Long) As Long fC = (ans And &H80) <> 0 rlc = ((ans * 2) Or -(fC)) And &HFF fS = (rlc And F_S) <> 0 f3 = (rlc And F_3) <> 0 f5 = (rlc And F_5) <> 0 fZ = (rlc = 0) fPV = Parity(rlc) fH = False fN = False End Function Private Sub rlc_a() fC = (regA And &H80) <> 0 regA = ((regA * 2) Or -(fC)) And &HFF f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fN = False fH = False End Sub Private Function rr(ByVal ans As Long) As Long Dim c As Long c = (ans And &H1) <> 0 rr = (ans \ 2) Or (&H80 * -(fC)) fS = (rr And F_S) <> 0 f3 = (rr And F_3) <> 0 f5 = (rr And F_5) <> 0 fZ = (rr = 0) fPV = Parity(rr) fH = False fN = False fC = c End Function Private Sub rr_a() Dim c As Long c = (regA And &H1) <> 0 regA = (regA \ 2) Or (&H80 * -(fC)) f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fN = False fH = False fC = c End Sub Private Function rrc(ByVal ans As Long) As Long fC = (ans And &H1) <> 0 rrc = (ans \ 2) Or (&H80 * -(fC)) fS = (rrc And F_S) <> 0 f3 = (rrc And F_3) <> 0 f5 = (rrc And F_5) <> 0 fZ = (rrc = 0) fPV = Parity(rrc) fH = False fN = False End Function Private Sub rrd_a() Dim t As Long, q As Long t = peekb(regHL) q = t t = (t \ 16) Or (regA * 16) regA = (regA And &HF0) Or (q And &HF) pokeb(regHL, t) fS = (regA And F_S) <> 0 f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fZ = (regA = 0) fPV = intIFF2 fH = False fN = False End Sub Private Sub rrc_a() fC = (regA And &H1) <> 0 regA = (regA \ 2) Or (&H80 * -(fC)) f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fN = False fH = False End Sub Private Sub sbc_a(ByVal b As Long) Dim ans As Long, c As Long c = -(fC) ans = (regA - b - c) And &HFF fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fC = ((regA - b - c) And &H100) <> 0 fPV = ((regA Xor b) And (regA Xor ans) And &H80) <> 0 fH = (((regA And &HF) - (b And &HF) - c) And F_H) <> 0 fN = True regA = ans End Sub Private Function sbc16(ByVal a As Long, ByVal b As Long) As Long Dim c As Long, ans As Long c = -(fC) ans = (a - b - c) And &HFFFF fS = (ans And (F_S << 8)) <> 0 f3 = (ans And (F_3 << 8)) <> 0 f5 = (ans And (F_5 << 8)) <> 0 fZ = (ans = 0) fC = ((a - b - c) And &H10000) <> 0 fPV = ((a Xor b) And (a Xor ans) And &H8000) <> 0 fH = (((a And &HFFF) - (b And &HFFF) - c) And &H1000) <> 0 fN = True sbc16 = ans End Function Private Sub scf() f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fN = False fH = False fC = True End Sub Public Sub setAF(ByVal v As Long) regA = HighByte(v And &HFF00) setF(v And &HFF) End Sub Public Sub setBC(ByVal nn As Long) regB = HighByte(nn And &HFF00) regC = nn And &HFF End Sub Public Sub execute() Dim d As Long, lTemp As Long Dim xxx As Long, lTStates As Long ' if comment out the 1TStates lines, watch for lines like lTStates = execute_id() xxx = Z80Memory(regPC) regPC = regPC + 1 Select Case xxx Case 0 ' NOP lTStates = 4 Case 1 ' 001 LD BC,nn setBC(nxtpcw()) lTStates = 10 Case 2 ' 002 LD (BC),A pokeb(((regB << 8) Or regC), regA) lTStates = 7 Case 3 ' 003 INC BC setBC(((regB << 8) Or regC) + 1 And &HFFFF) lTStates = 6 Case 4 ' 004 INC B regB = inc8(regB) lTStates = 4 Case 5 ' 005 DEC B regB = dec8(regB) lTStates = 4 Case 6 ' 006 LD B,n regB = nxtpcb() lTStates = 7 Case 7 ' 007 RLCA rlc_a() lTStates = 4 Case 8 ' 008 EX AF,AF' ex_af_af() lTStates = 4 Case 9 '009 ADD HL,BC regHL = add16(regHL, (regB << 8) Or regC) lTStates = 11 Case 10 ' 010 LD A,(BC) regA = peekb((regB << 8) Or regC) lTStates = 7 Case 11 ' 011 DEC BC setBC(dec16((regB << 8) Or regC)) lTStates = 6 Case 12 ' 012 INC C regC = inc8(regC) lTStates = 4 Case 13 ' 013 DEC C regC = dec8(regC) lTStates = 4 Case 14 ' 014 LD C,n regC = nxtpcb() lTStates = 7 Case 15 ' 015 RRCA rrc_a() lTStates = 4 Case 16 ' 016 DJNZ dis regB = (regB - 1) And &HFF If regB <> 0 Then d = nxtpcb() If d And 128 Then d = -(256 - d) regPC = (regPC + d) And &HFFFF lTStates = 13 Else regPC = (regPC + 1) And &HFFFF lTStates = 8 End If Case 17 ' 017 LD DE,nn regDE = nxtpcw() lTStates = 10 Case 18 ' 018 LD (DE),A pokeb(regDE, regA) lTStates = 7 Case 19 ' 019 INC DE regDE = (regDE + 1) And &HFFFF lTStates = 6 Case 20 ' 020 INC D setD(inc8(HighByte(regDE))) lTStates = 4 Case 21 ' 021 DEC D setD(dec8(HighByte(regDE))) lTStates = 4 Case 22 ' 022 LD D,n setD(nxtpcb()) lTStates = 7 Case 23 ' 023 ' RLA rl_a() lTStates = 4 Case 24 ' 024 JR dis d = nxtpcb() If d And 128 Then d = -(256 - d) regPC = (regPC + d) And &HFFFF lTStates = 12 Case 25 ' 025 ADD HL,DE regHL = add16(regHL, regDE) lTStates = 11 Case 26 ' 026 LD A,(DE) regA = peekb(regDE) lTStates = 7 Case 27 ' 027 DEC DE regDE = dec16(regDE) lTStates = 6 Case 28 ' 028 INC E setE(inc8(getE)) lTStates = 4 Case 29 ' 029 DEC E setE(dec8(getE)) lTStates = 4 Case 30 ' 030 LD E,n setE(nxtpcb()) lTStates = 7 Case 31 ' 031 RRA rr_a() lTStates = 4 Case 32 ' 032 JR NZ dis If fZ = False Then d = nxtpcb() If d And 128 Then d = -(256 - d) regPC = ((regPC + d) And &HFFFF) lTStates = 12 Else regPC = (regPC + 1) And &HFFFF lTStates = 7 End If Case 33 ' 033 LD HL,nn regHL = nxtpcw() lTStates = 10 Case 34 ' 034 LD (nn),HL pokew(nxtpcw(), regHL) lTStates = 16 Case 35 ' 035 INC HL regHL = (regHL + 1) And &HFFFF lTStates = 6 Case 36 ' 036 INC H setH(inc8(HighByte(regHL))) lTStates = 4 Case 37 ' 037 DEC H setH(dec8(HighByte(regHL))) lTStates = 4 Case 38 ' 038 LD H,n setH(nxtpcb()) lTStates = 7 Case 39 ' 039 DAA daa_a() lTStates = 4 Case 40 ' 040 JR Z dis If fZ = True Then d = nxtpcb() If d And 128 Then d = -(256 - d) regPC = ((regPC + d) And &HFFFF) lTStates = 12 Else regPC = (regPC + 1) And &HFFFF lTStates = 7 End If Case 41 ' 041 ADD HL,HL regHL = add16(regHL, regHL) lTStates = 11 Case 42 ' 042 LD HL,(nn) regHL = peekw(nxtpcw()) lTStates = 16 Case 43 ' 043 DEC HL regHL = dec16(regHL) lTStates = 6 Case 44 ' 044 INC L setL(inc8(regHL And &HFF)) lTStates = 4 Case 45 ' 045 DEC L setL(dec8(regHL And &HFF)) lTStates = 4 Case 46 ' 046 LD L,n setL(nxtpcb()) lTStates = 7 Case 47 ' 047 CPL cpl_a() lTStates = 4 Case 48 ' 048 JR NC dis If fC = False Then d = nxtpcb() If d And 128 Then d = -(256 - d) regPC = ((regPC + d) And &HFFFF) lTStates = 12 Else regPC = (regPC + 1) And &HFFFF lTStates = 7 End If Case 49 ' 049 LD SP,nn regSP = nxtpcw() lTStates = 10 Case 50 ' 050 LD (nn),A pokeb(nxtpcw, regA) lTStates = 13 Case 51 ' 051 INC SP regSP = (regSP + 1) And &HFFFF lTStates = 6 Case 52 ' 052 INC (HL) pokeb(regHL, inc8(peekb(regHL))) lTStates = 11 Case 53 ' 053 DEC (HL) pokeb(regHL, dec8(peekb(regHL))) lTStates = 11 Case 54 ' 054 LD (HL),n pokeb(regHL, nxtpcb()) lTStates = 10 Case 55 ' 055 SCF scf() lTStates = 4 Case 56 ' 056 JR C dis If fC = True Then d = nxtpcb() If d And 128 Then d = -(256 - d) regPC = ((regPC + d) And &HFFFF) lTStates = 12 Else regPC = (regPC + 1) And &HFFFF lTStates = 7 End If Case 57 ' 057 ADD HL,SP regHL = add16(regHL, regSP) lTStates = 11 Case 58 ' 058 LD A,(nn) regA = peekb(nxtpcw()) lTStates = 13 Case 59 ' 059 DEC SP regSP = dec16(regSP) lTStates = 6 Case 60 ' 060 INC A regA = inc8(regA) lTStates = 4 Case 61 ' 061 DEC A regA = dec8(regA) lTStates = 4 Case 62 ' 062 LD A,n regA = nxtpcb() lTStates = 7 Case 63 ' 063 CCF ccf() lTStates = 4 Case 64 ' LD B,B lTStates = 4 Case 65 ' 65 LD B,C regB = regC lTStates = 4 Case 66 ' 66 LD B,D regB = HighByte(regDE) lTStates = 4 Case 67 ' 67 LD B,E regB = getE() lTStates = 4 Case 68 ' 68 LD B,H regB = HighByte(regHL) lTStates = 4 Case 69 ' 69 LD B,L regB = (regHL And &HFF) lTStates = 4 Case 70 ' 70 LD B,(HL) regB = peekb(regHL) lTStates = 7 Case 71 ' 71 LD B,A regB = regA lTStates = 4 Case 72 ' 72 LD C,B regC = regB lTStates = 4 Case 73 ' 73 LD C,C lTStates = 4 Case 74 ' 74 LD C,D regC = HighByte(regDE) lTStates = 4 Case 75 ' 75 LD C,E regC = getE() lTStates = 4 Case 76 ' 76 LD C,H regC = HighByte(regHL) lTStates = 4 Case 77 ' 77 LD C,L regC = (regHL And &HFF) lTStates = 4 Case 78 ' 78 LD C,(HL) regC = peekb(regHL) lTStates = 7 Case 79 ' 79 LD C,A regC = regA lTStates = 4 Case 80 ' 80 LD D,B setD(regB) lTStates = 4 Case 81 ' 81 LD D,C setD(regC) lTStates = 4 Case 82 ' 82 LD D,D lTStates = 4 Case 83 ' 83 LD D,E setD(getE) lTStates = 4 Case 84 ' 84 LD D,H setD(HighByte(regHL)) lTStates = 4 Case 85 ' 85 LD D,L setD(regHL And &HFF) lTStates = 4 Case 86 ' 86 LD D,(HL) setD(peekb(regHL)) lTStates = 7 Case 87 ' 87 LD D,A setD(regA) lTStates = 4 Case 88 ' 88 LD E,B setE(regB) lTStates = 4 Case 89 ' 89 LD E,C setE(regC) lTStates = 4 Case 90 ' 90 LD E,D setE(HighByte(regDE)) lTStates = 4 Case 91 ' 91 LD E,E lTStates = 4 Case 92 ' 92 LD E,H setE(HighByte(regHL)) lTStates = 4 Case 93 ' 93 LD E,L setE(regHL And &HFF) lTStates = 4 Case 94 ' 94 LD E,(HL) setE(peekb(regHL)) lTStates = 7 Case 95 ' 95 LD E,A setE(regA) lTStates = 4 Case 96 ' 96 LD H,B setH(regB) lTStates = 4 Case 97 ' 97 LD H,C setH(regC) lTStates = 4 Case 98 ' 98 LD H,D setH(HighByte(regDE)) lTStates = 4 Case 99 ' 99 LD H,E setH(getE) lTStates = 4 Case 100 ' 100 LD H,H lTStates = 4 Case 101 ' 101 LD H,L setH(regHL And &HFF) lTStates = 4 Case 102 ' 102 LD H,(HL) setH(peekb(regHL)) lTStates = 7 Case 103 ' 103 LD H,A setH(regA) lTStates = 4 Case 104 ' 104 LD L,B setL(regB) lTStates = 4 Case 105 ' 105 LD L,C setL(regC) lTStates = 4 Case 106 ' 106 LD L,D setL(HighByte(regDE)) lTStates = 4 Case 107 ' 107 LD L,E setL(getE) lTStates = 4 Case 108 ' 108 LD L,H setL(HighByte(regHL)) lTStates = 4 Case 109 ' 109 LD L,L lTStates = 4 Case 110 ' 110 LD L,(HL) setL(peekb(regHL)) lTStates = 7 Case 111 ' 111 LD L,A setL(regA) lTStates = 4 Case 112 ' 112 LD (HL),B pokeb(regHL, regB) lTStates = 7 Case 113 ' 113 LD (HL),C pokeb(regHL, regC) lTStates = 7 Case 114 ' 114 LD (HL),D pokeb(regHL, HighByte(regDE)) lTStates = 7 Case 115 ' 115 LD (HL),E pokeb(regHL, getE) lTStates = 7 Case 116 ' 116 LD (HL),H pokeb(regHL, HighByte(regHL)) lTStates = 7 Case 117 ' 117 LD (HL),L pokeb(regHL, (regHL And &HFF)) lTStates = 7 Case 118 ' 118 HALT regPC = regPC - 1 MsgBox("Halt at PC ", regPC) lTStates = 4 Case 119 ' 119 LD (HL),A pokeb(regHL, regA) lTStates = 7 Case 120 ' 120 LD A,B regA = regB lTStates = 4 Case 121 ' 121 LD A,C regA = regC lTStates = 4 Case 122 ' 122 LD A,D regA = HighByte(regDE) lTStates = 4 Case 123 ' 123 LD A,E regA = getE() lTStates = 4 Case 124 ' 124 LD A,H regA = HighByte(regHL) lTStates = 4 Case 125 ' 125 LD A,L regA = (regHL And &HFF) lTStates = 4 Case 126 ' 126 LD A,(HL) regA = peekb(regHL) lTStates = 7 Case 127 ' 127 LD A,A lTStates = 4 Case 128 ' ADD A,B add_a(regB) lTStates = 4 Case 129 ' ADD A,C add_a(regC) lTStates = 4 Case 130 ' ADD A,D add_a(HighByte(regDE)) lTStates = 4 Case 131 ' ADD A,E add_a(getE) lTStates = 4 Case 132 ' ADD A,H add_a(HighByte(regHL)) lTStates = 4 Case 133 ' ADD A,L add_a(regHL And &HFF) lTStates = 4 Case 134 ' ADD A,(HL) add_a(peekb(regHL)) lTStates = 7 Case 135 ' ADD A,A add_a(regA) lTStates = 4 Case 136 ' ADC A,B adc_a(regB) lTStates = 4 Case 137 ' ADC A,C adc_a(regC) lTStates = 4 Case 138 ' ADC A,D adc_a(HighByte(regDE)) lTStates = 4 Case 139 ' ADC A,E adc_a(getE) lTStates = 4 Case 140 ' ADC A,H adc_a(HighByte(regHL)) lTStates = 4 Case 141 ' ADC A,L adc_a(regHL And &HFF) lTStates = 4 Case 142 ' ADC A,(HL) adc_a(peekb(regHL)) lTStates = 7 Case 143 ' ADC A,A adc_a(regA) lTStates = 4 Case 144 ' SUB B sub_a(regB) lTStates = 4 Case 145 ' SUB C sub_a(regC) lTStates = 4 Case 146 ' SUB D sub_a(HighByte(regDE)) lTStates = 4 Case 147 ' SUB E sub_a(getE) lTStates = 4 Case 148 ' SUB H sub_a(HighByte(regHL)) lTStates = 4 Case 149 ' SUB L sub_a(regHL And &HFF) lTStates = 4 Case 150 ' SUB (HL) sub_a(peekb(regHL)) lTStates = 7 Case 151 ' SUB A sub_a(regA) lTStates = 4 Case 152 ' SBC A,B sbc_a(regB) lTStates = 4 Case 153 ' SBC A,C sbc_a(regC) lTStates = 4 Case 154 ' SBC A,D sbc_a(HighByte(regDE)) lTStates = 4 Case 155 ' SBC A,E sbc_a(getE) lTStates = 4 Case 156 ' SBC A,H sbc_a(HighByte(regHL)) lTStates = 4 Case 157 ' SBC A,L sbc_a(regHL And &HFF) lTStates = 4 Case 158 ' SBC A,(HL) sbc_a(peekb(regHL)) lTStates = 7 Case 159 ' SBC A,A sbc_a(regA) lTStates = 4 Case 160 ' 160 AND B and_a(regB) lTStates = 4 Case 161 ' 161 AND C and_a(regC) lTStates = 4 Case 162 ' 162 AND D and_a(HighByte(regDE)) lTStates = 4 Case 163 ' 163 AND E and_a(getE) lTStates = 4 Case 164 ' 164 AND H and_a(HighByte(regHL)) lTStates = 4 Case 165 ' 165 AND L and_a(regHL And &HFF) lTStates = 4 Case 166 ' 166 AND (HL) and_a(peekb(regHL)) lTStates = 7 Case 167 ' 167 AND A and_a(regA) lTStates = 4 Case 168 ' 168 XOR B xor_a(regB) lTStates = 4 Case 169 ' 169 XOR C xor_a(regC) lTStates = 4 Case 170 ' 170 XOR D xor_a(HighByte(regDE)) lTStates = 4 Case 171 ' 171 XOR E xor_a(getE) lTStates = 4 Case 172 ' 172 XOR H xor_a(HighByte(regHL)) lTStates = 4 Case 173 ' 173 XOR L xor_a(regHL And &HFF) lTStates = 4 Case 174 ' 174 XOR (HL) xor_a(peekb(regHL)) lTStates = 7 Case 175 ' 175 XOR A regA = 0 fS = False f3 = False f5 = False fH = False fPV = True fZ = True fN = False fC = False lTStates = 4 Case 176 ' 176 OR B or_a(regB) lTStates = 4 Case 177 ' 177 OR C or_a(regC) lTStates = 4 Case 178 ' 178 OR D or_a(HighByte(regDE)) lTStates = 4 Case 179 ' 179 OR E or_a(getE) lTStates = 4 Case 180 ' 180 OR H or_a(HighByte(regHL)) lTStates = 4 Case 181 ' 181 OR L or_a(regHL And &HFF) lTStates = 4 Case 182 ' 182 OR (HL) or_a(peekb(regHL)) lTStates = 7 Case 183 ' 183 OR A or_a(regA) lTStates = 4 Case 184 ' 184 CP B cp_a(regB) lTStates = 4 Case 185 ' 185 CP C cp_a(regC) lTStates = 4 Case 186 ' 186 CP D cp_a(HighByte(regDE)) lTStates = 4 Case 187 ' 187 CP E cp_a(getE) lTStates = 4 Case 188 ' 188 CP H cp_a(HighByte(regHL)) lTStates = 4 Case 189 ' 189 CP L cp_a(regHL And &HFF) lTStates = 4 Case 190 ' 190 CP (HL) cp_a(peekb(regHL)) lTStates = 7 Case 191 ' 191 CP A cp_a(regA) lTStates = 4 Case 192 ' 192 RET NZ If fZ = False Then poppc() lTStates = 11 Else lTStates = 5 End If Case 193 ' 193 POP BC setBC(popw) lTStates = 10 Case 194 ' 194 JP NZ,nn If fZ = False Then regPC = nxtpcw() Else regPC = regPC + 2 End If lTStates = 10 Case 195 ' 195 JP nn regPC = peekw(regPC) lTStates = 10 Case 196 ' 196 CALL NZ,nn If fZ = False Then lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Else regPC = regPC + 2 lTStates = 10 End If Case 197 ' 197 PUSH BC pushw((regB << 8) Or regC) lTStates = 11 Case 198 ' 198 ADD A,n add_a(nxtpcb()) lTStates = 7 Case 199 ' 199 RST 0 pushpc() regPC = 0 lTStates = 11 Case 200 ' 200 RET Z If fZ Then poppc() lTStates = 11 Else lTStates = 5 End If Case 201 ' 201 RET poppc() lTStates = 10 Case 202 ' 202 JP Z,nn If fZ Then regPC = nxtpcw() Else regPC = regPC + 2 End If lTStates = 10 Case 203 ' 203 Prefix CB lTStates = execute_cb() Case 204 ' 204 CALL Z,nn If fZ Then lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Else regPC = regPC + 2 lTStates = 10 End If Case 205 ' 205 CALL nn lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Case 206 ' 206 ADC A,n adc_a(nxtpcb()) lTStates = 7 Case 207 ' 207 RST 8 pushpc() regPC = 8 lTStates = 11 Case 208 ' 208 RET NC If fC = False Then poppc() lTStates = 11 Else lTStates = 5 End If Case 209 ' 209 POP DE regDE = popw() lTStates = 10 Case 210 ' 210 JP NC,nn If fC = False Then regPC = nxtpcw() Else regPC = regPC + 2 End If lTStates = 10 Case 211 ' 211 OUT (n),A Outb((256 * regA + nxtpcb()), regA) lTStates = 11 Case 212 ' 212 CALL NC,nn If fC = False Then lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Else regPC = regPC + 2 lTStates = 10 End If Case 213 ' 213 PUSH DE pushw(regDE) lTStates = 11 Case 214 ' 214 SUB n sub_a(nxtpcb()) lTStates = 7 Case 215 ' 215 RST 16 pushpc() regPC = 16 lTStates = 11 Case 216 ' RET C If fC Then poppc() lTStates = 11 Else lTStates = 5 End If Case 217 ' EXX exx() lTStates = 4 Case 218 ' JP C,nn If fC Then regPC = nxtpcw() Else regPC = regPC + 2 End If lTStates = 10 Case 219 ' IN A,(n) regA = Inb((regA * 256) Or nxtpcb()) lTStates = 11 Case 220 ' CALL C,nn If fC Then lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Else regPC = regPC + 2 lTStates = 10 End If Case 221 ' prefix IX regID = regIX lTStates = execute_id() regIX = regID Case 222 ' SBC n sbc_a(nxtpcb()) lTStates = 7 Case 223 ' RST 24 pushpc() regPC = 24 lTStates = 11 Case 224 ' 224 RET PO If fPV = False Then poppc() lTStates = 11 Else lTStates = 5 End If Case 225 ' 225 POP HL regHL = popw() lTStates = 10 Case 226 ' 226 JP PO,nn If fPV = False Then regPC = nxtpcw() Else regPC = regPC + 2 End If lTStates = 10 Case 227 ' 227 EX (SP),HL lTemp = regHL regHL = peekw(regSP) pokew(regSP, lTemp) lTStates = 19 Case 228 ' 228 CALL PO,nn If fPV = False Then lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Else regPC = regPC + 2 lTStates = 10 End If Case 229 ' 229 PUSH HL pushw(regHL) lTStates = 11 Case 230 ' 230 AND n and_a(nxtpcb()) lTStates = 7 Case 231 ' 231 RST 32 pushpc() regPC = 32 lTStates = 11 Case 232 ' RET PE If fPV Then poppc() lTStates = 11 Else lTStates = 5 End If Case 233 ' 233 JP HL regPC = regHL lTStates = 4 Case 234 ' 234 JP PE,nn If fPV Then regPC = nxtpcw() Else regPC = regPC + 2 End If lTStates = 10 Case 235 ' 235 EX DE,HL lTemp = regHL regHL = regDE regDE = lTemp lTStates = 4 Case 236 ' 236 CALL PE,nn If fPV Then lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Else regPC = regPC + 2 lTStates = 10 End If Case 237 ' 237 prefix ED lTStates = execute_ed() Case 238 ' 238 XOR n xor_a(nxtpcb()) lTStates = 7 Case 239 ' 239 RST 40 pushpc() regPC = 40 lTStates = 11 Case 240 ' 240 RET P If fS = False Then poppc() lTStates = 11 Else lTStates = 5 End If Case 241 ' 241 POP AF setAF(popw) lTStates = 10 Case 242 ' 242 JP P,nn If fS = False Then regPC = nxtpcw() Else regPC = regPC + 2 End If lTStates = 10 Case 243 ' 243 DI intIFF1 = False intIFF2 = False lTStates = 4 Case 244 ' 244 CALL P,nn If fS = False Then lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Else regPC = regPC + 2 lTStates = 10 End If Case 245 ' 245 PUSH AF pushw((regA << 8) Or getF()) lTStates = 11 Case 246 ' 246 OR n or_a(nxtpcb()) lTStates = 7 Case 247 ' 247 RST 48 pushpc() regPC = 48 lTStates = 11 Case 248 ' 248 RET M If fS Then poppc() lTStates = 11 Else lTStates = 5 End If Case 249 ' 249 LD SP,HL regSP = regHL lTStates = 6 Case 250 ' 250 JP M,nn If fS Then regPC = nxtpcw() Else regPC = regPC + 2 End If lTStates = 10 Case 251 ' 251 EI intIFF1 = True intIFF2 = True lTStates = 4 Case 252 ' 252 CALL M,nn If fS Then lTemp = nxtpcw() pushw(regPC) regPC = lTemp lTStates = 17 Else regPC = regPC + 2 lTStates = 10 End If Case 253 ' 253 prefix IY regID = regIY lTStates = execute_id() regIY = regID Case 254 ' 254 CP n cp_a(nxtpcb()) lTStates = 7 Case 255 ' 255 RST 56 pushpc() regPC = 56 lTStates = 11 End Select End Sub Private Function qdec8(ByVal a As Long) As Long qdec8 = (a - 1) And &HFF End Function Private Function execute_id() As Long Dim xxx As Long, lTemp As Long, op As Long xxx = Z80Memory(regPC) regPC = regPC + 1 Select Case xxx Case 0 To 8 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 9 ' ADD ID,BC regID = add16(regID, (regB << 8) Or regC) execute_id = 15 Case 10 To 15 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 16 To 23 ' 16 To 23 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 24 ' 24 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 25 ' 25 ' ADD ID,DE regID = add16(regID, regDE) execute_id = 15 Case 26 To 31 ' 26 To 31 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 32 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 33 ' LD ID,nn regID = nxtpcw() execute_id = 14 Case 34 ' LD (nn),ID pokew(nxtpcw, regID) execute_id = 20 Case 35 ' INC ID regID = (regID + 1) And &HFFFF execute_id = 10 Case 36 ' INC IDH setIDH(inc8(getIDH)) execute_id = 8 Case 37 ' DEC IDH setIDH(dec8(getIDH)) execute_id = 8 Case 38 ' LD IDH,n setIDH(nxtpcb()) execute_id = 11 Case 39, 40 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 41 ' ADD ID,ID lTemp = regID regID = add16(lTemp, lTemp) execute_id = 15 Case 42 ' LD ID,(nn) regID = peekw(nxtpcw) execute_id = 20 Case 43 ' DEC ID regID = dec16(regID) execute_id = 10 Case 44 ' INC IDL setIDL(inc8(getIDL)) execute_id = 8 Case 45 ' DEC IDL setIDL(dec8(getIDL)) execute_id = 8 Case 46 ' LD IDL,n setIDL(nxtpcb()) execute_id = 11 Case 47 To 51 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 52 ' INC (ID+d) lTemp = id_d() pokeb(lTemp, inc8(peekb(lTemp))) execute_id = 23 Case 53 ' DEC (ID+d) lTemp = id_d() pokeb(lTemp, dec8(peekb(lTemp))) execute_id = 23 Case 54 ' LD (ID+d),n lTemp = id_d() pokeb(lTemp, nxtpcb()) execute_id = 19 Case 55, 56 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 57 ' ADD ID,SP regID = add16(regID, regSP) execute_id = 15 Case 58 To 63 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 64 To 67 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 68 ' LD B,IDH regB = getIDH() execute_id = 8 Case 69 ' LD B,IDL regB = getIDL() execute_id = 8 Case 70 ' LD B,(ID+d) regB = peekb(id_d) execute_id = 19 Case 71 To 75 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 76 ' LD C,IDH regC = getIDH() execute_id = 8 Case 77 ' LD C,IDL regC = getIDL() execute_id = 8 Case 78 ' LD C,(ID+d) regC = peekb(id_d) execute_id = 19 Case 79 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 80 To 83 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 84 ' LD D,IDH setD(getIDH) execute_id = 8 Case 85 ' LD D,IDL setD(getIDL) execute_id = 8 Case 86 ' LD D,(ID+d) setD(peekb(id_d)) execute_id = 19 Case 87 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 88 To 91 ' 88 To 91 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 92 ' 92 ' LD E,IDH setE(getIDH) execute_id = 8 Case 93 ' 93 ' LD E,IDL setE(getIDL) execute_id = 8 Case 94 ' 94 ' LD E,(ID+d) setE(peekb(id_d)) execute_id = 19 Case 95 ' 95 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 96 ' 96 ' LD IDH,B setIDH(regB) execute_id = 8 Case 97 ' 97 ' LD IDH,C setIDH(regC) execute_id = 8 Case 98 ' 98 ' LD IDH,D setIDH(HighByte(regDE)) execute_id = 8 Case 99 ' 99 ' LD IDH,E setIDH(getE) execute_id = 8 Case 100 ' 100 ' LD IDH,IDH execute_id = 8 Case 101 ' 101 ' LD IDH,IDL setIDH(getIDL) execute_id = 8 Case 102 ' 102 ' LD H,(ID+d) setH(peekb(id_d)) execute_id = 19 Case 103 ' 103 ' LD IDH,A setIDH(regA) execute_id = 8 Case 104 ' LD IDL,B setIDL(regB) execute_id = 9 Case 105 ' LD IDL,C setIDL(regC) execute_id = 9 Case 106 ' LD IDL,D setIDL(HighByte(regDE)) execute_id = 9 Case 107 ' LD IDL,E setIDL(getE) execute_id = 9 Case 108 ' LD IDL,IDH setIDL(getIDH) execute_id = 8 Case 109 ' 109 ' LD IDL,IDL execute_id = 8 Case 110 ' LD L,(ID+d) setL(peekb(id_d)) execute_id = 19 Case 111 ' LD IDL,A setIDL(regA) execute_id = 8 Case 112 ' LD (ID+d),B pokeb(id_d, regB) execute_id = 19 Case 113 ' LD (ID+d),C pokeb(id_d, regC) execute_id = 19 Case 114 ' LD (ID+d),D pokeb(id_d, HighByte(regDE)) execute_id = 19 Case 115 ' LD (ID+d),E pokeb(id_d, getE) execute_id = 19 Case 116 ' LD (ID+d),H pokeb(id_d, HighByte(regHL)) execute_id = 19 Case 117 ' LD (ID+d),L pokeb(id_d, (regHL And &HFF)) execute_id = 19 Case 118 ' UNKNOWN MsgBox("Unknown ID instruction " & xxx & " at " & regPC) execute_id = 0 Case 119 ' LD (ID+d),A pokeb(id_d, regA) execute_id = 19 Case 120 To 123 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 124 ' LD A,IDH regA = getIDH() execute_id = 8 Case 125 ' LD A,IDL regA = getIDL() execute_id = 8 Case 126 ' LD A,(ID+d) regA = peekb(id_d) execute_id = 19 Case 127 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 128 To 131 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 132 ' ADD A,IDH add_a(getIDH) execute_id = 8 Case 133 ' ADD A,IDL add_a(getIDL) execute_id = 8 Case 134 ' ADD A,(ID+d) add_a(peekb(id_d)) execute_id = 19 Case 135 To 139 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 140 ' ADC A,IDH adc_a(getIDH) execute_id = 8 Case 141 ' ADC A,IDL adc_a(getIDL) execute_id = 8 Case 142 ' ADC A,(ID+d) adc_a(peekb(id_d)) execute_id = 19 Case 143 To 147 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 148 ' SUB IDH sub_a(getIDH) execute_id = 8 Case 149 ' SUB IDL sub_a(getIDL) execute_id = 8 Case 150 ' SUB (ID+d) sub_a(peekb(id_d)) execute_id = 19 Case 151 To 155 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 156 ' SBC A,IDH sbc_a(getIDH) execute_id = 8 Case 157 ' SBC A,IDL sbc_a(getIDL) execute_id = 8 Case 158 ' SBC A,(ID+d) sbc_a(peekb(id_d)) execute_id = 19 Case 159 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 160 To 163 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 164 ' AND IDH and_a(getIDH) execute_id = 8 Case 165 ' AND IDL and_a(getIDL) execute_id = 8 Case 166 ' AND (ID+d) and_a(peekb(id_d)) execute_id = 19 Case 167 To 171 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 172 ' XOR IDH xor_a(getIDH) execute_id = 8 Case 173 ' XOR IDL xor_a(getIDL) execute_id = 8 Case 174 'XOR (ID+d) xor_a(peekb(id_d)) execute_id = 19 Case 175 To 179 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 180 ' OR IDH or_a(getIDH) execute_id = 8 Case 181 ' OR IDL or_a(getIDL) execute_id = 8 Case 182 ' OR (ID+d) or_a(peekb(id_d)) execute_id = 19 Case 183 To 187 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 188 ' CP IDH cp_a(getIDH) execute_id = 8 Case 189 ' CP IDL cp_a(getIDL) execute_id = 8 Case 190 ' CP (ID+d) cp_a(peekb(id_d)) execute_id = 19 Case 191 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 192 To 202 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 203 ' prefix CB lTemp = id_d() op = nxtpcb() execute_id_cb(op, lTemp) If ((op And &HC0) = &H40) Then execute_id = 20 Else execute_id = 23 Case 204 To 224 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 225 ' POP ID regID = popw() execute_id = 14 Case 226 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 227 ' EX (SP),ID lTemp = regID regID = peekw(regSP) pokew(regSP, lTemp) execute_id = 23 Case 228 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 229 ' PUSH ID pushw(regID) execute_id = 15 Case 230 To 232 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 233 ' JP ID regPC = regID execute_id = 8 Case 234 To 248 regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case 249 ' LD SP,ID regSP = regID execute_id = 10 Case 253 ' // Prefix FD regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 Case Else regPC = dec16(regPC) ' // REFRESH -1 intRTemp = intRTemp - 1 execute_id = 4 End Select End Function Private Function inc16(ByVal a As Long) As Long inc16 = (a + 1) And &HFFFF End Function Private Function nxtpcw() As Long nxtpcw = peekb(regPC) + (peekb(regPC + 1) << 8) regPC = regPC + 2 End Function Private Function nxtpcb() As Long nxtpcb = peekb(regPC) regPC = (regPC + 1) End Function Private Function peekb(ByVal addr As Long) As Long 'If ((addr And 49152) = 16384) Then ' glContendedMemoryDelay = glContendedMemoryDelay + glContentionTable(-glTStates) ' End If 'peekb = gRAMPage(glPageAt(glMemAddrDiv16384(addr)), addr And 16383) peekb = Z80Memory(addr) End Function Private Sub setD(ByVal l As Long) regDE = (l << 8) Or (regDE And &HFF) End Sub Private Sub setE(ByVal l As Long) regDE = (regDE And &HFF00) Or l End Sub Public Sub setF(ByVal b As Long) fS = (b And F_S) <> 0 fZ = (b And F_Z) <> 0 f5 = (b And F_5) <> 0 fH = (b And F_H) <> 0 f3 = (b And F_3) <> 0 fPV = (b And F_PV) <> 0 fN = (b And F_N) <> 0 fC = (b And F_C) <> 0 End Sub Private Sub setH(ByVal l As Long) regHL = (l << 8) Or (regHL And &HFF) End Sub Private Sub setL(ByVal l As Long) regHL = (regHL And &HFF00) Or l End Sub Private Function sla(ByVal ans As Long) As Long fC = (ans And &H80) <> 0 ans = (ans * 2) And &HFF fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fPV = Parity(ans) fH = False fN = False sla = ans End Function Private Function sls(ByVal ans As Long) As Long fC = (ans And &H80) <> 0 ans = ((ans * 2) Or &H1) And &HFF fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fPV = Parity(ans) fH = False fN = False sls = ans End Function Private Function sra(ByVal ans As Long) As Long fC = (ans And &H1) <> 0 ans = (ans \ 2) Or (ans And &H80) fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fPV = Parity(ans) fH = False fN = False sra = ans End Function Private Function srl(ByVal ans As Long) As Long fC = (ans And &H1) <> 0 ans = ans \ 2 fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fPV = Parity(ans) fH = False fN = False srl = ans End Function Private Sub sub_a(ByVal b As Long) Dim wans As Long, ans As Long wans = regA - b ans = wans And &HFF fS = (ans And F_S) <> 0 f3 = (ans And F_3) <> 0 f5 = (ans And F_5) <> 0 fZ = (ans = 0) fC = (wans And &H100) <> 0 fPV = ((regA Xor b) And (regA Xor ans) And &H80) <> 0 fH = (((regA And &HF) - (b And &HF)) And F_H) <> 0 fN = True regA = ans End Sub Private Sub xor_a(ByVal b As Long) regA = (regA Xor b) And &HFF fS = (regA And F_S) <> 0 f3 = (regA And F_3) <> 0 f5 = (regA And F_5) <> 0 fH = False fPV = Parity(regA) fZ = (regA = 0) fN = False fC = False End Sub Public Sub Z80Reset() regPC = 0 regSP = 65535 regA = 0 setF(0) setBC(0) regDE = 0 regHL = 0 exx() ex_af_af() regA = 0 setF(0) setBC(0) regDE = 0 regHL = 0 regIX = 0 regIY = 0 intR = 128 intRTemp = 0 intI = 0 intIFF1 = False intIFF2 = False intIM = 0 'SetEmulatedModel(Val(GetSetting("Grok", "vbSpec", "EmulatedModel", "0")), Val(GetSetting("Grok", "vbSpec", "SEBasicROM", "0"))) 'AY8912_init(1773000, WAVE_FREQUENCY, 8) 'frmMainWnd.Caption = App.ProductName End Sub Sub initParity() Dim lCounter As Long, j As Byte, p As Boolean For lCounter = 0 To 255 p = True For j = 0 To 7 If (lCounter And (2 ^ j)) <> 0 Then p = Not p Next j Parity(lCounter) = p Next lCounter End Sub ' J Moxham code added from here down Function HighByte(ByVal Register As Long) HighByte = Register >> 8 ' bit shift - this was not available in vb6 End Function Sub Outb(ByVal Portnumber As Long, ByVal value As Long) ' output to a port ' do nothing for the moment End Sub Function Inb(ByVal Portnumber As Long) Inb = 0 ' do nothing for the moment End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click initParity() ' set up the parity array End Sub End Class