Author Topic: Useful Routines  (Read 2967 times)

0 Members and 1 Guest are viewing this topic.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Useful Routines
« on: January 04, 2012, 11:58:17 pm »
I figured that I would start a topic like this here. These are links to a bunch of routines that I find useful and I think other will find useful
ChkFindVar is like ChkFindSym for Program, Protected Program, Appvar, Temporary Program, Group.
SearchVarBC is a routine (with several others included) for searching for the other program types.
A_Times_DE This is a non-standard routine that is 25 bytes and faster than DE_Times_A.
DE_Times_A This is a fast, straightfoward, standard DE_Times_A
C_Div_D
C_Times_D
DE_Times_BC
DEHL_Div_C
DEHL_Mul_32Stack (no SMC) this is for Apps. This multiplies DEHL by the last two entries on the stack and returns the 64-bit result.
DEHL_Mul_32Stack (SMC) this gets a decent speed boost by using SMC. It does the same thing.
DEHL_Mul_A this uses shadow registers HLDE. I need to rewrite this
GCDHL_BC computes the Greatest Common Divisor
HL_Div_BC this can probably be optimised
HL_Div_C
HLDE_Div_C
ncrHL_DE this computes "n choose r" and it uses shadow registers
RoundHL_Div_C returns the rounded value of HL/C
RoundSqrtE returns the rounded square root of E
SqrtE
SqrtHL
PixelPlot (1) is a fast pixel plotting code that uses SMC and will draw to arbitrary buffers. I usually use a different version of this.
LineSearch will search a var for a line and return the location and size. This will not search beyond the given limit.
ConvRStr will convert a string of numbers to a 16-bit value
Is_2_Byte checks if A is the start byte of a 2-byte token
TokensToASCII converts a token string to ASCII
IncHLMem1 This is an optmised routine to increment HL and port 6 as necessary
TileMap1 This draws a tilemap. Uses DrawSpriteXxY.
DrawSpriteXxY This draws a tile

Pastebin won't let me paste anymore for a while, but I have a bunch more waiting in reserve :)

EDIT: As a note, these are all codes I wrote :)

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Useful Routines
« Reply #1 on: January 05, 2012, 05:44:27 am »
I think pretty much everyone knows about z80 bits, but:
http://baze.au.com/misc/z80bits.html
Also, there's a nice French site which talks about z80 multiplication/division:
http://projets.infos.free.fr/CPC/CPC_MultiplicationsDivisions.php5

Later i can maybe upload my tilemap routine or maybe my number-to-string routine. :)
EDIT: Here's the number-to-string routine i wrote:
http://pastebin.com/sudK27tC (anti-copyright)
And here's a simplified version of the 16x16 tilemap routine i wrote (though i was using a 7(wide)x5(tall) buffer):
http://pastebin.com/hxtH1Vqu (anti-copyright)
It's a bit smaller than the one you posted and maybe a bit faster as i incorporated the sprite drawing directly into the routine, not using a separate sprite routine, but i haven't checked, so i dunno. I'm really bad at multiplication routines, i usually do things by multiples of two or use a simple routine similar to what everyone else uses. I should look into how z80 math works a bit more...
« Last Edit: January 05, 2012, 08:17:53 am by chickendude »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Useful Routines
« Reply #2 on: January 05, 2012, 10:51:48 am »
Nice, chickendude! Here is the number to string routine I use in Grammer to display numbers.
Spoiler For Spoiler:
It converts HL to any base from 2 to 36 and uses the OS ASCII for the numbers and letters (which match the tokens). This can be easily modified to handle 32-bit input, too:
;=================================================================
ConvNumBase:
;=================================================================
;Inputs:
;     C is the base to convert to
;     HL is the number to convert
;Output:
;     A is the number of digits
;     BC is the number of digits
;     DE is 0
;     HL points to the number string
;Notes:
;     This converts HL to a zero terminated string stored in OP4
;     and possibly a few bytes of OP3.
;=================================================================
        xor a
        ld de,84A3h
        ld (de),a
        dec de
          call HL_Div_C
          cp 10
          jr c,$+4
            add a,7
          add a,48
          ld (de),a
          dec de
          ld a,h
          or l
          jr nz,$-15
        ld a,$A2
        sub e
        ld c,a
        inc de
        ex de,hl
        ret
And as for the tilemap routine, there are tons of different ways. The one I provided handles 8x8 tiles and tilemaps of any size greater than or equal to the size of the screen as well as 5 different drawing methods.

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Useful Routines
« Reply #3 on: January 05, 2012, 12:56:41 pm »
You're right, i love looking at how people go about their tilemappers. Thanks for sharing!