Tricks - Elegant clear the screen. Quickly scan the keyboard. Setting interrupt (IM2).

Echo #02

           Tips and Tricks



   Every programmer has their own tricks, often known only to 
himself. To publish this sort of thing and will be designed 
this section. 


   Today we have:


   1. Elegant clear the screen:


   Usually the screen clean as follows:


   XOR A; 1

   OUT (# FE), A; 2

   LD HL, # 4000; 3

   LD DE, # 4001, 3 Size in bytes

   LD BC, # 1AFF; 3

   LD (HL), A; 1

   LDIR; 2


   Size: 15 bytes.


   And we like:


   XOR A; 1

   OUT (# FE), A; 2

   LD HL, # 5B00; 3
L1 DEC HL; 1

   LD (HL), A; 1

   OR (HL); 1

   JR Z, L1; 2


   Size: 11 bytes. Four bytes shorter. It seems a trifle, but 
nice ... 


   2. Quickly scan the keyboard:


   LD L, 0

   LD A, # FE
L1 LD C, A

   LD B, 5

   IN A, (# FE)
L2 RRA

   JR NC, YES_KEY

   INC L

   DJNZ L1

   LD A, C

   RLCA

   JP C, L2
NO_KEY; no keys are pressed. In

             ; L sticks # 28 (40)

YES_KEY; on something pressed. In the L-code

             ; Keys. Convert to
ASCII can be using the tablet:



    Key code key code


    # 00 C. SHIFT # 14 0


    # 01 Z # 15 September


    # 02 X # 16 August


    C # # 03 July 17


    V # # 04 June 18


    # 05 A # 19 P


    # 06 S # 1A O


    # 07 D # 1B I


    # 08 F # 1C U


    # 09 G # 1D Y


    0A # Q # 1E ENTER


    # 0B W # 1F L


    # 0C E # 20 K


    # 0D R # 21 J


    # 0E T # 22 H


    # 0F 1 # 1923 SPACE


    # 10 2 # 24 S. SHIFT


    # 11 3 # 25 M


    # 12 4 # 26 N


    # 13 5 # 27 B



   If you have received value of the trigger
and keys, check the keyboard, you want to continue scanning, 
then let JP L2, while the registers A, BC, L should remain the 
same values ​​that were in the Output from the procedure. In 
bars scan takes maximum 1432 (NO_KEY)! 


   3. And here is a very old but beautiful setting IM 2 (we 
were not the invented): 


   LD HL, TABLE_ADR; TABLE_ADR should be

   LD A, H; multiple of 256!

   LD I, A

   LD B, L

   INC A
L1 LD (HL), A

   INC HL

   DJNZ L1

   LD (HL), A

   LD L, H

   LD (HL), # C9; RET

   IM 2


   Generate an interrupt table
with TABLE_ADR and establish processing procedures: a single 
command RET. Takes only 3 +1 +2 +1 +1 +1 +1 +2 +1 +1 +2 +2 = 18 
bytes. 


   Well enough for now ... Please send us your
Tricks, readers! If someone is able to thread the needle better 
than we will be happy. 

Share your thoughts about the article