eighty-eighty-js
Version:
A nice little Intel 8080 emulator for Node.js and Browser!
3 lines (2 loc) • 19.7 kB
JavaScript
import{u8 as e,u16 as s}from"typed-numbers";class t{static get(e,s){return 0!=(e&1<<s)}static set(s,t,r){return e(r?s|1<<t:s&~(1<<t))}static countOnes(e){let s=0;for(;e;)s+=1&e,e>>=1;return s}}class r{static getCycles(e){return r.OP_CYCLES_TABLE[e]}static toString(e){return r.ASM_TABLE[e]}}r.OP_CYCLES_TABLE=[4,10,7,5,5,5,7,4,4,10,7,5,5,5,7,4,4,10,7,5,5,5,7,4,4,10,7,5,5,5,7,4,4,10,16,5,5,5,7,4,4,10,16,5,5,5,7,4,4,10,13,5,10,10,10,4,4,10,13,5,5,5,7,4,5,5,5,5,5,5,7,5,5,5,5,5,5,5,7,5,5,5,5,5,5,5,7,5,5,5,5,5,5,5,7,5,5,5,5,5,5,5,7,5,5,5,5,5,5,5,7,5,7,7,7,7,7,7,7,7,5,5,5,5,5,5,7,5,4,4,4,4,4,4,7,4,4,4,4,4,4,4,7,4,4,4,4,4,4,4,7,4,4,4,4,4,4,4,7,4,4,4,4,4,4,4,7,4,4,4,4,4,4,4,7,4,4,4,4,4,4,4,7,4,4,4,4,4,4,4,7,4,5,10,10,10,11,11,7,11,5,10,10,10,11,17,7,11,5,10,10,10,11,11,7,11,5,10,10,10,11,17,7,11,5,10,10,18,11,11,7,11,5,5,10,5,11,17,7,11,5,10,10,4,11,11,7,11,5,5,10,4,11,17,7,11],r.ASM_TABLE={0:"NOP",1:"LXI BC",2:"STAX BC",3:"INX BC",4:"INR B",5:"DCR B",6:"MVI B",7:"RLC",8:"NOP",9:"DAD BC",10:"LDAX BC",11:"DCX BC",12:"INR C",13:"DCR C",14:"MVI C",15:"RRC",16:"NOP",17:"LXI DE",18:"STAX DE",19:"INX DE",20:"INR D",21:"DCR D",22:"MVI D",23:"RAL",24:"NOP",25:"DAD DE",26:"LDAX DE",27:"DCX DE",28:"INR E",29:"DCR E",30:"MVI E",31:"RAR",32:"NOP",33:"LXI HL",34:"SHLD",35:"INX HL",36:"INR H",37:"DCR H",38:"MVI H",39:"DAA",40:"NOP",41:"DAD HL",42:"LHLD",43:"DCX HL",44:"INR L",45:"DCR L",46:"MVI L",47:"CMA",48:"NOP",49:"LXI SP",50:"STA",51:"INX SP",52:"INR M",53:"DCR M",54:"MVI M",55:"STC",56:"NOP",57:"DAD SP",58:"LDA",59:"DCX SP",60:"INR A",61:"DCR A",62:"MVI A",63:"CMC",64:"MOV (B, B)",65:"MOV (B, C)",66:"MOV (B, D)",67:"MOV (B, E)",68:"MOV (B, H)",69:"MOV (B, L)",70:"MOV (B, M)",71:"MOV (B, A)",72:"MOV (C, B)",73:"MOV (C, C)",74:"MOV (C, D)",75:"MOV (C, E)",76:"MOV (C, H)",77:"MOV (C, L)",78:"MOV (C, M)",79:"MOV (C, A)",80:"MOV (D, B)",81:"MOV (D, C)",82:"MOV (D, D)",83:"MOV (D, E)",84:"MOV (D, H)",85:"MOV (D, L)",86:"MOV (D, M)",87:"MOV (D, A)",88:"MOV (E, B)",89:"MOV (E, C)",90:"MOV (E, D)",91:"MOV (E, E)",92:"MOV (E, H)",93:"MOV (E, L)",94:"MOV (E, M)",95:"MOV (E, A)",96:"MOV (H, B)",97:"MOV (H, C)",98:"MOV (H, D)",99:"MOV (H, E)",100:"MOV (H, H)",101:"MOV (H, L)",102:"MOV (H, M)",103:"MOV (H, A)",104:"MOV (L, B)",105:"MOV (L, C)",106:"MOV (L, D)",107:"MOV (L, E)",108:"MOV (L, H)",109:"MOV (L, L)",110:"MOV (L, M)",111:"MOV (L, A)",112:"MOV (M, B)",113:"MOV (M, C)",114:"MOV (M, D)",115:"MOV (M, E)",116:"MOV (M, H)",117:"MOV (M, L)",118:"HLT",119:"MOV (M, A)",120:"MOV (A, B)",121:"MOV (A, C)",122:"MOV (A, D)",123:"MOV (A, E)",124:"MOV (A, H)",125:"MOV (A, L)",126:"MOV (A, M)",127:"MOV (A, A)",128:"ADD B",129:"ADD C",130:"ADD D",131:"ADD E",132:"ADD H",133:"ADD L",134:"ADD M",135:"ADD A",136:"ADC B",137:"ADC C",138:"ADC D",139:"ADC E",140:"ADC H",141:"ADC L",142:"ADC M",143:"ADC A",144:"SUB B",145:"SUB C",146:"SUB D",147:"SUB E",148:"SUB H",149:"SUB L",150:"SUB M",151:"SUB A",152:"SBB B",153:"SBB C",154:"SBB D",155:"SBB E",156:"SBB H",157:"SBB L",158:"SBB M",159:"SBB A",160:"ANA B",161:"ANA C",162:"ANA D",163:"ANA E",164:"ANA H",165:"ANA L",166:"ANA M",167:"ANA A",168:"XRA B",169:"XRA C",170:"XRA D",171:"XRA E",172:"XRA H",173:"XRA L",174:"XRA M",175:"XRA A",176:"ORA B",177:"ORA C",178:"ORA D",179:"ORA E",180:"ORA H",181:"ORA L",182:"ORA M",183:"ORA A",184:"CMP B",185:"CMP C",186:"CMP D",187:"CMP E",188:"CMP H",189:"CMP L",190:"CMP M",191:"CMP A",192:"RNZ",193:"POP BC",194:"JNZ",195:"JMP",196:"CNZ",197:"PUSH B",198:"ADI",199:"RST 0",200:"RZ",201:"RET",202:"JZ",203:"JMP",204:"CZ",205:"CALL",206:"ACI",207:"RST 1",208:"RNC",209:"POP DE",210:"JNC",211:"OUT",212:"CNC",213:"PUSH D",214:"SUI",215:"RST 2",216:"RC",217:"RET",218:"JC",219:"IN",220:"CC",221:"CALL",222:"SBI",223:"RST 3",224:"RPO",225:"POP HL",226:"JPO",227:"XTHL",228:"CPO",229:"PUSH H",230:"ANI",231:"RST 4",232:"RPE",233:"PCHL",234:"JPE",235:"XCHG",236:"CPE",237:"CALL",238:"XRI",239:"RST 5",240:"RP",241:"POP PSW",242:"JP",243:"DI",244:"CP",245:"PUSH PSW",246:"ORI",247:"RST 6",248:"RM",249:"SPHL",250:"JM",251:"EI",252:"CM",253:"CALL",254:"CPI",255:"RST 7"};class a{static toHexStr(e,s=2){const t=e.toString(16).toUpperCase();return t.startsWith("-")?"-"+t.substr(1).padStart(s,"0"):t.padStart(s,"0")}static printOperation(e,s){console.log(`[${a.toHexStr(s.reg.pc-1,4)}] ${r.toString(e).padEnd(15," ")}(SP=${a.toHexStr(s.reg.sp,4)} A=${a.toHexStr(s.reg.a)} F=${a.toHexStr(s.reg.f)} B=${a.toHexStr(s.reg.b)} C=${a.toHexStr(s.reg.c)} D=${a.toHexStr(s.reg.d)} E=${a.toHexStr(s.reg.e)} H=${a.toHexStr(s.reg.h)} L=${a.toHexStr(s.reg.l)} FLAGS=`+(s.reg.getFlagS()?"S":".")+(s.reg.getFlagZ()?"Z":".")+(s.reg.getFlagA()?"A":".")+(s.reg.getFlagP()?"P":".")+(s.reg.getFlagC()?"C":".")+")")}}var i;!function(e){e[e.S=7]="S",e[e.Z=6]="Z",e[e.A=4]="A",e[e.P=2]="P",e[e.C=0]="C"}(i||(i={}));class h{constructor(){this.a=0,this.f=2,this.b=0,this.c=0,this.d=0,this.e=0,this.h=0,this.l=0,this.sp=0,this.pc=0}getAF(){return s(this.a<<8|this.f)}getBC(){return s(this.b<<8|this.c)}getDE(){return s(this.d<<8|this.e)}getHL(){return s(this.h<<8|this.l)}setAF(s){this.a=e(s>>8),this.f=e(213&s|2)}setBC(s){this.b=e(s>>8),this.c=e(255&s)}setDE(s){this.d=e(s>>8),this.e=e(255&s)}setHL(s){this.h=e(s>>8),this.l=e(255&s)}setFlag(e,s){this.f=t.set(this.f,e,s)}getFlag(e){return t.get(this.f,e)}setFlagS(e){this.setFlag(i.S,t.get(e,7))}setFlagZ(e){this.setFlag(i.Z,0==e)}setFlagA(e){this.setFlag(i.A,e)}setFlagP(e){this.setFlag(i.P,0==(1&t.countOnes(e)))}setFlagC(e){this.setFlag(i.C,e)}getFlagS(){return this.getFlag(i.S)}getFlagZ(){return this.getFlag(i.Z)}getFlagA(){return this.getFlag(i.A)}getFlagP(){return this.getFlag(i.P)}getFlagC(){return this.getFlag(i.C)}}class g{constructor(e,s,t=!1){this.reg=new h,this.mem=void 0,this.halted=!1,this.interruptEnabled=!1,this.debugEnabled=!1,this.device=void 0,this.stepCycles=0,this.stepZero=Date.now(),this.mem=e,this.device=s,this.debugEnabled=t}next(){if(this.halted)return 0;const t=this.getNextByte();this.debugEnabled&&a.printOperation(t,this);let i=0;switch(t){case 0:case 8:case 16:case 24:case 32:case 40:case 48:case 56:case 64:case 73:case 82:case 91:case 100:case 109:case 127:break;case 63:this.reg.setFlagC(!this.reg.getFlagC());break;case 55:this.reg.setFlagC(!0);break;case 4:this.reg.b=this.inr(this.reg.b);break;case 12:this.reg.c=this.inr(this.reg.c);break;case 20:this.reg.d=this.inr(this.reg.d);break;case 28:this.reg.e=this.inr(this.reg.e);break;case 36:this.reg.h=this.inr(this.reg.h);break;case 44:this.reg.l=this.inr(this.reg.l);break;case 52:{const e=this.getM(),s=this.inr(e);this.setM(s);break}case 60:this.reg.a=this.inr(this.reg.a);break;case 5:this.reg.b=this.dcr(this.reg.b);break;case 13:this.reg.c=this.dcr(this.reg.c);break;case 21:this.reg.d=this.dcr(this.reg.d);break;case 29:this.reg.e=this.dcr(this.reg.e);break;case 37:this.reg.h=this.dcr(this.reg.h);break;case 45:this.reg.l=this.dcr(this.reg.l);break;case 53:{const e=this.getM(),s=this.dcr(e);this.setM(s);break}case 61:this.reg.a=this.dcr(this.reg.a);break;case 47:this.reg.a=e(~this.reg.a);break;case 39:this.daa();break;case 65:this.reg.b=this.reg.c;break;case 66:this.reg.b=this.reg.d;break;case 67:this.reg.b=this.reg.e;break;case 68:this.reg.b=this.reg.h;break;case 69:this.reg.b=this.reg.l;break;case 70:this.reg.b=this.getM();break;case 71:this.reg.b=this.reg.a;break;case 72:this.reg.c=this.reg.b;break;case 74:this.reg.c=this.reg.d;break;case 75:this.reg.c=this.reg.e;break;case 76:this.reg.c=this.reg.h;break;case 77:this.reg.c=this.reg.l;break;case 78:this.reg.c=this.getM();break;case 79:this.reg.c=this.reg.a;break;case 80:this.reg.d=this.reg.b;break;case 81:this.reg.d=this.reg.c;break;case 83:this.reg.d=this.reg.e;break;case 84:this.reg.d=this.reg.h;break;case 85:this.reg.d=this.reg.l;break;case 86:this.reg.d=this.getM();break;case 87:this.reg.d=this.reg.a;break;case 88:this.reg.e=this.reg.b;break;case 89:this.reg.e=this.reg.c;break;case 90:this.reg.e=this.reg.d;break;case 92:this.reg.e=this.reg.h;break;case 93:this.reg.e=this.reg.l;break;case 94:this.reg.e=this.getM();break;case 95:this.reg.e=this.reg.a;break;case 96:this.reg.h=this.reg.b;break;case 97:this.reg.h=this.reg.c;break;case 98:this.reg.h=this.reg.d;break;case 99:this.reg.h=this.reg.e;break;case 101:this.reg.h=this.reg.l;break;case 102:this.reg.h=this.getM();break;case 103:this.reg.h=this.reg.a;break;case 104:this.reg.l=this.reg.b;break;case 105:this.reg.l=this.reg.c;break;case 106:this.reg.l=this.reg.d;break;case 107:this.reg.l=this.reg.e;break;case 108:this.reg.l=this.reg.h;break;case 110:this.reg.l=this.getM();break;case 111:this.reg.l=this.reg.a;break;case 112:this.setM(this.reg.b);break;case 113:this.setM(this.reg.c);break;case 114:this.setM(this.reg.d);break;case 115:this.setM(this.reg.e);break;case 116:this.setM(this.reg.h);break;case 117:this.setM(this.reg.l);break;case 119:this.setM(this.reg.a);break;case 120:this.reg.a=this.reg.b;break;case 121:this.reg.a=this.reg.c;break;case 122:this.reg.a=this.reg.d;break;case 123:this.reg.a=this.reg.e;break;case 124:this.reg.a=this.reg.h;break;case 125:this.reg.a=this.reg.l;break;case 126:this.reg.a=this.getM();break;case 2:this.mem.set(this.reg.getBC(),this.reg.a);break;case 18:this.mem.set(this.reg.getDE(),this.reg.a);break;case 10:this.reg.a=this.mem.get(this.reg.getBC());break;case 26:this.reg.a=this.mem.get(this.reg.getDE());break;case 128:this.add(this.reg.b);break;case 129:this.add(this.reg.c);break;case 130:this.add(this.reg.d);break;case 131:this.add(this.reg.e);break;case 132:this.add(this.reg.h);break;case 133:this.add(this.reg.l);break;case 134:this.add(this.getM());break;case 135:this.add(this.reg.a);break;case 136:this.adc(this.reg.b);break;case 137:this.adc(this.reg.c);break;case 138:this.adc(this.reg.d);break;case 139:this.adc(this.reg.e);break;case 140:this.adc(this.reg.h);break;case 141:this.adc(this.reg.l);break;case 142:this.adc(this.getM());break;case 143:this.adc(this.reg.a);break;case 144:this.sub(this.reg.b);break;case 145:this.sub(this.reg.c);break;case 146:this.sub(this.reg.d);break;case 147:this.sub(this.reg.e);break;case 148:this.sub(this.reg.h);break;case 149:this.sub(this.reg.l);break;case 150:this.sub(this.getM());break;case 151:this.sub(this.reg.a);break;case 152:this.sbb(this.reg.b);break;case 153:this.sbb(this.reg.c);break;case 154:this.sbb(this.reg.d);break;case 155:this.sbb(this.reg.e);break;case 156:this.sbb(this.reg.h);break;case 157:this.sbb(this.reg.l);break;case 158:this.sbb(this.getM());break;case 159:this.sbb(this.reg.a);break;case 160:this.ana(this.reg.b);break;case 161:this.ana(this.reg.c);break;case 162:this.ana(this.reg.d);break;case 163:this.ana(this.reg.e);break;case 164:this.ana(this.reg.h);break;case 165:this.ana(this.reg.l);break;case 166:this.ana(this.getM());break;case 167:this.ana(this.reg.a);break;case 168:this.xra(this.reg.b);break;case 169:this.xra(this.reg.c);break;case 170:this.xra(this.reg.d);break;case 171:this.xra(this.reg.e);break;case 172:this.xra(this.reg.h);break;case 173:this.xra(this.reg.l);break;case 174:this.xra(this.getM());break;case 175:this.xra(this.reg.a);break;case 176:this.ora(this.reg.b);break;case 177:this.ora(this.reg.c);break;case 178:this.ora(this.reg.d);break;case 179:this.ora(this.reg.e);break;case 180:this.ora(this.reg.h);break;case 181:this.ora(this.reg.l);break;case 182:this.ora(this.getM());break;case 183:this.ora(this.reg.a);break;case 184:this.cmp(this.reg.b);break;case 185:this.cmp(this.reg.c);break;case 186:this.cmp(this.reg.d);break;case 187:this.cmp(this.reg.e);break;case 188:this.cmp(this.reg.h);break;case 189:this.cmp(this.reg.l);break;case 190:this.cmp(this.getM());break;case 191:this.cmp(this.reg.a);break;case 7:this.rlc();break;case 15:this.rrc();break;case 23:this.ral();break;case 31:this.rar();break;case 197:this.stackAdd(this.reg.getBC());break;case 213:this.stackAdd(this.reg.getDE());break;case 229:this.stackAdd(this.reg.getHL());break;case 245:this.stackAdd(this.reg.getAF());break;case 193:this.reg.setBC(this.stackPop());break;case 209:this.reg.setDE(this.stackPop());break;case 225:this.reg.setHL(this.stackPop());break;case 241:this.reg.setAF(this.stackPop());break;case 9:this.dad(this.reg.getBC());break;case 25:this.dad(this.reg.getDE());break;case 41:this.dad(this.reg.getHL());break;case 57:this.dad(this.reg.sp);break;case 3:this.reg.setBC(s(this.reg.getBC()+1));break;case 19:this.reg.setDE(s(this.reg.getDE()+1));break;case 35:this.reg.setHL(s(this.reg.getHL()+1));break;case 51:this.reg.sp=s(this.reg.sp+1);break;case 11:this.reg.setBC(s(this.reg.getBC()-1));break;case 27:this.reg.setDE(s(this.reg.getDE()-1));break;case 43:this.reg.setHL(s(this.reg.getHL()-1));break;case 59:this.reg.sp=s(this.reg.sp-1);break;case 235:{const e=this.reg.h;this.reg.h=this.reg.d,this.reg.d=e;const s=this.reg.l;this.reg.l=this.reg.e,this.reg.e=s;break}case 227:{const e=this.mem.getWord(this.reg.sp),s=this.reg.getHL();this.reg.setHL(e),this.mem.setWord(this.reg.sp,s);break}case 249:this.reg.sp=this.reg.getHL();break;case 1:this.reg.setBC(this.getNextWord());break;case 17:this.reg.setDE(this.getNextWord());break;case 33:this.reg.setHL(this.getNextWord());break;case 49:this.reg.sp=this.getNextWord();break;case 6:this.reg.b=this.getNextByte();break;case 14:this.reg.c=this.getNextByte();break;case 22:this.reg.d=this.getNextByte();break;case 30:this.reg.e=this.getNextByte();break;case 38:this.reg.h=this.getNextByte();break;case 46:this.reg.l=this.getNextByte();break;case 54:this.setM(this.getNextByte());break;case 62:this.reg.a=this.getNextByte();break;case 198:this.add(this.getNextByte());break;case 206:this.adc(this.getNextByte());break;case 214:this.sub(this.getNextByte());break;case 222:this.sbb(this.getNextByte());break;case 230:this.ana(this.getNextByte());break;case 238:this.xra(this.getNextByte());break;case 246:this.ora(this.getNextByte());break;case 254:this.cmp(this.getNextByte());break;case 50:this.mem.set(this.getNextWord(),this.reg.a);break;case 58:this.reg.a=this.mem.get(this.getNextWord());break;case 34:this.mem.setWord(this.getNextWord(),this.reg.getHL());break;case 42:this.reg.setHL(this.mem.getWord(this.getNextWord()));break;case 233:this.reg.pc=this.reg.getHL();break;case 195:case 218:case 210:case 202:case 194:case 250:case 242:case 234:case 226:{const e=this.getNextWord();this.checkBranchCondition(t)&&(this.reg.pc=e);break}case 205:case 220:case 212:case 204:case 196:case 236:case 228:case 252:case 244:{const e=this.getNextWord();this.checkBranchCondition(t)&&(i=6,this.stackAdd(this.reg.pc),this.reg.pc=e);break}case 201:case 216:case 208:case 200:case 192:case 248:case 240:case 232:case 224:this.checkBranchCondition(t)&&(i=6,this.reg.pc=this.stackPop());break;case 199:case 207:case 215:case 223:case 231:case 239:case 247:case 255:this.stackAdd(this.reg.pc),this.reg.pc=s(56&t);break;case 251:this.interruptEnabled=!0;break;case 243:this.interruptEnabled=!1;break;case 219:this.reg.a=this.device.input(this.getNextByte());break;case 211:this.device.output(this.getNextByte(),this.reg.a);break;case 118:this.halted=!0;break;default:throw new g.UnimplementedInstructionError(t)}return r.getCycles(t)+i}async step(){if(this.stepCycles>g.STEP_CYCLES){this.stepCycles-=g.STEP_CYCLES;const e=Date.now()-this.stepZero,s=g.STEP_TIME-e;await new Promise(e=>setTimeout(e,s)),this.stepZero=this.stepZero+g.STEP_TIME}const e=this.next();return this.stepCycles+=e,e}handleInterrupt(e){this.interruptEnabled&&(this.interruptEnabled=!1,this.stackAdd(this.reg.pc),this.reg.pc=e,this.stepCycles+=r.getCycles(205))}getNextByte(){const e=this.mem.get(this.reg.pc);return this.reg.pc=s(this.reg.pc+1),e}getNextWord(){const e=this.mem.getWord(this.reg.pc);return this.reg.pc=s(this.reg.pc+2),e}getM(){const e=this.reg.getHL();return this.mem.get(e)}setM(e){const s=this.reg.getHL();this.mem.set(s,e)}stackAdd(e){this.reg.sp=s(this.reg.sp-2),this.mem.setWord(this.reg.sp,e)}stackPop(){const e=this.mem.getWord(this.reg.sp);return this.reg.sp=s(this.reg.sp+2),e}checkBranchCondition(e){switch(e){case 195:case 205:case 201:return!0;case 218:case 220:case 216:return this.reg.getFlagC();case 210:case 212:case 208:return!this.reg.getFlagC();case 202:case 204:case 200:return this.reg.getFlagZ();case 194:case 196:case 192:return!this.reg.getFlagZ();case 250:case 252:case 248:return this.reg.getFlagS();case 242:case 244:case 240:return!this.reg.getFlagS();case 234:case 236:case 232:return this.reg.getFlagP();case 226:case 228:case 224:return!this.reg.getFlagP();default:throw new g.UnimplementedInstructionError(e)}}inr(s){const t=e(s+1);return this.reg.setFlagS(t),this.reg.setFlagZ(t),this.reg.setFlagP(t),this.reg.setFlagA(1+(15&s)>15),t}dcr(s){const t=e(s-1);return this.reg.setFlagS(t),this.reg.setFlagZ(t),this.reg.setFlagP(t),this.reg.setFlagA(15!=(15&t)),t}daa(){let s=0,t=this.reg.getFlagC();const r=15&this.reg.a,a=this.reg.a>>4&15;(r>9||this.reg.getFlagA())&&(s=e(s+6)),(a>9||this.reg.getFlagC()||a>=9&&r>9)&&(s=e(s+96),t=!0),this.add(s),this.reg.setFlagC(t)}add(s){const t=this.reg.a,r=e(t+s);this.reg.setFlagS(r),this.reg.setFlagZ(r),this.reg.setFlagP(r),this.reg.setFlagA((15&t)+(15&s)>15),this.reg.setFlagC(t+s>255),this.reg.a=r}adc(s){const t=+this.reg.getFlagC(),r=this.reg.a,a=e(r+s+t);this.reg.setFlagS(a),this.reg.setFlagZ(a),this.reg.setFlagP(a),this.reg.setFlagA((15&r)+(15&s)+t>15),this.reg.setFlagC(r+s+t>255),this.reg.a=a}sub(s){const t=this.reg.a,r=e(t-s);this.reg.setFlagS(r),this.reg.setFlagZ(r),this.reg.setFlagP(r),this.reg.setFlagA((15&t)-(15&s)>=0),this.reg.setFlagC(t<s),this.reg.a=r}sbb(s){const t=+this.reg.getFlagC(),r=this.reg.a,a=e(r-s-t);this.reg.setFlagS(a),this.reg.setFlagZ(a),this.reg.setFlagP(a),this.reg.setFlagA((15&r)-(15&s)-t>=0),this.reg.setFlagC(r<s+t),this.reg.a=a}ana(s){const t=e(this.reg.a&s);this.reg.setFlagS(t),this.reg.setFlagZ(t),this.reg.setFlagP(t),this.reg.setFlagA(0!=(8&(this.reg.a|s))),this.reg.setFlagC(!1),this.reg.a=t}xra(s){const t=e(this.reg.a^s);this.reg.setFlagS(t),this.reg.setFlagZ(t),this.reg.setFlagP(t),this.reg.setFlagA(!1),this.reg.setFlagC(!1),this.reg.a=t}ora(s){const t=e(this.reg.a|s);this.reg.setFlagS(t),this.reg.setFlagZ(t),this.reg.setFlagP(t),this.reg.setFlagA(!1),this.reg.setFlagC(!1),this.reg.a=t}cmp(e){const s=this.reg.a;this.sub(e),this.reg.a=s}rlc(){const s=t.get(this.reg.a,7),r=e(this.reg.a<<1|+s);this.reg.setFlagC(s),this.reg.a=r}rrc(){const s=t.get(this.reg.a,0),r=e(s?128|this.reg.a>>1:this.reg.a>>1);this.reg.setFlagC(s),this.reg.a=r}ral(){const s=t.get(this.reg.a,7),r=e(this.reg.a<<1|+this.reg.getFlagC());this.reg.setFlagC(s),this.reg.a=r}rar(){const s=t.get(this.reg.a,0),r=this.reg.getFlagC()?e(128|this.reg.a>>1):e(this.reg.a>>1);this.reg.setFlagC(s),this.reg.a=r}dad(e){const t=this.reg.getHL(),r=s(t+e);this.reg.setFlagC(t>65535-e),this.reg.setHL(r)}}g.CLOCK_FREQUENCY=2e6,g.STEP_TIME=16,g.STEP_CYCLES=g.STEP_TIME/(1e3/g.CLOCK_FREQUENCY),function(e){class s extends Error{constructor(e){super(`Unimplemented instruction: ${r.toString(e)} (0x${a.toHexStr(e)})`),this.opcode=void 0,this.opcode=e}}e.UnimplementedInstructionError=s}(g||(g={}));class c{constructor(){this.data=new ArrayBuffer(c.MEMORY_SIZE),this.view=new DataView(this.data)}load(t,r=0){for(let a=0;a<t.length;a++){if(r+a>=c.MEMORY_SIZE)throw new c.NotEnoughMemoryError;this.set(s(r+a),e(t[a]))}}get(e){return this.view.getUint8(e)}set(e,s){this.view.setUint8(e,s)}getWord(e){return this.view.getUint16(e,!0)}setWord(e,s){this.view.setUint16(e,s,!0)}}c.MEMORY_SIZE=65536,function(e){class s extends Error{constructor(){super("Not enough memory for this operation!")}}e.NotEnoughMemoryError=s}(c||(c={}));export{t as Bit,g as Cpu,a as Debug,i as Flag,c as Memory,r as Opcode,h as Register};
//# sourceMappingURL=eighty-eighty.modern.js.map