Beyond Basic - Part 4

Downloads|Programming|Beyond Basic

Written By: Tony Cruise

First Published: Micro’s Gazette – Issue 002 (March/April 1989)

 

This issue I will start to cover some of the routines that will be used in our extended Basic.  We will start off with the new sprite commands.

You can have up to 32 sprites on the screen at once but if you want to move more than about five or six, your program slows down considerably.  The routines this issue allow you to specify X and Y velocities for each of the 32 sprites and then they will be moved independently of your Basic program.

I have included an assembler listing explaining how it works as well as a basic loader program for those people who do not have assemblers and a demonstration program.

Machine Code Program

  1 ;Program to move sprites by velocity
  2 ;  
D000 3 ORIGINORG D000H 
  4 ;  
  5 ;Label settings 
  6 ;  
  7 HTIMIEQU FD9FH; Hook jump – Timer
  8 SCRMODEQU FCAFH; RAM storage – Screen mode
  9 SETWRTEQU 0053H; ROM routine – Set video write
  10 SETRDEQU 0050H; ROM routine – Set video read
  11 ;  
D0003EC312 STARTLD A,C3H; Value for JUMP instruction
D002329FFD13LD (HTIMI),A; Load into Hook
D005210CD014LD HL,QUE; Address to jump to
D00822A0FD15LD (HTIMI+1),HL; Load into Hook
D00BC916RET;
  17 ;  
D00CCD14D018 QUECALL SPRMOV; Call SPRMOV routine
D00FF7879C7719DEFB F7H,87H,9CH,77H; Disk delay routine, remove if
D013C920RET; up do not have a disk drive
  21 ;  
D0143AAFFC22 SPRMOVLD A,(SCRMOD); Get screen mode
D017FE0023CP 0;
D019C824RET Z; Make sure not screen 0
D01A3A47D125LD A,(MOVFLG);
D01DFE0026CP 0; Get move flag
D01FC827RET Z;
D02021001B28LD HL,1B00H; Return if move set to off
D0231187D029LD DE,SPRTBL;
D02601800030LD BC,128;
D029CD78D031CALL LDIRVM; Get sprite table
D02C2187D032LD HL,SPRTBL; Set pointer to sprite table
D02F062033LD B,32; Set loop counter
D0311107D134LD DE,SPRVEL; Set point to velocities
D0347E35 LOOP1LD A,(HL);
D035FED136CP 209; Is the sprite on screen?
D037281A37JR Z,LOOP2; No skip
D0394F38LD C,A;
D03A1A39LD A,(DE); Get Y velocity
D03B8140ADD A,C; Add Y velocity
D03CFEDC41CP 220; Off Top of screen
D03E380442JR C,LOOP3; No skip
D0403EC043LD A,192; Yes, correct value
D042180544JR LOOP4;
D044FEC045 LOOP3CP 192; Off bottom of screen?
D046380146JR C,LOOP4; No skip
D048AF47XOR A; Yes, correct value
D0497748 LOOP4LD (HL),A; Store new value
D04A2349INC HL;
D04B1350INC DE; Increment pointers
D04C7E51LD A,(HL);
D04D4F52LD C,A;
D04E1A53LD A,(DE); Get X velocity
D04F8154ADD A,C; Add X velocity
D0507755LD (HL),A; Store new value
D0512B56DEC HL;
D0521B57DEC DE; Restore pointers
D0531358 LOOP2INC DE;
D0541359INC DE;
D0552360INC HL;
D0562361INC HL;
D0572362INC HL;
D0582363INC HL; Update pointers
D0590564DEC B; Decrease loop counter
D05A20D865JR NZ,LOOP1; Next loop
D05C21001B66LD HL,1B00H;
D05F1187D067LD DE,SPRTBL;
D06201800068LD BC,128;
D065CD69D069CALL LDIRMV; Save sprite table
D068C970RET;
  71 ;  
  72 ;Move a block of memory from video RAM
  73 ;  
D069CD530074 LDIRMVCALL SETWRT 
D06C1A75 LP1LD A,(DE) 
D06DD39876OUT (98H),A 
D06F1377INC DE 
D0700B78DEC BC 
D0717879LD A,B 
D072B180OR C 
D073FE0081CP 0 
D07520F582JR NZ,LP1 
D077C983RET 
  84 ;  
  85 ;Move a block of memory to video RAM
  86 ;  
D078CD500087 LDIRVMCALL SETRD 
D07BDB9888 LP2IN A,(98H) 
D07D1289LD (DE),A 
D07E1390INC DE 
D07F0B91DEC BC 
D0807892LD A,B 
D081B193OR C 
D082FE0094CP 0 
D08420F595JR NZ,LP2 
D086C996RET 
  97 ;  
  98 ;Data storage 
  99 ;  
D087 100 SPRTBLDEFS 128 
D107 101 SPRVELDEFS 64 
D14700102 MOVFLGDEFB 0 
  103 END  

Basic Loader

10 CLS:CLEAR 200,&HCFFF:DEFINTA-Z:A=&HD000
20 READ A$:IF A$ <> “@” THEN POKE A, VAL(“&H”+A$):A=A+1:GOTO 20
30 POKE &HD147,0
40 PRINT”  INSERT TAPE/DISK TO SAVE PROGRAM”
50 PRINT”       AND PRESS ANY KEY”
60 A$=INPUT$(1):PRINT:PRINT “  SAVING ....”
70 BSAVE”SPRITE.OBJ”,&HD000,&HD147
80 END
100 DATA 3E,C3,32,9F,FD,21,0C,D0,22,A0,FD,C9,CD,14,D0
110 DATA F7,87,9C,77 (or DATA 00,00,00,00 if you don’t have a disk drive)
120 DATA C9,3A,AF,FC,FE,00,C8,3A,47,D1,FE,00,C8,21,00
130 DATA 1B,11,87,D0,01,80,00,CD,78,D0,21,87,D0,06,20
140 DATA 11,07,D1,7E,FE,D1,28,1A,4F,1A,81,FE,DC,38,04
150 DATA 3E,C0,18,05,FE,C0,38,01,AF,77,23,13,7E,4F,1A
160 DATA 81,77,2B,1B,13,13,23,23,23,23,05,20,D8,21,00
170 DATA 1B,11,87,D0,01,80,00,CD,69,D0,C9,CD,53,00,1A
180 DATA D3,98,13,0B,78,B1,FE,00,20,F5,C9,CD,50,00,DB
190 DATA 98,12,13,0B,78,B1,FE,00,20,F5,C9,@


Basic Program Example

10 COLOR 15,1,9:SCREEN 2,2:SPRITE$(0) = STRING$(32,244):DEF FNA(X) = INT(RND(1)*X)+1:A = RND(-TIME)
20 STOP ON:ON STOP GOSUB 110
30 POKE &HD147,1:FOR A=0 TO 31
40 PUT SPRITE A,(128,96),FNA(15),0
50 XV=FNA(5)-3:IF XV<0 THEN XV=XV+256
60 YV=FNA(5)-3:IF YV<0 THEN YV=YV+256
70 IF XV+YV=0 THEN 50
80 POKE &HD107+A*2,YV:POKE &HD108+A*2,XV:NEXT
90 IF NOT(STRIG(0)) THEN 90 ELSE POKE &HD147,0
100 IF NOT(STRIG(0)) THEN 100 ELSE POLE &HD147,1:GOTO 80
110 POKE &HD147,0:END 


Type in the machine code loader first and save it to disk or tape.  Now type in the Basic example program and save it to tape or disk.  Then use BLOAD”SPRITE.OBJ”,R to run the machine code program and then RUN the example Basic program.

Next month I will cover the collision testing routines.  Bye for now!