FunkLibrary

Z80 Software Tools
  • Author
  • Message
Offline
User avatar

NanoWar

Site Admin

Topic Starter

FunkLibrary

Post30 November 2011, 10:51

I was starting a big API library, like the DoorsCS-SDK, just for the spasm compiler. I want to change how you write your code.
In the end a hello world program should look like this:
Code: Select all
; Hello Funk example program
#include funk/funk.z80

.funkdef "Hello", ion, ti8x
.start
      text  20, 30, "Hello Funk,"
      text  30, 33, "you rock!"
      wait
      quit
.data
.valid

It could integrate all spasm macros from the codeplex site (app.inc, relocate.inc) and could be itself integrated into Wabbitstudio.

Thumbs up or down?
Attachments
FunkLibrary-0.1.zip
Funky!
(12.23 KiB) Downloaded 402 times
Offline
User avatar

Buckeye Dude

Staff Member

Re: Funk Library

Post04 December 2011, 05:56

Thumbs up
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post04 December 2011, 14:54

Doesn't that basically make simple code large and inflated? I think defining macros for some things, like i like to define a macro (maybe that's the wrong word) for ld hl,(hl), things i use a lot and which aren't really worth making a subroutine out of (ie, call LDHLHL). There was an attempt a while back (well, several i suppose) at an "easyasm" language, basically basic-style code (macros) compiled into asm. The code was nice and quick, but really bloated. Maybe i'm not understanding quite right what you're talking about though.
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post04 December 2011, 17:03

chickendude wrote:Doesn't that basically make simple code large and inflated?

That might happen, but it's more about faster coding through automation.

This way I can easily extend that code. Let's say I wanted the text on the gbuf, copy it to a cleared screen and make it all an app:
Code: Select all
; Hello Funk example program
#include funk/funk.z80

.funkdef "Hello", app
.start
   clear
   textonbuf.on
   text   20, 30, "Hello Funk,"
   text   30, 33, "you rock!"
   textonbuf.off
   update
   wait
   quit
.data
.valid

That was easy, wasn't it?

The usage of clear and update then automatically includes the fastcopy and clear screen functions from the funk library. Lazy inclusion that is.

Also there would be plenty of handy functions/macros like: pen 30, 33 that does the pencol/currow stuff that I always mix up.
Offline
User avatar

Buckeye Dude

Staff Member

Re: Funk Library

Post04 December 2011, 23:09

ChickenDude you're thinking of something similar to z80ext.inc right? http://wabbit.codeplex.com/wikipage?tit ... x_x_z80ext
I agree there's always going to be some bloat in your code if you're using macros, but one of the most important things is that your code is understandable. How many times have you gone back and said, "shit I have no clue what I was doing here", because either you didn't comment it or you just wrote bad code. If you're code looked like Nano's example, you'd have much less of that issue. As assembly programmers we'd look at the following code
Code: Select all
ld hl,$0505
ld (CurRow),hl
ld hl,String
bcall(_PutS)
ld hl,$0506
ld (CurRow),hl
bcall(_PutS)
 

and immediately jump on the fact that you just need to update CurRow not CurCol as well the second time. How much are we really wasting? 2 bytes and 6 clocks. Seems like a good optimization, except nobody but you would know the difference. One of the biggest issues with all products is that they never get finished. If you can get rid of some of the stumbling points that keeps people from finishing and let them focus on real code, I say all the better.
Now the problem I think you'll run into is that for simple stuff macros do really well, but trying to find a balance between readable and huge code is tough. I'll be interested to see what you have coming, and how you'll balance it
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post05 December 2011, 09:05

Also, as you showed in your example, you didn't reload a new string the second time around. With a macro, you would surely do that, which is another 3 bytes. And _PutS updates CurRow/CurCol, so you may not even need to update it, or just add a space (ld hl,CurRow / inc (hl)). Though for a text routine, speed is almost never an issue. So, for those small annoying things like flags or even displaying text, i think it could be a fun tool, especially for new programmers. Personally, i really like how my raw unorganized asm code looks. If i don't understand how something works, it's always because i'm too lazy to sit down and actually look at it.

And i was talking about this program here:
http://www.ticalc.org/archives/files/fi ... 13847.html

Btw, i wonder if Alex Roper is somehow related to Kerey Roper?
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post05 December 2011, 11:53

I think when games made with AXE (like this one: http://tiforge.info/zcontest3/?a=projet&id=71) take 20kB of RAM, and everybody tends to use the TI84+ with plenty of user space, then saving 3 bytes isn't much of a deal. And when it saves 300 bytes, and when it saves 30000 bytes, it's doesn't really matter. Jim e was actually one of those who even unrolled loops to save a few cycles. But I must admit that writing good AND short code is a challenge that is, with today's resources, often sidestepped.

I could have problems "reassembling" this code into pure asm:
Code: Select all
Fight.Draw.health
; displays health for current player
      call  Player.getHealth
      pen   22, 41
      disp2
      putS  23, 49, 5, heart
      ret

ok, let's start:
Code: Select all
Fight.Draw.health
; displays health for current player
      call  Player.getHealth
      ;pen  22, 41
      ; okay, don't overwrite A
      ; how do you put two values into one register again?
      ld    de, 22 * 256 + 41
      ; was it penCol or penRow?
      ld    (penCol), de
      ;disp2
      ; okay just replace this
      call  disp_2
      ;putS 23, 49, 5, heart
      ; hmm look at the sprite routine source code to find input documentation
      ld    l, 23
      ld    a, 49
      ld    b, 5
      ld    ix, heart
      call  smallSprite
      ret

It took a while to look everything up and it didn't even save a BIT (okay, contrived example :) )

Macros are encapsulated, tested, documented. You can't forget parameters. They are extensible and easy to use.
chickendude wrote:If i don't understand how something works, it's always because i'm too lazy to sit down and actually look at it.

"If it's hard to code it should be hard to read." is C-mentality that I rather discourage from. Of course, demanding the opposite (the object oriented approach) would be too much for assembler, sure. If you manage to write good code, you are good. No problems :) .

As a programmer it becomes hard to put oneself in a noob's position. But I think a hello world program like mine is less daunting. Everything, even registers, can be explained later. You understand it by looking at it.
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post03 October 2012, 18:28

I really like your coding style, NanoWar, it's very fluid and easy to follow. Now that my projects are getting larger i'm getting lazier ;) Have you done any more with the Funk Library?
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post04 October 2012, 19:17

I'm not on my coding PC right now so I can't give you an exact update, but I worked on this for quite a bit. Mostly for better code readability and for easy program setup. I can send you the latest version later. Im curious if it works for you (you need the latest SPASM, too)!
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post04 October 2012, 21:20

That would be cool. What's the latest spasm? I just installed "spasm v2" last year i think, i'm not sure when it was updated last. I'll redownload it from codeplex just in case.
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post05 October 2012, 12:48

Here you go, the Hello.z80 file shows random usages. There is no documentation right now...
Attachments
FunkLibrary-0.2.zip
Funk Library 0.2
(169.2 KiB) Downloaded 360 times
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post07 October 2012, 03:42

Awesome, thanks, i'm gonna play with it tonight.
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post15 October 2012, 10:12

Sorry I've been quite busy the last weeks. I'm moving and a new chapter of university starts today.

How did the library work out for you?
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post16 October 2012, 09:10

I haven't actually looked through it yet, just played with the little test file. Actually, let me see what i can do with it now :D

EDIT: I just tried it out but i get a bunch of errors, maybe it's 'cuz i don't have the latest version of spasm? I'll try reinstalling spasm and see if that clears things up.
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post17 October 2012, 14:33

I expected that :-/ Please post the errors!
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post17 October 2012, 18:41

I'm just trying to compile the hello.z80 sample program, here're the errors:
Pass one...
hello.z80:13: error: Can't recognize 'clr' as an instruction or macro
hello.z80:13: error: Can't recognize 'wr' as an instruction or macro
hello.z80:13: error: Can't recognize 'run' as an instruction or macro
hello.z80:15: error: Can't recognize 'clr' as an instruction or macro
hello.z80:15: error: Can't recognize 'wr' as an instruction or macro
hello.z80:15: error: Can't recognize 'run' as an instruction or macro
hello.z80:16: error: Can't recognize 'clr' as an instruction or macro
hello.z80:16: error: Can't recognize 'wr' as an instruction or macro
hello.z80:16: error: Can't recognize 'run' as an instruction or macro
hello.z80:20: error: Can't recognize 'clr' as an instruction or macro
hello.z80:20: error: Can't recognize 'wr' as an instruction or macro
hello.z80:20: error: Can't recognize 'run' as an instruction or macro
hello.z80:21: error: Can't recognize 'clr' as an instruction or macro
hello.z80:21: error: Can't recognize 'wr' as an instruction or macro
hello.z80:21: error: Can't recognize 'run' as an instruction or macro
hello.z80:22: error: Can't recognize 'clr' as an instruction or macro
hello.z80:22: error: Can't recognize 'wr' as an instruction or macro
hello.z80:22: error: Can't recognize 'run' as an instruction or macro
hello.z80:23: error: Can't recognize 'clr' as an instruction or macro
hello.z80:23: error: Can't recognize 'wr' as an instruction or macro
hello.z80:23: error: Can't recognize 'run' as an instruction or macro
hello.z80:25: error: 'DEF' isn't a macro or label
hello.z80:26: error: 'mod.DEF2' isn't a macro or label
hello.z80:27: error: 'lol' isn't a macro or label
hello.z80:28: error: 'mod.lol' isn't a macro or label
hello.z80:29: error: 'free' isn't a macro or label
hello.z80:30: error: 'mod.free' isn't a macro or label
hello.z80:31: error: 'mod.free2' isn't a macro or label
hello.z80:36: error: Can't recognize 'clr' as an instruction or macro
hello.z80:36: error: Can't recognize 'wr' as an instruction or macro
hello.z80:36: error: Can't recognize 'run' as an instruction or macro
hello.z80:37: error: Can't recognize 'clr' as an instruction or macro
hello.z80:37: error: Can't recognize 'wr' as an instruction or macro
hello.z80:37: error: Can't recognize 'run' as an instruction or macro
hello.z80:38: error: Can't recognize 'clr' as an instruction or macro
hello.z80:38: error: Can't recognize 'wr' as an instruction or macro
hello.z80:38: error: Can't recognize 'run' as an instruction or macro
hello.z80:39: error: Can't recognize 'clr' as an instruction or macro
hello.z80:39: error: Can't recognize 'wr' as an instruction or macro
hello.z80:39: error: Can't recognize 'run' as an instruction or macro
hello.z80:43: error: Can't recognize 'clr' as an instruction or macro
hello.z80:43: error: Can't recognize 'wr' as an instruction or macro
hello.z80:43: error: Can't recognize 'run' as an instruction or macro
hello.z80:44: error: Can't recognize 'clr' as an instruction or macro
hello.z80:44: error: Can't recognize 'wr' as an instruction or macro
hello.z80:44: error: Can't recognize 'run' as an instruction or macro
hello.z80:45: error: Can't recognize 'clr' as an instruction or macro
hello.z80:45: error: Can't recognize 'wr' as an instruction or macro
hello.z80:45: error: Can't recognize 'run' as an instruction or macro
hello.z80:47: error: 'mod.ZERO' isn't a macro or label
hello.z80:48: error: 'mod.TWO' isn't a macro or label
hello.z80:50: error: 'FIVE' isn't a macro or label
hello.z80:51: error: 'SIX' isn't a macro or label
hello.z80:52: error: 'ENUMEND' isn't a macro or label
hello.z80:58: error: Can't recognize 'clr' as an instruction or macro
hello.z80:58: error: Can't recognize 'wr' as an instruction or macro
hello.z80:58: error: Can't recognize 'run' as an instruction or macro
hello.z80:59: error: Can't recognize 'clr' as an instruction or macro
hello.z80:59: error: Can't recognize 'wr' as an instruction or macro
hello.z80:59: error: Can't recognize 'run' as an instruction or macro
Pass two...
hello.z80:58: error: '_text_label' isn't a macro or label
hello.z80:59: error: '_text_label' isn't a macro or label
# Compiling "Hello" (ION) for TI83+/TI84+(SE)...
# Program size: 150 bytes

Re: Funk Library

Post21 October 2012, 07:33

So it's kinda like a language built on top of an assembler?

Looks like a big time-saver. I'll have to try it out sometime.

And thumbs up :)
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post22 October 2012, 07:06

Yeah, kinda. I think it's basically a way to make your source much neater providing macros for very common routines. It's not a full language per se, but definitely a cool way be able to make cool programs while you ease yourself into assembly, i think. Reading through the Robot War sources has made me start thinking about how to organize larger programs and how saving a few clocks in most places really isn't going to make that much of a different. Sure, if the code is just plain bloated you will start to notice, but clean organized code can be really nice to work with :D My code is still very messy and a lot of times i even avoid making routines into functions and just in-lining them to avoid wasting 17 clocks/3 bytes on a call :P (even though it really does look much neater).
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post22 October 2012, 13:07

CLR and WR are built-in macros of SPASM. Are you sure that you have the latest version installed? :/
Offline
User avatar

chickendude

Staff Member

Re: Funk Library

Post22 October 2012, 16:00

If the dates at the Codeplex set are right, i should have the latest version. The Linux version dates back to 2011, or is there a newer version?
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post22 October 2012, 21:41

Hrm hrm, maybe Spencer built that in for me in the Linux version :-J

Re: Funk Library

Post23 October 2012, 00:52

chickendude wrote:If the dates at the Codeplex set are right, i should have the latest version. The Linux version dates back to 2011, or is there a newer version?
Would this help? http://wabbit.codeplex.com/wikipage?tit ... _x_fcreate
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post23 October 2012, 18:24

fcreate.inc functions are now built into SPASM2. This is left for educational purposes only.

But yeah, that include file implements WR and CLR. So you could use that, but it will probably be much slower.
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post23 October 2012, 23:53

I tried the Win32 version and it worked. See the screenshot...
Attachments
spasm-likes-funk.png
Works on XP at least ;)
spasm-likes-funk.png (178.74 KiB) Viewed 30821 times
Offline
User avatar

NanoWar

Site Admin

Topic Starter

Re: Funk Library

Post21 August 2013, 15:30

New release and new hello world example:

Code: Select all
#include funk/funk.z80
.funk "Hello"
     
      clear
      textonbuf.on
      text  24, 30, "Hello Funk,"
      text  30, 33, "you rock!"
      textonbuf.off
      update
      anykey
      quit

.funkend


Easy APP macros:
Code: Select all
#include funk/funk.z80
.funk "TestApp", app
     
      pcal  call_on_page2
     
      quit

      .nextpage

call_on_page2
      ret

.funkend


I also added a macro to finally use Spencer's awesome bitmap headers:
Code: Select all
; draw a sprite at (10, 0)
 sprite 10, 0, gfx.player
 ; ...
#include sprites.z80


And in sprites.z80 you then have:
Code: Select all
; The exact spaces (or lack of) around the "=" signs matter, so do not reformat the two lines below.
 .option bm_hdr = TRUE
 .option bm_hdr_fmt="W,H"
gfx.player
#include graphics/player.bmp
; ...
 .option bm_hdr = FALSE
 

The autosprite routine then figures out what size your sprite has (two bytes per sprite) and calls the right routines, depending on the width: smallsprite or largesprite accordingly.

I have a subfolder graphics/autoSprites/ and a shell script in graphics/that automatically generates the sprites.z80 file:

Code: Select all
#!/bin/sh

cd ./graphics/

cd ./autoSprites
imageList=$(ls *.bmp)
cd ..

echo "; * auto sprites macro\n" > autoSprites.z80

echo " .option bm_hdr = TRUE" >> autoSprites.z80
echo " .option bm_hdr_fmt=\"W,H\"\n" >> autoSprites.z80

for image in $imageList ; do
      echo "gfx.${image%.bmp}" >> autoSprites.z80
      echo "#include \"graphics/autoSprites/${image}\"\n" >> autoSprites.z80
done

echo " .option bm_hdr = FALSE" >> autoSprites.z80

cd ..
 


The library lazy includes all functions, so code stays small!

Funk Library now uses (slightly modded) Brandon W's include files for ti83 and ti83+ (see funk/includes/) for use with SPASM.

More features:

Enums with modules:

Code: Select all
;
 .module "MyEnum"
 .enumStart ; defaults to "0"
 .enum "ZERO" ; 0
 .enum "ONE" ; 1
 .enum "TWO"
 .enum "THREE"
 .enumEnd 9, END; optional, to check maximum size. Throws error on overflow (that would be 10).

 .echo MyEnum.TWO ; echos "2" in console on spasm execution
 .echo MyEnum.END ; echos "3"
 


and more stuff I often use... e.g. dereference macro "deref"

Code: Select all
;
 ld hl, lookup
 deref
 ; hl = label
 ; (hl) = $AB

lookup
 .dw label
label
 .db $AB
 


it calls a function that does:
Code: Select all
;
funk_deReference
      push  af
            ld    a, (hl)
            inc   hl
            ld    h, (hl)
            ld    l, a
      pop   af
      ret
 


or .str for strings
and a "char" macro that converts a number 0 - 15 to the according hex char.

Code: Select all
#define SPELLDMG 3
; stores string: "Deals 3 damage."
 .str "Deals ", char(SPELLDMG), " damage."
 


Yeah, it inserts a "$00" at the end, I always forget it... and it looks cleaner.

:D
Attachments
FunkLibrary-0.3.zip
(64.96 KiB) Downloaded 279 times
Next

Return to WabbitStudio

Who is online

Users browsing this forum: No registered users and 1 guest

cron