; DRAW 1K ZX81 ; A picture sized 60x36 ZX81 pixels is hidden on the screen ; can you draw the hidden picture? ; PLOT positions ; 12 ; 48 ; set 1 : xor 1 ; set 2 : xor 2 ; set 3 : xor 4 ; set 4 : xor 135 start equ #4009 draws equ 539 ; 1 less, test H reaching 255 saves a byte org start USRcall jp init ; BASIC makes call to here d_file dw dfile ; lowres screen location dfcc dw dfile+1 var dw vars dest dw 0 eline dw last chadd dw last-1 xptr dw 0 stkbot dw 0,0 berg db 0 mem dw 0 setbits inc b ; start from ROM LDIR inc c ; BC = #101. 2 bytes saved by storing in sysvar jr loop ; start the game lastk db 255,255,255 margin db 55 nxtlin dw basic db 0,0 flagx db 0 dw 0 taddr dw 3213 nrfld dw draws frames dw 65535 coords db 0,0 prcc db 0 sposn db 33,24 cdflag db 64 bittab db #40 ; 0000 hidden values on screen db #49 ; 0001 translated to pixels to be set db #64 ; 0010 db #6d ; 0011 db #7f ; 0100 db #50 ; 0101 db #51 ; 0110 db #52 ; 0111 db #53 ; 1000 db #54 ; 1001 db #55 ; 1010 db #57 ; 1011 db #58 ; 1100 db #59 ; 1101 db #5a ; 1110 db #5b ; 1111 cloop ld (hl),23 ; "*" cursor call rkey wrbits ld (hl),0 ; restore original value ; also start of game loop call field ; get screenposition ld e,(hl) ; save original ld a,e ld (wrbits+1),a ; if false undo change bit 6,(hl) ; hidden pixels? jr z,cloop ; no, just move cursor s8 push hl ; save screenpos ld (hl),8 ; show chessboardcursor ; find the pixels ld hl,bittab-1 ld d,#ff fpix inc d inc hl ld a,(hl) sub e jr nz,fpix ; routine to read table ld (setbits),a ; reset previous number ld a,d ; get tablevalue ld (tabval+1),a ; save for later call bitnr ; count the bits needed ld a,h ld (pixnr),a ; show that number ; read 1-4 set pixel, check valid bitloop ld a,(setbits) ; get the set bits ld l,a ; save A-reg call bitnr ; count the bits set ld a,(pixnr) ; get nr bits to set cp h ; check it with current set ld a,l ; restore A, the bits set pop hl ; get screenpos jr nz,cont ; not all bits set, read more tabval sub 0 ; check with bits to be set from table jr nz,wrbits ; wrong bits set res 3,(hl) ; undo show of cursorboard on empty field ld (pixnr),a ; clear shown number ld hl,(nrfld) ; 1 field solved dec hl ; decrease that number ld (nrfld),hl ; store back inc h ; store 1 less, test on HL=65535 eog jr z,eog ; cont. loop when drawing is ready. end of game jr loop ; continue playing cont push hl ; save screenpos push bc ; save xy call rkey ; read a key cp 15 ; 1 ld b,1 ; b holds plotpos1 call z,plot-1 ; plot on position 1 inc b ; plotpos2 cp 16 ; 2 call z,plot-1 ; plot on position 2 ld b,4 ; plotpos3 cp 17 ; 3 call z,plot-1 ; plot on position 3 ld bc,#8708 ; b holds plotpos4, c the pixel value cp 18 ; 4 call z,plot ; plot on position 4 pop bc ; retrieve original xy jr bitloop ; do test on all bits set bitnr ld h,28 ; the number "0" fh and a ; clear carry rra ; shift bit 0 jr nc,skcn ; do nothing when reset inc h ; increase counter skcn jr nz,fh ; tricked routine. End is when A=0, but A has extra loop when from INC H. Saves bytes ret ld c,b ; 3/4 PLOTS C holds same bit plot ld e,a ; save A res 3,(hl) ; undo show chessboard push hl ; save screenpos ld hl,setbits ; point to current bits set ld a,(hl) ; get them or c ; insert next bit cp (hl) ; test if already set ld (hl),a ; store result pop hl ; get screenpos jr z,exit ; exit when already set to prevent unplot ld a,(hl) ; get screenpos xor b ; set pixels (pixel 4 inverts) ld (hl),a ; save result on screen exit ld a,e ; restore A ret rkey ld a,(lastk) ; read lastkey inc a jr nz,rkey ; Wait for key up push hl ; save screenpos push bc ; save xy reread ld bc,(lastk) ; read lastkey ld a,c inc a jr z,reread ; wait for key down call #7bd ; translate key pop bc ; get xy push bc ; save again cp 5 ; A jr nz,td inc b ; go down td cp 10 ; Q jr nz,tr dec b ; go up tr cp 25 ; P jr nz,tl inc c ; go right tl cp 26 ; O jr nz,tfield dec c ; go left tfield call field ; get new screenpos ld e,a ; save A ld a,(hl) ; get screenvalue cp 27 ; test out of screen jr z,false ; then illegal move pop af ; legal move, drop xy defb 254 ; hide "get xy" in CP N save a byte from JR false pop bc ; false, get xy ld a,e ; restore A pop hl ; original screenpos needed to undo cursor ret field ld hl,basic ; start of screen ld d,0 ld e,c ; de=dx push bc ; save xy inc b ; add 1 cnt add hl,de ; first add dx later add dy ld e,33 ; de=dy per line djnz cnt pop bc ; restore xy ret n equ 101 dfile db 118,118,118,118 pixnr db 0 db 0 db "H"+n,"I"+n,"D"+n,"D"+n,"E"+n,"N"+n,128 db "P"+n,"I"+n,"C"+n,"T"+n,"U"+n,"R"+n,"E"+n db 118 basic dw 0 ; basic is on screen dw 0 ; but erased before db 249,212,28 ; first screen is made db 126 ; 08 db 143,0,18 ; 11 init ld hl,setbits ; 14 gamestart ld sp,#4400 ; 17 SP on end of memory push hl ; 18 on stack ld hl,basic ; 21 ld de,basic+1 ; 24 ld (hl),27 ; 26 make init "." ld bc,31 ; 29 jp init2 ; 32 db 118 ;hidden picture inserted here db 27,#52,#53,#54,#55,#54,#55,#54,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,27,118 db 27,#52,#64,#51,#55,#54,#55,#51,#40,#58,#53,#58,#58,#58,#58,#58,#58,#7f,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,27,118 db 27,#40,#40,#40,#40,#40,#53,#5a,#6d,#40,#50,#40,#40,#40,#40,#40,#7f,#64,#6d,#7f,#40,#40,#40,#53,#58,#58,#40,#40,#40,#40,#40,27,118 db 27,#40,#40,#40,#40,#55,#49,#7f,#40,#40,#50,#40,#40,#40,#54,#51,#7f,#40,#40,#64,#7f,#40,#40,#64,#7f,#64,#7f,#40,#40,#40,#40,27,118 db 27,#40,#40,#40,#53,#49,#53,#59,#58,#5b,#50,#40,#40,#40,#40,#40,#51,#40,#40,#40,#55,#40,#40,#40,#54,#53,#51,#40,#40,#40,#40,27,118 db 27,#40,#40,#40,#5a,#5b,#5b,#59,#5a,#5b,#50,#40,#40,#40,#40,#40,#55,#40,#40,#40,#40,#54,#40,#40,#40,#50,#40,#40,#40,#40,#40,27,118 db 27,#40,#40,#55,#52,#6d,#6d,#54,#58,#5b,#50,#64,#6d,#6d,#53,#58,#58,#58,#58,#58,#58,#5a,#58,#58,#40,#50,#40,#40,#40,#40,#40,27,118 db 27,#40,#40,#58,#5b,#5b,#5b,#5b,#5b,#52,#6d,#6d,#57,#5b,#52,#5b,#5b,#5b,#52,#57,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#59,#7f,27,118 db 27,#40,#55,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#59,#5a,#5b,#5b,#55,#5b,#5b,#5b,#40,#40,#5b,#5b,#5b,#5b,#5b,#5b,#49,#64,#5b,#5b,#59,27,118 db 27,#40,#55,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5a,#5b,#5b,#5b,#59,#5a,#5b,#5b,#5b,#5b,#5b,#5b,#7f,#53,#5b,#5b,#52,27,118 db 27,#40,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#59,27,118 db 27,#55,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#52,#6d,#6d,#6d,#6d,#6d,#57,#5b,#5b,#5b,#5b,#5b,27,118 db 27,#40,#5b,#57,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#49,#55,#5b,#5b,#5b,#5b,#5b,#5b,#50,#52,#64,#52,#64,#51,#55,#5b,#5b,#5b,#5b,#50,27,118 db 27,#55,#64,#55,#5b,#5b,#5b,#5b,#5b,#5b,#40,#40,#5a,#5b,#5b,#5b,#5b,#5b,#5b,#59,#58,#58,#58,#58,#58,#5a,#5b,#5b,#5b,#5b,#50,27,118 db 27,#55,#40,#55,#5b,#5b,#5b,#5b,#5b,#5b,#40,#40,#5a,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#49,27,118 db 27,#40,#54,#5a,#5b,#50,#64,#40,#40,#55,#7f,#64,#5a,#5b,#52,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#5b,#40,27,118 db 27,#40,#40,#6d,#6d,#40,#40,#40,#40,#40,#5b,#5a,#5b,#5b,#49,#57,#5b,#52,#40,#40,#40,#40,#40,#40,#64,#5b,#5b,#5b,#5b,#50,#40,27,118 db 27,#40,#40,#40,#40,#40,#40,#40,#40,#40,#64,#6d,#6d,#49,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#40,#64,#57,#5b,#6d,#40,#40,27,118 ;insert between init2 ldir ld l,basic mod 256 ; H still set ok ld de,init2 ; initcode must become "." ld c,33 ; and also set NL jp #19f9 ; LDIR in ROM and RET vars db 128 last equ $ end