stackInitialize: ;in hl = endposStack ld (stackEndPos), hl ld hl, stackStartPos ld (stackNewPos), hl ret stackClearAll: ld hl, stackStartPos ld (stackNewPos), hl ret stackCreate: ;in hl = pointer to stack pointer(first 2 = posAtm, second 2 = stackPosStart, last 2 = stackPosEnd(NOT USED?? - pos after last byte of stack)) ; bc = length stack(max length 255 for stackFind(and stackEdit, stackargetix (just everything which uses index of 1 byte) ; a = 1 or 2 bytes ;out ;destroys hl, de ;affected c = set when memory overflow ld de, (stackNewPos) ;toch niet nodig, eerste byte wordt gwon niet gebruikt ;cp 2;als t 2byte stack is, moet pos eentje meer trug, omdat hij 2 bytes gaat opslaan ;jr nz, not2byte ;dec de ;not2byte: ld (hl), e inc hl ld (hl), d inc hl ld (hl), e;stack start inc hl ld (hl), d inc hl push hl;bewaren omdat we nog stackPosEnd moeten opslaan ld h, d ld l, e ld d, b ld e, c ld b, a;b = lenght of each value, hl=start array, de = length array stackCreateLoop: or a sbc hl, de djnz stackCreateLoop ld de, (stackEndPos) cphlde jp nc, nooverflow ld hl, errnotenoughrammsg call dspMsg scf jr stackCreate_ nooverflow: ld (stackNewPos), hl ld d, h ld e, l pop hl;pos waar we stackposEnd opslaan ld (hl), e inc hl ld (hl), d or a stackCreate_:;pop niet af ret stackCreateLocation: ; create stack on custom location - no out of memory checking ;in hl = pointer to stack pointer(first 2 = posAtm, second 2 = stackPosStart) ; bc = length stack(max length 255 for stackFind(and stackEdit, stackargetix (just everything which uses index of 1 byte) ; de = location of beginning stack ; a = 1 or 2 bytes ;out ;destroys all ld (hl), e inc hl ld (hl), d inc hl ld (hl), e;stack start inc hl ld (hl), d dec hl dec hl dec hl ret ; voor als we stackposend willen opslaan...... ; ex de, hl ; or a ;nogkeer: ; sbc hl, bc ; dec a ; jr nz, nogkeer ; ex de, hl ; inc hl ; ld (hl), e ; inc hl ; ld (hl), d ; ret stackPush: ;in hl = pointer to stack pointer ; a = value push de ; jr niks ; ld de, stTblAtomIx ; cphlde ; jr nz, niks ; call debuga ;niks: ; ; ld e, (hl) inc hl ld d, (hl) ld (de), a dec de ld (hl), d dec hl ld (hl), e pop de ret stackPop: ;in hl = pointer to stack pointer ;out a = value push de ld e, (hl) inc hl ld d, (hl) inc de ld a, (de) ld (hl), d dec hl ld (hl), e ; jr niks2 ; ld de, stTblAtomIx ; cphlde ; jr nz, niks2 ; call debuga1 ;niks2: ; ; pop de ret stackUnGet: ;in hl = pointer to stack pointer push de ld e, (hl) inc hl ld d, (hl) dec de ld (hl), d dec hl ld (hl), e pop de ret stackGet: ;in hl = pointer to stack pointer ;out a = value push de ld e, (hl) inc hl ld d, (hl) inc de ld a, (de) pop de dec hl ret stackFind: ;in hl = pointer to stack pointer ; a = value to find ;out a = index(0 is first one) ; c affected, set=not found push de push bc push hl call stackLength ld d, a;backup xor a cp b jr nz, not0length cp c jr z, stackFindNotFound not0length: ld a, d;restore inc hl inc hl ld e, (hl) inc hl ld d, (hl) ld h, d ld l, e;hl is start stack ld e, 0;e=index,a=value to find,d=value found,bc=length stack stackFindLoop: ld d, (hl) cp d jr z, stackFindFound dec hl inc e dec c jr nz, stackFindLoop stackFindNotFound: or a jr stackFind_ stackFindFound: ld a, e scf stackFind_: ccf pop hl pop bc pop de ret stackLength: ;in hl = pointer to stack pointer ;out bc = length(from start to last stack push) push hl push de ld e, (hl) inc hl ld d, (hl) inc hl ld c, (hl) inc hl ld b, (hl) ld h, b ld l, c or a sbc hl, de ld b, h ld c, l pop de pop hl ret stackGetAt: ;in hl = pointer to stack pointer ; a = index ;out a = value push hl push de inc hl inc hl ld e, (hl) inc hl ld d, (hl);de=start stack ld h, d ld l, e ld d, 0 ld e, a;;hl = start stack, de = index or a sbc hl, de;hl = pos value ld a, (hl) pop de pop hl ret stackEdit: ;in hl = pointer to stack pointer ; a = index ; e = value push af push hl push de inc hl inc hl ld e, (hl) inc hl ld d, (hl);de=start stack ld h, d ld l, e ld d, 0 ld e, a;;hl = start stack, de = index or a sbc hl, de;hl = pos value pop de ld (hl), e pop hl pop af ret stackPushDE: ;in hl = pointer to stack pointer ; de = value push bc push de ld c, a;backup a ld a, d;voor eerste push ld b, e;tweede push ld e, (hl) inc hl ld d, (hl) ld (de), a dec de ld a, b;tweede push ld (de), a dec de ld (hl), d dec hl ld (hl), e ld a, c;restore pop de pop bc ret stackPopDE: ;in hl = pointer to stack pointer ;out de = value push bc ld b, a;backup ld e, (hl) inc hl ld d, (hl) inc de ld a, (de) ld c, a inc de ld a, (de) ld (hl), d dec hl ld (hl), e ld d, a ld e, c ld a, b;restore pop bc ret stackGetDE: ;in hl = pointer to stack pointer ;out de = value push bc ld b, a;backup ld e, (hl) inc hl ld d, (hl) inc de ld a, (de) ld c, a inc de ld a, (de) ld d, a ld e, c ld a, b;restore pop bc dec hl ret stackEditDE: ;in hl = pointer to stack pointer ; a = index ; de = value push af push hl push de inc hl inc hl ld e, (hl) inc hl ld d, (hl);de=start stack ld h, d ld l, e ld d, 0 add a, a;2byte array, 2 times further back rl d;als vorige command overflow gaf doet ie um alsnog bij d ld e, a;;hl = start stack, de = index inc de;nog eentje verder trug, omdat t raar is opgeslagen, zie tekening? or a sbc hl, de;hl = pos value pop de ld (hl), e inc hl ld (hl), d pop hl pop af ret stackGetAtDE: ;in hl = pointer to stack pointer ; a = index ;out de = value push hl inc hl inc hl ld e, (hl) inc hl ld d, (hl);de=start stack ld h, d ld l, e ld d, a;backup add a, a;2byte array, 2 times further back ld e, a;;hl = start stack, de = index ld a, d;restore ld d, 0 rl d;als add a,a overflow gaf doet ie um alsnog bij d inc de;nog eentje verder trug, omdat t raar is opgeslagen, zie tekening? or a sbc hl, de;hl = pos value ld e, (hl) inc hl ld d, (hl) pop hl ret stackDestroyAllBut: ;in ix = pointer to stack pointer ld d, (ix+3) ld e, (ix+2);de = pointer to start pos stack ld hl, stackEndPos or a sbc hl, de;hl = verschil waarover we alles naar rechts moeten verschuiven ld d, h ld l, e ;alle pointers met DE naar rechts verschuiven ld h, (ix+1) ld l, (ix+0) add hl, de ld (ix+1), h ld (ix+0), l ; ld h, (ix+3) ld l, (ix+1) add hl, de ld (ix+3), h ld (ix+1), l ; ld h, (ix+5) ld l, (ix+3) add hl, de ld (ix+5), h ld (ix+3), l ;data verplaatsen ld de, stackStartPos;de = begin stack nieuwe pos ld h, (ix+3) ld l, (ix+1);hl = begin stack oude pos ld d, (ix+5) ld e, (ix+3) or a sbc hl, de ld b, h ld c, l;bc = length stack ld h, (ix+3) ld l, (ix+1);hl = begin stack oude pos ldir ret ;stackDebugLeft: ; push hl ; push de ; ld hl, (stackNewPos) ; ld de, (stackEndPos) ; or a ; sbc hl, de ; call debughl ; pop de ; pop hl ; ret stackFlush: push de inc hl inc hl ld e, (hl) inc hl ld d, (hl) dec hl dec hl ld (hl), d dec hl ld (hl), e pop de ret stackArGetIx: ;in hl = pointer to stack pointer ;out a = index of item wich will be popped next time push de push bc push hl ld c, (hl) inc hl ld b, (hl) inc hl ld e, (hl) inc hl ld d, (hl) ex de, hl or a sbc hl, bc ld a, l ;dec a pop hl pop bc pop de ret stackArSetIx: ;in hl = pointer to stack pointer ; a = index of item wich will be popped next time push de inc hl inc hl ld e, (hl) inc hl ld d, (hl) ex de, hl;de -= a; push bc ld b, 0 ld c, a or a sbc hl, bc pop bc ex de, hl dec hl dec hl ld (hl), d dec hl ld (hl), e pop de ret stackArSetIxDE: ;in hl = pointer to stack pointer ; a = index of item wich will be popped next time push de inc hl inc hl ld e, (hl) inc hl ld d, (hl) ex de, hl;de -= a*2; push bc ld b, 0 ld c, a or a sbc hl, bc sbc hl, bc pop bc ex de, hl dec hl dec hl ld (hl), d dec hl ld (hl), e pop de ret stackArPop: ;in hl = pointer to stack pointer ;out a = value ;descr gaat van begin naar einde stack(wordt gebruikt samen met stackFlush) push de ld e, (hl) inc hl ld d, (hl) ld a, (de) dec de ld (hl), d dec hl ld (hl), e pop de ret stackArPopDE: ;in hl = pointer to stack pointer ;out de = value ;descr gaat van begin naar einde stack(wordt gebruikt samen met stackFlush) push bc ld b, a;backup ld e, (hl) inc hl ld d, (hl) ld a, (de) dec de ld c, a ld a, (de) dec de ld (hl), d dec hl ld (hl), e ld d, c ld e, a ld a, b;restore pop bc ret stackArUngetDE: ;in hl = pointer to stack pointer push de call stackpopde;een arUnget is een normale pop :P(kunnen ook hele functie kopieren en wat dingen eruit halen) pop de ret stackArUnget: ;in hl = pointer to stack pointer push af call stackpop;een arUnget is een normale pop :P(kunnen ook hele functie kopieren en wat dingen eruit halen) pop af ret